From 1817dbc798cf3a919907546e4f64d63dbc20ca4d Mon Sep 17 00:00:00 2001 From: Gabor Javorszky Date: Thu, 24 Oct 2024 15:53:04 +0100 Subject: [PATCH] Clang format with custom --- src/java/nxt_jni.c | 60 +- src/java/nxt_jni.h | 46 +- src/java/nxt_jni_Context.c | 78 +- src/java/nxt_jni_Context.h | 17 +- src/java/nxt_jni_HeaderNamesEnumeration.c | 76 +- src/java/nxt_jni_HeaderNamesEnumeration.h | 9 +- src/java/nxt_jni_HeadersEnumeration.c | 71 +- src/java/nxt_jni_HeadersEnumeration.h | 9 +- src/java/nxt_jni_InputStream.c | 117 +- src/java/nxt_jni_InputStream.h | 6 +- src/java/nxt_jni_OutputStream.c | 110 +- src/java/nxt_jni_OutputStream.h | 9 +- src/java/nxt_jni_Request.c | 564 ++-- src/java/nxt_jni_Request.h | 15 +- src/java/nxt_jni_Response.c | 642 ++-- src/java/nxt_jni_Response.h | 9 +- src/java/nxt_jni_Thread.c | 33 +- src/java/nxt_jni_Thread.h | 13 +- src/java/nxt_jni_URLClassLoader.c | 80 +- src/java/nxt_jni_URLClassLoader.h | 24 +- src/nodejs/unit-http/nxt_napi.h | 509 +-- src/nodejs/unit-http/unit.h | 48 +- src/nxt_aix_send_file.c | 54 +- src/nxt_app_log.c | 57 +- src/nxt_app_nncq.h | 72 +- src/nxt_app_queue.h | 60 +- src/nxt_application.c | 647 ++-- src/nxt_application.h | 178 +- src/nxt_array.c | 50 +- src/nxt_array.h | 48 +- src/nxt_atomic.h | 33 +- src/nxt_buf.c | 162 +- src/nxt_buf.h | 210 +- src/nxt_buf_pool.c | 52 +- src/nxt_buf_pool.h | 55 +- src/nxt_capability.c | 38 +- src/nxt_capability.h | 9 +- src/nxt_cert.c | 468 ++- src/nxt_cert.h | 42 +- src/nxt_cgroup.c | 68 +- src/nxt_cgroup.h | 6 +- src/nxt_clang.h | 121 +- src/nxt_clone.c | 207 +- src/nxt_clone.h | 32 +- src/nxt_conf.c | 856 ++--- src/nxt_conf.h | 182 +- src/nxt_conf_validation.c | 3551 ++++++++++---------- src/nxt_conn.c | 76 +- src/nxt_conn.h | 448 +-- src/nxt_conn_accept.c | 217 +- src/nxt_conn_close.c | 90 +- src/nxt_conn_connect.c | 72 +- src/nxt_conn_proxy.c | 570 ++-- src/nxt_conn_read.c | 97 +- src/nxt_conn_write.c | 188 +- src/nxt_controller.c | 1189 +++---- src/nxt_credential.c | 103 +- src/nxt_credential.h | 22 +- src/nxt_cyassl.c | 293 +- src/nxt_devpoll_engine.c | 331 +- src/nxt_djb_hash.c | 19 +- src/nxt_djb_hash.h | 11 +- src/nxt_dyld.c | 27 +- src/nxt_dyld.h | 21 +- src/nxt_epoll_engine.c | 497 ++- src/nxt_errno.c | 57 +- src/nxt_errno.h | 114 +- src/nxt_event_conn_job_sendfile.c | 119 +- src/nxt_event_engine.c | 275 +- src/nxt_event_engine.h | 448 ++- src/nxt_eventport_engine.c | 325 +- src/nxt_external.c | 82 +- src/nxt_fd_event.c | 58 +- src/nxt_fd_event.h | 78 +- src/nxt_fiber.c | 214 +- src/nxt_fiber.h | 66 +- src/nxt_file.c | 282 +- src/nxt_file.h | 199 +- src/nxt_file_event.h | 9 +- src/nxt_file_name.c | 27 +- src/nxt_file_name.h | 6 +- src/nxt_freebsd_sendfile.c | 59 +- src/nxt_fs.c | 35 +- src/nxt_fs.h | 8 +- src/nxt_fs_mount.c | 67 +- src/nxt_fs_mount.h | 44 +- src/nxt_gmtime.c | 23 +- src/nxt_gnutls.c | 371 +-- src/nxt_h1proto.c | 1590 ++++----- src/nxt_h1proto.h | 53 +- src/nxt_h1proto_websocket.c | 393 +-- src/nxt_hash.h | 22 +- src/nxt_hpux_sendfile.c | 67 +- src/nxt_http.h | 607 ++-- src/nxt_http_chunk_parse.c | 76 +- src/nxt_http_error.c | 49 +- src/nxt_http_js.c | 161 +- src/nxt_http_parse.c | 584 ++-- src/nxt_http_parse.h | 147 +- src/nxt_http_proxy.c | 261 +- src/nxt_http_request.c | 585 ++-- src/nxt_http_response.c | 70 +- src/nxt_http_return.c | 82 +- src/nxt_http_rewrite.c | 27 +- src/nxt_http_route.c | 1036 +++--- src/nxt_http_route_addr.c | 80 +- src/nxt_http_route_addr.h | 33 +- src/nxt_http_set_headers.c | 59 +- src/nxt_http_static.c | 525 ++- src/nxt_http_variables.c | 501 ++- src/nxt_http_websocket.c | 74 +- src/nxt_isolation.c | 430 ++- src/nxt_isolation.h | 10 +- src/nxt_java.c | 257 +- src/nxt_job.c | 72 +- src/nxt_job.h | 53 +- src/nxt_job_cache_file.c | 9 +- src/nxt_js.c | 214 +- src/nxt_js.h | 53 +- src/nxt_kqueue_engine.c | 575 ++-- src/nxt_lib.c | 30 +- src/nxt_linux_sendfile.c | 115 +- src/nxt_list.c | 43 +- src/nxt_list.h | 123 +- src/nxt_listen_socket.c | 150 +- src/nxt_listen_socket.h | 48 +- src/nxt_log.c | 60 +- src/nxt_log.h | 172 +- src/nxt_log_moderation.c | 45 +- src/nxt_log_moderation.h | 52 +- src/nxt_lvlhsh.c | 441 +-- src/nxt_lvlhsh.h | 138 +- src/nxt_macosx_sendfile.c | 69 +- src/nxt_main.c | 11 +- src/nxt_main.h | 61 +- src/nxt_main_process.c | 711 ++-- src/nxt_main_process.h | 22 +- src/nxt_malloc.c | 88 +- src/nxt_malloc.h | 40 +- src/nxt_mem_map.c | 19 +- src/nxt_mem_map.h | 36 +- src/nxt_mem_zone.c | 376 +-- src/nxt_mem_zone.h | 23 +- src/nxt_mp.c | 409 +-- src/nxt_mp.h | 71 +- src/nxt_murmur_hash.c | 25 +- src/nxt_murmur_hash.h | 7 +- src/nxt_nncq.h | 69 +- src/nxt_nvbcq.h | 61 +- src/nxt_openssl.c | 803 +++-- src/nxt_parse.c | 83 +- src/nxt_parse.h | 26 +- src/nxt_pcre.c | 68 +- src/nxt_pcre2.c | 73 +- src/nxt_php_sapi.c | 686 ++-- src/nxt_polarssl.c | 50 +- src/nxt_poll_engine.c | 342 +- src/nxt_pollset_engine.c | 342 +- src/nxt_port.c | 318 +- src/nxt_port.h | 491 +-- src/nxt_port_hash.c | 80 +- src/nxt_port_hash.h | 13 +- src/nxt_port_memory.c | 348 +- src/nxt_port_memory.h | 17 +- src/nxt_port_memory_int.h | 103 +- src/nxt_port_queue.h | 37 +- src/nxt_port_rpc.c | 260 +- src/nxt_port_rpc.h | 32 +- src/nxt_port_socket.c | 617 ++-- src/nxt_process.c | 478 ++- src/nxt_process.h | 242 +- src/nxt_process_title.c | 73 +- src/nxt_process_type.h | 2 - src/nxt_queue.c | 24 +- src/nxt_queue.h | 194 +- src/nxt_random.c | 74 +- src/nxt_random.h | 20 +- src/nxt_rbtree.c | 201 +- src/nxt_rbtree.h | 89 +- src/nxt_recvbuf.c | 32 +- src/nxt_recvbuf.h | 17 +- src/nxt_regex.h | 33 +- src/nxt_router.c | 2589 +++++++------- src/nxt_router.h | 300 +- src/nxt_router_access_log.c | 218 +- src/nxt_router_request.h | 24 +- src/nxt_runtime.c | 832 +++-- src/nxt_runtime.h | 211 +- src/nxt_script.c | 288 +- src/nxt_script.h | 56 +- src/nxt_select_engine.c | 176 +- src/nxt_semaphore.c | 65 +- src/nxt_semaphore.h | 21 +- src/nxt_sendbuf.c | 165 +- src/nxt_sendbuf.h | 110 +- src/nxt_service.c | 72 +- src/nxt_service.h | 23 +- src/nxt_sha1.c | 126 +- src/nxt_sha1.h | 18 +- src/nxt_signal.c | 51 +- src/nxt_signal.h | 41 +- src/nxt_signal_handlers.c | 42 +- src/nxt_sockaddr.c | 335 +- src/nxt_sockaddr.h | 102 +- src/nxt_socket.c | 130 +- src/nxt_socket.h | 95 +- src/nxt_socket_msg.c | 38 +- src/nxt_socket_msg.h | 100 +- src/nxt_socketpair.c | 62 +- src/nxt_solaris_sendfilev.c | 76 +- src/nxt_sort.h | 3 +- src/nxt_source.h | 35 +- src/nxt_spinlock.c | 26 +- src/nxt_spinlock.h | 15 +- src/nxt_sprintf.c | 207 +- src/nxt_sprintf.h | 11 +- src/nxt_status.c | 75 +- src/nxt_status.h | 29 +- src/nxt_string.c | 507 ++- src/nxt_string.h | 172 +- src/nxt_test_build.c | 73 +- src/nxt_test_build.h | 281 +- src/nxt_thread.c | 83 +- src/nxt_thread.h | 121 +- src/nxt_thread_cond.c | 45 +- src/nxt_thread_id.h | 66 +- src/nxt_thread_log.h | 45 +- src/nxt_thread_mutex.c | 36 +- src/nxt_thread_pool.c | 132 +- src/nxt_thread_pool.h | 43 +- src/nxt_thread_time.c | 158 +- src/nxt_thread_time.h | 82 +- src/nxt_time.c | 96 +- src/nxt_time.h | 52 +- src/nxt_time_parse.c | 108 +- src/nxt_timer.c | 159 +- src/nxt_timer.h | 88 +- src/nxt_tls.h | 87 +- src/nxt_tstr.c | 108 +- src/nxt_tstr.h | 76 +- src/nxt_types.h | 105 +- src/nxt_unicode_lowcase.h | 3704 ++++++++++++++++++--- src/nxt_unicode_macosx_lowcase.h | 2126 ++++++++++-- src/nxt_unit.c | 3219 ++++++++---------- src/nxt_unit.h | 329 +- src/nxt_unit_field.h | 18 +- src/nxt_unit_request.h | 70 +- src/nxt_unit_response.h | 13 +- src/nxt_unit_sptr.h | 14 +- src/nxt_unit_typedefs.h | 29 +- src/nxt_unit_websocket.h | 14 +- src/nxt_unix.h | 74 +- src/nxt_upstream.c | 52 +- src/nxt_upstream.h | 62 +- src/nxt_upstream_round_robin.c | 100 +- src/nxt_utf8.c | 79 +- src/nxt_utf8.h | 37 +- src/nxt_var.c | 237 +- src/nxt_var.h | 71 +- src/nxt_websocket.c | 53 +- src/nxt_websocket.h | 17 +- src/nxt_websocket_accept.c | 27 +- src/nxt_websocket_header.h | 52 +- src/nxt_work_queue.c | 97 +- src/nxt_work_queue.h | 123 +- src/perl/nxt_perl_psgi.c | 700 ++-- src/perl/nxt_perl_psgi_layer.c | 229 +- src/perl/nxt_perl_psgi_layer.h | 32 +- src/python/nxt_python.c | 262 +- src/python/nxt_python.h | 82 +- src/python/nxt_python_asgi.c | 548 ++- src/python/nxt_python_asgi.h | 110 +- src/python/nxt_python_asgi_http.c | 307 +- src/python/nxt_python_asgi_lifespan.c | 334 +- src/python/nxt_python_asgi_str.c | 227 +- src/python/nxt_python_asgi_str.h | 117 +- src/python/nxt_python_asgi_websocket.c | 476 ++- src/python/nxt_python_wsgi.c | 677 ++-- src/ruby/nxt_ruby.c | 825 +++-- src/ruby/nxt_ruby.h | 13 +- src/ruby/nxt_ruby_stream_io.c | 115 +- src/test/nxt_base64_test.c | 96 +- src/test/nxt_clone_test.c | 483 +-- src/test/nxt_cq_test.c | 241 +- src/test/nxt_gmtime_test.c | 34 +- src/test/nxt_http_parse_test.c | 901 ++--- src/test/nxt_lvlhsh_test.c | 113 +- src/test/nxt_malloc_test.c | 37 +- src/test/nxt_mem_zone_test.c | 20 +- src/test/nxt_mp_test.c | 40 +- src/test/nxt_msec_diff_test.c | 39 +- src/test/nxt_rbtree1.c | 116 +- src/test/nxt_rbtree1.h | 76 +- src/test/nxt_rbtree1_test.c | 130 +- src/test/nxt_rbtree_test.c | 105 +- src/test/nxt_sprintf_test.c | 48 +- src/test/nxt_strverscmp_test.c | 79 +- src/test/nxt_term_parse_test.c | 55 +- src/test/nxt_tests.c | 24 +- src/test/nxt_tests.h | 80 +- src/test/nxt_unit_app_test.c | 178 +- src/test/nxt_unit_websocket_chat.c | 349 +- src/test/nxt_unit_websocket_echo.c | 33 +- src/test/nxt_utf8_file_name_test.c | 49 +- src/test/nxt_utf8_test.c | 136 +- src/wasm/nxt_rt_wasmtime.c | 279 +- src/wasm/nxt_wasm.c | 169 +- src/wasm/nxt_wasm.h | 143 +- 308 files changed, 31889 insertions(+), 32903 deletions(-) diff --git a/src/java/nxt_jni.c b/src/java/nxt_jni.c index 02ec1e376..b0bcdf589 100644 --- a/src/java/nxt_jni.c +++ b/src/java/nxt_jni.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,19 +11,18 @@ #include "nxt_jni.h" -static jclass nxt_java_NoSuchElementException_class; -static jclass nxt_java_IOException_class; -static jclass nxt_java_IllegalStateException_class; -static jclass nxt_java_File_class; -static jmethodID nxt_java_File_ctor; - -static inline char nxt_java_lowcase(char c); +static jclass nxt_java_NoSuchElementException_class; +static jclass nxt_java_IOException_class; +static jclass nxt_java_IllegalStateException_class; +static jclass nxt_java_File_class; +static jmethodID nxt_java_File_ctor; +static inline char +nxt_java_lowcase(char c); int -nxt_java_jni_init(JNIEnv *env) -{ - jclass cls; +nxt_java_jni_init(JNIEnv *env) { + jclass cls; cls = (*env)->FindClass(env, "java/util/NoSuchElementException"); if (cls == NULL) { @@ -69,7 +67,7 @@ nxt_java_jni_init(JNIEnv *env) nxt_java_File_ctor = (*env)->GetMethodID(env, nxt_java_File_class, "", - "(Ljava/lang/String;)V"); + "(Ljava/lang/String;)V"); if (nxt_java_File_ctor == NULL) { (*env)->DeleteGlobalRef(env, nxt_java_NoSuchElementException_class); (*env)->DeleteGlobalRef(env, nxt_java_IOException_class); @@ -81,35 +79,27 @@ nxt_java_jni_init(JNIEnv *env) return NXT_UNIT_OK; } - void -nxt_java_throw_NoSuchElementException(JNIEnv *env, const char *msg) -{ +nxt_java_throw_NoSuchElementException(JNIEnv *env, const char *msg) { (*env)->ThrowNew(env, nxt_java_NoSuchElementException_class, msg); } - void -nxt_java_throw_IOException(JNIEnv *env, const char *msg) -{ +nxt_java_throw_IOException(JNIEnv *env, const char *msg) { (*env)->ThrowNew(env, nxt_java_IOException_class, msg); } - void -nxt_java_throw_IllegalStateException(JNIEnv *env, const char *msg) -{ +nxt_java_throw_IllegalStateException(JNIEnv *env, const char *msg) { (*env)->ThrowNew(env, nxt_java_IllegalStateException_class, msg); } - nxt_unit_field_t * nxt_java_findHeader(nxt_unit_field_t *f, nxt_unit_field_t *end, - const char *name, uint8_t name_len) -{ - const char *field_name; + const char *name, uint8_t name_len) { + const char *field_name; - for (/* void */ ; f < end; f++) { + for (/* void */; f < end; f++) { if (f->skip != 0 || f->name_length != name_len) { continue; } @@ -124,12 +114,10 @@ nxt_java_findHeader(nxt_unit_field_t *f, nxt_unit_field_t *end, return NULL; } - int -nxt_java_strcaseeq(const char *str1, const char *str2, int len) -{ +nxt_java_strcaseeq(const char *str1, const char *str2, int len) { char c1, c2; - const char *end1; + const char *end1; end1 = str1 + len; @@ -145,19 +133,15 @@ nxt_java_strcaseeq(const char *str1, const char *str2, int len) return 1; } - static inline char -nxt_java_lowcase(char c) -{ +nxt_java_lowcase(char c) { return (c >= 'A' && c <= 'Z') ? c | 0x20 : c; } - jstring -nxt_java_newString(JNIEnv *env, char *str, uint32_t len) -{ - char tmp; - jstring res; +nxt_java_newString(JNIEnv *env, char *str, uint32_t len) { + char tmp; + jstring res; tmp = str[len]; diff --git a/src/java/nxt_jni.h b/src/java/nxt_jni.h index 62acb7522..16d012112 100644 --- a/src/java/nxt_jni.h +++ b/src/java/nxt_jni.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,45 +10,48 @@ #include -int nxt_java_jni_init(JNIEnv *env); - -void nxt_java_throw_NoSuchElementException(JNIEnv *env, const char *msg); +int +nxt_java_jni_init(JNIEnv *env); -void nxt_java_throw_IOException(JNIEnv *env, const char *msg); +void +nxt_java_throw_NoSuchElementException(JNIEnv *env, const char *msg); -void nxt_java_throw_IllegalStateException(JNIEnv *env, const char *msg); +void +nxt_java_throw_IOException(JNIEnv *env, const char *msg); -nxt_unit_field_t *nxt_java_findHeader(nxt_unit_field_t *f, nxt_unit_field_t *e, - const char *name, uint8_t name_len); +void +nxt_java_throw_IllegalStateException(JNIEnv *env, const char *msg); -int nxt_java_strcaseeq(const char *str1, const char *str2, int len); +nxt_unit_field_t * +nxt_java_findHeader(nxt_unit_field_t *f, nxt_unit_field_t *e, const char *name, + uint8_t name_len); -jstring nxt_java_newString(JNIEnv *env, char *str, uint32_t len); +int +nxt_java_strcaseeq(const char *str1, const char *str2, int len); +jstring +nxt_java_newString(JNIEnv *env, char *str, uint32_t len); typedef struct { - uint32_t header_size; - uint32_t buf_size; + uint32_t header_size; + uint32_t buf_size; - jobject jreq; - jobject jresp; + jobject jreq; + jobject jresp; - nxt_unit_buf_t *first; - nxt_unit_buf_t *buf; + nxt_unit_buf_t *first; + nxt_unit_buf_t *buf; } nxt_java_request_data_t; - static inline jlong -nxt_ptr2jlong(void *ptr) -{ +nxt_ptr2jlong(void *ptr) { return (jlong) (intptr_t) ptr; } static inline void * -nxt_jlong2ptr(jlong l) -{ +nxt_jlong2ptr(jlong l) { return (void *) (intptr_t) l; } -#endif /* _NXT_JAVA_JNI_H_INCLUDED_ */ +#endif /* _NXT_JAVA_JNI_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_Context.c b/src/java/nxt_jni_Context.c index 589e1c5bf..a81a7f7f7 100644 --- a/src/java/nxt_jni_Context.c +++ b/src/java/nxt_jni_Context.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -13,22 +12,22 @@ #include "nxt_jni_URLClassLoader.h" -static jclass nxt_java_Context_class; -static jmethodID nxt_java_Context_start; -static jmethodID nxt_java_Context_service; -static jmethodID nxt_java_Context_stop; - -static void JNICALL nxt_java_Context_log(JNIEnv *env, jclass cls, - jlong ctx_ptr, jstring msg, jint msg_len); -static void JNICALL nxt_java_Context_trace(JNIEnv *env, jclass cls, - jlong ctx_ptr, jstring msg, jint msg_len); +static jclass nxt_java_Context_class; +static jmethodID nxt_java_Context_start; +static jmethodID nxt_java_Context_service; +static jmethodID nxt_java_Context_stop; +static void JNICALL +nxt_java_Context_log(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg, + jint msg_len); +static void JNICALL +nxt_java_Context_trace(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg, + jint msg_len); int -nxt_java_initContext(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initContext(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.Context"); if (cls == NULL) { @@ -41,14 +40,14 @@ nxt_java_initContext(JNIEnv *env, jobject cl) cls = nxt_java_Context_class; nxt_java_Context_start = (*env)->GetStaticMethodID(env, cls, "start", - "(Ljava/lang/String;[Ljava/net/URL;)Lnginx/unit/Context;"); + "(Ljava/lang/String;[Ljava/net/URL;)Lnginx/unit/Context;"); if (nxt_java_Context_start == NULL) { nxt_unit_warn(NULL, "nginx.unit.Context.start() not found"); goto failed; } nxt_java_Context_service = (*env)->GetMethodID(env, cls, "service", - "(Lnginx/unit/Request;Lnginx/unit/Response;)V"); + "(Lnginx/unit/Request;Lnginx/unit/Response;)V"); if (nxt_java_Context_service == NULL) { nxt_unit_warn(NULL, "nginx.unit.Context.service() not found"); goto failed; @@ -61,20 +60,16 @@ nxt_java_initContext(JNIEnv *env, jobject cl) } JNINativeMethod context_methods[] = { - { (char *) "log", - (char *) "(JLjava/lang/String;I)V", - nxt_java_Context_log }, + {(char *) "log", (char *) "(JLjava/lang/String;I)V", + nxt_java_Context_log}, - { (char *) "trace", - (char *) "(JLjava/lang/String;I)V", - nxt_java_Context_trace }, + {(char *) "trace", (char *) "(JLjava/lang/String;I)V", + nxt_java_Context_trace}, }; - res = (*env)->RegisterNatives(env, nxt_java_Context_class, - context_methods, - sizeof(context_methods) - / sizeof(context_methods[0])); + res = (*env)->RegisterNatives(env, nxt_java_Context_class, context_methods, + sizeof(context_methods) / sizeof(context_methods[0])); nxt_unit_debug(NULL, "registered Context methods: %d", res); @@ -91,10 +86,8 @@ nxt_java_initContext(JNIEnv *env, jobject cl) return NXT_UNIT_ERROR; } - jobject -nxt_java_startContext(JNIEnv *env, const char *webapp, jobject classpaths) -{ +nxt_java_startContext(JNIEnv *env, const char *webapp, jobject classpaths) { jstring webapp_str; webapp_str = (*env)->NewStringUTF(env, webapp); @@ -103,31 +96,24 @@ nxt_java_startContext(JNIEnv *env, const char *webapp, jobject classpaths) } return (*env)->CallStaticObjectMethod(env, nxt_java_Context_class, - nxt_java_Context_start, webapp_str, - classpaths); + nxt_java_Context_start, webapp_str, classpaths); } - void -nxt_java_service(JNIEnv *env, jobject ctx, jobject jreq, jobject jresp) -{ +nxt_java_service(JNIEnv *env, jobject ctx, jobject jreq, jobject jresp) { (*env)->CallVoidMethod(env, ctx, nxt_java_Context_service, jreq, jresp); } - void -nxt_java_stopContext(JNIEnv *env, jobject ctx) -{ +nxt_java_stopContext(JNIEnv *env, jobject ctx) { (*env)->CallVoidMethod(env, ctx, nxt_java_Context_stop); } - static void JNICALL nxt_java_Context_log(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg, - jint msg_len) -{ - const char *msg_str; - nxt_unit_ctx_t *ctx; + jint msg_len) { + const char *msg_str; + nxt_unit_ctx_t *ctx; ctx = nxt_jlong2ptr(ctx_ptr); @@ -141,14 +127,12 @@ nxt_java_Context_log(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg, (*env)->ReleaseStringUTFChars(env, msg, msg_str); } - static void JNICALL nxt_java_Context_trace(JNIEnv *env, jclass cls, jlong ctx_ptr, jstring msg, - jint msg_len) -{ + jint msg_len) { #if (NXT_DEBUG) - const char *msg_str; - nxt_unit_ctx_t *ctx; + const char *msg_str; + nxt_unit_ctx_t *ctx; ctx = nxt_jlong2ptr(ctx_ptr); diff --git a/src/java/nxt_jni_Context.h b/src/java/nxt_jni_Context.h index 976c36cf6..6fe9618e3 100644 --- a/src/java/nxt_jni_Context.h +++ b/src/java/nxt_jni_Context.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -10,14 +9,16 @@ #include -int nxt_java_initContext(JNIEnv *env, jobject cl); - -jobject nxt_java_startContext(JNIEnv *env, const char *webapp, - jobject classpaths); +int +nxt_java_initContext(JNIEnv *env, jobject cl); -void nxt_java_service(JNIEnv *env, jobject ctx, jobject jreq, jobject jresp); +jobject +nxt_java_startContext(JNIEnv *env, const char *webapp, jobject classpaths); -void nxt_java_stopContext(JNIEnv *env, jobject ctx); +void +nxt_java_service(JNIEnv *env, jobject ctx, jobject jreq, jobject jresp); -#endif /* _NXT_JAVA_CONTEXT_H_INCLUDED_ */ +void +nxt_java_stopContext(JNIEnv *env, jobject ctx); +#endif /* _NXT_JAVA_CONTEXT_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_HeaderNamesEnumeration.c b/src/java/nxt_jni_HeaderNamesEnumeration.c index eea0c387e..07298982e 100644 --- a/src/java/nxt_jni_HeaderNamesEnumeration.c +++ b/src/java/nxt_jni_HeaderNamesEnumeration.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -15,21 +14,21 @@ #include "nxt_jni_HeaderNamesEnumeration.h" -static jlong JNICALL nxt_java_HeaderNamesEnumeration_nextElementPos(JNIEnv *env, - jclass cls, jlong headers_ptr, jlong size, jlong pos); -static jstring JNICALL nxt_java_HeaderNamesEnumeration_nextElement(JNIEnv *env, - jclass cls, jlong headers_ptr, jlong size, jlong pos); - +static jlong JNICALL +nxt_java_HeaderNamesEnumeration_nextElementPos(JNIEnv *env, jclass cls, + jlong headers_ptr, jlong size, jlong pos); +static jstring JNICALL +nxt_java_HeaderNamesEnumeration_nextElement(JNIEnv *env, jclass cls, + jlong headers_ptr, jlong size, jlong pos); -static jclass nxt_java_HeaderNamesEnumeration_class; -static jmethodID nxt_java_HeaderNamesEnumeration_ctor; +static jclass nxt_java_HeaderNamesEnumeration_class; +static jmethodID nxt_java_HeaderNamesEnumeration_ctor; int -nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.HeaderNamesEnumeration"); if (cls == NULL) { @@ -40,27 +39,23 @@ nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl) (*env)->DeleteLocalRef(env, cls); cls = nxt_java_HeaderNamesEnumeration_class; - nxt_java_HeaderNamesEnumeration_ctor = (*env)->GetMethodID(env, cls, - "", "(JJ)V"); + nxt_java_HeaderNamesEnumeration_ctor + = (*env)->GetMethodID(env, cls, "", "(JJ)V"); if (nxt_java_HeaderNamesEnumeration_ctor == NULL) { (*env)->DeleteGlobalRef(env, cls); return NXT_UNIT_ERROR; } JNINativeMethod hnenum_methods[] = { - { (char *) "nextElementPos", - (char *) "(JJJ)J", - nxt_java_HeaderNamesEnumeration_nextElementPos }, + {(char *) "nextElementPos", (char *) "(JJJ)J", + nxt_java_HeaderNamesEnumeration_nextElementPos}, - { (char *) "nextElement", - (char *) "(JJJ)Ljava/lang/String;", - nxt_java_HeaderNamesEnumeration_nextElement }, + {(char *) "nextElement", (char *) "(JJJ)Ljava/lang/String;", + nxt_java_HeaderNamesEnumeration_nextElement}, }; res = (*env)->RegisterNatives(env, nxt_java_HeaderNamesEnumeration_class, - hnenum_methods, - sizeof(hnenum_methods) - / sizeof(hnenum_methods[0])); + hnenum_methods, sizeof(hnenum_methods) / sizeof(hnenum_methods[0])); nxt_unit_debug(NULL, "registered HeaderNamesEnumeration methods: %d", res); @@ -72,23 +67,18 @@ nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl) return NXT_UNIT_OK; } - jobject nxt_java_newHeaderNamesEnumeration(JNIEnv *env, nxt_unit_field_t *f, - uint32_t fields_count) -{ - return (*env)->NewObject(env, - nxt_java_HeaderNamesEnumeration_class, + uint32_t fields_count) { + return (*env)->NewObject(env, nxt_java_HeaderNamesEnumeration_class, nxt_java_HeaderNamesEnumeration_ctor, nxt_ptr2jlong(f), (jlong) fields_count); } - static jlong JNICALL nxt_java_HeaderNamesEnumeration_nextElementPos(JNIEnv *env, jclass cls, - jlong headers_ptr, jlong size, jlong pos) -{ - nxt_unit_field_t *f; + jlong headers_ptr, jlong size, jlong pos) { + nxt_unit_field_t *f; f = nxt_jlong2ptr(headers_ptr); @@ -97,10 +87,8 @@ nxt_java_HeaderNamesEnumeration_nextElementPos(JNIEnv *env, jclass cls, } if (pos > 0) { - while (pos < size - && f[pos].hash == f[pos - 1].hash - && f[pos].name_length == f[pos - 1].name_length) - { + while (pos < size && f[pos].hash == f[pos - 1].hash + && f[pos].name_length == f[pos - 1].name_length) { pos++; } } @@ -108,22 +96,18 @@ nxt_java_HeaderNamesEnumeration_nextElementPos(JNIEnv *env, jclass cls, return pos; } - static jstring JNICALL nxt_java_HeaderNamesEnumeration_nextElement(JNIEnv *env, jclass cls, - jlong headers_ptr, jlong size, jlong pos) -{ - char *name, tmp; + jlong headers_ptr, jlong size, jlong pos) { + char *name, tmp; jstring res; - nxt_unit_field_t *f; + nxt_unit_field_t *f; f = nxt_jlong2ptr(headers_ptr); if (pos > 0) { - while (pos < size - && f[pos].hash == f[pos - 1].hash - && f[pos].name_length == f[pos - 1].name_length) - { + while (pos < size && f[pos].hash == f[pos - 1].hash + && f[pos].name_length == f[pos - 1].name_length) { pos++; } } @@ -137,7 +121,7 @@ nxt_java_HeaderNamesEnumeration_nextElement(JNIEnv *env, jclass cls, f += pos; name = nxt_unit_sptr_get(&f->name); - tmp = name[f->name_length]; + tmp = name[f->name_length]; if (tmp != '\0') { name[f->name_length] = '\0'; diff --git a/src/java/nxt_jni_HeaderNamesEnumeration.h b/src/java/nxt_jni_HeaderNamesEnumeration.h index 90df8f542..7b021d9de 100644 --- a/src/java/nxt_jni_HeaderNamesEnumeration.h +++ b/src/java/nxt_jni_HeaderNamesEnumeration.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,9 +10,11 @@ #include -int nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl); +int +nxt_java_initHeaderNamesEnumeration(JNIEnv *env, jobject cl); -jobject nxt_java_newHeaderNamesEnumeration(JNIEnv *env, nxt_unit_field_t *f, +jobject +nxt_java_newHeaderNamesEnumeration(JNIEnv *env, nxt_unit_field_t *f, uint32_t fields_count); -#endif /* _NXT_JAVA_HEADERNAMESENUMERATION_H_INCLUDED_ */ +#endif /* _NXT_JAVA_HEADERNAMESENUMERATION_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_HeadersEnumeration.c b/src/java/nxt_jni_HeadersEnumeration.c index aaea710db..b32761a02 100644 --- a/src/java/nxt_jni_HeadersEnumeration.c +++ b/src/java/nxt_jni_HeadersEnumeration.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -15,22 +14,22 @@ #include "nxt_jni_HeadersEnumeration.h" -static jclass nxt_java_HeadersEnumeration_class; -static jmethodID nxt_java_HeadersEnumeration_ctor; - +static jclass nxt_java_HeadersEnumeration_class; +static jmethodID nxt_java_HeadersEnumeration_ctor; -static jlong JNICALL nxt_java_HeadersEnumeration_nextElementPos(JNIEnv *env, - jclass cls, jlong headers_ptr, jlong size, jlong ipos, jlong pos); -static jstring JNICALL nxt_java_HeadersEnumeration_nextElement(JNIEnv *env, - jclass cls, jlong headers_ptr, jlong size, jlong ipos, jlong pos); +static jlong JNICALL +nxt_java_HeadersEnumeration_nextElementPos(JNIEnv *env, jclass cls, + jlong headers_ptr, jlong size, jlong ipos, jlong pos); +static jstring JNICALL +nxt_java_HeadersEnumeration_nextElement(JNIEnv *env, jclass cls, + jlong headers_ptr, jlong size, jlong ipos, jlong pos); int -nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.HeadersEnumeration"); if (cls == NULL) { @@ -41,26 +40,23 @@ nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl) (*env)->DeleteLocalRef(env, cls); cls = nxt_java_HeadersEnumeration_class; - nxt_java_HeadersEnumeration_ctor = (*env)->GetMethodID(env, cls, - "", "(JJJ)V"); + nxt_java_HeadersEnumeration_ctor + = (*env)->GetMethodID(env, cls, "", "(JJJ)V"); if (nxt_java_HeadersEnumeration_ctor == NULL) { (*env)->DeleteGlobalRef(env, cls); return NXT_UNIT_ERROR; } JNINativeMethod methods[] = { - { (char *) "nextElementPos", - (char *) "(JJJJ)J", - nxt_java_HeadersEnumeration_nextElementPos }, + {(char *) "nextElementPos", (char *) "(JJJJ)J", + nxt_java_HeadersEnumeration_nextElementPos}, - { (char *) "nextElement", - (char *) "(JJJJ)Ljava/lang/String;", - nxt_java_HeadersEnumeration_nextElement }, + {(char *) "nextElement", (char *) "(JJJJ)Ljava/lang/String;", + nxt_java_HeadersEnumeration_nextElement}, }; res = (*env)->RegisterNatives(env, nxt_java_HeadersEnumeration_class, - methods, - sizeof(methods) / sizeof(methods[0])); + methods, sizeof(methods) / sizeof(methods[0])); nxt_unit_debug(NULL, "registered HeadersEnumeration methods: %d", res); @@ -72,23 +68,18 @@ nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl) return NXT_UNIT_OK; } - jobject nxt_java_newHeadersEnumeration(JNIEnv *env, nxt_unit_field_t *f, - uint32_t fields_count, uint32_t pos) -{ - return (*env)->NewObject(env, - nxt_java_HeadersEnumeration_class, + uint32_t fields_count, uint32_t pos) { + return (*env)->NewObject(env, nxt_java_HeadersEnumeration_class, nxt_java_HeadersEnumeration_ctor, nxt_ptr2jlong(f), (jlong) fields_count, (jlong) pos); } - static jlong JNICALL nxt_java_HeadersEnumeration_nextElementPos(JNIEnv *env, jclass cls, - jlong headers_ptr, jlong size, jlong ipos, jlong pos) -{ - nxt_unit_field_t *f, *init_field; + jlong headers_ptr, jlong size, jlong ipos, jlong pos) { + nxt_unit_field_t *f, *init_field; f = nxt_jlong2ptr(headers_ptr); @@ -101,27 +92,22 @@ nxt_java_HeadersEnumeration_nextElementPos(JNIEnv *env, jclass cls, f += pos; if (f->hash != init_field->hash - || f->name_length != init_field->name_length) - { + || f->name_length != init_field->name_length) { return size; } if (!nxt_java_strcaseeq(nxt_unit_sptr_get(&f->name), - nxt_unit_sptr_get(&init_field->name), - init_field->name_length)) - { + nxt_unit_sptr_get(&init_field->name), init_field->name_length)) { return size; } return pos; } - static jstring JNICALL nxt_java_HeadersEnumeration_nextElement(JNIEnv *env, jclass cls, - jlong headers_ptr, jlong size, jlong ipos, jlong pos) -{ - nxt_unit_field_t *f, *init_field; + jlong headers_ptr, jlong size, jlong ipos, jlong pos) { + nxt_unit_field_t *f, *init_field; f = nxt_jlong2ptr(headers_ptr); @@ -136,13 +122,12 @@ nxt_java_HeadersEnumeration_nextElement(JNIEnv *env, jclass cls, f += pos; if (f->hash != init_field->hash - || f->name_length != init_field->name_length) - { + || f->name_length != init_field->name_length) { nxt_java_throw_IOException(env, "f->hash != hash"); return NULL; } return nxt_java_newString(env, nxt_unit_sptr_get(&f->value), - f->value_length); + f->value_length); } diff --git a/src/java/nxt_jni_HeadersEnumeration.h b/src/java/nxt_jni_HeadersEnumeration.h index 10f9393c6..976279957 100644 --- a/src/java/nxt_jni_HeadersEnumeration.h +++ b/src/java/nxt_jni_HeadersEnumeration.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,9 +10,11 @@ #include -int nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl); +int +nxt_java_initHeadersEnumeration(JNIEnv *env, jobject cl); -jobject nxt_java_newHeadersEnumeration(JNIEnv *env, nxt_unit_field_t *f, +jobject +nxt_java_newHeadersEnumeration(JNIEnv *env, nxt_unit_field_t *f, uint32_t fields_count, uint32_t pos); -#endif /* _NXT_JAVA_HEADERSENUMERATION_H_INCLUDED_ */ +#endif /* _NXT_JAVA_HEADERSENUMERATION_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_InputStream.c b/src/java/nxt_jni_InputStream.c index fabff685e..a49c7b795 100644 --- a/src/java/nxt_jni_InputStream.c +++ b/src/java/nxt_jni_InputStream.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -14,28 +13,28 @@ #include "nxt_jni_URLClassLoader.h" -static jint JNICALL nxt_java_InputStream_readLine(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray b, jint off, jint len); -static jboolean JNICALL nxt_java_InputStream_isFinished(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static jint JNICALL nxt_java_InputStream_readByte(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static jint JNICALL nxt_java_InputStream_read(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray b, jint off, jint len); -static jlong JNICALL nxt_java_InputStream_skip(JNIEnv *env, jclass cls, - jlong req_info_ptr, jlong n); -static jint JNICALL nxt_java_InputStream_available(JNIEnv *env, jclass cls, - jlong req_info_ptr); - +static jint JNICALL +nxt_java_InputStream_readLine(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray b, jint off, jint len); +static jboolean JNICALL +nxt_java_InputStream_isFinished(JNIEnv *env, jclass cls, jlong req_info_ptr); +static jint JNICALL +nxt_java_InputStream_readByte(JNIEnv *env, jclass cls, jlong req_info_ptr); +static jint JNICALL +nxt_java_InputStream_read(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray b, + jint off, jint len); +static jlong JNICALL +nxt_java_InputStream_skip(JNIEnv *env, jclass cls, jlong req_info_ptr, jlong n); +static jint JNICALL +nxt_java_InputStream_available(JNIEnv *env, jclass cls, jlong req_info_ptr); -static jclass nxt_java_InputStream_class; +static jclass nxt_java_InputStream_class; int -nxt_java_initInputStream(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initInputStream(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.InputStream"); if (cls == NULL) { @@ -46,34 +45,23 @@ nxt_java_initInputStream(JNIEnv *env, jobject cl) (*env)->DeleteLocalRef(env, cls); JNINativeMethod is_methods[] = { - { (char *) "readLine", - (char *) "(J[BII)I", - nxt_java_InputStream_readLine }, + {(char *) "readLine", (char *) "(J[BII)I", + nxt_java_InputStream_readLine}, - { (char *) "isFinished", - (char *) "(J)Z", - nxt_java_InputStream_isFinished }, + {(char *) "isFinished", (char *) "(J)Z", + nxt_java_InputStream_isFinished}, - { (char *) "read", - (char *) "(J)I", - nxt_java_InputStream_readByte }, + {(char *) "read", (char *) "(J)I", nxt_java_InputStream_readByte}, - { (char *) "read", - (char *) "(J[BII)I", - nxt_java_InputStream_read }, + {(char *) "read", (char *) "(J[BII)I", nxt_java_InputStream_read}, - { (char *) "skip", - (char *) "(JJ)J", - nxt_java_InputStream_skip }, + {(char *) "skip", (char *) "(JJ)J", nxt_java_InputStream_skip}, - { (char *) "available", - (char *) "(J)I", - nxt_java_InputStream_available }, + {(char *) "available", (char *) "(J)I", nxt_java_InputStream_available}, }; - res = (*env)->RegisterNatives(env, nxt_java_InputStream_class, - is_methods, - sizeof(is_methods) / sizeof(is_methods[0])); + res = (*env)->RegisterNatives(env, nxt_java_InputStream_class, is_methods, + sizeof(is_methods) / sizeof(is_methods[0])); nxt_unit_debug(NULL, "registered InputStream methods: %d", res); @@ -85,14 +73,12 @@ nxt_java_initInputStream(JNIEnv *env, jobject cl) return NXT_UNIT_OK; } - static jint JNICALL -nxt_java_InputStream_readLine(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray out, jint off, jint len) -{ - uint8_t *data; +nxt_java_InputStream_readLine(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray out, jint off, jint len) { + uint8_t *data; ssize_t res; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -111,24 +97,20 @@ nxt_java_InputStream_readLine(JNIEnv *env, jclass cls, return res > 0 ? res : -1; } - static jboolean JNICALL -nxt_java_InputStream_isFinished(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_InputStream_isFinished(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); return req->content_length == 0; } - static jint JNICALL -nxt_java_InputStream_readByte(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ +nxt_java_InputStream_readByte(JNIEnv *env, jclass cls, jlong req_info_ptr) { uint8_t b; ssize_t size; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -137,14 +119,12 @@ nxt_java_InputStream_readByte(JNIEnv *env, jclass cls, jlong req_info_ptr) return size == 1 ? b : -1; } - static jint JNICALL -nxt_java_InputStream_read(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray b, jint off, jint len) -{ - uint8_t *data; +nxt_java_InputStream_read(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray b, + jint off, jint len) { + uint8_t *data; ssize_t res; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -159,13 +139,12 @@ nxt_java_InputStream_read(JNIEnv *env, jclass cls, jlong req_info_ptr, return res > 0 ? res : -1; } - static jlong JNICALL -nxt_java_InputStream_skip(JNIEnv *env, jclass cls, jlong req_info_ptr, jlong n) -{ +nxt_java_InputStream_skip(JNIEnv *env, jclass cls, jlong req_info_ptr, + jlong n) { size_t rest, b_size; - nxt_unit_buf_t *buf; - nxt_unit_request_info_t *req; + nxt_unit_buf_t *buf; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -178,7 +157,7 @@ nxt_java_InputStream_skip(JNIEnv *env, jclass cls, jlong req_info_ptr, jlong n) b_size = rest < b_size ? rest : b_size; buf->free += b_size; - rest -= b_size; + rest -= b_size; if (rest == 0) { if (buf->end == buf->free) { @@ -198,11 +177,9 @@ nxt_java_InputStream_skip(JNIEnv *env, jclass cls, jlong req_info_ptr, jlong n) return n; } - static jint JNICALL -nxt_java_InputStream_available(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_InputStream_available(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); diff --git a/src/java/nxt_jni_InputStream.h b/src/java/nxt_jni_InputStream.h index b20b262a4..3a7c2369a 100644 --- a/src/java/nxt_jni_InputStream.h +++ b/src/java/nxt_jni_InputStream.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -10,6 +9,7 @@ #include -int nxt_java_initInputStream(JNIEnv *env, jobject cl); +int +nxt_java_initInputStream(JNIEnv *env, jobject cl); -#endif /* _NXT_JAVA_INPUTSTREAM_H_INCLUDED_ */ +#endif /* _NXT_JAVA_INPUTSTREAM_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_OutputStream.c b/src/java/nxt_jni_OutputStream.c index 170b33ba9..976345514 100644 --- a/src/java/nxt_jni_OutputStream.c +++ b/src/java/nxt_jni_OutputStream.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -13,26 +12,26 @@ #include "nxt_jni_URLClassLoader.h" -static void JNICALL nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls, - jlong req_info_ptr, jint b); -static nxt_unit_buf_t *nxt_java_OutputStream_req_buf(JNIEnv *env, - nxt_unit_request_info_t *req); -static void JNICALL nxt_java_OutputStream_write(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray b, jint off, jint len); -static void JNICALL nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static void JNICALL nxt_java_OutputStream_close(JNIEnv *env, jclass cls, - jlong req_info_ptr); - +static void JNICALL +nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls, jlong req_info_ptr, + jint b); +static nxt_unit_buf_t * +nxt_java_OutputStream_req_buf(JNIEnv *env, nxt_unit_request_info_t *req); +static void JNICALL +nxt_java_OutputStream_write(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray b, jint off, jint len); +static void JNICALL +nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, jlong req_info_ptr); +static void JNICALL +nxt_java_OutputStream_close(JNIEnv *env, jclass cls, jlong req_info_ptr); -static jclass nxt_java_OutputStream_class; +static jclass nxt_java_OutputStream_class; int -nxt_java_initOutputStream(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initOutputStream(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.OutputStream"); if (cls == NULL) { @@ -45,26 +44,17 @@ nxt_java_initOutputStream(JNIEnv *env, jobject cl) cls = nxt_java_OutputStream_class; JNINativeMethod os_methods[] = { - { (char *) "write", - (char *) "(JI)V", - nxt_java_OutputStream_writeByte }, + {(char *) "write", (char *) "(JI)V", nxt_java_OutputStream_writeByte}, - { (char *) "write", - (char *) "(J[BII)V", - nxt_java_OutputStream_write }, + {(char *) "write", (char *) "(J[BII)V", nxt_java_OutputStream_write}, - { (char *) "flush", - (char *) "(J)V", - nxt_java_OutputStream_flush }, + {(char *) "flush", (char *) "(J)V", nxt_java_OutputStream_flush}, - { (char *) "close", - (char *) "(J)V", - nxt_java_OutputStream_close }, + {(char *) "close", (char *) "(J)V", nxt_java_OutputStream_close}, }; - res = (*env)->RegisterNatives(env, nxt_java_OutputStream_class, - os_methods, - sizeof(os_methods) / sizeof(os_methods[0])); + res = (*env)->RegisterNatives(env, nxt_java_OutputStream_class, os_methods, + sizeof(os_methods) / sizeof(os_methods[0])); nxt_unit_debug(NULL, "registered OutputStream methods: %d", res); @@ -76,16 +66,14 @@ nxt_java_initOutputStream(JNIEnv *env, jobject cl) return NXT_UNIT_OK; } - static void JNICALL nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls, jlong req_info_ptr, - jint b) -{ - nxt_unit_buf_t *buf; - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; + jint b) { + nxt_unit_buf_t *buf; + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; buf = nxt_java_OutputStream_req_buf(env, req); @@ -100,12 +88,10 @@ nxt_java_OutputStream_writeByte(JNIEnv *env, jclass cls, jlong req_info_ptr, } } - int -nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req) -{ +nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req) { int rc; - nxt_java_request_data_t *data; + nxt_java_request_data_t *data; data = req->data; @@ -143,16 +129,14 @@ nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req) return rc; } - static nxt_unit_buf_t * -nxt_java_OutputStream_req_buf(JNIEnv *env, nxt_unit_request_info_t *req) -{ +nxt_java_OutputStream_req_buf(JNIEnv *env, nxt_unit_request_info_t *req) { uint32_t size; - nxt_unit_buf_t *buf; - nxt_java_request_data_t *data; + nxt_unit_buf_t *buf; + nxt_java_request_data_t *data; data = req->data; - buf = data->buf; + buf = data->buf; if (buf == NULL || buf->free >= buf->end) { size = data->buf_size == 0 ? nxt_unit_buf_min() : data->buf_size; @@ -170,19 +154,17 @@ nxt_java_OutputStream_req_buf(JNIEnv *env, nxt_unit_request_info_t *req) return buf; } - static void JNICALL nxt_java_OutputStream_write(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray b, jint off, jint len) -{ + jarray b, jint off, jint len) { int rc; jint copy; - uint8_t *ptr; - nxt_unit_buf_t *buf; - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; + uint8_t *ptr; + nxt_unit_buf_t *buf; + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; ptr = (*env)->GetPrimitiveArrayCritical(env, b, NULL); @@ -213,14 +195,12 @@ nxt_java_OutputStream_write(JNIEnv *env, jclass cls, jlong req_info_ptr, (*env)->ReleasePrimitiveArrayCritical(env, b, ptr, 0); } - static void JNICALL -nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; if (data->buf != NULL && data->buf->free > data->buf->start) { @@ -228,9 +208,7 @@ nxt_java_OutputStream_flush(JNIEnv *env, jclass cls, jlong req_info_ptr) } } - static void JNICALL -nxt_java_OutputStream_close(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ +nxt_java_OutputStream_close(JNIEnv *env, jclass cls, jlong req_info_ptr) { nxt_java_OutputStream_flush_buf(env, nxt_jlong2ptr(req_info_ptr)); } diff --git a/src/java/nxt_jni_OutputStream.h b/src/java/nxt_jni_OutputStream.h index 0c3c9989d..abf12933f 100644 --- a/src/java/nxt_jni_OutputStream.h +++ b/src/java/nxt_jni_OutputStream.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -10,8 +9,10 @@ #include -int nxt_java_initOutputStream(JNIEnv *env, jobject cl); +int +nxt_java_initOutputStream(JNIEnv *env, jobject cl); -int nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req); +int +nxt_java_OutputStream_flush_buf(JNIEnv *env, nxt_unit_request_info_t *req); -#endif /* _NXT_JAVA_OUTPUTSTREAM_H_INCLUDED_ */ +#endif /* _NXT_JAVA_OUTPUTSTREAM_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_Request.c b/src/java/nxt_jni_Request.c index 980a26b68..03f5468b1 100644 --- a/src/java/nxt_jni_Request.c +++ b/src/java/nxt_jni_Request.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -18,75 +17,80 @@ #include "nxt_jni_HeaderNamesEnumeration.h" -static jstring JNICALL nxt_java_Request_getHeader(JNIEnv *env, jclass cls, - jlong req_ptr, jstring name, jint name_len); -static jobject JNICALL nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls, - jlong req_ptr); -static jobject JNICALL nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, - jlong req_ptr, jstring name, jint name_len); -static jint JNICALL nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, - jlong req_ptr, jstring name, jint name_len); -static jstring JNICALL nxt_java_Request_getMethod(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, - jlong req_ptr); -static jlong JNICALL nxt_java_Request_getContentLength(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getContentType(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, - jlong req_ptr); -static jint JNICALL nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getProtocol(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, - jlong req_ptr); -static jint JNICALL nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getScheme(JNIEnv *env, jclass cls, - jlong req_ptr); -static jstring JNICALL nxt_java_Request_getServerName(JNIEnv *env, jclass cls, - jlong req_ptr); -static jint JNICALL nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, - jlong req_ptr); -static jboolean JNICALL nxt_java_Request_isSecure(JNIEnv *env, jclass cls, - jlong req_ptr); -static void JNICALL nxt_java_Request_upgrade(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static jboolean JNICALL nxt_java_Request_isUpgrade(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static void JNICALL nxt_java_Request_log(JNIEnv *env, jclass cls, - jlong req_info_ptr, jstring msg, jint msg_len); -static void JNICALL nxt_java_Request_trace(JNIEnv *env, jclass cls, - jlong req_info_ptr, jstring msg, jint msg_len); -static jobject JNICALL nxt_java_Request_getResponse(JNIEnv *env, jclass cls, - jlong req_info_ptr); -static void JNICALL nxt_java_Request_sendWsFrameBuf(JNIEnv *env, jclass cls, - jlong req_info_ptr, jobject buf, jint pos, jint len, jbyte opCode, jboolean last); -static void JNICALL nxt_java_Request_sendWsFrameArr(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray arr, jint pos, jint len, jbyte opCode, jboolean last); -static void JNICALL nxt_java_Request_closeWs(JNIEnv *env, jclass cls, - jlong req_info_ptr); - - -static jclass nxt_java_Request_class; -static jmethodID nxt_java_Request_ctor; -static jmethodID nxt_java_Request_processWsFrame; -static jmethodID nxt_java_Request_closeWsSession; +static jstring JNICALL +nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr, jstring name, + jint name_len); +static jobject JNICALL +nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls, jlong req_ptr); +static jobject JNICALL +nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, jlong req_ptr, + jstring name, jint name_len); +static jint JNICALL +nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr, + jstring name, jint name_len); +static jstring JNICALL +nxt_java_Request_getMethod(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, jlong req_ptr); +static jlong JNICALL +nxt_java_Request_getContentLength(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getContentType(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, jlong req_ptr); +static jint JNICALL +nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getProtocol(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, jlong req_ptr); +static jint JNICALL +nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getScheme(JNIEnv *env, jclass cls, jlong req_ptr); +static jstring JNICALL +nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr); +static jint JNICALL +nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr); +static jboolean JNICALL +nxt_java_Request_isSecure(JNIEnv *env, jclass cls, jlong req_ptr); +static void JNICALL +nxt_java_Request_upgrade(JNIEnv *env, jclass cls, jlong req_info_ptr); +static jboolean JNICALL +nxt_java_Request_isUpgrade(JNIEnv *env, jclass cls, jlong req_info_ptr); +static void JNICALL +nxt_java_Request_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, + jint msg_len); +static void JNICALL +nxt_java_Request_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, + jint msg_len); +static jobject JNICALL +nxt_java_Request_getResponse(JNIEnv *env, jclass cls, jlong req_info_ptr); +static void JNICALL +nxt_java_Request_sendWsFrameBuf(JNIEnv *env, jclass cls, jlong req_info_ptr, + jobject buf, jint pos, jint len, jbyte opCode, jboolean last); +static void JNICALL +nxt_java_Request_sendWsFrameArr(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray arr, jint pos, jint len, jbyte opCode, jboolean last); +static void JNICALL +nxt_java_Request_closeWs(JNIEnv *env, jclass cls, jlong req_info_ptr); + +static jclass nxt_java_Request_class; +static jmethodID nxt_java_Request_ctor; +static jmethodID nxt_java_Request_processWsFrame; +static jmethodID nxt_java_Request_closeWsSession; int -nxt_java_initRequest(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initRequest(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.Request"); if (cls == NULL) { @@ -97,142 +101,114 @@ nxt_java_initRequest(JNIEnv *env, jobject cl) (*env)->DeleteLocalRef(env, cls); cls = nxt_java_Request_class; - nxt_java_Request_ctor = (*env)->GetMethodID(env, cls, "", "(Lnginx/unit/Context;JJ)V"); + nxt_java_Request_ctor + = (*env)->GetMethodID(env, cls, "", "(Lnginx/unit/Context;JJ)V"); if (nxt_java_Request_ctor == NULL) { (*env)->DeleteGlobalRef(env, cls); return NXT_UNIT_ERROR; } - nxt_java_Request_processWsFrame = (*env)->GetMethodID(env, cls, "processWsFrame", "(Ljava/nio/ByteBuffer;BZ)V"); + nxt_java_Request_processWsFrame = (*env)->GetMethodID(env, cls, + "processWsFrame", "(Ljava/nio/ByteBuffer;BZ)V"); if (nxt_java_Request_processWsFrame == NULL) { (*env)->DeleteGlobalRef(env, cls); return NXT_UNIT_ERROR; } - nxt_java_Request_closeWsSession = (*env)->GetMethodID(env, cls, "closeWsSession", "()V"); + nxt_java_Request_closeWsSession + = (*env)->GetMethodID(env, cls, "closeWsSession", "()V"); if (nxt_java_Request_closeWsSession == NULL) { (*env)->DeleteGlobalRef(env, cls); return NXT_UNIT_ERROR; } JNINativeMethod request_methods[] = { - { (char *) "getHeader", - (char *) "(JLjava/lang/String;I)Ljava/lang/String;", - nxt_java_Request_getHeader }, + {(char *) "getHeader", + (char *) "(JLjava/lang/String;I)Ljava/lang/String;", + nxt_java_Request_getHeader}, - { (char *) "getHeaderNames", - (char *) "(J)Ljava/util/Enumeration;", - nxt_java_Request_getHeaderNames }, + {(char *) "getHeaderNames", (char *) "(J)Ljava/util/Enumeration;", + nxt_java_Request_getHeaderNames}, - { (char *) "getHeaders", - (char *) "(JLjava/lang/String;I)Ljava/util/Enumeration;", - nxt_java_Request_getHeaders }, + {(char *) "getHeaders", + (char *) "(JLjava/lang/String;I)Ljava/util/Enumeration;", + nxt_java_Request_getHeaders}, - { (char *) "getIntHeader", - (char *) "(JLjava/lang/String;I)I", - nxt_java_Request_getIntHeader }, + {(char *) "getIntHeader", (char *) "(JLjava/lang/String;I)I", + nxt_java_Request_getIntHeader}, - { (char *) "getMethod", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getMethod }, + {(char *) "getMethod", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getMethod}, - { (char *) "getQueryString", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getQueryString }, + {(char *) "getQueryString", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getQueryString}, - { (char *) "getRequestURI", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getRequestURI }, + {(char *) "getRequestURI", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getRequestURI}, - { (char *) "getContentLength", - (char *) "(J)J", - nxt_java_Request_getContentLength }, + {(char *) "getContentLength", (char *) "(J)J", + nxt_java_Request_getContentLength}, - { (char *) "getContentType", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getContentType }, + {(char *) "getContentType", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getContentType}, - { (char *) "getLocalAddr", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getLocalAddr }, + {(char *) "getLocalAddr", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getLocalAddr}, - { (char *) "getLocalName", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getLocalName }, + {(char *) "getLocalName", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getLocalName}, - { (char *) "getLocalPort", - (char *) "(J)I", - nxt_java_Request_getLocalPort }, + {(char *) "getLocalPort", (char *) "(J)I", + nxt_java_Request_getLocalPort}, - { (char *) "getProtocol", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getProtocol }, + {(char *) "getProtocol", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getProtocol}, - { (char *) "getRemoteAddr", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getRemoteAddr }, + {(char *) "getRemoteAddr", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getRemoteAddr}, - { (char *) "getRemoteHost", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getRemoteHost }, + {(char *) "getRemoteHost", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getRemoteHost}, - { (char *) "getRemotePort", - (char *) "(J)I", - nxt_java_Request_getRemotePort }, + {(char *) "getRemotePort", (char *) "(J)I", + nxt_java_Request_getRemotePort}, - { (char *) "getScheme", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getScheme }, + {(char *) "getScheme", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getScheme}, - { (char *) "getServerName", - (char *) "(J)Ljava/lang/String;", - nxt_java_Request_getServerName }, + {(char *) "getServerName", (char *) "(J)Ljava/lang/String;", + nxt_java_Request_getServerName}, - { (char *) "getServerPort", - (char *) "(J)I", - nxt_java_Request_getServerPort }, + {(char *) "getServerPort", (char *) "(J)I", + nxt_java_Request_getServerPort}, - { (char *) "isSecure", - (char *) "(J)Z", - nxt_java_Request_isSecure }, + {(char *) "isSecure", (char *) "(J)Z", nxt_java_Request_isSecure}, - { (char *) "upgrade", - (char *) "(J)V", - nxt_java_Request_upgrade }, + {(char *) "upgrade", (char *) "(J)V", nxt_java_Request_upgrade}, - { (char *) "isUpgrade", - (char *) "(J)Z", - nxt_java_Request_isUpgrade }, + {(char *) "isUpgrade", (char *) "(J)Z", nxt_java_Request_isUpgrade}, - { (char *) "log", - (char *) "(JLjava/lang/String;I)V", - nxt_java_Request_log }, + {(char *) "log", (char *) "(JLjava/lang/String;I)V", + nxt_java_Request_log}, - { (char *) "trace", - (char *) "(JLjava/lang/String;I)V", - nxt_java_Request_trace }, + {(char *) "trace", (char *) "(JLjava/lang/String;I)V", + nxt_java_Request_trace}, - { (char *) "getResponse", - (char *) "(J)Lnginx/unit/Response;", - nxt_java_Request_getResponse }, + {(char *) "getResponse", (char *) "(J)Lnginx/unit/Response;", + nxt_java_Request_getResponse}, - { (char *) "sendWsFrame", - (char *) "(JLjava/nio/ByteBuffer;IIBZ)V", - nxt_java_Request_sendWsFrameBuf }, + {(char *) "sendWsFrame", (char *) "(JLjava/nio/ByteBuffer;IIBZ)V", + nxt_java_Request_sendWsFrameBuf}, - { (char *) "sendWsFrame", - (char *) "(J[BIIBZ)V", - nxt_java_Request_sendWsFrameArr }, + {(char *) "sendWsFrame", (char *) "(J[BIIBZ)V", + nxt_java_Request_sendWsFrameArr}, - { (char *) "closeWs", - (char *) "(J)V", - nxt_java_Request_closeWs }, + {(char *) "closeWs", (char *) "(J)V", nxt_java_Request_closeWs}, }; - res = (*env)->RegisterNatives(env, nxt_java_Request_class, - request_methods, - sizeof(request_methods) / sizeof(request_methods[0])); + res = (*env)->RegisterNatives(env, nxt_java_Request_class, request_methods, + sizeof(request_methods) / sizeof(request_methods[0])); nxt_unit_debug(NULL, "registered Request methods: %d", res); @@ -259,23 +235,18 @@ nxt_java_initRequest(JNIEnv *env, jobject cl) return NXT_UNIT_ERROR; } - jobject -nxt_java_newRequest(JNIEnv *env, jobject ctx, nxt_unit_request_info_t *req) -{ - return (*env)->NewObject(env, nxt_java_Request_class, - nxt_java_Request_ctor, ctx, nxt_ptr2jlong(req), - nxt_ptr2jlong(req->request)); +nxt_java_newRequest(JNIEnv *env, jobject ctx, nxt_unit_request_info_t *req) { + return (*env)->NewObject(env, nxt_java_Request_class, nxt_java_Request_ctor, + ctx, nxt_ptr2jlong(req), nxt_ptr2jlong(req->request)); } - static jstring JNICALL -nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr, - jstring name, jint name_len) -{ - const char *name_str; - nxt_unit_field_t *f; - nxt_unit_request_t *r; +nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr, jstring name, + jint name_len) { + const char *name_str; + nxt_unit_field_t *f; + nxt_unit_request_t *r; name_str = (*env)->GetStringUTFChars(env, name, NULL); if (name_str == NULL) { @@ -284,8 +255,8 @@ nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr, r = nxt_jlong2ptr(req_ptr); - f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, - name_str, name_len); + f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, name_str, + name_len); (*env)->ReleaseStringUTFChars(env, name, name_str); @@ -296,25 +267,21 @@ nxt_java_Request_getHeader(JNIEnv *env, jclass cls, jlong req_ptr, return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&f->value)); } - static jobject JNICALL -nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getHeaderNames(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return nxt_java_newHeaderNamesEnumeration(env, r->fields, r->fields_count); } - static jobject JNICALL nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, jlong req_ptr, - jstring name, jint name_len) -{ - const char *name_str; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + jstring name, jint name_len) { + const char *name_str; + nxt_unit_field_t *f; + nxt_unit_request_t *r; name_str = (*env)->GetStringUTFChars(env, name, NULL); if (name_str == NULL) { @@ -323,8 +290,8 @@ nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, jlong req_ptr, r = nxt_jlong2ptr(req_ptr); - f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, - name_str, name_len); + f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, name_str, + name_len); (*env)->ReleaseStringUTFChars(env, name, name_str); @@ -333,19 +300,17 @@ nxt_java_Request_getHeaders(JNIEnv *env, jclass cls, jlong req_ptr, } return nxt_java_newHeadersEnumeration(env, r->fields, r->fields_count, - f - r->fields); + f - r->fields); } - static jint JNICALL nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr, - jstring name, jint name_len) -{ + jstring name, jint name_len) { jint res; - char *value, *end; - const char *name_str; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + char *value, *end; + const char *name_str; + nxt_unit_field_t *f; + nxt_unit_request_t *r; res = -1; @@ -356,8 +321,8 @@ nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr, r = nxt_jlong2ptr(req_ptr); - f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, - name_str, name_len); + f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, name_str, + name_len); (*env)->ReleaseStringUTFChars(env, name, name_str); @@ -366,7 +331,7 @@ nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr, } value = nxt_unit_sptr_get(&f->value); - end = value + f->value_length; + end = value + f->value_length; res = strtol(value, &end, 10); @@ -377,23 +342,19 @@ nxt_java_Request_getIntHeader(JNIEnv *env, jclass cls, jlong req_ptr, return res; } - static jstring JNICALL -nxt_java_Request_getMethod(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getMethod(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&r->method)); } - static jstring JNICALL -nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, jlong req_ptr) -{ - char *query; - nxt_unit_request_t *r; +nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, jlong req_ptr) { + char *query; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); @@ -405,12 +366,10 @@ nxt_java_Request_getQueryString(JNIEnv *env, jclass cls, jlong req_ptr) return NULL; } - static jstring JNICALL -nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, jlong req_ptr) -{ - char *target, *query; - nxt_unit_request_t *r; +nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, jlong req_ptr) { + char *target, *query; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); @@ -424,23 +383,19 @@ nxt_java_Request_getRequestURI(JNIEnv *env, jclass cls, jlong req_ptr) return (*env)->NewStringUTF(env, target); } - static jlong JNICALL -nxt_java_Request_getContentLength(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getContentLength(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return r->content_length; } - static jstring JNICALL -nxt_java_Request_getContentType(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_field_t *f; - nxt_unit_request_t *r; +nxt_java_Request_getContentType(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_field_t *f; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); @@ -453,24 +408,20 @@ nxt_java_Request_getContentType(JNIEnv *env, jclass cls, jlong req_ptr) return NULL; } - static jstring JNICALL -nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getLocalAddr(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return nxt_java_newString(env, nxt_unit_sptr_get(&r->local_addr), - r->local_addr_length); + r->local_addr_length); } - static jstring JNICALL -nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, jlong req_ptr) -{ - char *local, *colon; - nxt_unit_request_t *r; +nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, jlong req_ptr) { + char *local, *colon; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); @@ -484,13 +435,11 @@ nxt_java_Request_getLocalName(JNIEnv *env, jclass cls, jlong req_ptr) return nxt_java_newString(env, local, colon - local); } - static jint JNICALL -nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, jlong req_ptr) -{ +nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, jlong req_ptr) { jint res; - char *local, *colon, tmp; - nxt_unit_request_t *r; + char *local, *colon, tmp; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); @@ -512,40 +461,34 @@ nxt_java_Request_getLocalPort(JNIEnv *env, jclass cls, jlong req_ptr) return res; } - static jstring JNICALL -nxt_java_Request_getProtocol(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getProtocol(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return (*env)->NewStringUTF(env, nxt_unit_sptr_get(&r->version)); } - static jstring JNICALL -nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getRemoteAddr(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return nxt_java_newString(env, nxt_unit_sptr_get(&r->remote), - r->remote_length); + r->remote_length); } - static jstring JNICALL -nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, jlong req_ptr) -{ - char *remote, *colon; - nxt_unit_request_t *r; +nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, jlong req_ptr) { + char *remote, *colon; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); remote = nxt_unit_sptr_get(&r->remote); - colon = memchr(remote, ':', r->remote_length); + colon = memchr(remote, ':', r->remote_length); if (colon == NULL) { colon = remote + r->remote_length; @@ -554,18 +497,16 @@ nxt_java_Request_getRemoteHost(JNIEnv *env, jclass cls, jlong req_ptr) return nxt_java_newString(env, remote, colon - remote); } - static jint JNICALL -nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, jlong req_ptr) -{ +nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, jlong req_ptr) { jint res; - char *remote, *colon, tmp; - nxt_unit_request_t *r; + char *remote, *colon, tmp; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); remote = nxt_unit_sptr_get(&r->remote); - colon = memchr(remote, ':', r->remote_length); + colon = memchr(remote, ':', r->remote_length); if (colon == NULL) { return 80; @@ -582,29 +523,24 @@ nxt_java_Request_getRemotePort(JNIEnv *env, jclass cls, jlong req_ptr) return res; } - static jstring JNICALL -nxt_java_Request_getScheme(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_getScheme(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return (*env)->NewStringUTF(env, r->tls ? "https" : "http"); } - static jstring JNICALL -nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr) -{ - char *host, *colon; - nxt_unit_field_t *f; - nxt_unit_request_t *r; +nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr) { + char *host, *colon; + nxt_unit_field_t *f; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); - f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, - "Host", 4); + f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, "Host", 4); if (f != NULL) { host = nxt_unit_sptr_get(&f->value); @@ -620,19 +556,16 @@ nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr) return nxt_java_Request_getLocalName(env, cls, req_ptr); } - static jint JNICALL -nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr) -{ +nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr) { jint res; - char *host, *colon, tmp; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + char *host, *colon, tmp; + nxt_unit_field_t *f; + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); - f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, - "Host", 4); + f = nxt_java_findHeader(r->fields, r->fields + r->fields_count, "Host", 4); if (f != NULL) { host = nxt_unit_sptr_get(&f->value); @@ -656,22 +589,18 @@ nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr) return nxt_java_Request_getLocalPort(env, cls, req_ptr); } - static jboolean JNICALL -nxt_java_Request_isSecure(JNIEnv *env, jclass cls, jlong req_ptr) -{ - nxt_unit_request_t *r; +nxt_java_Request_isSecure(JNIEnv *env, jclass cls, jlong req_ptr) { + nxt_unit_request_t *r; r = nxt_jlong2ptr(req_ptr); return r->tls != 0; } - static void JNICALL -nxt_java_Request_upgrade(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Request_upgrade(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -682,24 +611,20 @@ nxt_java_Request_upgrade(JNIEnv *env, jclass cls, jlong req_info_ptr) (void) nxt_unit_response_upgrade(req); } - static jboolean JNICALL -nxt_java_Request_isUpgrade(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Request_isUpgrade(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); return nxt_unit_request_is_websocket_handshake(req); } - static void JNICALL nxt_java_Request_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, - jint msg_len) -{ - const char *msg_str; - nxt_unit_request_info_t *req; + jint msg_len) { + const char *msg_str; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -713,14 +638,12 @@ nxt_java_Request_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, (*env)->ReleaseStringUTFChars(env, msg, msg_str); } - static void JNICALL nxt_java_Request_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, - jint msg_len) -{ + jint msg_len) { #if (NXT_DEBUG) - const char *msg_str; - nxt_unit_request_info_t *req; + const char *msg_str; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -735,27 +658,23 @@ nxt_java_Request_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jstring msg, #endif } - static jobject JNICALL -nxt_java_Request_getResponse(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Request_getResponse(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; return data->jresp; } - static void JNICALL -nxt_java_Request_sendWsFrameBuf(JNIEnv *env, jclass cls, - jlong req_info_ptr, jobject buf, jint pos, jint len, jbyte opCode, jboolean last) -{ - nxt_unit_request_info_t *req; +nxt_java_Request_sendWsFrameBuf(JNIEnv *env, jclass cls, jlong req_info_ptr, + jobject buf, jint pos, jint len, jbyte opCode, jboolean last) { + nxt_unit_request_info_t *req; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); uint8_t *b = (*env)->GetDirectBufferAddress(env, buf); if (b != NULL) { @@ -766,14 +685,12 @@ nxt_java_Request_sendWsFrameBuf(JNIEnv *env, jclass cls, } } - static void JNICALL -nxt_java_Request_sendWsFrameArr(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray arr, jint pos, jint len, jbyte opCode, jboolean last) -{ - nxt_unit_request_info_t *req; +nxt_java_Request_sendWsFrameArr(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray arr, jint pos, jint len, jbyte opCode, jboolean last) { + nxt_unit_request_info_t *req; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); uint8_t *b = (*env)->GetPrimitiveArrayCritical(env, arr, NULL); if (b != NULL) { @@ -790,12 +707,10 @@ nxt_java_Request_sendWsFrameArr(JNIEnv *env, jclass cls, } } - static void JNICALL -nxt_java_Request_closeWs(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Request_closeWs(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; req = nxt_jlong2ptr(req_info_ptr); @@ -807,17 +722,14 @@ nxt_java_Request_closeWs(JNIEnv *env, jclass cls, jlong req_info_ptr) nxt_unit_request_done(req, NXT_UNIT_OK); } - void nxt_java_Request_websocket(JNIEnv *env, jobject jreq, jobject jbuf, - uint8_t opcode, uint8_t fin) -{ - (*env)->CallVoidMethod(env, jreq, nxt_java_Request_processWsFrame, jbuf, opcode, fin); + uint8_t opcode, uint8_t fin) { + (*env)->CallVoidMethod(env, jreq, nxt_java_Request_processWsFrame, jbuf, + opcode, fin); } - void -nxt_java_Request_close(JNIEnv *env, jobject jreq) -{ +nxt_java_Request_close(JNIEnv *env, jobject jreq) { (*env)->CallVoidMethod(env, jreq, nxt_java_Request_closeWsSession); } diff --git a/src/java/nxt_jni_Request.h b/src/java/nxt_jni_Request.h index 9187d8786..41f77ab08 100644 --- a/src/java/nxt_jni_Request.h +++ b/src/java/nxt_jni_Request.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,13 +10,17 @@ #include -int nxt_java_initRequest(JNIEnv *env, jobject cl); +int +nxt_java_initRequest(JNIEnv *env, jobject cl); -jobject nxt_java_newRequest(JNIEnv *env, jobject ctx, nxt_unit_request_info_t *req); +jobject +nxt_java_newRequest(JNIEnv *env, jobject ctx, nxt_unit_request_info_t *req); -void nxt_java_Request_websocket(JNIEnv *env, jobject jreq, jobject jbuf, +void +nxt_java_Request_websocket(JNIEnv *env, jobject jreq, jobject jbuf, uint8_t opcode, uint8_t fin); -void nxt_java_Request_close(JNIEnv *env, jobject jreq); +void +nxt_java_Request_close(JNIEnv *env, jobject jreq); -#endif /* _NXT_JAVA_REQUEST_H_INCLUDED_ */ +#endif /* _NXT_JAVA_REQUEST_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_Response.c b/src/java/nxt_jni_Response.c index fa698ee89..9a31281da 100644 --- a/src/java/nxt_jni_Response.c +++ b/src/java/nxt_jni_Response.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -18,102 +17,120 @@ #include "nxt_jni_URLClassLoader.h" -static jclass nxt_java_Response_class; -static jmethodID nxt_java_Response_ctor; +static jclass nxt_java_Response_class; +static jmethodID nxt_java_Response_ctor; -static void JNICALL nxt_java_Response_addHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jarray value); +static void JNICALL +nxt_java_Response_addHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jarray value); -static nxt_unit_request_info_t *nxt_java_get_response_info( - jlong req_info_ptr, uint32_t extra_fields, uint32_t extra_data); +static nxt_unit_request_info_t * +nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields, + uint32_t extra_data); -static void JNICALL nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jint value); +static void JNICALL +nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jint value); -static void nxt_java_add_int_header(nxt_unit_request_info_t *req, - const char *name, uint8_t name_len, int value); +static void +nxt_java_add_int_header(nxt_unit_request_info_t *req, const char *name, + uint8_t name_len, int value); -static jboolean JNICALL nxt_java_Response_containsHeader(JNIEnv *env, - jclass cls, jlong req_info_ptr, jarray name); +static jboolean JNICALL +nxt_java_Response_containsHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name); -static jstring JNICALL nxt_java_Response_getHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name); +static jstring JNICALL +nxt_java_Response_getHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name); -static jobject JNICALL nxt_java_Response_getHeaderNames(JNIEnv *env, - jclass cls, jlong req_info_ptr); +static jobject JNICALL +nxt_java_Response_getHeaderNames(JNIEnv *env, jclass cls, jlong req_info_ptr); -static jobject JNICALL nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name); +static jobject JNICALL +nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name); -static jint JNICALL nxt_java_Response_getStatus(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static jint JNICALL +nxt_java_Response_getStatus(JNIEnv *env, jclass cls, jlong req_info_ptr); -static jobject JNICALL nxt_java_Response_getRequest(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static jobject JNICALL +nxt_java_Response_getRequest(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_commit(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static void JNICALL +nxt_java_Response_commit(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray loc); +static void JNICALL +nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray loc); -static int nxt_java_response_set_header(jlong req_info_ptr, - const char *name, jint name_len, const char *value, jint value_len); +static int +nxt_java_response_set_header(jlong req_info_ptr, const char *name, + jint name_len, const char *value, jint value_len); -static void JNICALL nxt_java_Response_setHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jarray value); +static void JNICALL +nxt_java_Response_setHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jarray value); -static void JNICALL nxt_java_Response_removeHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name); +static void JNICALL +nxt_java_Response_removeHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name); -static int nxt_java_response_remove_header(jlong req_info_ptr, - const char *name, jint name_len); +static int +nxt_java_response_remove_header(jlong req_info_ptr, const char *name, + jint name_len); -static void JNICALL nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jint value); +static void JNICALL +nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jint value); -static void JNICALL nxt_java_Response_setStatus(JNIEnv *env, jclass cls, - jlong req_info_ptr, jint sc); +static void JNICALL +nxt_java_Response_setStatus(JNIEnv *env, jclass cls, jlong req_info_ptr, + jint sc); -static jstring JNICALL nxt_java_Response_getContentType(JNIEnv *env, - jclass cls, jlong req_info_ptr); +static jstring JNICALL +nxt_java_Response_getContentType(JNIEnv *env, jclass cls, jlong req_info_ptr); -static jboolean JNICALL nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static jboolean JNICALL +nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_reset(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static void JNICALL +nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static void JNICALL +nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls, - jlong req_info_ptr, jint size); +static void JNICALL +nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr, + jint size); -static jint JNICALL nxt_java_Response_getBufferSize(JNIEnv *env, jclass cls, - jlong req_info_ptr); +static jint JNICALL +nxt_java_Response_getBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_setContentLength(JNIEnv *env, jclass cls, - jlong req_info_ptr, jlong len); +static void JNICALL +nxt_java_Response_setContentLength(JNIEnv *env, jclass cls, jlong req_info_ptr, + jlong len); -static void JNICALL nxt_java_Response_setContentType(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray type); +static void JNICALL +nxt_java_Response_setContentType(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray type); -static void JNICALL nxt_java_Response_removeContentType(JNIEnv *env, jclass cls, +static void JNICALL +nxt_java_Response_removeContentType(JNIEnv *env, jclass cls, jlong req_info_ptr); -static void JNICALL nxt_java_Response_log(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray msg); +static void JNICALL +nxt_java_Response_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg); -static void JNICALL nxt_java_Response_trace(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray msg); +static void JNICALL +nxt_java_Response_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray msg); int -nxt_java_initResponse(JNIEnv *env, jobject cl) -{ - int res; - jclass cls; +nxt_java_initResponse(JNIEnv *env, jobject cl) { + int res; + jclass cls; cls = nxt_java_loadClass(env, cl, "nginx.unit.Response"); if (cls == NULL) { @@ -131,112 +148,79 @@ nxt_java_initResponse(JNIEnv *env, jobject cl) } JNINativeMethod resp_methods[] = { - { (char *) "addHeader", - (char *) "(J[B[B)V", - nxt_java_Response_addHeader }, + {(char *) "addHeader", (char *) "(J[B[B)V", + nxt_java_Response_addHeader}, - { (char *) "addIntHeader", - (char *) "(J[BI)V", - nxt_java_Response_addIntHeader }, + {(char *) "addIntHeader", (char *) "(J[BI)V", + nxt_java_Response_addIntHeader}, - { (char *) "containsHeader", - (char *) "(J[B)Z", - nxt_java_Response_containsHeader }, + {(char *) "containsHeader", (char *) "(J[B)Z", + nxt_java_Response_containsHeader}, - { (char *) "getHeader", - (char *) "(J[B)Ljava/lang/String;", - nxt_java_Response_getHeader }, + {(char *) "getHeader", (char *) "(J[B)Ljava/lang/String;", + nxt_java_Response_getHeader}, - { (char *) "getHeaderNames", - (char *) "(J)Ljava/util/Enumeration;", - nxt_java_Response_getHeaderNames }, + {(char *) "getHeaderNames", (char *) "(J)Ljava/util/Enumeration;", + nxt_java_Response_getHeaderNames}, - { (char *) "getHeaders", - (char *) "(J[B)Ljava/util/Enumeration;", - nxt_java_Response_getHeaders }, + {(char *) "getHeaders", (char *) "(J[B)Ljava/util/Enumeration;", + nxt_java_Response_getHeaders}, - { (char *) "getStatus", - (char *) "(J)I", - nxt_java_Response_getStatus }, + {(char *) "getStatus", (char *) "(J)I", nxt_java_Response_getStatus}, - { (char *) "getRequest", - (char *) "(J)Lnginx/unit/Request;", - nxt_java_Response_getRequest }, + {(char *) "getRequest", (char *) "(J)Lnginx/unit/Request;", + nxt_java_Response_getRequest}, - { (char *) "commit", - (char *) "(J)V", - nxt_java_Response_commit }, + {(char *) "commit", (char *) "(J)V", nxt_java_Response_commit}, - { (char *) "sendRedirect", - (char *) "(J[B)V", - nxt_java_Response_sendRedirect }, + {(char *) "sendRedirect", (char *) "(J[B)V", + nxt_java_Response_sendRedirect}, - { (char *) "setHeader", - (char *) "(J[B[B)V", - nxt_java_Response_setHeader }, + {(char *) "setHeader", (char *) "(J[B[B)V", + nxt_java_Response_setHeader}, - { (char *) "removeHeader", - (char *) "(J[B)V", - nxt_java_Response_removeHeader }, + {(char *) "removeHeader", (char *) "(J[B)V", + nxt_java_Response_removeHeader}, - { (char *) "setIntHeader", - (char *) "(J[BI)V", - nxt_java_Response_setIntHeader }, + {(char *) "setIntHeader", (char *) "(J[BI)V", + nxt_java_Response_setIntHeader}, - { (char *) "setStatus", - (char *) "(JI)V", - nxt_java_Response_setStatus }, + {(char *) "setStatus", (char *) "(JI)V", nxt_java_Response_setStatus}, - { (char *) "getContentType", - (char *) "(J)Ljava/lang/String;", - nxt_java_Response_getContentType }, + {(char *) "getContentType", (char *) "(J)Ljava/lang/String;", + nxt_java_Response_getContentType}, - { (char *) "isCommitted", - (char *) "(J)Z", - nxt_java_Response_isCommitted }, + {(char *) "isCommitted", (char *) "(J)Z", + nxt_java_Response_isCommitted}, - { (char *) "reset", - (char *) "(J)V", - nxt_java_Response_reset }, + {(char *) "reset", (char *) "(J)V", nxt_java_Response_reset}, - { (char *) "resetBuffer", - (char *) "(J)V", - nxt_java_Response_resetBuffer }, + {(char *) "resetBuffer", (char *) "(J)V", + nxt_java_Response_resetBuffer}, - { (char *) "setBufferSize", - (char *) "(JI)V", - nxt_java_Response_setBufferSize }, + {(char *) "setBufferSize", (char *) "(JI)V", + nxt_java_Response_setBufferSize}, - { (char *) "getBufferSize", - (char *) "(J)I", - nxt_java_Response_getBufferSize }, + {(char *) "getBufferSize", (char *) "(J)I", + nxt_java_Response_getBufferSize}, - { (char *) "setContentLength", - (char *) "(JJ)V", - nxt_java_Response_setContentLength }, + {(char *) "setContentLength", (char *) "(JJ)V", + nxt_java_Response_setContentLength}, - { (char *) "setContentType", - (char *) "(J[B)V", - nxt_java_Response_setContentType }, + {(char *) "setContentType", (char *) "(J[B)V", + nxt_java_Response_setContentType}, - { (char *) "removeContentType", - (char *) "(J)V", - nxt_java_Response_removeContentType }, + {(char *) "removeContentType", (char *) "(J)V", + nxt_java_Response_removeContentType}, - { (char *) "log", - (char *) "(J[B)V", - nxt_java_Response_log }, + {(char *) "log", (char *) "(J[B)V", nxt_java_Response_log}, - { (char *) "trace", - (char *) "(J[B)V", - nxt_java_Response_trace }, + {(char *) "trace", (char *) "(J[B)V", nxt_java_Response_trace}, }; - res = (*env)->RegisterNatives(env, nxt_java_Response_class, - resp_methods, - sizeof(resp_methods) - / sizeof(resp_methods[0])); + res = (*env)->RegisterNatives(env, nxt_java_Response_class, resp_methods, + sizeof(resp_methods) / sizeof(resp_methods[0])); nxt_unit_debug(NULL, "registered Response methods: %d", res); @@ -248,25 +232,21 @@ nxt_java_initResponse(JNIEnv *env, jobject cl) return NXT_UNIT_OK; } - jobject -nxt_java_newResponse(JNIEnv *env, nxt_unit_request_info_t *req) -{ +nxt_java_newResponse(JNIEnv *env, nxt_unit_request_info_t *req) { return (*env)->NewObject(env, nxt_java_Response_class, - nxt_java_Response_ctor, nxt_ptr2jlong(req)); + nxt_java_Response_ctor, nxt_ptr2jlong(req)); } - static void JNICALL nxt_java_Response_addHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray name, jarray value) -{ + jarray name, jarray value) { int rc; - char *name_str, *value_str; + char *name_str, *value_str; jsize name_len, value_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - name_len = (*env)->GetArrayLength(env, name); + name_len = (*env)->GetArrayLength(env, name); value_len = (*env)->GetArrayLength(env, value); req = nxt_java_get_response_info(req_info_ptr, 1, name_len + value_len + 2); @@ -288,8 +268,8 @@ nxt_java_Response_addHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, return; } - rc = nxt_unit_response_add_field(req, name_str, name_len, - value_str, value_len); + rc = nxt_unit_response_add_field(req, name_str, name_len, value_str, + value_len); if (rc != NXT_UNIT_OK) { // throw } @@ -298,17 +278,15 @@ nxt_java_Response_addHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); } - static nxt_unit_request_info_t * nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields, - uint32_t extra_data) -{ + uint32_t extra_data) { int rc; - char *p; + char *p; uint32_t max_size; - nxt_unit_buf_t *buf; - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; + nxt_unit_buf_t *buf; + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; req = nxt_jlong2ptr(req_info_ptr); @@ -330,24 +308,22 @@ nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields, buf = req->response_buf; - if (extra_fields > req->response_max_fields - - req->response->fields_count - || extra_data > (uint32_t) (buf->end - buf->free)) - { + if (extra_fields > req->response_max_fields - req->response->fields_count + || extra_data > (uint32_t) (buf->end - buf->free)) { p = buf->start + sizeof(nxt_unit_response_t) + req->response_max_fields * sizeof(nxt_unit_field_t); max_size = 2 * (buf->end - p); if (max_size > nxt_unit_buf_max()) { - nxt_unit_req_warn(req, "required max_size is too big: %"PRIu32, + nxt_unit_req_warn(req, "required max_size is too big: %" PRIu32, max_size); return NULL; } rc = nxt_unit_response_realloc(req, 2 * req->response_max_fields, - max_size); + max_size); if (rc != NXT_UNIT_OK) { - nxt_unit_req_warn(req, "reallocation failed: %"PRIu32", %"PRIu32, + nxt_unit_req_warn(req, "reallocation failed: %" PRIu32 ", %" PRIu32, 2 * req->response_max_fields, max_size); return NULL; } @@ -356,14 +332,12 @@ nxt_java_get_response_info(jlong req_info_ptr, uint32_t extra_fields, return req; } - static void JNICALL nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray name, jint value) -{ - char *name_str; + jarray name, jint value) { + char *name_str; jsize name_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; name_len = (*env)->GetArrayLength(env, name); @@ -383,22 +357,20 @@ nxt_java_Response_addIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); } - static void nxt_java_add_int_header(nxt_unit_request_info_t *req, const char *name, - uint8_t name_len, int value) -{ - char *p; - nxt_unit_field_t *f; - nxt_unit_response_t *resp; + uint8_t name_len, int value) { + char *p; + nxt_unit_field_t *f; + nxt_unit_response_t *resp; resp = req->response; f = resp->fields + resp->fields_count; p = req->response_buf->free; - f->hash = nxt_unit_field_hash(name, name_len); - f->skip = 0; + f->hash = nxt_unit_field_hash(name, name_len); + f->skip = 0; f->name_length = name_len; nxt_unit_sptr_set(&f->name, p); @@ -406,24 +378,21 @@ nxt_java_add_int_header(nxt_unit_request_info_t *req, const char *name, p += name_len; nxt_unit_sptr_set(&f->value, p); - f->value_length = snprintf(p, 40, "%d", (int) value); - p += f->value_length + 1; + f->value_length = snprintf(p, 40, "%d", (int) value); + p += f->value_length + 1; resp->fields_count++; req->response_buf->free = p; - } - static jboolean JNICALL -nxt_java_Response_containsHeader(JNIEnv *env, - jclass cls, jlong req_info_ptr, jarray name) -{ +nxt_java_Response_containsHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name) { jboolean res; - char *name_str; + char *name_str; jsize name_len; - nxt_unit_response_t *resp; - nxt_unit_request_info_t *req; + nxt_unit_response_t *resp; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -447,24 +416,22 @@ nxt_java_Response_containsHeader(JNIEnv *env, resp = req->response; - res = nxt_java_findHeader(resp->fields, - resp->fields + resp->fields_count, - name_str, name_len) != NULL; + res = nxt_java_findHeader(resp->fields, resp->fields + resp->fields_count, + name_str, name_len) + != NULL; (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); return res; } - static jstring JNICALL nxt_java_Response_getHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray name) -{ - char *name_str; + jarray name) { + char *name_str; jsize name_len; - nxt_unit_field_t *f; - nxt_unit_request_info_t *req; + nxt_unit_field_t *f; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -487,8 +454,8 @@ nxt_java_Response_getHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, } f = nxt_java_findHeader(req->response->fields, - req->response->fields + req->response->fields_count, - name_str, name_len); + req->response->fields + req->response->fields_count, name_str, + name_len); (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); @@ -497,14 +464,12 @@ nxt_java_Response_getHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, } return nxt_java_newString(env, nxt_unit_sptr_get(&f->value), - f->value_length); + f->value_length); } - static jobject JNICALL -nxt_java_Response_getHeaderNames(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Response_getHeaderNames(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -519,19 +484,17 @@ nxt_java_Response_getHeaderNames(JNIEnv *env, jclass cls, jlong req_info_ptr) } return nxt_java_newHeaderNamesEnumeration(env, req->response->fields, - req->response->fields_count); + req->response->fields_count); } - static jobject JNICALL -nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name) -{ - char *name_str; +nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name) { + char *name_str; jsize name_len; - nxt_unit_field_t *f; - nxt_unit_response_t *resp; - nxt_unit_request_info_t *req; + nxt_unit_field_t *f; + nxt_unit_response_t *resp; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -556,7 +519,7 @@ nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, } f = nxt_java_findHeader(resp->fields, resp->fields + resp->fields_count, - name_str, name_len); + name_str, name_len); (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); @@ -565,14 +528,12 @@ nxt_java_Response_getHeaders(JNIEnv *env, jclass cls, } return nxt_java_newHeadersEnumeration(env, resp->fields, resp->fields_count, - f - resp->fields); + f - resp->fields); } - static jint JNICALL -nxt_java_Response_getStatus(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Response_getStatus(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -589,42 +550,36 @@ nxt_java_Response_getStatus(JNIEnv *env, jclass cls, jlong req_info_ptr) return req->response->status; } - static jobject JNICALL -nxt_java_Response_getRequest(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Response_getRequest(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; return data->jreq; } - static void JNICALL -nxt_java_Response_commit(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Response_commit(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); nxt_java_OutputStream_flush_buf(env, req); } - static void JNICALL -nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray loc) -{ +nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray loc) { int rc; - char *loc_str; + char *loc_str; jsize loc_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - static const char location[] = "Location"; - static const uint32_t location_len = sizeof(location) - 1; + static const char location[] = "Location"; + static const uint32_t location_len = sizeof(location) - 1; req = nxt_jlong2ptr(req_info_ptr); @@ -637,7 +592,7 @@ nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, loc_len = (*env)->GetArrayLength(env, loc); req = nxt_java_get_response_info(req_info_ptr, 1, - location_len + loc_len + 2); + location_len + loc_len + 2); if (req == NULL) { return; } @@ -651,7 +606,7 @@ nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, req->response->status = 302; rc = nxt_java_response_set_header(req_info_ptr, location, location_len, - loc_str, loc_len); + loc_str, loc_len); if (rc != NXT_UNIT_OK) { // throw } @@ -661,16 +616,14 @@ nxt_java_Response_sendRedirect(JNIEnv *env, jclass cls, nxt_unit_response_send(req); } - static int -nxt_java_response_set_header(jlong req_info_ptr, - const char *name, jint name_len, const char *value, jint value_len) -{ +nxt_java_response_set_header(jlong req_info_ptr, const char *name, + jint name_len, const char *value, jint value_len) { int add_field; - char *dst; - nxt_unit_field_t *f, *e; - nxt_unit_response_t *resp; - nxt_unit_request_info_t *req; + char *dst; + nxt_unit_field_t *f, *e; + nxt_unit_response_t *resp; + nxt_unit_request_info_t *req; req = nxt_java_get_response_info(req_info_ptr, 0, 0); if (req == NULL) { @@ -684,7 +637,7 @@ nxt_java_response_set_header(jlong req_info_ptr, add_field = 1; - for ( ;; ) { + for (;;) { f = nxt_java_findHeader(f, e, name, name_len); if (f == NULL) { break; @@ -693,11 +646,11 @@ nxt_java_response_set_header(jlong req_info_ptr, if (add_field && f->value_length >= (uint32_t) value_len) { dst = nxt_unit_sptr_get(&f->value); memcpy(dst, value, value_len); - dst[value_len] = '\0'; + dst[value_len] = '\0'; f->value_length = value_len; add_field = 0; - f->skip = 0; + f->skip = 0; } else { f->skip = 1; @@ -718,15 +671,13 @@ nxt_java_response_set_header(jlong req_info_ptr, return nxt_unit_response_add_field(req, name, name_len, value, value_len); } - static void JNICALL -nxt_java_Response_setHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jarray value) -{ +nxt_java_Response_setHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jarray value) { int rc; - char *name_str, *value_str; + char *name_str, *value_str; jsize name_len, value_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL); if (name_str == NULL) { @@ -745,11 +696,11 @@ nxt_java_Response_setHeader(JNIEnv *env, jclass cls, return; } - name_len = (*env)->GetArrayLength(env, name); + name_len = (*env)->GetArrayLength(env, name); value_len = (*env)->GetArrayLength(env, value); rc = nxt_java_response_set_header(req_info_ptr, name_str, name_len, - value_str, value_len); + value_str, value_len); if (rc != NXT_UNIT_OK) { // throw } @@ -758,15 +709,13 @@ nxt_java_Response_setHeader(JNIEnv *env, jclass cls, (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); } - static void JNICALL -nxt_java_Response_removeHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name) -{ +nxt_java_Response_removeHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name) { int rc; - char *name_str; + char *name_str; jsize name_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; name_len = (*env)->GetArrayLength(env, name); @@ -785,14 +734,12 @@ nxt_java_Response_removeHeader(JNIEnv *env, jclass cls, (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); } - static int -nxt_java_response_remove_header(jlong req_info_ptr, - const char *name, jint name_len) -{ - nxt_unit_field_t *f, *e; - nxt_unit_response_t *resp; - nxt_unit_request_info_t *req; +nxt_java_response_remove_header(jlong req_info_ptr, const char *name, + jint name_len) { + nxt_unit_field_t *f, *e; + nxt_unit_response_t *resp; + nxt_unit_request_info_t *req; req = nxt_java_get_response_info(req_info_ptr, 0, 0); if (req == NULL) { @@ -804,7 +751,7 @@ nxt_java_response_remove_header(jlong req_info_ptr, f = resp->fields; e = f + resp->fields_count; - for ( ;; ) { + for (;;) { f = nxt_java_findHeader(f, e, name, name_len); if (f == NULL) { break; @@ -818,15 +765,13 @@ nxt_java_response_remove_header(jlong req_info_ptr, return NXT_UNIT_OK; } - static void JNICALL -nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, - jlong req_info_ptr, jarray name, jint value) -{ - int value_len, rc; - char value_str[40]; - char *name_str; - jsize name_len; +nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray name, jint value) { + int value_len, rc; + char value_str[40]; + char *name_str; + jsize name_len; value_len = snprintf(value_str, sizeof(value_str), "%d", (int) value); @@ -835,12 +780,12 @@ nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, name_str = (*env)->GetPrimitiveArrayCritical(env, name, NULL); if (name_str == NULL) { nxt_unit_req_warn(nxt_jlong2ptr(req_info_ptr), - "setIntHeader: failed to get name content"); + "setIntHeader: failed to get name content"); return; } rc = nxt_java_response_set_header(req_info_ptr, name_str, name_len, - value_str, value_len); + value_str, value_len); if (rc != NXT_UNIT_OK) { // throw } @@ -848,12 +793,10 @@ nxt_java_Response_setIntHeader(JNIEnv *env, jclass cls, (*env)->ReleasePrimitiveArrayCritical(env, name, name_str, 0); } - static void JNICALL nxt_java_Response_setStatus(JNIEnv *env, jclass cls, jlong req_info_ptr, - jint sc) -{ - nxt_unit_request_info_t *req; + jint sc) { + nxt_unit_request_info_t *req; req = nxt_java_get_response_info(req_info_ptr, 0, 0); if (req == NULL) { @@ -863,12 +806,10 @@ nxt_java_Response_setStatus(JNIEnv *env, jclass cls, jlong req_info_ptr, req->response->status = sc; } - static jstring JNICALL -nxt_java_Response_getContentType(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_field_t *f; - nxt_unit_request_info_t *req; +nxt_java_Response_getContentType(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_field_t *f; + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -883,22 +824,20 @@ nxt_java_Response_getContentType(JNIEnv *env, jclass cls, jlong req_info_ptr) } f = nxt_java_findHeader(req->response->fields, - req->response->fields + req->response->fields_count, - "Content-Type", sizeof("Content-Type") - 1); + req->response->fields + req->response->fields_count, "Content-Type", + sizeof("Content-Type") - 1); if (f == NULL) { return NULL; } return nxt_java_newString(env, nxt_unit_sptr_get(&f->value), - f->value_length); + f->value_length); } - static jboolean JNICALL -nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; +nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; req = nxt_jlong2ptr(req_info_ptr); @@ -909,13 +848,11 @@ nxt_java_Response_isCommitted(JNIEnv *env, jclass cls, jlong req_info_ptr) return 0; } - static void JNICALL -nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_buf_t *buf; - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_buf_t *buf; + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; req = nxt_jlong2ptr(req_info_ptr); @@ -932,7 +869,7 @@ nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr) } if (nxt_unit_response_is_init(req)) { - req->response->status = 200; + req->response->status = 200; req->response->fields_count = 0; buf = req->response_buf; @@ -942,14 +879,12 @@ nxt_java_Response_reset(JNIEnv *env, jclass cls, jlong req_info_ptr) } } - static void JNICALL -nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; if (data->buf != NULL && data->buf->free > data->buf->start) { @@ -957,15 +892,13 @@ nxt_java_Response_resetBuffer(JNIEnv *env, jclass cls, jlong req_info_ptr) } } - static void JNICALL nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr, - jint size) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; + jint size) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; if (data->buf_size == (uint32_t) size) { @@ -985,33 +918,28 @@ nxt_java_Response_setBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr, } if (data->buf != NULL - && (uint32_t) (data->buf->end - data->buf->start) < data->buf_size) - { + && (uint32_t) (data->buf->end - data->buf->start) < data->buf_size) { nxt_unit_buf_free(data->buf); data->buf = NULL; } } - static jint JNICALL -nxt_java_Response_getBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ - nxt_unit_request_info_t *req; - nxt_java_request_data_t *data; +nxt_java_Response_getBufferSize(JNIEnv *env, jclass cls, jlong req_info_ptr) { + nxt_unit_request_info_t *req; + nxt_java_request_data_t *data; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); data = req->data; return data->buf_size; } - static void JNICALL nxt_java_Response_setContentLength(JNIEnv *env, jclass cls, jlong req_info_ptr, - jlong len) -{ - nxt_unit_request_info_t *req; + jlong len) { + nxt_unit_request_info_t *req; req = nxt_java_get_response_info(req_info_ptr, 0, 0); if (req == NULL) { @@ -1021,17 +949,15 @@ nxt_java_Response_setContentLength(JNIEnv *env, jclass cls, jlong req_info_ptr, req->response->content_length = len; } - static void JNICALL nxt_java_Response_setContentType(JNIEnv *env, jclass cls, jlong req_info_ptr, - jarray type) -{ - int rc; - char *type_str; - jsize type_len; + jarray type) { + int rc; + char *type_str; + jsize type_len; - static const char content_type[] = "Content-Type"; - static const uint32_t content_type_len = sizeof(content_type) - 1; + static const char content_type[] = "Content-Type"; + static const uint32_t content_type_len = sizeof(content_type) - 1; type_len = (*env)->GetArrayLength(env, type); @@ -1040,9 +966,8 @@ nxt_java_Response_setContentType(JNIEnv *env, jclass cls, jlong req_info_ptr, return; } - rc = nxt_java_response_set_header(req_info_ptr, - content_type, content_type_len, - type_str, type_len); + rc = nxt_java_response_set_header(req_info_ptr, content_type, + content_type_len, type_str, type_len); if (rc != NXT_UNIT_OK) { // throw } @@ -1050,23 +975,20 @@ nxt_java_Response_setContentType(JNIEnv *env, jclass cls, jlong req_info_ptr, (*env)->ReleasePrimitiveArrayCritical(env, type, type_str, 0); } - static void JNICALL -nxt_java_Response_removeContentType(JNIEnv *env, jclass cls, jlong req_info_ptr) -{ +nxt_java_Response_removeContentType(JNIEnv *env, jclass cls, + jlong req_info_ptr) { nxt_java_response_remove_header(req_info_ptr, "Content-Type", - sizeof("Content-Type") - 1); + sizeof("Content-Type") - 1); } - static void JNICALL -nxt_java_Response_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg) -{ - char *msg_str; +nxt_java_Response_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg) { + char *msg_str; jsize msg_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); msg_len = (*env)->GetArrayLength(env, msg); msg_str = (*env)->GetPrimitiveArrayCritical(env, msg, NULL); @@ -1080,16 +1002,15 @@ nxt_java_Response_log(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg) (*env)->ReleasePrimitiveArrayCritical(env, msg, msg_str, 0); } - static void JNICALL -nxt_java_Response_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg) -{ +nxt_java_Response_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, + jarray msg) { #if (NXT_DEBUG) - char *msg_str; + char *msg_str; jsize msg_len; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - req = nxt_jlong2ptr(req_info_ptr); + req = nxt_jlong2ptr(req_info_ptr); msg_len = (*env)->GetArrayLength(env, msg); msg_str = (*env)->GetPrimitiveArrayCritical(env, msg, NULL); @@ -1103,4 +1024,3 @@ nxt_java_Response_trace(JNIEnv *env, jclass cls, jlong req_info_ptr, jarray msg) (*env)->ReleasePrimitiveArrayCritical(env, msg, msg_str, 0); #endif } - diff --git a/src/java/nxt_jni_Response.h b/src/java/nxt_jni_Response.h index d10dba58b..f7e6ef6b1 100644 --- a/src/java/nxt_jni_Response.h +++ b/src/java/nxt_jni_Response.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,8 +10,10 @@ #include -int nxt_java_initResponse(JNIEnv *env, jobject cl); +int +nxt_java_initResponse(JNIEnv *env, jobject cl); -jobject nxt_java_newResponse(JNIEnv *env, nxt_unit_request_info_t *req); +jobject +nxt_java_newResponse(JNIEnv *env, nxt_unit_request_info_t *req); -#endif /* _NXT_JAVA_RESPONSE_H_INCLUDED_ */ +#endif /* _NXT_JAVA_RESPONSE_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_Thread.c b/src/java/nxt_jni_Thread.c index 43dd90bdd..c96f9253c 100644 --- a/src/java/nxt_jni_Thread.c +++ b/src/java/nxt_jni_Thread.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,16 +8,14 @@ #include "nxt_jni_Thread.h" -static jclass nxt_java_Thread_class; -static jmethodID nxt_java_Thread_currentThread; -static jmethodID nxt_java_Thread_getContextClassLoader; -static jmethodID nxt_java_Thread_setContextClassLoader; - +static jclass nxt_java_Thread_class; +static jmethodID nxt_java_Thread_currentThread; +static jmethodID nxt_java_Thread_getContextClassLoader; +static jmethodID nxt_java_Thread_setContextClassLoader; int -nxt_java_initThread(JNIEnv *env) -{ - jclass cls; +nxt_java_initThread(JNIEnv *env) { + jclass cls; cls = (*env)->FindClass(env, "java/lang/Thread"); if (cls == NULL) { @@ -41,7 +38,7 @@ nxt_java_initThread(JNIEnv *env) "getContextClassLoader", "()Ljava/lang/ClassLoader;"); if (nxt_java_Thread_getContextClassLoader == NULL) { nxt_unit_warn(NULL, "java.lang.Thread.getContextClassLoader() " - "not found"); + "not found"); goto failed; } @@ -49,7 +46,7 @@ nxt_java_initThread(JNIEnv *env) "setContextClassLoader", "(Ljava/lang/ClassLoader;)V"); if (nxt_java_Thread_setContextClassLoader == NULL) { nxt_unit_warn(NULL, "java.lang.Thread.setContextClassLoader() " - "not found"); + "not found"); goto failed; } @@ -62,33 +59,31 @@ nxt_java_initThread(JNIEnv *env) } void -nxt_java_setContextClassLoader(JNIEnv *env, jobject cl) -{ +nxt_java_setContextClassLoader(JNIEnv *env, jobject cl) { jobject thread; thread = (*env)->CallStaticObjectMethod(env, nxt_java_Thread_class, - nxt_java_Thread_currentThread); + nxt_java_Thread_currentThread); if (thread == NULL) { return; } (*env)->CallVoidMethod(env, thread, nxt_java_Thread_setContextClassLoader, - cl); + cl); } jobject -nxt_java_getContextClassLoader(JNIEnv *env) -{ +nxt_java_getContextClassLoader(JNIEnv *env) { jobject thread; thread = (*env)->CallStaticObjectMethod(env, nxt_java_Thread_class, - nxt_java_Thread_currentThread); + nxt_java_Thread_currentThread); if (thread == NULL) { return NULL; } return (*env)->CallObjectMethod(env, thread, - nxt_java_Thread_getContextClassLoader); + nxt_java_Thread_getContextClassLoader); } diff --git a/src/java/nxt_jni_Thread.h b/src/java/nxt_jni_Thread.h index 4d0b650eb..7a366cb95 100644 --- a/src/java/nxt_jni_Thread.h +++ b/src/java/nxt_jni_Thread.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -10,11 +9,13 @@ #include -int nxt_java_initThread(JNIEnv *env); - -void nxt_java_setContextClassLoader(JNIEnv *env, jobject cl); +int +nxt_java_initThread(JNIEnv *env); -jobject nxt_java_getContextClassLoader(JNIEnv *env); +void +nxt_java_setContextClassLoader(JNIEnv *env, jobject cl); -#endif /* _NXT_JAVA_THREAD_H_INCLUDED_ */ +jobject +nxt_java_getContextClassLoader(JNIEnv *env); +#endif /* _NXT_JAVA_THREAD_H_INCLUDED_ */ diff --git a/src/java/nxt_jni_URLClassLoader.c b/src/java/nxt_jni_URLClassLoader.c index bf3ab0c38..fb4bbdb15 100644 --- a/src/java/nxt_jni_URLClassLoader.c +++ b/src/java/nxt_jni_URLClassLoader.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,20 +8,18 @@ #include "nxt_jni_URLClassLoader.h" -static jclass nxt_java_URLClassLoader_class; -static jmethodID nxt_java_URLClassLoader_ctor; -static jmethodID nxt_java_URLClassLoader_parent_ctor; -static jmethodID nxt_java_URLClassLoader_loadClass; -static jmethodID nxt_java_URLClassLoader_addURL; - -static jclass nxt_java_URL_class; -static jmethodID nxt_java_URL_ctor; +static jclass nxt_java_URLClassLoader_class; +static jmethodID nxt_java_URLClassLoader_ctor; +static jmethodID nxt_java_URLClassLoader_parent_ctor; +static jmethodID nxt_java_URLClassLoader_loadClass; +static jmethodID nxt_java_URLClassLoader_addURL; +static jclass nxt_java_URL_class; +static jmethodID nxt_java_URL_ctor; int -nxt_java_initURLClassLoader(JNIEnv *env) -{ - jclass cls; +nxt_java_initURLClassLoader(JNIEnv *env) { + jclass cls; cls = (*env)->FindClass(env, "java/net/URLClassLoader"); if (cls == NULL) { @@ -34,8 +31,8 @@ nxt_java_initURLClassLoader(JNIEnv *env) (*env)->DeleteLocalRef(env, cls); cls = nxt_java_URLClassLoader_class; - nxt_java_URLClassLoader_ctor = (*env)->GetMethodID(env, cls, - "", "([Ljava/net/URL;)V"); + nxt_java_URLClassLoader_ctor + = (*env)->GetMethodID(env, cls, "", "([Ljava/net/URL;)V"); if (nxt_java_URLClassLoader_ctor == NULL) { nxt_unit_warn(NULL, "java.net.URLClassLoader constructor not found"); goto failed; @@ -55,8 +52,8 @@ nxt_java_initURLClassLoader(JNIEnv *env) goto failed; } - nxt_java_URLClassLoader_addURL = (*env)->GetMethodID(env, cls, - "addURL", "(Ljava/net/URL;)V"); + nxt_java_URLClassLoader_addURL + = (*env)->GetMethodID(env, cls, "addURL", "(Ljava/net/URL;)V"); if (nxt_java_URLClassLoader_addURL == NULL) { nxt_unit_warn(NULL, "java.net.URLClassLoader.addURL not found"); goto failed; @@ -72,8 +69,8 @@ nxt_java_initURLClassLoader(JNIEnv *env) (*env)->DeleteLocalRef(env, cls); cls = nxt_java_URL_class; - nxt_java_URL_ctor = (*env)->GetMethodID(env, cls, - "", "(Ljava/lang/String;)V"); + nxt_java_URL_ctor + = (*env)->GetMethodID(env, cls, "", "(Ljava/lang/String;)V"); if (nxt_java_URL_ctor == NULL) { nxt_unit_warn(NULL, "java.net.URL constructor not found"); goto failed; @@ -87,11 +84,9 @@ nxt_java_initURLClassLoader(JNIEnv *env) return NXT_UNIT_ERROR; } - jobject -nxt_java_newURLClassLoader(JNIEnv *env, int url_count, char **urls) -{ - jobjectArray jurls; +nxt_java_newURLClassLoader(JNIEnv *env, int url_count, char **urls) { + jobjectArray jurls; jurls = nxt_java_newURLs(env, url_count, urls); if (jurls == NULL) { @@ -99,15 +94,13 @@ nxt_java_newURLClassLoader(JNIEnv *env, int url_count, char **urls) } return (*env)->NewObject(env, nxt_java_URLClassLoader_class, - nxt_java_URLClassLoader_ctor, jurls); + nxt_java_URLClassLoader_ctor, jurls); } - jobject nxt_java_newURLClassLoader_parent(JNIEnv *env, int url_count, char **urls, - jobject parent) -{ - jobjectArray jurls; + jobject parent) { + jobjectArray jurls; jurls = nxt_java_newURLs(env, url_count, urls); if (jurls == NULL) { @@ -115,18 +108,15 @@ nxt_java_newURLClassLoader_parent(JNIEnv *env, int url_count, char **urls, } return (*env)->NewObject(env, nxt_java_URLClassLoader_class, - nxt_java_URLClassLoader_parent_ctor, jurls, - parent); + nxt_java_URLClassLoader_parent_ctor, jurls, parent); } - jobjectArray -nxt_java_newURLs(JNIEnv *env, int url_count, char **urls) -{ - int i; - jstring surl; - jobject jurl; - jobjectArray jurls; +nxt_java_newURLs(JNIEnv *env, int url_count, char **urls) { + int i; + jstring surl; + jobject jurl; + jobjectArray jurls; jurls = (*env)->NewObjectArray(env, url_count, nxt_java_URL_class, NULL); if (jurls == NULL) { @@ -140,7 +130,7 @@ nxt_java_newURLs(JNIEnv *env, int url_count, char **urls) } jurl = (*env)->NewObject(env, nxt_java_URL_class, nxt_java_URL_ctor, - surl); + surl); if (jurl == NULL) { return NULL; } @@ -151,11 +141,9 @@ nxt_java_newURLs(JNIEnv *env, int url_count, char **urls) return jurls; } - jclass -nxt_java_loadClass(JNIEnv *env, jobject cl, const char *name) -{ - jstring jname; +nxt_java_loadClass(JNIEnv *env, jobject cl, const char *name) { + jstring jname; jname = (*env)->NewStringUTF(env, name); if (jname == NULL) { @@ -163,15 +151,13 @@ nxt_java_loadClass(JNIEnv *env, jobject cl, const char *name) } return (*env)->CallObjectMethod(env, cl, nxt_java_URLClassLoader_loadClass, - jname); + jname); } - void -nxt_java_addURL(JNIEnv *env, jobject cl, const char *url) -{ - jstring surl; - jobject jurl; +nxt_java_addURL(JNIEnv *env, jobject cl, const char *url) { + jstring surl; + jobject jurl; surl = (*env)->NewStringUTF(env, url); if (surl == NULL) { diff --git a/src/java/nxt_jni_URLClassLoader.h b/src/java/nxt_jni_URLClassLoader.h index 4cf2c0ec9..19a891947 100644 --- a/src/java/nxt_jni_URLClassLoader.h +++ b/src/java/nxt_jni_URLClassLoader.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -10,18 +9,23 @@ #include -int nxt_java_initURLClassLoader(JNIEnv *env); - -jobject nxt_java_newURLClassLoader(JNIEnv *env, int url_count, char **urls); +int +nxt_java_initURLClassLoader(JNIEnv *env); -jobject nxt_java_newURLClassLoader_parent(JNIEnv *env, int url_count, - char **urls, jobject parent); +jobject +nxt_java_newURLClassLoader(JNIEnv *env, int url_count, char **urls); -jobjectArray nxt_java_newURLs(JNIEnv *env, int url_count, char **urls); +jobject +nxt_java_newURLClassLoader_parent(JNIEnv *env, int url_count, char **urls, + jobject parent); -jclass nxt_java_loadClass(JNIEnv *env, jobject cl, const char *name); +jobjectArray +nxt_java_newURLs(JNIEnv *env, int url_count, char **urls); -void nxt_java_addURL(JNIEnv *env, jobject cl, const char *url); +jclass +nxt_java_loadClass(JNIEnv *env, jobject cl, const char *name); -#endif /* _NXT_JAVA_URLCLASSLOADER_H_INCLUDED_ */ +void +nxt_java_addURL(JNIEnv *env, jobject cl, const char *url); +#endif /* _NXT_JAVA_URLCLASSLOADER_H_INCLUDED_ */ diff --git a/src/nodejs/unit-http/nxt_napi.h b/src/nodejs/unit-http/nxt_napi.h index d9721a406..cdaa2fbb5 100644 --- a/src/nodejs/unit-http/nxt_napi.h +++ b/src/nodejs/unit-http/nxt_napi.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -30,22 +29,19 @@ extern "C" { struct nxt_napi { - struct exception { - exception(const char *s) : str(s) { } + exception(const char *s) : str(s) { + } const char *str; }; + nxt_napi(napi_env env) : env_(env) { + } - nxt_napi(napi_env env) : env_(env) { } - - - inline napi_value - coerce_to_string(napi_value val) - { - napi_value res; - napi_status status; + inline napi_value coerce_to_string(napi_value val) { + napi_value res; + napi_status status; status = napi_coerce_to_string(env_, val, &res); if (status != napi_ok) { @@ -55,12 +51,9 @@ struct nxt_napi { return res; } - - inline napi_value - create_buffer(size_t size, void **data) - { - napi_value res; - napi_status status; + inline napi_value create_buffer(size_t size, void **data) { + napi_value res; + napi_status status; status = napi_create_buffer(env_, size, data, &res); if (status != napi_ok) { @@ -70,12 +63,10 @@ struct nxt_napi { return res; } - - inline napi_value - create_function(const char *name, size_t len, napi_callback cb, void *data) - { - napi_value res; - napi_status status; + inline napi_value create_function(const char *name, size_t len, + napi_callback cb, void *data) { + napi_value res; + napi_status status; status = napi_create_function(env_, name, len, cb, data, &res); if (status != napi_ok) { @@ -85,19 +76,13 @@ struct nxt_napi { return res; } - - inline napi_value - create_function(napi_callback cb) - { + inline napi_value create_function(napi_callback cb) { return create_function(NULL, 0, cb, NULL); } - - inline napi_value - create_object() - { - napi_value res; - napi_status status; + inline napi_value create_object() { + napi_value res; + napi_status status; status = napi_create_object(env_, &res); if (status != napi_ok) { @@ -107,12 +92,9 @@ struct nxt_napi { return res; } - - inline napi_ref - create_reference(napi_value val, int ref_count = 1) - { - napi_ref res; - napi_status status; + inline napi_ref create_reference(napi_value val, int ref_count = 1) { + napi_ref res; + napi_status status; status = napi_create_reference(env_, val, ref_count, &res); if (status != napi_ok) { @@ -122,12 +104,9 @@ struct nxt_napi { return res; } - - inline napi_value - create_string_latin1(const char *str, size_t len) - { - napi_value res; - napi_status status; + inline napi_value create_string_latin1(const char *str, size_t len) { + napi_value res; + napi_status status; status = napi_create_string_latin1(env_, str, len, &res); if (status != napi_ok) { @@ -137,27 +116,21 @@ struct nxt_napi { return res; } - - inline napi_value - create_string_latin1(nxt_unit_sptr_t &str, size_t len) - { - const char *p; + inline napi_value create_string_latin1(nxt_unit_sptr_t &str, size_t len) { + const char *p; p = (const char *) nxt_unit_sptr_get(&str); return create_string_latin1(p, len); } - - inline napi_value - define_class(const char *name, napi_callback ctor, size_t prop_count, - const napi_property_descriptor* props) - { - napi_value res; - napi_status status; + inline napi_value define_class(const char *name, napi_callback ctor, + size_t prop_count, const napi_property_descriptor *props) { + napi_value res; + napi_status status; status = napi_define_class(env_, name, NAPI_AUTO_LENGTH, ctor, nullptr, - prop_count, props, &res); + prop_count, props, &res); if (status != napi_ok) { throw exception("Failed to define class"); } @@ -165,19 +138,13 @@ struct nxt_napi { return res; } - - inline void - delete_reference(napi_ref ref) - { + inline void delete_reference(napi_ref ref) { napi_delete_reference(env_, ref); } - - inline uint32_t - get_array_length(napi_value val) - { - uint32_t res; - napi_status status; + inline uint32_t get_array_length(napi_value val) { + uint32_t res; + napi_status status; status = napi_get_array_length(env_, val, &res); if (status != napi_ok) { @@ -187,12 +154,9 @@ struct nxt_napi { return res; } - - inline void * - get_buffer_info(napi_value val, size_t &size) - { - void *res; - napi_status status; + inline void *get_buffer_info(napi_value val, size_t &size) { + void *res; + napi_status status; status = napi_get_buffer_info(env_, val, &res, &size); if (status != napi_ok) { @@ -202,12 +166,10 @@ struct nxt_napi { return res; } - - inline napi_value - get_cb_info(napi_callback_info info, size_t &argc, napi_value *argv) - { - napi_value res; - napi_status status; + inline napi_value get_cb_info(napi_callback_info info, size_t &argc, + napi_value *argv) { + napi_value res; + napi_status status; status = napi_get_cb_info(env_, info, &argc, argv, &res, nullptr); if (status != napi_ok) { @@ -217,12 +179,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_cb_info(napi_callback_info info) - { - napi_value res; - napi_status status; + inline napi_value get_cb_info(napi_callback_info info) { + napi_value res; + napi_status status; status = napi_get_cb_info(env_, info, nullptr, nullptr, &res, nullptr); if (status != napi_ok) { @@ -232,15 +191,12 @@ struct nxt_napi { return res; } - - inline napi_value - get_cb_info(napi_callback_info info, napi_value &arg) - { - size_t argc; - napi_value res; + inline napi_value get_cb_info(napi_callback_info info, napi_value &arg) { + size_t argc; + napi_value res; argc = 1; - res = get_cb_info(info, argc, &arg); + res = get_cb_info(info, argc, &arg); if (argc != 1) { throw exception("Wrong args count. Expected 1"); @@ -249,12 +205,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_element(napi_value obj, uint32_t i) - { - napi_value res; - napi_status status; + inline napi_value get_element(napi_value obj, uint32_t i) { + napi_value res; + napi_status status; status = napi_get_element(env_, obj, i, &res); if (status != napi_ok) { @@ -264,12 +217,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_named_property(napi_value obj, const char *name) - { - napi_value res; - napi_status status; + inline napi_value get_named_property(napi_value obj, const char *name) { + napi_value res; + napi_status status; status = napi_get_named_property(env_, obj, name, &res); if (status != napi_ok) { @@ -279,12 +229,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_new_target(napi_callback_info info) - { - napi_value res; - napi_status status; + inline napi_value get_new_target(napi_callback_info info) { + napi_value res; + napi_status status; status = napi_get_new_target(env_, info, &res); if (status != napi_ok) { @@ -294,12 +241,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_property(napi_value val, napi_value key) - { - napi_value res; - napi_status status; + inline napi_value get_property(napi_value val, napi_value key) { + napi_value res; + napi_status status; status = napi_get_property(env_, val, key, &res); if (status != napi_ok) { @@ -309,12 +253,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_property_names(napi_value val) - { - napi_value res; - napi_status status; + inline napi_value get_property_names(napi_value val) { + napi_value res; + napi_status status; status = napi_get_property_names(env_, val, &res); if (status != napi_ok) { @@ -324,12 +265,9 @@ struct nxt_napi { return res; } - - inline napi_value - get_reference_value(napi_ref ref) - { - napi_value res; - napi_status status; + inline napi_value get_reference_value(napi_ref ref) { + napi_value res; + napi_status status; status = napi_get_reference_value(env_, ref, &res); if (status != napi_ok) { @@ -339,19 +277,13 @@ struct nxt_napi { return res; } - - inline nxt_unit_request_info_t * - get_request_info(napi_value obj) - { + inline nxt_unit_request_info_t *get_request_info(napi_value obj) { return (nxt_unit_request_info_t *) unwrap(obj); } - - inline uint32_t - get_value_bool(napi_value obj) - { - bool res; - napi_status status; + inline uint32_t get_value_bool(napi_value obj) { + bool res; + napi_status status; status = napi_get_value_bool(env_, obj, &res); if (status != napi_ok) { @@ -361,12 +293,10 @@ struct nxt_napi { return res; } - - inline size_t - get_value_string_latin1(napi_value val, char *buf, size_t bufsize) - { - size_t res; - napi_status status; + inline size_t get_value_string_latin1(napi_value val, char *buf, + size_t bufsize) { + size_t res; + napi_status status; status = napi_get_value_string_latin1(env_, val, buf, bufsize, &res); if (status != napi_ok) { @@ -376,12 +306,9 @@ struct nxt_napi { return res; } - - inline uint32_t - get_value_uint32(napi_value obj) - { - uint32_t res; - napi_status status; + inline uint32_t get_value_uint32(napi_value obj) { + uint32_t res; + napi_status status; status = napi_get_value_uint32(env_, obj, &res); if (status != napi_ok) { @@ -391,12 +318,10 @@ struct nxt_napi { return res; } - - inline size_t - get_value_string_utf8(napi_value val, char *buf, size_t bufsize) - { - size_t res; - napi_status status; + inline size_t get_value_string_utf8(napi_value val, char *buf, + size_t bufsize) { + size_t res; + napi_status status; status = napi_get_value_string_utf8(env_, val, buf, bufsize, &res); if (status != napi_ok) { @@ -406,12 +331,9 @@ struct nxt_napi { return res; } - - inline bool - is_array(napi_value val) - { - bool res; - napi_status status; + inline bool is_array(napi_value val) { + bool res; + napi_status status; status = napi_is_array(env_, val, &res); if (status != napi_ok) { @@ -421,12 +343,9 @@ struct nxt_napi { return res; } - - inline bool - is_buffer(napi_value val) - { - bool res; - napi_status status; + inline bool is_buffer(napi_value val) { + bool res; + napi_status status; status = napi_is_buffer(env_, val, &res); if (status != napi_ok) { @@ -436,13 +355,10 @@ struct nxt_napi { return res; } - - inline napi_value - make_callback(napi_async_context ctx, napi_value val, napi_value func, - int argc, const napi_value *argv) - { - napi_value res, ex; - napi_status status; + inline napi_value make_callback(napi_async_context ctx, napi_value val, + napi_value func, int argc, const napi_value *argv) { + napi_value res, ex; + napi_status status; status = napi_make_callback(env_, ctx, val, func, argc, argv, &res); if (status != napi_ok) { @@ -465,47 +381,33 @@ struct nxt_napi { return res; } - - inline napi_value - make_callback(napi_async_context ctx, napi_value val, napi_value func) - { + inline napi_value make_callback(napi_async_context ctx, napi_value val, + napi_value func) { return make_callback(ctx, val, func, 0, NULL); } - - inline napi_value - make_callback(napi_async_context ctx, napi_value val, napi_value func, - napi_value arg1) - { + inline napi_value make_callback(napi_async_context ctx, napi_value val, + napi_value func, napi_value arg1) { return make_callback(ctx, val, func, 1, &arg1); } - - inline napi_value - make_callback(napi_async_context ctx, napi_value val, napi_value func, - napi_value arg1, napi_value arg2) - { - napi_value args[2] = { arg1, arg2 }; + inline napi_value make_callback(napi_async_context ctx, napi_value val, + napi_value func, napi_value arg1, napi_value arg2) { + napi_value args[2] = {arg1, arg2}; return make_callback(ctx, val, func, 2, args); } - - inline napi_value - make_callback(napi_async_context ctx, napi_value val, napi_value func, - napi_value arg1, napi_value arg2, napi_value arg3) - { - napi_value args[3] = { arg1, arg2, arg3 }; + inline napi_value make_callback(napi_async_context ctx, napi_value val, + napi_value func, napi_value arg1, napi_value arg2, napi_value arg3) { + napi_value args[3] = {arg1, arg2, arg3}; return make_callback(ctx, val, func, 3, args); } - - inline napi_value - new_instance(napi_value ctor) - { - napi_value res; - napi_status status; + inline napi_value new_instance(napi_value ctor) { + napi_value res; + napi_status status; status = napi_new_instance(env_, ctor, 0, NULL, &res); if (status != napi_ok) { @@ -515,12 +417,9 @@ struct nxt_napi { return res; } - - inline napi_value - new_instance(napi_value ctor, napi_value param) - { - napi_value res; - napi_status status; + inline napi_value new_instance(napi_value ctor, napi_value param) { + napi_value res; + napi_status status; status = napi_new_instance(env_, ctor, 1, ¶m, &res); if (status != napi_ok) { @@ -530,13 +429,11 @@ struct nxt_napi { return res; } - - inline napi_value - new_instance(napi_value ctor, napi_value param1, napi_value param2) - { - napi_value res; - napi_status status; - napi_value param[2] = { param1, param2 }; + inline napi_value new_instance(napi_value ctor, napi_value param1, + napi_value param2) { + napi_value res; + napi_status status; + napi_value param[2] = {param1, param2}; status = napi_new_instance(env_, ctor, 2, param, &res); if (status != napi_ok) { @@ -546,11 +443,8 @@ struct nxt_napi { return res; } - - inline void - set_element(napi_value obj, uint32_t i, napi_value val) - { - napi_status status; + inline void set_element(napi_value obj, uint32_t i, napi_value val) { + napi_status status; status = napi_set_element(env_, obj, i, val); if (status != napi_ok) { @@ -558,11 +452,9 @@ struct nxt_napi { } } - - inline void - set_named_property(napi_value obj, const char *name, napi_value val) - { - napi_status status; + inline void set_named_property(napi_value obj, const char *name, + napi_value val) { + napi_status status; status = napi_set_named_property(env_, obj, name, val); if (status != napi_ok) { @@ -570,19 +462,14 @@ struct nxt_napi { } } - - inline void - set_named_property(napi_value obj, const char *name, napi_callback cb) - { + inline void set_named_property(napi_value obj, const char *name, + napi_callback cb) { set_named_property(obj, name, create_function(cb)); } - - inline napi_value - set_named_property(napi_value obj, const char *name, nxt_unit_sptr_t &val, - size_t len) - { - napi_value str; + inline napi_value set_named_property(napi_value obj, const char *name, + nxt_unit_sptr_t &val, size_t len) { + napi_value str; str = create_string_latin1(val, len); @@ -591,20 +478,14 @@ struct nxt_napi { return str; } - - template - inline void - set_named_property(napi_value obj, const char *name, T val) - { + template + inline void set_named_property(napi_value obj, const char *name, T val) { set_named_property(obj, name, create(val)); } - - inline napi_value - create(int32_t val) - { - napi_value ptr; - napi_status status; + inline napi_value create(int32_t val) { + napi_value ptr; + napi_status status; status = napi_create_int32(env_, val, &ptr); if (status != napi_ok) { @@ -614,12 +495,9 @@ struct nxt_napi { return ptr; } - - inline napi_value - create(uint32_t val) - { - napi_value ptr; - napi_status status; + inline napi_value create(uint32_t val) { + napi_value ptr; + napi_status status; status = napi_create_uint32(env_, val, &ptr); if (status != napi_ok) { @@ -629,12 +507,9 @@ struct nxt_napi { return ptr; } - - inline napi_value - create(int64_t val) - { - napi_value ptr; - napi_status status; + inline napi_value create(int64_t val) { + napi_value ptr; + napi_status status; status = napi_create_int64(env_, val, &ptr); if (status != napi_ok) { @@ -644,22 +519,16 @@ struct nxt_napi { return ptr; } - - inline void - remove_wrap(napi_ref& ref) - { + inline void remove_wrap(napi_ref &ref) { if (ref != nullptr) { remove_wrap(get_reference_value(ref)); ref = nullptr; } } - - inline void * - remove_wrap(napi_value val) - { - void *res; - napi_status status; + inline void *remove_wrap(napi_value val) { + void *res; + napi_status status; status = napi_remove_wrap(env_, val, &res); if (status != napi_ok) { @@ -669,26 +538,17 @@ struct nxt_napi { return res; } - - inline void - throw_error(const char *str) - { + inline void throw_error(const char *str) { napi_throw_error(env_, NULL, str); } - - inline void - throw_error(const exception &e) - { + inline void throw_error(const exception &e) { napi_throw_error(env_, NULL, e.str); } - - inline napi_valuetype - type_of(napi_value val) - { - napi_status status; - napi_valuetype res; + inline napi_valuetype type_of(napi_value val) { + napi_status status; + napi_valuetype res; status = napi_typeof(env_, val, &res); if (status != napi_ok) { @@ -698,12 +558,9 @@ struct nxt_napi { return res; } - - inline void * - unwrap(napi_value val) - { - void *res; - napi_status status; + inline void *unwrap(napi_value val) { + void *res; + napi_status status; status = napi_unwrap(env_, val, &res); if (status != napi_ok) { @@ -713,12 +570,10 @@ struct nxt_napi { return res; } - - inline napi_ref - wrap(napi_value val, void *obj, napi_finalize fin_cb, void *hint = nullptr) - { - napi_ref res; - napi_status status; + inline napi_ref wrap(napi_value val, void *obj, napi_finalize fin_cb, + void *hint = nullptr) { + napi_ref res; + napi_status status; status = napi_wrap(env_, val, obj, fin_cb, hint, &res); if (status != napi_ok) { @@ -728,28 +583,21 @@ struct nxt_napi { return res; } - - inline - operator napi_env() - { + inline operator napi_env() { return env_; } - - napi_env env() - { + napi_env env() { return env_; } -private: - napi_env env_; + private: + napi_env env_; }; - struct nxt_handle_scope : public nxt_napi { - nxt_handle_scope(napi_env env) : nxt_napi(env) - { - napi_status status; + nxt_handle_scope(napi_env env) : nxt_napi(env) { + napi_status status; status = napi_open_handle_scope(env, &scope_); if (status != napi_ok) { @@ -757,9 +605,8 @@ struct nxt_handle_scope : public nxt_napi { } } - ~nxt_handle_scope() - { - napi_status status; + ~nxt_handle_scope() { + napi_status status; status = napi_close_handle_scope(env(), scope_); if (status != napi_ok) { @@ -767,17 +614,14 @@ struct nxt_handle_scope : public nxt_napi { } } -private: - napi_handle_scope scope_; + private: + napi_handle_scope scope_; }; - struct nxt_async_context : public nxt_napi { - nxt_async_context(napi_env env, const char *name) : - nxt_napi(env) - { - napi_value name_val; - napi_status status; + nxt_async_context(napi_env env, const char *name) : nxt_napi(env) { + napi_value name_val; + napi_status status; name_val = create_string_latin1(name, NAPI_AUTO_LENGTH); @@ -791,9 +635,8 @@ struct nxt_async_context : public nxt_napi { return context_; } - ~nxt_async_context() - { - napi_status status; + ~nxt_async_context() { + napi_status status; status = napi_async_destroy(env(), context_); if (status != napi_ok) { @@ -801,17 +644,14 @@ struct nxt_async_context : public nxt_napi { } } -private: - napi_async_context context_; + private: + napi_async_context context_; }; - struct nxt_callback_scope : public nxt_napi { - nxt_callback_scope(nxt_async_context& ctx) : - nxt_napi(ctx.env()) - { - napi_value resource; - napi_status status; + nxt_callback_scope(nxt_async_context &ctx) : nxt_napi(ctx.env()) { + napi_value resource; + napi_status status; resource = create_object(); @@ -821,9 +661,8 @@ struct nxt_callback_scope : public nxt_napi { } } - ~nxt_callback_scope() - { - napi_status status; + ~nxt_callback_scope() { + napi_status status; status = napi_close_callback_scope(env(), scope_); if (status != napi_ok) { @@ -831,8 +670,8 @@ struct nxt_callback_scope : public nxt_napi { } } -private: - napi_callback_scope scope_; + private: + napi_callback_scope scope_; }; diff --git a/src/nodejs/unit-http/unit.h b/src/nodejs/unit-http/unit.h index 1aa93073a..d25d663a7 100644 --- a/src/nodejs/unit-http/unit.h +++ b/src/nodejs/unit-http/unit.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,79 +7,82 @@ #include "nxt_napi.h" - class Unit : public nxt_napi { -public: + public: static napi_value init(napi_env env, napi_value exports); -private: + private: Unit(napi_env env, napi_value jsthis); ~Unit(); static napi_value create(napi_env env, napi_callback_info info); static void destroy(napi_env env, void *nativeObject, void *finalize_hint); - static void conn_destroy(napi_env env, void *nativeObject, void *finalize_hint); - static void sock_destroy(napi_env env, void *nativeObject, void *finalize_hint); - static void req_destroy(napi_env env, void *nativeObject, void *finalize_hint); - static void resp_destroy(napi_env env, void *nativeObject, void *finalize_hint); + static void conn_destroy(napi_env env, void *nativeObject, + void *finalize_hint); + static void sock_destroy(napi_env env, void *nativeObject, + void *finalize_hint); + static void req_destroy(napi_env env, void *nativeObject, + void *finalize_hint); + static void resp_destroy(napi_env env, void *nativeObject, + void *finalize_hint); static napi_value create_server(napi_env env, napi_callback_info info); static napi_value listen(napi_env env, napi_callback_info info); static napi_value _read(napi_env env, napi_callback_info info); static void request_handler_cb(nxt_unit_request_info_t *req); - void request_handler(nxt_unit_request_info_t *req); + void request_handler(nxt_unit_request_info_t *req); static void websocket_handler_cb(nxt_unit_websocket_frame_t *ws); - void websocket_handler(nxt_unit_websocket_frame_t *ws); + void websocket_handler(nxt_unit_websocket_frame_t *ws); static void close_handler_cb(nxt_unit_request_info_t *req); - void close_handler(nxt_unit_request_info_t *req); + void close_handler(nxt_unit_request_info_t *req); static void shm_ack_handler_cb(nxt_unit_ctx_t *ctx); - void shm_ack_handler(nxt_unit_ctx_t *ctx); + void shm_ack_handler(nxt_unit_ctx_t *ctx); - static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); + static int add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); static void remove_port(nxt_unit_t *unit, nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port); + nxt_unit_port_t *port); static void quit_cb(nxt_unit_ctx_t *ctx); - void quit(nxt_unit_ctx_t *ctx); + void quit(nxt_unit_ctx_t *ctx); napi_value get_server_object(); napi_value create_socket(napi_value server_obj, - nxt_unit_request_info_t *req); + nxt_unit_request_info_t *req); napi_value create_request(napi_value server_obj, napi_value socket, - nxt_unit_request_info_t *req); + nxt_unit_request_info_t *req); napi_value create_response(napi_value server_obj, napi_value request, - nxt_unit_request_info_t *req); + nxt_unit_request_info_t *req); napi_value create_websocket_frame(napi_value server_obj, - nxt_unit_websocket_frame_t *ws); + nxt_unit_websocket_frame_t *ws); static napi_value request_read(napi_env env, napi_callback_info info); static napi_value response_send_headers(napi_env env, - napi_callback_info info); + napi_callback_info info); static napi_value response_write(napi_env env, napi_callback_info info); static napi_value response_end(napi_env env, napi_callback_info info); static napi_value websocket_send_frame(napi_env env, - napi_callback_info info); + napi_callback_info info); static napi_value websocket_set_sock(napi_env env, napi_callback_info info); void create_headers(nxt_unit_request_info_t *req, napi_value request); void append_header(nxt_unit_field_t *f, napi_value headers, - napi_value raw_headers, uint32_t idx); + napi_value raw_headers, uint32_t idx); static napi_ref constructor_; napi_ref wrapper_; - nxt_unit_ctx_t *unit_ctx_; + nxt_unit_ctx_t *unit_ctx_; }; diff --git a/src/nxt_aix_send_file.c b/src/nxt_aix_send_file.c index 6462efd55..ace313835 100644 --- a/src/nxt_aix_send_file.c +++ b/src/nxt_aix_send_file.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,27 +8,27 @@ /* send_file() has been introduced in AIX 4.3.2 */ -ssize_t nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); - ssize_t -nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) -{ - ssize_t n; - nxt_buf_t *fb; - nxt_err_t err; - nxt_off_t file_size, sent; - nxt_uint_t nhd, ntr; - struct iovec hd[NXT_IOBUF_MAX], tr; - struct sf_parms sfp; - nxt_sendbuf_coalesce_t sb; - - sb.buf = b; +nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, + size_t limit) { + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_off_t file_size, sent; + nxt_uint_t nhd, ntr; + struct iovec hd[NXT_IOBUF_MAX], tr; + struct sf_parms sfp; + nxt_sendbuf_coalesce_t sb; + + sb.buf = b; sb.iobuf = hd; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; nhd = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -51,36 +50,36 @@ nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) } sb.iobuf = &tr; - sb.nmax = 1; + sb.nmax = 1; ntr = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); nxt_memzero(&sfp, sizeof(struct sf_parms)); if (nhd != 0) { - sfp.header_data = hd[0].iov_base; + sfp.header_data = hd[0].iov_base; sfp.header_length = hd[0].iov_len; } sfp.file_descriptor = fb->file->fd; - sfp.file_offset = fb->file_pos; - sfp.file_bytes = file_size; + sfp.file_offset = fb->file_pos; + sfp.file_bytes = file_size; if (ntr != 0) { - sfp.trailer_data = tr.iov_base; + sfp.trailer_data = tr.iov_base; sfp.trailer_length = tr.iov_len; } nxt_debug(c->socket.task, "send_file(%d) fd:%FD @%O:%O hd:%ui tr:%ui", - c->socket.fd, fb->file->fd, fb->file_pos, file_size, nhd, ntr); + c->socket.fd, fb->file->fd, fb->file_pos, file_size, nhd, ntr); n = send_file(&c->socket.fd, &sfp, 0); - err = (n == -1) ? nxt_errno : 0; + err = (n == -1) ? nxt_errno : 0; sent = sfp.bytes_sent; - nxt_debug(c->socket.task, "send_file(%d): %d sent:%O", - c->socket.fd, n, sent); + nxt_debug(c->socket.task, "send_file(%d): %d sent:%O", c->socket.fd, n, + sent); /* * -1 an error has occurred, errno contains the error code; @@ -91,7 +90,6 @@ nxt_aix_event_conn_io_send_file(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) */ if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; diff --git a/src/nxt_app_log.c b/src/nxt_app_log.c index ae57c2a2f..c066c3f94 100644 --- a/src/nxt_app_log.c +++ b/src/nxt_app_log.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,32 +7,32 @@ #include -static nxt_time_string_t nxt_log_error_time_cache; -static u_char *nxt_log_error_time(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, const char *format); -static nxt_time_string_t nxt_log_debug_time_cache; -static u_char *nxt_log_debug_time(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, const char *format); - +static nxt_time_string_t nxt_log_error_time_cache; +static u_char * +nxt_log_error_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, + const char *format); +static nxt_time_string_t nxt_log_debug_time_cache; +static u_char * +nxt_log_debug_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, + const char *format); void nxt_cdecl -nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) -{ - u_char *p, *end; +nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) { + u_char *p, *end; #if 0 u_char *syslogmsg; #endif va_list args; - nxt_thread_t *thr; - nxt_time_string_t *time_cache; + nxt_thread_t *thr; + nxt_time_string_t *time_cache; u_char msg[NXT_MAX_ERROR_STR]; thr = nxt_thread(); end = msg + NXT_MAX_ERROR_STR; - time_cache = (log->level != NXT_LOG_DEBUG) ? &nxt_log_error_time_cache: - &nxt_log_debug_time_cache; + time_cache = (log->level != NXT_LOG_DEBUG) ? &nxt_log_error_time_cache + : &nxt_log_debug_time_cache; p = nxt_thread_time_string(thr, time_cache, msg); @@ -61,7 +60,7 @@ nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) nxt_thread_tid(thr), fid); #else p = nxt_sprintf(p, end, "[%V] %PI#%PT ", &nxt_log_levels[level], nxt_pid, - nxt_thread_tid(thr)); + nxt_thread_tid(thr)); #endif if (log->ident != 0) { @@ -97,8 +96,7 @@ nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) #endif } - -static nxt_time_string_t nxt_log_error_time_cache = { +static nxt_time_string_t nxt_log_error_time_cache = { (nxt_atomic_uint_t) -1, nxt_log_error_time, "%4d/%02d/%02d %02d:%02d:%02d ", @@ -107,18 +105,14 @@ static nxt_time_string_t nxt_log_error_time_cache = { NXT_THREAD_TIME_SEC, }; - static u_char * nxt_log_error_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, - const char *format) -{ - return nxt_sprintf(buf, buf + size, format, - tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec); + const char *format) { + return nxt_sprintf(buf, buf + size, format, tm->tm_year + 1900, + tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); } - -static nxt_time_string_t nxt_log_debug_time_cache = { +static nxt_time_string_t nxt_log_debug_time_cache = { (nxt_atomic_uint_t) -1, nxt_log_debug_time, "%4d/%02d/%02d %02d:%02d:%02d.%03d ", @@ -127,13 +121,10 @@ static nxt_time_string_t nxt_log_debug_time_cache = { NXT_THREAD_TIME_MSEC, }; - static u_char * nxt_log_debug_time(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, - const char *format) -{ - return nxt_sprintf(buf, buf + size, format, - tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, - now->nsec / 1000000); + const char *format) { + return nxt_sprintf(buf, buf + size, format, tm->tm_year + 1900, + tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, + now->nsec / 1000000); } diff --git a/src/nxt_app_nncq.h b/src/nxt_app_nncq.h index f9b8ce0c8..2d80704a9 100644 --- a/src/nxt_app_nncq.h +++ b/src/nxt_app_nncq.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,101 +8,78 @@ /* Appilcation Numeric Naive Circular Queue */ -#define NXT_APP_NNCQ_SIZE 131072 +#define NXT_APP_NNCQ_SIZE 131072 typedef uint32_t nxt_app_nncq_atomic_t; typedef uint16_t nxt_app_nncq_cycle_t; typedef struct { - nxt_app_nncq_atomic_t head; - nxt_app_nncq_atomic_t entries[NXT_APP_NNCQ_SIZE]; - nxt_app_nncq_atomic_t tail; + nxt_app_nncq_atomic_t head; + nxt_app_nncq_atomic_t entries[NXT_APP_NNCQ_SIZE]; + nxt_app_nncq_atomic_t tail; } nxt_app_nncq_t; - static inline nxt_app_nncq_atomic_t -nxt_app_nncq_head(nxt_app_nncq_t const volatile *q) -{ +nxt_app_nncq_head(nxt_app_nncq_t const volatile *q) { return q->head; } - static inline nxt_app_nncq_atomic_t -nxt_app_nncq_tail(nxt_app_nncq_t const volatile *q) -{ +nxt_app_nncq_tail(nxt_app_nncq_t const volatile *q) { return q->tail; } - static inline void -nxt_app_nncq_tail_cmp_inc(nxt_app_nncq_t volatile *q, nxt_app_nncq_atomic_t t) -{ +nxt_app_nncq_tail_cmp_inc(nxt_app_nncq_t volatile *q, nxt_app_nncq_atomic_t t) { nxt_atomic_cmp_set(&q->tail, t, t + 1); } - static inline nxt_app_nncq_atomic_t -nxt_app_nncq_index(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) -{ +nxt_app_nncq_index(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) { return i % NXT_APP_NNCQ_SIZE; } - static inline nxt_app_nncq_atomic_t -nxt_app_nncq_map(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) -{ +nxt_app_nncq_map(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) { return i % NXT_APP_NNCQ_SIZE; } - static inline nxt_app_nncq_cycle_t -nxt_app_nncq_cycle(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) -{ +nxt_app_nncq_cycle(nxt_app_nncq_t const volatile *q, nxt_app_nncq_atomic_t i) { return i / NXT_APP_NNCQ_SIZE; } - static inline nxt_app_nncq_cycle_t nxt_app_nncq_next_cycle(nxt_app_nncq_t const volatile *q, - nxt_app_nncq_cycle_t i) -{ + nxt_app_nncq_cycle_t i) { return i + 1; } - static inline nxt_app_nncq_atomic_t nxt_app_nncq_new_entry(nxt_app_nncq_t const volatile *q, - nxt_app_nncq_cycle_t cycle, - nxt_app_nncq_atomic_t i) -{ + nxt_app_nncq_cycle_t cycle, nxt_app_nncq_atomic_t i) { return cycle * NXT_APP_NNCQ_SIZE + (i % NXT_APP_NNCQ_SIZE); } - static inline nxt_app_nncq_atomic_t -nxt_app_nncq_empty(nxt_app_nncq_t const volatile *q) -{ +nxt_app_nncq_empty(nxt_app_nncq_t const volatile *q) { return NXT_APP_NNCQ_SIZE; } - static void -nxt_app_nncq_init(nxt_app_nncq_t volatile *q) -{ +nxt_app_nncq_init(nxt_app_nncq_t volatile *q) { q->head = NXT_APP_NNCQ_SIZE; nxt_memzero((void *) q->entries, - NXT_APP_NNCQ_SIZE * sizeof(nxt_app_nncq_atomic_t)); + NXT_APP_NNCQ_SIZE * sizeof(nxt_app_nncq_atomic_t)); q->tail = NXT_APP_NNCQ_SIZE; } - static void -nxt_app_nncq_enqueue(nxt_app_nncq_t volatile *q, nxt_app_nncq_atomic_t val) -{ - nxt_app_nncq_cycle_t e_cycle, t_cycle; - nxt_app_nncq_atomic_t n, t, e, j; +nxt_app_nncq_enqueue(nxt_app_nncq_t volatile *q, nxt_app_nncq_atomic_t val) { + nxt_app_nncq_cycle_t e_cycle, t_cycle; + nxt_app_nncq_atomic_t n, t, e, j; - for ( ;; ) { + for (;;) { t = nxt_app_nncq_tail(q); j = nxt_app_nncq_map(q, t); e = q->entries[j]; @@ -130,14 +106,12 @@ nxt_app_nncq_enqueue(nxt_app_nncq_t volatile *q, nxt_app_nncq_atomic_t val) nxt_app_nncq_tail_cmp_inc(q, t); } - static nxt_app_nncq_atomic_t -nxt_app_nncq_dequeue(nxt_app_nncq_t volatile *q) -{ - nxt_app_nncq_cycle_t e_cycle, h_cycle; - nxt_app_nncq_atomic_t h, j, e; +nxt_app_nncq_dequeue(nxt_app_nncq_t volatile *q) { + nxt_app_nncq_cycle_t e_cycle, h_cycle; + nxt_app_nncq_atomic_t h, j, e; - for ( ;; ) { + for (;;) { h = nxt_app_nncq_head(q); j = nxt_app_nncq_map(q, h); e = q->entries[j]; diff --git a/src/nxt_app_queue.h b/src/nxt_app_queue.h index a1cc2f11d..12eff6671 100644 --- a/src/nxt_app_queue.h +++ b/src/nxt_app_queue.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,28 +11,25 @@ /* Using Numeric Naive Circular Queue as a backend. */ -#define NXT_APP_QUEUE_SIZE NXT_APP_NNCQ_SIZE -#define NXT_APP_QUEUE_MSG_SIZE 31 +#define NXT_APP_QUEUE_SIZE NXT_APP_NNCQ_SIZE +#define NXT_APP_QUEUE_MSG_SIZE 31 typedef struct { - uint8_t size; - uint8_t data[NXT_APP_QUEUE_MSG_SIZE]; - uint32_t tracking; + uint8_t size; + uint8_t data[NXT_APP_QUEUE_MSG_SIZE]; + uint32_t tracking; } nxt_app_queue_item_t; - typedef struct { - nxt_app_nncq_atomic_t notified; - nxt_app_nncq_t free_items; - nxt_app_nncq_t queue; - nxt_app_queue_item_t items[NXT_APP_QUEUE_SIZE]; + nxt_app_nncq_atomic_t notified; + nxt_app_nncq_t free_items; + nxt_app_nncq_t queue; + nxt_app_queue_item_t items[NXT_APP_QUEUE_SIZE]; } nxt_app_queue_t; - nxt_inline void -nxt_app_queue_init(nxt_app_queue_t volatile *q) -{ - nxt_app_nncq_atomic_t i; +nxt_app_queue_init(nxt_app_queue_t volatile *q) { + nxt_app_nncq_atomic_t i; nxt_app_nncq_init(&q->free_items); nxt_app_nncq_init(&q->queue); @@ -45,14 +41,12 @@ nxt_app_queue_init(nxt_app_queue_t volatile *q) q->notified = 0; } - nxt_inline nxt_int_t -nxt_app_queue_send(nxt_app_queue_t volatile *q, const void *p, - uint8_t size, uint32_t tracking, int *notify, uint32_t *cookie) -{ - int n; - nxt_app_queue_item_t *qi; - nxt_app_nncq_atomic_t i; +nxt_app_queue_send(nxt_app_queue_t volatile *q, const void *p, uint8_t size, + uint32_t tracking, int *notify, uint32_t *cookie) { + int n; + nxt_app_queue_item_t *qi; + nxt_app_nncq_atomic_t i; i = nxt_app_nncq_dequeue(&q->free_items); if (i == nxt_app_nncq_empty(&q->free_items)) { @@ -64,7 +58,7 @@ nxt_app_queue_send(nxt_app_queue_t volatile *q, const void *p, qi->size = size; nxt_memcpy(qi->data, p, size); qi->tracking = tracking; - *cookie = i; + *cookie = i; nxt_app_nncq_enqueue(&q->queue, i); @@ -77,32 +71,26 @@ nxt_app_queue_send(nxt_app_queue_t volatile *q, const void *p, return NXT_OK; } - nxt_inline void -nxt_app_queue_notification_received(nxt_app_queue_t volatile *q) -{ +nxt_app_queue_notification_received(nxt_app_queue_t volatile *q) { q->notified = 0; } - nxt_inline nxt_bool_t nxt_app_queue_cancel(nxt_app_queue_t volatile *q, uint32_t cookie, - uint32_t tracking) -{ - nxt_app_queue_item_t *qi; + uint32_t tracking) { + nxt_app_queue_item_t *qi; qi = (nxt_app_queue_item_t *) &q->items[cookie]; return nxt_atomic_cmp_set(&qi->tracking, tracking, 0); } - nxt_inline ssize_t -nxt_app_queue_recv(nxt_app_queue_t volatile *q, void *p, uint32_t *cookie) -{ - ssize_t res; - nxt_app_queue_item_t *qi; - nxt_app_nncq_atomic_t i; +nxt_app_queue_recv(nxt_app_queue_t volatile *q, void *p, uint32_t *cookie) { + ssize_t res; + nxt_app_queue_item_t *qi; + nxt_app_nncq_atomic_t i; i = nxt_app_nncq_dequeue(&q->queue); if (i == nxt_app_nncq_empty(&q->queue)) { diff --git a/src/nxt_application.c b/src/nxt_application.c index 629aa11c4..6a1489c47 100644 --- a/src/nxt_application.c +++ b/src/nxt_application.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) Igor Sysoev @@ -31,75 +30,90 @@ typedef struct { - nxt_app_type_t type; - nxt_str_t name; - nxt_str_t version; - nxt_str_t file; - nxt_array_t *mounts; + nxt_app_type_t type; + nxt_str_t name; + nxt_str_t version; + nxt_str_t file; + nxt_array_t *mounts; } nxt_module_t; - -static nxt_int_t nxt_discovery_start(nxt_task_t *task, - nxt_process_data_t *data); -static nxt_buf_t *nxt_discovery_modules(nxt_task_t *task, const char *path); -static nxt_int_t nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, - nxt_array_t *modules, const char *name); -static void nxt_discovery_completion_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_discovery_quit(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data); -static nxt_app_module_t *nxt_app_module_load(nxt_task_t *task, +static nxt_int_t +nxt_discovery_start(nxt_task_t *task, nxt_process_data_t *data); +static nxt_buf_t * +nxt_discovery_modules(nxt_task_t *task, const char *path); +static nxt_int_t +nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, const char *name); -static nxt_int_t nxt_proto_setup(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_proto_start(nxt_task_t *task, nxt_process_data_t *data); -static nxt_int_t nxt_app_setup(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_app_set_environment(nxt_conf_value_t *environment); -static void nxt_proto_start_process_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_proto_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -static void nxt_proto_process_created_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_proto_quit_children(nxt_task_t *task); -static nxt_process_t *nxt_proto_process_find(nxt_task_t *task, nxt_pid_t pid); -static void nxt_proto_process_add(nxt_task_t *task, nxt_process_t *process); -static nxt_process_t *nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid); -static u_char *nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src); -static void nxt_proto_signal_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_proto_sigterm_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data); - - -nxt_str_t nxt_server = nxt_string(NXT_SERVER); - - -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static void +nxt_discovery_completion_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_discovery_quit(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); +static nxt_app_module_t * +nxt_app_module_load(nxt_task_t *task, const char *name); +static nxt_int_t +nxt_proto_setup(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_proto_start(nxt_task_t *task, nxt_process_data_t *data); +static nxt_int_t +nxt_app_setup(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_app_set_environment(nxt_conf_value_t *environment); +static void +nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_proto_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_proto_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_proto_quit_children(nxt_task_t *task); +static nxt_process_t * +nxt_proto_process_find(nxt_task_t *task, nxt_pid_t pid); +static void +nxt_proto_process_add(nxt_task_t *task, nxt_process_t *process); +static nxt_process_t * +nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid); +static u_char * +nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src); +static void +nxt_proto_signal_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_proto_sigterm_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data); + + +nxt_str_t nxt_server = nxt_string(NXT_SERVER); + + +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -static nxt_lvlhsh_t nxt_proto_processes; -static nxt_queue_t nxt_proto_children; -static nxt_bool_t nxt_proto_exiting; +static nxt_lvlhsh_t nxt_proto_processes; +static nxt_queue_t nxt_proto_children; +static nxt_bool_t nxt_proto_exiting; -static nxt_app_module_t *nxt_app; -static nxt_common_app_conf_t *nxt_app_conf; +static nxt_app_module_t *nxt_app; +static nxt_common_app_conf_t *nxt_app_conf; -static const nxt_port_handlers_t nxt_discovery_process_port_handlers = { - .quit = nxt_signal_quit_handler, - .new_port = nxt_port_new_port_handler, - .change_file = nxt_port_change_log_file_handler, - .mmap = nxt_port_mmap_handler, - .data = nxt_port_data_handler, - .remove_pid = nxt_port_remove_pid_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, +static const nxt_port_handlers_t nxt_discovery_process_port_handlers = { + .quit = nxt_signal_quit_handler, + .new_port = nxt_port_new_port_handler, + .change_file = nxt_port_change_log_file_handler, + .mmap = nxt_port_mmap_handler, + .data = nxt_port_data_handler, + .remove_pid = nxt_port_remove_pid_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, }; -const nxt_sig_event_t nxt_prototype_signals[] = { - nxt_event_signal(SIGHUP, nxt_proto_signal_handler), - nxt_event_signal(SIGINT, nxt_proto_sigterm_handler), +const nxt_sig_event_t nxt_prototype_signals[] = { + nxt_event_signal(SIGHUP, nxt_proto_signal_handler), + nxt_event_signal(SIGINT, nxt_proto_sigterm_handler), nxt_event_signal(SIGQUIT, nxt_proto_sigterm_handler), nxt_event_signal(SIGTERM, nxt_proto_sigterm_handler), nxt_event_signal(SIGCHLD, nxt_proto_sigchld_handler), @@ -107,7 +121,7 @@ const nxt_sig_event_t nxt_prototype_signals[] = { }; -static const nxt_port_handlers_t nxt_proto_process_port_handlers = { +static const nxt_port_handlers_t nxt_proto_process_port_handlers = { .quit = nxt_proto_quit_handler, .change_file = nxt_port_change_log_file_handler, .new_port = nxt_port_new_port_handler, @@ -120,55 +134,53 @@ static const nxt_port_handlers_t nxt_proto_process_port_handlers = { }; -static const nxt_port_handlers_t nxt_app_process_port_handlers = { - .quit = nxt_signal_quit_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, +static const nxt_port_handlers_t nxt_app_process_port_handlers = { + .quit = nxt_signal_quit_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, }; -const nxt_process_init_t nxt_discovery_process = { - .name = "discovery", - .type = NXT_PROCESS_DISCOVERY, - .prefork = NULL, - .restart = 0, - .setup = nxt_process_core_setup, - .start = nxt_discovery_start, - .port_handlers = &nxt_discovery_process_port_handlers, - .signals = nxt_process_signals, +const nxt_process_init_t nxt_discovery_process = { + .name = "discovery", + .type = NXT_PROCESS_DISCOVERY, + .prefork = NULL, + .restart = 0, + .setup = nxt_process_core_setup, + .start = nxt_discovery_start, + .port_handlers = &nxt_discovery_process_port_handlers, + .signals = nxt_process_signals, }; -const nxt_process_init_t nxt_proto_process = { - .type = NXT_PROCESS_PROTOTYPE, - .prefork = nxt_isolation_main_prefork, - .restart = 0, - .setup = nxt_proto_setup, - .start = nxt_proto_start, - .port_handlers = &nxt_proto_process_port_handlers, - .signals = nxt_prototype_signals, +const nxt_process_init_t nxt_proto_process = { + .type = NXT_PROCESS_PROTOTYPE, + .prefork = nxt_isolation_main_prefork, + .restart = 0, + .setup = nxt_proto_setup, + .start = nxt_proto_start, + .port_handlers = &nxt_proto_process_port_handlers, + .signals = nxt_prototype_signals, }; -const nxt_process_init_t nxt_app_process = { - .type = NXT_PROCESS_APP, - .setup = nxt_app_setup, - .start = NULL, - .prefork = NULL, - .restart = 0, - .port_handlers = &nxt_app_process_port_handlers, - .signals = nxt_process_signals, +const nxt_process_init_t nxt_app_process = { + .type = NXT_PROCESS_APP, + .setup = nxt_app_setup, + .start = NULL, + .prefork = NULL, + .restart = 0, + .port_handlers = &nxt_app_process_port_handlers, + .signals = nxt_process_signals, }; - static nxt_int_t -nxt_discovery_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_discovery_start(nxt_task_t *task, nxt_process_data_t *data) { uint32_t stream; - nxt_buf_t *b; + nxt_buf_t *b; nxt_int_t ret; - nxt_port_t *main_port, *discovery_port; - nxt_runtime_t *rt; + nxt_port_t *main_port, *discovery_port; + nxt_runtime_t *rt; nxt_log(task, NXT_LOG_INFO, "discovery started"); @@ -179,20 +191,18 @@ nxt_discovery_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - main_port = rt->port_by_type[NXT_PROCESS_MAIN]; + main_port = rt->port_by_type[NXT_PROCESS_MAIN]; discovery_port = rt->port_by_type[NXT_PROCESS_DISCOVERY]; stream = nxt_port_rpc_register_handler(task, discovery_port, - nxt_discovery_quit, - nxt_discovery_quit, - main_port->pid, NULL); + nxt_discovery_quit, nxt_discovery_quit, main_port->pid, NULL); if (nxt_slow_path(stream == 0)) { return NXT_ERROR; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_MODULES, -1, - stream, discovery_port->id, b); + stream, discovery_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, discovery_port, stream); @@ -202,21 +212,19 @@ nxt_discovery_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_OK; } - static nxt_buf_t * -nxt_discovery_modules(nxt_task_t *task, const char *path) -{ - char *name; - u_char *p, *end; +nxt_discovery_modules(nxt_task_t *task, const char *path) { + char *name; + u_char *p, *end; size_t size; glob_t glb; - nxt_mp_t *mp; - nxt_buf_t *b; + nxt_mp_t *mp; + nxt_buf_t *b; nxt_int_t ret; nxt_uint_t i, n, j; - nxt_array_t *modules, *mounts; - nxt_module_t *module; - nxt_fs_mount_t *mnt; + nxt_array_t *modules, *mounts; + nxt_module_t *module; + nxt_fs_mount_t *mnt; b = NULL; @@ -230,8 +238,8 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) n = glb.gl_pathc; if (ret != 0) { - nxt_log(task, NXT_LOG_NOTICE, - "no modules matching: \"%s\" found", path); + nxt_log(task, NXT_LOG_NOTICE, "no modules matching: \"%s\" found", + path); n = 0; } @@ -249,13 +257,13 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) } } - size = nxt_length("[]"); + size = nxt_length("[]"); module = modules->elts; - n = modules->nelts; + n = modules->nelts; for (i = 0; i < n; i++) { - nxt_debug(task, "module: %d %V %V", - module[i].type, &module[i].version, &module[i].file); + nxt_debug(task, "module: %d %V %V", module[i].type, &module[i].version, + &module[i].file); size += nxt_length("{\"type\": ,"); size += nxt_length(" \"name\": \"\","); @@ -263,16 +271,15 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) size += nxt_length(" \"file\": \"\","); size += nxt_length(" \"mounts\": []},"); - size += NXT_INT_T_LEN - + module[i].version.length - + module[i].name.length + size += NXT_INT_T_LEN + module[i].version.length + module[i].name.length + module[i].file.length; mounts = module[i].mounts; - size += mounts->nelts * nxt_length("{\"src\": \"\", \"dst\": \"\", " - "\"type\": , \"name\": \"\", " - "\"flags\": , \"data\": \"\"},"); + size += mounts->nelts + * nxt_length("{\"src\": \"\", \"dst\": \"\", " + "\"type\": , \"name\": \"\", " + "\"flags\": , \"data\": \"\"},"); mnt = mounts->elts; @@ -290,27 +297,27 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) b->completion_handler = nxt_discovery_completion_handler; - p = b->mem.free; - end = b->mem.end; + p = b->mem.free; + end = b->mem.end; *p++ = '['; for (i = 0; i < n; i++) { mounts = module[i].mounts; - p = nxt_sprintf(p, end, "{\"type\": %d, \"name\": \"%V\", " - "\"version\": \"%V\", \"file\": \"%V\", \"mounts\": [", - module[i].type, &module[i].name, &module[i].version, - &module[i].file); + p = nxt_sprintf(p, end, + "{\"type\": %d, \"name\": \"%V\", " + "\"version\": \"%V\", \"file\": \"%V\", \"mounts\": [", + module[i].type, &module[i].name, &module[i].version, + &module[i].file); mnt = mounts->elts; for (j = 0; j < mounts->nelts; j++) { p = nxt_sprintf(p, end, - "{\"src\": \"%s\", \"dst\": \"%s\", " - "\"name\": \"%s\", \"type\": %d, \"flags\": %d, " - "\"data\": \"%s\"},", - mnt[j].src, mnt[j].dst, mnt[j].name, mnt[j].type, - mnt[j].flags, - mnt[j].data == NULL ? (u_char *) "" : mnt[j].data); + "{\"src\": \"%s\", \"dst\": \"%s\", " + "\"name\": \"%s\", \"type\": %d, \"flags\": %d, " + "\"data\": \"%s\"},", + mnt[j].src, mnt[j].dst, mnt[j].name, mnt[j].type, mnt[j].flags, + mnt[j].data == NULL ? (u_char *) "" : mnt[j].data); } *p++ = ']'; @@ -334,21 +341,19 @@ nxt_discovery_modules(nxt_task_t *task, const char *path) return b; } - static nxt_int_t nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, - const char *name) -{ - void *dl; + const char *name) { + void *dl; nxt_str_t version; nxt_int_t ret; nxt_uint_t i, j, n; - nxt_array_t *mounts; - nxt_module_t *module; + nxt_array_t *mounts; + nxt_module_t *module; nxt_app_type_t type; - nxt_fs_mount_t *to; - nxt_app_module_t *app; - const nxt_fs_mount_t *from; + nxt_fs_mount_t *to; + nxt_app_module_t *app; + const nxt_fs_mount_t *from; /* * Only memory allocation failure should return NXT_ERROR. @@ -366,12 +371,11 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, app = dlsym(dl, "nxt_app_module"); if (app != NULL) { - nxt_log(task, NXT_LOG_NOTICE, "module: %V %s \"%s\"", - &app->type, app->version, name); + nxt_log(task, NXT_LOG_NOTICE, "module: %V %s \"%s\"", &app->type, + app->version, name); if (app->compat_length != sizeof(compat) - || memcmp(app->compat, compat, sizeof(compat)) != 0) - { + || memcmp(app->compat, compat, sizeof(compat)) != 0) { nxt_log(task, NXT_LOG_NOTICE, "incompatible module %s", name); goto done; @@ -386,19 +390,18 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, } module = modules->elts; - n = modules->nelts; + n = modules->nelts; - version.start = (u_char *) app->version; + version.start = (u_char *) app->version; version.length = nxt_strlen(app->version); for (i = 0; i < n; i++) { if (type == module[i].type - && nxt_strstr_eq(&module[i].version, &version)) - { + && nxt_strstr_eq(&module[i].version, &version)) { nxt_log(task, NXT_LOG_NOTICE, - "ignoring %s module with the same " - "application language version %V %V as in %V", - name, &app->type, &version, &module[i].file); + "ignoring %s module with the same " + "application language version %V %V as in %V", + name, &app->type, &version, &module[i].file); goto done; } @@ -430,8 +433,8 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, nxt_memcpy(module->file.start, name, module->file.length); - module->mounts = nxt_array_create(mp, app->nmounts, - sizeof(nxt_fs_mount_t)); + module->mounts + = nxt_array_create(mp, app->nmounts, sizeof(nxt_fs_mount_t)); if (nxt_slow_path(module->mounts == NULL)) { goto fail; @@ -441,7 +444,7 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, for (j = 0; j < app->nmounts; j++) { from = &app->mounts[j]; - to = nxt_array_zero_add(mounts); + to = nxt_array_zero_add(mounts); if (nxt_slow_path(to == NULL)) { goto fail; } @@ -490,33 +493,27 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules, return ret; } - static void -nxt_discovery_completion_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b; +nxt_discovery_completion_handler(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b; - b = obj; + b = obj; mp = b->data; nxt_mp_destroy(mp); } - static void -nxt_discovery_quit(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) -{ +nxt_discovery_quit(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) { nxt_signal_quit_handler(task, msg); } - static nxt_int_t -nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) -{ +nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret; - nxt_app_lang_module_t *lang; - nxt_common_app_conf_t *app_conf; + nxt_app_lang_module_t *lang; + nxt_common_app_conf_t *app_conf; app_conf = process->data.app; @@ -533,8 +530,8 @@ nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) nxt_app = lang->module; if (nxt_app == NULL) { - nxt_debug(task, "application language module: %s \"%s\"", - lang->version, lang->file); + nxt_debug(task, "application language module: %s \"%s\"", lang->version, + lang->file); nxt_app = nxt_app_module_load(task, lang->file); if (nxt_slow_path(nxt_app == NULL)) { @@ -542,9 +539,8 @@ nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) } } - if (nxt_slow_path(nxt_app_set_environment(app_conf->environment) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_app_set_environment(app_conf->environment) != NXT_OK)) { nxt_alert(task, "failed to set environment"); return NXT_ERROR; } @@ -573,13 +569,12 @@ nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) #endif if (app_conf->working_directory != NULL - && app_conf->working_directory[0] != 0) - { + && app_conf->working_directory[0] != 0) { ret = chdir(app_conf->working_directory); if (nxt_slow_path(ret != 0)) { nxt_log(task, NXT_LOG_WARN, "chdir(%s) failed %E", - app_conf->working_directory, nxt_errno); + app_conf->working_directory, nxt_errno); return NXT_ERROR; } @@ -590,25 +585,21 @@ nxt_proto_setup(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - static nxt_int_t -nxt_proto_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_proto_start(nxt_task_t *task, nxt_process_data_t *data) { nxt_debug(task, "prototype waiting for clone messages"); return NXT_OK; } - static void -nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; +nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; nxt_int_t ret; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_process_t *process; - nxt_process_init_t *init; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_process_t *process; + nxt_process_init_t *init; rt = task->thread->runtime; @@ -625,11 +616,11 @@ nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) process->parent_port = rt->port_by_type[NXT_PROCESS_PROTOTYPE]; - init = nxt_process_init(process); + init = nxt_process_init(process); *init = nxt_app_process; - process->name = nxt_mp_alloc(process->mem_pool, nxt_app_conf->name.length - + sizeof("\"\" application") + 1); + process->name = nxt_mp_alloc(process->mem_pool, + nxt_app_conf->name.length + sizeof("\"\" application") + 1); if (nxt_slow_path(process->name == NULL)) { nxt_process_use(task, process, -1); @@ -641,16 +632,16 @@ nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) init->name = (const char *) nxt_app_conf->name.start; - p = (u_char *) process->name; + p = (u_char *) process->name; *p++ = '"'; - p = nxt_cpymem(p, nxt_app_conf->name.start, nxt_app_conf->name.length); - p = nxt_cpymem(p, "\" application", 13); - *p = '\0'; + p = nxt_cpymem(p, nxt_app_conf->name.start, nxt_app_conf->name.length); + p = nxt_cpymem(p, "\" application", 13); + *p = '\0'; process->user_cred = &rt->user_cred; process->data.app = nxt_app_conf; - process->stream = msg->port_msg.stream; + process->stream = msg->port_msg.stream; init->siblings = &nxt_proto_children; @@ -668,18 +659,16 @@ nxt_proto_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) failed: port = nxt_runtime_port_find(rt, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_fast_path(port != NULL)) { - nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, - -1, msg->port_msg.stream, 0, NULL); + nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, + msg->port_msg.stream, 0, NULL); } } - static void -nxt_proto_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_proto_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_debug(task, "prototype quit handler"); nxt_proto_quit_children(task); @@ -691,28 +680,24 @@ nxt_proto_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static void -nxt_proto_quit_children(nxt_task_t *task) -{ - nxt_port_t *port; - nxt_process_t *process; +nxt_proto_quit_children(nxt_task_t *task) { + nxt_port_t *port; + nxt_process_t *process; nxt_queue_each(process, &nxt_proto_children, nxt_process_t, link) { port = nxt_process_port_first(process); - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, - -1, 0, 0, NULL); + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, -1, 0, 0, + NULL); } nxt_queue_loop; } - static void -nxt_proto_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_proto_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_pid_t isolated_pid, pid; - nxt_process_t *process; + nxt_process_t *process; isolated_pid = nxt_recv_msg_cmsg_pid(msg); @@ -727,7 +712,7 @@ nxt_proto_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (process->pid != pid) { nxt_debug(task, "app process %PI (aka %PI) is created", isolated_pid, - pid); + pid); process->pid = pid; @@ -744,20 +729,16 @@ nxt_proto_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static void -nxt_proto_signal_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_proto_signal_handler(nxt_task_t *task, void *obj, void *data) { nxt_trace(task, "signal signo:%d (%s) received, ignored", - (int) (uintptr_t) obj, data); + (int) (uintptr_t) obj, data); } - static void -nxt_proto_sigterm_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_trace(task, "signal signo:%d (%s) received", - (int) (uintptr_t) obj, data); +nxt_proto_sigterm_handler(nxt_task_t *task, void *obj, void *data) { + nxt_trace(task, "signal signo:%d (%s) received", (int) (uintptr_t) obj, + data); nxt_proto_quit_children(task); @@ -768,29 +749,25 @@ nxt_proto_sigterm_handler(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data) { int status; nxt_err_t err; nxt_pid_t pid; - nxt_port_t *port; - nxt_process_t *process; - nxt_runtime_t *rt; + nxt_port_t *port; + nxt_process_t *process; + nxt_runtime_t *rt; rt = task->thread->runtime; nxt_debug(task, "proto sigchld handler signo:%d (%s)", - (int) (uintptr_t) obj, data); + (int) (uintptr_t) obj, data); - for ( ;; ) { + for (;;) { pid = waitpid(-1, &status, WNOHANG); if (pid == -1) { - switch (err = nxt_errno) { - case NXT_ECHILD: return; @@ -813,28 +790,29 @@ nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data) if (WTERMSIG(status)) { if (rt->is_pid_isolated) { - nxt_alert(task, "app process %PI (isolated %PI) " - "exited on signal %d%s", - process != NULL ? process->pid : 0, - pid, WTERMSIG(status), - NXT_WCOREDUMP(status) ? " (core dumped)" : ""); + nxt_alert(task, + "app process %PI (isolated %PI) " + "exited on signal %d%s", + process != NULL ? process->pid : 0, pid, WTERMSIG(status), + NXT_WCOREDUMP(status) ? " (core dumped)" : ""); } else { - nxt_alert(task, "app process %PI exited on signal %d%s", - pid, WTERMSIG(status), - NXT_WCOREDUMP(status) ? " (core dumped)" : ""); + nxt_alert(task, "app process %PI exited on signal %d%s", pid, + WTERMSIG(status), + NXT_WCOREDUMP(status) ? " (core dumped)" : ""); } } else { if (rt->is_pid_isolated) { - nxt_trace(task, "app process %PI (isolated %PI) " - "exited with code %d", - process != NULL ? process->pid : 0, - pid, WEXITSTATUS(status)); + nxt_trace(task, + "app process %PI (isolated %PI) " + "exited with code %d", + process != NULL ? process->pid : 0, pid, + WEXITSTATUS(status)); } else { - nxt_trace(task, "app process %PI exited with code %d", - pid, WEXITSTATUS(status)); + nxt_trace(task, "app process %PI exited with code %d", pid, + WEXITSTATUS(status)); } } @@ -868,20 +846,18 @@ nxt_proto_sigchld_handler(nxt_task_t *task, void *obj, void *data) } } - static nxt_app_module_t * -nxt_app_module_load(nxt_task_t *task, const char *name) -{ - char *err; - void *dl; - nxt_app_module_t *app; +nxt_app_module_load(nxt_task_t *task, const char *name) { + char *err; + void *dl; + nxt_app_module_t *app; dl = dlopen(name, RTLD_GLOBAL | RTLD_LAZY); if (nxt_slow_path(dl == NULL)) { err = dlerror(); - nxt_alert(task, "dlopen(\"%s\") failed: \"%s\"", - name, err != NULL ? err : "(null)"); + nxt_alert(task, "dlopen(\"%s\") failed: \"%s\"", name, + err != NULL ? err : "(null)"); return NULL; } @@ -890,31 +866,29 @@ nxt_app_module_load(nxt_task_t *task, const char *name) if (nxt_slow_path(app == NULL)) { err = dlerror(); nxt_alert(task, "dlsym(\"%s\", \"nxt_app_module\") failed: \"%s\"", - name, err != NULL ? err : "(null)"); + name, err != NULL ? err : "(null)"); if (dlclose(dl) != 0) { err = dlerror(); - nxt_alert(task, "dlclose(\"%s\") failed: \"%s\"", - name, err != NULL ? err : "(null)"); + nxt_alert(task, "dlclose(\"%s\") failed: \"%s\"", name, + err != NULL ? err : "(null)"); } } return app; } - static nxt_int_t -nxt_app_set_environment(nxt_conf_value_t *environment) -{ - char *env, *p; +nxt_app_set_environment(nxt_conf_value_t *environment) { + char *env, *p; uint32_t next; nxt_str_t name, value; - nxt_conf_value_t *value_obj; + nxt_conf_value_t *value_obj; if (environment != NULL) { next = 0; - for ( ;; ) { + for (;;) { value_obj = nxt_conf_next_object_member(environment, &name, &next); if (value_obj == NULL) { break; @@ -927,10 +901,10 @@ nxt_app_set_environment(nxt_conf_value_t *environment) return NXT_ERROR; } - p = nxt_cpymem(env, name.start, name.length); + p = nxt_cpymem(env, name.start, name.length); *p++ = '='; - p = nxt_cpymem(p, value.start, value.length); - *p = '\0'; + p = nxt_cpymem(p, value.start, value.length); + *p = '\0'; if (nxt_slow_path(putenv(env) != 0)) { return NXT_ERROR; @@ -941,17 +915,15 @@ nxt_app_set_environment(nxt_conf_value_t *environment) return NXT_OK; } - nxt_int_t -nxt_app_set_logs(void) -{ +nxt_app_set_logs(void) { nxt_int_t ret; nxt_file_t file; - nxt_task_t *task; - nxt_thread_t *thr; - nxt_process_t *process; - nxt_runtime_t *rt; - nxt_common_app_conf_t *app_conf; + nxt_task_t *task; + nxt_thread_t *thr; + nxt_process_t *process; + nxt_runtime_t *rt; + nxt_common_app_conf_t *app_conf; thr = nxt_thread(); @@ -962,13 +934,13 @@ nxt_app_set_logs(void) return NXT_OK; } - process = rt->port_by_type[NXT_PROCESS_PROTOTYPE]->process; + process = rt->port_by_type[NXT_PROCESS_PROTOTYPE]->process; app_conf = process->data.app; if (app_conf->stdout_log != NULL) { nxt_memzero(&file, sizeof(nxt_file_t)); file.log_level = 1; - file.name = (u_char *) app_conf->stdout_log; + file.name = (u_char *) app_conf->stdout_log; ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT, 0666); if (ret == NXT_ERROR) { return NXT_ERROR; @@ -981,7 +953,7 @@ nxt_app_set_logs(void) if (app_conf->stderr_log != NULL) { nxt_memzero(&file, sizeof(nxt_file_t)); file.log_level = 1; - file.name = (u_char *) app_conf->stderr_log; + file.name = (u_char *) app_conf->stderr_log; ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT, 0666); if (ret == NXT_ERROR) { return NXT_ERROR; @@ -994,11 +966,9 @@ nxt_app_set_logs(void) return NXT_OK; } - static u_char * -nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src) -{ - u_char *p; +nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src) { + u_char *p; size_t len; len = nxt_strlen(src); @@ -1010,17 +980,15 @@ nxt_cstr_dup(nxt_mp_t *mp, u_char *dst, u_char *src) } } - p = nxt_cpymem(dst, src, len); + p = nxt_cpymem(dst, src, len); *p = '\0'; return dst; } - static nxt_int_t -nxt_app_setup(nxt_task_t *task, nxt_process_t *process) -{ - nxt_process_init_t *init; +nxt_app_setup(nxt_task_t *task, nxt_process_t *process) { + nxt_process_init_t *init; process->state = NXT_PROCESS_STATE_CREATED; @@ -1029,17 +997,15 @@ nxt_app_setup(nxt_task_t *task, nxt_process_t *process) return init->start(task, &process->data); } - nxt_app_lang_module_t * -nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) -{ - u_char *p, *end, *version; +nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) { + u_char *p, *end, *version; size_t version_length; nxt_uint_t i, n; nxt_app_type_t type; - nxt_app_lang_module_t *lang; + nxt_app_lang_module_t *lang; - end = name->start + name->length; + end = name->start + name->length; version = end; for (p = name->start; p < end; p++) { @@ -1063,18 +1029,16 @@ nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) version_length = end - version; lang = rt->languages->elts; - n = rt->languages->nelts; + n = rt->languages->nelts; for (i = 0; i < n; i++) { - /* * Versions are sorted in descending order * so first match chooses the highest version. */ if (lang[i].type == type - && nxt_strvers_match(lang[i].version, version, version_length)) - { + && nxt_strvers_match(lang[i].version, version, version_length)) { return &lang[i]; } } @@ -1082,14 +1046,12 @@ nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name) return NULL; } - nxt_app_type_t -nxt_app_parse_type(u_char *p, size_t length) -{ +nxt_app_parse_type(u_char *p, size_t length) { nxt_str_t str; str.length = length; - str.start = p; + str.start = p; if (nxt_str_eq(&str, "external", 8) || nxt_str_eq(&str, "go", 2)) { return NXT_APP_EXTERNAL; @@ -1119,13 +1081,11 @@ nxt_app_parse_type(u_char *p, size_t length) return NXT_APP_UNKNOWN; } - nxt_int_t nxt_unit_default_init(nxt_task_t *task, nxt_unit_init_t *init, - nxt_common_app_conf_t *conf) -{ - nxt_port_t *my_port, *proto_port, *router_port; - nxt_runtime_t *rt; + nxt_common_app_conf_t *conf) { + nxt_port_t *my_port, *proto_port, *router_port; + nxt_runtime_t *rt; nxt_memzero(init, sizeof(nxt_unit_init_t)); @@ -1147,42 +1107,40 @@ nxt_unit_default_init(nxt_task_t *task, nxt_unit_init_t *init, } init->ready_port.id.pid = proto_port->pid; - init->ready_port.id.id = proto_port->id; - init->ready_port.in_fd = -1; + init->ready_port.id.id = proto_port->id; + init->ready_port.in_fd = -1; init->ready_port.out_fd = proto_port->pair[1]; init->ready_stream = my_port->process->stream; init->router_port.id.pid = router_port->pid; - init->router_port.id.id = router_port->id; - init->router_port.in_fd = -1; + init->router_port.id.id = router_port->id; + init->router_port.in_fd = -1; init->router_port.out_fd = router_port->pair[1]; init->read_port.id.pid = my_port->pid; - init->read_port.id.id = my_port->id; - init->read_port.in_fd = my_port->pair[0]; + init->read_port.id.id = my_port->id; + init->read_port.in_fd = my_port->pair[0]; init->read_port.out_fd = my_port->pair[1]; - init->shared_port_fd = conf->shared_port_fd; + init->shared_port_fd = conf->shared_port_fd; init->shared_queue_fd = conf->shared_queue_fd; init->log_fd = 2; - init->shm_limit = conf->shm_limit; + init->shm_limit = conf->shm_limit; init->request_limit = conf->request_limit; return NXT_OK; } - static nxt_int_t -nxt_proto_lvlhsh_isolated_pid_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_pid_t *qpid; - nxt_process_t *process; +nxt_proto_lvlhsh_isolated_pid_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_pid_t *qpid; + nxt_process_t *process; process = data; - qpid = (nxt_pid_t *) lhq->key.start; + qpid = (nxt_pid_t *) lhq->key.start; if (*qpid == process->isolated_pid) { return NXT_OK; @@ -1191,41 +1149,35 @@ nxt_proto_lvlhsh_isolated_pid_test(nxt_lvlhsh_query_t *lhq, void *data) return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_proto_lvlhsh_isolated_pid_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - nxt_inline void -nxt_proto_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) -{ - lhq->key_hash = nxt_murmur_hash2(pid, sizeof(nxt_pid_t)); +nxt_proto_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) { + lhq->key_hash = nxt_murmur_hash2(pid, sizeof(nxt_pid_t)); lhq->key.length = sizeof(nxt_pid_t); - lhq->key.start = (u_char *) pid; - lhq->proto = &lvlhsh_processes_proto; + lhq->key.start = (u_char *) pid; + lhq->proto = &lvlhsh_processes_proto; } - static void -nxt_proto_process_add(nxt_task_t *task, nxt_process_t *process) -{ - nxt_runtime_t *rt; - nxt_lvlhsh_query_t lhq; +nxt_proto_process_add(nxt_task_t *task, nxt_process_t *process) { + nxt_runtime_t *rt; + nxt_lvlhsh_query_t lhq; rt = task->thread->runtime; nxt_proto_process_lhq_pid(&lhq, &process->isolated_pid); lhq.replace = 0; - lhq.value = process; - lhq.pool = rt->mem_pool; + lhq.value = process; + lhq.pool = rt->mem_pool; switch (nxt_lvlhsh_insert(&nxt_proto_processes, &lhq)) { - case NXT_OK: nxt_debug(task, "process (isolated %PI) added", process->isolated_pid); @@ -1234,18 +1186,16 @@ nxt_proto_process_add(nxt_task_t *task, nxt_process_t *process) default: nxt_alert(task, "process (isolated %PI) failed to add", - process->isolated_pid); + process->isolated_pid); break; } } - static nxt_process_t * -nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid) -{ - nxt_runtime_t *rt; - nxt_process_t *process; - nxt_lvlhsh_query_t lhq; +nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid) { + nxt_runtime_t *rt; + nxt_process_t *process; + nxt_lvlhsh_query_t lhq; nxt_proto_process_lhq_pid(&lhq, &pid); @@ -1254,7 +1204,6 @@ nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid) lhq.pool = rt->mem_pool; switch (nxt_lvlhsh_delete(&nxt_proto_processes, &lhq)) { - case NXT_OK: nxt_debug(task, "process (isolated %PI) removed", pid); @@ -1274,12 +1223,10 @@ nxt_proto_process_remove(nxt_task_t *task, nxt_pid_t pid) return process; } - static nxt_process_t * -nxt_proto_process_find(nxt_task_t *task, nxt_pid_t pid) -{ - nxt_process_t *process; - nxt_lvlhsh_query_t lhq; +nxt_proto_process_find(nxt_task_t *task, nxt_pid_t pid) { + nxt_process_t *process; + nxt_lvlhsh_query_t lhq; nxt_proto_process_lhq_pid(&lhq, &pid); diff --git a/src/nxt_application.h b/src/nxt_application.h index a3b4230a0..12b48442b 100644 --- a/src/nxt_application.h +++ b/src/nxt_application.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) Valentin V. Bartenev @@ -13,7 +12,6 @@ #include - typedef enum { NXT_APP_EXTERNAL, NXT_APP_PYTHON, @@ -28,151 +26,143 @@ typedef enum { } nxt_app_type_t; -typedef struct nxt_app_module_s nxt_app_module_t; -typedef nxt_int_t (*nxt_application_setup_t)(nxt_task_t *task, +typedef struct nxt_app_module_s nxt_app_module_t; +typedef nxt_int_t (*nxt_application_setup_t)(nxt_task_t *task, nxt_process_t *process, nxt_common_app_conf_t *conf); - typedef struct { - nxt_app_type_t type; - char *name; - u_char *version; - char *file; - nxt_app_module_t *module; - nxt_array_t *mounts; /* of nxt_fs_mount_t */ + nxt_app_type_t type; + char *name; + u_char *version; + char *file; + nxt_app_module_t *module; + nxt_array_t *mounts; /* of nxt_fs_mount_t */ } nxt_app_lang_module_t; - typedef struct { - char *executable; - nxt_conf_value_t *arguments; + char *executable; + nxt_conf_value_t *arguments; } nxt_external_app_conf_t; - typedef struct { - char *home; - nxt_conf_value_t *path; - nxt_str_t protocol; - uint32_t threads; - uint32_t thread_stack_size; - nxt_conf_value_t *targets; + char *home; + nxt_conf_value_t *path; + nxt_str_t protocol; + uint32_t threads; + uint32_t thread_stack_size; + nxt_conf_value_t *targets; } nxt_python_app_conf_t; - typedef struct { - nxt_conf_value_t *targets; - nxt_conf_value_t *options; + nxt_conf_value_t *targets; + nxt_conf_value_t *options; } nxt_php_app_conf_t; - typedef struct { - char *script; - uint32_t threads; - uint32_t thread_stack_size; + char *script; + uint32_t threads; + uint32_t thread_stack_size; } nxt_perl_app_conf_t; - typedef struct { - nxt_str_t script; - uint32_t threads; - nxt_str_t hooks; + nxt_str_t script; + uint32_t threads; + nxt_str_t hooks; } nxt_ruby_app_conf_t; - typedef struct { - nxt_conf_value_t *classpath; - char *webapp; - nxt_conf_value_t *options; - char *unit_jars; - uint32_t threads; - uint32_t thread_stack_size; + nxt_conf_value_t *classpath; + char *webapp; + nxt_conf_value_t *options; + char *unit_jars; + uint32_t threads; + uint32_t thread_stack_size; } nxt_java_app_conf_t; - typedef struct { - const char *module; + const char *module; - const char *request_handler; - const char *malloc_handler; - const char *free_handler; + const char *request_handler; + const char *malloc_handler; + const char *free_handler; - const char *module_init_handler; - const char *module_end_handler; - const char *request_init_handler; - const char *request_end_handler; - const char *response_end_handler; + const char *module_init_handler; + const char *module_end_handler; + const char *request_init_handler; + const char *request_end_handler; + const char *response_end_handler; - nxt_conf_value_t *access; + nxt_conf_value_t *access; } nxt_wasm_app_conf_t; - typedef struct { - const char *component; + const char *component; - nxt_conf_value_t *access; + nxt_conf_value_t *access; } nxt_wasm_wc_app_conf_t; - struct nxt_common_app_conf_s { - nxt_str_t name; - nxt_str_t type; - nxt_str_t user; - nxt_str_t group; + nxt_str_t name; + nxt_str_t type; + nxt_str_t user; + nxt_str_t group; - char *stdout_log; - char *stderr_log; + char *stdout_log; + char *stderr_log; - char *working_directory; - nxt_conf_value_t *environment; + char *working_directory; + nxt_conf_value_t *environment; - nxt_conf_value_t *isolation; - nxt_conf_value_t *limits; + nxt_conf_value_t *isolation; + nxt_conf_value_t *limits; - size_t shm_limit; - uint32_t request_limit; + size_t shm_limit; + uint32_t request_limit; - nxt_fd_t shared_port_fd; - nxt_fd_t shared_queue_fd; + nxt_fd_t shared_port_fd; + nxt_fd_t shared_queue_fd; union { - nxt_external_app_conf_t external; - nxt_python_app_conf_t python; - nxt_php_app_conf_t php; - nxt_perl_app_conf_t perl; - nxt_ruby_app_conf_t ruby; - nxt_java_app_conf_t java; - nxt_wasm_app_conf_t wasm; - nxt_wasm_wc_app_conf_t wasm_wc; + nxt_external_app_conf_t external; + nxt_python_app_conf_t python; + nxt_php_app_conf_t php; + nxt_perl_app_conf_t perl; + nxt_ruby_app_conf_t ruby; + nxt_java_app_conf_t java; + nxt_wasm_app_conf_t wasm; + nxt_wasm_wc_app_conf_t wasm_wc; } u; - nxt_conf_value_t *self; + nxt_conf_value_t *self; }; - struct nxt_app_module_s { - size_t compat_length; - uint32_t *compat; + size_t compat_length; + uint32_t *compat; - nxt_str_t type; - const char *version; + nxt_str_t type; + const char *version; - const nxt_fs_mount_t *mounts; - nxt_uint_t nmounts; + const nxt_fs_mount_t *mounts; + nxt_uint_t nmounts; - nxt_application_setup_t setup; - nxt_process_start_t start; + nxt_application_setup_t setup; + nxt_process_start_t start; }; +nxt_app_lang_module_t * +nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name); +nxt_app_type_t +nxt_app_parse_type(u_char *p, size_t length); -nxt_app_lang_module_t *nxt_app_lang_module(nxt_runtime_t *rt, nxt_str_t *name); -nxt_app_type_t nxt_app_parse_type(u_char *p, size_t length); - -NXT_EXPORT extern nxt_str_t nxt_server; -extern nxt_app_module_t nxt_external_module; +NXT_EXPORT extern nxt_str_t nxt_server; +extern nxt_app_module_t nxt_external_module; -NXT_EXPORT nxt_int_t nxt_unit_default_init(nxt_task_t *task, - nxt_unit_init_t *init, nxt_common_app_conf_t *conf); +NXT_EXPORT nxt_int_t +nxt_unit_default_init(nxt_task_t *task, nxt_unit_init_t *init, + nxt_common_app_conf_t *conf); -NXT_EXPORT nxt_int_t nxt_app_set_logs(void); +NXT_EXPORT nxt_int_t +nxt_app_set_logs(void); #endif /* _NXT_APPLICATION_H_INCLIDED_ */ diff --git a/src/nxt_array.c b/src/nxt_array.c index 1e13c22a8..7af85c765 100644 --- a/src/nxt_array.c +++ b/src/nxt_array.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,11 +5,9 @@ #include - nxt_array_t * -nxt_array_create(nxt_mp_t *mp, nxt_uint_t n, size_t size) -{ - nxt_array_t *array; +nxt_array_create(nxt_mp_t *mp, nxt_uint_t n, size_t size) { + nxt_array_t *array; array = nxt_mp_alloc(mp, sizeof(nxt_array_t) + n * size); @@ -18,19 +15,17 @@ nxt_array_create(nxt_mp_t *mp, nxt_uint_t n, size_t size) return NULL; } - array->elts = nxt_pointer_to(array, sizeof(nxt_array_t)); - array->nelts = 0; - array->size = size; - array->nalloc = n; + array->elts = nxt_pointer_to(array, sizeof(nxt_array_t)); + array->nelts = 0; + array->size = size; + array->nalloc = n; array->mem_pool = mp; return array; } - void -nxt_array_destroy(nxt_array_t *array) -{ +nxt_array_destroy(nxt_array_t *array) { if (array->elts != nxt_pointer_to(array, sizeof(nxt_array_t))) { nxt_mp_free(array->mem_pool, array->elts); } @@ -38,17 +33,14 @@ nxt_array_destroy(nxt_array_t *array) nxt_mp_free(array->mem_pool, array); } - void * -nxt_array_add(nxt_array_t *array) -{ - void *p; - uint32_t nalloc, new_alloc; +nxt_array_add(nxt_array_t *array) { + void *p; + uint32_t nalloc, new_alloc; nalloc = array->nalloc; if (array->nelts == nalloc) { - if (nalloc < 16) { /* Allocate new array twice larger than current. */ new_alloc = (nalloc == 0) ? 4 : nalloc * 2; @@ -70,7 +62,7 @@ nxt_array_add(nxt_array_t *array) nxt_mp_free(array->mem_pool, array->elts); } - array->elts = p; + array->elts = p; array->nalloc = new_alloc; } @@ -80,11 +72,9 @@ nxt_array_add(nxt_array_t *array) return p; } - void * -nxt_array_zero_add(nxt_array_t *array) -{ - void *p; +nxt_array_zero_add(nxt_array_t *array) { + void *p; p = nxt_array_add(array); @@ -95,11 +85,9 @@ nxt_array_zero_add(nxt_array_t *array) return p; } - void -nxt_array_remove(nxt_array_t *array, void *elt) -{ - void *last; +nxt_array_remove(nxt_array_t *array, void *elt) { + void *last; last = nxt_array_last(array); @@ -110,12 +98,10 @@ nxt_array_remove(nxt_array_t *array, void *elt) array->nelts--; } - nxt_array_t * -nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst, nxt_array_t *src) -{ - void *data; - uint32_t i, size; +nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst, nxt_array_t *src) { + void *data; + uint32_t i, size; size = src->size; diff --git a/src/nxt_array.h b/src/nxt_array.h index f06ff14c8..21b71607d 100644 --- a/src/nxt_array.h +++ b/src/nxt_array.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,49 +6,46 @@ #ifndef _NXT_ARRAY_H_INCLUDED_ #define _NXT_ARRAY_H_INCLUDED_ - typedef struct { - void *elts; + void *elts; /* nelts has uint32_t type because it is used most often. */ uint32_t nelts; uint16_t size; uint16_t nalloc; - nxt_mp_t *mem_pool; + nxt_mp_t *mem_pool; } nxt_array_t; - nxt_inline void -nxt_array_init(nxt_array_t *array, nxt_mp_t *mp, size_t size) -{ - array->elts = nxt_pointer_to(array, sizeof(nxt_array_t)); - array->size = size; +nxt_array_init(nxt_array_t *array, nxt_mp_t *mp, size_t size) { + array->elts = nxt_pointer_to(array, sizeof(nxt_array_t)); + array->size = size; array->mem_pool = mp; } -NXT_EXPORT nxt_array_t *nxt_array_create(nxt_mp_t *mp, nxt_uint_t n, - size_t size); -NXT_EXPORT void nxt_array_destroy(nxt_array_t *array); -NXT_EXPORT void *nxt_array_add(nxt_array_t *array); -NXT_EXPORT void *nxt_array_zero_add(nxt_array_t *array); -NXT_EXPORT void nxt_array_remove(nxt_array_t *array, void *elt); -NXT_EXPORT nxt_array_t *nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst, - nxt_array_t *src); - -#define nxt_array_last(array) \ - nxt_pointer_to((array)->elts, (array)->size * ((array)->nelts - 1)) +NXT_EXPORT nxt_array_t * +nxt_array_create(nxt_mp_t *mp, nxt_uint_t n, size_t size); +NXT_EXPORT void +nxt_array_destroy(nxt_array_t *array); +NXT_EXPORT void * +nxt_array_add(nxt_array_t *array); +NXT_EXPORT void * +nxt_array_zero_add(nxt_array_t *array); +NXT_EXPORT void +nxt_array_remove(nxt_array_t *array, void *elt); +NXT_EXPORT nxt_array_t * +nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst, nxt_array_t *src); +#define nxt_array_last(array) \ + nxt_pointer_to((array)->elts, (array)->size *((array)->nelts - 1)) -#define nxt_array_reset(array) \ - (array)->nelts = 0; +#define nxt_array_reset(array) (array)->nelts = 0; -#define nxt_array_is_empty(array) \ - ((array)->nelts == 0) +#define nxt_array_is_empty(array) ((array)->nelts == 0) nxt_inline void * -nxt_array_remove_last(nxt_array_t *array) -{ +nxt_array_remove_last(nxt_array_t *array) { array->nelts--; return nxt_pointer_to(array->elts, array->size * array->nelts); } diff --git a/src/nxt_atomic.h b/src/nxt_atomic.h index 376375c58..31adde4dd 100644 --- a/src/nxt_atomic.h +++ b/src/nxt_atomic.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -16,9 +15,9 @@ #if (NXT_HAVE_GCC_ATOMIC) /* GCC 4.1 builtin atomic operations */ -typedef intptr_t nxt_atomic_int_t; -typedef uintptr_t nxt_atomic_uint_t; -typedef volatile nxt_atomic_uint_t nxt_atomic_t; +typedef intptr_t nxt_atomic_int_t; +typedef uintptr_t nxt_atomic_uint_t; +typedef volatile nxt_atomic_uint_t nxt_atomic_t; /* * __sync_bool_compare_and_swap() is a full barrier. @@ -26,41 +25,33 @@ typedef volatile nxt_atomic_uint_t nxt_atomic_t; * __sync_lock_release() is a release barrier. */ -#define nxt_atomic_cmp_set(lock, cmp, set) \ +#define nxt_atomic_cmp_set(lock, cmp, set) \ __sync_bool_compare_and_swap(lock, cmp, set) -#define nxt_atomic_xchg(lock, set) \ - __sync_lock_test_and_set(lock, set) +#define nxt_atomic_xchg(lock, set) __sync_lock_test_and_set(lock, set) -#define nxt_atomic_fetch_add(value, add) \ - __sync_fetch_and_add(value, add) +#define nxt_atomic_fetch_add(value, add) __sync_fetch_and_add(value, add) -#define nxt_atomic_try_lock(lock) \ - nxt_atomic_cmp_set(lock, 0, 1) +#define nxt_atomic_try_lock(lock) nxt_atomic_cmp_set(lock, 0, 1) -#define nxt_atomic_release(lock) \ - __sync_lock_release(lock) +#define nxt_atomic_release(lock) __sync_lock_release(lock) -#define nxt_atomic_or_fetch(ptr, val) \ - __sync_or_and_fetch(ptr, val) +#define nxt_atomic_or_fetch(ptr, val) __sync_or_and_fetch(ptr, val) -#define nxt_atomic_and_fetch(ptr, val) \ - __sync_and_and_fetch(ptr, val) +#define nxt_atomic_and_fetch(ptr, val) __sync_and_and_fetch(ptr, val) #if (__i386__ || __i386 || __amd64__ || __amd64) -#define nxt_cpu_pause() \ - __asm__ ("pause") +#define nxt_cpu_pause() __asm__("pause") #elif (__aarch64__ || __arm64__) -#define nxt_cpu_pause() \ - __asm__ ("isb") +#define nxt_cpu_pause() __asm__("isb") #else #define nxt_cpu_pause() diff --git a/src/nxt_buf.c b/src/nxt_buf.c index cbde069ed..2d6c330f9 100644 --- a/src/nxt_buf.c +++ b/src/nxt_buf.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,30 +6,27 @@ #include -static void nxt_buf_completion(nxt_task_t *task, void *obj, void *data); -static void nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data); - +static void +nxt_buf_completion(nxt_task_t *task, void *obj, void *data); +static void +nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data); typedef struct { nxt_work_t work; - nxt_event_engine_t *engine; + nxt_event_engine_t *engine; } nxt_buf_ts_t; - void -nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size) -{ +nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size) { b->mem.start = start; - b->mem.pos = start; - b->mem.free = start; - b->mem.end = nxt_pointer_to(start, size); + b->mem.pos = start; + b->mem.free = start; + b->mem.end = nxt_pointer_to(start, size); } - nxt_buf_t * -nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) -{ - nxt_buf_t *b; +nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) { + nxt_buf_t *b; b = nxt_mp_alloc(mp, NXT_BUF_MEM_SIZE + size); if (nxt_slow_path(b == NULL)) { @@ -39,25 +35,23 @@ nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) nxt_memzero(b, NXT_BUF_MEM_SIZE); - b->data = mp; + b->data = mp; b->completion_handler = nxt_buf_completion; if (size != 0) { b->mem.start = nxt_pointer_to(b, NXT_BUF_MEM_SIZE); - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start; - b->mem.end = b->mem.start + size; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start; + b->mem.end = b->mem.start + size; } return b; } - nxt_buf_t * -nxt_buf_mem_ts_alloc(nxt_task_t *task, nxt_mp_t *mp, size_t size) -{ - nxt_buf_t *b; - nxt_buf_ts_t *ts; +nxt_buf_mem_ts_alloc(nxt_task_t *task, nxt_mp_t *mp, size_t size) { + nxt_buf_t *b; + nxt_buf_ts_t *ts; b = nxt_mp_alloc(mp, NXT_BUF_MEM_SIZE + sizeof(nxt_buf_ts_t) + size); if (nxt_slow_path(b == NULL)) { @@ -68,34 +62,32 @@ nxt_buf_mem_ts_alloc(nxt_task_t *task, nxt_mp_t *mp, size_t size) nxt_memzero(b, NXT_BUF_MEM_SIZE + sizeof(nxt_buf_ts_t)); - b->data = mp; + b->data = mp; b->completion_handler = nxt_buf_ts_completion; - b->is_ts = 1; + b->is_ts = 1; if (size != 0) { - b->mem.start = nxt_pointer_to(b, NXT_BUF_MEM_SIZE - + sizeof(nxt_buf_ts_t)); - b->mem.pos = b->mem.start; + b->mem.start + = nxt_pointer_to(b, NXT_BUF_MEM_SIZE + sizeof(nxt_buf_ts_t)); + b->mem.pos = b->mem.start; b->mem.free = b->mem.start; - b->mem.end = b->mem.start + size; + b->mem.end = b->mem.start + size; } - ts = nxt_pointer_to(b, NXT_BUF_MEM_SIZE); + ts = nxt_pointer_to(b, NXT_BUF_MEM_SIZE); ts->engine = task->thread->engine; ts->work.handler = nxt_buf_ts_completion; - ts->work.task = task; - ts->work.obj = b; - ts->work.data = b->parent; + ts->work.task = task; + ts->work.obj = b; + ts->work.data = b->parent; return b; } - nxt_buf_t * -nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) -{ - nxt_buf_t *b; +nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) { + nxt_buf_t *b; b = nxt_mp_alloc(mp, NXT_BUF_FILE_SIZE + size); if (nxt_slow_path(b == NULL)) { @@ -104,30 +96,28 @@ nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags) nxt_memzero(b, NXT_BUF_FILE_SIZE); - b->data = mp; + b->data = mp; b->completion_handler = nxt_buf_completion; nxt_buf_set_file(b); if (size != 0) { b->mem.start = nxt_pointer_to(b, NXT_BUF_FILE_SIZE); - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start; - b->mem.end = b->mem.start + size; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start; + b->mem.end = b->mem.start + size; } return b; } - nxt_buf_t * -nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size) -{ - nxt_buf_t *b; +nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size) { + nxt_buf_t *b; b = nxt_mp_zalloc(mp, NXT_BUF_MMAP_SIZE); if (nxt_fast_path(b != NULL)) { - b->data = mp; + b->data = mp; b->completion_handler = nxt_buf_completion; nxt_buf_set_file(b); @@ -138,32 +128,28 @@ nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size) return b; } - nxt_buf_t * -nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags) -{ - nxt_buf_t *b; +nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags) { + nxt_buf_t *b; b = nxt_mp_zalloc(mp, NXT_BUF_MEM_SIZE); if (nxt_fast_path(b != NULL)) { - b->data = mp; + b->data = mp; b->completion_handler = nxt_buf_completion; nxt_buf_set_sync(b); b->is_nobuf = ((flags & NXT_BUF_SYNC_NOBUF) != 0); b->is_flush = ((flags & NXT_BUF_SYNC_FLUSH) != 0); - b->is_last = ((flags & NXT_BUF_SYNC_LAST) != 0); + b->is_last = ((flags & NXT_BUF_SYNC_LAST) != 0); } return b; } - void -nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in) -{ - nxt_buf_t *b, **prev; +nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in) { + nxt_buf_t *b, **prev; prev = head; @@ -174,11 +160,9 @@ nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in) *prev = in; } - size_t -nxt_buf_chain_length(nxt_buf_t *b) -{ - size_t length; +nxt_buf_chain_length(nxt_buf_t *b) { + size_t length; length = 0; @@ -193,12 +177,10 @@ nxt_buf_chain_length(nxt_buf_t *b) return length; } - static void -nxt_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b, *next, *parent; +nxt_buf_completion(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b, *next, *parent; b = obj; @@ -207,9 +189,9 @@ nxt_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_assert(data == b->parent); do { - next = b->next; + next = b->next; parent = b->parent; - mp = b->data; + mp = b->data; nxt_mp_free(mp, b); @@ -219,10 +201,8 @@ nxt_buf_completion(nxt_task_t *task, void *obj, void *data) } while (b != NULL); } - void -nxt_buf_parent_completion(nxt_task_t *task, nxt_buf_t *parent) -{ +nxt_buf_parent_completion(nxt_task_t *task, nxt_buf_t *parent) { if (parent != NULL) { nxt_debug(task, "parent retain:%uD", parent->retain); @@ -236,12 +216,10 @@ nxt_buf_parent_completion(nxt_task_t *task, nxt_buf_t *parent) } } - nxt_int_t -nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_buf_ts_t *ts; +nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_buf_ts_t *ts; b = obj; @@ -250,13 +228,12 @@ nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data) ts = nxt_pointer_to(b, NXT_BUF_MEM_SIZE); if (ts->engine != task->thread->engine) { - - nxt_debug(task, "buf ts: %p current engine is %p, expected %p", - b, task->thread->engine, ts->engine); + nxt_debug(task, "buf ts: %p current engine is %p, expected %p", b, + task->thread->engine, ts->engine); ts->work.handler = b->completion_handler; - ts->work.obj = obj; - ts->work.data = data; + ts->work.obj = obj; + ts->work.data = data; nxt_event_engine_post(ts->engine, &ts->work); @@ -266,12 +243,10 @@ nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data) return 0; } - static void -nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b, *next, *parent; +nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b, *next, *parent; b = obj; @@ -284,9 +259,9 @@ nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data) nxt_assert(data == b->parent); do { - next = b->next; + next = b->next; parent = b->parent; - mp = b->data; + mp = b->data; nxt_mp_free(mp, b); nxt_mp_release(mp); @@ -297,11 +272,9 @@ nxt_buf_ts_completion(nxt_task_t *task, void *obj, void *data) } while (b != NULL); } - nxt_buf_t * -nxt_buf_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) -{ - nxt_buf_t *b, *i; +nxt_buf_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) { + nxt_buf_t *b, *i; if (nxt_slow_path(size == 0)) { for (i = src; i != NULL; i = i->next) { @@ -316,9 +289,8 @@ nxt_buf_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) } for (i = src; i != NULL; i = i->next) { - if (nxt_slow_path(nxt_buf_mem_free_size(&b->mem) - < nxt_buf_used_size(i))) - { + if (nxt_slow_path( + nxt_buf_mem_free_size(&b->mem) < nxt_buf_used_size(i))) { break; } diff --git a/src/nxt_buf.h b/src/nxt_buf.h index a561ef4e1..4c746b19d 100644 --- a/src/nxt_buf.h +++ b/src/nxt_buf.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_BUF_H_INCLUDED_ #define _NXT_BUF_H_INCLUDED_ - /* * There are four types of buffers. They are different sizes, so they * should be allocated by appropriate nxt_buf_XXX_alloc() function. @@ -57,179 +55,156 @@ */ typedef struct { - u_char *pos; - u_char *free; - u_char *start; - u_char *end; + u_char *pos; + u_char *free; + u_char *start; + u_char *end; } nxt_buf_mem_t; - struct nxt_buf_s { - void *data; - nxt_work_handler_t completion_handler; - void *parent; + void *data; + nxt_work_handler_t completion_handler; + void *parent; /* * The next link, flags, and nxt_buf_mem_t should * reside together to improve cache locality. */ - nxt_buf_t *next; + nxt_buf_t *next; - uint32_t retain; + uint32_t retain; - uint8_t cache_hint; + uint8_t cache_hint; - uint8_t is_file:1; - uint8_t is_mmap:1; - uint8_t is_port_mmap:1; - uint8_t is_sync:1; - uint8_t is_nobuf:1; - uint8_t is_flush:1; - uint8_t is_last:1; - uint8_t is_port_mmap_sent:1; - uint8_t is_ts:1; + uint8_t is_file :1; + uint8_t is_mmap :1; + uint8_t is_port_mmap :1; + uint8_t is_sync :1; + uint8_t is_nobuf :1; + uint8_t is_flush :1; + uint8_t is_last :1; + uint8_t is_port_mmap_sent:1; + uint8_t is_ts :1; - nxt_buf_mem_t mem; + nxt_buf_mem_t mem; /* The file and mmap parts are not allocated by nxt_buf_mem_alloc(). */ - nxt_file_t *file; - nxt_off_t file_pos; - nxt_off_t file_end; + nxt_file_t *file; + nxt_off_t file_pos; + nxt_off_t file_end; /* The mmap part is not allocated by nxt_buf_file_alloc(). */ - nxt_mem_map_file_ctx_t (mmap) + nxt_mem_map_file_ctx_t(mmap) }; +#define NXT_BUF_SYNC_SIZE offsetof(nxt_buf_t, mem.free) +#define NXT_BUF_MEM_SIZE offsetof(nxt_buf_t, file) +#define NXT_BUF_FILE_SIZE sizeof(nxt_buf_t) +#define NXT_BUF_MMAP_SIZE NXT_BUF_FILE_SIZE +#define NXT_BUF_PORT_MMAP_SIZE NXT_BUF_MEM_SIZE -#define NXT_BUF_SYNC_SIZE offsetof(nxt_buf_t, mem.free) -#define NXT_BUF_MEM_SIZE offsetof(nxt_buf_t, file) -#define NXT_BUF_FILE_SIZE sizeof(nxt_buf_t) -#define NXT_BUF_MMAP_SIZE NXT_BUF_FILE_SIZE -#define NXT_BUF_PORT_MMAP_SIZE NXT_BUF_MEM_SIZE +#define NXT_BUF_SYNC_NOBUF 1 +#define NXT_BUF_SYNC_FLUSH 2 +#define NXT_BUF_SYNC_LAST 4 -#define NXT_BUF_SYNC_NOBUF 1 -#define NXT_BUF_SYNC_FLUSH 2 -#define NXT_BUF_SYNC_LAST 4 +#define nxt_buf_is_mem(b) ((b)->mem.pos != NULL) -#define nxt_buf_is_mem(b) \ - ((b)->mem.pos != NULL) +#define nxt_buf_is_file(b) ((b)->is_file) -#define nxt_buf_is_file(b) \ - ((b)->is_file) +#define nxt_buf_set_file(b) (b)->is_file = 1 -#define nxt_buf_set_file(b) \ - (b)->is_file = 1 +#define nxt_buf_clear_file(b) (b)->is_file = 0 -#define nxt_buf_clear_file(b) \ - (b)->is_file = 0 +#define nxt_buf_is_mmap(b) ((b)->is_mmap) -#define nxt_buf_is_mmap(b) \ - ((b)->is_mmap) +#define nxt_buf_set_mmap(b) (b)->is_mmap = 1 -#define nxt_buf_set_mmap(b) \ - (b)->is_mmap = 1 +#define nxt_buf_clear_mmap(b) (b)->is_mmap = 0 -#define nxt_buf_clear_mmap(b) \ - (b)->is_mmap = 0 +#define nxt_buf_is_port_mmap(b) ((b)->is_port_mmap) -#define nxt_buf_is_port_mmap(b) \ - ((b)->is_port_mmap) +#define nxt_buf_set_port_mmap(b) (b)->is_port_mmap = 1 -#define nxt_buf_set_port_mmap(b) \ - (b)->is_port_mmap = 1 +#define nxt_buf_clear_port_mmap(b) (b)->is_port_mmap = 0 -#define nxt_buf_clear_port_mmap(b) \ - (b)->is_port_mmap = 0 +#define nxt_buf_is_sync(b) ((b)->is_sync) -#define nxt_buf_is_sync(b) \ - ((b)->is_sync) +#define nxt_buf_set_sync(b) (b)->is_sync = 1 -#define nxt_buf_set_sync(b) \ - (b)->is_sync = 1 +#define nxt_buf_clear_sync(b) (b)->is_sync = 0 -#define nxt_buf_clear_sync(b) \ - (b)->is_sync = 0 +#define nxt_buf_is_nobuf(b) ((b)->is_nobuf) -#define nxt_buf_is_nobuf(b) \ - ((b)->is_nobuf) +#define nxt_buf_set_nobuf(b) (b)->is_nobuf = 1 -#define nxt_buf_set_nobuf(b) \ - (b)->is_nobuf = 1 +#define nxt_buf_clear_nobuf(b) (b)->is_nobuf = 0 -#define nxt_buf_clear_nobuf(b) \ - (b)->is_nobuf = 0 +#define nxt_buf_is_flush(b) ((b)->is_flush) -#define nxt_buf_is_flush(b) \ - ((b)->is_flush) +#define nxt_buf_set_flush(b) (b)->is_flush = 1 -#define nxt_buf_set_flush(b) \ - (b)->is_flush = 1 +#define nxt_buf_clear_flush(b) (b)->is_flush = 0 -#define nxt_buf_clear_flush(b) \ - (b)->is_flush = 0 +#define nxt_buf_is_last(b) ((b)->is_last) -#define nxt_buf_is_last(b) \ - ((b)->is_last) +#define nxt_buf_set_last(b) (b)->is_last = 1 -#define nxt_buf_set_last(b) \ - (b)->is_last = 1 +#define nxt_buf_clear_last(b) (b)->is_last = 0 -#define nxt_buf_clear_last(b) \ - (b)->is_last = 0 - -#define nxt_buf_mem_set_size(bm, size) \ - do { \ - (bm)->start = 0; \ - (bm)->end = (void *) size; \ +#define nxt_buf_mem_set_size(bm, size) \ + do { \ + (bm)->start = 0; \ + (bm)->end = (void *) size; \ } while (0) -#define nxt_buf_mem_size(bm) \ - ((bm)->end - (bm)->start) +#define nxt_buf_mem_size(bm) ((bm)->end - (bm)->start) -#define nxt_buf_mem_used_size(bm) \ - ((bm)->free - (bm)->pos) +#define nxt_buf_mem_used_size(bm) ((bm)->free - (bm)->pos) -#define nxt_buf_mem_free_size(bm) \ - ((bm)->end - (bm)->free) +#define nxt_buf_mem_free_size(bm) ((bm)->end - (bm)->free) -#define nxt_buf_used_size(b) \ - (nxt_buf_is_file(b) ? (b)->file_end - (b)->file_pos: \ - nxt_buf_mem_used_size(&(b)->mem)) +#define nxt_buf_used_size(b) \ + (nxt_buf_is_file(b) ? (b)->file_end - (b)->file_pos \ + : nxt_buf_mem_used_size(&(b)->mem)) -NXT_EXPORT void nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size); -NXT_EXPORT nxt_buf_t *nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, - nxt_uint_t flags); -NXT_EXPORT nxt_buf_t *nxt_buf_mem_ts_alloc(nxt_task_t *task, nxt_mp_t *mp, - size_t size); -NXT_EXPORT nxt_buf_t *nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, - nxt_uint_t flags); -NXT_EXPORT nxt_buf_t *nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size); -NXT_EXPORT nxt_buf_t *nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags); +NXT_EXPORT void +nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size); +NXT_EXPORT nxt_buf_t * +nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags); +NXT_EXPORT nxt_buf_t * +nxt_buf_mem_ts_alloc(nxt_task_t *task, nxt_mp_t *mp, size_t size); +NXT_EXPORT nxt_buf_t * +nxt_buf_file_alloc(nxt_mp_t *mp, size_t size, nxt_uint_t flags); +NXT_EXPORT nxt_buf_t * +nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size); +NXT_EXPORT nxt_buf_t * +nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags); -NXT_EXPORT nxt_int_t nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data); +NXT_EXPORT nxt_int_t +nxt_buf_ts_handle(nxt_task_t *task, void *obj, void *data); -NXT_EXPORT void nxt_buf_parent_completion(nxt_task_t *task, nxt_buf_t *parent); -NXT_EXPORT nxt_buf_t *nxt_buf_make_plain(nxt_mp_t *mp, nxt_buf_t *src, - size_t size); +NXT_EXPORT void +nxt_buf_parent_completion(nxt_task_t *task, nxt_buf_t *parent); +NXT_EXPORT nxt_buf_t * +nxt_buf_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size); nxt_inline nxt_buf_t * -nxt_buf_chk_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) -{ +nxt_buf_chk_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) { if (nxt_slow_path(src != NULL && src->next != NULL)) { return nxt_buf_make_plain(mp, src, size); } @@ -237,16 +212,16 @@ nxt_buf_chk_make_plain(nxt_mp_t *mp, nxt_buf_t *src, size_t size) return src; } -#define nxt_buf_free(mp, b) \ - nxt_mp_free((mp), (b)) +#define nxt_buf_free(mp, b) nxt_mp_free((mp), (b)) -NXT_EXPORT void nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in); -NXT_EXPORT size_t nxt_buf_chain_length(nxt_buf_t *b); +NXT_EXPORT void +nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in); +NXT_EXPORT size_t +nxt_buf_chain_length(nxt_buf_t *b); nxt_inline nxt_buf_t * -nxt_buf_cpy(nxt_buf_t *b, const void *src, size_t length) -{ +nxt_buf_cpy(nxt_buf_t *b, const void *src, size_t length) { nxt_memcpy(b->mem.free, src, length); b->mem.free += length; @@ -254,15 +229,12 @@ nxt_buf_cpy(nxt_buf_t *b, const void *src, size_t length) } nxt_inline nxt_buf_t * -nxt_buf_cpystr(nxt_buf_t *b, const nxt_str_t *str) -{ +nxt_buf_cpystr(nxt_buf_t *b, const nxt_str_t *str) { return nxt_buf_cpy(b, str->start, str->length); } - nxt_inline void -nxt_buf_dummy_completion(nxt_task_t *task, void *obj, void *data) -{ +nxt_buf_dummy_completion(nxt_task_t *task, void *obj, void *data) { } diff --git a/src/nxt_buf_pool.c b/src/nxt_buf_pool.c index f2be88a79..fce6e6284 100644 --- a/src/nxt_buf_pool.c +++ b/src/nxt_buf_pool.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,11 +5,9 @@ #include - nxt_int_t -nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size) -{ - nxt_buf_t *b; +nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size) { + nxt_buf_t *b; b = bp->current; @@ -22,8 +19,8 @@ nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size) if (b != NULL) { bp->current = b; - bp->free = b->next; - b->next = NULL; + bp->free = b->next; + b->next = NULL; return NXT_OK; } @@ -46,11 +43,9 @@ nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size) return NXT_ERROR; } - nxt_int_t -nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size) -{ - nxt_buf_t *b; +nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size) { + nxt_buf_t *b; b = bp->current; @@ -62,8 +57,8 @@ nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size) if (b != NULL) { bp->current = b; - bp->free = b->next; - b->next = NULL; + bp->free = b->next; + b->next = NULL; return NXT_OK; } @@ -86,11 +81,9 @@ nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size) return NXT_ERROR; } - nxt_int_t -nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size) -{ - nxt_buf_t *b; +nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size) { + nxt_buf_t *b; b = bp->current; @@ -102,8 +95,8 @@ nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size) if (b != NULL) { bp->current = b; - bp->free = b->next; - b->next = NULL; + bp->free = b->next; + b->next = NULL; return NXT_OK; } @@ -118,7 +111,7 @@ nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size) b = nxt_buf_mmap_alloc(bp->mem_pool, size); if (nxt_fast_path(b != NULL)) { - bp->mmap = 1; + bp->mmap = 1; bp->current = b; bp->num++; return NXT_OK; @@ -127,11 +120,9 @@ nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size) return NXT_ERROR; } - void -nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b) -{ - size_t size; +nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b) { + size_t size; nxt_thread_log_debug("buf pool free: %p %p", b, b->mem.start); @@ -142,7 +133,6 @@ nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b) } if (bp->destroy) { - if (b == bp->current) { bp->current = NULL; } @@ -153,26 +143,24 @@ nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b) } if (bp->mmap) { - b->mem.pos = NULL; + b->mem.pos = NULL; b->mem.free = NULL; nxt_buf_mem_set_size(&b->mem, size); } else { - b->mem.pos = b->mem.start; + b->mem.pos = b->mem.start; b->mem.free = b->mem.start; } if (b != bp->current) { - b->next = bp->free; + b->next = bp->free; bp->free = b; } } - void -nxt_buf_pool_destroy(nxt_buf_pool_t *bp) -{ - nxt_buf_t *b, *n; +nxt_buf_pool_destroy(nxt_buf_pool_t *bp) { + nxt_buf_t *b, *n; bp->destroy = 1; diff --git a/src/nxt_buf_pool.h b/src/nxt_buf_pool.h index 3d22d7fa0..6768af566 100644 --- a/src/nxt_buf_pool.h +++ b/src/nxt_buf_pool.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_BUF_POOL_H_INCLUDED_ #define _NXT_BUF_POOL_H_INCLUDED_ - /* * nxt_buf_pool_t is intended to allocate up to the "max" number * memory, memory/file, or mmap/file buffers. A size of the buffers @@ -18,58 +16,59 @@ */ typedef struct { - nxt_buf_t *current; - nxt_buf_t *free; - nxt_mp_t *mem_pool; + nxt_buf_t *current; + nxt_buf_t *free; + nxt_mp_t *mem_pool; - uint16_t num; - uint16_t max; + uint16_t num; + uint16_t max; - uint32_t size; + uint32_t size; - uint8_t flags; /* 2 bits */ - uint8_t destroy; /* 1 bit */ - uint8_t mmap; /* 1 bit */ + uint8_t flags; /* 2 bits */ + uint8_t destroy; /* 1 bit */ + uint8_t mmap; /* 1 bit */ } nxt_buf_pool_t; - -NXT_EXPORT nxt_int_t nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size); -NXT_EXPORT nxt_int_t nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size); -NXT_EXPORT nxt_int_t nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size); -NXT_EXPORT void nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b); -NXT_EXPORT void nxt_buf_pool_destroy(nxt_buf_pool_t *bp); +NXT_EXPORT nxt_int_t +nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size); +NXT_EXPORT nxt_int_t +nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size); +NXT_EXPORT nxt_int_t +nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size); +NXT_EXPORT void +nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b); +NXT_EXPORT void +nxt_buf_pool_destroy(nxt_buf_pool_t *bp); /* There is ready free buffer. */ -#define nxt_buf_pool_ready(bp) \ - ((bp)->free != NULL \ - || ((bp)->current != NULL \ - && (bp)->current->mem.free < (bp)->current->mem.end)) +#define nxt_buf_pool_ready(bp) \ + ((bp)->free != NULL \ + || ((bp)->current != NULL \ + && (bp)->current->mem.free < (bp)->current->mem.end)) /* A free buffer is allowed to be allocated. */ -#define nxt_buf_pool_obtainable(bp) \ - ((bp)->num < (bp)->max) +#define nxt_buf_pool_obtainable(bp) ((bp)->num < (bp)->max) /* There is ready free buffer or it is allowed to be allocated. */ -#define nxt_buf_pool_available(bp) \ +#define nxt_buf_pool_available(bp) \ (nxt_buf_pool_obtainable(bp) || nxt_buf_pool_ready(bp)) /* Reserve allocation of "n" free buffers as they were allocated. */ -#define nxt_buf_pool_reserve(bp, n) \ - (bp)->num += (n) +#define nxt_buf_pool_reserve(bp, n) (bp)->num += (n) /* Release a reservation. */ -#define nxt_buf_pool_release(bp, n) \ - (bp)->num -= (n) +#define nxt_buf_pool_release(bp, n) (bp)->num -= (n) #endif /* _NXT_BUF_POOL_H_INCLUDED_ */ diff --git a/src/nxt_capability.c b/src/nxt_capability.c index 9f36ab997..82fb1fc81 100644 --- a/src/nxt_capability.c +++ b/src/nxt_capability.c @@ -12,33 +12,29 @@ #if (_LINUX_CAPABILITY_VERSION_3) -#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3 +#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_3 #elif (_LINUX_CAPABILITY_VERSION_2) -#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_2 +#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION_2 #else -#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION +#define NXT_CAPABILITY_VERSION _LINUX_CAPABILITY_VERSION #endif -#define nxt_capget(hdrp, datap) \ - syscall(SYS_capget, hdrp, datap) -#define nxt_capset(hdrp, datap) \ - syscall(SYS_capset, hdrp, datap) +#define nxt_capget(hdrp, datap) syscall(SYS_capget, hdrp, datap) +#define nxt_capset(hdrp, datap) syscall(SYS_capset, hdrp, datap) #endif /* NXT_HAVE_LINUX_CAPABILITY */ -static nxt_int_t nxt_capability_specific_set(nxt_task_t *task, - nxt_capabilities_t *cap); - +static nxt_int_t +nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap); nxt_int_t -nxt_capability_set(nxt_task_t *task, nxt_capabilities_t *cap) -{ +nxt_capability_set(nxt_task_t *task, nxt_capabilities_t *cap) { nxt_assert(cap->setid == 0); if (geteuid() == 0) { - cap->setid = 1; + cap->setid = 1; cap->chroot = 1; return NXT_OK; } @@ -50,8 +46,7 @@ nxt_capability_set(nxt_task_t *task, nxt_capabilities_t *cap) #if (NXT_HAVE_LINUX_CAPABILITY) static uint32_t -nxt_capability_linux_get_version(void) -{ +nxt_capability_linux_get_version(void) { struct __user_cap_header_struct hdr; hdr.version = NXT_CAPABILITY_VERSION; @@ -61,12 +56,10 @@ nxt_capability_linux_get_version(void) return hdr.version; } - static nxt_int_t -nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) -{ - struct __user_cap_data_struct *val, data[2]; - struct __user_cap_header_struct hdr; +nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) { + struct __user_cap_data_struct *val, data[2]; + struct __user_cap_header_struct hdr; /* * Linux capability v1 fills an u32 struct. @@ -85,7 +78,7 @@ nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) * This is safer when distributing a pre-compiled Unit binary. */ hdr.version = nxt_capability_linux_get_version(); - hdr.pid = nxt_pid; + hdr.pid = nxt_pid; if (nxt_slow_path(nxt_capget(&hdr, val) == -1)) { nxt_alert(task, "failed to get process capabilities: %E", nxt_errno); @@ -111,8 +104,7 @@ nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) #else static nxt_int_t -nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) -{ +nxt_capability_specific_set(nxt_task_t *task, nxt_capabilities_t *cap) { return NXT_OK; } diff --git a/src/nxt_capability.h b/src/nxt_capability.h index 1575d4094..a6fcaf19d 100644 --- a/src/nxt_capability.h +++ b/src/nxt_capability.h @@ -7,12 +7,11 @@ #define _NXT_CAPABILITY_INCLUDED_ typedef struct { - uint8_t setid; /* 1 bit */ - uint8_t chroot; /* 1 bit */ + uint8_t setid; /* 1 bit */ + uint8_t chroot; /* 1 bit */ } nxt_capabilities_t; - -NXT_EXPORT nxt_int_t nxt_capability_set(nxt_task_t *task, - nxt_capabilities_t *cap); +NXT_EXPORT nxt_int_t +nxt_capability_set(nxt_task_t *task, nxt_capabilities_t *cap); #endif /* _NXT_CAPABILITY_INCLUDED_ */ diff --git a/src/nxt_cert.c b/src/nxt_cert.c index 4a1f14961..6637f3aef 100644 --- a/src/nxt_cert.c +++ b/src/nxt_cert.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Valentin V. Bartenev * Copyright (C) NGINX, Inc. @@ -18,47 +17,46 @@ #include #include - struct nxt_cert_s { - EVP_PKEY *key; - nxt_uint_t count; - X509 *chain[]; + EVP_PKEY *key; + nxt_uint_t count; + X509 *chain[]; }; - typedef struct { nxt_str_t name; - nxt_conf_value_t *value; - nxt_mp_t *mp; + nxt_conf_value_t *value; + nxt_mp_t *mp; } nxt_cert_info_t; - typedef struct { - nxt_str_t name; - nxt_fd_t fd; + nxt_str_t name; + nxt_fd_t fd; } nxt_cert_item_t; +static nxt_cert_t * +nxt_cert_fd(nxt_task_t *task, nxt_fd_t fd); +static nxt_cert_t * +nxt_cert_bio(nxt_task_t *task, BIO *bio); +static int +nxt_nxt_cert_pem_suffix(char *pem_str, const char *suffix); -static nxt_cert_t *nxt_cert_fd(nxt_task_t *task, nxt_fd_t fd); -static nxt_cert_t *nxt_cert_bio(nxt_task_t *task, BIO *bio); -static int nxt_nxt_cert_pem_suffix(char *pem_str, const char *suffix); - -static nxt_conf_value_t *nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert); -static nxt_conf_value_t *nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, - nxt_bool_t issuer); -static nxt_conf_value_t *nxt_cert_alt_names_details(nxt_mp_t *mp, - STACK_OF(GENERAL_NAME) *alt_names); -static void nxt_cert_buf_completion(nxt_task_t *task, void *obj, void *data); - +static nxt_conf_value_t * +nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert); +static nxt_conf_value_t * +nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer); +static nxt_conf_value_t * +nxt_cert_alt_names_details(nxt_mp_t *mp, STACK_OF(GENERAL_NAME) * alt_names); +static void +nxt_cert_buf_completion(nxt_task_t *task, void *obj, void *data); -static nxt_lvlhsh_t nxt_cert_info; +static nxt_lvlhsh_t nxt_cert_info; nxt_cert_t * -nxt_cert_mem(nxt_task_t *task, nxt_buf_mem_t *mbuf) -{ - BIO *bio; - nxt_cert_t *cert; +nxt_cert_mem(nxt_task_t *task, nxt_buf_mem_t *mbuf) { + BIO *bio; + nxt_cert_t *cert; bio = BIO_new_mem_buf(mbuf->pos, nxt_buf_mem_used_size(mbuf)); if (nxt_slow_path(bio == NULL)) { @@ -73,12 +71,10 @@ nxt_cert_mem(nxt_task_t *task, nxt_buf_mem_t *mbuf) return cert; } - static nxt_cert_t * -nxt_cert_fd(nxt_task_t *task, nxt_fd_t fd) -{ - BIO *bio; - nxt_cert_t *cert; +nxt_cert_fd(nxt_task_t *task, nxt_fd_t fd) { + BIO *bio; + nxt_cert_t *cert; bio = BIO_new_fd(fd, 0); if (nxt_slow_path(bio == NULL)) { @@ -93,21 +89,19 @@ nxt_cert_fd(nxt_task_t *task, nxt_fd_t fd) return cert; } - static nxt_cert_t * -nxt_cert_bio(nxt_task_t *task, BIO *bio) -{ +nxt_cert_bio(nxt_task_t *task, BIO *bio) { int ret, suffix, key_id; long length, reason; - char *type, *header; - X509 *x509; - EVP_PKEY *key; + char *type, *header; + X509 *x509; + EVP_PKEY *key; nxt_uint_t nalloc; - nxt_cert_t *cert, *new_cert; - u_char *data; - const u_char *data_copy; - PKCS8_PRIV_KEY_INFO *p8inf; - const EVP_PKEY_ASN1_METHOD *ameth; + nxt_cert_t *cert, *new_cert; + u_char *data; + const u_char *data_copy; + PKCS8_PRIV_KEY_INFO *p8inf; + const EVP_PKEY_ASN1_METHOD *ameth; nalloc = 4; @@ -116,14 +110,14 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) return NULL; } - for ( ;; ) { + for (;;) { ret = PEM_read_bio(bio, &type, &header, &data, &length); if (ret == 0) { reason = ERR_GET_REASON(ERR_peek_last_error()); if (reason != PEM_R_NO_START_LINE) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "PEM_read_bio() failed"); + "PEM_read_bio() failed"); goto fail; } @@ -133,16 +127,16 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) nxt_debug(task, "PEM type: \"%s\"", type); - key = NULL; + key = NULL; x509 = NULL; -/* - EVP_CIPHER_INFO cipher; + /* + EVP_CIPHER_INFO cipher; - if (PEM_get_EVP_CIPHER_INFO(header, &cipher) != 0) { - nxt_alert(task, "encrypted PEM isn't supported"); - goto done; - } -*/ + if (PEM_get_EVP_CIPHER_INFO(header, &cipher) != 0) { + nxt_alert(task, "encrypted PEM isn't supported"); + goto done; + } + */ if (nxt_strcmp(type, PEM_STRING_PKCS8) == 0) { nxt_alert(task, "PEM PKCS8 isn't supported"); goto done; @@ -155,7 +149,7 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) if (p8inf == NULL) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "d2i_PKCS8_PRIV_KEY_INFO() failed"); + "d2i_PKCS8_PRIV_KEY_INFO() failed"); goto done; } @@ -168,11 +162,10 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) suffix = nxt_nxt_cert_pem_suffix(type, PEM_STRING_PKCS8INF); if (suffix != 0) { - ameth = EVP_PKEY_asn1_find_str(NULL, type, suffix); if (ameth == NULL) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "EVP_PKEY_asn1_find_str() failed"); + "EVP_PKEY_asn1_find_str() failed"); goto done; } @@ -185,14 +178,12 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) } if (nxt_strcmp(type, PEM_STRING_X509) == 0 - || nxt_strcmp(type, PEM_STRING_X509_OLD) == 0) - { + || nxt_strcmp(type, PEM_STRING_X509_OLD) == 0) { data_copy = data; x509 = d2i_X509(NULL, &data_copy, length); if (x509 == NULL) { - nxt_openssl_log_error(task, NXT_LOG_ALERT, - "d2i_X509() failed"); + nxt_openssl_log_error(task, NXT_LOG_ALERT, "d2i_X509() failed"); } goto done; @@ -204,7 +195,7 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) x509 = d2i_X509_AUX(NULL, &data_copy, length); if (x509 == NULL) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "d2i_X509_AUX() failed"); + "d2i_X509_AUX() failed"); } goto done; @@ -230,12 +221,11 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) } if (x509 != NULL) { - if (cert->count == nalloc) { nalloc += 4; - new_cert = nxt_realloc(cert, sizeof(nxt_cert_t) - + nalloc * sizeof(X509 *)); + new_cert = nxt_realloc(cert, + sizeof(nxt_cert_t) + nalloc * sizeof(X509 *)); if (new_cert == NULL) { X509_free(x509); goto fail; @@ -270,14 +260,12 @@ nxt_cert_bio(nxt_task_t *task, BIO *bio) return NULL; } - static int -nxt_nxt_cert_pem_suffix(char *pem_str, const char *suffix) -{ - char *p; - nxt_uint_t pem_len, suffix_len; +nxt_nxt_cert_pem_suffix(char *pem_str, const char *suffix) { + char *p; + nxt_uint_t pem_len, suffix_len; - pem_len = strlen(pem_str); + pem_len = strlen(pem_str); suffix_len = strlen(suffix); if (suffix_len + 1 >= pem_len) { @@ -299,11 +287,9 @@ nxt_nxt_cert_pem_suffix(char *pem_str, const char *suffix) return p - pem_str; } - void -nxt_cert_destroy(nxt_cert_t *cert) -{ - nxt_uint_t i; +nxt_cert_destroy(nxt_cert_t *cert) { + nxt_uint_t i; EVP_PKEY_free(cert->key); @@ -314,12 +300,9 @@ nxt_cert_destroy(nxt_cert_t *cert) nxt_free(cert); } - - static nxt_int_t -nxt_cert_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_cert_info_t *info; +nxt_cert_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_cert_info_t *info; info = data; @@ -330,23 +313,18 @@ nxt_cert_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t nxt_cert_info_hash_proto - nxt_aligned(64) = -{ +static const nxt_lvlhsh_proto_t nxt_cert_info_hash_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_cert_info_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - void -nxt_cert_info_init(nxt_task_t *task, nxt_array_t *certs) -{ +nxt_cert_info_init(nxt_task_t *task, nxt_array_t *certs) { uint32_t i; - nxt_cert_t *cert; - nxt_cert_item_t *items; + nxt_cert_t *cert; + nxt_cert_item_t *items; for (items = certs->elts, i = 0; i < certs->nelts; i++) { cert = nxt_cert_fd(task, items[i].fd); @@ -361,15 +339,13 @@ nxt_cert_info_init(nxt_task_t *task, nxt_array_t *certs) } } - nxt_int_t -nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert) -{ - nxt_mp_t *mp; - nxt_int_t ret; - nxt_cert_info_t *info; - nxt_conf_value_t *value; - nxt_lvlhsh_query_t lhq; +nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert) { + nxt_mp_t *mp; + nxt_int_t ret; + nxt_cert_info_t *info; + nxt_conf_value_t *value; + nxt_lvlhsh_query_t lhq; mp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp == NULL)) { @@ -391,14 +367,14 @@ nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert) goto fail; } - info->mp = mp; + info->mp = mp; info->value = value; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.replace = 1; - lhq.key = *name; - lhq.value = info; - lhq.proto = &nxt_cert_info_hash_proto; + lhq.replace = 1; + lhq.key = *name; + lhq.value = info; + lhq.proto = &nxt_cert_info_hash_proto; ret = nxt_lvlhsh_insert(&nxt_cert_info, &lhq); if (nxt_slow_path(ret != NXT_OK)) { @@ -418,17 +394,15 @@ nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert) return NXT_ERROR; } - nxt_conf_value_t * -nxt_cert_info_get(nxt_str_t *name) -{ - nxt_int_t ret; - nxt_cert_info_t *info; - nxt_lvlhsh_query_t lhq; +nxt_cert_info_get(nxt_str_t *name) { + nxt_int_t ret; + nxt_cert_info_t *info; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_cert_info_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_cert_info_hash_proto; ret = nxt_lvlhsh_find(&nxt_cert_info, &lhq); if (ret != NXT_OK) { @@ -440,20 +414,18 @@ nxt_cert_info_get(nxt_str_t *name) return info->value; } - nxt_conf_value_t * -nxt_cert_info_get_all(nxt_mp_t *mp) -{ - uint32_t i; - nxt_cert_info_t *info; - nxt_conf_value_t *all; - nxt_lvlhsh_each_t lhe; +nxt_cert_info_get_all(nxt_mp_t *mp) { + uint32_t i; + nxt_cert_info_t *info; + nxt_conf_value_t *all; + nxt_lvlhsh_each_t lhe; nxt_lvlhsh_each_init(&lhe, &nxt_cert_info_hash_proto); i = 0; - for ( ;; ) { + for (;;) { info = nxt_lvlhsh_each(&nxt_cert_info, &lhe); if (info == NULL) { @@ -472,7 +444,7 @@ nxt_cert_info_get_all(nxt_mp_t *mp) i = 0; - for ( ;; ) { + for (;;) { info = nxt_lvlhsh_each(&nxt_cert_info, &lhe); if (info == NULL) { @@ -487,27 +459,25 @@ nxt_cert_info_get_all(nxt_mp_t *mp) return all; } - static nxt_conf_value_t * -nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) -{ - BIO *bio; - X509 *x509; - u_char *end; - EVP_PKEY *key; - ASN1_TIME *asn1_time; +nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) { + BIO *bio; + X509 *x509; + u_char *end; + EVP_PKEY *key; + ASN1_TIME *asn1_time; nxt_str_t str; nxt_int_t ret; nxt_uint_t i; - nxt_conf_value_t *object, *chain, *element, *value; + nxt_conf_value_t *object, *chain, *element, *value; u_char buf[256]; - static nxt_str_t key_str = nxt_string("key"); - static nxt_str_t chain_str = nxt_string("chain"); - static nxt_str_t since_str = nxt_string("since"); - static nxt_str_t until_str = nxt_string("until"); - static nxt_str_t issuer_str = nxt_string("issuer"); - static nxt_str_t subject_str = nxt_string("subject"); + static nxt_str_t key_str = nxt_string("key"); + static nxt_str_t chain_str = nxt_string("chain"); + static nxt_str_t since_str = nxt_string("since"); + static nxt_str_t until_str = nxt_string("until"); + static nxt_str_t issuer_str = nxt_string("issuer"); + static nxt_str_t subject_str = nxt_string("subject"); static nxt_str_t validity_str = nxt_string("validity"); object = nxt_conf_create_object(mp, 2); @@ -521,18 +491,18 @@ nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) switch (EVP_PKEY_base_id(key)) { case EVP_PKEY_RSA: end = nxt_sprintf(buf, buf + sizeof(buf), "RSA (%d bits)", - EVP_PKEY_bits(key)); + EVP_PKEY_bits(key)); str.length = end - buf; - str.start = buf; + str.start = buf; break; case EVP_PKEY_DH: end = nxt_sprintf(buf, buf + sizeof(buf), "DH (%d bits)", - EVP_PKEY_bits(key)); + EVP_PKEY_bits(key)); str.length = end - buf; - str.start = buf; + str.start = buf; break; case EVP_PKEY_EC: @@ -597,7 +567,7 @@ nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) if (nxt_fast_path(ret == 1)) { str.length = BIO_get_mem_data(bio, &str.start); ret = nxt_conf_set_member_string_dup(value, mp, &since_str, &str, - 0); + 0); } else { ret = NXT_ERROR; } @@ -620,7 +590,7 @@ nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) if (nxt_fast_path(ret == 1)) { str.length = BIO_get_mem_data(bio, &str.start); ret = nxt_conf_set_member_string_dup(value, mp, &until_str, &str, - 1); + 1); } else { ret = NXT_ERROR; } @@ -641,43 +611,39 @@ nxt_cert_details(nxt_mp_t *mp, nxt_cert_t *cert) return object; } - typedef struct { - int nid; - nxt_str_t name; + int nid; + nxt_str_t name; } nxt_cert_nid_t; - static nxt_conf_value_t * -nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) -{ - int len; - X509_NAME *x509_name; - nxt_str_t str; - nxt_int_t ret; - nxt_uint_t i, n, count; - nxt_conf_value_t *object, *names; - STACK_OF(GENERAL_NAME) *alt_names; - u_char buf[256]; - - static nxt_cert_nid_t nids[] = { - { NID_commonName, nxt_string("common_name") }, - { NID_countryName, nxt_string("country") }, - { NID_stateOrProvinceName, nxt_string("state_or_province") }, - { NID_localityName, nxt_string("locality") }, - { NID_organizationName, nxt_string("organization") }, - { NID_organizationalUnitName, nxt_string("department") }, +nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) { + int len; + X509_NAME *x509_name; + nxt_str_t str; + nxt_int_t ret; + nxt_uint_t i, n, count; + nxt_conf_value_t *object, *names; + STACK_OF(GENERAL_NAME) * alt_names; + u_char buf[256]; + + static nxt_cert_nid_t nids[] = { + {NID_commonName, nxt_string("common_name")}, + {NID_countryName, nxt_string("country")}, + {NID_stateOrProvinceName, nxt_string("state_or_province")}, + {NID_localityName, nxt_string("locality")}, + {NID_organizationName, nxt_string("organization")}, + {NID_organizationalUnitName, nxt_string("department")}, }; static nxt_str_t alt_names_str = nxt_string("alt_names"); count = 0; - x509_name = issuer ? X509_get_issuer_name(x509) - : X509_get_subject_name(x509); + x509_name + = issuer ? X509_get_issuer_name(x509) : X509_get_subject_name(x509); for (n = 0; n != nxt_nitems(nids); n++) { - if (X509_NAME_get_index_by_NID(x509_name, nids[n].nid, -1) < 0) { continue; } @@ -685,9 +651,8 @@ nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) count++; } - alt_names = X509_get_ext_d2i(x509, issuer ? NID_issuer_alt_name - : NID_subject_alt_name, - NULL, NULL); + alt_names = X509_get_ext_d2i(x509, + issuer ? NID_issuer_alt_name : NID_subject_alt_name, NULL, NULL); if (alt_names != NULL) { names = nxt_cert_alt_names_details(mp, alt_names); @@ -710,9 +675,8 @@ nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) } for (n = 0, i = 0; n != nxt_nitems(nids) && i != count; n++) { - - len = X509_NAME_get_text_by_NID(x509_name, nids[n].nid, - (char *) buf, sizeof(buf)); + len = X509_NAME_get_text_by_NID(x509_name, nids[n].nid, (char *) buf, + sizeof(buf)); if (n == 1 && names != NULL) { nxt_conf_set_member(object, &alt_names_str, names, i++); @@ -723,10 +687,10 @@ nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) } str.length = len; - str.start = buf; + str.start = buf; - ret = nxt_conf_set_member_string_dup(object, mp, &nids[n].name, - &str, i++); + ret = nxt_conf_set_member_string_dup(object, mp, &nids[n].name, &str, + i++); if (nxt_slow_path(ret != NXT_OK)) { return NULL; } @@ -735,18 +699,16 @@ nxt_cert_name_details(nxt_mp_t *mp, X509 *x509, nxt_bool_t issuer) return object; } - static nxt_conf_value_t * -nxt_cert_alt_names_details(nxt_mp_t *mp, STACK_OF(GENERAL_NAME) *alt_names) -{ +nxt_cert_alt_names_details(nxt_mp_t *mp, STACK_OF(GENERAL_NAME) * alt_names) { nxt_str_t str; nxt_int_t ret; nxt_uint_t i, n, count; - GENERAL_NAME *name; - nxt_conf_value_t *array; + GENERAL_NAME *name; + nxt_conf_value_t *array; count = sk_GENERAL_NAME_num(alt_names); - n = 0; + n = 0; for (i = 0; i != count; i++) { name = sk_GENERAL_NAME_value(alt_names, i); @@ -786,17 +748,15 @@ nxt_cert_alt_names_details(nxt_mp_t *mp, STACK_OF(GENERAL_NAME) *alt_names) return array; } - nxt_int_t -nxt_cert_info_delete(nxt_str_t *name) -{ - nxt_int_t ret; - nxt_cert_info_t *info; - nxt_lvlhsh_query_t lhq; +nxt_cert_info_delete(nxt_str_t *name) { + nxt_int_t ret; + nxt_cert_info_t *info; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_cert_info_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_cert_info_hash_proto; ret = nxt_lvlhsh_delete(&nxt_cert_info, &lhq); @@ -808,21 +768,18 @@ nxt_cert_info_delete(nxt_str_t *name) return ret; } - - nxt_array_t * -nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) -{ - DIR *dir; +nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) { + DIR *dir; size_t size, alloc; - u_char *buf, *p; + u_char *buf, *p; nxt_str_t name; nxt_int_t ret; nxt_file_t file; - nxt_array_t *certs; - nxt_runtime_t *rt; - struct dirent *de; - nxt_cert_item_t *item; + nxt_array_t *certs; + nxt_runtime_t *rt; + struct dirent *de; + nxt_cert_item_t *item; rt = task->thread->runtime; @@ -836,17 +793,17 @@ nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) return NULL; } - buf = NULL; + buf = NULL; alloc = 0; dir = opendir((char *) rt->certs.start); if (nxt_slow_path(dir == NULL)) { - nxt_alert(task, "opendir(\"%s\") failed %E", - rt->certs.start, nxt_errno); + nxt_alert(task, "opendir(\"%s\") failed %E", rt->certs.start, + nxt_errno); goto fail; } - for ( ;; ) { + for (;;) { de = readdir(dir); if (de == NULL) { break; @@ -855,7 +812,7 @@ nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) nxt_debug(task, "readdir(\"%s\"): \"%s\"", rt->certs.start, de->d_name); name.length = nxt_strlen(de->d_name); - name.start = (u_char *) de->d_name; + name.start = (u_char *) de->d_name; if (nxt_str_eq(&name, ".", 1) || nxt_str_eq(&name, "..", 2)) { continue; @@ -879,7 +836,7 @@ nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) } alloc = size; - buf = p; + buf = p; } p = nxt_cpymem(buf, rt->certs.start, rt->certs.length); @@ -890,7 +847,7 @@ nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) file.name = buf; ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); if (nxt_slow_path(ret != NXT_OK)) { @@ -928,17 +885,12 @@ nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mp) return NULL; } - void -nxt_cert_store_release(nxt_array_t *certs) -{ +nxt_cert_store_release(nxt_array_t *certs) { uint32_t i; - nxt_cert_item_t *items; + nxt_cert_item_t *items; - for (items = certs->elts, i = 0; - i < certs->nelts; - i++) - { + for (items = certs->elts, i = 0; i < certs->nelts; i++) { nxt_fd_close(items[i].fd); } @@ -1060,13 +1012,12 @@ nxt_cert_store_discovery_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) void nxt_cert_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, - nxt_port_rpc_handler_t handler, void *ctx) -{ + nxt_port_rpc_handler_t handler, void *ctx) { uint32_t stream; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *main_port, *recv_port; - nxt_runtime_t *rt; + nxt_buf_t *b; + nxt_port_t *main_port, *recv_port; + nxt_runtime_t *rt; b = nxt_buf_mem_alloc(mp, name->length + 1, 0); if (nxt_slow_path(b == NULL)) { @@ -1079,18 +1030,18 @@ nxt_cert_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, nxt_buf_cpystr(b, name); *b->mem.free++ = '\0'; - rt = task->thread->runtime; + rt = task->thread->runtime; main_port = rt->port_by_type[NXT_PROCESS_MAIN]; recv_port = rt->port_by_type[rt->type]; stream = nxt_port_rpc_register_handler(task, recv_port, handler, handler, - -1, ctx); + -1, ctx); if (nxt_slow_path(stream == 0)) { goto fail; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_CERT_GET, -1, - stream, recv_port->id, b); + stream, recv_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, recv_port, stream); @@ -1104,14 +1055,12 @@ nxt_cert_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, handler(task, NULL, ctx); } - static void -nxt_cert_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b; +nxt_cert_buf_completion(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b; - b = obj; + b = obj; mp = b->data; nxt_assert(b->next == NULL); @@ -1119,39 +1068,36 @@ nxt_cert_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_mp_release(mp); } - void -nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; - nxt_int_t ret; - nxt_str_t name; - nxt_file_t file; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_port_msg_type_t type; +nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; + nxt_int_t ret; + nxt_str_t name; + nxt_file_t file; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_port_msg_type_t type; port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "process port not found (pid %PI, reply_port %d)", - msg->port_msg.pid, msg->port_msg.reply_port); + msg->port_msg.pid, msg->port_msg.reply_port); return; } if (nxt_slow_path(port->type != NXT_PROCESS_CONTROLLER - && port->type != NXT_PROCESS_ROUTER)) - { + && port->type != NXT_PROCESS_ROUTER)) { nxt_alert(task, "process %PI cannot store certificates", - msg->port_msg.pid); + msg->port_msg.pid); return; } nxt_memzero(&file, sizeof(nxt_file_t)); file.fd = -1; - type = NXT_PORT_MSG_RPC_ERROR; + type = NXT_PORT_MSG_RPC_ERROR; rt = task->thread->runtime; @@ -1160,7 +1106,7 @@ nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto error; } - name.start = msg->buf->mem.pos; + name.start = msg->buf->mem.pos; name.length = nxt_strlen(name.start); file.name = nxt_malloc(rt->certs.length + name.length + 1); @@ -1173,7 +1119,7 @@ nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) p = nxt_cpymem(p, name.start, name.length + 1); ret = nxt_file_open(task, &file, NXT_FILE_RDWR, NXT_FILE_CREATE_OR_OPEN, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); nxt_free(file.name); @@ -1184,16 +1130,14 @@ nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) error: (void) nxt_port_socket_write(task, port, type, file.fd, - msg->port_msg.stream, 0, NULL); + msg->port_msg.stream, 0, NULL); } - void -nxt_cert_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) -{ - nxt_buf_t *b; - nxt_port_t *main_port; - nxt_runtime_t *rt; +nxt_cert_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) { + nxt_buf_t *b; + nxt_port_t *main_port; + nxt_runtime_t *rt; b = nxt_buf_mem_alloc(mp, name->length + 1, 0); @@ -1201,25 +1145,23 @@ nxt_cert_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) nxt_buf_cpystr(b, name); *b->mem.free++ = '\0'; - rt = task->thread->runtime; + rt = task->thread->runtime; main_port = rt->port_by_type[NXT_PROCESS_MAIN]; (void) nxt_port_socket_write(task, main_port, NXT_PORT_MSG_CERT_DELETE, - -1, 0, 0, b); + -1, 0, 0, b); } } - void -nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; +nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; nxt_str_t name; - nxt_port_t *ctl_port; - nxt_runtime_t *rt; - nxt_file_name_t *path; + nxt_port_t *ctl_port; + nxt_runtime_t *rt; + nxt_file_name_t *path; - rt = task->thread->runtime; + rt = task->thread->runtime; ctl_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; if (nxt_slow_path(ctl_port == NULL)) { @@ -1229,7 +1171,7 @@ nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_slow_path(nxt_recv_msg_cmsg_pid(msg) != ctl_port->pid)) { nxt_alert(task, "process %PI cannot delete certificates", - nxt_recv_msg_cmsg_pid(msg)); + nxt_recv_msg_cmsg_pid(msg)); return; } @@ -1238,7 +1180,7 @@ nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) return; } - name.start = msg->buf->mem.pos; + name.start = msg->buf->mem.pos; name.length = nxt_strlen(name.start); path = nxt_malloc(rt->certs.length + name.length + 1); diff --git a/src/nxt_cert.h b/src/nxt_cert.h index dbaddcf93..36bb10d0a 100644 --- a/src/nxt_cert.h +++ b/src/nxt_cert.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Valentin V. Bartenev * Copyright (C) NGINX, Inc. @@ -8,25 +7,38 @@ #define _NXT_CERT_INCLUDED_ -typedef struct nxt_cert_s nxt_cert_t; +typedef struct nxt_cert_s nxt_cert_t; -nxt_cert_t *nxt_cert_mem(nxt_task_t *task, nxt_buf_mem_t *mbuf); -void nxt_cert_destroy(nxt_cert_t *cert); +nxt_cert_t * +nxt_cert_mem(nxt_task_t *task, nxt_buf_mem_t *mbuf); +void +nxt_cert_destroy(nxt_cert_t *cert); -void nxt_cert_info_init(nxt_task_t *task, nxt_array_t *certs); -nxt_int_t nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert); -nxt_conf_value_t *nxt_cert_info_get(nxt_str_t *name); -nxt_conf_value_t *nxt_cert_info_get_all(nxt_mp_t *mp); -nxt_int_t nxt_cert_info_delete(nxt_str_t *name); +void +nxt_cert_info_init(nxt_task_t *task, nxt_array_t *certs); +nxt_int_t +nxt_cert_info_save(nxt_str_t *name, nxt_cert_t *cert); +nxt_conf_value_t * +nxt_cert_info_get(nxt_str_t *name); +nxt_conf_value_t * +nxt_cert_info_get_all(nxt_mp_t *mp); +nxt_int_t +nxt_cert_info_delete(nxt_str_t *name); -nxt_array_t *nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mem_pool); -void nxt_cert_store_release(nxt_array_t *certs); +nxt_array_t * +nxt_cert_store_load(nxt_task_t *task, nxt_mp_t *mem_pool); +void +nxt_cert_store_release(nxt_array_t *certs); -void nxt_cert_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, +void +nxt_cert_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, nxt_port_rpc_handler_t handler, void *ctx); -void nxt_cert_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp); +void +nxt_cert_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp); -void nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_cert_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_cert_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); #endif /* _NXT_CERT_INCLUDED_ */ diff --git a/src/nxt_cgroup.c b/src/nxt_cgroup.c index 79e240f1e..759898cad 100644 --- a/src/nxt_cgroup.c +++ b/src/nxt_cgroup.c @@ -8,24 +8,21 @@ #include -static int nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, - char *cgpath); -static nxt_int_t nxt_mk_cgpath(nxt_task_t *task, const char *dir, - char *cgpath); - +static int +nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath); +static nxt_int_t +nxt_mk_cgpath(nxt_task_t *task, const char *dir, char *cgpath); nxt_int_t -nxt_cgroup_proc_add(nxt_task_t *task, nxt_process_t *process) -{ - int len; - char cgprocs[NXT_MAX_PATH_LEN]; - FILE *fp; - nxt_int_t ret; +nxt_cgroup_proc_add(nxt_task_t *task, nxt_process_t *process) { + int len; + char cgprocs[NXT_MAX_PATH_LEN]; + FILE *fp; + nxt_int_t ret; if (task->thread->runtime->type != NXT_PROCESS_MAIN || nxt_process_type(process) != NXT_PROCESS_PROTOTYPE - || process->isolation.cgroup.path == NULL) - { + || process->isolation.cgroup.path == NULL) { return NXT_OK; } @@ -63,13 +60,11 @@ nxt_cgroup_proc_add(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - void -nxt_cgroup_cleanup(nxt_task_t *task, const nxt_process_t *process) -{ - char *ptr; - char cgroot[NXT_MAX_PATH_LEN], cgpath[NXT_MAX_PATH_LEN]; - nxt_int_t ret; +nxt_cgroup_cleanup(nxt_task_t *task, const nxt_process_t *process) { + char *ptr; + char cgroot[NXT_MAX_PATH_LEN], cgpath[NXT_MAX_PATH_LEN]; + nxt_int_t ret; ret = nxt_mk_cgpath(task, "", cgroot); if (nxt_slow_path(ret == NXT_ERROR)) { @@ -83,29 +78,27 @@ nxt_cgroup_cleanup(nxt_task_t *task, const nxt_process_t *process) while (*cgpath != '\0' && strcmp(cgroot, cgpath) != 0) { rmdir(cgpath); - ptr = strrchr(cgpath, '/'); + ptr = strrchr(cgpath, '/'); *ptr = '\0'; } } - static int -nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath) -{ - int i, len; - char *buf, *ptr; - FILE *fp; - size_t size; - ssize_t nread; - nxt_bool_t found; +nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath) { + int i, len; + char *buf, *ptr; + FILE *fp; + size_t size; + ssize_t nread; + nxt_bool_t found; fp = nxt_file_fopen(task, "/proc/self/cgroup", "re"); if (nxt_slow_path(fp == NULL)) { return -1; } - len = -1; - buf = NULL; + len = -1; + buf = NULL; found = 0; while ((nread = getline(&buf, &size, fp)) != -1) { if (strncmp(buf, "0::", 3) == 0) { @@ -121,8 +114,8 @@ nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath) goto out_free_buf; } - buf[nread - 1] = '\0'; /* lose the trailing '\n' */ - ptr = buf; + buf[nread - 1] = '\0'; /* lose the trailing '\n' */ + ptr = buf; for (i = 0; i < 2; i++) { ptr = strchr(ptr, ':'); if (ptr == NULL) { @@ -133,8 +126,7 @@ nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath) ptr++; } - len = snprintf(cgpath, NXT_MAX_PATH_LEN, NXT_CGROUP_ROOT "%s/%s", - ptr, dir); + len = snprintf(cgpath, NXT_MAX_PATH_LEN, NXT_CGROUP_ROOT "%s/%s", ptr, dir); out_free_buf: @@ -143,11 +135,9 @@ nxt_mk_cgpath_relative(nxt_task_t *task, const char *dir, char *cgpath) return len; } - static nxt_int_t -nxt_mk_cgpath(nxt_task_t *task, const char *dir, char *cgpath) -{ - int len; +nxt_mk_cgpath(nxt_task_t *task, const char *dir, char *cgpath) { + int len; /* * If the path from the config is relative, we need to make diff --git a/src/nxt_cgroup.h b/src/nxt_cgroup.h index 0b9055d29..2ca3f4b99 100644 --- a/src/nxt_cgroup.h +++ b/src/nxt_cgroup.h @@ -7,8 +7,10 @@ #define _NXT_CGROUP_H_INCLUDED_ -nxt_int_t nxt_cgroup_proc_add(nxt_task_t *task, nxt_process_t *process); -void nxt_cgroup_cleanup(nxt_task_t *task, const nxt_process_t *process); +nxt_int_t +nxt_cgroup_proc_add(nxt_task_t *task, nxt_process_t *process); +void +nxt_cgroup_cleanup(nxt_task_t *task, const nxt_process_t *process); #endif /* _NXT_CGROUP_H_INCLUDED_ */ diff --git a/src/nxt_clang.h b/src/nxt_clang.h index 94638346e..1e2fa495c 100644 --- a/src/nxt_clang.h +++ b/src/nxt_clang.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,16 +7,15 @@ #define _NXT_CLANG_H_INCLUDED_ -#define nxt_inline static inline __attribute__((always_inline)) -#define nxt_noinline __attribute__((noinline)) +#define nxt_inline static inline __attribute__((always_inline)) +#define nxt_noinline __attribute__((noinline)) #define nxt_cdecl #if (NXT_CLANG) /* Any __asm__ directive disables loop vectorization in GCC and Clang. */ -#define nxt_pragma_loop_disable_vectorization \ - __asm__("") +#define nxt_pragma_loop_disable_vectorization __asm__("") #else @@ -28,34 +26,27 @@ #if (NXT_HAVE_BUILTIN_EXPECT) -#define nxt_expect(c, x) \ - __builtin_expect((long) (x), (c)) +#define nxt_expect(c, x) __builtin_expect((long) (x), (c)) -#define nxt_fast_path(x) \ - nxt_expect(1, x) +#define nxt_fast_path(x) nxt_expect(1, x) -#define nxt_slow_path(x) \ - nxt_expect(0, x) +#define nxt_slow_path(x) nxt_expect(0, x) #else -#define nxt_expect(c, x) \ - (x) +#define nxt_expect(c, x) (x) -#define nxt_fast_path(x) \ - (x) +#define nxt_fast_path(x) (x) -#define nxt_slow_path(x) \ - (x) +#define nxt_slow_path(x) (x) #endif #if (NXT_HAVE_BUILTIN_UNREACHABLE) -#define nxt_unreachable() \ - __builtin_unreachable() +#define nxt_unreachable() __builtin_unreachable() #else @@ -66,8 +57,7 @@ #if (NXT_HAVE_BUILTIN_PREFETCH) -#define nxt_prefetch(a) \ - __builtin_prefetch(a) +#define nxt_prefetch(a) __builtin_prefetch(a) #else @@ -78,7 +68,7 @@ #if (NXT_HAVE_GCC_ATTRIBUTE_VISIBILITY) -#define NXT_EXPORT __attribute__((visibility("default"))) +#define NXT_EXPORT __attribute__((visibility("default"))) #else @@ -89,7 +79,7 @@ #if (NXT_HAVE_GCC_ATTRIBUTE_MALLOC) -#define NXT_MALLOC_LIKE __attribute__((__malloc__)) +#define NXT_MALLOC_LIKE __attribute__((__malloc__)) #else @@ -100,7 +90,7 @@ #if (NXT_HAVE_GCC_ATTRIBUTE_ALIGNED) -#define nxt_aligned(x) __attribute__((aligned(x))) +#define nxt_aligned(x) __attribute__((aligned(x))) #else @@ -111,7 +101,7 @@ #if (NXT_HAVE_GCC_ATTRIBUTE_PACKED) -#define nxt_packed __attribute__((__packed__)) +#define nxt_packed __attribute__((__packed__)) #else @@ -122,7 +112,7 @@ #if (NXT_HAVE_GCC_ATTRIBUTE_UNUSED) -#define NXT_MAYBE_UNUSED __attribute__((__unused__)) +#define NXT_MAYBE_UNUSED __attribute__((__unused__)) #else @@ -133,14 +123,13 @@ #if (NXT_HAVE_BUILTIN_POPCOUNT) -#define nxt_popcount __builtin_popcount +#define nxt_popcount __builtin_popcount #else nxt_inline int -nxt_popcount(unsigned int x) -{ - int count; +nxt_popcount(unsigned int x) { + int count; for (count = 0; x != 0; count++) { x &= x - 1; @@ -155,19 +144,19 @@ nxt_popcount(unsigned int x) #ifndef NXT_ALIGNMENT #if (NXT_SOLARIS) -#define NXT_ALIGNMENT _POINTER_ALIGNMENT /* x86_64: 8, i386: 4 */ - /* sparcv9: 8, sparcv8: 4 */ +#define NXT_ALIGNMENT _POINTER_ALIGNMENT /* x86_64: 8, i386: 4 */ + /* sparcv9: 8, sparcv8: 4 */ #elif (__i386__ || __i386) -#define NXT_ALIGNMENT 4 +#define NXT_ALIGNMENT 4 #elif (__arm__) -#define NXT_ALIGNMENT 8 /* 32-bit ARM may use 64-bit load/store */ +#define NXT_ALIGNMENT 8 /* 32-bit ARM may use 64-bit load/store */ #elif (__ia64__) -#define NXT_ALIGNMENT 8 /* long long */ +#define NXT_ALIGNMENT 8 /* long long */ #else -#define NXT_ALIGNMENT NXT_PTR_SIZE +#define NXT_ALIGNMENT NXT_PTR_SIZE #endif #endif @@ -176,83 +165,71 @@ nxt_popcount(unsigned int x) #ifndef NXT_MAX_ALIGNMENT #if (NXT_SOLARIS) -#define NXT_MAX_ALIGNMENT _MAX_ALIGNMENT /* x86_64: 16, i386: 4 */ - /* sparcv9: 16, sparcv8: 8 */ +#define NXT_MAX_ALIGNMENT _MAX_ALIGNMENT /* x86_64: 16, i386: 4 */ + /* sparcv9: 16, sparcv8: 8 */ #elif (__i386__ || __i386) -#define NXT_MAX_ALIGNMENT 4 +#define NXT_MAX_ALIGNMENT 4 #elif (__arm__) -#define NXT_MAX_ALIGNMENT 16 +#define NXT_MAX_ALIGNMENT 16 #elif (__ia64__) -#define NXT_MAX_ALIGNMENT 16 +#define NXT_MAX_ALIGNMENT 16 #else -#define NXT_MAX_ALIGNMENT 16 +#define NXT_MAX_ALIGNMENT 16 #endif #endif -#define nxt_alloca(size) \ - alloca(size) +#define nxt_alloca(size) alloca(size) -#define nxt_container_of(p, type, field) \ +#define nxt_container_of(p, type, field) \ (type *) ((u_char *) (p) - offsetof(type, field)) -#define nxt_pointer_to(p, offset) \ - ((void *) ((char *) (p) + (offset))) +#define nxt_pointer_to(p, offset) ((void *) ((char *) (p) + (offset))) -#define nxt_value_at(type, p, offset) \ - *(type *) ((u_char *) p + offset) +#define nxt_value_at(type, p, offset) *(type *) ((u_char *) p + offset) -#define nxt_nitems(x) \ - (sizeof(x) / sizeof((x)[0])) +#define nxt_nitems(x) (sizeof(x) / sizeof((x)[0])) /* GCC and Clang use __builtin_abs() instead of libc abs(). */ -#define nxt_abs(val) \ - abs(val) +#define nxt_abs(val) abs(val) -#define nxt_max(val1, val2) \ - ((val1 < val2) ? (val2) : (val1)) +#define nxt_max(val1, val2) ((val1 < val2) ? (val2) : (val1)) -#define nxt_min(val1, val2) \ - ((val1 > val2) ? (val2) : (val1)) +#define nxt_min(val1, val2) ((val1 > val2) ? (val2) : (val1)) -#define nxt_bswap32(val) \ - ( ((val) >> 24) \ - | (((val) & 0x00FF0000) >> 8) \ - | (((val) & 0x0000FF00) << 8) \ - | ((val) << 24)) +#define nxt_bswap32(val) \ + (((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) \ + | ((val) << 24)) -#define nxt_is_power_of_two(value) \ - ((((value) - 1) & (value)) == 0) +#define nxt_is_power_of_two(value) ((((value) - 1) & (value)) == 0) -#define nxt_align_size(d, a) \ - (((d) + ((size_t) (a) - 1)) & ~((size_t) (a) - 1)) +#define nxt_align_size(d, a) (((d) + ((size_t) (a) - 1)) & ~((size_t) (a) - 1)) -#define nxt_align_ptr(p, a) \ - (u_char *) (((uintptr_t) (p) + ((uintptr_t) (a) - 1)) \ - & ~((uintptr_t) (a) - 1)) +#define nxt_align_ptr(p, a) \ + (u_char *) (((uintptr_t) (p) + ((uintptr_t) (a) - 1)) \ + & ~((uintptr_t) (a) - 1)) -#define nxt_trunc_ptr(p, a) \ +#define nxt_trunc_ptr(p, a) \ (u_char *) ((uintptr_t) (p) & ~((uintptr_t) (a) - 1)) -#define nxt_length(s) \ - (sizeof(s) - 1) +#define nxt_length(s) (sizeof(s) - 1) #endif /* _NXT_CLANG_H_INCLUDED_ */ diff --git a/src/nxt_clone.c b/src/nxt_clone.c index e78a7822a..451589562 100644 --- a/src/nxt_clone.c +++ b/src/nxt_clone.c @@ -11,33 +11,34 @@ #if (NXT_HAVE_CLONE_NEWUSER) -nxt_int_t nxt_clone_credential_setgroups(nxt_task_t *task, pid_t child_pid, +nxt_int_t +nxt_clone_credential_setgroups(nxt_task_t *task, pid_t child_pid, const char *str); -nxt_int_t nxt_clone_credential_map_set(nxt_task_t *task, const char* mapfile, - pid_t pid, nxt_int_t default_container, nxt_int_t default_host, +nxt_int_t +nxt_clone_credential_map_set(nxt_task_t *task, const char *mapfile, pid_t pid, + nxt_int_t default_container, nxt_int_t default_host, nxt_clone_credential_map_t *map); -nxt_int_t nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, - pid_t pid, u_char *mapinfo); - +nxt_int_t +nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, pid_t pid, + u_char *mapinfo); nxt_int_t nxt_clone_credential_setgroups(nxt_task_t *task, pid_t child_pid, - const char *str) -{ + const char *str) { int fd, n; - u_char *p, *end; + u_char *p, *end; u_char path[PATH_MAX]; end = path + PATH_MAX; - p = nxt_sprintf(path, end, "/proc/%d/setgroups", child_pid); - *p = '\0'; + p = nxt_sprintf(path, end, "/proc/%d/setgroups", child_pid); + *p = '\0'; if (nxt_slow_path(p == end)) { nxt_alert(task, "error write past the buffer: %s", path); return NXT_ERROR; } - fd = open((char *)path, O_RDWR); + fd = open((char *) path, O_RDWR); if (fd == -1) { /* @@ -65,15 +66,13 @@ nxt_clone_credential_setgroups(nxt_task_t *task, pid_t child_pid, return NXT_OK; } - nxt_int_t -nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, - pid_t pid, u_char *mapinfo) -{ - int len, mapfd; - u_char *p, *end; - ssize_t n; - u_char buf[256]; +nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, pid_t pid, + u_char *mapinfo) { + int len, mapfd; + u_char *p, *end; + ssize_t n; + u_char buf[256]; end = buf + sizeof(buf); @@ -85,7 +84,7 @@ nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, *p = '\0'; - mapfd = open((char*)buf, O_RDWR); + mapfd = open((char *) buf, O_RDWR); if (nxt_slow_path(mapfd == -1)) { nxt_alert(task, "failed to open proc map (%s) %E", buf, nxt_errno); return NXT_ERROR; @@ -93,16 +92,16 @@ nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, len = nxt_strlen(mapinfo); - n = write(mapfd, (char *)mapinfo, len); + n = write(mapfd, (char *) mapinfo, len); if (nxt_slow_path(n != len)) { - if (n == -1 && nxt_errno == EINVAL) { - nxt_alert(task, "failed to write %s: Check kernel maximum " \ - "allowed lines %E", buf, nxt_errno); + nxt_alert(task, + "failed to write %s: Check kernel maximum " + "allowed lines %E", + buf, nxt_errno); } else { - nxt_alert(task, "failed to write proc map (%s) %E", buf, - nxt_errno); + nxt_alert(task, "failed to write proc map (%s) %E", buf, nxt_errno); } close(mapfd); @@ -115,15 +114,13 @@ nxt_clone_credential_map_write(nxt_task_t *task, const char *mapfile, return NXT_OK; } - nxt_int_t -nxt_clone_credential_map_set(nxt_task_t *task, const char* mapfile, pid_t pid, +nxt_clone_credential_map_set(nxt_task_t *task, const char *mapfile, pid_t pid, nxt_int_t default_container, nxt_int_t default_host, - nxt_clone_credential_map_t *map) -{ - u_char *p, *end, *mapinfo; - nxt_int_t ret, len; - nxt_uint_t i; + nxt_clone_credential_map_t *map) { + u_char *p, *end, *mapinfo; + nxt_int_t ret, len; + nxt_uint_t i; /* * uid_map one-entry size: @@ -139,12 +136,12 @@ nxt_clone_credential_map_set(nxt_task_t *task, const char* mapfile, pid_t pid, return NXT_ERROR; } - p = mapinfo; + p = mapinfo; end = mapinfo + len; for (i = 0; i < map->size; i++) { p = nxt_sprintf(p, end, "%L %L %L", map->map[i].container, - map->map[i].host, map->map[i].size); + map->map[i].host, map->map[i].size); if (nxt_slow_path(p == end)) { nxt_alert(task, "write past the mapinfo buffer"); @@ -167,9 +164,9 @@ nxt_clone_credential_map_set(nxt_task_t *task, const char* mapfile, pid_t pid, } end = mapinfo + len; - p = nxt_sprintf(mapinfo, end, "%d %d 1", - default_container, default_host); - *p = '\0'; + p = nxt_sprintf(mapinfo, end, "%d %d 1", default_container, + default_host); + *p = '\0'; if (nxt_slow_path(p == end)) { nxt_alert(task, "write past mapinfo buffer"); @@ -185,18 +182,16 @@ nxt_clone_credential_map_set(nxt_task_t *task, const char* mapfile, pid_t pid, return ret; } - nxt_int_t nxt_clone_credential_map(nxt_task_t *task, pid_t pid, - nxt_credential_t *app_creds, nxt_clone_t *clone) -{ + nxt_credential_t *app_creds, nxt_clone_t *clone) { nxt_int_t ret; nxt_int_t default_host_uid; nxt_int_t default_host_gid; - const char *rule; - nxt_runtime_t *rt; + const char *rule; + nxt_runtime_t *rt; - rt = task->thread->runtime; + rt = task->thread->runtime; if (rt->capabilities.setid) { rule = "allow"; @@ -214,8 +209,7 @@ nxt_clone_credential_map(nxt_task_t *task, pid_t pid, } ret = nxt_clone_credential_map_set(task, "uid_map", pid, app_creds->uid, - default_host_uid, - &clone->uidmap); + default_host_uid, &clone->uidmap); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -227,9 +221,8 @@ nxt_clone_credential_map(nxt_task_t *task, pid_t pid, return NXT_ERROR; } - ret = nxt_clone_credential_map_set(task, "gid_map", pid, app_creds->base_gid, - default_host_gid, - &clone->gidmap); + ret = nxt_clone_credential_map_set(task, "gid_map", pid, + app_creds->base_gid, default_host_gid, &clone->gidmap); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -238,15 +231,13 @@ nxt_clone_credential_map(nxt_task_t *task, pid_t pid, return NXT_OK; } - nxt_int_t nxt_clone_vldt_credential_uidmap(nxt_task_t *task, - nxt_clone_credential_map_t *map, nxt_credential_t *creds) -{ - nxt_int_t id; - nxt_uint_t i; - nxt_runtime_t *rt; - nxt_clone_map_entry_t m; + nxt_clone_credential_map_t *map, nxt_credential_t *creds) { + nxt_int_t id; + nxt_uint_t i; + nxt_runtime_t *rt; + nxt_clone_map_entry_t m; if (map->size == 0) { return NXT_OK; @@ -256,9 +247,10 @@ nxt_clone_vldt_credential_uidmap(nxt_task_t *task, if (!rt->capabilities.setid) { if (nxt_slow_path(map->size > 1)) { - nxt_log(task, NXT_LOG_NOTICE, "\"uidmap\" field has %d entries " - "but unprivileged unit has a maximum of 1 map.", - map->size); + nxt_log(task, NXT_LOG_NOTICE, + "\"uidmap\" field has %d entries " + "but unprivileged unit has a maximum of 1 map.", + map->size); return NXT_ERROR; } @@ -266,9 +258,11 @@ nxt_clone_vldt_credential_uidmap(nxt_task_t *task, id = map->map[0].host; if (nxt_slow_path((nxt_uid_t) id != nxt_euid)) { - nxt_log(task, NXT_LOG_NOTICE, "\"uidmap\" field has an entry for " - "host uid %d but unprivileged unit can only map itself " - "(uid %d) into child namespaces.", id, nxt_euid); + nxt_log(task, NXT_LOG_NOTICE, + "\"uidmap\" field has an entry for " + "host uid %d but unprivileged unit can only map itself " + "(uid %d) into child namespaces.", + id, nxt_euid); return NXT_ERROR; } @@ -280,27 +274,26 @@ nxt_clone_vldt_credential_uidmap(nxt_task_t *task, m = map->map[i]; if (creds->uid >= (nxt_uid_t) m.container - && creds->uid < (nxt_uid_t) (m.container + m.size)) - { + && creds->uid < (nxt_uid_t) (m.container + m.size)) { return NXT_OK; } } - nxt_log(task, NXT_LOG_NOTICE, "\"uidmap\" field has no \"container\" " - "entry for user \"%s\" (uid %d)", creds->user, creds->uid); + nxt_log(task, NXT_LOG_NOTICE, + "\"uidmap\" field has no \"container\" " + "entry for user \"%s\" (uid %d)", + creds->user, creds->uid); return NXT_ERROR; } - nxt_int_t nxt_clone_vldt_credential_gidmap(nxt_task_t *task, - nxt_clone_credential_map_t *map, nxt_credential_t *creds) -{ - nxt_uint_t base_ok, gid_ok, gids_ok; - nxt_uint_t i, j; - nxt_runtime_t *rt; - nxt_clone_map_entry_t m; + nxt_clone_credential_map_t *map, nxt_credential_t *creds) { + nxt_uint_t base_ok, gid_ok, gids_ok; + nxt_uint_t i, j; + nxt_runtime_t *rt; + nxt_clone_map_entry_t m; rt = task->thread->runtime; @@ -308,10 +301,9 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, if (creds->ngroups > 0 && !(creds->ngroups == 1 && creds->gids[0] == creds->base_gid)) { nxt_log(task, NXT_LOG_NOTICE, - "unprivileged unit disallow supplementary groups for " - "new namespace (user \"%s\" has %d group%s).", - creds->user, creds->ngroups, - creds->ngroups > 1 ? "s" : ""); + "unprivileged unit disallow supplementary groups for " + "new namespace (user \"%s\" has %d group%s).", + creds->user, creds->ngroups, creds->ngroups > 1 ? "s" : ""); return NXT_ERROR; } @@ -321,9 +313,10 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, } if (nxt_slow_path(map->size > 1)) { - nxt_log(task, NXT_LOG_NOTICE, "\"gidmap\" field has %d entries " - "but unprivileged unit has a maximum of 1 map.", - map->size); + nxt_log(task, NXT_LOG_NOTICE, + "\"gidmap\" field has %d entries " + "but unprivileged unit has a maximum of 1 map.", + map->size); return NXT_ERROR; } @@ -331,25 +324,28 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, m = map->map[0]; if (nxt_slow_path((nxt_gid_t) m.host != nxt_egid)) { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" field has an entry for " - "host gid %L but unprivileged unit can only map itself " - "(gid %d) into child namespaces.", m.host, nxt_egid); + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" field has an entry for " + "host gid %L but unprivileged unit can only map itself " + "(gid %d) into child namespaces.", + m.host, nxt_egid); return NXT_ERROR; } if (nxt_slow_path(m.size > 1)) { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" field has an entry with " - "\"size\": %L, but for unprivileged unit it must be 1.", - m.size); + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" field has an entry with " + "\"size\": %L, but for unprivileged unit it must be 1.", + m.size); return NXT_ERROR; } if (nxt_slow_path((nxt_gid_t) m.container != creds->base_gid)) { nxt_log(task, NXT_LOG_ERR, - "\"gidmap\" field has no \"container\" entry for gid %d.", - creds->base_gid); + "\"gidmap\" field has no \"container\" entry for gid %d.", + creds->base_gid); return NXT_ERROR; } @@ -359,12 +355,11 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, if (map->size == 0) { if (creds->ngroups > 0 - && !(creds->ngroups == 1 && creds->gids[0] == creds->base_gid)) - { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" field has no entries " - "but user \"%s\" has %d suplementary group%s.", - creds->user, creds->ngroups, - creds->ngroups > 1 ? "s" : ""); + && !(creds->ngroups == 1 && creds->gids[0] == creds->base_gid)) { + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" field has no entries " + "but user \"%s\" has %d suplementary group%s.", + creds->user, creds->ngroups, creds->ngroups > 1 ? "s" : ""); return NXT_ERROR; } @@ -382,14 +377,12 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, m = map->map[j]; if (!base_ok && creds->base_gid >= (nxt_gid_t) m.container - && creds->base_gid < (nxt_gid_t) (m.container + m.size)) - { + && creds->base_gid < (nxt_gid_t) (m.container + m.size)) { base_ok = 1; } if (creds->gids[i] >= (nxt_gid_t) m.container - && creds->gids[i] < (nxt_gid_t) (m.container + m.size)) - { + && creds->gids[i] < (nxt_gid_t) (m.container + m.size)) { gid_ok = 1; break; } @@ -405,8 +398,7 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, m = map->map[i]; if (creds->base_gid >= (nxt_gid_t) m.container - && creds->base_gid < (nxt_gid_t) (m.container + m.size)) - { + && creds->base_gid < (nxt_gid_t) (m.container + m.size)) { base_ok = 1; break; } @@ -414,16 +406,19 @@ nxt_clone_vldt_credential_gidmap(nxt_task_t *task, } if (nxt_slow_path(!base_ok)) { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" field has no \"container\" " - "entry for gid %d.", creds->base_gid); + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" field has no \"container\" " + "entry for gid %d.", + creds->base_gid); return NXT_ERROR; } if (nxt_slow_path(gids_ok < creds->ngroups)) { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" field has missing " - "suplementary gid mappings (found %d out of %d).", gids_ok, - creds->ngroups); + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" field has missing " + "suplementary gid mappings (found %d out of %d).", + gids_ok, creds->ngroups); return NXT_ERROR; } diff --git a/src/nxt_clone.h b/src/nxt_clone.h index bf28322ff..c48e967fe 100644 --- a/src/nxt_clone.h +++ b/src/nxt_clone.h @@ -9,43 +9,45 @@ #if (NXT_HAVE_CLONE_NEWUSER) -typedef int64_t nxt_cred_t; +typedef int64_t nxt_cred_t; typedef struct { - nxt_cred_t container; - nxt_cred_t host; - nxt_cred_t size; + nxt_cred_t container; + nxt_cred_t host; + nxt_cred_t size; } nxt_clone_map_entry_t; typedef struct { - nxt_uint_t size; - nxt_clone_map_entry_t *map; + nxt_uint_t size; + nxt_clone_map_entry_t *map; } nxt_clone_credential_map_t; #endif typedef struct { - nxt_int_t flags; + nxt_int_t flags; #if (NXT_HAVE_CLONE_NEWUSER) - nxt_clone_credential_map_t uidmap; - nxt_clone_credential_map_t gidmap; + nxt_clone_credential_map_t uidmap; + nxt_clone_credential_map_t gidmap; #endif } nxt_clone_t; - -#define nxt_is_clone_flag_set(flags, test) \ +#define nxt_is_clone_flag_set(flags, test) \ ((flags & CLONE_##test) == CLONE_##test) #if (NXT_HAVE_CLONE_NEWUSER) -NXT_EXPORT nxt_int_t nxt_clone_credential_map(nxt_task_t *task, pid_t pid, - nxt_credential_t *creds, nxt_clone_t *clone); -NXT_EXPORT nxt_int_t nxt_clone_vldt_credential_uidmap(nxt_task_t *task, +NXT_EXPORT nxt_int_t +nxt_clone_credential_map(nxt_task_t *task, pid_t pid, nxt_credential_t *creds, + nxt_clone_t *clone); +NXT_EXPORT nxt_int_t +nxt_clone_vldt_credential_uidmap(nxt_task_t *task, nxt_clone_credential_map_t *map, nxt_credential_t *creds); -NXT_EXPORT nxt_int_t nxt_clone_vldt_credential_gidmap(nxt_task_t *task, +NXT_EXPORT nxt_int_t +nxt_clone_vldt_credential_gidmap(nxt_task_t *task, nxt_clone_credential_map_t *map, nxt_credential_t *creds); #endif diff --git a/src/nxt_conf.c b/src/nxt_conf.c index bb229c331..3e2526e23 100644 --- a/src/nxt_conf.c +++ b/src/nxt_conf.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -13,12 +12,11 @@ #include -#define NXT_CONF_MAX_SHORT_STRING 14 -#define NXT_CONF_MAX_NUMBER_LEN 14 -#define NXT_CONF_MAX_STRING NXT_INT32_T_MAX - -#define NXT_CONF_MAX_TOKEN_LEN 256 +#define NXT_CONF_MAX_SHORT_STRING 14 +#define NXT_CONF_MAX_NUMBER_LEN 14 +#define NXT_CONF_MAX_STRING NXT_INT32_T_MAX +#define NXT_CONF_MAX_TOKEN_LEN 256 typedef enum { NXT_CONF_VALUE_NULL = 0, @@ -31,7 +29,6 @@ typedef enum { NXT_CONF_VALUE_OBJECT, } nxt_conf_value_type_t; - typedef enum { NXT_CONF_OP_PASS = 0, NXT_CONF_OP_CREATE, @@ -40,119 +37,129 @@ typedef enum { } nxt_conf_op_action_t; -typedef struct nxt_conf_array_s nxt_conf_array_t; -typedef struct nxt_conf_object_s nxt_conf_object_t; - +typedef struct nxt_conf_array_s nxt_conf_array_t; +typedef struct nxt_conf_object_s nxt_conf_object_t; struct nxt_conf_value_s { union { - uint8_t boolean; /* 1 bit. */ - u_char number[NXT_CONF_MAX_NUMBER_LEN + 1]; + uint8_t boolean; /* 1 bit. */ + u_char number[NXT_CONF_MAX_NUMBER_LEN + 1]; struct { - u_char start[NXT_CONF_MAX_SHORT_STRING]; - uint8_t length; + u_char start[NXT_CONF_MAX_SHORT_STRING]; + uint8_t length; } str; struct { - u_char *start; - uint32_t length; + u_char *start; + uint32_t length; } nxt_packed string; - nxt_conf_array_t *array; - nxt_conf_object_t *object; + nxt_conf_array_t *array; + nxt_conf_object_t *object; } nxt_packed u; - uint8_t type; /* 3 bits. */ + uint8_t type; /* 3 bits. */ } nxt_aligned(8); - struct nxt_conf_array_s { - nxt_uint_t count; - nxt_conf_value_t elements[]; + nxt_uint_t count; + nxt_conf_value_t elements[]; }; - typedef struct { - nxt_conf_value_t name; - nxt_conf_value_t value; + nxt_conf_value_t name; + nxt_conf_value_t value; } nxt_conf_object_member_t; - struct nxt_conf_object_s { - nxt_uint_t count; - nxt_conf_object_member_t members[]; + nxt_uint_t count; + nxt_conf_object_member_t members[]; }; - struct nxt_conf_op_s { - uint32_t index; - uint32_t action; /* nxt_conf_op_action_t */ - void *ctx; + uint32_t index; + uint32_t action; /* nxt_conf_op_action_t */ + void *ctx; }; - typedef struct { - u_char *start; - u_char *end; - nxt_bool_t last; - u_char buf[NXT_CONF_MAX_TOKEN_LEN]; + u_char *start; + u_char *end; + nxt_bool_t last; + u_char buf[NXT_CONF_MAX_TOKEN_LEN]; } nxt_conf_path_parse_t; +static nxt_int_t +nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, nxt_str_t *token); -static nxt_int_t nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, - nxt_str_t *token); - -static u_char *nxt_conf_json_skip_space(u_char *start, const u_char *end); -static u_char *nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, - u_char *start, u_char *end, nxt_conf_json_error_t *error); -static u_char *nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, - u_char *start, u_char *end, nxt_conf_json_error_t *error); -static nxt_int_t nxt_conf_object_hash_add(nxt_mp_t *mp, - nxt_lvlhsh_t *lvlhsh, nxt_conf_object_member_t *member); -static nxt_int_t nxt_conf_object_hash_test(nxt_lvlhsh_query_t *lhq, - void *data); -static void *nxt_conf_object_hash_alloc(void *data, size_t size); -static void nxt_conf_object_hash_free(void *data, void *p); -static u_char *nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, - u_char *start, u_char *end, nxt_conf_json_error_t *error); -static u_char *nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, - u_char *start, u_char *end, nxt_conf_json_error_t *error); -static u_char *nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, - u_char *start, u_char *end, nxt_conf_json_error_t *error); -static void nxt_conf_json_parse_error(nxt_conf_json_error_t *error, u_char *pos, +static u_char * +nxt_conf_json_skip_space(u_char *start, const u_char *end); +static u_char * +nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, + u_char *end, nxt_conf_json_error_t *error); +static u_char * +nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, + u_char *end, nxt_conf_json_error_t *error); +static nxt_int_t +nxt_conf_object_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *lvlhsh, + nxt_conf_object_member_t *member); +static nxt_int_t +nxt_conf_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static void * +nxt_conf_object_hash_alloc(void *data, size_t size); +static void +nxt_conf_object_hash_free(void *data, void *p); +static u_char * +nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, + u_char *end, nxt_conf_json_error_t *error); +static u_char * +nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, + u_char *end, nxt_conf_json_error_t *error); +static u_char * +nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, + u_char *end, nxt_conf_json_error_t *error); +static void +nxt_conf_json_parse_error(nxt_conf_json_error_t *error, u_char *pos, const char *detail); -static nxt_int_t nxt_conf_copy_value(nxt_mp_t *mp, const nxt_conf_op_t *op, +static nxt_int_t +nxt_conf_copy_value(nxt_mp_t *mp, const nxt_conf_op_t *op, nxt_conf_value_t *dst, const nxt_conf_value_t *src); -static nxt_int_t nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, +static nxt_int_t +nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, nxt_conf_value_t *dst, const nxt_conf_value_t *src); -static nxt_int_t nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, +static nxt_int_t +nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, nxt_conf_value_t *dst, const nxt_conf_value_t *src); -static size_t nxt_conf_json_string_length(const nxt_conf_value_t *value); -static u_char *nxt_conf_json_print_string(u_char *p, - const nxt_conf_value_t *value); -static size_t nxt_conf_json_array_length(const nxt_conf_value_t *value, +static size_t +nxt_conf_json_string_length(const nxt_conf_value_t *value); +static u_char * +nxt_conf_json_print_string(u_char *p, const nxt_conf_value_t *value); +static size_t +nxt_conf_json_array_length(const nxt_conf_value_t *value, + nxt_conf_json_pretty_t *pretty); +static u_char * +nxt_conf_json_print_array(u_char *p, const nxt_conf_value_t *value, nxt_conf_json_pretty_t *pretty); -static u_char *nxt_conf_json_print_array(u_char *p, - const nxt_conf_value_t *value, nxt_conf_json_pretty_t *pretty); -static size_t nxt_conf_json_object_length(const nxt_conf_value_t *value, +static size_t +nxt_conf_json_object_length(const nxt_conf_value_t *value, + nxt_conf_json_pretty_t *pretty); +static u_char * +nxt_conf_json_print_object(u_char *p, const nxt_conf_value_t *value, nxt_conf_json_pretty_t *pretty); -static u_char *nxt_conf_json_print_object(u_char *p, - const nxt_conf_value_t *value, nxt_conf_json_pretty_t *pretty); - -static size_t nxt_conf_json_escape_length(u_char *p, size_t size); -static u_char *nxt_conf_json_escape(u_char *dst, u_char *src, size_t size); +static size_t +nxt_conf_json_escape_length(u_char *p, size_t size); +static u_char * +nxt_conf_json_escape(u_char *dst, u_char *src, size_t size); -#define nxt_conf_json_newline(p) \ - ((p)[0] = '\r', (p)[1] = '\n', (p) + 2) +#define nxt_conf_json_newline(p) ((p)[0] = '\r', (p)[1] = '\n', (p) + 2) nxt_inline u_char * -nxt_conf_json_indentation(u_char *p, uint32_t level) -{ +nxt_conf_json_indentation(u_char *p, uint32_t level) { while (level) { *p++ = '\t'; level--; @@ -161,54 +168,46 @@ nxt_conf_json_indentation(u_char *p, uint32_t level) return p; } - void -nxt_conf_get_string(const nxt_conf_value_t *value, nxt_str_t *str) -{ +nxt_conf_get_string(const nxt_conf_value_t *value, nxt_str_t *str) { if (value->type == NXT_CONF_VALUE_SHORT_STRING) { str->length = value->u.str.length; - str->start = (u_char *) value->u.str.start; + str->start = (u_char *) value->u.str.start; } else { str->length = value->u.string.length; - str->start = (u_char *) value->u.string.start; + str->start = (u_char *) value->u.string.start; } } - nxt_str_t * nxt_conf_get_string_dup(const nxt_conf_value_t *value, nxt_mp_t *mp, - nxt_str_t *str) -{ - nxt_str_t s; + nxt_str_t *str) { + nxt_str_t s; nxt_conf_get_string(value, &s); return nxt_str_dup(mp, str, &s); } - void -nxt_conf_set_string(nxt_conf_value_t *value, const nxt_str_t *str) -{ +nxt_conf_set_string(nxt_conf_value_t *value, const nxt_str_t *str) { if (str->length > NXT_CONF_MAX_SHORT_STRING) { - value->type = NXT_CONF_VALUE_STRING; + value->type = NXT_CONF_VALUE_STRING; value->u.string.length = str->length; - value->u.string.start = str->start; + value->u.string.start = str->start; } else { - value->type = NXT_CONF_VALUE_SHORT_STRING; + value->type = NXT_CONF_VALUE_SHORT_STRING; value->u.str.length = str->length; nxt_memcpy(value->u.str.start, str->start, str->length); } } - nxt_int_t nxt_conf_set_string_dup(nxt_conf_value_t *value, nxt_mp_t *mp, - const nxt_str_t *str) -{ - nxt_str_t tmp, *ptr; + const nxt_str_t *str) { + nxt_str_t tmp, *ptr; if (str->length > NXT_CONF_MAX_SHORT_STRING) { value->type = NXT_CONF_VALUE_STRING; @@ -219,10 +218,10 @@ nxt_conf_set_string_dup(nxt_conf_value_t *value, nxt_mp_t *mp, } value->u.string.length = tmp.length; - value->u.string.start = tmp.start; + value->u.string.start = tmp.start; } else { - value->type = NXT_CONF_VALUE_SHORT_STRING; + value->type = NXT_CONF_VALUE_SHORT_STRING; value->u.str.length = str->length; nxt_memcpy(value->u.str.start, str->start, str->length); @@ -231,36 +230,27 @@ nxt_conf_set_string_dup(nxt_conf_value_t *value, nxt_mp_t *mp, return NXT_OK; } - double -nxt_conf_get_number(nxt_conf_value_t *value) -{ +nxt_conf_get_number(nxt_conf_value_t *value) { return nxt_strtod(value->u.number, NULL); } - uint8_t -nxt_conf_get_boolean(nxt_conf_value_t *value) -{ +nxt_conf_get_boolean(nxt_conf_value_t *value) { return value->u.boolean; } - nxt_uint_t -nxt_conf_object_members_count(const nxt_conf_value_t *value) -{ +nxt_conf_object_members_count(const nxt_conf_value_t *value) { return value->u.object->count; } - nxt_conf_value_t * -nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count) -{ +nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count) { size_t size; - nxt_conf_value_t *value; + nxt_conf_value_t *value; - size = sizeof(nxt_conf_value_t) - + sizeof(nxt_conf_object_t) + size = sizeof(nxt_conf_value_t) + sizeof(nxt_conf_object_t) + count * sizeof(nxt_conf_object_member_t); value = nxt_mp_get(mp, size); @@ -268,7 +258,7 @@ nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count) return NULL; } - value->u.object = nxt_pointer_to(value, sizeof(nxt_conf_value_t)); + value->u.object = nxt_pointer_to(value, sizeof(nxt_conf_value_t)); value->u.object->count = count; value->type = NXT_CONF_VALUE_OBJECT; @@ -276,12 +266,10 @@ nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count) return value; } - void nxt_conf_set_member(nxt_conf_value_t *object, const nxt_str_t *name, - const nxt_conf_value_t *value, uint32_t index) -{ - nxt_conf_object_member_t *member; + const nxt_conf_value_t *value, uint32_t index) { + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; @@ -290,12 +278,10 @@ nxt_conf_set_member(nxt_conf_value_t *object, const nxt_str_t *name, member->value = *value; } - nxt_int_t nxt_conf_set_member_dup(nxt_conf_value_t *object, nxt_mp_t *mp, - const nxt_str_t *name, const nxt_conf_value_t *value, uint32_t index) -{ - nxt_conf_object_member_t *member; + const nxt_str_t *name, const nxt_conf_value_t *value, uint32_t index) { + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; @@ -304,12 +290,10 @@ nxt_conf_set_member_dup(nxt_conf_value_t *object, nxt_mp_t *mp, return nxt_conf_set_string_dup(&member->name, mp, name); } - void nxt_conf_set_member_string(nxt_conf_value_t *object, const nxt_str_t *name, - const nxt_str_t *value, uint32_t index) -{ - nxt_conf_object_member_t *member; + const nxt_str_t *value, uint32_t index) { + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; @@ -318,12 +302,10 @@ nxt_conf_set_member_string(nxt_conf_value_t *object, const nxt_str_t *name, nxt_conf_set_string(&member->value, value); } - nxt_int_t nxt_conf_set_member_string_dup(nxt_conf_value_t *object, nxt_mp_t *mp, - const nxt_str_t *name, const nxt_str_t *value, uint32_t index) -{ - nxt_conf_object_member_t *member; + const nxt_str_t *name, const nxt_str_t *value, uint32_t index) { + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; @@ -332,33 +314,29 @@ nxt_conf_set_member_string_dup(nxt_conf_value_t *object, nxt_mp_t *mp, return nxt_conf_set_string_dup(&member->value, mp, value); } - void nxt_conf_set_member_integer(nxt_conf_value_t *object, const nxt_str_t *name, - int64_t value, uint32_t index) -{ - u_char *p, *end; - nxt_conf_object_member_t *member; + int64_t value, uint32_t index) { + u_char *p, *end; + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; nxt_conf_set_string(&member->name, name); - p = member->value.u.number; + p = member->value.u.number; end = p + NXT_CONF_MAX_NUMBER_LEN; - end = nxt_sprintf(p, end, "%L", value); + end = nxt_sprintf(p, end, "%L", value); *end = '\0'; member->value.type = NXT_CONF_VALUE_INTEGER; } - void nxt_conf_set_member_null(nxt_conf_value_t *object, const nxt_str_t *name, - uint32_t index) -{ - nxt_conf_object_member_t *member; + uint32_t index) { + nxt_conf_object_member_t *member; member = &object->u.object->members[index]; @@ -367,15 +345,12 @@ nxt_conf_set_member_null(nxt_conf_value_t *object, const nxt_str_t *name, member->value.type = NXT_CONF_VALUE_NULL; } - nxt_conf_value_t * -nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count) -{ +nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count) { size_t size; - nxt_conf_value_t *value; + nxt_conf_value_t *value; - size = sizeof(nxt_conf_value_t) - + sizeof(nxt_conf_array_t) + size = sizeof(nxt_conf_value_t) + sizeof(nxt_conf_array_t) + count * sizeof(nxt_conf_value_t); value = nxt_mp_get(mp, size); @@ -383,7 +358,7 @@ nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count) return NULL; } - value->u.array = nxt_pointer_to(value, sizeof(nxt_conf_value_t)); + value->u.array = nxt_pointer_to(value, sizeof(nxt_conf_value_t)); value->u.array->count = count; value->type = NXT_CONF_VALUE_ARRAY; @@ -391,46 +366,35 @@ nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count) return value; } - void nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index, - const nxt_conf_value_t *value) -{ + const nxt_conf_value_t *value) { array->u.array->elements[index] = *value; } - nxt_int_t nxt_conf_set_element_string_dup(nxt_conf_value_t *array, nxt_mp_t *mp, - nxt_uint_t index, const nxt_str_t *value) -{ - nxt_conf_value_t *element; + nxt_uint_t index, const nxt_str_t *value) { + nxt_conf_value_t *element; element = &array->u.array->elements[index]; return nxt_conf_set_string_dup(element, mp, value); } - nxt_uint_t -nxt_conf_array_elements_count(const nxt_conf_value_t *value) -{ +nxt_conf_array_elements_count(const nxt_conf_value_t *value) { return value->u.array->count; } - nxt_uint_t -nxt_conf_array_elements_count_or_1(const nxt_conf_value_t *value) -{ +nxt_conf_array_elements_count_or_1(const nxt_conf_value_t *value) { return (value->type == NXT_CONF_VALUE_ARRAY) ? value->u.array->count : 1; } - nxt_uint_t -nxt_conf_type(const nxt_conf_value_t *value) -{ +nxt_conf_type(const nxt_conf_value_t *value) { switch (value->type) { - case NXT_CONF_VALUE_NULL: return NXT_CONF_NULL; @@ -459,17 +423,15 @@ nxt_conf_type(const nxt_conf_value_t *value) return 0; } - nxt_conf_value_t * -nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path) -{ - nxt_str_t token; - nxt_int_t ret, index; - nxt_conf_path_parse_t parse; +nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path) { + nxt_str_t token; + nxt_int_t ret, index; + nxt_conf_path_parse_t parse; parse.start = path->start; - parse.end = path->start + path->length; - parse.last = 0; + parse.end = path->start + path->length; + parse.last = 0; do { ret = nxt_conf_path_next_token(&parse, &token); @@ -478,7 +440,6 @@ nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path) } if (token.length == 0) { - if (parse.last) { break; } @@ -487,7 +448,6 @@ nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path) } switch (value->type) { - case NXT_CONF_VALUE_OBJECT: value = nxt_conf_get_object_member(value, &token, NULL); break; @@ -515,11 +475,9 @@ nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path) return value; } - static nxt_int_t -nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, nxt_str_t *token) -{ - u_char *p, *start, *end; +nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, nxt_str_t *token) { + u_char *p, *start, *end; size_t length; start = parse->start + 1; @@ -531,7 +489,7 @@ nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, nxt_str_t *token) } parse->start = p; - parse->last = (p >= parse->end); + parse->last = (p >= parse->end); length = p - start; @@ -545,20 +503,18 @@ nxt_conf_path_next_token(nxt_conf_path_parse_t *parse, nxt_str_t *token) } token->length = end - parse->buf; - token->start = parse->buf; + token->start = parse->buf; return NXT_OK; } - nxt_conf_value_t * nxt_conf_get_object_member(const nxt_conf_value_t *value, const nxt_str_t *name, - uint32_t *index) -{ + uint32_t *index) { nxt_str_t str; nxt_uint_t n; - nxt_conf_object_t *object; - nxt_conf_object_member_t *member; + nxt_conf_object_t *object; + nxt_conf_object_member_t *member; if (value->type != NXT_CONF_VALUE_OBJECT) { return NULL; @@ -572,7 +528,6 @@ nxt_conf_get_object_member(const nxt_conf_value_t *value, const nxt_str_t *name, nxt_conf_get_string(&member->name, &str); if (nxt_strstr_eq(&str, name)) { - if (index != NULL) { *index = n; } @@ -584,32 +539,29 @@ nxt_conf_get_object_member(const nxt_conf_value_t *value, const nxt_str_t *name, return NULL; } - nxt_int_t nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, - const nxt_conf_map_t *map, nxt_uint_t n, void *data) -{ + const nxt_conf_map_t *map, nxt_uint_t n, void *data) { double num; nxt_str_t str, *s; nxt_uint_t i; - nxt_conf_value_t *v; + nxt_conf_value_t *v; union { - uint8_t ui8; - int32_t i32; - int64_t i64; - int i; - ssize_t size; - off_t off; - nxt_msec_t msec; - double dbl; - nxt_str_t str; - char *cstrz; - void *v; + uint8_t ui8; + int32_t i32; + int64_t i64; + int i; + ssize_t size; + off_t off; + nxt_msec_t msec; + double dbl; + nxt_str_t str; + char *cstrz; + void *v; } *ptr; for (i = 0; i < n; i++) { - v = nxt_conf_get_object_member(value, &map[i].name, NULL); if (v == NULL || v->type == NXT_CONF_VALUE_NULL) { @@ -619,7 +571,6 @@ nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, ptr = nxt_pointer_to(data, map[i].offset); switch (map[i].type) { - case NXT_CONF_MAP_INT8: if (v->type == NXT_CONF_VALUE_BOOLEAN) { @@ -642,7 +593,6 @@ nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, num = nxt_strtod(v->u.number, NULL); switch (map[i].type) { - case NXT_CONF_MAP_INT32: ptr->i32 = num; break; @@ -686,15 +636,13 @@ nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, case NXT_CONF_MAP_CSTRZ: if (v->type != NXT_CONF_VALUE_SHORT_STRING - && v->type != NXT_CONF_VALUE_STRING) - { + && v->type != NXT_CONF_VALUE_STRING) { break; } nxt_conf_get_string(v, &str); switch (map[i].type) { - case NXT_CONF_MAP_STR: ptr->str = str; break; @@ -736,20 +684,18 @@ nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, return NXT_OK; } - nxt_conf_value_t * nxt_conf_next_object_member(const nxt_conf_value_t *value, nxt_str_t *name, - uint32_t *next) -{ + uint32_t *next) { uint32_t n; - nxt_conf_object_t *object; - nxt_conf_object_member_t *member; + nxt_conf_object_t *object; + nxt_conf_object_member_t *member; if (value->type != NXT_CONF_VALUE_OBJECT) { return NULL; } - n = *next; + n = *next; object = value->u.object; if (n >= object->count) { @@ -757,18 +703,16 @@ nxt_conf_next_object_member(const nxt_conf_value_t *value, nxt_str_t *name, } member = &object->members[n]; - *next = n + 1; + *next = n + 1; nxt_conf_get_string(&member->name, name); return &member->value; } - nxt_conf_value_t * -nxt_conf_get_array_element(const nxt_conf_value_t *value, uint32_t index) -{ - nxt_conf_array_t *array; +nxt_conf_get_array_element(const nxt_conf_value_t *value, uint32_t index) { + nxt_conf_array_t *array; if (value->type != NXT_CONF_VALUE_ARRAY) { return NULL; @@ -783,11 +727,9 @@ nxt_conf_get_array_element(const nxt_conf_value_t *value, uint32_t index) return &array->elements[index]; } - nxt_conf_value_t * -nxt_conf_get_array_element_or_itself(nxt_conf_value_t *value, uint32_t index) -{ - nxt_conf_array_t *array; +nxt_conf_get_array_element_or_itself(nxt_conf_value_t *value, uint32_t index) { + nxt_conf_array_t *array; if (value->type != NXT_CONF_VALUE_ARRAY) { return (index == 0) ? value : NULL; @@ -802,12 +744,10 @@ nxt_conf_get_array_element_or_itself(nxt_conf_value_t *value, uint32_t index) return &array->elements[index]; } - void nxt_conf_array_qsort(const nxt_conf_value_t *value, - int (*compare)(const void *, const void *)) -{ - nxt_conf_array_t *array; + int (*compare)(const void *, const void *)) { + nxt_conf_array_t *array; if (value->type != NXT_CONF_VALUE_ARRAY) { return; @@ -818,33 +758,31 @@ nxt_conf_array_qsort(const nxt_conf_value_t *value, nxt_qsort(array->elements, array->count, sizeof(nxt_conf_value_t), compare); } - nxt_conf_op_ret_t nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, const nxt_conf_value_t *root, const nxt_str_t *path, - nxt_conf_value_t *value, nxt_bool_t add) -{ + nxt_conf_value_t *value, nxt_bool_t add) { nxt_str_t token; nxt_int_t ret, index; - nxt_conf_op_t *op, **parent; - nxt_conf_value_t *node; + nxt_conf_op_t *op, **parent; + nxt_conf_value_t *node; nxt_conf_path_parse_t parse; - nxt_conf_object_member_t *member; + nxt_conf_object_member_t *member; parse.start = path->start; - parse.end = path->start + path->length; - parse.last = 0; + parse.end = path->start + path->length; + parse.last = 0; parent = ops; - for ( ;; ) { + for (;;) { op = nxt_mp_zget(mp, sizeof(nxt_conf_op_t)); if (nxt_slow_path(op == NULL)) { return NXT_CONF_OP_ERROR; } *parent = op; - parent = (nxt_conf_op_t **) &op->ctx; + parent = (nxt_conf_op_t **) &op->ctx; ret = nxt_conf_path_next_token(&parse, &token); if (nxt_slow_path(ret != NXT_OK)) { @@ -852,7 +790,6 @@ nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, } switch (root->type) { - case NXT_CONF_VALUE_OBJECT: node = nxt_conf_get_object_member(root, &token, &op->index); break; @@ -882,11 +819,10 @@ nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, } op->action = NXT_CONF_OP_PASS; - root = node; + root = node; } if (value == NULL) { - if (node == NULL) { return NXT_CONF_OP_NOT_FOUND; } @@ -914,16 +850,16 @@ nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, *parent = op; - op->index = node->u.array->count; + op->index = node->u.array->count; op->action = NXT_CONF_OP_CREATE; - op->ctx = value; + op->ctx = value; return NXT_CONF_OP_OK; } if (node != NULL) { op->action = NXT_CONF_OP_REPLACE; - op->ctx = value; + op->ctx = value; return NXT_CONF_OP_OK; } @@ -951,18 +887,16 @@ nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, member->value = *value; op->index = root->u.object->count; - op->ctx = member; + op->ctx = member; } return NXT_CONF_OP_OK; } - nxt_conf_value_t * -nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op, const nxt_conf_value_t *value) -{ +nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op, const nxt_conf_value_t *value) { nxt_int_t rc; - nxt_conf_value_t *copy; + nxt_conf_value_t *copy; copy = nxt_mp_get(mp, sizeof(nxt_conf_value_t)); if (nxt_slow_path(copy == NULL)) { @@ -978,20 +912,15 @@ nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op, const nxt_conf_value_t *value) return copy; } - static nxt_int_t nxt_conf_copy_value(nxt_mp_t *mp, const nxt_conf_op_t *op, - nxt_conf_value_t *dst, const nxt_conf_value_t *src) -{ - if (op != NULL - && src->type != NXT_CONF_VALUE_ARRAY - && src->type != NXT_CONF_VALUE_OBJECT) - { + nxt_conf_value_t *dst, const nxt_conf_value_t *src) { + if (op != NULL && src->type != NXT_CONF_VALUE_ARRAY + && src->type != NXT_CONF_VALUE_OBJECT) { return NXT_ERROR; } switch (src->type) { - case NXT_CONF_VALUE_STRING: dst->u.string.start = nxt_mp_nget(mp, src->u.string.length); @@ -1000,7 +929,7 @@ nxt_conf_copy_value(nxt_mp_t *mp, const nxt_conf_op_t *op, } nxt_memcpy(dst->u.string.start, src->u.string.start, - src->u.string.length); + src->u.string.length); dst->u.string.length = src->u.string.length; @@ -1021,16 +950,14 @@ nxt_conf_copy_value(nxt_mp_t *mp, const nxt_conf_op_t *op, return NXT_OK; } - static nxt_int_t nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, - nxt_conf_value_t *dst, const nxt_conf_value_t *src) -{ + nxt_conf_value_t *dst, const nxt_conf_value_t *src) { size_t size; nxt_int_t rc; nxt_uint_t s, d, count, index; - nxt_conf_op_t *pass_op; - nxt_conf_value_t *value; + nxt_conf_op_t *pass_op; + nxt_conf_value_t *value; count = src->u.array->count; @@ -1070,7 +997,7 @@ nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, while (s != index) { rc = nxt_conf_copy_value(mp, pass_op, &dst->u.array->elements[d], - &src->u.array->elements[s]); + &src->u.array->elements[s]); if (nxt_slow_path(rc != NXT_OK)) { return NXT_ERROR; } @@ -1092,14 +1019,14 @@ nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, break; case NXT_CONF_OP_CREATE: - value = op->ctx; + value = op->ctx; dst->u.array->elements[d] = *value; d++; break; case NXT_CONF_OP_REPLACE: - value = op->ctx; + value = op->ctx; dst->u.array->elements[d] = *value; s++; @@ -1121,17 +1048,15 @@ nxt_conf_copy_array(nxt_mp_t *mp, const nxt_conf_op_t *op, return NXT_OK; } - static nxt_int_t nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, - nxt_conf_value_t *dst, const nxt_conf_value_t *src) -{ + nxt_conf_value_t *dst, const nxt_conf_value_t *src) { size_t size; nxt_int_t rc; nxt_uint_t s, d, count, index; - nxt_conf_op_t *pass_op; - nxt_conf_value_t *value; - nxt_conf_object_member_t *member; + nxt_conf_op_t *pass_op; + nxt_conf_value_t *value; + nxt_conf_object_member_t *member; count = src->u.object->count; @@ -1144,8 +1069,7 @@ nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, } } - size = sizeof(nxt_conf_object_t) - + count * sizeof(nxt_conf_object_member_t); + size = sizeof(nxt_conf_object_t) + count * sizeof(nxt_conf_object_member_t); dst->u.object = nxt_mp_get(mp, size); if (nxt_slow_path(dst->u.object == NULL)) { @@ -1171,17 +1095,16 @@ nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, } while (s != index) { - rc = nxt_conf_copy_value(mp, NULL, - &dst->u.object->members[d].name, - &src->u.object->members[s].name); + rc = nxt_conf_copy_value(mp, NULL, &dst->u.object->members[d].name, + &src->u.object->members[s].name); if (nxt_slow_path(rc != NXT_OK)) { return NXT_ERROR; } rc = nxt_conf_copy_value(mp, pass_op, - &dst->u.object->members[d].value, - &src->u.object->members[s].value); + &dst->u.object->members[d].value, + &src->u.object->members[s].value); if (nxt_slow_path(rc != NXT_OK)) { return NXT_ERROR; @@ -1207,8 +1130,7 @@ nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, member = op->ctx; rc = nxt_conf_copy_value(mp, NULL, - &dst->u.object->members[d].name, - &member->name); + &dst->u.object->members[d].name, &member->name); if (nxt_slow_path(rc != NXT_OK)) { return NXT_ERROR; @@ -1221,8 +1143,8 @@ nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, case NXT_CONF_OP_REPLACE: rc = nxt_conf_copy_value(mp, NULL, - &dst->u.object->members[d].name, - &src->u.object->members[s].name); + &dst->u.object->members[d].name, + &src->u.object->members[s].name); if (nxt_slow_path(rc != NXT_OK)) { return NXT_ERROR; @@ -1251,13 +1173,11 @@ nxt_conf_copy_object(nxt_mp_t *mp, const nxt_conf_op_t *op, return NXT_OK; } - nxt_conf_value_t * nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, - nxt_conf_json_error_t *error) -{ - u_char *p; - nxt_conf_value_t *value; + nxt_conf_json_error_t *error) { + u_char *p; + nxt_conf_value_t *value; value = nxt_mp_get(mp, sizeof(nxt_conf_value_t)); if (nxt_slow_path(value == NULL)) { @@ -1267,12 +1187,11 @@ nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, p = nxt_conf_json_skip_space(start, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, start, "An empty JSON payload isn't allowed. It must be either a literal " "(null, true, or false), a number, a string (in double quotes " - "\"\"), an array (with brackets []), or an object (with braces {})." - ); + "\"\"), an array (with brackets []), or an object (with braces " + "{})."); return NULL; } @@ -1286,10 +1205,8 @@ nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, p = nxt_conf_json_skip_space(p, end); if (nxt_slow_path(p != end)) { - nxt_conf_json_parse_error(error, p, - "Unexpected character after the end of a valid JSON value." - ); + "Unexpected character after the end of a valid JSON value."); return NULL; } @@ -1297,11 +1214,9 @@ nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, return value; } - static u_char * -nxt_conf_json_skip_space(u_char *start, const u_char *end) -{ - u_char *p, ch; +nxt_conf_json_skip_space(u_char *start, const u_char *end) { + u_char *p, ch; enum { sw_normal = 0, @@ -1317,7 +1232,6 @@ nxt_conf_json_skip_space(u_char *start, const u_char *end) ch = *p; switch (state) { - case sw_normal: switch (ch) { case ' ': @@ -1382,12 +1296,10 @@ nxt_conf_json_skip_space(u_char *start, const u_char *end) return p; } - static u_char * nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, - u_char *end, nxt_conf_json_error_t *error) -{ - u_char ch, *p; + u_char *end, nxt_conf_json_error_t *error) { + u_char ch, *p; ch = *start; @@ -1402,11 +1314,9 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, return nxt_conf_json_parse_string(mp, value, start, end, error); case 't': - if (nxt_fast_path(end - start >= 4 - && memcmp(start, "true", 4) == 0)) - { + if (nxt_fast_path(end - start >= 4 && memcmp(start, "true", 4) == 0)) { value->u.boolean = 1; - value->type = NXT_CONF_VALUE_BOOLEAN; + value->type = NXT_CONF_VALUE_BOOLEAN; return start + 4; } @@ -1414,11 +1324,9 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, goto error; case 'f': - if (nxt_fast_path(end - start >= 5 - && memcmp(start, "false", 5) == 0)) - { + if (nxt_fast_path(end - start >= 5 && memcmp(start, "false", 5) == 0)) { value->u.boolean = 0; - value->type = NXT_CONF_VALUE_BOOLEAN; + value->type = NXT_CONF_VALUE_BOOLEAN; return start + 5; } @@ -1426,9 +1334,7 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, goto error; case 'n': - if (nxt_fast_path(end - start >= 4 - && memcmp(start, "null", 4) == 0)) - { + if (nxt_fast_path(end - start >= 4 && memcmp(start, "null", 4) == 0)) { value->type = NXT_CONF_VALUE_NULL; return start + 4; } @@ -1444,7 +1350,7 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, goto error; } - if (nxt_fast_path((u_char)(ch - '0') <= 9)) { + if (nxt_fast_path((u_char) (ch - '0') <= 9)) { p = nxt_conf_json_parse_number(mp, value, start, end, error); if (nxt_slow_path(p == NULL)) { @@ -1476,35 +1382,29 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, start, "A valid JSON value is expected here. It must be either a literal " "(null, true, or false), a number, a string (in double quotes \"\"), " - "an array (with brackets []), or an object (with braces {})." - ); + "an array (with brackets []), or an object (with braces {})."); return NULL; } - -static const nxt_lvlhsh_proto_t nxt_conf_object_hash_proto - nxt_aligned(64) = -{ +static const nxt_lvlhsh_proto_t nxt_conf_object_hash_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_conf_object_hash_test, nxt_conf_object_hash_alloc, nxt_conf_object_hash_free, }; - static u_char * nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, - u_char *end, nxt_conf_json_error_t *error) -{ - u_char *p, *name; - nxt_mp_t *mp_temp; + u_char *end, nxt_conf_json_error_t *error) { + u_char *p, *name; + nxt_mp_t *mp_temp; nxt_int_t rc; nxt_uint_t count; nxt_lvlhsh_t hash; nxt_lvlhsh_each_t lhe; - nxt_conf_object_t *object; - nxt_conf_object_member_t *member, *element; + nxt_conf_object_t *object; + nxt_conf_object_member_t *member, *element; mp_temp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp_temp == NULL)) { @@ -1514,17 +1414,15 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_lvlhsh_init(&hash); count = 0; - p = start; + p = start; - for ( ;; ) { + for (;;) { p = nxt_conf_json_skip_space(p + 1, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an object without " - "a closing brace (})." - ); + "a closing brace (})."); goto error; } @@ -1537,8 +1435,7 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, p, "A double quote (\") is expected here. There must be a valid " "JSON object member starts with a name, which is a string " - "enclosed in double quotes." - ); + "enclosed in double quotes."); goto error; } @@ -1561,12 +1458,10 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, rc = nxt_conf_object_hash_add(mp_temp, &hash, member); if (nxt_slow_path(rc != NXT_OK)) { - if (rc == NXT_DECLINED) { nxt_conf_json_parse_error(error, name, "Duplicate object member. All JSON object members must " - "have unique names." - ); + "have unique names."); } goto error; @@ -1575,21 +1470,17 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, p = nxt_conf_json_skip_space(p, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an object member " - "without a value." - ); + "without a value."); goto error; } if (nxt_slow_path(*p != ':')) { - nxt_conf_json_parse_error(error, p, "A colon (:) is expected here. There must be a colon after " - "a JSON member name." - ); + "a JSON member name."); goto error; } @@ -1597,11 +1488,9 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, p = nxt_conf_json_skip_space(p + 1, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an object member " - "without a value." - ); + "without a value."); goto error; } @@ -1615,11 +1504,9 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, p = nxt_conf_json_skip_space(p, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an object without " - "a closing brace (})." - ); + "a closing brace (})."); goto error; } @@ -1632,28 +1519,27 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, p, "Either a closing brace (}) or a comma (,) is expected here. " "Each JSON object must be enclosed in braces and its members " - "must be separated by commas." - ); + "must be separated by commas."); goto error; } } - object = nxt_mp_get(mp, sizeof(nxt_conf_object_t) - + count * sizeof(nxt_conf_object_member_t)); + object = nxt_mp_get(mp, + sizeof(nxt_conf_object_t) + count * sizeof(nxt_conf_object_member_t)); if (nxt_slow_path(object == NULL)) { goto error; } value->u.object = object; - value->type = NXT_CONF_VALUE_OBJECT; + value->type = NXT_CONF_VALUE_OBJECT; object->count = count; - member = object->members; + member = object->members; nxt_lvlhsh_each_init(&lhe, &nxt_conf_object_hash_proto); - for ( ;; ) { + for (;;) { element = nxt_lvlhsh_each(&hash, &lhe); if (element == NULL) { @@ -1673,30 +1559,26 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, return NULL; } - static nxt_int_t nxt_conf_object_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *lvlhsh, - nxt_conf_object_member_t *member) -{ - nxt_lvlhsh_query_t lhq; + nxt_conf_object_member_t *member) { + nxt_lvlhsh_query_t lhq; nxt_conf_get_string(&member->name, &lhq.key); lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length); - lhq.replace = 0; - lhq.value = member; - lhq.proto = &nxt_conf_object_hash_proto; - lhq.pool = mp; + lhq.replace = 0; + lhq.value = member; + lhq.proto = &nxt_conf_object_hash_proto; + lhq.pool = mp; return nxt_lvlhsh_insert(lvlhsh, &lhq); } - static nxt_int_t -nxt_conf_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_conf_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { nxt_str_t str; - nxt_conf_object_member_t *member; + nxt_conf_object_member_t *member; member = data; @@ -1705,31 +1587,25 @@ nxt_conf_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data) return nxt_strstr_eq(&lhq->key, &str) ? NXT_OK : NXT_DECLINED; } - static void * -nxt_conf_object_hash_alloc(void *data, size_t size) -{ +nxt_conf_object_hash_alloc(void *data, size_t size) { return nxt_mp_align(data, size, size); } - static void -nxt_conf_object_hash_free(void *data, void *p) -{ +nxt_conf_object_hash_free(void *data, void *p) { nxt_mp_free(data, p); } - static u_char * nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, - u_char *end, nxt_conf_json_error_t *error) -{ - u_char *p; - nxt_mp_t *mp_temp; + u_char *end, nxt_conf_json_error_t *error) { + u_char *p; + nxt_mp_t *mp_temp; nxt_uint_t count; - nxt_list_t *list; - nxt_conf_array_t *array; - nxt_conf_value_t *element; + nxt_list_t *list; + nxt_conf_array_t *array; + nxt_conf_value_t *element; mp_temp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp_temp == NULL)) { @@ -1742,17 +1618,15 @@ nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, } count = 0; - p = start; + p = start; - for ( ;; ) { + for (;;) { p = nxt_conf_json_skip_space(p + 1, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an array without " - "a closing bracket (])." - ); + "a closing bracket (])."); goto error; } @@ -1777,11 +1651,9 @@ nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, p = nxt_conf_json_skip_space(p, end); if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's an array without " - "a closing bracket (])." - ); + "a closing bracket (])."); goto error; } @@ -1794,28 +1666,28 @@ nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, p, "Either a closing bracket (]) or a comma (,) is expected " "here. Each array must be enclosed in brackets and its " - "members must be separated by commas." - ); + "members must be separated by commas."); goto error; } } - array = nxt_mp_get(mp, sizeof(nxt_conf_array_t) - + count * sizeof(nxt_conf_value_t)); + array = nxt_mp_get(mp, + sizeof(nxt_conf_array_t) + count * sizeof(nxt_conf_value_t)); if (nxt_slow_path(array == NULL)) { goto error; } value->u.array = array; - value->type = NXT_CONF_VALUE_ARRAY; + value->type = NXT_CONF_VALUE_ARRAY; array->count = count; - element = array->elements; + element = array->elements; nxt_list_each(value, list) { *element++ = *value; - } nxt_list_loop; + } + nxt_list_loop; nxt_mp_destroy(mp_temp); @@ -1827,15 +1699,14 @@ nxt_conf_json_parse_array(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, return NULL; } - static u_char * nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, - u_char *end, nxt_conf_json_error_t *error) -{ - u_char *p, ch, *last, *s; - size_t size, surplus; - uint32_t utf, utf_high; - nxt_uint_t i; + u_char *end, nxt_conf_json_error_t *error) { + u_char *p, ch, *last, *s; + size_t size, surplus; + uint32_t utf, utf_high; + nxt_uint_t i; + enum { sw_usual = 0, sw_escape, @@ -1847,14 +1718,13 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, start++; - state = 0; + state = 0; surplus = 0; for (p = start; nxt_fast_path(p != end); p++) { ch = *p; switch (state) { - case sw_usual: if (ch == '"') { @@ -1872,8 +1742,7 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, p, "Unexpected character. All control characters in a JSON " - "string must be escaped." - ); + "string must be escaped."); return NULL; @@ -1901,14 +1770,13 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, * and 3 or 4 bytes in UTF-8. */ surplus += 3; - state = sw_encoded1; + state = sw_encoded1; continue; } nxt_conf_json_parse_error(error, p - 1, "Unexpected backslash. A literal backslash in a JSON string " - "must be escaped with a second backslash (\\\\)." - ); + "must be escaped with a second backslash (\\\\)."); return NULL; @@ -1919,8 +1787,7 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, if (nxt_fast_path((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') - || (ch >= 'a' && ch <= 'f'))) - { + || (ch >= 'a' && ch <= 'f'))) { state = (state == sw_encoded4) ? sw_usual : state + 1; continue; } @@ -1928,8 +1795,7 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, nxt_conf_json_parse_error(error, p, "Invalid escape sequence. An escape sequence in a JSON " "string must start with a backslash, followed by the lowercase " - "letter u, followed by four hexadecimal digits (\\uXXXX)." - ); + "letter u, followed by four hexadecimal digits (\\uXXXX)."); return NULL; } @@ -1938,11 +1804,9 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, } if (nxt_slow_path(p == end)) { - nxt_conf_json_parse_error(error, p, "Unexpected end of JSON payload. There's a string without " - "a final double quote (\")." - ); + "a final double quote (\")."); return NULL; } @@ -1953,13 +1817,10 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, size = last - start - surplus; if (size > NXT_CONF_MAX_SHORT_STRING) { - if (nxt_slow_path(size > NXT_CONF_MAX_STRING)) { - nxt_conf_json_parse_error(error, start, "The string is too long. Such a long JSON string value " - "is not supported." - ); + "is not supported."); return NULL; } @@ -1976,7 +1837,7 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, s = value->u.string.start; } else { - value->type = NXT_CONF_VALUE_SHORT_STRING; + value->type = NXT_CONF_VALUE_SHORT_STRING; value->u.str.length = size; s = value->u.str.start; @@ -2027,24 +1888,23 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, continue; } - utf = 0; + utf = 0; utf_high = 0; - for ( ;; ) { + for (;;) { for (i = 0; i < 4; i++) { - utf = (utf << 4) | (p[i] >= 'A' ? 10 + ((p[i] & ~0x20) - 'A') - : p[i] - '0'); + utf = (utf << 4) + | (p[i] >= 'A' ? 10 + ((p[i] & ~0x20) - 'A') + : p[i] - '0'); } p += 4; if (utf_high != 0) { if (nxt_slow_path(utf < 0xDC00 || utf > 0xDFFF)) { - nxt_conf_json_parse_error(error, p - 12, "Invalid JSON encoding sequence. This 12-byte " - "sequence composes an illegal UTF-16 surrogate pair." - ); + "sequence composes an illegal UTF-16 surrogate pair."); return NULL; } @@ -2059,11 +1919,9 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, } if (utf > 0xDBFF || p[0] != '\\' || p[1] != 'u') { - nxt_conf_json_parse_error(error, p - 6, "Invalid JSON encoding sequence. This 6-byte sequence " - "does not represent a valid UTF character." - ); + "does not represent a valid UTF character."); return NULL; } @@ -2071,7 +1929,7 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, p += 2; utf_high = utf; - utf = 0; + utf = 0; } s = nxt_utf8_encode(s, utf); @@ -2088,16 +1946,14 @@ nxt_conf_json_parse_string(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, return last + 1; } - static u_char * nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, - u_char *end, nxt_conf_json_error_t *error) -{ - u_char *p, *s, ch, c, *dot_pos; + u_char *end, nxt_conf_json_error_t *error) { + u_char *p, *s, ch, c, *dot_pos; size_t size; double num; - s = start; + s = start; ch = *s; if (ch == '-') { @@ -2126,8 +1982,7 @@ nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, if (nxt_slow_path(p - dot_pos <= 1)) { nxt_conf_json_parse_error(error, s, "The number is invalid. A fraction part in JSON numbers " - "must contain at least one digit." - ); + "must contain at least one digit."); return NULL; } @@ -2139,8 +1994,7 @@ nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, if (nxt_slow_path(dot_pos - s > 1 && *start == '0')) { nxt_conf_json_parse_error(error, s, "The number is invalid. Leading zeros are not allowed in JSON " - "numbers." - ); + "numbers."); return NULL; } @@ -2171,8 +2025,7 @@ nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, if (nxt_slow_path(p == s)) { nxt_conf_json_parse_error(error, start, "The number is invalid. An exponent part in JSON numbers " - "must contain at least one digit." - ); + "must contain at least one digit."); return NULL; } @@ -2183,8 +2036,7 @@ nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, if (size > NXT_CONF_MAX_NUMBER_LEN) { nxt_conf_json_parse_error(error, start, "The number is too long. Such a long JSON number value " - "is not supported." - ); + "is not supported."); return NULL; } @@ -2193,53 +2045,46 @@ nxt_conf_json_parse_number(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start, value->u.number[size] = '\0'; nxt_errno = 0; - end = NULL; + end = NULL; num = nxt_strtod(value->u.number, &end); - if (nxt_slow_path(nxt_errno == NXT_ERANGE - || fabs(num) > (double) NXT_INT64_T_MAX)) - { + if (nxt_slow_path( + nxt_errno == NXT_ERANGE || fabs(num) > (double) NXT_INT64_T_MAX)) { nxt_conf_json_parse_error(error, start, "The number is out of representable range. Such JSON number " - "value is not supported." - ); + "value is not supported."); return NULL; } if (nxt_slow_path(end == NULL || *end != '\0')) { nxt_thread_log_alert("strtod(\"%s\", %s) failed %E", value->u.number, - end == NULL ? (u_char *) "NULL" : end, nxt_errno); + end == NULL ? (u_char *) "NULL" : end, nxt_errno); return NULL; } - value->type = (num == trunc(num)) ? NXT_CONF_VALUE_INTEGER - : NXT_CONF_VALUE_NUMBER; + value->type + = (num == trunc(num)) ? NXT_CONF_VALUE_INTEGER : NXT_CONF_VALUE_NUMBER; return p; } - static void nxt_conf_json_parse_error(nxt_conf_json_error_t *error, u_char *pos, - const char *detail) -{ + const char *detail) { if (error == NULL) { return; } - error->pos = pos; + error->pos = pos; error->detail = (u_char *) detail; } - size_t nxt_conf_json_length(const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { switch (value->type) { - case NXT_CONF_VALUE_NULL: return nxt_length("null"); @@ -2266,13 +2111,10 @@ nxt_conf_json_length(const nxt_conf_value_t *value, return 0; } - u_char * nxt_conf_json_print(u_char *p, const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { switch (value->type) { - case NXT_CONF_VALUE_NULL: return nxt_cpymem(p, "null", 4); @@ -2300,22 +2142,18 @@ nxt_conf_json_print(u_char *p, const nxt_conf_value_t *value, return p; } - static size_t -nxt_conf_json_string_length(const nxt_conf_value_t *value) -{ - nxt_str_t str; +nxt_conf_json_string_length(const nxt_conf_value_t *value) { + nxt_str_t str; nxt_conf_get_string(value, &str); return 2 + nxt_conf_json_escape_length(str.start, str.length); } - static u_char * -nxt_conf_json_print_string(u_char *p, const nxt_conf_value_t *value) -{ - nxt_str_t str; +nxt_conf_json_print_string(u_char *p, const nxt_conf_value_t *value) { + nxt_str_t str; nxt_conf_get_string(value, &str); @@ -2328,14 +2166,12 @@ nxt_conf_json_print_string(u_char *p, const nxt_conf_value_t *value) return p; } - static size_t nxt_conf_json_array_length(const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { size_t len; nxt_uint_t n; - nxt_conf_array_t *array; + nxt_conf_array_t *array; array = value->u.array; @@ -2370,13 +2206,11 @@ nxt_conf_json_array_length(const nxt_conf_value_t *value, return len + n; } - static u_char * nxt_conf_json_print_array(u_char *p, const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { nxt_uint_t n; - nxt_conf_array_t *array; + nxt_conf_array_t *array; array = value->u.array; @@ -2422,15 +2256,13 @@ nxt_conf_json_print_array(u_char *p, const nxt_conf_value_t *value, return p; } - static size_t nxt_conf_json_object_length(const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { size_t len; nxt_uint_t n; - nxt_conf_object_t *object; - nxt_conf_object_member_t *member; + nxt_conf_object_t *object; + nxt_conf_object_member_t *member; object = value->u.object; @@ -2466,21 +2298,18 @@ nxt_conf_json_object_length(const nxt_conf_value_t *value, return len; } - static u_char * nxt_conf_json_print_object(u_char *p, const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty) -{ + nxt_conf_json_pretty_t *pretty) { nxt_uint_t n; - nxt_conf_object_t *object; - nxt_conf_object_member_t *member; + nxt_conf_object_t *object; + nxt_conf_object_member_t *member; object = value->u.object; *p++ = '{'; if (object->count != 0) { - if (pretty != NULL) { p = nxt_conf_json_newline(p); pretty->level++; @@ -2490,7 +2319,7 @@ nxt_conf_json_print_object(u_char *p, const nxt_conf_value_t *value, n = 0; - for ( ;; ) { + for (;;) { if (pretty != NULL) { p = nxt_conf_json_indentation(p, pretty->level); } @@ -2518,7 +2347,7 @@ nxt_conf_json_print_object(u_char *p, const nxt_conf_value_t *value, if (pretty->more_space) { pretty->more_space = 0; - p = nxt_conf_json_newline(p); + p = nxt_conf_json_newline(p); } } } @@ -2538,12 +2367,10 @@ nxt_conf_json_print_object(u_char *p, const nxt_conf_value_t *value, return p; } - static size_t -nxt_conf_json_escape_length(u_char *p, size_t size) -{ - u_char ch; - size_t len; +nxt_conf_json_escape_length(u_char *p, size_t size) { + u_char ch; + size_t len; len = size; @@ -2554,7 +2381,6 @@ nxt_conf_json_escape_length(u_char *p, size_t size) len++; } else if (ch <= 0x1F) { - switch (ch) { case '\n': case '\r': @@ -2575,17 +2401,14 @@ nxt_conf_json_escape_length(u_char *p, size_t size) return len; } - static u_char * -nxt_conf_json_escape(u_char *dst, u_char *src, size_t size) -{ - u_char ch; +nxt_conf_json_escape(u_char *dst, u_char *src, size_t size) { + u_char ch; while (size) { ch = *src++; if (ch > 0x1F) { - if (ch == '\\' || ch == '"') { *dst++ = '\\'; } @@ -2617,7 +2440,9 @@ nxt_conf_json_escape(u_char *dst, u_char *src, size_t size) break; default: - *dst++ = 'u'; *dst++ = '0'; *dst++ = '0'; + *dst++ = 'u'; + *dst++ = '0'; + *dst++ = '0'; *dst++ = '0' + (ch >> 4); ch &= 0xF; @@ -2632,19 +2457,16 @@ nxt_conf_json_escape(u_char *dst, u_char *src, size_t size) return dst; } - void nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line, - nxt_uint_t *column) -{ - u_char *p; - ssize_t symbols; - nxt_uint_t lines; + nxt_uint_t *column) { + u_char *p; + ssize_t symbols; + nxt_uint_t lines; lines = 1; for (p = start; p != pos; p++) { - if (*p != '\n') { continue; } @@ -2656,7 +2478,7 @@ nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line, symbols = nxt_utf8_length(start, p - start); if (symbols != -1) { - *line = lines; + *line = lines; *column = 1 + symbols; } } diff --git a/src/nxt_conf.h b/src/nxt_conf.h index 493d64801..b8f6176f4 100644 --- a/src/nxt_conf.h +++ b/src/nxt_conf.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -9,7 +8,6 @@ #ifndef _NXT_CONF_INCLUDED_ #define _NXT_CONF_INCLUDED_ - typedef enum { NXT_CONF_NULL = 0, NXT_CONF_BOOLEAN, @@ -20,7 +18,6 @@ typedef enum { NXT_CONF_OBJECT, } nxt_conf_type_t; - typedef enum { NXT_CONF_OP_OK = 0, NXT_CONF_OP_NOT_FOUND, @@ -29,16 +26,14 @@ typedef enum { } nxt_conf_op_ret_t; -typedef struct nxt_conf_value_s nxt_conf_value_t; -typedef struct nxt_conf_op_s nxt_conf_op_t; - +typedef struct nxt_conf_value_s nxt_conf_value_t; +typedef struct nxt_conf_op_s nxt_conf_op_t; typedef struct { - u_char *pos; - u_char *detail; + u_char *pos; + u_char *detail; } nxt_conf_json_error_t; - typedef enum { NXT_CONF_MAP_INT8, NXT_CONF_MAP_INT32, @@ -54,108 +49,127 @@ typedef enum { NXT_CONF_MAP_PTR, } nxt_conf_map_type_t; - typedef struct { - nxt_str_t name; - nxt_conf_map_type_t type; - size_t offset; + nxt_str_t name; + nxt_conf_map_type_t type; + size_t offset; } nxt_conf_map_t; - typedef struct { - uint32_t level; - uint8_t more_space; /* 1 bit. */ + uint32_t level; + uint8_t more_space; /* 1 bit. */ } nxt_conf_json_pretty_t; - typedef struct { - nxt_conf_value_t *conf; - nxt_mp_t *pool; - nxt_str_t error; - void *ctx; - nxt_tstr_state_t *tstr_state; - nxt_mp_t *conf_pool; - nxt_uint_t ver; + nxt_conf_value_t *conf; + nxt_mp_t *pool; + nxt_str_t error; + void *ctx; + nxt_tstr_state_t *tstr_state; + nxt_mp_t *conf_pool; + nxt_uint_t ver; } nxt_conf_validation_t; - -NXT_EXPORT nxt_uint_t nxt_conf_type(const nxt_conf_value_t *value); - -NXT_EXPORT nxt_conf_value_t *nxt_conf_get_path(nxt_conf_value_t *value, - const nxt_str_t *path); -NXT_EXPORT nxt_conf_value_t *nxt_conf_get_object_member( - const nxt_conf_value_t *value, const nxt_str_t *name, uint32_t *index); -NXT_EXPORT nxt_conf_value_t *nxt_conf_next_object_member( - const nxt_conf_value_t *value, nxt_str_t *name, uint32_t *next); -NXT_EXPORT nxt_conf_value_t *nxt_conf_get_array_element( - const nxt_conf_value_t *value, uint32_t index); -NXT_EXPORT nxt_conf_value_t *nxt_conf_get_array_element_or_itself( - nxt_conf_value_t *value, uint32_t index); - -NXT_EXPORT nxt_int_t nxt_conf_map_object(nxt_mp_t *mp, - const nxt_conf_value_t *value, const nxt_conf_map_t *map, nxt_uint_t n, - void *data); - -nxt_conf_op_ret_t nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, - const nxt_conf_value_t *root, const nxt_str_t *path, nxt_conf_value_t *value, - nxt_bool_t add); -nxt_conf_value_t *nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op, - const nxt_conf_value_t *value); - -nxt_conf_value_t *nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, +NXT_EXPORT nxt_uint_t +nxt_conf_type(const nxt_conf_value_t *value); + +NXT_EXPORT nxt_conf_value_t * +nxt_conf_get_path(nxt_conf_value_t *value, const nxt_str_t *path); +NXT_EXPORT nxt_conf_value_t * +nxt_conf_get_object_member(const nxt_conf_value_t *value, const nxt_str_t *name, + uint32_t *index); +NXT_EXPORT nxt_conf_value_t * +nxt_conf_next_object_member(const nxt_conf_value_t *value, nxt_str_t *name, + uint32_t *next); +NXT_EXPORT nxt_conf_value_t * +nxt_conf_get_array_element(const nxt_conf_value_t *value, uint32_t index); +NXT_EXPORT nxt_conf_value_t * +nxt_conf_get_array_element_or_itself(nxt_conf_value_t *value, uint32_t index); + +NXT_EXPORT nxt_int_t +nxt_conf_map_object(nxt_mp_t *mp, const nxt_conf_value_t *value, + const nxt_conf_map_t *map, nxt_uint_t n, void *data); + +nxt_conf_op_ret_t +nxt_conf_op_compile(nxt_mp_t *mp, nxt_conf_op_t **ops, + const nxt_conf_value_t *root, const nxt_str_t *path, + nxt_conf_value_t *value, nxt_bool_t add); +nxt_conf_value_t * +nxt_conf_clone(nxt_mp_t *mp, nxt_conf_op_t *op, const nxt_conf_value_t *value); + +nxt_conf_value_t * +nxt_conf_json_parse(nxt_mp_t *mp, u_char *start, u_char *end, nxt_conf_json_error_t *error); -#define nxt_conf_json_parse_str(mp, str) \ +#define nxt_conf_json_parse_str(mp, str) \ nxt_conf_json_parse(mp, (str)->start, (str)->start + (str)->length, NULL) -size_t nxt_conf_json_length(const nxt_conf_value_t *value, +size_t +nxt_conf_json_length(const nxt_conf_value_t *value, + nxt_conf_json_pretty_t *pretty); +u_char * +nxt_conf_json_print(u_char *p, const nxt_conf_value_t *value, nxt_conf_json_pretty_t *pretty); -u_char *nxt_conf_json_print(u_char *p, const nxt_conf_value_t *value, - nxt_conf_json_pretty_t *pretty); -void nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line, +void +nxt_conf_json_position(u_char *start, const u_char *pos, nxt_uint_t *line, nxt_uint_t *column); -nxt_int_t nxt_conf_validate(nxt_conf_validation_t *vldt); +nxt_int_t +nxt_conf_validate(nxt_conf_validation_t *vldt); -NXT_EXPORT void nxt_conf_get_string(const nxt_conf_value_t *value, +NXT_EXPORT void +nxt_conf_get_string(const nxt_conf_value_t *value, nxt_str_t *str); +NXT_EXPORT nxt_str_t * +nxt_conf_get_string_dup(const nxt_conf_value_t *value, nxt_mp_t *mp, nxt_str_t *str); -NXT_EXPORT nxt_str_t *nxt_conf_get_string_dup(const nxt_conf_value_t *value, - nxt_mp_t *mp, nxt_str_t *str); -NXT_EXPORT void nxt_conf_set_string(nxt_conf_value_t *value, +NXT_EXPORT void +nxt_conf_set_string(nxt_conf_value_t *value, const nxt_str_t *str); +NXT_EXPORT nxt_int_t +nxt_conf_set_string_dup(nxt_conf_value_t *value, nxt_mp_t *mp, const nxt_str_t *str); -NXT_EXPORT nxt_int_t nxt_conf_set_string_dup(nxt_conf_value_t *value, - nxt_mp_t *mp, const nxt_str_t *str); -NXT_EXPORT double nxt_conf_get_number(nxt_conf_value_t *value); -NXT_EXPORT uint8_t nxt_conf_get_boolean(nxt_conf_value_t *value); +NXT_EXPORT double +nxt_conf_get_number(nxt_conf_value_t *value); +NXT_EXPORT uint8_t +nxt_conf_get_boolean(nxt_conf_value_t *value); // FIXME reimplement and reorder functions below -NXT_EXPORT nxt_uint_t nxt_conf_object_members_count( - const nxt_conf_value_t *value); -nxt_conf_value_t *nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count); -void nxt_conf_set_member(nxt_conf_value_t *object, const nxt_str_t *name, +NXT_EXPORT nxt_uint_t +nxt_conf_object_members_count(const nxt_conf_value_t *value); +nxt_conf_value_t * +nxt_conf_create_object(nxt_mp_t *mp, nxt_uint_t count); +void +nxt_conf_set_member(nxt_conf_value_t *object, const nxt_str_t *name, const nxt_conf_value_t *value, uint32_t index); -nxt_int_t nxt_conf_set_member_dup(nxt_conf_value_t *object, nxt_mp_t *mp, +nxt_int_t +nxt_conf_set_member_dup(nxt_conf_value_t *object, nxt_mp_t *mp, const nxt_str_t *name, const nxt_conf_value_t *value, uint32_t index); -void nxt_conf_set_member_string(nxt_conf_value_t *object, +void +nxt_conf_set_member_string(nxt_conf_value_t *object, const nxt_str_t *name, + const nxt_str_t *value, uint32_t index); +nxt_int_t +nxt_conf_set_member_string_dup(nxt_conf_value_t *object, nxt_mp_t *mp, const nxt_str_t *name, const nxt_str_t *value, uint32_t index); -nxt_int_t nxt_conf_set_member_string_dup(nxt_conf_value_t *object, - nxt_mp_t *mp, const nxt_str_t *name, const nxt_str_t *value, - uint32_t index); -void nxt_conf_set_member_integer(nxt_conf_value_t *object, - const nxt_str_t *name, int64_t value, uint32_t index); -void nxt_conf_set_member_null(nxt_conf_value_t *object, const nxt_str_t *name, +void +nxt_conf_set_member_integer(nxt_conf_value_t *object, const nxt_str_t *name, + int64_t value, uint32_t index); +void +nxt_conf_set_member_null(nxt_conf_value_t *object, const nxt_str_t *name, uint32_t index); -nxt_conf_value_t *nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count); -void nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index, - const nxt_conf_value_t *value); -nxt_int_t nxt_conf_set_element_string_dup(nxt_conf_value_t *array, - nxt_mp_t *mp, nxt_uint_t index, const nxt_str_t *value); -NXT_EXPORT nxt_uint_t nxt_conf_array_elements_count( - const nxt_conf_value_t *value); -NXT_EXPORT nxt_uint_t nxt_conf_array_elements_count_or_1( +nxt_conf_value_t * +nxt_conf_create_array(nxt_mp_t *mp, nxt_uint_t count); +void +nxt_conf_set_element(nxt_conf_value_t *array, nxt_uint_t index, const nxt_conf_value_t *value); -void nxt_conf_array_qsort(const nxt_conf_value_t *value, +nxt_int_t +nxt_conf_set_element_string_dup(nxt_conf_value_t *array, nxt_mp_t *mp, + nxt_uint_t index, const nxt_str_t *value); +NXT_EXPORT nxt_uint_t +nxt_conf_array_elements_count(const nxt_conf_value_t *value); +NXT_EXPORT nxt_uint_t +nxt_conf_array_elements_count_or_1(const nxt_conf_value_t *value); +void +nxt_conf_array_qsort(const nxt_conf_value_t *value, int (*compare)(const void *, const void *)); diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c index 5d7f7c52b..a0c1dadca 100644 --- a/src/nxt_conf_validation.c +++ b/src/nxt_conf_validation.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Valentin V. Bartenev * Copyright (C) NGINX, Inc. @@ -15,7 +14,6 @@ #include #include - typedef enum { NXT_CONF_VLDT_NULL = 1 << NXT_CONF_NULL, NXT_CONF_VLDT_BOOLEAN = 1 << NXT_CONF_BOOLEAN, @@ -26,1414 +24,1520 @@ typedef enum { NXT_CONF_VLDT_OBJECT = 1 << NXT_CONF_OBJECT, } nxt_conf_vldt_type_t; -#define NXT_CONF_VLDT_ANY_TYPE (NXT_CONF_VLDT_NULL \ - |NXT_CONF_VLDT_BOOLEAN \ - |NXT_CONF_VLDT_NUMBER \ - |NXT_CONF_VLDT_STRING \ - |NXT_CONF_VLDT_ARRAY \ - |NXT_CONF_VLDT_OBJECT) - +#define NXT_CONF_VLDT_ANY_TYPE \ + (NXT_CONF_VLDT_NULL | NXT_CONF_VLDT_BOOLEAN | NXT_CONF_VLDT_NUMBER \ + | NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY | NXT_CONF_VLDT_OBJECT) typedef enum { - NXT_CONF_VLDT_REQUIRED = 1 << 0, - NXT_CONF_VLDT_TSTR = 1 << 1, + NXT_CONF_VLDT_REQUIRED = 1 << 0, + NXT_CONF_VLDT_TSTR = 1 << 1, } nxt_conf_vldt_flags_t; - typedef nxt_int_t (*nxt_conf_vldt_handler_t)(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, - void *data); + nxt_conf_value_t *value, void *data); typedef nxt_int_t (*nxt_conf_vldt_member_t)(nxt_conf_validation_t *vldt, - nxt_str_t *name, - nxt_conf_value_t *value); + nxt_str_t *name, nxt_conf_value_t *value); typedef nxt_int_t (*nxt_conf_vldt_element_t)(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); + nxt_conf_value_t *value); -typedef struct nxt_conf_vldt_object_s nxt_conf_vldt_object_t; +typedef struct nxt_conf_vldt_object_s nxt_conf_vldt_object_t; struct nxt_conf_vldt_object_s { - nxt_str_t name; - nxt_conf_vldt_type_t type:32; - nxt_conf_vldt_flags_t flags:32; - nxt_conf_vldt_handler_t validator; + nxt_str_t name; + nxt_conf_vldt_type_t type :32; + nxt_conf_vldt_flags_t flags:32; + nxt_conf_vldt_handler_t validator; union { - nxt_conf_vldt_object_t *members; - nxt_conf_vldt_object_t *next; - nxt_conf_vldt_member_t object; - nxt_conf_vldt_element_t array; - const char *string; + nxt_conf_vldt_object_t *members; + nxt_conf_vldt_object_t *next; + nxt_conf_vldt_member_t object; + nxt_conf_vldt_element_t array; + const char *string; } u; }; +#define NXT_CONF_VLDT_NEXT(next) {.u.members = next} +#define NXT_CONF_VLDT_END {.name = nxt_null_string} -#define NXT_CONF_VLDT_NEXT(next) { .u.members = next } -#define NXT_CONF_VLDT_END { .name = nxt_null_string } +static nxt_int_t +nxt_conf_vldt_type(nxt_conf_validation_t *vldt, const nxt_str_t *name, + nxt_conf_value_t *value, nxt_conf_vldt_type_t type); +static nxt_int_t +nxt_conf_vldt_error(nxt_conf_validation_t *vldt, const char *fmt, ...); +static nxt_int_t +nxt_conf_vldt_var(nxt_conf_validation_t *vldt, const nxt_str_t *name, + nxt_str_t *value); +static nxt_int_t +nxt_conf_vldt_if(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +nxt_inline nxt_int_t +nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data) NXT_MAYBE_UNUSED; -static nxt_int_t nxt_conf_vldt_type(nxt_conf_validation_t *vldt, - const nxt_str_t *name, nxt_conf_value_t *value, nxt_conf_vldt_type_t type); -static nxt_int_t nxt_conf_vldt_error(nxt_conf_validation_t *vldt, - const char *fmt, ...); -static nxt_int_t nxt_conf_vldt_var(nxt_conf_validation_t *vldt, - const nxt_str_t *name, nxt_str_t *value); -static nxt_int_t nxt_conf_vldt_if(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -nxt_inline nxt_int_t nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) - NXT_MAYBE_UNUSED; - -static nxt_int_t nxt_conf_vldt_mtypes(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_mtypes_type(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_mtypes_extension(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_mtypes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_mtypes_type(nxt_conf_validation_t *vldt, nxt_str_t *name, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_mtypes_extension(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); #if (NXT_TLS) -static nxt_int_t nxt_conf_vldt_certificate(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); +static nxt_int_t +nxt_conf_vldt_certificate(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); #if (NXT_HAVE_OPENSSL_CONF_CMD) -static nxt_int_t nxt_conf_vldt_object_conf_commands(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_object_conf_commands(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); #endif -static nxt_int_t nxt_conf_vldt_certificate_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_tls_cache_size(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_tls_timeout(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_certificate_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_tls_cache_size(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); +static nxt_int_t +nxt_conf_vldt_tls_timeout(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); #if (NXT_HAVE_OPENSSL_TLSEXT) -static nxt_int_t nxt_conf_vldt_ticket_key(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_ticket_key_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_ticket_key(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_ticket_key_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); #endif #endif -static nxt_int_t nxt_conf_vldt_action(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_return(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_share(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_action(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_return(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_share(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_python(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_python_path(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_python_path_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_python_protocol(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_python_prefix(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_python(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_listen_threads(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_python_path(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_threads(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_thread_stack_size(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_python_path_element(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_routes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_routes_member(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_python_protocol(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_python_prefix(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_listen_threads(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_route(nxt_conf_validation_t *vldt, nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns_sets(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_threads(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns_set(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns_set_member(nxt_conf_validation_t *vldt, + nxt_str_t *name, nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_thread_stack_size(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_encoded_pattern(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_patterns(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_routes(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_pattern(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_patterns_sets(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_routes_member(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_patterns_set(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_match_patterns_set_member(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_route(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_encoded_patterns_sets( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_encoded_patterns_set( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_encoded_patterns_set_member( - nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_encoded_patterns( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_encoded_pattern( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_patterns(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_scheme_pattern(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_pattern(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_match_addrs(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_match_addr(nxt_conf_validation_t *vldt, nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_response_header(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_patterns_sets(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_forwarded(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_listen_backlog(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_patterns_set(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_patterns_set_member( - nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_match_scheme_pattern(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_processes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_addrs(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_array_iterator(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_match_addr(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_environment(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_response_header(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_forwarded(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_listen_backlog(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_app(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_object(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_processes(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_array_iterator(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_environment(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_targets_exclusive( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_targets(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_targets_exclusive(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_target(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_argument(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_targets(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_target(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_php(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_php_option(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_java_classpath(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_argument(nxt_conf_validation_t *vldt, nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_php(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_php_option(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_java_option(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_java_classpath(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_java_option(nxt_conf_validation_t *vldt, nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_upstream(nxt_conf_validation_t *vldt, nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_upstream(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_server(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_server(nxt_conf_validation_t *vldt, nxt_str_t *name, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); +static nxt_int_t +nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); -static nxt_int_t nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_clone_namespaces(nxt_conf_validation_t *vldt, +static nxt_int_t +nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_clone_namespaces(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data); #if (NXT_HAVE_CLONE_NEWUSER) -static nxt_int_t nxt_conf_vldt_clone_uidmap(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); -static nxt_int_t nxt_conf_vldt_clone_gidmap(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_clone_uidmap(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_clone_gidmap(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); #endif #if (NXT_HAVE_CGROUP) -static nxt_int_t nxt_conf_vldt_cgroup_path(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); +static nxt_int_t +nxt_conf_vldt_cgroup_path(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); #endif #if (NXT_HAVE_NJS) -static nxt_int_t nxt_conf_vldt_js_module(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data); -static nxt_int_t nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value); +static nxt_int_t +nxt_conf_vldt_js_module(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data); +static nxt_int_t +nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value); #endif -static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_http_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_websocket_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_static_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_forwarded_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_client_ip_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_http_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_websocket_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_static_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_forwarded_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_client_ip_members[]; #if (NXT_TLS) -static nxt_conf_vldt_object_t nxt_conf_vldt_tls_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_session_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_tls_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_session_members[]; #endif -static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_python_target_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_php_common_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[]; -static nxt_conf_vldt_object_t nxt_conf_vldt_app_namespaces_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_python_target_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_php_common_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_namespaces_members[]; #if (NXT_HAVE_CGROUP) -static nxt_conf_vldt_object_t nxt_conf_vldt_app_cgroup_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_cgroup_members[]; #endif #if (NXT_HAVE_ISOLATION_ROOTFS) -static nxt_conf_vldt_object_t nxt_conf_vldt_app_automount_members[]; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_automount_members[]; #endif -static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[]; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] = { - { - .name = nxt_string("settings"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_setting_members, - }, { - .name = nxt_string("listeners"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_listener, - }, { - .name = nxt_string("routes"), - .type = NXT_CONF_VLDT_ARRAY | NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_routes, - }, { - .name = nxt_string("applications"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_app, - }, { - .name = nxt_string("upstreams"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_upstream, - }, { - .name = nxt_string("access_log"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_access_log, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[] = { - { - .name = nxt_string("listen_threads"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_listen_threads, - }, { - .name = nxt_string("http"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_http_members, +static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[]; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] + = {{ + .name = nxt_string("settings"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_setting_members, + }, + { + .name = nxt_string("listeners"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_listener, + }, + { + .name = nxt_string("routes"), + .type = NXT_CONF_VLDT_ARRAY | NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_routes, + }, + { + .name = nxt_string("applications"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_app, + }, + { + .name = nxt_string("upstreams"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_upstream, + }, + { + .name = nxt_string("access_log"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_access_log, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[] + = {{ + .name = nxt_string("listen_threads"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_listen_threads, + }, + { + .name = nxt_string("http"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_http_members, #if (NXT_HAVE_NJS) - }, { - .name = nxt_string("js_module"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_js_module, + }, + { + .name = nxt_string("js_module"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_js_module, #endif - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_http_members[] = { - { - .name = nxt_string("header_read_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("body_read_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("send_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("idle_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("large_header_buffer_size"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("large_header_buffers"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("body_buffer_size"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("max_body_size"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("body_temp_path"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("discard_unsafe_fields"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("websocket"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_websocket_members, - }, { - .name = nxt_string("static"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_static_members, - }, { - .name = nxt_string("log_route"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("server_version"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("chunked_transform"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_websocket_members[] = { - { - .name = nxt_string("read_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - - .name = nxt_string("keepalive_interval"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("max_frame_size"), - .type = NXT_CONF_VLDT_INTEGER, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_static_members[] = { - { - .name = nxt_string("mime_types"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_mtypes, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_listener_members[] = { - { - .name = nxt_string("pass"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_pass, - .flags = NXT_CONF_VLDT_TSTR, - }, { - .name = nxt_string("application"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_app_name, - }, { - .name = nxt_string("forwarded"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_forwarded, - }, { - .name = nxt_string("client_ip"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_client_ip_members - }, { - .name = nxt_string("backlog"), - .type = NXT_CONF_VLDT_NUMBER, - .validator = nxt_conf_vldt_listen_backlog, - }, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_http_members[] + = {{ + .name = nxt_string("header_read_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("body_read_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("send_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("idle_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("large_header_buffer_size"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("large_header_buffers"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("body_buffer_size"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("max_body_size"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("body_temp_path"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("discard_unsafe_fields"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("websocket"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_websocket_members, + }, + { + .name = nxt_string("static"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_static_members, + }, + { + .name = nxt_string("log_route"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("server_version"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("chunked_transform"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_websocket_members[] + = {{ + .name = nxt_string("read_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + + .name = nxt_string("keepalive_interval"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("max_frame_size"), + .type = NXT_CONF_VLDT_INTEGER, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_static_members[] + = {{ + .name = nxt_string("mime_types"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_mtypes, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_listener_members[] + = {{ + .name = nxt_string("pass"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_pass, + .flags = NXT_CONF_VLDT_TSTR, + }, + { + .name = nxt_string("application"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_app_name, + }, + { + .name = nxt_string("forwarded"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_forwarded, + }, + {.name = nxt_string("client_ip"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_client_ip_members}, + { + .name = nxt_string("backlog"), + .type = NXT_CONF_VLDT_NUMBER, + .validator = nxt_conf_vldt_listen_backlog, + }, #if (NXT_TLS) - { - .name = nxt_string("tls"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_tls_members, - }, + { + .name = nxt_string("tls"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_tls_members, + }, #endif - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_forwarded_members[] = { - { - .name = nxt_string("client_ip"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("protocol"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("source"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_addrs, - .flags = NXT_CONF_VLDT_REQUIRED - }, { - .name = nxt_string("recursive"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_client_ip_members[] = { - { - .name = nxt_string("source"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_addrs, - .flags = NXT_CONF_VLDT_REQUIRED - }, { - .name = nxt_string("header"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED - }, { - .name = nxt_string("recursive"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, - - NXT_CONF_VLDT_END -}; + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_forwarded_members[] + = {{ + .name = nxt_string("client_ip"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("protocol"), + .type = NXT_CONF_VLDT_STRING, + }, + {.name = nxt_string("source"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_addrs, + .flags = NXT_CONF_VLDT_REQUIRED}, + { + .name = nxt_string("recursive"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_client_ip_members[] + = {{.name = nxt_string("source"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_addrs, + .flags = NXT_CONF_VLDT_REQUIRED}, + {.name = nxt_string("header"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED}, + { + .name = nxt_string("recursive"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + + NXT_CONF_VLDT_END}; #if (NXT_TLS) -static nxt_conf_vldt_object_t nxt_conf_vldt_tls_members[] = { - { - .name = nxt_string("certificate"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .flags = NXT_CONF_VLDT_REQUIRED, - .validator = nxt_conf_vldt_certificate, - }, { - .name = nxt_string("conf_commands"), - .type = NXT_CONF_VLDT_OBJECT, +static nxt_conf_vldt_object_t nxt_conf_vldt_tls_members[] + = {{ + .name = nxt_string("certificate"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .flags = NXT_CONF_VLDT_REQUIRED, + .validator = nxt_conf_vldt_certificate, + }, + { + .name = nxt_string("conf_commands"), + .type = NXT_CONF_VLDT_OBJECT, #if (NXT_HAVE_OPENSSL_CONF_CMD) - .validator = nxt_conf_vldt_object_conf_commands, + .validator = nxt_conf_vldt_object_conf_commands, #else - .validator = nxt_conf_vldt_unsupported, - .u.string = "conf_commands", + .validator = nxt_conf_vldt_unsupported, + .u.string = "conf_commands", #endif - }, { - .name = nxt_string("session"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_session_members, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_session_members[] = { - { - .name = nxt_string("cache_size"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_tls_cache_size, - }, { - .name = nxt_string("timeout"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_tls_timeout, - }, { - .name = nxt_string("tickets"), - .type = NXT_CONF_VLDT_STRING - | NXT_CONF_VLDT_ARRAY - | NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("session"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_session_members, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_session_members[] + = {{ + .name = nxt_string("cache_size"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_tls_cache_size, + }, + { + .name = nxt_string("timeout"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_tls_timeout, + }, + { + .name = nxt_string("tickets"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY + | NXT_CONF_VLDT_BOOLEAN, #if (NXT_HAVE_OPENSSL_TLSEXT) - .validator = nxt_conf_vldt_ticket_key, + .validator = nxt_conf_vldt_ticket_key, #else - .validator = nxt_conf_vldt_unsupported, - .u.string = "tickets", + .validator = nxt_conf_vldt_unsupported, + .u.string = "tickets", #endif - }, + }, - NXT_CONF_VLDT_END -}; + NXT_CONF_VLDT_END}; static nxt_int_t nxt_conf_vldt_tls_cache_size(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - int64_t cache_size; + nxt_conf_value_t *value, void *data) { + int64_t cache_size; cache_size = nxt_conf_get_number(value); if (cache_size < 0) { return nxt_conf_vldt_error(vldt, "The \"cache_size\" number must not " - "be negative."); - } - - return NXT_OK; -} - - -static nxt_int_t -nxt_conf_vldt_tls_timeout(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - int64_t timeout; - - timeout = nxt_conf_get_number(value); - - if (timeout <= 0) { - return nxt_conf_vldt_error(vldt, "The \"timeout\" number must be " - "greater than zero."); - } - - return NXT_OK; -} - -#endif - -#if (NXT_HAVE_OPENSSL_TLSEXT) - -static nxt_int_t -nxt_conf_vldt_ticket_key(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - if (nxt_conf_type(value) == NXT_CONF_BOOLEAN) { - return NXT_OK; - } - - if (nxt_conf_type(value) == NXT_CONF_ARRAY) { - return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_ticket_key_element); - } - - /* NXT_CONF_STRING */ - - return nxt_conf_vldt_ticket_key_element(vldt, value); -} - - -static nxt_int_t -nxt_conf_vldt_ticket_key_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - ssize_t ret; - nxt_str_t key; - - if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"key\" array must " - "contain only string values."); - } - - nxt_conf_get_string(value, &key); - - ret = nxt_base64_decode(NULL, key.start, key.length); - if (ret == NXT_ERROR) { - return nxt_conf_vldt_error(vldt, "Invalid Base64 format for the ticket " - "key \"%V\".", &key); - } - - if (ret != 48 && ret != 80) { - return nxt_conf_vldt_error(vldt, "Invalid length %d of the ticket " - "key \"%V\". Must be 48 or 80 bytes.", - ret, &key); - } - - return NXT_OK; -} - -#endif - - -static nxt_conf_vldt_object_t nxt_conf_vldt_route_members[] = { - { - .name = nxt_string("match"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_match_members, - }, { - .name = nxt_string("action"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_action, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[] = { - { - .name = nxt_string("method"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_patterns, - .u.string = "method", - }, { - .name = nxt_string("scheme"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_match_scheme_pattern, - }, { - .name = nxt_string("host"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_patterns, - .u.string = "host", - }, { - .name = nxt_string("source"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_addrs, - }, { - .name = nxt_string("destination"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_addrs, - }, { - .name = nxt_string("uri"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_encoded_patterns, - .u.string = "uri" - }, { - .name = nxt_string("query"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_encoded_patterns, - .u.string = "query" - }, { - .name = nxt_string("arguments"), - .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_encoded_patterns_sets, - }, { - .name = nxt_string("headers"), - .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_patterns_sets, - .u.string = "headers" - }, { - .name = nxt_string("cookies"), - .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_patterns_sets, - .u.string = "cookies" - }, { - .name = nxt_string("if"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_if, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_action_common_members[] = { - { - .name = nxt_string("rewrite"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_TSTR, - }, - { - .name = nxt_string("response_headers"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_response_header, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_pass_action_members[] = { - { - .name = nxt_string("pass"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_pass, - .flags = NXT_CONF_VLDT_TSTR, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_return_action_members[] = { - { - .name = nxt_string("return"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_return, - }, { - .name = nxt_string("location"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_TSTR, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_share_action_members[] = { - { - .name = nxt_string("share"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_share, - }, { - .name = nxt_string("index"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("types"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_match_patterns, - }, { - .name = nxt_string("fallback"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_action, - }, { - .name = nxt_string("chroot"), - .type = NXT_CONF_VLDT_STRING, -#if !(NXT_HAVE_OPENAT2) - .validator = nxt_conf_vldt_unsupported, - .u.string = "chroot", -#endif - .flags = NXT_CONF_VLDT_TSTR, - }, { - .name = nxt_string("follow_symlinks"), - .type = NXT_CONF_VLDT_BOOLEAN, -#if !(NXT_HAVE_OPENAT2) - .validator = nxt_conf_vldt_unsupported, - .u.string = "follow_symlinks", -#endif - }, { - .name = nxt_string("traverse_mounts"), - .type = NXT_CONF_VLDT_BOOLEAN, -#if !(NXT_HAVE_OPENAT2) - .validator = nxt_conf_vldt_unsupported, - .u.string = "traverse_mounts", -#endif - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_proxy_action_members[] = { - { - .name = nxt_string("proxy"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_proxy, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_external_members[] = { - { - .name = nxt_string("executable"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("arguments"), - .type = NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_array_iterator, - .u.array = nxt_conf_vldt_argument, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_python_common_members[] = { - { - .name = nxt_string("home"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("path"), - .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_python_path, - }, { - .name = nxt_string("protocol"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_python_protocol, - }, { - .name = nxt_string("threads"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_threads, - }, { - .name = nxt_string("thread_stack_size"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_thread_stack_size, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; - -static nxt_conf_vldt_object_t nxt_conf_vldt_python_members[] = { - { - .name = nxt_string("module"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "module", - }, { - .name = nxt_string("callable"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "callable", - }, { - .name = nxt_string("factory"), - .type = NXT_CONF_VLDT_BOOLEAN, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "factory", - }, { - .name = nxt_string("prefix"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "prefix", - }, { - .name = nxt_string("targets"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_targets, - .u.members = nxt_conf_vldt_python_target_members - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_python_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_python_target_members[] = { - { - .name = nxt_string("module"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("callable"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("factory"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("prefix"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_python_prefix, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_python_notargets_members[] = { - { - .name = nxt_string("module"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("callable"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("factory"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("prefix"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_python_prefix, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_python_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_php_members[] = { - { - .name = nxt_string("root"), - .type = NXT_CONF_VLDT_ANY_TYPE, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "root", - }, { - .name = nxt_string("script"), - .type = NXT_CONF_VLDT_ANY_TYPE, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "script", - }, { - .name = nxt_string("index"), - .type = NXT_CONF_VLDT_ANY_TYPE, - .validator = nxt_conf_vldt_targets_exclusive, - .u.string = "index", - }, { - .name = nxt_string("targets"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_targets, - .u.members = nxt_conf_vldt_php_target_members - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_php_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_php_common_members[] = { - { - .name = nxt_string("options"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_php_options_members, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[] = { - { - .name = nxt_string("file"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("admin"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_php_option, - }, { - .name = nxt_string("user"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_php_option, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[] = { - { - .name = nxt_string("root"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("script"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("index"), - .type = NXT_CONF_VLDT_STRING, - }, - - NXT_CONF_VLDT_END -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_php_notargets_members[] = { - { - .name = nxt_string("root"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("script"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("index"), - .type = NXT_CONF_VLDT_STRING, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_php_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_perl_members[] = { - { - .name = nxt_string("script"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("threads"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_threads, - }, { - .name = nxt_string("thread_stack_size"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_thread_stack_size, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_ruby_members[] = { - { - .name = nxt_string("script"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("threads"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_threads, - }, { - .name = nxt_string("hooks"), - .type = NXT_CONF_VLDT_STRING - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; - - -static nxt_conf_vldt_object_t nxt_conf_vldt_java_members[] = { - { - .name = nxt_string("classpath"), - .type = NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_array_iterator, - .u.array = nxt_conf_vldt_java_classpath, - }, { - .name = nxt_string("webapp"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("options"), - .type = NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_array_iterator, - .u.array = nxt_conf_vldt_java_option, - }, { - .name = nxt_string("unit_jars"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("threads"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_threads, - }, { - .name = nxt_string("thread_stack_size"), - .type = NXT_CONF_VLDT_INTEGER, - .validator = nxt_conf_vldt_thread_stack_size, - }, - - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; + "be negative."); + } + return NXT_OK; +} -static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_members[] = { - { - .name = nxt_string("module"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("request_handler"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - },{ - .name = nxt_string("malloc_handler"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("free_handler"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("module_init_handler"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("module_end_handler"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("request_init_handler"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("request_end_handler"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("response_end_handler"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("access"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_wasm_access_members, - }, +static nxt_int_t +nxt_conf_vldt_tls_timeout(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data) { + int64_t timeout; - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; + timeout = nxt_conf_get_number(value); + if (timeout <= 0) { + return nxt_conf_vldt_error(vldt, "The \"timeout\" number must be " + "greater than zero."); + } -static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_wc_members[] = { - { - .name = nxt_string("component"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("access"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_wasm_access_members, - }, + return NXT_OK; +} - NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members) -}; +#endif +#if (NXT_HAVE_OPENSSL_TLSEXT) -static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[] = { - { - .name = nxt_string("filesystem"), - .type = NXT_CONF_VLDT_ARRAY, - }, +static nxt_int_t +nxt_conf_vldt_ticket_key(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data) { + if (nxt_conf_type(value) == NXT_CONF_BOOLEAN) { + return NXT_OK; + } - NXT_CONF_VLDT_END -}; + if (nxt_conf_type(value) == NXT_CONF_ARRAY) { + return nxt_conf_vldt_array_iterator(vldt, value, + &nxt_conf_vldt_ticket_key_element); + } + /* NXT_CONF_STRING */ -static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = { - { - .name = nxt_string("type"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("limits"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_app_limits_members, - }, { - .name = nxt_string("processes"), - .type = NXT_CONF_VLDT_INTEGER | NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_processes, - .u.members = nxt_conf_vldt_app_processes_members, - }, { - .name = nxt_string("user"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("group"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("working_directory"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("environment"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_environment, - }, { - .name = nxt_string("isolation"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_isolation, - .u.members = nxt_conf_vldt_app_isolation_members, - }, { - .name = nxt_string("stdout"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("stderr"), - .type = NXT_CONF_VLDT_STRING, - }, + return nxt_conf_vldt_ticket_key_element(vldt, value); +} - NXT_CONF_VLDT_END -}; +static nxt_int_t +nxt_conf_vldt_ticket_key_element(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value) { + ssize_t ret; + nxt_str_t key; + if (nxt_conf_type(value) != NXT_CONF_STRING) { + return nxt_conf_vldt_error(vldt, "The \"key\" array must " + "contain only string values."); + } -static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[] = { - { - .name = nxt_string("timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("requests"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("shm"), - .type = NXT_CONF_VLDT_INTEGER, - }, + nxt_conf_get_string(value, &key); - NXT_CONF_VLDT_END -}; + ret = nxt_base64_decode(NULL, key.start, key.length); + if (ret == NXT_ERROR) { + return nxt_conf_vldt_error(vldt, + "Invalid Base64 format for the ticket " + "key \"%V\".", + &key); + } + if (ret != 48 && ret != 80) { + return nxt_conf_vldt_error(vldt, + "Invalid length %d of the ticket " + "key \"%V\". Must be 48 or 80 bytes.", + ret, &key); + } -static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[] = { - { - .name = nxt_string("spare"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("max"), - .type = NXT_CONF_VLDT_INTEGER, - }, { - .name = nxt_string("idle_timeout"), - .type = NXT_CONF_VLDT_INTEGER, - }, + return NXT_OK; +} - NXT_CONF_VLDT_END -}; +#endif -static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[] = { - { - .name = nxt_string("namespaces"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_clone_namespaces, - .u.members = nxt_conf_vldt_app_namespaces_members, - }, +static nxt_conf_vldt_object_t nxt_conf_vldt_route_members[] + = {{ + .name = nxt_string("match"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_match_members, + }, + { + .name = nxt_string("action"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_action, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_match_members[] + = {{ + .name = nxt_string("method"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns, + .u.string = "method", + }, + { + .name = nxt_string("scheme"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_match_scheme_pattern, + }, + { + .name = nxt_string("host"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns, + .u.string = "host", + }, + { + .name = nxt_string("source"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_addrs, + }, + { + .name = nxt_string("destination"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_addrs, + }, + {.name = nxt_string("uri"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_encoded_patterns, + .u.string = "uri"}, + {.name = nxt_string("query"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_encoded_patterns, + .u.string = "query"}, + { + .name = nxt_string("arguments"), + .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_encoded_patterns_sets, + }, + {.name = nxt_string("headers"), + .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns_sets, + .u.string = "headers"}, + {.name = nxt_string("cookies"), + .type = NXT_CONF_VLDT_OBJECT | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns_sets, + .u.string = "cookies"}, + { + .name = nxt_string("if"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_if, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_action_common_members[] + = {{ + .name = nxt_string("rewrite"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_TSTR, + }, + { + .name = nxt_string("response_headers"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_response_header, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_pass_action_members[] + = {{ + .name = nxt_string("pass"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_pass, + .flags = NXT_CONF_VLDT_TSTR, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_return_action_members[] + = {{ + .name = nxt_string("return"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_return, + }, + { + .name = nxt_string("location"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_TSTR, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_share_action_members[] + = {{ + .name = nxt_string("share"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_share, + }, + { + .name = nxt_string("index"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("types"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_match_patterns, + }, + { + .name = nxt_string("fallback"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_action, + }, + { + .name = nxt_string("chroot"), + .type = NXT_CONF_VLDT_STRING, +#if !(NXT_HAVE_OPENAT2) + .validator = nxt_conf_vldt_unsupported, + .u.string = "chroot", +#endif + .flags = NXT_CONF_VLDT_TSTR, + }, + { + .name = nxt_string("follow_symlinks"), + .type = NXT_CONF_VLDT_BOOLEAN, +#if !(NXT_HAVE_OPENAT2) + .validator = nxt_conf_vldt_unsupported, + .u.string = "follow_symlinks", +#endif + }, + { + .name = nxt_string("traverse_mounts"), + .type = NXT_CONF_VLDT_BOOLEAN, +#if !(NXT_HAVE_OPENAT2) + .validator = nxt_conf_vldt_unsupported, + .u.string = "traverse_mounts", +#endif + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_proxy_action_members[] + = {{ + .name = nxt_string("proxy"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_proxy, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_action_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_external_members[] + = {{ + .name = nxt_string("executable"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("arguments"), + .type = NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_array_iterator, + .u.array = nxt_conf_vldt_argument, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_python_common_members[] + = {{ + .name = nxt_string("home"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("path"), + .type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_python_path, + }, + { + .name = nxt_string("protocol"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_python_protocol, + }, + { + .name = nxt_string("threads"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_threads, + }, + { + .name = nxt_string("thread_stack_size"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_thread_stack_size, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + +static nxt_conf_vldt_object_t nxt_conf_vldt_python_members[] + = {{ + .name = nxt_string("module"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "module", + }, + { + .name = nxt_string("callable"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "callable", + }, + { + .name = nxt_string("factory"), + .type = NXT_CONF_VLDT_BOOLEAN, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "factory", + }, + { + .name = nxt_string("prefix"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "prefix", + }, + {.name = nxt_string("targets"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_targets, + .u.members = nxt_conf_vldt_python_target_members}, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_python_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_python_target_members[] + = {{ + .name = nxt_string("module"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("callable"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("factory"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("prefix"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_python_prefix, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_python_notargets_members[] + = {{ + .name = nxt_string("module"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("callable"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("factory"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("prefix"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_python_prefix, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_python_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_php_members[] + = {{ + .name = nxt_string("root"), + .type = NXT_CONF_VLDT_ANY_TYPE, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "root", + }, + { + .name = nxt_string("script"), + .type = NXT_CONF_VLDT_ANY_TYPE, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "script", + }, + { + .name = nxt_string("index"), + .type = NXT_CONF_VLDT_ANY_TYPE, + .validator = nxt_conf_vldt_targets_exclusive, + .u.string = "index", + }, + {.name = nxt_string("targets"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_targets, + .u.members = nxt_conf_vldt_php_target_members}, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_php_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_php_common_members[] + = {{ + .name = nxt_string("options"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_php_options_members, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[] + = {{ + .name = nxt_string("file"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("admin"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_php_option, + }, + { + .name = nxt_string("user"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_php_option, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[] + = {{ + .name = nxt_string("root"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("script"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("index"), + .type = NXT_CONF_VLDT_STRING, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_php_notargets_members[] + = {{ + .name = nxt_string("root"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("script"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("index"), + .type = NXT_CONF_VLDT_STRING, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_php_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_perl_members[] + = {{ + .name = nxt_string("script"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("threads"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_threads, + }, + { + .name = nxt_string("thread_stack_size"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_thread_stack_size, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_ruby_members[] + = {{ + .name = nxt_string("script"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("threads"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_threads, + }, + {.name = nxt_string("hooks"), .type = NXT_CONF_VLDT_STRING}, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_java_members[] + = {{ + .name = nxt_string("classpath"), + .type = NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_array_iterator, + .u.array = nxt_conf_vldt_java_classpath, + }, + { + .name = nxt_string("webapp"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("options"), + .type = NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_array_iterator, + .u.array = nxt_conf_vldt_java_option, + }, + { + .name = nxt_string("unit_jars"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("threads"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_threads, + }, + { + .name = nxt_string("thread_stack_size"), + .type = NXT_CONF_VLDT_INTEGER, + .validator = nxt_conf_vldt_thread_stack_size, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_members[] + = {{ + .name = nxt_string("module"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("request_handler"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("malloc_handler"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("free_handler"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("module_init_handler"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("module_end_handler"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("request_init_handler"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("request_end_handler"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("response_end_handler"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("access"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_wasm_access_members, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_wc_members[] + = {{ + .name = nxt_string("component"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("access"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_wasm_access_members, + }, + + NXT_CONF_VLDT_NEXT(nxt_conf_vldt_common_members)}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[] + = {{ + .name = nxt_string("filesystem"), + .type = NXT_CONF_VLDT_ARRAY, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] + = {{ + .name = nxt_string("type"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("limits"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_app_limits_members, + }, + { + .name = nxt_string("processes"), + .type = NXT_CONF_VLDT_INTEGER | NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_processes, + .u.members = nxt_conf_vldt_app_processes_members, + }, + { + .name = nxt_string("user"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("group"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("working_directory"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("environment"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_environment, + }, + { + .name = nxt_string("isolation"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_isolation, + .u.members = nxt_conf_vldt_app_isolation_members, + }, + { + .name = nxt_string("stdout"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("stderr"), + .type = NXT_CONF_VLDT_STRING, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[] + = {{ + .name = nxt_string("timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("requests"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("shm"), + .type = NXT_CONF_VLDT_INTEGER, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[] + = {{ + .name = nxt_string("spare"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("max"), + .type = NXT_CONF_VLDT_INTEGER, + }, + { + .name = nxt_string("idle_timeout"), + .type = NXT_CONF_VLDT_INTEGER, + }, + + NXT_CONF_VLDT_END}; + + +static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[] + = {{ + .name = nxt_string("namespaces"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_clone_namespaces, + .u.members = nxt_conf_vldt_app_namespaces_members, + }, #if (NXT_HAVE_CLONE_NEWUSER) - { - .name = nxt_string("uidmap"), - .type = NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_array_iterator, - .u.array = nxt_conf_vldt_clone_uidmap, - }, { - .name = nxt_string("gidmap"), - .type = NXT_CONF_VLDT_ARRAY, - .validator = nxt_conf_vldt_array_iterator, - .u.array = nxt_conf_vldt_clone_gidmap, - }, + { + .name = nxt_string("uidmap"), + .type = NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_array_iterator, + .u.array = nxt_conf_vldt_clone_uidmap, + }, + { + .name = nxt_string("gidmap"), + .type = NXT_CONF_VLDT_ARRAY, + .validator = nxt_conf_vldt_array_iterator, + .u.array = nxt_conf_vldt_clone_gidmap, + }, #endif #if (NXT_HAVE_ISOLATION_ROOTFS) - { - .name = nxt_string("rootfs"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("automount"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_app_automount_members, - }, + { + .name = nxt_string("rootfs"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("automount"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_app_automount_members, + }, #endif #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) - { - .name = nxt_string("new_privs"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, + { + .name = nxt_string("new_privs"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, #endif #if (NXT_HAVE_CGROUP) - { - .name = nxt_string("cgroup"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object, - .u.members = nxt_conf_vldt_app_cgroup_members, - }, + { + .name = nxt_string("cgroup"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object, + .u.members = nxt_conf_vldt_app_cgroup_members, + }, #endif - NXT_CONF_VLDT_END -}; + NXT_CONF_VLDT_END}; -static nxt_conf_vldt_object_t nxt_conf_vldt_app_namespaces_members[] = { +static nxt_conf_vldt_object_t nxt_conf_vldt_app_namespaces_members[] = { #if (NXT_HAVE_CLONE_NEWUSER) { - .name = nxt_string("credential"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("credential"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif #if (NXT_HAVE_CLONE_NEWPID) { - .name = nxt_string("pid"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("pid"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif #if (NXT_HAVE_CLONE_NEWNET) { - .name = nxt_string("network"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("network"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif #if (NXT_HAVE_CLONE_NEWNS) { - .name = nxt_string("mount"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("mount"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif #if (NXT_HAVE_CLONE_NEWUTS) { - .name = nxt_string("uname"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("uname"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif #if (NXT_HAVE_CLONE_NEWCGROUP) { - .name = nxt_string("cgroup"), - .type = NXT_CONF_VLDT_BOOLEAN, + .name = nxt_string("cgroup"), + .type = NXT_CONF_VLDT_BOOLEAN, }, #endif - NXT_CONF_VLDT_END -}; + NXT_CONF_VLDT_END}; #if (NXT_HAVE_ISOLATION_ROOTFS) -static nxt_conf_vldt_object_t nxt_conf_vldt_app_automount_members[] = { - { - .name = nxt_string("language_deps"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("tmpfs"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, { - .name = nxt_string("procfs"), - .type = NXT_CONF_VLDT_BOOLEAN, - }, - - NXT_CONF_VLDT_END -}; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_automount_members[] + = {{ + .name = nxt_string("language_deps"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("tmpfs"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + { + .name = nxt_string("procfs"), + .type = NXT_CONF_VLDT_BOOLEAN, + }, + + NXT_CONF_VLDT_END}; #endif #if (NXT_HAVE_CGROUP) -static nxt_conf_vldt_object_t nxt_conf_vldt_app_cgroup_members[] = { - { - .name = nxt_string("path"), - .type = NXT_CONF_VLDT_STRING, - .flags = NXT_CONF_VLDT_REQUIRED, - .validator = nxt_conf_vldt_cgroup_path, - }, +static nxt_conf_vldt_object_t nxt_conf_vldt_app_cgroup_members[] + = {{ + .name = nxt_string("path"), + .type = NXT_CONF_VLDT_STRING, + .flags = NXT_CONF_VLDT_REQUIRED, + .validator = nxt_conf_vldt_cgroup_path, + }, - NXT_CONF_VLDT_END -}; + NXT_CONF_VLDT_END}; #endif #if (NXT_HAVE_CLONE_NEWUSER) -static nxt_conf_vldt_object_t nxt_conf_vldt_app_procmap_members[] = { - { - .name = nxt_string("container"), - .type = NXT_CONF_VLDT_INTEGER, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("host"), - .type = NXT_CONF_VLDT_INTEGER, - .flags = NXT_CONF_VLDT_REQUIRED, - }, { - .name = nxt_string("size"), - .type = NXT_CONF_VLDT_INTEGER, - .flags = NXT_CONF_VLDT_REQUIRED, - }, - - NXT_CONF_VLDT_END -}; +static nxt_conf_vldt_object_t nxt_conf_vldt_app_procmap_members[] + = {{ + .name = nxt_string("container"), + .type = NXT_CONF_VLDT_INTEGER, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("host"), + .type = NXT_CONF_VLDT_INTEGER, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + { + .name = nxt_string("size"), + .type = NXT_CONF_VLDT_INTEGER, + .flags = NXT_CONF_VLDT_REQUIRED, + }, + + NXT_CONF_VLDT_END}; #endif -static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_members[] = { - { - .name = nxt_string("servers"), - .type = NXT_CONF_VLDT_OBJECT, - .validator = nxt_conf_vldt_object_iterator, - .u.object = nxt_conf_vldt_server, - }, - - NXT_CONF_VLDT_END -}; +static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_members[] + = {{ + .name = nxt_string("servers"), + .type = NXT_CONF_VLDT_OBJECT, + .validator = nxt_conf_vldt_object_iterator, + .u.object = nxt_conf_vldt_server, + }, + NXT_CONF_VLDT_END}; -static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_server_members[] = { - { - .name = nxt_string("weight"), - .type = NXT_CONF_VLDT_NUMBER, - .validator = nxt_conf_vldt_server_weight, - }, - NXT_CONF_VLDT_END -}; +static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_server_members[] + = {{ + .name = nxt_string("weight"), + .type = NXT_CONF_VLDT_NUMBER, + .validator = nxt_conf_vldt_server_weight, + }, + NXT_CONF_VLDT_END}; -static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] = { - { - .name = nxt_string("path"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("format"), - .type = NXT_CONF_VLDT_STRING, - }, { - .name = nxt_string("if"), - .type = NXT_CONF_VLDT_STRING, - .validator = nxt_conf_vldt_if, - }, - NXT_CONF_VLDT_END -}; +static nxt_conf_vldt_object_t nxt_conf_vldt_access_log_members[] + = {{ + .name = nxt_string("path"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("format"), + .type = NXT_CONF_VLDT_STRING, + }, + { + .name = nxt_string("if"), + .type = NXT_CONF_VLDT_STRING, + .validator = nxt_conf_vldt_if, + }, + NXT_CONF_VLDT_END}; nxt_int_t -nxt_conf_validate(nxt_conf_validation_t *vldt) -{ - nxt_int_t ret; - u_char error[NXT_MAX_ERROR_STR]; +nxt_conf_validate(nxt_conf_validation_t *vldt) { + nxt_int_t ret; + u_char error[NXT_MAX_ERROR_STR]; vldt->tstr_state = nxt_tstr_state_new(vldt->pool, 1); if (nxt_slow_path(vldt->tstr_state == NULL)) { @@ -1459,23 +1563,20 @@ nxt_conf_validate(nxt_conf_validation_t *vldt) return NXT_OK; } - -#define NXT_CONF_VLDT_ANY_TYPE_STR \ - "either a null, a boolean, an integer, " \ +#define NXT_CONF_VLDT_ANY_TYPE_STR \ + "either a null, a boolean, an integer, " \ "a number, a string, an array, or an object" - static nxt_int_t nxt_conf_vldt_type(nxt_conf_validation_t *vldt, const nxt_str_t *name, - nxt_conf_value_t *value, nxt_conf_vldt_type_t type) -{ - u_char *p; - nxt_str_t expected; - nxt_bool_t comma; - nxt_uint_t value_type, n, t; - u_char buf[nxt_length(NXT_CONF_VLDT_ANY_TYPE_STR)]; - - static const nxt_str_t type_name[] = { + nxt_conf_value_t *value, nxt_conf_vldt_type_t type) { + u_char *p; + nxt_str_t expected; + nxt_bool_t comma; + nxt_uint_t value_type, n, t; + u_char buf[nxt_length(NXT_CONF_VLDT_ANY_TYPE_STR)]; + + static const nxt_str_t type_name[] = { nxt_string("a null"), nxt_string("a boolean"), nxt_string("an integer number"), @@ -1501,7 +1602,7 @@ nxt_conf_vldt_type(nxt_conf_validation_t *vldt, const nxt_str_t *name, comma = (n > 2); - for ( ;; ) { + for (;;) { t = __builtin_ffs(type) - 1; p = nxt_cpymem(p, type_name[t].start, type_name[t].length); @@ -1526,27 +1627,24 @@ nxt_conf_vldt_type(nxt_conf_validation_t *vldt, const nxt_str_t *name, } expected.length = p - buf; - expected.start = buf; + expected.start = buf; if (name == NULL) { return nxt_conf_vldt_error(vldt, - "The configuration must be %V, but not %V.", - &expected, &type_name[value_type]); + "The configuration must be %V, but not %V.", &expected, + &type_name[value_type]); } - return nxt_conf_vldt_error(vldt, - "The \"%V\" value must be %V, but not %V.", - name, &expected, &type_name[value_type]); + return nxt_conf_vldt_error(vldt, "The \"%V\" value must be %V, but not %V.", + name, &expected, &type_name[value_type]); } - static nxt_int_t -nxt_conf_vldt_error(nxt_conf_validation_t *vldt, const char *fmt, ...) -{ - u_char *p, *end; - size_t size; - va_list args; - u_char error[NXT_MAX_ERROR_STR]; +nxt_conf_vldt_error(nxt_conf_validation_t *vldt, const char *fmt, ...) { + u_char *p, *end; + size_t size; + va_list args; + u_char error[NXT_MAX_ERROR_STR]; va_start(args, fmt); end = nxt_vsprintf(error, error + NXT_MAX_ERROR_STR, fmt, args); @@ -1562,43 +1660,39 @@ nxt_conf_vldt_error(nxt_conf_validation_t *vldt, const char *fmt, ...) nxt_memcpy(p, error, size); vldt->error.length = size; - vldt->error.start = p; + vldt->error.start = p; return NXT_DECLINED; } - nxt_inline nxt_int_t nxt_conf_vldt_unsupported(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - return nxt_conf_vldt_error(vldt, "Unit is built without the \"%s\" " - "option support.", data); + void *data) { + return nxt_conf_vldt_error(vldt, + "Unit is built without the \"%s\" " + "option support.", + data); } - static nxt_int_t nxt_conf_vldt_var(nxt_conf_validation_t *vldt, const nxt_str_t *name, - nxt_str_t *value) -{ - u_char error[NXT_MAX_ERROR_STR]; + nxt_str_t *value) { + u_char error[NXT_MAX_ERROR_STR]; if (nxt_tstr_test(vldt->tstr_state, value, error) != NXT_OK) { - return nxt_conf_vldt_error(vldt, "%s in the \"%V\" value.", - error, name); + return nxt_conf_vldt_error(vldt, "%s in the \"%V\" value.", error, + name); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_if(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_str_t str; + void *data) { + nxt_str_t str; - static const nxt_str_t if_str = nxt_string("if"); + static const nxt_str_t if_str = nxt_string("if"); if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"if\" must be a string"); @@ -1622,20 +1716,17 @@ nxt_conf_vldt_if(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return NXT_OK; } - typedef struct { - nxt_mp_t *pool; - nxt_str_t *type; - nxt_lvlhsh_t hash; + nxt_mp_t *pool; + nxt_str_t *type; + nxt_lvlhsh_t hash; } nxt_conf_vldt_mtypes_ctx_t; - static nxt_int_t nxt_conf_vldt_mtypes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_int_t ret; - nxt_conf_vldt_mtypes_ctx_t ctx; + void *data) { + nxt_int_t ret; + nxt_conf_vldt_mtypes_ctx_t ctx; ctx.pool = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(ctx.pool == NULL)) { @@ -1647,7 +1738,7 @@ nxt_conf_vldt_mtypes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, vldt->ctx = &ctx; ret = nxt_conf_vldt_object_iterator(vldt, value, - &nxt_conf_vldt_mtypes_type); + &nxt_conf_vldt_mtypes_type); vldt->ctx = NULL; @@ -1656,16 +1747,14 @@ nxt_conf_vldt_mtypes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return ret; } - static nxt_int_t nxt_conf_vldt_mtypes_type(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_int_t ret; - nxt_conf_vldt_mtypes_ctx_t *ctx; + nxt_conf_vldt_mtypes_ctx_t *ctx; ret = nxt_conf_vldt_type(vldt, name, value, - NXT_CONF_VLDT_STRING|NXT_CONF_VLDT_ARRAY); + NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY); if (ret != NXT_OK) { return ret; } @@ -1681,7 +1770,7 @@ nxt_conf_vldt_mtypes_type(nxt_conf_validation_t *vldt, nxt_str_t *name, if (nxt_conf_type(value) == NXT_CONF_ARRAY) { return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_mtypes_extension); + &nxt_conf_vldt_mtypes_extension); } /* NXT_CONF_STRING */ @@ -1689,49 +1778,50 @@ nxt_conf_vldt_mtypes_type(nxt_conf_validation_t *vldt, nxt_str_t *name, return nxt_conf_vldt_mtypes_extension(vldt, value); } - static nxt_int_t nxt_conf_vldt_mtypes_extension(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_str_t exten, *dup_type; - nxt_conf_vldt_mtypes_ctx_t *ctx; + nxt_conf_vldt_mtypes_ctx_t *ctx; ctx = vldt->ctx; if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"%V\" MIME type array must " - "contain only strings.", ctx->type); + return nxt_conf_vldt_error(vldt, + "The \"%V\" MIME type array must " + "contain only strings.", + ctx->type); } nxt_conf_get_string(value, &exten); if (exten.length == 0) { - return nxt_conf_vldt_error(vldt, "An empty file extension for " - "the \"%V\" MIME type.", ctx->type); + return nxt_conf_vldt_error(vldt, + "An empty file extension for " + "the \"%V\" MIME type.", + ctx->type); } dup_type = nxt_http_static_mtype_get(&ctx->hash, &exten); if (dup_type->length != 0) { - return nxt_conf_vldt_error(vldt, "The \"%V\" file extension has been " - "declared for \"%V\" and \"%V\" " - "MIME types at the same time.", - &exten, dup_type, ctx->type); + return nxt_conf_vldt_error(vldt, + "The \"%V\" file extension has been " + "declared for \"%V\" and \"%V\" " + "MIME types at the same time.", + &exten, dup_type, ctx->type); } return nxt_http_static_mtypes_hash_add(ctx->pool, &ctx->hash, &exten, - ctx->type); + ctx->type); } - static nxt_int_t nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_int_t ret; nxt_str_t str; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; if (nxt_slow_path(nxt_str_dup(vldt->pool, &str, name) == NULL)) { return NXT_ERROR; @@ -1740,8 +1830,7 @@ nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, nxt_str_t *name, sa = nxt_sockaddr_parse(vldt->pool, &str); if (nxt_slow_path(sa == NULL)) { return nxt_conf_vldt_error(vldt, - "The listener address \"%V\" is invalid.", - name); + "The listener address \"%V\" is invalid.", name); } ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT); @@ -1752,24 +1841,22 @@ nxt_conf_vldt_listener(nxt_conf_validation_t *vldt, nxt_str_t *name, return nxt_conf_vldt_object(vldt, value, nxt_conf_vldt_listener_members); } - static nxt_int_t nxt_conf_vldt_action(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { nxt_uint_t i; - nxt_conf_value_t *action; - nxt_conf_vldt_object_t *members; + nxt_conf_value_t *action; + nxt_conf_vldt_object_t *members; static const struct { nxt_str_t name; - nxt_conf_vldt_object_t *members; + nxt_conf_vldt_object_t *members; } actions[] = { - { nxt_string("pass"), nxt_conf_vldt_pass_action_members }, - { nxt_string("return"), nxt_conf_vldt_return_action_members }, - { nxt_string("share"), nxt_conf_vldt_share_action_members }, - { nxt_string("proxy"), nxt_conf_vldt_proxy_action_members }, + {nxt_string("pass"), nxt_conf_vldt_pass_action_members}, + {nxt_string("return"), nxt_conf_vldt_return_action_members}, + {nxt_string("share"), nxt_conf_vldt_share_action_members}, + {nxt_string("proxy"), nxt_conf_vldt_proxy_action_members}, }; members = NULL; @@ -1782,33 +1869,33 @@ nxt_conf_vldt_action(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } if (members != NULL) { - return nxt_conf_vldt_error(vldt, "The \"action\" object must have " - "just one of \"pass\", \"return\", " - "\"share\", or \"proxy\" options set."); + return nxt_conf_vldt_error(vldt, + "The \"action\" object must have " + "just one of \"pass\", \"return\", " + "\"share\", or \"proxy\" options set."); } members = actions[i].members; } if (members == NULL) { - return nxt_conf_vldt_error(vldt, "The \"action\" object must have " - "either \"pass\", \"return\", \"share\", " - "or \"proxy\" option set."); + return nxt_conf_vldt_error(vldt, + "The \"action\" object must have " + "either \"pass\", \"return\", \"share\", " + "or \"proxy\" option set."); } return nxt_conf_vldt_object(vldt, value, members); } - static nxt_int_t nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_str_t pass; - nxt_int_t ret; - nxt_str_t segments[3]; + void *data) { + nxt_str_t pass; + nxt_int_t ret; + nxt_str_t segments[3]; - static const nxt_str_t targets_str = nxt_string("targets"); + static const nxt_str_t targets_str = nxt_string("targets"); nxt_conf_get_string(value, &pass); @@ -1816,15 +1903,16 @@ nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, if (ret != NXT_OK) { if (ret == NXT_DECLINED) { - return nxt_conf_vldt_error(vldt, "Request \"pass\" value \"%V\" " - "is invalid.", &pass); + return nxt_conf_vldt_error(vldt, + "Request \"pass\" value \"%V\" " + "is invalid.", + &pass); } return NXT_ERROR; } if (nxt_str_eq(&segments[0], "applications", 12)) { - if (segments[1].length == 0) { goto error; } @@ -1859,7 +1947,6 @@ nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } if (nxt_str_eq(&segments[0], "upstreams", 9)) { - if (segments[1].length == 0 || segments[2].length != 0) { goto error; } @@ -1880,7 +1967,6 @@ nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } if (nxt_str_eq(&segments[0], "routes", 6)) { - if (segments[2].length != 0) { goto error; } @@ -1914,40 +2000,40 @@ nxt_conf_vldt_pass(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, error: - return nxt_conf_vldt_error(vldt, "Request \"pass\" points to invalid " - "location \"%V\".", &pass); + return nxt_conf_vldt_error(vldt, + "Request \"pass\" points to invalid " + "location \"%V\".", + &pass); } - static nxt_int_t nxt_conf_vldt_return(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - int64_t status; + void *data) { + int64_t status; status = nxt_conf_get_number(value); if (status < NXT_HTTP_INVALID || status > NXT_HTTP_STATUS_MAX) { - return nxt_conf_vldt_error(vldt, "The \"return\" value is out of " - "allowed HTTP status code range 0-999."); + return nxt_conf_vldt_error(vldt, + "The \"return\" value is out of " + "allowed HTTP status code range 0-999."); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_share(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { if (nxt_conf_array_elements_count(value) == 0) { - return nxt_conf_vldt_error(vldt, "The \"share\" array " - "must contain at least one element."); + return nxt_conf_vldt_error(vldt, + "The \"share\" array " + "must contain at least one element."); } return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_share_element); + &nxt_conf_vldt_share_element); } /* NXT_CONF_STRING */ @@ -1955,18 +2041,16 @@ nxt_conf_vldt_share(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return nxt_conf_vldt_share_element(vldt, value); } - static nxt_int_t nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - nxt_str_t str; + nxt_conf_value_t *value) { + nxt_str_t str; - static const nxt_str_t share = nxt_string("share"); + static const nxt_str_t share = nxt_string("share"); if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"share\" array must " - "contain only string values."); + "contain only string values."); } nxt_conf_get_string(value, &str); @@ -1978,13 +2062,11 @@ nxt_conf_vldt_share_element(nxt_conf_validation_t *vldt, return NXT_OK; } - static nxt_int_t nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { nxt_str_t name, *ret; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; ret = nxt_conf_get_string_dup(value, vldt->pool, &name); if (nxt_slow_path(ret == NULL)) { @@ -1993,7 +2075,7 @@ nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, if (nxt_str_start(&name, "http://", 7)) { name.length -= 7; - name.start += 7; + name.start += 7; sa = nxt_sockaddr_parse(vldt->pool, &name); if (sa != NULL) { @@ -2002,17 +2084,15 @@ nxt_conf_vldt_proxy(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } return nxt_conf_vldt_error(vldt, "The \"proxy\" address is invalid \"%V\"", - &name); + &name); } - static nxt_int_t nxt_conf_vldt_python(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_conf_value_t *targets; + void *data) { + nxt_conf_value_t *targets; - static const nxt_str_t targets_str = nxt_string("targets"); + static const nxt_str_t targets_str = nxt_string("targets"); targets = nxt_conf_get_object_member(value, &targets_str, NULL); @@ -2021,17 +2101,15 @@ nxt_conf_vldt_python(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } return nxt_conf_vldt_object(vldt, value, - nxt_conf_vldt_python_notargets_members); + nxt_conf_vldt_python_notargets_members); } - static nxt_int_t -nxt_conf_vldt_python_path(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ +nxt_conf_vldt_python_path(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_python_path_element); + &nxt_conf_vldt_python_path_element); } /* NXT_CONF_STRING */ @@ -2039,28 +2117,24 @@ nxt_conf_vldt_python_path(nxt_conf_validation_t *vldt, return NXT_OK; } - static nxt_int_t nxt_conf_vldt_python_path_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"path\" array must contain " - "only string values."); + "only string values."); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_python_protocol(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_str_t proto; + nxt_conf_value_t *value, void *data) { + nxt_str_t proto; - static const nxt_str_t wsgi = nxt_string("wsgi"); - static const nxt_str_t asgi = nxt_string("asgi"); + static const nxt_str_t wsgi = nxt_string("wsgi"); + static const nxt_str_t asgi = nxt_string("asgi"); nxt_conf_get_string(value, &proto); @@ -2072,23 +2146,21 @@ nxt_conf_vldt_python_protocol(nxt_conf_validation_t *vldt, "\"wsgi\" or \"asgi\"."); } - static nxt_int_t nxt_conf_vldt_python_prefix(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_str_t prefix; + nxt_conf_value_t *value, void *data) { + nxt_str_t prefix; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"prefix\" must be a string " - "beginning with \"/\"."); + "beginning with \"/\"."); } nxt_conf_get_string(value, &prefix); if (!nxt_strchr_start(&prefix, '/')) { return nxt_conf_vldt_error(vldt, "The \"prefix\" must be a string " - "beginning with \"/\"."); + "beginning with \"/\"."); } return NXT_OK; @@ -2096,94 +2168,90 @@ nxt_conf_vldt_python_prefix(nxt_conf_validation_t *vldt, static nxt_int_t nxt_conf_vldt_listen_threads(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - int64_t threads; + nxt_conf_value_t *value, void *data) { + int64_t threads; threads = nxt_conf_get_number(value); if (threads < 1) { return nxt_conf_vldt_error(vldt, "The \"listen_threads\" number must " - "be equal to or greater than 1."); + "be equal to or greater than 1."); } if (threads > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"listen_threads\" number must " - "not exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"listen_threads\" number must " + "not exceed %d.", + NXT_INT32_T_MAX); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_threads(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - int64_t threads; + void *data) { + int64_t threads; threads = nxt_conf_get_number(value); if (threads < 1) { return nxt_conf_vldt_error(vldt, "The \"threads\" number must be " - "equal to or greater than 1."); + "equal to or greater than 1."); } if (threads > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"threads\" number must " - "not exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"threads\" number must " + "not exceed %d.", + NXT_INT32_T_MAX); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_thread_stack_size(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - int64_t size, min_size; + nxt_conf_value_t *value, void *data) { + int64_t size, min_size; - size = nxt_conf_get_number(value); + size = nxt_conf_get_number(value); min_size = sysconf(_SC_THREAD_STACK_MIN); if (size < min_size) { - return nxt_conf_vldt_error(vldt, "The \"thread_stack_size\" number " - "must be equal to or greater than %d.", - min_size); + return nxt_conf_vldt_error(vldt, + "The \"thread_stack_size\" number " + "must be equal to or greater than %d.", + min_size); } if ((size % nxt_pagesize) != 0) { - return nxt_conf_vldt_error(vldt, "The \"thread_stack_size\" number " - "must be a multiple of the system page size (%d).", - nxt_pagesize); + return nxt_conf_vldt_error(vldt, + "The \"thread_stack_size\" number " + "must be a multiple of the system page size (%d).", + nxt_pagesize); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_routes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { - return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_route); + return nxt_conf_vldt_array_iterator(vldt, value, &nxt_conf_vldt_route); } /* NXT_CONF_OBJECT */ return nxt_conf_vldt_object_iterator(vldt, value, - &nxt_conf_vldt_routes_member); + &nxt_conf_vldt_routes_member); } - static nxt_int_t nxt_conf_vldt_routes_member(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ - nxt_int_t ret; + nxt_conf_value_t *value) { + nxt_int_t ret; ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_ARRAY); @@ -2194,30 +2262,26 @@ nxt_conf_vldt_routes_member(nxt_conf_validation_t *vldt, nxt_str_t *name, return nxt_conf_vldt_array_iterator(vldt, value, &nxt_conf_vldt_route); } - static nxt_int_t -nxt_conf_vldt_route(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ +nxt_conf_vldt_route(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) { if (nxt_conf_type(value) != NXT_CONF_OBJECT) { return nxt_conf_vldt_error(vldt, "The \"routes\" array must contain " - "only object values."); + "only object values."); } return nxt_conf_vldt_object(vldt, value, nxt_conf_vldt_route_members); } - static nxt_int_t nxt_conf_vldt_match_patterns(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_int_t ret; + nxt_conf_value_t *value, void *data) { + nxt_int_t ret; vldt->ctx = data; if (nxt_conf_type(value) == NXT_CONF_ARRAY) { ret = nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_match_pattern); + &nxt_conf_vldt_match_pattern); } else { /* NXT_CONF_STRING */ @@ -2229,21 +2293,21 @@ nxt_conf_vldt_match_patterns(nxt_conf_validation_t *vldt, return ret; } - static nxt_int_t nxt_conf_vldt_match_pattern(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - nxt_str_t pattern; - nxt_uint_t i, first, last; + nxt_conf_value_t *value) { + nxt_str_t pattern; + nxt_uint_t i, first, last; #if (NXT_HAVE_REGEX) - nxt_regex_t *re; - nxt_regex_err_t err; + nxt_regex_t *re; + nxt_regex_err_t err; #endif if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"match\" pattern for \"%s\" " - "must be strings.", vldt->ctx); + return nxt_conf_vldt_error(vldt, + "The \"match\" pattern for \"%s\" " + "must be strings.", + vldt->ctx); } nxt_conf_get_string(value, &pattern); @@ -2256,26 +2320,27 @@ nxt_conf_vldt_match_pattern(nxt_conf_validation_t *vldt, if (first < pattern.length && pattern.start[first] == '~') { #if (NXT_HAVE_REGEX) - pattern.start += first + 1; + pattern.start += first + 1; pattern.length -= first + 1; re = nxt_regex_compile(vldt->pool, &pattern, &err); if (nxt_slow_path(re == NULL)) { if (err.offset < pattern.length) { - return nxt_conf_vldt_error(vldt, "Invalid regular expression: " - "%s at offset %d", - err.msg, err.offset); + return nxt_conf_vldt_error(vldt, + "Invalid regular expression: " + "%s at offset %d", + err.msg, err.offset); } return nxt_conf_vldt_error(vldt, "Invalid regular expression: %s", - err.msg); + err.msg); } return NXT_OK; #else return nxt_conf_vldt_error(vldt, "Unit is built without support of " - "regular expressions: \"--no-regex\" " - "./configure option was set."); + "regular expressions: \"--no-regex\" " + "./configure option was set."); #endif } @@ -2283,21 +2348,21 @@ nxt_conf_vldt_match_pattern(nxt_conf_validation_t *vldt, for (i = first; i < last; i++) { if (pattern.start[i] == '*' && pattern.start[i + 1] == '*') { - return nxt_conf_vldt_error(vldt, "The \"match\" pattern must " - "not contain double \"*\" markers."); + return nxt_conf_vldt_error(vldt, + "The \"match\" pattern must " + "not contain double \"*\" markers."); } } return NXT_OK; } - -static nxt_int_t nxt_conf_vldt_match_encoded_patterns_sets( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value, void *data) -{ +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns_sets(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value, void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_match_encoded_patterns_set); + &nxt_conf_vldt_match_encoded_patterns_set); } /* NXT_CONF_OBJECT */ @@ -2305,29 +2370,26 @@ static nxt_int_t nxt_conf_vldt_match_encoded_patterns_sets( return nxt_conf_vldt_match_encoded_patterns_set(vldt, value); } - -static nxt_int_t nxt_conf_vldt_match_encoded_patterns_set( - nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ +static nxt_int_t +nxt_conf_vldt_match_encoded_patterns_set(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value) { if (nxt_conf_type(value) != NXT_CONF_OBJECT) { return nxt_conf_vldt_error(vldt, "The \"match\" pattern for " - "\"arguments\" must be an object."); + "\"arguments\" must be an object."); } return nxt_conf_vldt_object_iterator(vldt, value, - &nxt_conf_vldt_match_encoded_patterns_set_member); + &nxt_conf_vldt_match_encoded_patterns_set_member); } - static nxt_int_t nxt_conf_vldt_match_encoded_patterns_set_member(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value) -{ - u_char *p, *end; + nxt_str_t *name, nxt_conf_value_t *value) { + u_char *p, *end; if (nxt_slow_path(name->length == 0)) { return nxt_conf_vldt_error(vldt, "The \"match\" pattern objects must " - "not contain empty member names."); + "not contain empty member names."); } p = nxt_mp_nget(vldt->pool, name->length); @@ -2337,26 +2399,25 @@ nxt_conf_vldt_match_encoded_patterns_set_member(nxt_conf_validation_t *vldt, end = nxt_decode_uri(p, name->start, name->length); if (nxt_slow_path(end == NULL)) { - return nxt_conf_vldt_error(vldt, "The \"match\" pattern for " - "\"arguments\" is encoded but is invalid."); + return nxt_conf_vldt_error(vldt, + "The \"match\" pattern for " + "\"arguments\" is encoded but is invalid."); } return nxt_conf_vldt_match_encoded_patterns(vldt, value, - (void *) "arguments"); + (void *) "arguments"); } - static nxt_int_t nxt_conf_vldt_match_encoded_patterns(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_int_t ret; + nxt_conf_value_t *value, void *data) { + nxt_int_t ret; vldt->ctx = data; if (nxt_conf_type(value) == NXT_CONF_ARRAY) { ret = nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_match_encoded_pattern); + &nxt_conf_vldt_match_encoded_pattern); } else { /* NXT_CONF_STRING */ @@ -2368,18 +2429,18 @@ nxt_conf_vldt_match_encoded_patterns(nxt_conf_validation_t *vldt, return ret; } - static nxt_int_t nxt_conf_vldt_match_encoded_pattern(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - u_char *p, *end; - nxt_int_t ret; - nxt_str_t pattern; + nxt_conf_value_t *value) { + u_char *p, *end; + nxt_int_t ret; + nxt_str_t pattern; if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"match\" pattern for \"%s\" " - "must be a string.", vldt->ctx); + return nxt_conf_vldt_error(vldt, + "The \"match\" pattern for \"%s\" " + "must be a string.", + vldt->ctx); } ret = nxt_conf_vldt_match_pattern(vldt, value); @@ -2396,35 +2457,31 @@ nxt_conf_vldt_match_encoded_pattern(nxt_conf_validation_t *vldt, end = nxt_decode_uri(p, pattern.start, pattern.length); if (nxt_slow_path(end == NULL)) { - return nxt_conf_vldt_error(vldt, "The \"match\" pattern for \"%s\" " - "is encoded but is invalid.", vldt->ctx); + return nxt_conf_vldt_error(vldt, + "The \"match\" pattern for \"%s\" " + "is encoded but is invalid.", + vldt->ctx); } return NXT_OK; } - static nxt_int_t -nxt_conf_vldt_match_addrs(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ +nxt_conf_vldt_match_addrs(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_match_addr); + &nxt_conf_vldt_match_addr); } return nxt_conf_vldt_match_addr(vldt, value); } - static nxt_int_t -nxt_conf_vldt_match_addr(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - nxt_http_route_addr_pattern_t pattern; +nxt_conf_vldt_match_addr(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) { + nxt_http_route_addr_pattern_t pattern; switch (nxt_http_route_addr_pattern_parse(vldt->pool, &pattern, value)) { - case NXT_OK: return NXT_OK; @@ -2465,21 +2522,18 @@ nxt_conf_vldt_match_addr(nxt_conf_validation_t *vldt, } } - static nxt_int_t nxt_conf_vldt_match_scheme_pattern(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_str_t scheme; + nxt_conf_value_t *value, void *data) { + nxt_str_t scheme; - static const nxt_str_t http = nxt_string("http"); - static const nxt_str_t https = nxt_string("https"); + static const nxt_str_t http = nxt_string("http"); + static const nxt_str_t https = nxt_string("https"); nxt_conf_get_string(value, &scheme); if (nxt_strcasestr_eq(&scheme, &http) - || nxt_strcasestr_eq(&scheme, &https)) - { + || nxt_strcasestr_eq(&scheme, &https)) { return NXT_OK; } @@ -2487,18 +2541,16 @@ nxt_conf_vldt_match_scheme_pattern(nxt_conf_validation_t *vldt, "\"http\" or \"https\"."); } - static nxt_int_t nxt_conf_vldt_match_patterns_sets(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - nxt_int_t ret; + nxt_conf_value_t *value, void *data) { + nxt_int_t ret; vldt->ctx = data; if (nxt_conf_type(value) == NXT_CONF_ARRAY) { ret = nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_match_patterns_set); + &nxt_conf_vldt_match_patterns_set); } else { /* NXT_CONF_OBJECT */ @@ -2510,28 +2562,26 @@ nxt_conf_vldt_match_patterns_sets(nxt_conf_validation_t *vldt, return ret; } - static nxt_int_t nxt_conf_vldt_match_patterns_set(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { if (nxt_conf_type(value) != NXT_CONF_OBJECT) { - return nxt_conf_vldt_error(vldt, "The \"match\" patterns for " - "\"%s\" must be objects.", vldt->ctx); + return nxt_conf_vldt_error(vldt, + "The \"match\" patterns for " + "\"%s\" must be objects.", + vldt->ctx); } return nxt_conf_vldt_object_iterator(vldt, value, - &nxt_conf_vldt_match_patterns_set_member); + &nxt_conf_vldt_match_patterns_set_member); } - static nxt_int_t nxt_conf_vldt_match_patterns_set_member(nxt_conf_validation_t *vldt, - nxt_str_t *name, nxt_conf_value_t *value) -{ + nxt_str_t *name, nxt_conf_value_t *value) { if (name->length == 0) { return nxt_conf_vldt_error(vldt, "The \"match\" pattern objects must " - "not contain empty member names."); + "not contain empty member names."); } return nxt_conf_vldt_match_patterns(vldt, value, vldt->ctx); @@ -2542,16 +2592,16 @@ nxt_conf_vldt_match_patterns_set_member(nxt_conf_validation_t *vldt, static nxt_int_t nxt_conf_vldt_certificate(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { if (nxt_conf_array_elements_count(value) == 0) { - return nxt_conf_vldt_error(vldt, "The \"certificate\" array " - "must contain at least one element."); + return nxt_conf_vldt_error(vldt, + "The \"certificate\" array " + "must contain at least one element."); } return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_certificate_element); + &nxt_conf_vldt_certificate_element); } /* NXT_CONF_STRING */ @@ -2559,17 +2609,15 @@ nxt_conf_vldt_certificate(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return nxt_conf_vldt_certificate_element(vldt, value); } - static nxt_int_t nxt_conf_vldt_certificate_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_str_t name; - nxt_conf_value_t *cert; + nxt_conf_value_t *cert; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"certificate\" array must " - "contain only string values."); + "contain only string values."); } nxt_conf_get_string(value, &name); @@ -2578,7 +2626,7 @@ nxt_conf_vldt_certificate_element(nxt_conf_validation_t *vldt, if (cert == NULL) { return nxt_conf_vldt_error(vldt, "Certificate \"%V\" is not found.", - &name); + &name); } return NXT_OK; @@ -2589,16 +2637,15 @@ nxt_conf_vldt_certificate_element(nxt_conf_validation_t *vldt, static nxt_int_t nxt_conf_vldt_object_conf_commands(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ + nxt_conf_value_t *value, void *data) { uint32_t index; nxt_int_t ret; nxt_str_t name; - nxt_conf_value_t *member; + nxt_conf_value_t *member; index = 0; - for ( ;; ) { + for (;;) { member = nxt_conf_next_object_member(value, &name, &index); if (member == NULL) { @@ -2621,12 +2668,11 @@ nxt_conf_vldt_object_conf_commands(nxt_conf_validation_t *vldt, static nxt_int_t nxt_conf_vldt_response_header(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ - nxt_str_t str; - nxt_uint_t type; + nxt_conf_value_t *value) { + nxt_str_t str; + nxt_uint_t type; - static const nxt_str_t content_length = nxt_string("Content-Length"); + static const nxt_str_t content_length = nxt_string("Content-Length"); if (name->length == 0) { return nxt_conf_vldt_error(vldt, "The response header name " @@ -2654,19 +2700,19 @@ nxt_conf_vldt_response_header(nxt_conf_validation_t *vldt, nxt_str_t *name, return NXT_OK; } - return nxt_conf_vldt_error(vldt, "The \"%V\" response header value " - "must either be a string or a null", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" response header value " + "must either be a string or a null", + name); } - static nxt_int_t nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { nxt_str_t name; - nxt_conf_value_t *apps, *app; + nxt_conf_value_t *apps, *app; - static const nxt_str_t apps_str = nxt_string("applications"); + static const nxt_str_t apps_str = nxt_string("applications"); nxt_conf_get_string(value, &name); @@ -2686,39 +2732,36 @@ nxt_conf_vldt_app_name(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, error: - return nxt_conf_vldt_error(vldt, "Listening socket is assigned for " - "a non existing application \"%V\".", - &name); + return nxt_conf_vldt_error(vldt, + "Listening socket is assigned for " + "a non existing application \"%V\".", + &name); } - static nxt_int_t nxt_conf_vldt_forwarded(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_conf_value_t *client_ip, *protocol; + void *data) { + nxt_conf_value_t *client_ip, *protocol; - static const nxt_str_t client_ip_str = nxt_string("client_ip"); - static const nxt_str_t protocol_str = nxt_string("protocol"); + static const nxt_str_t client_ip_str = nxt_string("client_ip"); + static const nxt_str_t protocol_str = nxt_string("protocol"); client_ip = nxt_conf_get_object_member(value, &client_ip_str, NULL); - protocol = nxt_conf_get_object_member(value, &protocol_str, NULL); + protocol = nxt_conf_get_object_member(value, &protocol_str, NULL); if (client_ip == NULL && protocol == NULL) { return nxt_conf_vldt_error(vldt, "The \"forwarded\" object must have " - "either \"client_ip\" or \"protocol\" " - "option set."); + "either \"client_ip\" or \"protocol\" " + "option set."); } return nxt_conf_vldt_object(vldt, value, nxt_conf_vldt_forwarded_members); } - static nxt_int_t nxt_conf_vldt_listen_backlog(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - int64_t backlog; + nxt_conf_value_t *value, void *data) { + int64_t backlog; backlog = nxt_conf_get_number(value); @@ -2728,43 +2771,43 @@ nxt_conf_vldt_listen_backlog(nxt_conf_validation_t *vldt, */ if (backlog < -1) { return nxt_conf_vldt_error(vldt, "The \"backlog\" number must be " - "equal to or greater than -1."); + "equal to or greater than -1."); } if (backlog > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"backlog\" number must " - "not exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"backlog\" number must " + "not exceed %d.", + NXT_INT32_T_MAX); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_int_t ret; nxt_str_t type; - nxt_thread_t *thread; - nxt_conf_value_t *type_value; - nxt_app_lang_module_t *lang; + nxt_thread_t *thread; + nxt_conf_value_t *type_value; + nxt_app_lang_module_t *lang; - static const nxt_str_t type_str = nxt_string("type"); + static const nxt_str_t type_str = nxt_string("type"); static const struct { - nxt_conf_vldt_handler_t validator; - nxt_conf_vldt_object_t *members; + nxt_conf_vldt_handler_t validator; + nxt_conf_vldt_object_t *members; } types[] = { - { nxt_conf_vldt_object, nxt_conf_vldt_external_members }, - { nxt_conf_vldt_python, NULL }, - { nxt_conf_vldt_php, NULL }, - { nxt_conf_vldt_object, nxt_conf_vldt_perl_members }, - { nxt_conf_vldt_object, nxt_conf_vldt_ruby_members }, - { nxt_conf_vldt_object, nxt_conf_vldt_java_members }, - { nxt_conf_vldt_object, nxt_conf_vldt_wasm_members }, - { nxt_conf_vldt_object, nxt_conf_vldt_wasm_wc_members }, + {nxt_conf_vldt_object, nxt_conf_vldt_external_members}, + {nxt_conf_vldt_python, NULL}, + {nxt_conf_vldt_php, NULL}, + {nxt_conf_vldt_object, nxt_conf_vldt_perl_members}, + {nxt_conf_vldt_object, nxt_conf_vldt_ruby_members}, + {nxt_conf_vldt_object, nxt_conf_vldt_java_members}, + {nxt_conf_vldt_object, nxt_conf_vldt_wasm_members}, + {nxt_conf_vldt_object, nxt_conf_vldt_wasm_wc_members}, }; ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT); @@ -2777,7 +2820,7 @@ nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name, if (type_value == NULL) { return nxt_conf_vldt_error(vldt, - "Application must have the \"type\" property set."); + "Application must have the \"type\" property set."); } ret = nxt_conf_vldt_type(vldt, &type_str, type_value, NXT_CONF_VLDT_STRING); @@ -2793,30 +2836,27 @@ nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name, lang = nxt_app_lang_module(thread->runtime, &type); if (lang == NULL) { return nxt_conf_vldt_error(vldt, - "The module to run \"%V\" is not found " - "among the available application modules.", - &type); + "The module to run \"%V\" is not found " + "among the available application modules.", + &type); } return types[lang->type].validator(vldt, value, types[lang->type].members); } - static nxt_int_t nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { uint32_t index; nxt_int_t ret; nxt_str_t name, var; - nxt_conf_value_t *member; - nxt_conf_vldt_object_t *vals; + nxt_conf_value_t *member; + nxt_conf_vldt_object_t *vals; vals = data; - for ( ;; ) { + for (;;) { if (vals->name.length == 0) { - if (vals->u.members != NULL) { vals = vals->u.members; continue; @@ -2829,8 +2869,10 @@ nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, member = nxt_conf_get_object_member(value, &vals->name, NULL); if (member == NULL) { - return nxt_conf_vldt_error(vldt, "Required parameter \"%V\" " - "is missing.", &vals->name); + return nxt_conf_vldt_error(vldt, + "Required parameter \"%V\" " + "is missing.", + &vals->name); } } @@ -2839,7 +2881,7 @@ nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, index = 0; - for ( ;; ) { + for (;;) { member = nxt_conf_next_object_member(value, &name, &index); if (member == NULL) { @@ -2848,16 +2890,15 @@ nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, vals = data; - for ( ;; ) { + for (;;) { if (vals->name.length == 0) { - if (vals->u.members != NULL) { vals = vals->u.members; continue; } return nxt_conf_vldt_error(vldt, "Unknown parameter \"%V\".", - &name); + &name); } if (!nxt_strstr_eq(&vals->name, &name)) { @@ -2866,8 +2907,7 @@ nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } if (vals->flags & NXT_CONF_VLDT_TSTR - && nxt_conf_type(member) == NXT_CONF_STRING) - { + && nxt_conf_type(member) == NXT_CONF_STRING) { nxt_conf_get_string(member, &var); if (nxt_is_tstr(&var)) { @@ -2898,15 +2938,13 @@ nxt_conf_vldt_object(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } } - typedef struct { - int64_t spare; - int64_t max; - int64_t idle_timeout; + int64_t spare; + int64_t max; + int64_t idle_timeout; } nxt_conf_vldt_processes_conf_t; - -static nxt_conf_map_t nxt_conf_vldt_processes_conf_map[] = { +static nxt_conf_map_t nxt_conf_vldt_processes_conf_map[] = { { nxt_string("spare"), NXT_CONF_MAP_INT64, @@ -2926,26 +2964,26 @@ static nxt_conf_map_t nxt_conf_vldt_processes_conf_map[] = { }, }; - static nxt_int_t nxt_conf_vldt_processes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - int64_t int_value; - nxt_int_t ret; - nxt_conf_vldt_processes_conf_t proc; + void *data) { + int64_t int_value; + nxt_int_t ret; + nxt_conf_vldt_processes_conf_t proc; if (nxt_conf_type(value) == NXT_CONF_INTEGER) { int_value = nxt_conf_get_number(value); if (int_value < 1) { return nxt_conf_vldt_error(vldt, "The \"processes\" number must be " - "equal to or greater than 1."); + "equal to or greater than 1."); } if (int_value > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"processes\" number must " - "not exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"processes\" number must " + "not exceed %d.", + NXT_INT32_T_MAX); } return NXT_OK; @@ -2956,71 +2994,74 @@ nxt_conf_vldt_processes(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return ret; } - proc.spare = 0; - proc.max = 1; + proc.spare = 0; + proc.max = 1; proc.idle_timeout = 15; ret = nxt_conf_map_object(vldt->pool, value, - nxt_conf_vldt_processes_conf_map, - nxt_nitems(nxt_conf_vldt_processes_conf_map), - &proc); + nxt_conf_vldt_processes_conf_map, + nxt_nitems(nxt_conf_vldt_processes_conf_map), &proc); if (ret != NXT_OK) { return ret; } if (proc.spare < 0) { return nxt_conf_vldt_error(vldt, "The \"spare\" number must not be " - "negative."); + "negative."); } if (proc.spare > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"spare\" number must not " - "exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"spare\" number must not " + "exceed %d.", + NXT_INT32_T_MAX); } if (proc.max < 1) { return nxt_conf_vldt_error(vldt, "The \"max\" number must be equal " - "to or greater than 1."); + "to or greater than 1."); } if (proc.max > NXT_INT32_T_MAX) { - return nxt_conf_vldt_error(vldt, "The \"max\" number must not " - "exceed %d.", NXT_INT32_T_MAX); + return nxt_conf_vldt_error(vldt, + "The \"max\" number must not " + "exceed %d.", + NXT_INT32_T_MAX); } if (proc.max < proc.spare) { return nxt_conf_vldt_error(vldt, "The \"spare\" number must be " - "less than or equal to \"max\"."); + "less than or equal to \"max\"."); } if (proc.idle_timeout < 0) { return nxt_conf_vldt_error(vldt, "The \"idle_timeout\" number must not " - "be negative."); + "be negative."); } if (proc.idle_timeout > NXT_INT32_T_MAX / 1000) { - return nxt_conf_vldt_error(vldt, "The \"idle_timeout\" number must not " - "exceed %d.", NXT_INT32_T_MAX / 1000); + return nxt_conf_vldt_error(vldt, + "The \"idle_timeout\" number must not " + "exceed %d.", + NXT_INT32_T_MAX / 1000); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - uint32_t index; - nxt_int_t ret; - nxt_str_t name; - nxt_conf_value_t *member; - nxt_conf_vldt_member_t validator; + nxt_conf_value_t *value, void *data) { + uint32_t index; + nxt_int_t ret; + nxt_str_t name; + nxt_conf_value_t *member; + nxt_conf_vldt_member_t validator; validator = (nxt_conf_vldt_member_t) data; - index = 0; + index = 0; - for ( ;; ) { + for (;;) { member = nxt_conf_next_object_member(value, &name, &index); if (member == NULL) { return NXT_OK; @@ -3033,19 +3074,17 @@ nxt_conf_vldt_object_iterator(nxt_conf_validation_t *vldt, } } - static nxt_int_t nxt_conf_vldt_array_iterator(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - uint32_t index; - nxt_int_t ret; - nxt_conf_value_t *element; - nxt_conf_vldt_element_t validator; + nxt_conf_value_t *value, void *data) { + uint32_t index; + nxt_int_t ret; + nxt_conf_value_t *element; + nxt_conf_vldt_element_t validator; validator = (nxt_conf_vldt_element_t) data; - for (index = 0; /* void */ ; index++) { + for (index = 0; /* void */; index++) { element = nxt_conf_get_array_element(value, index); if (element == NULL) { @@ -3060,65 +3099,65 @@ nxt_conf_vldt_array_iterator(nxt_conf_validation_t *vldt, } } - static nxt_int_t nxt_conf_vldt_environment(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ - nxt_str_t str; + nxt_conf_value_t *value) { + nxt_str_t str; if (name->length == 0) { return nxt_conf_vldt_error(vldt, - "The environment name must not be empty."); + "The environment name must not be empty."); } if (memchr(name->start, '\0', name->length) != NULL) { return nxt_conf_vldt_error(vldt, "The environment name must not " - "contain null character."); + "contain null character."); } if (memchr(name->start, '=', name->length) != NULL) { return nxt_conf_vldt_error(vldt, "The environment name must not " - "contain '=' character."); + "contain '=' character."); } if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"%V\" environment value must be " - "a string.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" environment value must be " + "a string.", + name); } nxt_conf_get_string(value, &str); if (memchr(str.start, '\0', str.length) != NULL) { - return nxt_conf_vldt_error(vldt, "The \"%V\" environment value must " - "not contain null character.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" environment value must " + "not contain null character.", + name); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_targets_exclusive(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - return nxt_conf_vldt_error(vldt, "The \"%s\" option is mutually exclusive " - "with the \"targets\" object.", data); + nxt_conf_value_t *value, void *data) { + return nxt_conf_vldt_error(vldt, + "The \"%s\" option is mutually exclusive " + "with the \"targets\" object.", + data); } - static nxt_int_t nxt_conf_vldt_targets(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_int_t ret; - nxt_uint_t n; + void *data) { + nxt_int_t ret; + nxt_uint_t n; n = nxt_conf_object_members_count(value); if (n > 254) { return nxt_conf_vldt_error(vldt, "The \"targets\" object must not " - "contain more than 254 members."); + "contain more than 254 members."); } vldt->ctx = data; @@ -3130,19 +3169,18 @@ nxt_conf_vldt_targets(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return ret; } - static nxt_int_t nxt_conf_vldt_target(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { if (name->length == 0) { - return nxt_conf_vldt_error(vldt, - "The target name must not be empty."); + return nxt_conf_vldt_error(vldt, "The target name must not be empty."); } if (nxt_conf_type(value) != NXT_CONF_OBJECT) { - return nxt_conf_vldt_error(vldt, "The \"%V\" target must be " - "an object.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" target must be " + "an object.", + name); } return nxt_conf_vldt_object(vldt, value, vldt->ctx); @@ -3153,23 +3191,21 @@ nxt_conf_vldt_target(nxt_conf_validation_t *vldt, nxt_str_t *name, static nxt_int_t nxt_conf_vldt_cgroup_path(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - char path[NXT_MAX_PATH_LEN]; - nxt_str_t cgpath; + void *data) { + char path[NXT_MAX_PATH_LEN]; + nxt_str_t cgpath; nxt_conf_get_string(value, &cgpath); if (cgpath.length >= NXT_MAX_PATH_LEN - strlen(NXT_CGROUP_ROOT) - 1) { return nxt_conf_vldt_error(vldt, "The cgroup path \"%V\" is too long.", - &cgpath); + &cgpath); } sprintf(path, "/%*s/", (int) cgpath.length, cgpath.start); if (cgpath.length == 0 || strstr(path, "/../") != NULL) { - return nxt_conf_vldt_error(vldt, - "The cgroup path \"%V\" is invalid.", - &cgpath); + return nxt_conf_vldt_error(vldt, "The cgroup path \"%V\" is invalid.", + &cgpath); } return NXT_OK; @@ -3180,16 +3216,13 @@ nxt_conf_vldt_cgroup_path(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, static nxt_int_t nxt_conf_vldt_clone_namespaces(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ + nxt_conf_value_t *value, void *data) { return nxt_conf_vldt_object(vldt, value, data); } - static nxt_int_t nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { return nxt_conf_vldt_object(vldt, value, data); } @@ -3197,17 +3230,17 @@ nxt_conf_vldt_isolation(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, #if (NXT_HAVE_CLONE_NEWUSER) static nxt_int_t -nxt_conf_vldt_clone_uidmap(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ - nxt_int_t ret; +nxt_conf_vldt_clone_uidmap(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value) { + nxt_int_t ret; if (nxt_conf_type(value) != NXT_CONF_OBJECT) { return nxt_conf_vldt_error(vldt, "The \"uidmap\" array " - "must contain only object values."); + "must contain only object values."); } ret = nxt_conf_vldt_object(vldt, value, - (void *) nxt_conf_vldt_app_procmap_members); + (void *) nxt_conf_vldt_app_procmap_members); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -3215,19 +3248,18 @@ nxt_conf_vldt_clone_uidmap(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) return NXT_OK; } - static nxt_int_t -nxt_conf_vldt_clone_gidmap(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ +nxt_conf_vldt_clone_gidmap(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value) { nxt_int_t ret; if (nxt_conf_type(value) != NXT_CONF_OBJECT) { return nxt_conf_vldt_error(vldt, "The \"gidmap\" array " - "must contain only object values."); + "must contain only object values."); } ret = nxt_conf_vldt_object(vldt, value, - (void *) nxt_conf_vldt_app_procmap_members); + (void *) nxt_conf_vldt_app_procmap_members); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -3239,33 +3271,31 @@ nxt_conf_vldt_clone_gidmap(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) static nxt_int_t -nxt_conf_vldt_argument(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ - nxt_str_t str; +nxt_conf_vldt_argument(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) { + nxt_str_t str; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"arguments\" array " - "must contain only string values."); + "must contain only string values."); } nxt_conf_get_string(value, &str); if (memchr(str.start, '\0', str.length) != NULL) { - return nxt_conf_vldt_error(vldt, "The \"arguments\" array must not " - "contain strings with null character."); + return nxt_conf_vldt_error(vldt, + "The \"arguments\" array must not " + "contain strings with null character."); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_php(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_conf_value_t *targets; + void *data) { + nxt_conf_value_t *targets; - static const nxt_str_t targets_str = nxt_string("targets"); + static const nxt_str_t targets_str = nxt_string("targets"); targets = nxt_conf_get_object_member(value, &targets_str, NULL); @@ -3274,79 +3304,76 @@ nxt_conf_vldt_php(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, } return nxt_conf_vldt_object(vldt, value, - nxt_conf_vldt_php_notargets_members); + nxt_conf_vldt_php_notargets_members); } - static nxt_int_t nxt_conf_vldt_php_option(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { if (name->length == 0) { return nxt_conf_vldt_error(vldt, - "The PHP option name must not be empty."); + "The PHP option name must not be empty."); } if (nxt_conf_type(value) != NXT_CONF_STRING) { - return nxt_conf_vldt_error(vldt, "The \"%V\" PHP option must be " - "a string.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" PHP option must be " + "a string.", + name); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_java_classpath(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ - nxt_str_t str; + nxt_conf_value_t *value) { + nxt_str_t str; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"classpath\" array " - "must contain only string values."); + "must contain only string values."); } nxt_conf_get_string(value, &str); if (memchr(str.start, '\0', str.length) != NULL) { - return nxt_conf_vldt_error(vldt, "The \"classpath\" array must not " - "contain strings with null character."); + return nxt_conf_vldt_error(vldt, + "The \"classpath\" array must not " + "contain strings with null character."); } return NXT_OK; } - static nxt_int_t -nxt_conf_vldt_java_option(nxt_conf_validation_t *vldt, nxt_conf_value_t *value) -{ - nxt_str_t str; +nxt_conf_vldt_java_option(nxt_conf_validation_t *vldt, + nxt_conf_value_t *value) { + nxt_str_t str; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"options\" array " - "must contain only string values."); + "must contain only string values."); } nxt_conf_get_string(value, &str); if (memchr(str.start, '\0', str.length) != NULL) { - return nxt_conf_vldt_error(vldt, "The \"options\" array must not " - "contain strings with null character."); + return nxt_conf_vldt_error(vldt, + "The \"options\" array must not " + "contain strings with null character."); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_upstream(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_int_t ret; - nxt_conf_value_t *conf; + nxt_conf_value_t *conf; - static const nxt_str_t servers = nxt_string("servers"); + static const nxt_str_t servers = nxt_string("servers"); ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT); @@ -3362,21 +3389,21 @@ nxt_conf_vldt_upstream(nxt_conf_validation_t *vldt, nxt_str_t *name, conf = nxt_conf_get_object_member(value, &servers, NULL); if (conf == NULL) { - return nxt_conf_vldt_error(vldt, "The \"%V\" upstream must contain " - "\"servers\" object value.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" upstream must contain " + "\"servers\" object value.", + name); } return NXT_OK; } - static nxt_int_t nxt_conf_vldt_server(nxt_conf_validation_t *vldt, nxt_str_t *name, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_int_t ret; nxt_str_t str; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT); if (ret != NXT_OK) { @@ -3389,31 +3416,31 @@ nxt_conf_vldt_server(nxt_conf_validation_t *vldt, nxt_str_t *name, sa = nxt_sockaddr_parse(vldt->pool, &str); if (sa == NULL) { - return nxt_conf_vldt_error(vldt, "The \"%V\" is not valid " - "server address.", name); + return nxt_conf_vldt_error(vldt, + "The \"%V\" is not valid " + "server address.", + name); } return nxt_conf_vldt_object(vldt, value, - nxt_conf_vldt_upstream_server_members); + nxt_conf_vldt_upstream_server_members); } - static nxt_int_t nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value, void *data) -{ - double num_value; + nxt_conf_value_t *value, void *data) { + double num_value; num_value = nxt_conf_get_number(value); if (num_value < 0) { return nxt_conf_vldt_error(vldt, "The \"weight\" number must be " - "positive."); + "positive."); } if (num_value > 1000000) { return nxt_conf_vldt_error(vldt, "The \"weight\" number must " - "not exceed 1,000,000"); + "not exceed 1,000,000"); } return NXT_OK; @@ -3424,11 +3451,10 @@ nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt, static nxt_int_t nxt_conf_vldt_js_module(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ + void *data) { if (nxt_conf_type(value) == NXT_CONF_ARRAY) { return nxt_conf_vldt_array_iterator(vldt, value, - &nxt_conf_vldt_js_module_element); + &nxt_conf_vldt_js_module_element); } /* NXT_CONF_STRING */ @@ -3436,17 +3462,15 @@ nxt_conf_vldt_js_module(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, return nxt_conf_vldt_js_module_element(vldt, value); } - static nxt_int_t nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt, - nxt_conf_value_t *value) -{ + nxt_conf_value_t *value) { nxt_str_t name; - nxt_conf_value_t *module; + nxt_conf_value_t *module; if (nxt_conf_type(value) != NXT_CONF_STRING) { return nxt_conf_vldt_error(vldt, "The \"js_module\" array must " - "contain only string values."); + "contain only string values."); } nxt_conf_get_string(value, &name); @@ -3454,7 +3478,7 @@ nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt, module = nxt_script_info_get(&name); if (module == NULL) { return nxt_conf_vldt_error(vldt, "JS module \"%V\" is not found.", - &name); + &name); } return NXT_OK; @@ -3464,12 +3488,11 @@ nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt, typedef struct { - nxt_str_t path; - nxt_str_t format; + nxt_str_t path; + nxt_str_t format; } nxt_conf_vldt_access_log_conf_t; - -static nxt_conf_map_t nxt_conf_vldt_access_log_map[] = { +static nxt_conf_map_t nxt_conf_vldt_access_log_map[] = { { nxt_string("path"), NXT_CONF_MAP_STR, @@ -3483,15 +3506,13 @@ static nxt_conf_map_t nxt_conf_vldt_access_log_map[] = { }, }; - static nxt_int_t nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, - void *data) -{ - nxt_int_t ret; - nxt_conf_vldt_access_log_conf_t conf; + void *data) { + nxt_int_t ret; + nxt_conf_vldt_access_log_conf_t conf; - static const nxt_str_t format_str = nxt_string("format"); + static const nxt_str_t format_str = nxt_string("format"); if (nxt_conf_type(value) == NXT_CONF_STRING) { return NXT_OK; @@ -3504,17 +3525,15 @@ nxt_conf_vldt_access_log(nxt_conf_validation_t *vldt, nxt_conf_value_t *value, nxt_memzero(&conf, sizeof(nxt_conf_vldt_access_log_conf_t)); - ret = nxt_conf_map_object(vldt->pool, value, - nxt_conf_vldt_access_log_map, - nxt_nitems(nxt_conf_vldt_access_log_map), - &conf); + ret = nxt_conf_map_object(vldt->pool, value, nxt_conf_vldt_access_log_map, + nxt_nitems(nxt_conf_vldt_access_log_map), &conf); if (ret != NXT_OK) { return ret; } if (conf.path.length == 0) { return nxt_conf_vldt_error(vldt, - "The \"path\" string must not be empty."); + "The \"path\" string must not be empty."); } if (nxt_is_tstr(&conf.format)) { diff --git a/src/nxt_conn.c b/src/nxt_conn.c index 498d7d5c5..ff22d3d78 100644 --- a/src/nxt_conn.c +++ b/src/nxt_conn.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,15 +6,15 @@ #include -nxt_conn_io_t nxt_unix_conn_io = { +nxt_conn_io_t nxt_unix_conn_io = { .connect = nxt_conn_io_connect, - .accept = nxt_conn_io_accept, + .accept = nxt_conn_io_accept, - .read = nxt_conn_io_read, + .read = nxt_conn_io_read, .recvbuf = nxt_conn_io_recvbuf, - .recv = nxt_conn_io_recv, + .recv = nxt_conn_io_recv, - .write = nxt_conn_io_write, + .write = nxt_conn_io_write, .sendbuf = nxt_conn_io_sendbuf, #if (NXT_HAVE_LINUX_SENDFILE) @@ -35,15 +34,14 @@ nxt_conn_io_t nxt_unix_conn_io = { #endif .writev = nxt_event_conn_io_writev, - .send = nxt_event_conn_io_send, + .send = nxt_event_conn_io_send, }; nxt_conn_t * -nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task) -{ - nxt_conn_t *c; - nxt_thread_t *thr; +nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task) { + nxt_conn_t *c; + nxt_thread_t *thr; c = nxt_mp_zget(mp, sizeof(nxt_conn_t)); if (nxt_slow_path(c == NULL)) { @@ -55,7 +53,7 @@ nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task) c->socket.fd = -1; c->socket.log = &c->log; - c->log = *task->log; + c->log = *task->log; /* The while loop skips possible uint32_t overflow. */ @@ -66,18 +64,18 @@ nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task) thr = nxt_thread(); thr->engine->connections++; - c->task.thread = thr; - c->task.log = &c->log; - c->task.ident = c->log.ident; - c->socket.task = &c->task; - c->read_timer.task = &c->task; + c->task.thread = thr; + c->task.log = &c->log; + c->task.ident = c->log.ident; + c->socket.task = &c->task; + c->read_timer.task = &c->task; c->write_timer.task = &c->task; - c->io = thr->engine->event.io; + c->io = thr->engine->event.io; c->max_chunk = NXT_INT32_T_MAX; - c->sendfile = NXT_CONN_SENDFILE_UNSET; + c->sendfile = NXT_CONN_SENDFILE_UNSET; - c->socket.read_work_queue = &thr->engine->fast_work_queue; + c->socket.read_work_queue = &thr->engine->fast_work_queue; c->socket.write_work_queue = &thr->engine->fast_work_queue; nxt_conn_timer_init(&c->read_timer, c, c->socket.read_work_queue); @@ -88,11 +86,9 @@ nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task) return c; } - void -nxt_conn_free(nxt_task_t *task, nxt_conn_t *c) -{ - nxt_mp_t *mp; +nxt_conn_free(nxt_task_t *task, nxt_conn_t *c) { + nxt_mp_t *mp; task->thread->engine->connections--; @@ -100,12 +96,10 @@ nxt_conn_free(nxt_task_t *task, nxt_conn_t *c) nxt_mp_release(mp); } - void nxt_conn_timer(nxt_event_engine_t *engine, nxt_conn_t *c, - const nxt_conn_state_t *state, nxt_timer_t *timer) -{ - nxt_msec_t value; + const nxt_conn_state_t *state, nxt_timer_t *timer) { + nxt_msec_t value; if (state->timer_value != NULL) { value = state->timer_value(c, state->timer_data); @@ -117,24 +111,20 @@ nxt_conn_timer(nxt_event_engine_t *engine, nxt_conn_t *c, } } - void -nxt_conn_work_queue_set(nxt_conn_t *c, nxt_work_queue_t *wq) -{ - c->read_work_queue = wq; - c->write_work_queue = wq; - c->read_timer.work_queue = wq; +nxt_conn_work_queue_set(nxt_conn_t *c, nxt_work_queue_t *wq) { + c->read_work_queue = wq; + c->write_work_queue = wq; + c->read_timer.work_queue = wq; c->write_timer.work_queue = wq; } - nxt_sockaddr_t * -nxt_conn_local_addr(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_conn_local_addr(nxt_task_t *task, nxt_conn_t *c) { int ret; size_t size, length; socklen_t socklen; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; if (c->local != NULL) { return c->local; @@ -146,15 +136,15 @@ nxt_conn_local_addr(nxt_task_t *task, nxt_conn_t *c) #if (NXT_INET6) case AF_INET6: socklen = sizeof(struct sockaddr_in6); - length = NXT_INET6_ADDR_STR_LEN; - size = offsetof(nxt_sockaddr_t, u) + socklen + length; + length = NXT_INET6_ADDR_STR_LEN; + size = offsetof(nxt_sockaddr_t, u) + socklen + length; break; #endif case AF_INET: default: socklen = sizeof(struct sockaddr_in); - length = NXT_INET_ADDR_STR_LEN; - size = offsetof(nxt_sockaddr_t, u) + socklen + length; + length = NXT_INET_ADDR_STR_LEN; + size = offsetof(nxt_sockaddr_t, u) + socklen + length; break; } @@ -164,7 +154,7 @@ nxt_conn_local_addr(nxt_task_t *task, nxt_conn_t *c) } sa->socklen = socklen; - sa->length = length; + sa->length = length; ret = getsockname(c->socket.fd, &sa->u.sockaddr, &socklen); if (nxt_slow_path(ret != 0)) { diff --git a/src/nxt_conn.h b/src/nxt_conn.h index 5717d3c99..982eeff58 100644 --- a/src/nxt_conn.h +++ b/src/nxt_conn.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,38 +7,34 @@ #define _NXT_CONN_H_INCLUDED_ -typedef ssize_t (*nxt_conn_io_read_t)(nxt_task_t *task, nxt_conn_t *c); +typedef ssize_t (*nxt_conn_io_read_t)(nxt_task_t *task, nxt_conn_t *c); typedef nxt_msec_t (*nxt_conn_timer_value_t)(nxt_conn_t *c, uintptr_t data); - typedef struct { - nxt_work_handler_t ready_handler; - nxt_work_handler_t close_handler; - nxt_work_handler_t error_handler; + nxt_work_handler_t ready_handler; + nxt_work_handler_t close_handler; + nxt_work_handler_t error_handler; - nxt_conn_io_read_t io_read_handler; + nxt_conn_io_read_t io_read_handler; - nxt_work_handler_t timer_handler; - nxt_conn_timer_value_t timer_value; - uintptr_t timer_data; + nxt_work_handler_t timer_handler; + nxt_conn_timer_value_t timer_value; + uintptr_t timer_data; - uint8_t timer_autoreset; + uint8_t timer_autoreset; } nxt_conn_state_t; - typedef struct { - double average; - size_t limit; - size_t limit_after; - size_t max_limit; - nxt_msec_t last; + double average; + size_t limit; + size_t limit_after; + size_t max_limit; + nxt_msec_t last; } nxt_event_write_rate_t; - typedef struct { - - nxt_work_handler_t connect; - nxt_work_handler_t accept; + nxt_work_handler_t connect; + nxt_work_handler_t accept; /* * The read() with NULL c->read buffer waits readiness of a connection @@ -49,44 +44,38 @@ typedef struct { * connection without errors. In the latter case state's close_handler * is called. */ - nxt_work_handler_t read; + nxt_work_handler_t read; - ssize_t (*recvbuf)(nxt_conn_t *c, nxt_buf_t *b); + ssize_t (*recvbuf)(nxt_conn_t *c, nxt_buf_t *b); - ssize_t (*recv)(nxt_conn_t *c, void *buf, - size_t size, nxt_uint_t flags); + ssize_t (*recv)(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags); /* The write() is an interface to write a buffer chain. */ - nxt_work_handler_t write; + nxt_work_handler_t write; /* * The sendbuf() is an interface for OS-specific sendfile * implementations or simple writev(). */ - ssize_t (*sendbuf)(nxt_task_t *task, - nxt_sendbuf_t *sb); + ssize_t (*sendbuf)(nxt_task_t *task, nxt_sendbuf_t *sb); /* * The sendbuf() is an interface for OS-specific sendfile * implementations or simple writev(). */ - ssize_t (*old_sendbuf)(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); + ssize_t (*old_sendbuf)(nxt_conn_t *c, nxt_buf_t *b, size_t limit); /* * The writev() is an interface to write several nxt_iobuf_t buffers. */ - ssize_t (*writev)(nxt_conn_t *c, - nxt_iobuf_t *iob, nxt_uint_t niob); + ssize_t (*writev)(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob); /* * The send() is an interface to write a single buffer. SSL/TLS * libraries' send() interface handles also the libraries' errors. */ - ssize_t (*send)(nxt_conn_t *c, void *buf, - size_t size); + ssize_t (*send)(nxt_conn_t *c, void *buf, size_t size); - nxt_work_handler_t shutdown; + nxt_work_handler_t shutdown; } nxt_conn_io_t; - /* * The nxt_listen_event_t is separated from nxt_listen_socket_t * because nxt_listen_socket_t is one per process whilst each worker @@ -94,271 +83,288 @@ typedef struct { */ typedef struct { /* Must be the first field. */ - nxt_fd_event_t socket; + nxt_fd_event_t socket; - nxt_task_t task; + nxt_task_t task; - uint32_t ready; - uint32_t batch; - uint32_t count; + uint32_t ready; + uint32_t batch; + uint32_t count; /* An accept() interface is cached to minimize memory accesses. */ - nxt_work_handler_t accept; + nxt_work_handler_t accept; - nxt_listen_socket_t *listen; - nxt_conn_t *next; - nxt_work_queue_t *work_queue; + nxt_listen_socket_t *listen; + nxt_conn_t *next; + nxt_work_queue_t *work_queue; - nxt_timer_t timer; + nxt_timer_t timer; - nxt_queue_link_t link; + nxt_queue_link_t link; } nxt_listen_event_t; - struct nxt_conn_s { /* * Must be the first field, since nxt_fd_event_t * and nxt_conn_t are used interchangeably. */ - nxt_fd_event_t socket; + nxt_fd_event_t socket; - nxt_buf_t *read; - const nxt_conn_state_t *read_state; - nxt_work_queue_t *read_work_queue; - nxt_timer_t read_timer; + nxt_buf_t *read; + const nxt_conn_state_t *read_state; + nxt_work_queue_t *read_work_queue; + nxt_timer_t read_timer; - nxt_buf_t *write; - const nxt_conn_state_t *write_state; - nxt_work_queue_t *write_work_queue; - nxt_event_write_rate_t *rate; - nxt_timer_t write_timer; + nxt_buf_t *write; + const nxt_conn_state_t *write_state; + nxt_work_queue_t *write_work_queue; + nxt_event_write_rate_t *rate; + nxt_timer_t write_timer; - nxt_off_t sent; - uint32_t max_chunk; - uint32_t nbytes; + nxt_off_t sent; + uint32_t max_chunk; + uint32_t nbytes; - nxt_conn_io_t *io; + nxt_conn_io_t *io; union { #if (NXT_TLS) - void *tls; + void *tls; #endif - nxt_thread_pool_t *thread_pool; + nxt_thread_pool_t *thread_pool; } u; - nxt_mp_t *mem_pool; + nxt_mp_t *mem_pool; - nxt_task_t task; - nxt_log_t log; + nxt_task_t task; + nxt_log_t log; - nxt_listen_event_t *listen; + nxt_listen_event_t *listen; - nxt_sockaddr_t *remote; - nxt_sockaddr_t *local; - const char *action; + nxt_sockaddr_t *remote; + nxt_sockaddr_t *local; + const char *action; - uint8_t block_read; /* 1 bit */ - uint8_t block_write; /* 1 bit */ - uint8_t delayed; /* 1 bit */ - uint8_t idle; /* 1 bit */ + uint8_t block_read; /* 1 bit */ + uint8_t block_write; /* 1 bit */ + uint8_t delayed; /* 1 bit */ + uint8_t idle; /* 1 bit */ -#define NXT_CONN_SENDFILE_OFF 0 -#define NXT_CONN_SENDFILE_ON 1 -#define NXT_CONN_SENDFILE_UNSET 3 +#define NXT_CONN_SENDFILE_OFF 0 +#define NXT_CONN_SENDFILE_ON 1 +#define NXT_CONN_SENDFILE_UNSET 3 - uint8_t sendfile; /* 2 bits */ - uint8_t tcp_nodelay; /* 1 bit */ + uint8_t sendfile; /* 2 bits */ + uint8_t tcp_nodelay; /* 1 bit */ - nxt_queue_link_t link; + nxt_queue_link_t link; }; - -#define nxt_conn_timer_init(ev, c, wq) \ - do { \ - (ev)->work_queue = (wq); \ - (ev)->log = &(c)->log; \ - (ev)->bias = NXT_TIMER_DEFAULT_BIAS; \ +#define nxt_conn_timer_init(ev, c, wq) \ + do { \ + (ev)->work_queue = (wq); \ + (ev)->log = &(c)->log; \ + (ev)->bias = NXT_TIMER_DEFAULT_BIAS; \ } while (0) -#define nxt_read_timer_conn(ev) \ - nxt_timer_data(ev, nxt_conn_t, read_timer) +#define nxt_read_timer_conn(ev) nxt_timer_data(ev, nxt_conn_t, read_timer) -#define nxt_write_timer_conn(ev) \ - nxt_timer_data(ev, nxt_conn_t, write_timer) +#define nxt_write_timer_conn(ev) nxt_timer_data(ev, nxt_conn_t, write_timer) #if (NXT_HAVE_UNIX_DOMAIN) -#define nxt_conn_tcp_nodelay_on(task, c) \ - do { \ - nxt_int_t ret; \ - \ - if ((c)->remote->u.sockaddr.sa_family != AF_UNIX) { \ - ret = nxt_socket_setsockopt(task, (c)->socket.fd, IPPROTO_TCP, \ - TCP_NODELAY, 1); \ - \ - (c)->tcp_nodelay = (ret == NXT_OK); \ - } \ +#define nxt_conn_tcp_nodelay_on(task, c) \ + do { \ + nxt_int_t ret; \ + \ + if ((c)->remote->u.sockaddr.sa_family != AF_UNIX) { \ + ret = nxt_socket_setsockopt(task, (c)->socket.fd, IPPROTO_TCP, \ + TCP_NODELAY, 1); \ + \ + (c)->tcp_nodelay = (ret == NXT_OK); \ + } \ } while (0) #else -#define nxt_conn_tcp_nodelay_on(task, c) \ - do { \ - nxt_int_t ret; \ - \ - ret = nxt_socket_setsockopt(task, (c)->socket.fd, IPPROTO_TCP, \ - TCP_NODELAY, 1); \ - \ - (c)->tcp_nodelay = (ret == NXT_OK); \ +#define nxt_conn_tcp_nodelay_on(task, c) \ + do { \ + nxt_int_t ret; \ + \ + ret = nxt_socket_setsockopt(task, (c)->socket.fd, IPPROTO_TCP, \ + TCP_NODELAY, 1); \ + \ + (c)->tcp_nodelay = (ret == NXT_OK); \ } while (0) #endif -NXT_EXPORT nxt_conn_t *nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task); -NXT_EXPORT void nxt_conn_free(nxt_task_t *task, nxt_conn_t *c); -NXT_EXPORT void nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c); +NXT_EXPORT nxt_conn_t * +nxt_conn_create(nxt_mp_t *mp, nxt_task_t *task); +NXT_EXPORT void +nxt_conn_free(nxt_task_t *task, nxt_conn_t *c); +NXT_EXPORT void +nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c); -NXT_EXPORT void nxt_conn_timer(nxt_event_engine_t *engine, nxt_conn_t *c, +NXT_EXPORT void +nxt_conn_timer(nxt_event_engine_t *engine, nxt_conn_t *c, const nxt_conn_state_t *state, nxt_timer_t *tev); -NXT_EXPORT void nxt_conn_work_queue_set(nxt_conn_t *c, nxt_work_queue_t *wq); -NXT_EXPORT nxt_sockaddr_t *nxt_conn_local_addr(nxt_task_t *task, - nxt_conn_t *c); - -void nxt_conn_sys_socket(nxt_task_t *task, void *obj, void *data); -void nxt_conn_io_connect(nxt_task_t *task, void *obj, void *data); -nxt_int_t nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c); -void nxt_conn_connect_test(nxt_task_t *task, void *obj, void *data); -void nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data); - -NXT_EXPORT nxt_listen_event_t *nxt_listen_event(nxt_task_t *task, - nxt_listen_socket_t *ls); -void nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data); -NXT_EXPORT void nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, - nxt_conn_t *c); -void nxt_conn_accept_error(nxt_task_t *task, nxt_listen_event_t *lev, +NXT_EXPORT void +nxt_conn_work_queue_set(nxt_conn_t *c, nxt_work_queue_t *wq); +NXT_EXPORT nxt_sockaddr_t * +nxt_conn_local_addr(nxt_task_t *task, nxt_conn_t *c); + +void +nxt_conn_sys_socket(nxt_task_t *task, void *obj, void *data); +void +nxt_conn_io_connect(nxt_task_t *task, void *obj, void *data); +nxt_int_t +nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c); +void +nxt_conn_connect_test(nxt_task_t *task, void *obj, void *data); +void +nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data); + +NXT_EXPORT nxt_listen_event_t * +nxt_listen_event(nxt_task_t *task, nxt_listen_socket_t *ls); +void +nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data); +NXT_EXPORT void +nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c); +void +nxt_conn_accept_error(nxt_task_t *task, nxt_listen_event_t *lev, const char *accept_syscall, nxt_err_t err); -void nxt_conn_wait(nxt_conn_t *c); - -void nxt_conn_io_read(nxt_task_t *task, void *obj, void *data); -ssize_t nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); -ssize_t nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, - nxt_uint_t flags); - -void nxt_conn_io_write(nxt_task_t *task, void *obj, void *data); -ssize_t nxt_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb); -ssize_t nxt_conn_io_writev(nxt_task_t *task, nxt_sendbuf_t *sb, - nxt_iobuf_t *iob, nxt_uint_t niob); -ssize_t nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, - size_t size); - -size_t nxt_event_conn_write_limit(nxt_conn_t *c); -nxt_bool_t nxt_event_conn_write_delayed(nxt_event_engine_t *engine, - nxt_conn_t *c, size_t sent); -ssize_t nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, +void +nxt_conn_wait(nxt_conn_t *c); + +void +nxt_conn_io_read(nxt_task_t *task, void *obj, void *data); +ssize_t +nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); +ssize_t +nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags); + +void +nxt_conn_io_write(nxt_task_t *task, void *obj, void *data); +ssize_t +nxt_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb); +ssize_t +nxt_conn_io_writev(nxt_task_t *task, nxt_sendbuf_t *sb, nxt_iobuf_t *iob, nxt_uint_t niob); -ssize_t nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size); - -NXT_EXPORT void nxt_event_conn_job_sendfile(nxt_task_t *task, - nxt_conn_t *c); - - -#define nxt_conn_connect(engine, c) \ - nxt_work_queue_add(&engine->socket_work_queue, nxt_conn_sys_socket, \ - c->socket.task, c, c->socket.data) - - -#define nxt_conn_read(engine, c) \ - do { \ - nxt_event_engine_t *e = engine; \ - \ - c->socket.read_work_queue = &e->read_work_queue; \ - \ - nxt_work_queue_add(&e->read_work_queue, c->io->read, \ - c->socket.task, c, c->socket.data); \ +ssize_t +nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, size_t size); + +size_t +nxt_event_conn_write_limit(nxt_conn_t *c); +nxt_bool_t +nxt_event_conn_write_delayed(nxt_event_engine_t *engine, nxt_conn_t *c, + size_t sent); +ssize_t +nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob); +ssize_t +nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size); + +NXT_EXPORT void +nxt_event_conn_job_sendfile(nxt_task_t *task, nxt_conn_t *c); + + +#define nxt_conn_connect(engine, c) \ + nxt_work_queue_add(&engine->socket_work_queue, nxt_conn_sys_socket, \ + c->socket.task, c, c->socket.data) + + +#define nxt_conn_read(engine, c) \ + do { \ + nxt_event_engine_t *e = engine; \ + \ + c->socket.read_work_queue = &e->read_work_queue; \ + \ + nxt_work_queue_add(&e->read_work_queue, c->io->read, c->socket.task, \ + c, c->socket.data); \ } while (0) -#define nxt_conn_write(engine, c) \ - do { \ - nxt_event_engine_t *e = engine; \ - \ - c->socket.write_work_queue = &e->write_work_queue; \ - \ - nxt_work_queue_add(&e->write_work_queue, c->io->write, \ - c->socket.task, c, c->socket.data); \ +#define nxt_conn_write(engine, c) \ + do { \ + nxt_event_engine_t *e = engine; \ + \ + c->socket.write_work_queue = &e->write_work_queue; \ + \ + nxt_work_queue_add(&e->write_work_queue, c->io->write, c->socket.task, \ + c, c->socket.data); \ } while (0) -#define nxt_conn_idle(engine, c) \ - do { \ - nxt_event_engine_t *e = engine; \ - \ - nxt_queue_insert_head(&e->idle_connections, &c->link); \ - \ - c->idle = 1; \ - e->idle_conns_cnt++; \ +#define nxt_conn_idle(engine, c) \ + do { \ + nxt_event_engine_t *e = engine; \ + \ + nxt_queue_insert_head(&e->idle_connections, &c->link); \ + \ + c->idle = 1; \ + e->idle_conns_cnt++; \ } while (0) -#define nxt_conn_active(engine, c) \ - do { \ - nxt_event_engine_t *e = engine; \ - \ - nxt_queue_remove(&c->link); \ - \ - e->idle_conns_cnt -= c->idle; \ +#define nxt_conn_active(engine, c) \ + do { \ + nxt_event_engine_t *e = engine; \ + \ + nxt_queue_remove(&c->link); \ + \ + e->idle_conns_cnt -= c->idle; \ } while (0) -extern nxt_conn_io_t nxt_unix_conn_io; - +extern nxt_conn_io_t nxt_unix_conn_io; typedef struct { /* * Client and peer connections are not embedded because already * existent connections can be switched to the event connection proxy. */ - nxt_conn_t *client; - nxt_conn_t *peer; - nxt_buf_t *client_buffer; - nxt_buf_t *peer_buffer; - - size_t client_buffer_size; - size_t peer_buffer_size; - - nxt_msec_t client_wait_timeout; - nxt_msec_t connect_timeout; - nxt_msec_t reconnect_timeout; - nxt_msec_t peer_wait_timeout; - nxt_msec_t client_write_timeout; - nxt_msec_t peer_write_timeout; - - uint8_t connected; /* 1 bit */ - uint8_t delayed; /* 1 bit */ - uint8_t retries; /* 8 bits */ - uint8_t retain; /* 2 bits */ - - nxt_work_handler_t completion_handler; + nxt_conn_t *client; + nxt_conn_t *peer; + nxt_buf_t *client_buffer; + nxt_buf_t *peer_buffer; + + size_t client_buffer_size; + size_t peer_buffer_size; + + nxt_msec_t client_wait_timeout; + nxt_msec_t connect_timeout; + nxt_msec_t reconnect_timeout; + nxt_msec_t peer_wait_timeout; + nxt_msec_t client_write_timeout; + nxt_msec_t peer_write_timeout; + + uint8_t connected; /* 1 bit */ + uint8_t delayed; /* 1 bit */ + uint8_t retries; /* 8 bits */ + uint8_t retain; /* 2 bits */ + + nxt_work_handler_t completion_handler; } nxt_conn_proxy_t; - -NXT_EXPORT nxt_conn_proxy_t *nxt_conn_proxy_create(nxt_conn_t *c); -NXT_EXPORT void nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p); +NXT_EXPORT nxt_conn_proxy_t * +nxt_conn_proxy_create(nxt_conn_t *c); +NXT_EXPORT void +nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p); /* STUB */ -#define nxt_event_conn_t nxt_conn_t -#define nxt_event_conn_state_t nxt_conn_state_t -#define nxt_event_conn_proxy_t nxt_conn_proxy_t -#define nxt_event_conn_read nxt_conn_read -#define nxt_event_conn_write nxt_conn_write -#define nxt_event_conn_close nxt_conn_close +#define nxt_event_conn_t nxt_conn_t +#define nxt_event_conn_state_t nxt_conn_state_t +#define nxt_event_conn_proxy_t nxt_conn_proxy_t +#define nxt_event_conn_read nxt_conn_read +#define nxt_event_conn_write nxt_conn_write +#define nxt_event_conn_close nxt_conn_close #endif /* _NXT_CONN_H_INCLUDED_ */ diff --git a/src/nxt_conn_accept.c b/src/nxt_conn_accept.c index 720c7b647..98efd1a33 100644 --- a/src/nxt_conn_accept.c +++ b/src/nxt_conn_accept.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -18,56 +17,54 @@ */ -static nxt_conn_t *nxt_conn_accept_alloc(nxt_task_t *task, - nxt_listen_event_t *lev); -static void nxt_conn_listen_handler(nxt_task_t *task, void *obj, - void *data); -static nxt_conn_t *nxt_conn_accept_next(nxt_task_t *task, - nxt_listen_event_t *lev); -static void nxt_conn_accept_close_idle(nxt_task_t *task, - nxt_listen_event_t *lev); -static void nxt_conn_accept_close_idle_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_listen_event_error(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, - void *data); - +static nxt_conn_t * +nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev); +static void +nxt_conn_listen_handler(nxt_task_t *task, void *obj, void *data); +static nxt_conn_t * +nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev); +static void +nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev); +static void +nxt_conn_accept_close_idle_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_listen_event_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data); nxt_listen_event_t * -nxt_listen_event(nxt_task_t *task, nxt_listen_socket_t *ls) -{ - nxt_listen_event_t *lev; - nxt_event_engine_t *engine; +nxt_listen_event(nxt_task_t *task, nxt_listen_socket_t *ls) { + nxt_listen_event_t *lev; + nxt_event_engine_t *engine; lev = nxt_zalloc(sizeof(nxt_listen_event_t)); if (nxt_fast_path(lev != NULL)) { lev->socket.fd = ls->socket; - engine = task->thread->engine; + engine = task->thread->engine; lev->batch = engine->batch; lev->count = 1; lev->socket.read_work_queue = &engine->accept_work_queue; - lev->socket.read_handler = nxt_conn_listen_handler; - lev->socket.error_handler = nxt_conn_listen_event_error; - lev->socket.log = &nxt_main_log; + lev->socket.read_handler = nxt_conn_listen_handler; + lev->socket.error_handler = nxt_conn_listen_event_error; + lev->socket.log = &nxt_main_log; lev->accept = engine->event.io->accept; - lev->listen = ls; + lev->listen = ls; lev->work_queue = &engine->read_work_queue; lev->timer.work_queue = &engine->fast_work_queue; - lev->timer.handler = nxt_conn_listen_timer_handler; - lev->timer.log = &nxt_main_log; + lev->timer.handler = nxt_conn_listen_timer_handler; + lev->timer.log = &nxt_main_log; lev->task.thread = task->thread; - lev->task.log = &nxt_main_log; - lev->task.ident = nxt_task_next_ident(); + lev->task.log = &nxt_main_log; + lev->task.ident = nxt_task_next_ident(); lev->socket.task = &lev->task; - lev->timer.task = &lev->task; + lev->timer.task = &lev->task; if (nxt_conn_accept_alloc(task, lev) != NULL) { nxt_fd_event_enable_accept(engine, &lev->socket); @@ -81,18 +78,15 @@ nxt_listen_event(nxt_task_t *task, nxt_listen_socket_t *ls) return NULL; } - static nxt_conn_t * -nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev) -{ - nxt_mp_t *mp; - nxt_conn_t *c; - nxt_event_engine_t *engine; +nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev) { + nxt_mp_t *mp; + nxt_conn_t *c; + nxt_event_engine_t *engine; engine = task->thread->engine; if (engine->connections < engine->max_connections) { - mp = nxt_mp_create(1024, 128, 256, 32); if (nxt_fast_path(mp != NULL)) { @@ -104,7 +98,7 @@ nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev) } c->socket.read_work_queue = lev->socket.read_work_queue; - c->socket.write_ready = 1; + c->socket.write_ready = 1; c->remote = nxt_sockaddr_cache_alloc(engine, lev->listen); if (nxt_fast_path(c->remote != NULL)) { @@ -119,35 +113,31 @@ nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev) return NULL; } - static void -nxt_conn_listen_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_listen_event_t *lev; +nxt_conn_listen_handler(nxt_task_t *task, void *obj, void *data) { + nxt_listen_event_t *lev; - lev = obj; + lev = obj; lev->ready = lev->batch; lev->accept(task, lev, data); } - void -nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data) { socklen_t socklen; - nxt_conn_t *c; + nxt_conn_t *c; nxt_socket_t s; - struct sockaddr *sa; - nxt_listen_event_t *lev; + struct sockaddr *sa; + nxt_listen_event_t *lev; lev = obj; - c = lev->next; + c = lev->next; lev->ready--; lev->socket.read_ready = (lev->ready != 0); - sa = &c->remote->u.sockaddr; + sa = &c->remote->u.sockaddr; socklen = c->remote->socklen; /* * The returned socklen is ignored here, because sockaddr_in and @@ -158,7 +148,7 @@ nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data) * and truncate surplus zero part. Only bound sockaddr_un will be really * truncated here. */ - s = accept(lev->socket.fd, sa, &socklen); + s = accept(lev->socket.fd, sa, &socklen); if (s == -1) { nxt_conn_accept_error(task, lev, "accept", nxt_socket_errno); @@ -183,18 +173,15 @@ nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data) nxt_conn_accept(task, lev, c); } - void -nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c) -{ - nxt_conn_t *next; - nxt_event_engine_t *engine; +nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c) { + nxt_conn_t *next; + nxt_event_engine_t *engine; nxt_sockaddr_text(c->remote); - nxt_debug(task, "client: %*s", - (size_t) c->remote->address_length, - nxt_sockaddr_address(c->remote)); + nxt_debug(task, "client: %*s", (size_t) c->remote->address_length, + nxt_sockaddr_address(c->remote)); engine = task->thread->engine; @@ -204,37 +191,34 @@ nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c) c->listen = lev; lev->count++; - lev->next = NULL; + lev->next = NULL; c->socket.data = NULL; - c->read_work_queue = lev->work_queue; + c->read_work_queue = lev->work_queue; c->write_work_queue = lev->work_queue; if (lev->listen->read_after_accept) { - - //c->socket.read_ready = 1; -// lev->listen->handler(task, c, lev); - nxt_work_queue_add(c->read_work_queue, lev->listen->handler, - &c->task, c, lev); + // c->socket.read_ready = 1; + // lev->listen->handler(task, c, lev); + nxt_work_queue_add(c->read_work_queue, lev->listen->handler, &c->task, + c, lev); } else { - nxt_work_queue_add(c->write_work_queue, lev->listen->handler, - &c->task, c, lev); + nxt_work_queue_add(c->write_work_queue, lev->listen->handler, &c->task, + c, lev); } next = nxt_conn_accept_next(task, lev); if (next != NULL && lev->socket.read_ready) { - nxt_work_queue_add(lev->socket.read_work_queue, - lev->accept, task, lev, next); + nxt_work_queue_add(lev->socket.read_work_queue, lev->accept, task, lev, + next); } } - static nxt_conn_t * -nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev) -{ - nxt_conn_t *c; +nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev) { + nxt_conn_t *c; c = lev->next; @@ -249,16 +233,14 @@ nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev) return c; } - static void -nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev) -{ - nxt_event_engine_t *engine; +nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev) { + nxt_event_engine_t *engine; engine = task->thread->engine; nxt_work_queue_add(&engine->close_work_queue, - nxt_conn_accept_close_idle_handler, task, NULL, NULL); + nxt_conn_accept_close_idle_handler, task, NULL, NULL); nxt_timer_add(engine, &lev->timer, 100); @@ -267,39 +249,35 @@ nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev) nxt_alert(task, "new connections are not accepted within 100ms"); } - static void -nxt_conn_accept_close_idle_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_accept_close_idle_handler(nxt_task_t *task, void *obj, void *data) { nxt_uint_t times; - nxt_conn_t *c; - nxt_queue_t *idle; - nxt_queue_link_t *link, *next; - nxt_event_engine_t *engine; + nxt_conn_t *c; + nxt_queue_t *idle; + nxt_queue_link_t *link, *next; + nxt_event_engine_t *engine; - static nxt_log_moderation_t nxt_idle_close_log_moderation = { - NXT_LOG_INFO, 2, "idle connections closed", NXT_LOG_MODERATION - }; + static nxt_log_moderation_t nxt_idle_close_log_moderation + = {NXT_LOG_INFO, 2, "idle connections closed", NXT_LOG_MODERATION}; - times = 10; + times = 10; engine = task->thread->engine; - idle = &engine->idle_connections; + idle = &engine->idle_connections; - for (link = nxt_queue_last(idle); - link != nxt_queue_head(idle); - link = next) - { + for (link = nxt_queue_last(idle); link != nxt_queue_head(idle); + link = next) { next = nxt_queue_next(link); c = nxt_queue_link_data(link, nxt_conn_t, link); - nxt_debug(c->socket.task, "idle connection: %d rdy:%d", - c->socket.fd, c->socket.read_ready); + nxt_debug(c->socket.task, "idle connection: %d rdy:%d", c->socket.fd, + c->socket.read_ready); if (!c->socket.read_ready) { nxt_log_moderate(&nxt_idle_close_log_moderation, NXT_LOG_INFO, - task->log, "no available connections, " - "close idle connection"); + task->log, + "no available connections, " + "close idle connection"); c->read_state->close_handler(c->socket.task, c, c->socket.data); @@ -312,53 +290,46 @@ nxt_conn_accept_close_idle_handler(nxt_task_t *task, void *obj, void *data) } } - void nxt_conn_accept_error(nxt_task_t *task, nxt_listen_event_t *lev, - const char *accept_syscall, nxt_err_t err) -{ - static nxt_log_moderation_t nxt_accept_log_moderation = { - NXT_LOG_INFO, 2, "accept() failed", NXT_LOG_MODERATION - }; + const char *accept_syscall, nxt_err_t err) { + static nxt_log_moderation_t nxt_accept_log_moderation + = {NXT_LOG_INFO, 2, "accept() failed", NXT_LOG_MODERATION}; lev->socket.read_ready = 0; switch (err) { - case NXT_EAGAIN: nxt_debug(task, "%s(%d) %E", accept_syscall, lev->socket.fd, err); return; case ECONNABORTED: - nxt_log_moderate(&nxt_accept_log_moderation, NXT_LOG_WARN, - task->log, "%s(%d) failed %E", - accept_syscall, lev->socket.fd, err); + nxt_log_moderate(&nxt_accept_log_moderation, NXT_LOG_WARN, task->log, + "%s(%d) failed %E", accept_syscall, lev->socket.fd, err); return; case EMFILE: case ENFILE: case ENOBUFS: case ENOMEM: - nxt_alert(task, "%s(%d) failed %E", - accept_syscall, lev->socket.fd, err); + nxt_alert(task, "%s(%d) failed %E", accept_syscall, lev->socket.fd, + err); nxt_conn_accept_close_idle(task, lev); return; default: - nxt_alert(task, "%s(%d) failed %E", - accept_syscall, lev->socket.fd, err); + nxt_alert(task, "%s(%d) failed %E", accept_syscall, lev->socket.fd, + err); return; } } - static void -nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_listen_event_t *lev; +nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_listen_event_t *lev; timer = obj; @@ -374,11 +345,9 @@ nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data) lev->accept(task, lev, c); } - static void -nxt_conn_listen_event_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_fd_event_t *ev; +nxt_conn_listen_event_error(nxt_task_t *task, void *obj, void *data) { + nxt_fd_event_t *ev; ev = obj; diff --git a/src/nxt_conn_close.c b/src/nxt_conn_close.c index bdd669515..7fb9b0769 100644 --- a/src/nxt_conn_close.c +++ b/src/nxt_conn_close.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,28 +6,28 @@ #include -static void nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_close_error_ignore(nxt_task_t *task, void *obj, - void *data); - +static void +nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_close_error_ignore(nxt_task_t *task, void *obj, void *data); void -nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c) -{ - int ret; - nxt_work_queue_t *wq; - nxt_work_handler_t handler; - - static const struct linger linger_off = { - .l_onoff = 1, +nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c) { + int ret; + nxt_work_queue_t *wq; + nxt_work_handler_t handler; + + static const struct linger linger_off = { + .l_onoff = 1, .l_linger = 0, }; - nxt_debug(c->socket.task, "conn close fd:%d, to:%d", - c->socket.fd, c->socket.timedout); + nxt_debug(c->socket.task, "conn close fd:%d, to:%d", c->socket.fd, + c->socket.timedout); /* * Disable all pending write operations because on success they @@ -43,11 +42,11 @@ nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c) * This also causes sending TCP/IP RST to a peer. */ ret = setsockopt(c->socket.fd, SOL_SOCKET, SO_LINGER, &linger_off, - sizeof(struct linger)); + sizeof(struct linger)); if (nxt_slow_path(ret != 0)) { nxt_alert(c->socket.task, "setsockopt(%d, SO_LINGER) failed %E", - c->socket.fd, nxt_socket_errno); + c->socket.fd, nxt_socket_errno); } } @@ -59,25 +58,23 @@ nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c) c->socket.error_handler = nxt_conn_close_error_ignore; if (c->socket.error == 0 && !c->socket.closed && !c->socket.shutdown) { - wq = &engine->shutdown_work_queue; + wq = &engine->shutdown_work_queue; handler = nxt_conn_shutdown_handler; } else { - wq = &engine->close_work_queue; + wq = &engine->close_work_queue; handler = nxt_conn_close_handler; } nxt_work_queue_add(wq, handler, c->socket.task, c, engine); } - static void -nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_event_engine_t *engine; +nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_event_engine_t *engine; - c = obj; + c = obj; engine = data; nxt_debug(task, "conn shutdown handler fd:%d", c->socket.fd); @@ -86,19 +83,17 @@ nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data) nxt_socket_shutdown(task, c->socket.fd, SHUT_RDWR); - nxt_work_queue_add(&engine->close_work_queue, nxt_conn_close_handler, - task, c, engine); + nxt_work_queue_add(&engine->close_work_queue, nxt_conn_close_handler, task, + c, engine); } - static void -nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data) { nxt_uint_t events_pending, timers_pending; - nxt_conn_t *c; - nxt_event_engine_t *engine; + nxt_conn_t *c; + nxt_event_engine_t *engine; - c = obj; + c = obj; engine = data; nxt_debug(task, "conn close handler fd:%d", c->socket.fd); @@ -110,7 +105,7 @@ nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data) * processed before going to the kernel. */ - timers_pending = nxt_timer_delete(engine, &c->read_timer); + timers_pending = nxt_timer_delete(engine, &c->read_timer); timers_pending += nxt_timer_delete(engine, &c->write_timer); events_pending = nxt_fd_event_close(engine, &c->socket); @@ -125,25 +120,22 @@ nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data) if (timers_pending == 0) { nxt_work_queue_add(&engine->fast_work_queue, - c->write_state->ready_handler, - task, c, c->socket.data); + c->write_state->ready_handler, task, c, c->socket.data); return; } } - c->write_timer.handler = nxt_conn_close_timer_handler; + c->write_timer.handler = nxt_conn_close_timer_handler; c->write_timer.work_queue = &engine->fast_work_queue; nxt_timer_add(engine, &c->write_timer, 0); } - static void -nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_event_engine_t *engine; +nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_event_engine_t *engine; timer = obj; @@ -163,12 +155,10 @@ nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, void *data) } nxt_work_queue_add(&engine->fast_work_queue, c->write_state->ready_handler, - task, c, c->socket.data); + task, c, c->socket.data); } - static void -nxt_conn_close_error_ignore(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_close_error_ignore(nxt_task_t *task, void *obj, void *data) { nxt_debug(task, "conn close error ignore"); } diff --git a/src/nxt_conn_connect.c b/src/nxt_conn_connect.c index 220fb5f9d..fba8428a0 100644 --- a/src/nxt_conn_connect.c +++ b/src/nxt_conn_connect.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,47 +6,43 @@ #include -static nxt_err_t nxt_conn_connect_test_error(nxt_task_t *task, nxt_conn_t *c); - +static nxt_err_t +nxt_conn_connect_test_error(nxt_task_t *task, nxt_conn_t *c); void -nxt_conn_sys_socket(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_work_handler_t handler; +nxt_conn_sys_socket(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_work_handler_t handler; c = obj; if (nxt_conn_socket(task, c) == NXT_OK) { c->socket.write_work_queue = c->write_work_queue; - handler = c->io->connect; + handler = c->io->connect; } else { handler = c->write_state->error_handler; } - nxt_work_queue_add(&task->thread->engine->connect_work_queue, - handler, task, c, data); + nxt_work_queue_add(&task->thread->engine->connect_work_queue, handler, task, + c, data); } - void -nxt_conn_io_connect(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_conn_io_connect(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; nxt_work_handler_t handler; - nxt_event_engine_t *engine; - const nxt_conn_state_t *state; + nxt_event_engine_t *engine; + const nxt_conn_state_t *state; c = obj; state = c->write_state; switch (nxt_socket_connect(task, c->socket.fd, c->remote)) { - case NXT_OK: c->socket.write_ready = 1; - handler = state->ready_handler; + handler = state->ready_handler; break; case NXT_AGAIN: @@ -73,12 +68,10 @@ nxt_conn_io_connect(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(c->write_work_queue, handler, task, c, data); } - nxt_int_t -nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c) -{ - nxt_uint_t family; - nxt_socket_t s; +nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c) { + nxt_uint_t family; + nxt_socket_t s; nxt_debug(task, "event conn socket"); @@ -103,8 +96,8 @@ nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c) c->socket.fd = s; - c->socket.task = task; - c->read_timer.task = task; + c->socket.task = task; + c->read_timer.task = task; c->write_timer.task = task; if (c->local != NULL) { @@ -117,12 +110,10 @@ nxt_conn_socket(nxt_task_t *task, nxt_conn_t *c) return NXT_OK; } - void -nxt_conn_connect_test(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_connect_test(nxt_task_t *task, void *obj, void *data) { nxt_err_t err; - nxt_conn_t *c; + nxt_conn_t *c; c = obj; @@ -138,22 +129,20 @@ nxt_conn_connect_test(nxt_task_t *task, void *obj, void *data) if (err == 0) { nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, - task, c, data); + task, c, data); } else { nxt_conn_connect_error(task, c, data); } } - void -nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data) { nxt_err_t err; - nxt_conn_t *c; + nxt_conn_t *c; nxt_work_handler_t handler; - const nxt_conn_state_t *state; + const nxt_conn_state_t *state; - c = obj; + c = obj; err = c->socket.error; if (err == 0) { @@ -163,7 +152,6 @@ nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data) state = c->write_state; switch (err) { - case NXT_ECONNREFUSED: #if (NXT_LINUX) case NXT_EAGAIN: @@ -183,11 +171,9 @@ nxt_conn_connect_error(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(c->write_work_queue, handler, task, c, data); } - static nxt_err_t -nxt_conn_connect_test_error(nxt_task_t *task, nxt_conn_t *c) -{ - nxt_err_t err; +nxt_conn_connect_test_error(nxt_task_t *task, nxt_conn_t *c) { + nxt_err_t err; err = nxt_socket_error(c->socket.fd); @@ -195,8 +181,8 @@ nxt_conn_connect_test_error(nxt_task_t *task, nxt_conn_t *c) c->socket.error = err; nxt_log(task, nxt_socket_error_level(err), "connect(%d, %*s) failed %E", - c->socket.fd, (size_t) c->remote->length, - nxt_sockaddr_start(c->remote), err); + c->socket.fd, (size_t) c->remote->length, + nxt_sockaddr_start(c->remote), err); } return err; diff --git a/src/nxt_conn_proxy.c b/src/nxt_conn_proxy.c index 055a288d3..4bb09f88a 100644 --- a/src/nxt_conn_proxy.c +++ b/src/nxt_conn_proxy.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,61 +6,75 @@ #include -static void nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, +static void +nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, nxt_conn_t *source, nxt_conn_t *sink); -static void nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b); -static void nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, +static void +nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b); +static void +nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, nxt_conn_t *sink, nxt_conn_t *source); -static void nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b); -static void nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj, - void *data); -static nxt_msec_t nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data); -static void nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p, +static void +nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b); +static void +nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj, void *data); +static nxt_msec_t +nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data); +static void +nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p, nxt_conn_t *source, nxt_conn_t *sink); -static void nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data); -static void nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p); -static void nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data); - +static void +nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p); +static void +nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data); -static const nxt_conn_state_t nxt_conn_proxy_client_wait_state; -static const nxt_conn_state_t nxt_conn_proxy_client_first_read_state; -static const nxt_conn_state_t nxt_conn_proxy_peer_connect_state; -static const nxt_conn_state_t nxt_conn_proxy_peer_wait_state; -static const nxt_conn_state_t nxt_conn_proxy_client_read_state; -static const nxt_conn_state_t nxt_conn_proxy_peer_read_state; -static const nxt_conn_state_t nxt_conn_proxy_client_write_state; -static const nxt_conn_state_t nxt_conn_proxy_peer_write_state; +static const nxt_conn_state_t nxt_conn_proxy_client_wait_state; +static const nxt_conn_state_t nxt_conn_proxy_client_first_read_state; +static const nxt_conn_state_t nxt_conn_proxy_peer_connect_state; +static const nxt_conn_state_t nxt_conn_proxy_peer_wait_state; +static const nxt_conn_state_t nxt_conn_proxy_client_read_state; +static const nxt_conn_state_t nxt_conn_proxy_peer_read_state; +static const nxt_conn_state_t nxt_conn_proxy_client_write_state; +static const nxt_conn_state_t nxt_conn_proxy_peer_write_state; nxt_conn_proxy_t * -nxt_conn_proxy_create(nxt_conn_t *client) -{ - nxt_conn_t *peer; - nxt_thread_t *thr; - nxt_conn_proxy_t *p; +nxt_conn_proxy_create(nxt_conn_t *client) { + nxt_conn_t *peer; + nxt_thread_t *thr; + nxt_conn_proxy_t *p; p = nxt_mp_zget(client->mem_pool, sizeof(nxt_conn_proxy_t)); if (nxt_slow_path(p == NULL)) { @@ -75,31 +88,29 @@ nxt_conn_proxy_create(nxt_conn_t *client) thr = nxt_thread(); - client->read_work_queue = &thr->engine->read_work_queue; - client->write_work_queue = &thr->engine->write_work_queue; - client->socket.read_work_queue = &thr->engine->read_work_queue; + client->read_work_queue = &thr->engine->read_work_queue; + client->write_work_queue = &thr->engine->write_work_queue; + client->socket.read_work_queue = &thr->engine->read_work_queue; client->socket.write_work_queue = &thr->engine->write_work_queue; - peer->socket.read_work_queue = &thr->engine->read_work_queue; - peer->socket.write_work_queue = &thr->engine->write_work_queue; + peer->socket.read_work_queue = &thr->engine->read_work_queue; + peer->socket.write_work_queue = &thr->engine->write_work_queue; peer->socket.data = client->socket.data; - peer->read_work_queue = client->read_work_queue; - peer->write_work_queue = client->write_work_queue; - peer->read_timer.work_queue = client->read_work_queue; + peer->read_work_queue = client->read_work_queue; + peer->write_work_queue = client->write_work_queue; + peer->read_timer.work_queue = client->read_work_queue; peer->write_timer.work_queue = client->write_work_queue; p->client = client; - p->peer = peer; + p->peer = peer; return p; } - void -nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p) -{ - nxt_conn_t *peer; +nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p) { + nxt_conn_t *peer; /* * Peer read event: not connected, disabled. @@ -111,7 +122,7 @@ nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p) * Peer write event: waiting for connection * to be established with connect_timeout. */ - peer = p->peer; + peer = p->peer; peer->write_state = &nxt_conn_proxy_peer_connect_state; nxt_conn_connect(task->thread->engine, peer); @@ -126,29 +137,25 @@ nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p) nxt_conn_wait(p->client); } +static const nxt_conn_state_t nxt_conn_proxy_client_wait_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_client_buffer_alloc, + .close_handler = nxt_conn_proxy_close, + .error_handler = nxt_conn_proxy_error, -static const nxt_conn_state_t nxt_conn_proxy_client_wait_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_client_buffer_alloc, - .close_handler = nxt_conn_proxy_close, - .error_handler = nxt_conn_proxy_error, - - .timer_handler = nxt_conn_proxy_read_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout), + .timer_handler = nxt_conn_proxy_read_timeout, + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout), }; - static void -nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_conn_t *client; - nxt_conn_proxy_t *p; +nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_conn_t *client; + nxt_conn_proxy_t *p; client = obj; - p = data; + p = data; nxt_debug(task, "conn proxy client first read fd:%d", client->socket.fd); @@ -160,7 +167,7 @@ nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data) } p->client_buffer = b; - client->read = b; + client->read = b; if (p->peer->socket.fd != -1) { /* @@ -186,29 +193,26 @@ nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data) nxt_conn_read(task->thread->engine, client); } - -static const nxt_conn_state_t nxt_conn_proxy_client_first_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_peer_connect, - .close_handler = nxt_conn_proxy_close, - .error_handler = nxt_conn_proxy_error, - - .timer_handler = nxt_conn_proxy_read_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout), - .timer_autoreset = 1, +static const nxt_conn_state_t + nxt_conn_proxy_client_first_read_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_peer_connect, + .close_handler = nxt_conn_proxy_close, + .error_handler = nxt_conn_proxy_error, + + .timer_handler = nxt_conn_proxy_read_timeout, + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout), + .timer_autoreset = 1, }; - static void -nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *client; - nxt_conn_proxy_t *p; +nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *client; + nxt_conn_proxy_t *p; client = obj; - p = data; + p = data; /* * Client read event: waiting, no timeout. @@ -224,29 +228,25 @@ nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, void *data) nxt_conn_connect(task->thread->engine, p->peer); } +static const nxt_conn_state_t nxt_conn_proxy_peer_connect_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_connected, + .close_handler = nxt_conn_proxy_refused, + .error_handler = nxt_conn_proxy_error, -static const nxt_conn_state_t nxt_conn_proxy_peer_connect_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_connected, - .close_handler = nxt_conn_proxy_refused, - .error_handler = nxt_conn_proxy_error, - - .timer_handler = nxt_conn_proxy_write_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, connect_timeout), - .timer_autoreset = 1, + .timer_handler = nxt_conn_proxy_write_timeout, + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, connect_timeout), + .timer_autoreset = 1, }; - static void -nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *client, *peer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *client, *peer; + nxt_conn_proxy_t *p; peer = obj; - p = data; + p = data; nxt_debug(task, "conn proxy connected fd:%d", peer->socket.fd); @@ -257,7 +257,7 @@ nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data) /* Peer read event: waiting with peer_wait_timeout. */ - peer->read_state = &nxt_conn_proxy_peer_wait_state; + peer->read_state = &nxt_conn_proxy_peer_wait_state; peer->write_state = &nxt_conn_proxy_peer_write_state; nxt_conn_wait(peer); @@ -265,7 +265,7 @@ nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data) if (p->client_buffer != NULL) { client = p->client; - client->read_state = &nxt_conn_proxy_client_read_state; + client->read_state = &nxt_conn_proxy_client_read_state; client->write_state = &nxt_conn_proxy_client_write_state; /* * Send a client read data to the connected peer. @@ -275,29 +275,24 @@ nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data) } } - -static const nxt_conn_state_t nxt_conn_proxy_peer_wait_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_conn_proxy_peer_wait_state nxt_aligned(64) = { .ready_handler = nxt_conn_proxy_peer_read, .close_handler = nxt_conn_proxy_close, .error_handler = nxt_conn_proxy_error, .timer_handler = nxt_conn_proxy_read_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, peer_wait_timeout), + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, peer_wait_timeout), }; - static void -nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_conn_t *peer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_conn_t *peer; + nxt_conn_proxy_t *p; peer = obj; - p = data; + p = data; nxt_debug(task, "conn proxy peer read fd:%d", peer->socket.fd); @@ -309,11 +304,11 @@ nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data) } p->peer_buffer = b; - peer->read = b; + peer->read = b; p->client->write_state = &nxt_conn_proxy_client_write_state; - peer->read_state = &nxt_conn_proxy_peer_read_state; - peer->write_state = &nxt_conn_proxy_peer_write_state; + peer->read_state = &nxt_conn_proxy_peer_read_state; + peer->write_state = &nxt_conn_proxy_peer_write_state; /* * Client read event: waiting, no timeout. @@ -324,75 +319,62 @@ nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data) nxt_conn_read(task->thread->engine, peer); } - -static const nxt_conn_state_t nxt_conn_proxy_client_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_client_read_ready, - .close_handler = nxt_conn_proxy_close, - .error_handler = nxt_conn_proxy_read_error, +static const nxt_conn_state_t nxt_conn_proxy_client_read_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_client_read_ready, + .close_handler = nxt_conn_proxy_close, + .error_handler = nxt_conn_proxy_read_error, }; - static void -nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *client; - nxt_conn_proxy_t *p; +nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *client; + nxt_conn_proxy_t *p; client = obj; - p = data; + p = data; nxt_debug(task, "conn proxy client read ready fd:%d", client->socket.fd); nxt_conn_proxy_read_process(task, p, client, p->peer); } - -static const nxt_conn_state_t nxt_conn_proxy_peer_read_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_conn_proxy_peer_read_state nxt_aligned(64) = { .ready_handler = nxt_conn_proxy_peer_read_ready, .close_handler = nxt_conn_proxy_close, .error_handler = nxt_conn_proxy_read_error, }; - static void -nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *peer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *peer; + nxt_conn_proxy_t *p; peer = obj; - p = data; + p = data; nxt_debug(task, "conn proxy peer read ready fd:%d", peer->socket.fd); nxt_conn_proxy_read_process(task, p, peer, p->client); } - static void nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, - nxt_conn_t *source, nxt_conn_t *sink) -{ - nxt_buf_t *rb, *wb; + nxt_conn_t *source, nxt_conn_t *sink) { + nxt_buf_t *rb, *wb; if (sink->socket.error != 0) { - nxt_debug(task, "conn proxy sink fd:%d error:%d", - sink->socket.fd, sink->socket.error); + nxt_debug(task, "conn proxy sink fd:%d error:%d", sink->socket.fd, + sink->socket.error); nxt_conn_proxy_write_error(task, sink, sink->socket.data); return; } while (source->read != NULL) { - rb = source->read; if (rb->mem.pos != rb->mem.free) { - /* Add a read part to a write chain. */ wb = nxt_buf_mem_alloc(source->mem_pool, 0, 0); @@ -402,12 +384,12 @@ nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, return; } - wb->mem.pos = rb->mem.pos; - wb->mem.free = rb->mem.free; + wb->mem.pos = rb->mem.pos; + wb->mem.free = rb->mem.free; wb->mem.start = rb->mem.pos; - wb->mem.end = rb->mem.free; + wb->mem.end = rb->mem.free; - rb->mem.pos = rb->mem.free; + rb->mem.pos = rb->mem.free; rb->mem.start = rb->mem.free; nxt_conn_proxy_write_add(sink, wb); @@ -415,7 +397,7 @@ nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, if (rb->mem.start != rb->mem.end) { nxt_work_queue_add(source->read_work_queue, nxt_conn_proxy_read, - task, source, source->socket.data); + task, source, source->socket.data); break; } @@ -428,11 +410,9 @@ nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p, } } - static void -nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b) -{ - nxt_buf_t *first, *second, *prev; +nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b) { + nxt_buf_t *first, *second, *prev; first = c->write; @@ -452,7 +432,6 @@ nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b) second = first->next; if (second == NULL) { - if (first->mem.end != b->mem.start) { first->next = b; return; @@ -468,7 +447,7 @@ nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b) if (second->mem.end != b->mem.start) { nxt_thread_log_alert("event conn proxy write: second buffer end:%p " "is not equal to added buffer start:%p", - second->mem.end, b->mem.start); + second->mem.end, b->mem.start); return; } @@ -481,20 +460,18 @@ nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b) } prev->mem.free = b->mem.end; - prev->mem.end = b->mem.end; + prev->mem.end = b->mem.end; nxt_buf_free(c->mem_pool, b); } - static void -nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *source, *sink; - nxt_conn_proxy_t *p; +nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *source, *sink; + nxt_conn_proxy_t *p; source = obj; - p = data; + p = data; nxt_debug(task, "conn proxy read fd:%d", source->socket.fd); @@ -507,75 +484,63 @@ nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data) } } +static const nxt_conn_state_t nxt_conn_proxy_client_write_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_client_write_ready, + .error_handler = nxt_conn_proxy_write_error, -static const nxt_conn_state_t nxt_conn_proxy_client_write_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_client_write_ready, - .error_handler = nxt_conn_proxy_write_error, - - .timer_handler = nxt_conn_proxy_write_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, client_write_timeout), - .timer_autoreset = 1, + .timer_handler = nxt_conn_proxy_write_timeout, + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, client_write_timeout), + .timer_autoreset = 1, }; - static void -nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *client; - nxt_conn_proxy_t *p; +nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *client; + nxt_conn_proxy_t *p; client = obj; - p = data; + p = data; nxt_debug(task, "conn proxy client write ready fd:%d", client->socket.fd); nxt_conn_proxy_write_process(task, p, client, p->peer); } +static const nxt_conn_state_t nxt_conn_proxy_peer_write_state nxt_aligned(64) + = { + .ready_handler = nxt_conn_proxy_peer_write_ready, + .error_handler = nxt_conn_proxy_write_error, -static const nxt_conn_state_t nxt_conn_proxy_peer_write_state - nxt_aligned(64) = -{ - .ready_handler = nxt_conn_proxy_peer_write_ready, - .error_handler = nxt_conn_proxy_write_error, - - .timer_handler = nxt_conn_proxy_write_timeout, - .timer_value = nxt_conn_proxy_timeout_value, - .timer_data = offsetof(nxt_conn_proxy_t, peer_write_timeout), - .timer_autoreset = 1, + .timer_handler = nxt_conn_proxy_write_timeout, + .timer_value = nxt_conn_proxy_timeout_value, + .timer_data = offsetof(nxt_conn_proxy_t, peer_write_timeout), + .timer_autoreset = 1, }; - static void -nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *peer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *peer; + nxt_conn_proxy_t *p; peer = obj; - p = data; + p = data; nxt_debug(task, "conn proxy peer write ready fd:%d", peer->socket.fd); nxt_conn_proxy_write_process(task, p, peer, p->client); } - static void nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, - nxt_conn_t *sink, nxt_conn_t *source) -{ - nxt_buf_t *rb, *wb; + nxt_conn_t *sink, nxt_conn_t *source) { + nxt_buf_t *rb, *wb; while (sink->write != NULL) { - wb = sink->write; if (nxt_buf_is_sync(wb)) { - /* A sync buffer marks the end of stream. */ sink->write = NULL; @@ -585,7 +550,6 @@ nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, } if (wb->mem.start != wb->mem.pos) { - /* Add a written part to a read chain. */ rb = nxt_buf_mem_alloc(sink->mem_pool, 0, 0); @@ -595,10 +559,10 @@ nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, return; } - rb->mem.pos = wb->mem.start; - rb->mem.free = wb->mem.start; + rb->mem.pos = wb->mem.start; + rb->mem.free = wb->mem.start; rb->mem.start = wb->mem.start; - rb->mem.end = wb->mem.pos; + rb->mem.end = wb->mem.pos; wb->mem.start = wb->mem.pos; @@ -615,15 +579,13 @@ nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p, nxt_buf_free(sink->mem_pool, wb); } - nxt_work_queue_add(source->read_work_queue, nxt_conn_proxy_read, - task, source, source->socket.data); + nxt_work_queue_add(source->read_work_queue, nxt_conn_proxy_read, task, + source, source->socket.data); } - static void -nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) -{ - nxt_buf_t *first, *second; +nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) { + nxt_buf_t *first, *second; first = c->read; @@ -644,14 +606,13 @@ nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) second = first->next; if (second == NULL) { - if (first->mem.start == b->mem.end) { /* * The added buffer is just before the first buffer, so expand * the first buffer to the beginning of the added buffer. */ - first->mem.pos = b->mem.start; - first->mem.free = b->mem.start; + first->mem.pos = b->mem.start; + first->mem.free = b->mem.start; first->mem.start = b->mem.start; } else if (first->mem.end == b->mem.start) { @@ -670,7 +631,7 @@ nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) if (second->mem.end != b->mem.start) { nxt_thread_log_alert("event conn proxy read: second buffer end:%p " "is not equal to added buffer start:%p", - second->mem.end, b->mem.start); + second->mem.end, b->mem.start); return; } @@ -685,10 +646,10 @@ nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) * The second buffer is just before the first buffer, so expand * the first buffer to the beginning of the second buffer. */ - first->mem.pos = second->mem.start; - first->mem.free = second->mem.start; + first->mem.pos = second->mem.start; + first->mem.free = second->mem.start; first->mem.start = second->mem.start; - first->next = NULL; + first->next = NULL; nxt_buf_free(c->mem_pool, second); } @@ -697,16 +658,14 @@ nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b) nxt_buf_free(c->mem_pool, b); } - static void -nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_conn_t *source, *sink; - nxt_conn_proxy_t *p; +nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_conn_t *source, *sink; + nxt_conn_proxy_t *p; source = obj; - p = data; + p = data; nxt_debug(task, "conn proxy close fd:%d", source->socket.fd); @@ -727,12 +686,10 @@ nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data) nxt_buf_chain_add(&sink->write, b); } - static void -nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_conn_proxy_t *p; +nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_conn_proxy_t *p; c = obj; p = data; @@ -742,58 +699,50 @@ nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data) nxt_conn_proxy_close(task, c, p); } - static void -nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; - c = nxt_read_timer_conn(timer); + c = nxt_read_timer_conn(timer); c->socket.timedout = 1; - c->socket.closed = 1; + c->socket.closed = 1; nxt_debug(task, "conn proxy read timeout fd:%d", c->socket.fd); nxt_conn_proxy_close(task, c, c->socket.data); } - static void -nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; - c = nxt_write_timer_conn(timer); + c = nxt_write_timer_conn(timer); c->socket.timedout = 1; - c->socket.closed = 1; + c->socket.closed = 1; nxt_debug(task, "conn proxy write timeout fd:%d", c->socket.fd); nxt_conn_proxy_close(task, c, c->socket.data); } - static nxt_msec_t -nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data) -{ +nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data) { return nxt_value_at(nxt_msec_t, c->socket.data, data); } - static void -nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *peer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *peer; + nxt_conn_proxy_t *p; peer = obj; - p = data; + p = data; nxt_debug(task, "conn proxy refused fd:%d", peer->socket.fd); @@ -806,30 +755,28 @@ nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data) p->retries--; nxt_socket_close(task, peer->socket.fd); - peer->socket.fd = -1; + peer->socket.fd = -1; peer->socket.error = 0; p->delayed = 1; peer->write_timer.handler = nxt_conn_proxy_reconnect_handler; nxt_timer_add(task->thread->engine, &peer->write_timer, - p->reconnect_timeout); + p->reconnect_timeout); } - static void -nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *peer; - nxt_timer_t *timer; - nxt_conn_proxy_t *p; +nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *peer; + nxt_timer_t *timer; + nxt_conn_proxy_t *p; timer = obj; nxt_debug(task, "conn proxy reconnect timer"); peer = nxt_write_timer_conn(timer); - p = peer->socket.data; + p = peer->socket.data; if (p->client->socket.closed) { nxt_conn_proxy_complete(task, p); @@ -846,20 +793,17 @@ nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, void *data) nxt_conn_connect(task->thread->engine, peer); } - static void nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p, - nxt_conn_t *source, nxt_conn_t *sink) -{ - nxt_buf_t *b; + nxt_conn_t *source, nxt_conn_t *sink) { + nxt_buf_t *b; nxt_debug(source->socket.task, - "conn proxy shutdown source fd:%d cl:%d err:%d", - source->socket.fd, source->socket.closed, source->socket.error); + "conn proxy shutdown source fd:%d cl:%d err:%d", source->socket.fd, + source->socket.closed, source->socket.error); - nxt_debug(sink->socket.task, - "conn proxy shutdown sink fd:%d cl:%d err:%d", - sink->socket.fd, sink->socket.closed, sink->socket.error); + nxt_debug(sink->socket.task, "conn proxy shutdown sink fd:%d cl:%d err:%d", + sink->socket.fd, sink->socket.closed, sink->socket.error); if (!p->connected || p->delayed) { nxt_conn_proxy_complete(task, p); @@ -872,8 +816,7 @@ nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p, } if (sink->socket.error != 0 - || (sink->socket.closed && source->write == NULL)) - { + || (sink->socket.closed && source->write == NULL)) { /* The opposite direction also has been already closed. */ nxt_conn_proxy_complete(task, p); return; @@ -886,12 +829,10 @@ nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p, nxt_mp_free(source->mem_pool, b); } - static void -nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_conn_proxy_t *p; +nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_conn_proxy_t *p; c = obj; p = data; @@ -901,15 +842,13 @@ nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data) nxt_conn_proxy_close(task, c, p); } - static void -nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *source, *sink; - nxt_conn_proxy_t *p; +nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *source, *sink; + nxt_conn_proxy_t *p; sink = obj; - p = data; + p = data; nxt_debug(task, "conn proxy write error fd:%d", sink->socket.fd); @@ -929,23 +868,18 @@ nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data) } } - -static const nxt_conn_state_t nxt_conn_proxy_close_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_conn_proxy_close_state nxt_aligned(64) = { .ready_handler = nxt_conn_proxy_completion, }; - static void -nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p) -{ - nxt_event_engine_t *engine; +nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p) { + nxt_event_engine_t *engine; engine = task->thread->engine; nxt_debug(p->client->socket.task, "conn proxy complete %d:%d", - p->client->socket.fd, p->peer->socket.fd); + p->client->socket.fd, p->peer->socket.fd); if (p->delayed) { p->delayed = 0; @@ -953,7 +887,7 @@ nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p) } if (p->client->socket.fd != -1) { - p->retain = 1; + p->retain = 1; p->client->write_state = &nxt_conn_proxy_close_state; nxt_conn_close(engine, p->client); } @@ -965,16 +899,14 @@ nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p) } } - static void -nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_proxy_t *p; +nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data) { + nxt_conn_proxy_t *p; p = data; nxt_debug(p->client->socket.task, "conn proxy completion %d:%d:%d", - p->retain, p->client->socket.fd, p->peer->socket.fd); + p->retain, p->client->socket.fd, p->peer->socket.fd); p->retain--; diff --git a/src/nxt_conn_read.c b/src/nxt_conn_read.c index 3285abcd1..39a8a3035 100644 --- a/src/nxt_conn_read.c +++ b/src/nxt_conn_read.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,26 +5,24 @@ #include - void -nxt_conn_wait(nxt_conn_t *c) -{ - nxt_event_engine_t *engine; - const nxt_conn_state_t *state; +nxt_conn_wait(nxt_conn_t *c) { + nxt_event_engine_t *engine; + const nxt_conn_state_t *state; - nxt_debug(c->socket.task, "conn wait fd:%d rdy:%d", - c->socket.fd, c->socket.read_ready); + nxt_debug(c->socket.task, "conn wait fd:%d rdy:%d", c->socket.fd, + c->socket.read_ready); engine = c->socket.task->thread->engine; - state = c->read_state; + state = c->read_state; if (c->socket.read_ready) { nxt_work_queue_add(&engine->fast_work_queue, state->ready_handler, - c->socket.task, c, c->socket.data); + c->socket.task, c, c->socket.data); return; } - c->socket.read_handler = state->ready_handler; + c->socket.read_handler = state->ready_handler; c->socket.error_handler = state->error_handler; nxt_conn_timer(engine, c, state, &c->read_timer); @@ -33,21 +30,18 @@ nxt_conn_wait(nxt_conn_t *c) nxt_fd_event_enable_read(engine, &c->socket); } - void -nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) { ssize_t n; - nxt_conn_t *c; - nxt_event_engine_t *engine; + nxt_conn_t *c; + nxt_event_engine_t *engine; nxt_work_handler_t handler; - const nxt_conn_state_t *state; + const nxt_conn_state_t *state; c = obj; - nxt_debug(task, "conn read fd:%d rdy:%d cl:%d er:%d bl:%d", - c->socket.fd, c->socket.read_ready, c->socket.closed, - c->socket.error, c->block_read); + nxt_debug(task, "conn read fd:%d rdy:%d cl:%d er:%d bl:%d", c->socket.fd, + c->socket.read_ready, c->socket.closed, c->socket.error, c->block_read); if (c->socket.error != 0 || c->block_read) { return; @@ -59,17 +53,16 @@ nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) * Here c->io->read() is assigned instead of direct nxt_conn_io_read() * because the function can be called by nxt_kqueue_conn_io_read(). */ - c->socket.read_handler = c->io->read; - state = c->read_state; + c->socket.read_handler = c->io->read; + state = c->read_state; c->socket.error_handler = state->error_handler; if (c->socket.read_ready) { - if (state->io_read_handler == NULL) { n = c->io->recvbuf(c, c->read); } else { - n = state->io_read_handler(task, c); + n = state->io_read_handler(task, c); /* The state can be changed by io_read_handler. */ state = c->read_state; } @@ -85,8 +78,8 @@ nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) nxt_timer_disable(engine, &c->read_timer); } - nxt_work_queue_add(c->read_work_queue, - state->ready_handler, task, c, data); + nxt_work_queue_add(c->read_work_queue, state->ready_handler, task, + c, data); return; } @@ -97,8 +90,8 @@ nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) nxt_fd_event_block_read(engine, &c->socket); nxt_timer_disable(engine, &c->read_timer); - nxt_work_queue_add(&engine->fast_work_queue, - handler, task, c, data); + nxt_work_queue_add(&engine->fast_work_queue, handler, task, c, + data); return; } @@ -127,20 +120,18 @@ nxt_conn_io_read(nxt_task_t *task, void *obj, void *data) } } - ssize_t -nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) -{ - ssize_t n; - nxt_err_t err; - nxt_uint_t niov; - struct iovec iov[NXT_IOBUF_MAX]; - nxt_recvbuf_coalesce_t rb; - - rb.buf = b; +nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) { + ssize_t n; + nxt_err_t err; + nxt_uint_t niov; + struct iovec iov[NXT_IOBUF_MAX]; + nxt_recvbuf_coalesce_t rb; + + rb.buf = b; rb.iobuf = iov; - rb.nmax = NXT_IOBUF_MAX; - rb.size = 0; + rb.nmax = NXT_IOBUF_MAX; + rb.size = 0; niov = nxt_recvbuf_mem_coalesce(&rb); @@ -149,7 +140,7 @@ nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) return nxt_conn_io_recv(c, iov->iov_base, iov->iov_len, 0); } - for ( ;; ) { + for (;;) { n = readv(c->socket.fd, iov, niov); err = (n == -1) ? nxt_socket_errno : 0; @@ -165,7 +156,7 @@ nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) } if (n == 0) { - c->socket.closed = 1; + c->socket.closed = 1; c->socket.read_ready = 0; return n; } @@ -173,7 +164,6 @@ nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(c->socket.task, "readv() %E", err); c->socket.read_ready = 0; @@ -186,27 +176,25 @@ nxt_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "readv(%d, %ui) failed %E", c->socket.fd, niov, err); + "readv(%d, %ui) failed %E", c->socket.fd, niov, err); return NXT_ERROR; } } } - ssize_t -nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags) -{ - ssize_t n; - nxt_err_t err; +nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags) { + ssize_t n; + nxt_err_t err; - for ( ;; ) { + for (;;) { n = recv(c->socket.fd, buf, size, flags); err = (n == -1) ? nxt_socket_errno : 0; - nxt_debug(c->socket.task, "recv(%d, %p, %uz, 0x%ui): %z", - c->socket.fd, buf, size, flags, n); + nxt_debug(c->socket.task, "recv(%d, %p, %uz, 0x%ui): %z", c->socket.fd, + buf, size, flags, n); if (n > 0) { if ((size_t) n < size && (flags & MSG_PEEK) == 0) { @@ -229,7 +217,6 @@ nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags) /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(c->socket.task, "recv() %E", err); c->socket.read_ready = 0; @@ -243,8 +230,8 @@ nxt_conn_io_recv(nxt_conn_t *c, void *buf, size_t size, nxt_uint_t flags) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "recv(%d, %p, %uz, %ui) failed %E", - c->socket.fd, buf, size, flags, err); + "recv(%d, %p, %uz, %ui) failed %E", c->socket.fd, buf, size, + flags, err); return NXT_ERROR; } diff --git a/src/nxt_conn_write.c b/src/nxt_conn_write.c index 7d0a579f2..52e81cd1c 100644 --- a/src/nxt_conn_write.c +++ b/src/nxt_conn_write.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,25 +6,25 @@ #include -static void nxt_conn_write_timer_handler(nxt_task_t *task, void *obj, - void *data); -static ssize_t nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb); -static ssize_t nxt_sendfile(int fd, int s, off_t pos, size_t size); - +static void +nxt_conn_write_timer_handler(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb); +static ssize_t +nxt_sendfile(int fd, int s, off_t pos, size_t size); void -nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) -{ +nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) { ssize_t ret; - nxt_buf_t *b; - nxt_conn_t *c; + nxt_buf_t *b; + nxt_conn_t *c; nxt_sendbuf_t sb; - nxt_event_engine_t *engine; + nxt_event_engine_t *engine; c = obj; - nxt_debug(task, "conn write fd:%d er:%d bl:%d", - c->socket.fd, c->socket.error, c->block_write); + nxt_debug(task, "conn write fd:%d er:%d bl:%d", c->socket.fd, + c->socket.error, c->block_write); if (c->socket.error != 0 || c->block_write) { goto error; @@ -43,29 +42,29 @@ nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) b = c->write; sb.socket = c->socket.fd; - sb.error = 0; - sb.sent = 0; - sb.size = 0; - sb.buf = b; + sb.error = 0; + sb.sent = 0; + sb.size = 0; + sb.buf = b; #if (NXT_TLS) sb.tls = c->u.tls; #endif sb.limit = 10 * 1024 * 1024; sb.ready = 1; - sb.sync = 0; + sb.sync = 0; do { ret = c->io->sendbuf(task, &sb); c->socket.write_ready = sb.ready; - c->socket.error = sb.error; + c->socket.error = sb.error; if (ret < 0) { /* ret == NXT_AGAIN || ret == NXT_ERROR. */ break; } - sb.sent += ret; + sb.sent += ret; sb.limit -= ret; b = nxt_sendbuf_update(b, ret); @@ -93,7 +92,6 @@ nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) } if (ret != NXT_ERROR) { - if (sb.limit == 0) { /* * Postpone writing until next event poll to allow to @@ -126,7 +124,7 @@ nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) */ c->sent += sb.sent; nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, - task, c, data); + task, c, data); return; } @@ -138,33 +136,29 @@ nxt_conn_io_write(nxt_task_t *task, void *obj, void *data) error: - nxt_work_queue_add(c->write_work_queue, c->write_state->error_handler, - task, c, data); + nxt_work_queue_add(c->write_work_queue, c->write_state->error_handler, task, + c, data); } - static void -nxt_conn_write_timer_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_conn_write_timer_handler(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; nxt_debug(task, "conn write timer"); - c = nxt_write_timer_conn(timer); + c = nxt_write_timer_conn(timer); c->delayed = 0; c->io->write(task, c, c->socket.data); } - ssize_t -nxt_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) -{ - nxt_uint_t niov; - struct iovec iov[NXT_IOBUF_MAX]; +nxt_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) { + nxt_uint_t niov; + struct iovec iov[NXT_IOBUF_MAX]; niov = nxt_sendbuf_mem_coalesce0(task, sb, iov, NXT_IOBUF_MAX); @@ -186,26 +180,24 @@ nxt_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) return nxt_conn_io_writev(task, sb, iov, niov); } - static ssize_t -nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb) -{ +nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb) { size_t size; ssize_t n; - nxt_buf_t *b; + nxt_buf_t *b; nxt_err_t err; b = sb->buf; - for ( ;; ) { + for (;;) { size = b->file_end - b->file_pos; n = nxt_sendfile(b->file->fd, sb->socket, b->file_pos, size); err = (n == -1) ? nxt_errno : 0; - nxt_debug(task, "sendfile(%FD, %d, @%O, %uz): %z", - b->file->fd, sb->socket, b->file_pos, size, n); + nxt_debug(task, "sendfile(%FD, %d, @%O, %uz): %z", b->file->fd, + sb->socket, b->file_pos, size, n); if (n > 0) { if (n < (ssize_t) size) { @@ -217,7 +209,7 @@ nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb) if (nxt_slow_path(n == 0)) { nxt_alert(task, "sendfile() reported that file was truncated at %O", - b->file_pos); + b->file_pos); return NXT_ERROR; } @@ -225,7 +217,6 @@ nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb) /* n == -1 */ switch (err) { - case NXT_EAGAIN: sb->ready = 0; nxt_debug(task, "sendfile() %E", err); @@ -239,19 +230,17 @@ nxt_conn_io_sendfile(nxt_task_t *task, nxt_sendbuf_t *sb) default: sb->error = err; nxt_log(task, nxt_socket_error_level(err), - "sendfile(%FD, %d, @%O, %uz) failed %E", - b->file->fd, sb->socket, b->file_pos, size, err); + "sendfile(%FD, %d, @%O, %uz) failed %E", b->file->fd, + sb->socket, b->file_pos, size, err); return NXT_ERROR; } } } - static ssize_t -nxt_sendfile(int fd, int s, off_t pos, size_t size) -{ - ssize_t res; +nxt_sendfile(int fd, int s, off_t pos, size_t size) { + ssize_t res; #if (NXT_HAVE_MACOSX_SENDFILE) @@ -275,14 +264,14 @@ nxt_sendfile(int fd, int s, off_t pos, size_t size) #else - int err; - void *map; - off_t page_off; + int err; + void *map; + off_t page_off; page_off = pos % nxt_pagesize; map = nxt_mem_mmap(NULL, size + page_off, PROT_READ, MAP_SHARED, fd, - pos - page_off); + pos - page_off); if (nxt_slow_path(map == MAP_FAILED)) { return -1; } @@ -299,20 +288,18 @@ nxt_sendfile(int fd, int s, off_t pos, size_t size) return res; } - ssize_t nxt_conn_io_writev(nxt_task_t *task, nxt_sendbuf_t *sb, struct iovec *iov, - nxt_uint_t niov) -{ - ssize_t n; - nxt_err_t err; + nxt_uint_t niov) { + ssize_t n; + nxt_err_t err; if (niov == 1) { /* Disposal of surplus kernel iovec copy-in operation. */ return nxt_conn_io_send(task, sb, iov[0].iov_base, iov[0].iov_len); } - for ( ;; ) { + for (;;) { n = writev(sb->socket, iov, niov); err = (n == -1) ? nxt_socket_errno : 0; @@ -326,7 +313,6 @@ nxt_conn_io_writev(nxt_task_t *task, nxt_sendbuf_t *sb, struct iovec *iov, /* n == -1 */ switch (err) { - case NXT_EAGAIN: sb->ready = 0; nxt_debug(task, "writev() %E", err); @@ -340,21 +326,19 @@ nxt_conn_io_writev(nxt_task_t *task, nxt_sendbuf_t *sb, struct iovec *iov, default: sb->error = err; nxt_log(task, nxt_socket_error_level(err), - "writev(%d, %ui) failed %E", sb->socket, niov, err); + "writev(%d, %ui) failed %E", sb->socket, niov, err); return NXT_ERROR; } } } - ssize_t -nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, size_t size) -{ - ssize_t n; - nxt_err_t err; +nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, size_t size) { + ssize_t n; + nxt_err_t err; - for ( ;; ) { + for (;;) { n = send(sb->socket, buf, size, 0); err = (n == -1) ? nxt_socket_errno : 0; @@ -368,7 +352,6 @@ nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, size_t size) /* n == -1 */ switch (err) { - case NXT_EAGAIN: sb->ready = 0; nxt_debug(task, "send() %E", err); @@ -382,21 +365,19 @@ nxt_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, size_t size) default: sb->error = err; nxt_log(task, nxt_socket_error_level(err), - "send(%d, %p, %uz) failed %E", sb->socket, buf, size, err); + "send(%d, %p, %uz) failed %E", sb->socket, buf, size, err); return NXT_ERROR; } } } - /* Obsolete interfaces. */ size_t -nxt_event_conn_write_limit(nxt_conn_t *c) -{ +nxt_event_conn_write_limit(nxt_conn_t *c) { ssize_t limit, correction; - nxt_event_write_rate_t *rate; + nxt_event_write_rate_t *rate; rate = c->rate; @@ -404,11 +385,11 @@ nxt_event_conn_write_limit(nxt_conn_t *c) return c->max_chunk; } - limit = rate->limit; + limit = rate->limit; correction = limit - (size_t) rate->average; nxt_debug(c->socket.task, "event conn correction:%z average:%0.3f", - correction, rate->average); + correction, rate->average); limit += correction; @@ -418,33 +399,29 @@ nxt_event_conn_write_limit(nxt_conn_t *c) if (rate->limit_after != 0) { limit += rate->limit_after; - limit = nxt_min((size_t) limit, rate->max_limit); + limit = nxt_min((size_t) limit, rate->max_limit); } return nxt_min((size_t) limit, c->max_chunk); } - nxt_bool_t nxt_event_conn_write_delayed(nxt_event_engine_t *engine, nxt_conn_t *c, - size_t sent) -{ + size_t sent) { return 0; } - ssize_t -nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, size_t limit) -{ - nxt_uint_t niob; - struct iovec iob[NXT_IOBUF_MAX]; - nxt_sendbuf_coalesce_t sb; +nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, size_t limit) { + nxt_uint_t niob; + struct iovec iob[NXT_IOBUF_MAX]; + nxt_sendbuf_coalesce_t sb; - sb.buf = b; + sb.buf = b; sb.iobuf = iob; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; niob = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -456,19 +433,17 @@ nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, size_t limit) return nxt_event_conn_io_writev(c, iob, niob); } - ssize_t -nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob) -{ - ssize_t n; - nxt_err_t err; +nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob) { + ssize_t n; + nxt_err_t err; if (niob == 1) { /* Disposal of surplus kernel iovec copy-in operation. */ return nxt_event_conn_io_send(c, iob->iov_base, iob->iov_len); } - for ( ;; ) { + for (;;) { n = writev(c->socket.fd, iob, niob); err = (n == -1) ? nxt_socket_errno : 0; @@ -482,7 +457,6 @@ nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob) /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(c->socket.task, "writev() %E", err); c->socket.write_ready = 0; @@ -495,26 +469,24 @@ nxt_event_conn_io_writev(nxt_conn_t *c, nxt_iobuf_t *iob, nxt_uint_t niob) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "writev(%d, %ui) failed %E", c->socket.fd, niob, err); + "writev(%d, %ui) failed %E", c->socket.fd, niob, err); return NXT_ERROR; } } } - ssize_t -nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size) -{ - ssize_t n; - nxt_err_t err; +nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size) { + ssize_t n; + nxt_err_t err; - for ( ;; ) { + for (;;) { n = send(c->socket.fd, buf, size, 0); err = (n == -1) ? nxt_socket_errno : 0; - nxt_debug(c->socket.task, "send(%d, %p, %uz): %z", - c->socket.fd, buf, size, n); + nxt_debug(c->socket.task, "send(%d, %p, %uz): %z", c->socket.fd, buf, + size, n); if (n > 0) { return n; @@ -523,7 +495,6 @@ nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size) /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(c->socket.task, "send() %E", err); c->socket.write_ready = 0; @@ -536,8 +507,7 @@ nxt_event_conn_io_send(nxt_conn_t *c, void *buf, size_t size) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "send(%d, %p, %uz) failed %E", - c->socket.fd, buf, size, err); + "send(%d, %p, %uz) failed %E", c->socket.fd, buf, size, err); return NXT_ERROR; } } diff --git a/src/nxt_controller.c b/src/nxt_controller.c index 1ffcf815a..17404d76f 100644 --- a/src/nxt_controller.c +++ b/src/nxt_controller.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -13,171 +12,192 @@ #include #include - typedef struct { - nxt_conf_value_t *root; - nxt_mp_t *pool; + nxt_conf_value_t *root; + nxt_mp_t *pool; } nxt_controller_conf_t; - typedef struct { - nxt_http_request_parse_t parser; - size_t length; - nxt_controller_conf_t conf; - nxt_conn_t *conn; - nxt_queue_link_t link; + nxt_http_request_parse_t parser; + size_t length; + nxt_controller_conf_t conf; + nxt_conn_t *conn; + nxt_queue_link_t link; } nxt_controller_request_t; - typedef struct { nxt_uint_t status; - nxt_conf_value_t *conf; + nxt_conf_value_t *conf; - u_char *title; - nxt_str_t detail; - ssize_t offset; - nxt_uint_t line; - nxt_uint_t column; + u_char *title; + nxt_str_t detail; + ssize_t offset; + nxt_uint_t line; + nxt_uint_t column; } nxt_controller_response_t; +static nxt_int_t +nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp); +static nxt_int_t +nxt_controller_file_read(nxt_task_t *task, const char *name, nxt_str_t *str, + nxt_mp_t *mp); +static nxt_int_t +nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data); +static void +nxt_controller_process_new_port_handler(nxt_task_t *task, + nxt_port_recv_msg_t *msg); +static void +nxt_controller_send_current_conf(nxt_task_t *task); +static void +nxt_controller_router_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_controller_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static nxt_int_t +nxt_controller_conf_default(void); +static void +nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_controller_flush_requests(nxt_task_t *task); +static nxt_int_t +nxt_controller_conf_send(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf, + nxt_port_rpc_handler_t handler, void *data); + +static void +nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data); +static nxt_msec_t +nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data); +static void +nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data); -static nxt_int_t nxt_controller_prefork(nxt_task_t *task, - nxt_process_t *process, nxt_mp_t *mp); -static nxt_int_t nxt_controller_file_read(nxt_task_t *task, const char *name, - nxt_str_t *str, nxt_mp_t *mp); -static nxt_int_t nxt_controller_start(nxt_task_t *task, - nxt_process_data_t *data); -static void nxt_controller_process_new_port_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_controller_send_current_conf(nxt_task_t *task); -static void nxt_controller_router_ready_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_controller_remove_pid_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static nxt_int_t nxt_controller_conf_default(void); -static void nxt_controller_conf_init_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_controller_flush_requests(nxt_task_t *task); -static nxt_int_t nxt_controller_conf_send(nxt_task_t *task, nxt_mp_t *mp, - nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data); - -static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data); -static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data); -static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c, +static nxt_int_t +nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, uintptr_t data); -static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj, - void *data); -static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, - void *data); -static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj, - void *data); -static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data); -static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj, - void *data); -static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, + +static void +nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req); +static void +nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path); +static nxt_bool_t +nxt_controller_check_postpone_request(nxt_task_t *task); +static void +nxt_controller_process_status(nxt_task_t *task, nxt_controller_request_t *req); +static void +nxt_controller_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data); -static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data); - -static nxt_int_t nxt_controller_request_content_length(void *ctx, - nxt_http_field_t *field, uintptr_t data); - -static void nxt_controller_process_request(nxt_task_t *task, - nxt_controller_request_t *req); -static void nxt_controller_process_config(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path); -static nxt_bool_t nxt_controller_check_postpone_request(nxt_task_t *task); -static void nxt_controller_process_status(nxt_task_t *task, - nxt_controller_request_t *req); -static void nxt_controller_status_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_controller_status_response(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path); +static void +nxt_controller_status_response(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path); #if (NXT_TLS) -static void nxt_controller_process_cert(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path); -static void nxt_controller_process_cert_save(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_bool_t nxt_controller_cert_in_use(nxt_str_t *name); -static void nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, +static void +nxt_controller_process_cert(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path); +static void +nxt_controller_process_cert_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); +static nxt_bool_t +nxt_controller_cert_in_use(nxt_str_t *name); +static void +nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data); #endif #if (NXT_HAVE_NJS) -static void nxt_controller_process_script(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path); -static void nxt_controller_process_script_save(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_bool_t nxt_controller_script_in_use(nxt_str_t *name); -static void nxt_controller_script_cleanup(nxt_task_t *task, void *obj, +static void +nxt_controller_process_script(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path); +static void +nxt_controller_process_script_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); +static nxt_bool_t +nxt_controller_script_in_use(nxt_str_t *name); +static void +nxt_controller_script_cleanup(nxt_task_t *task, void *obj, void *data); #endif -static void nxt_controller_process_control(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path); -static void nxt_controller_app_restart_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_controller_conf_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_controller_conf_store(nxt_task_t *task, - nxt_conf_value_t *conf); -static void nxt_controller_response(nxt_task_t *task, - nxt_controller_request_t *req, nxt_controller_response_t *resp); -static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, const char *format); - - -static nxt_http_field_proc_t nxt_controller_request_fields[] = { - { nxt_string("Content-Length"), - &nxt_controller_request_content_length, 0 }, +static void +nxt_controller_process_control(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path); +static void +nxt_controller_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf); +static void +nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, + nxt_controller_response_t *resp); +static u_char * +nxt_controller_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, + size_t size, const char *format); + + +static nxt_http_field_proc_t nxt_controller_request_fields[] = { + {nxt_string("Content-Length"), &nxt_controller_request_content_length, 0}, }; -static nxt_lvlhsh_t nxt_controller_fields_hash; +static nxt_lvlhsh_t nxt_controller_fields_hash; -static nxt_uint_t nxt_controller_listening; -static nxt_uint_t nxt_controller_router_ready; -static nxt_controller_conf_t nxt_controller_conf; -static nxt_queue_t nxt_controller_waiting_requests; -static nxt_bool_t nxt_controller_waiting_init_conf; -static nxt_conf_value_t *nxt_controller_status; +static nxt_uint_t nxt_controller_listening; +static nxt_uint_t nxt_controller_router_ready; +static nxt_controller_conf_t nxt_controller_conf; +static nxt_queue_t nxt_controller_waiting_requests; +static nxt_bool_t nxt_controller_waiting_init_conf; +static nxt_conf_value_t *nxt_controller_status; -static const nxt_event_conn_state_t nxt_controller_conn_read_state; -static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; -static const nxt_event_conn_state_t nxt_controller_conn_write_state; -static const nxt_event_conn_state_t nxt_controller_conn_close_state; +static const nxt_event_conn_state_t nxt_controller_conn_read_state; +static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; +static const nxt_event_conn_state_t nxt_controller_conn_write_state; +static const nxt_event_conn_state_t nxt_controller_conn_close_state; -static const nxt_port_handlers_t nxt_controller_process_port_handlers = { - .quit = nxt_signal_quit_handler, - .new_port = nxt_controller_process_new_port_handler, - .change_file = nxt_port_change_log_file_handler, - .mmap = nxt_port_mmap_handler, - .process_ready = nxt_controller_router_ready_handler, - .data = nxt_port_data_handler, - .remove_pid = nxt_controller_remove_pid_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, +static const nxt_port_handlers_t nxt_controller_process_port_handlers = { + .quit = nxt_signal_quit_handler, + .new_port = nxt_controller_process_new_port_handler, + .change_file = nxt_port_change_log_file_handler, + .mmap = nxt_port_mmap_handler, + .process_ready = nxt_controller_router_ready_handler, + .data = nxt_port_data_handler, + .remove_pid = nxt_controller_remove_pid_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, }; -const nxt_process_init_t nxt_controller_process = { - .name = "controller", - .type = NXT_PROCESS_CONTROLLER, - .prefork = nxt_controller_prefork, - .restart = 1, - .setup = nxt_process_core_setup, - .start = nxt_controller_start, - .port_handlers = &nxt_controller_process_port_handlers, - .signals = nxt_process_signals, +const nxt_process_init_t nxt_controller_process = { + .name = "controller", + .type = NXT_PROCESS_CONTROLLER, + .prefork = nxt_controller_prefork, + .restart = 1, + .setup = nxt_process_core_setup, + .start = nxt_controller_start, + .port_handlers = &nxt_controller_process_port_handlers, + .signals = nxt_process_signals, }; - static nxt_int_t -nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) -{ - nxt_str_t ver; - nxt_int_t ret, num; - nxt_runtime_t *rt; - nxt_controller_init_t ctrl_init; +nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) { + nxt_str_t ver; + nxt_int_t ret, num; + nxt_runtime_t *rt; + nxt_controller_init_t ctrl_init; nxt_log(task, NXT_LOG_INFO, "controller started"); @@ -188,7 +208,7 @@ nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) /* * Since configuration version has only been introduced in 1.26, * set the default version to 1.25. - */ + */ nxt_conf_ver = 12500; ret = nxt_controller_file_read(task, rt->conf, &ctrl_init.conf, mp); @@ -206,8 +226,10 @@ nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) num = nxt_int_parse(ver.start, ver.length); if (nxt_slow_path(num < 0)) { - nxt_alert(task, "failed to restore previous configuration: " - "invalid version string \"%V\"", &ver); + nxt_alert(task, + "failed to restore previous configuration: " + "invalid version string \"%V\"", + &ver); nxt_str_null(&ctrl_init.conf); @@ -227,7 +249,7 @@ nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) ctrl_init.scripts = nxt_script_store_load(task, mp); nxt_mp_cleanup(mp, nxt_controller_script_cleanup, task, ctrl_init.scripts, - rt); + rt); #endif process->data.controller = ctrl_init; @@ -235,15 +257,13 @@ nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) return NXT_OK; } - static nxt_int_t nxt_controller_file_read(nxt_task_t *task, const char *name, nxt_str_t *str, - nxt_mp_t *mp) -{ - ssize_t n; - nxt_int_t ret; - nxt_file_t file; - nxt_file_info_t fi; + nxt_mp_t *mp) { + ssize_t n; + nxt_int_t ret; + nxt_file_t file; + nxt_file_info_t fi; nxt_memzero(&file, sizeof(nxt_file_t)); @@ -259,7 +279,7 @@ nxt_controller_file_read(nxt_task_t *task, const char *name, nxt_str_t *str, if (nxt_fast_path(nxt_is_file(&fi))) { str->length = nxt_file_size(&fi); - str->start = nxt_mp_nget(mp, str->length); + str->start = nxt_mp_nget(mp, str->length); if (nxt_slow_path(str->start == NULL)) { goto fail; } @@ -290,14 +310,13 @@ nxt_controller_file_read(nxt_task_t *task, const char *name, nxt_str_t *str, #if (NXT_TLS) static void -nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data) -{ +nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data) { pid_t main_pid; - nxt_array_t *certs; - nxt_runtime_t *rt; + nxt_array_t *certs; + nxt_runtime_t *rt; certs = obj; - rt = data; + rt = data; main_pid = rt->port_by_type[NXT_PROCESS_MAIN]->pid; @@ -310,18 +329,17 @@ nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data) static nxt_int_t -nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data) -{ - nxt_mp_t *mp; +nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data) { + nxt_mp_t *mp; nxt_int_t ret; - nxt_str_t *json; - nxt_conf_value_t *conf; + nxt_str_t *json; + nxt_conf_value_t *conf; nxt_conf_validation_t vldt; - nxt_controller_init_t *init; + nxt_controller_init_t *init; ret = nxt_http_fields_hash(&nxt_controller_fields_hash, - nxt_controller_request_fields, - nxt_nitems(nxt_controller_request_fields)); + nxt_controller_request_fields, + nxt_nitems(nxt_controller_request_fields)); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -359,7 +377,7 @@ nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data) conf = nxt_conf_json_parse_str(mp, json); if (nxt_slow_path(conf == NULL)) { nxt_alert(task, "failed to restore previous configuration: " - "file is corrupted or not enough memory"); + "file is corrupted or not enough memory"); nxt_mp_destroy(mp); return NXT_OK; @@ -373,17 +391,16 @@ nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - vldt.conf = conf; + vldt.conf = conf; vldt.conf_pool = mp; - vldt.ver = nxt_conf_ver; + vldt.ver = nxt_conf_ver; ret = nxt_conf_validate(&vldt); if (nxt_slow_path(ret != NXT_OK)) { - if (ret == NXT_DECLINED) { nxt_alert(task, "the previous configuration is invalid: %V", - &vldt.error); + &vldt.error); nxt_mp_destroy(vldt.pool); nxt_mp_destroy(mp); @@ -404,35 +421,30 @@ nxt_controller_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_OK; } - static void nxt_controller_process_new_port_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg) -{ + nxt_port_recv_msg_t *msg) { nxt_port_new_port_handler(task, msg); if (msg->u.new_port->type != NXT_PROCESS_ROUTER - || !nxt_controller_router_ready) - { + || !nxt_controller_router_ready) { return; } nxt_controller_send_current_conf(task); } - static void -nxt_controller_send_current_conf(nxt_task_t *task) -{ +nxt_controller_send_current_conf(nxt_task_t *task) { nxt_int_t rc; - nxt_runtime_t *rt; - nxt_conf_value_t *conf; + nxt_runtime_t *rt; + nxt_conf_value_t *conf; conf = nxt_controller_conf.root; if (conf != NULL) { rc = nxt_controller_conf_send(task, nxt_controller_conf.pool, conf, - nxt_controller_conf_init_handler, NULL); + nxt_controller_conf_init_handler, NULL); if (nxt_fast_path(rc == NXT_OK)) { nxt_controller_waiting_init_conf = 1; @@ -462,13 +474,11 @@ nxt_controller_send_current_conf(nxt_task_t *task) nxt_controller_flush_requests(task); } - static void nxt_controller_router_ready_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg) -{ - nxt_port_t *router_port; - nxt_runtime_t *rt; + nxt_port_recv_msg_t *msg) { + nxt_port_t *router_port; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -481,13 +491,11 @@ nxt_controller_router_ready_handler(nxt_task_t *task, } } - static void -nxt_controller_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_controller_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_pid_t pid; - nxt_process_t *process; - nxt_runtime_t *rt; + nxt_process_t *process; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -503,16 +511,13 @@ nxt_controller_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_remove_pid_handler(task, msg); } - static nxt_int_t -nxt_controller_conf_default(void) -{ - nxt_mp_t *mp; - nxt_conf_value_t *conf; +nxt_controller_conf_default(void) { + nxt_mp_t *mp; + nxt_conf_value_t *conf; static const nxt_str_t json = nxt_string( - "{ \"listeners\": {}, \"routes\": [], \"applications\": {} }" - ); + "{ \"listeners\": {}, \"routes\": [], \"applications\": {} }"); mp = nxt_mp_create(1024, 128, 256, 32); @@ -532,12 +537,10 @@ nxt_controller_conf_default(void) return NXT_OK; } - static void nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_runtime_t *rt; + void *data) { + nxt_runtime_t *rt; nxt_controller_waiting_init_conf = 0; @@ -554,9 +557,8 @@ nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, if (nxt_controller_listening == 0) { rt = task->thread->runtime; - if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) - == NULL)) - { + if (nxt_slow_path( + nxt_listen_event(task, rt->controller_socket) == NULL)) { nxt_abort(); } @@ -566,12 +568,10 @@ nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_controller_flush_requests(task); } - static void -nxt_controller_flush_requests(nxt_task_t *task) -{ +nxt_controller_flush_requests(nxt_task_t *task) { nxt_queue_t queue; - nxt_controller_request_t *req; + nxt_controller_request_t *req; nxt_queue_init(&queue); nxt_queue_add(&queue, &nxt_controller_waiting_requests); @@ -580,23 +580,22 @@ nxt_controller_flush_requests(nxt_task_t *task) nxt_queue_each(req, &queue, nxt_controller_request_t, link) { nxt_controller_process_request(task, req); - } nxt_queue_loop; + } + nxt_queue_loop; } - static nxt_int_t nxt_controller_conf_send(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf, - nxt_port_rpc_handler_t handler, void *data) -{ - void *mem; - u_char *end; + nxt_port_rpc_handler_t handler, void *data) { + void *mem; + u_char *end; size_t size; uint32_t stream; nxt_fd_t fd; nxt_int_t rc; - nxt_buf_t *b; - nxt_port_t *router_port, *controller_port; - nxt_runtime_t *rt; + nxt_buf_t *b; + nxt_port_t *router_port, *controller_port; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -632,16 +631,15 @@ nxt_controller_conf_send(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf, b->mem.free = nxt_cpymem(b->mem.pos, &size, sizeof(size_t)); - stream = nxt_port_rpc_register_handler(task, controller_port, - handler, handler, - router_port->pid, data); + stream = nxt_port_rpc_register_handler(task, controller_port, handler, + handler, router_port->pid, data); if (nxt_slow_path(stream == 0)) { goto fail; } rc = nxt_port_socket_write(task, router_port, - NXT_PORT_MSG_DATA_LAST | NXT_PORT_MSG_CLOSE_FD, - fd, stream, controller_port->id, b); + NXT_PORT_MSG_DATA_LAST | NXT_PORT_MSG_CLOSE_FD, fd, stream, + controller_port->id, b); if (nxt_slow_path(rc != NXT_OK)) { nxt_port_rpc_cancel(task, controller_port, stream); @@ -658,11 +656,9 @@ nxt_controller_conf_send(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf, return NXT_ERROR; } - nxt_int_t -nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) -{ - nxt_listen_socket_t *ls; +nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) { + nxt_listen_socket_t *ls; ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t)); if (ls == NULL) { @@ -673,10 +669,10 @@ nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) nxt_listen_socket_remote_size(ls); - ls->socket = -1; - ls->backlog = NXT_LISTEN_BACKLOG; + ls->socket = -1; + ls->backlog = NXT_LISTEN_BACKLOG; ls->read_after_accept = 1; - ls->flags = NXT_NONBLOCK; + ls->flags = NXT_NONBLOCK; #if 0 /* STUB */ @@ -708,14 +704,12 @@ nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static void -nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_conn_t *c; - nxt_event_engine_t *engine; - nxt_controller_request_t *r; +nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_conn_t *c; + nxt_event_engine_t *engine; + nxt_controller_request_t *r; c = obj; @@ -729,9 +723,8 @@ nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) r->conn = c; - if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_http_parse_request_init(&r->parser, c->mem_pool) != NXT_OK)) { nxt_controller_conn_free(task, c, NULL); return; } @@ -744,40 +737,37 @@ nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) return; } - c->read = b; - c->socket.data = r; + c->read = b; + c->socket.data = r; c->socket.read_ready = 1; - c->read_state = &nxt_controller_conn_read_state; + c->read_state = &nxt_controller_conn_read_state; - engine = task->thread->engine; - c->read_work_queue = &engine->read_work_queue; + engine = task->thread->engine; + c->read_work_queue = &engine->read_work_queue; c->write_work_queue = &engine->write_work_queue; nxt_conn_read(engine, c); } +static const nxt_event_conn_state_t nxt_controller_conn_read_state nxt_aligned( + 64) + = { + .ready_handler = nxt_controller_conn_read, + .close_handler = nxt_controller_conn_close, + .error_handler = nxt_controller_conn_read_error, -static const nxt_event_conn_state_t nxt_controller_conn_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_controller_conn_read, - .close_handler = nxt_controller_conn_close, - .error_handler = nxt_controller_conn_read_error, - - .timer_handler = nxt_controller_conn_read_timeout, - .timer_value = nxt_controller_conn_timeout_value, - .timer_data = 300 * 1000, + .timer_handler = nxt_controller_conn_read_timeout, + .timer_value = nxt_controller_conn_timeout_value, + .timer_data = 300 * 1000, }; - static void -nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) -{ +nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) { size_t preread; - nxt_buf_t *b; + nxt_buf_t *b; nxt_int_t rc; - nxt_conn_t *c; - nxt_controller_request_t *r; + nxt_conn_t *c; + nxt_controller_request_t *r; c = obj; r = data; @@ -792,7 +782,6 @@ nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) rc = nxt_http_parse_request(&r->parser, &b->mem); if (nxt_slow_path(rc != NXT_DONE)) { - if (rc == NXT_AGAIN) { if (nxt_buf_mem_free_size(&b->mem) == 0) { nxt_log(task, NXT_LOG_ERR, "too long request headers"); @@ -813,7 +802,7 @@ nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) } rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash, - r); + r); if (nxt_slow_path(rc != NXT_OK)) { nxt_controller_conn_close(task, c, r); @@ -822,9 +811,10 @@ nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) preread = nxt_buf_mem_used_size(&b->mem); - nxt_debug(task, "controller request header parsing complete, " - "body length: %uz, preread: %uz", - r->length, preread); + nxt_debug(task, + "controller request header parsing complete, " + "body length: %uz, preread: %uz", + r->length, preread); if (preread >= r->length) { nxt_controller_process_request(task, r); @@ -848,18 +838,14 @@ nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) nxt_conn_read(task->thread->engine, c); } - static nxt_msec_t -nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data) -{ +nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data) { return (nxt_msec_t) data; } - static void -nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -868,46 +854,41 @@ nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data) nxt_controller_conn_close(task, c, data); } - static void -nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_conn_t *c; +nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_conn_t *c; timer = obj; - c = nxt_read_timer_conn(timer); + c = nxt_read_timer_conn(timer); c->socket.timedout = 1; - c->socket.closed = 1; + c->socket.closed = 1; nxt_debug(task, "controller conn read timeout"); nxt_controller_conn_close(task, c, data); } - -static const nxt_event_conn_state_t nxt_controller_conn_body_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_controller_conn_body_read, - .close_handler = nxt_controller_conn_close, - .error_handler = nxt_controller_conn_read_error, - - .timer_handler = nxt_controller_conn_read_timeout, - .timer_value = nxt_controller_conn_timeout_value, - .timer_data = 60 * 1000, - .timer_autoreset = 1, +static const nxt_event_conn_state_t + nxt_controller_conn_body_read_state nxt_aligned(64) + = { + .ready_handler = nxt_controller_conn_body_read, + .close_handler = nxt_controller_conn_close, + .error_handler = nxt_controller_conn_read_error, + + .timer_handler = nxt_controller_conn_read_timeout, + .timer_value = nxt_controller_conn_timeout_value, + .timer_data = 60 * 1000, + .timer_autoreset = 1, }; - static void -nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) -{ +nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) { size_t read; - nxt_buf_t *b; - nxt_conn_t *c; - nxt_controller_request_t *r; + nxt_buf_t *b; + nxt_conn_t *c; + nxt_controller_request_t *r; c = obj; r = data; @@ -915,8 +896,7 @@ nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) read = nxt_buf_mem_used_size(&b->mem); - nxt_debug(task, "controller conn body read: %uz of %uz", - read, r->length); + nxt_debug(task, "controller conn body read: %uz of %uz", read, r->length); if (read >= r->length) { nxt_controller_process_request(task, r); @@ -926,25 +906,22 @@ nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) nxt_conn_read(task->thread->engine, c); } +static const nxt_event_conn_state_t nxt_controller_conn_write_state nxt_aligned( + 64) + = { + .ready_handler = nxt_controller_conn_write, + .error_handler = nxt_controller_conn_write_error, -static const nxt_event_conn_state_t nxt_controller_conn_write_state - nxt_aligned(64) = -{ - .ready_handler = nxt_controller_conn_write, - .error_handler = nxt_controller_conn_write_error, - - .timer_handler = nxt_controller_conn_write_timeout, - .timer_value = nxt_controller_conn_timeout_value, - .timer_data = 60 * 1000, - .timer_autoreset = 1, + .timer_handler = nxt_controller_conn_write_timeout, + .timer_value = nxt_controller_conn_timeout_value, + .timer_data = 60 * 1000, + .timer_autoreset = 1, }; - static void -nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b; - nxt_conn_t *c; +nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b; + nxt_conn_t *c; c = obj; @@ -962,11 +939,9 @@ nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data) nxt_controller_conn_close(task, c, data); } - static void -nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -975,36 +950,31 @@ nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data) nxt_controller_conn_close(task, c, data); } - static void -nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; - c = nxt_write_timer_conn(timer); + c = nxt_write_timer_conn(timer); c->socket.timedout = 1; - c->socket.closed = 1; + c->socket.closed = 1; nxt_debug(task, "controller conn write timeout"); nxt_controller_conn_close(task, c, data); } - -static const nxt_event_conn_state_t nxt_controller_conn_close_state - nxt_aligned(64) = -{ - .ready_handler = nxt_controller_conn_free, +static const nxt_event_conn_state_t nxt_controller_conn_close_state nxt_aligned( + 64) + = { + .ready_handler = nxt_controller_conn_free, }; - static void -nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1017,11 +987,9 @@ nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data) nxt_conn_close(task->thread->engine, c); } - static void -nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1032,23 +1000,20 @@ nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) nxt_conn_free(task, c); } - static nxt_int_t nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, - uintptr_t data) -{ + uintptr_t data) { off_t length; - nxt_controller_request_t *r; + nxt_controller_request_t *r; r = ctx; length = nxt_off_t_parse(field->value, field->value_length); if (nxt_fast_path(length >= 0)) { - if (nxt_slow_path(length > NXT_SIZE_T_MAX)) { nxt_log_error(NXT_LOG_ERR, &r->conn->log, - "Content-Length is too big"); + "Content-Length is too big"); return NXT_ERROR; } @@ -1061,20 +1026,19 @@ nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, return NXT_ERROR; } - static void -nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) -{ - uint32_t i, count; - nxt_str_t path; - nxt_conn_t *c; - nxt_conf_value_t *value; - nxt_controller_response_t resp; +nxt_controller_process_request(nxt_task_t *task, + nxt_controller_request_t *req) { + uint32_t i, count; + nxt_str_t path; + nxt_conn_t *c; + nxt_conf_value_t *value; + nxt_controller_response_t resp; #if (NXT_TLS) - nxt_conf_value_t *certs; + nxt_conf_value_t *certs; #endif #if (NXT_HAVE_NJS) - nxt_conf_value_t *scripts; + nxt_conf_value_t *scripts; #endif #if (NXT_TLS) @@ -1088,7 +1052,7 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) static nxt_str_t config = nxt_string("config"); static nxt_str_t status = nxt_string("status"); - c = req->conn; + c = req->conn; path = req->parser.path; if (path.length > 1 && path.start[path.length - 1] == '/') { @@ -1096,14 +1060,13 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) } if (nxt_str_start(&path, "/config", 7) - && (path.length == 7 || path.start[7] == '/')) - { + && (path.length == 7 || path.start[7] == '/')) { if (path.length == 7) { path.length = 1; } else { path.length -= 7; - path.start += 7; + path.start += 7; } nxt_controller_process_config(task, req, &path); @@ -1113,8 +1076,7 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) nxt_memzero(&resp, sizeof(nxt_controller_response_t)); if (nxt_str_start(&path, "/status", 7) - && (path.length == 7 || path.start[7] == '/')) - { + && (path.length == 7 || path.start[7] == '/')) { if (!nxt_str_eq(&req->parser.method, "GET", 3)) { goto invalid_method; } @@ -1129,7 +1091,7 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) } else { path.length -= 7; - path.start += 7; + path.start += 7; } nxt_controller_status_response(task, req, &path); @@ -1139,14 +1101,13 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) #if (NXT_TLS) if (nxt_str_start(&path, "/certificates", 13) - && (path.length == 13 || path.start[13] == '/')) - { + && (path.length == 13 || path.start[13] == '/')) { if (path.length == 13) { path.length = 1; } else { path.length -= 13; - path.start += 13; + path.start += 13; } nxt_controller_process_cert(task, req, &path); @@ -1158,14 +1119,13 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) #if (NXT_HAVE_NJS) if (nxt_str_start(&path, "/js_modules", 11) - && (path.length == 11 || path.start[11] == '/')) - { + && (path.length == 11 || path.start[11] == '/')) { if (path.length == 11) { path.length = 1; } else { path.length -= 11; - path.start += 11; + path.start += 11; } nxt_controller_process_script(task, req, &path); @@ -1176,14 +1136,13 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) if (nxt_str_start(&path, "/control/", 9)) { path.length -= 9; - path.start += 9; + path.start += 9; nxt_controller_process_control(task, req, &path); return; } if (path.length == 1 && path.start[0] == '/') { - if (!nxt_str_eq(&req->parser.method, "GET", 3)) { goto invalid_method; } @@ -1230,14 +1189,14 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) nxt_conf_set_member(value, &status, nxt_controller_status, i); resp.status = 200; - resp.conf = value; + resp.conf = value; nxt_controller_response(task, req, &resp); return; } resp.status = 404; - resp.title = (u_char *) "Value doesn't exist."; + resp.title = (u_char *) "Value doesn't exist."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1246,7 +1205,7 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) invalid_method: resp.status = 405; - resp.title = (u_char *) "Invalid method."; + resp.title = (u_char *) "Invalid method."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1255,28 +1214,26 @@ nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) alloc_fail: resp.status = 500; - resp.title = (u_char *) "Memory allocation failed."; + resp.title = (u_char *) "Memory allocation failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); return; } - static void nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, - nxt_str_t *path) -{ - nxt_mp_t *mp; - nxt_int_t rc; - nxt_conn_t *c; - nxt_bool_t post; - nxt_buf_mem_t *mbuf; - nxt_conf_op_t *ops; - nxt_conf_value_t *value; - nxt_conf_validation_t vldt; - nxt_conf_json_error_t error; - nxt_controller_response_t resp; + nxt_str_t *path) { + nxt_mp_t *mp; + nxt_int_t rc; + nxt_conn_t *c; + nxt_bool_t post; + nxt_buf_mem_t *mbuf; + nxt_conf_op_t *ops; + nxt_conf_value_t *value; + nxt_conf_validation_t vldt; + nxt_conf_json_error_t error; + nxt_controller_response_t resp; static const nxt_str_t empty_obj = nxt_string("{}"); @@ -1285,7 +1242,6 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, c = req->conn; if (nxt_str_eq(&req->parser.method, "GET", 3)) { - value = nxt_conf_get_path(nxt_controller_conf.root, path); if (value == NULL) { @@ -1293,7 +1249,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } resp.status = 200; - resp.conf = value; + resp.conf = value; nxt_controller_response(task, req, &resp); return; @@ -1311,7 +1267,6 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } if (post || nxt_str_eq(&req->parser.method, "PUT", 3)) { - if (nxt_controller_check_postpone_request(task)) { nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); return; @@ -1329,8 +1284,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, /* Skip UTF-8 BOM. */ if (nxt_buf_mem_used_size(mbuf) >= 3 - && memcmp(mbuf->pos, "\xEF\xBB\xBF", 3) == 0) - { + && memcmp(mbuf->pos, "\xEF\xBB\xBF", 3) == 0) { mbuf->pos += 3; } @@ -1343,14 +1297,14 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, goto alloc_fail; } - resp.status = 400; - resp.title = (u_char *) "Invalid JSON."; + resp.status = 400; + resp.title = (u_char *) "Invalid JSON."; resp.detail.length = nxt_strlen(error.detail); - resp.detail.start = error.detail; - resp.offset = error.pos - mbuf->pos; + resp.detail.start = error.detail; + resp.offset = error.pos - mbuf->pos; - nxt_conf_json_position(mbuf->pos, error.pos, - &resp.line, &resp.column); + nxt_conf_json_position(mbuf->pos, error.pos, &resp.line, + &resp.column); nxt_controller_response(task, req, &resp); return; @@ -1358,8 +1312,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, if (path->length != 1) { rc = nxt_conf_op_compile(c->mem_pool, &ops, - nxt_controller_conf.root, - path, value, post); + nxt_controller_conf.root, path, value, post); if (rc != NXT_CONF_OP_OK) { nxt_mp_destroy(mp); @@ -1386,10 +1339,10 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, nxt_memzero(&vldt, sizeof(nxt_conf_validation_t)); - vldt.conf = value; - vldt.pool = c->mem_pool; + vldt.conf = value; + vldt.pool = c->mem_pool; vldt.conf_pool = mp; - vldt.ver = NXT_VERNUM; + vldt.ver = NXT_VERNUM; rc = nxt_conf_validate(&vldt); @@ -1406,7 +1359,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } rc = nxt_controller_conf_send(task, mp, value, - nxt_controller_conf_handler, req); + nxt_controller_conf_handler, req); if (nxt_slow_path(rc != NXT_OK)) { nxt_mp_destroy(mp); @@ -1424,7 +1377,6 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { - if (nxt_controller_check_postpone_request(task)) { nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); return; @@ -1441,8 +1393,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } else { rc = nxt_conf_op_compile(c->mem_pool, &ops, - nxt_controller_conf.root, - path, NULL, 0); + nxt_controller_conf.root, path, NULL, 0); if (rc != NXT_OK) { if (rc == NXT_CONF_OP_NOT_FOUND) { @@ -1469,10 +1420,10 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, nxt_memzero(&vldt, sizeof(nxt_conf_validation_t)); - vldt.conf = value; - vldt.pool = c->mem_pool; + vldt.conf = value; + vldt.pool = c->mem_pool; vldt.conf_pool = mp; - vldt.ver = NXT_VERNUM; + vldt.ver = NXT_VERNUM; rc = nxt_conf_validate(&vldt); @@ -1489,7 +1440,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, } rc = nxt_controller_conf_send(task, mp, value, - nxt_controller_conf_handler, req); + nxt_controller_conf_handler, req); if (nxt_slow_path(rc != NXT_OK)) { nxt_mp_destroy(mp); @@ -1509,7 +1460,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, not_allowed: resp.status = 405; - resp.title = (u_char *) "Method isn't allowed."; + resp.title = (u_char *) "Method isn't allowed."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1518,7 +1469,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, not_found: resp.status = 404; - resp.title = (u_char *) "Value doesn't exist."; + resp.title = (u_char *) "Value doesn't exist."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1527,7 +1478,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, invalid_conf: resp.status = 400; - resp.title = (u_char *) "Invalid configuration."; + resp.title = (u_char *) "Invalid configuration."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1536,23 +1487,19 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req, alloc_fail: resp.status = 500; - resp.title = (u_char *) "Memory allocation failed."; + resp.title = (u_char *) "Memory allocation failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); } - static nxt_bool_t -nxt_controller_check_postpone_request(nxt_task_t *task) -{ - nxt_port_t *router_port; - nxt_runtime_t *rt; +nxt_controller_check_postpone_request(nxt_task_t *task) { + nxt_port_t *router_port; + nxt_runtime_t *rt; if (!nxt_queue_is_empty(&nxt_controller_waiting_requests) - || nxt_controller_waiting_init_conf - || !nxt_controller_router_ready) - { + || nxt_controller_waiting_init_conf || !nxt_controller_router_ready) { return 1; } @@ -1563,15 +1510,13 @@ nxt_controller_check_postpone_request(nxt_task_t *task) return (router_port == NULL); } - static void -nxt_controller_process_status(nxt_task_t *task, nxt_controller_request_t *req) -{ - uint32_t stream; - nxt_int_t rc; - nxt_port_t *router_port, *controller_port; - nxt_runtime_t *rt; - nxt_controller_response_t resp; +nxt_controller_process_status(nxt_task_t *task, nxt_controller_request_t *req) { + uint32_t stream; + nxt_int_t rc; + nxt_port_t *router_port, *controller_port; + nxt_runtime_t *rt; + nxt_controller_response_t resp; if (nxt_controller_check_postpone_request(task)) { nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); @@ -1588,15 +1533,14 @@ nxt_controller_process_status(nxt_task_t *task, nxt_controller_request_t *req) controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; stream = nxt_port_rpc_register_handler(task, controller_port, - nxt_controller_status_handler, - nxt_controller_status_handler, - router_port->pid, req); + nxt_controller_status_handler, nxt_controller_status_handler, + router_port->pid, req); if (nxt_slow_path(stream == 0)) { goto fail; } - rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_STATUS, - -1, stream, controller_port->id, NULL); + rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_STATUS, -1, + stream, controller_port->id, NULL); if (nxt_slow_path(rc != NXT_OK)) { nxt_port_rpc_cancel(task, controller_port, stream); @@ -1612,21 +1556,19 @@ nxt_controller_process_status(nxt_task_t *task, nxt_controller_request_t *req) nxt_memzero(&resp, sizeof(nxt_controller_response_t)); resp.status = 500; - resp.title = (u_char *) "Failed to get status."; + resp.title = (u_char *) "Failed to get status."; resp.offset = -1; nxt_controller_response(task, req, &resp); return; } - static void nxt_controller_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_conf_value_t *status; - nxt_controller_request_t *req; - nxt_controller_response_t resp; + void *data) { + nxt_conf_value_t *status; + nxt_controller_request_t *req; + nxt_controller_response_t resp; nxt_debug(task, "controller status handler"); @@ -1634,7 +1576,7 @@ nxt_controller_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) { status = nxt_status_get((nxt_status_report_t *) msg->buf->mem.pos, - req->conn->mem_pool); + req->conn->mem_pool); } else { status = NULL; } @@ -1645,7 +1587,7 @@ nxt_controller_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_memzero(&resp, sizeof(nxt_controller_response_t)); resp.status = 500; - resp.title = (u_char *) "Failed to get status."; + resp.title = (u_char *) "Failed to get status."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1658,13 +1600,11 @@ nxt_controller_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_controller_status = NULL; } - static void nxt_controller_status_response(nxt_task_t *task, nxt_controller_request_t *req, - nxt_str_t *path) -{ - nxt_conf_value_t *status; - nxt_controller_response_t resp; + nxt_str_t *path) { + nxt_conf_value_t *status; + nxt_controller_response_t resp; status = nxt_conf_get_path(nxt_controller_status, path); @@ -1672,7 +1612,7 @@ nxt_controller_status_response(nxt_task_t *task, nxt_controller_request_t *req, if (status == NULL) { resp.status = 404; - resp.title = (u_char *) "Invalid path."; + resp.title = (u_char *) "Invalid path."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1680,7 +1620,7 @@ nxt_controller_status_response(nxt_task_t *task, nxt_controller_request_t *req, } resp.status = 200; - resp.conf = status; + resp.conf = status; nxt_controller_response(task, req, &resp); } @@ -1689,19 +1629,18 @@ nxt_controller_status_response(nxt_task_t *task, nxt_controller_request_t *req, #if (NXT_TLS) static void -nxt_controller_process_cert(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path) -{ - u_char *p; - nxt_str_t name; - nxt_int_t ret; - nxt_conn_t *c; - nxt_cert_t *cert; - nxt_conf_value_t *value; - nxt_controller_response_t resp; +nxt_controller_process_cert(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path) { + u_char *p; + nxt_str_t name; + nxt_int_t ret; + nxt_conn_t *c; + nxt_cert_t *cert; + nxt_conf_value_t *value; + nxt_controller_response_t resp; name.length = path->length - 1; - name.start = path->start + 1; + name.start = path->start + 1; p = memchr(name.start, '/', name.length); @@ -1709,7 +1648,7 @@ nxt_controller_process_cert(nxt_task_t *task, name.length = p - name.start; path->length -= p - path->start; - path->start = p; + path->start = p; } else { path = NULL; @@ -1720,7 +1659,6 @@ nxt_controller_process_cert(nxt_task_t *task, c = req->conn; if (nxt_str_eq(&req->parser.method, "GET", 3)) { - if (name.length != 0) { value = nxt_cert_info_get(&name); if (value == NULL) { @@ -1742,7 +1680,7 @@ nxt_controller_process_cert(nxt_task_t *task, } resp.status = 200; - resp.conf = value; + resp.conf = value; nxt_controller_response(task, req, &resp); return; @@ -1772,12 +1710,11 @@ nxt_controller_process_cert(nxt_task_t *task, } nxt_cert_store_get(task, &name, c->mem_pool, - nxt_controller_process_cert_save, req); + nxt_controller_process_cert_save, req); return; } if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { - if (nxt_controller_cert_in_use(&name)) { goto cert_in_use; } @@ -1789,14 +1726,14 @@ nxt_controller_process_cert(nxt_task_t *task, nxt_cert_store_delete(task, &name, c->mem_pool); resp.status = 200; - resp.title = (u_char *) "Certificate deleted."; + resp.title = (u_char *) "Certificate deleted."; nxt_controller_response(task, req, &resp); return; } resp.status = 405; - resp.title = (u_char *) "Invalid method."; + resp.title = (u_char *) "Invalid method."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1805,7 +1742,7 @@ nxt_controller_process_cert(nxt_task_t *task, invalid_name: resp.status = 400; - resp.title = (u_char *) "Invalid certificate name."; + resp.title = (u_char *) "Invalid certificate name."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1814,7 +1751,7 @@ nxt_controller_process_cert(nxt_task_t *task, invalid_cert: resp.status = 400; - resp.title = (u_char *) "Invalid certificate."; + resp.title = (u_char *) "Invalid certificate."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1823,7 +1760,7 @@ nxt_controller_process_cert(nxt_task_t *task, exists_cert: resp.status = 400; - resp.title = (u_char *) "Certificate already exists."; + resp.title = (u_char *) "Certificate already exists."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1832,7 +1769,7 @@ nxt_controller_process_cert(nxt_task_t *task, cert_in_use: resp.status = 400; - resp.title = (u_char *) "Certificate is used in the configuration."; + resp.title = (u_char *) "Certificate is used in the configuration."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1841,7 +1778,7 @@ nxt_controller_process_cert(nxt_task_t *task, cert_not_found: resp.status = 404; - resp.title = (u_char *) "Certificate doesn't exist."; + resp.title = (u_char *) "Certificate doesn't exist."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1850,7 +1787,7 @@ nxt_controller_process_cert(nxt_task_t *task, not_found: resp.status = 404; - resp.title = (u_char *) "Invalid path."; + resp.title = (u_char *) "Invalid path."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -1859,22 +1796,20 @@ nxt_controller_process_cert(nxt_task_t *task, alloc_fail: resp.status = 500; - resp.title = (u_char *) "Memory allocation failed."; + resp.title = (u_char *) "Memory allocation failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); return; } - static void nxt_controller_process_cert_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_conn_t *c; - nxt_buf_mem_t *mbuf; - nxt_controller_request_t *req; - nxt_controller_response_t resp; + void *data) { + nxt_conn_t *c; + nxt_buf_mem_t *mbuf; + nxt_controller_request_t *req; + nxt_controller_response_t resp; req = data; @@ -1882,7 +1817,7 @@ nxt_controller_process_cert_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, if (msg == NULL || msg->port_msg.type == _NXT_PORT_MSG_RPC_ERROR) { resp.status = 500; - resp.title = (u_char *) "Failed to store certificate."; + resp.title = (u_char *) "Failed to store certificate."; nxt_controller_response(task, req, &resp); return; @@ -1899,28 +1834,26 @@ nxt_controller_process_cert_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_memzero(&resp, sizeof(nxt_controller_response_t)); resp.status = 200; - resp.title = (u_char *) "Certificate chain uploaded."; + resp.title = (u_char *) "Certificate chain uploaded."; nxt_controller_response(task, req, &resp); } - static nxt_bool_t -nxt_controller_cert_in_use(nxt_str_t *name) -{ +nxt_controller_cert_in_use(nxt_str_t *name) { uint32_t i, n, next; nxt_str_t str; - nxt_conf_value_t *listeners, *listener, *value, *element; + nxt_conf_value_t *listeners, *listener, *value, *element; - static const nxt_str_t listeners_path = nxt_string("/listeners"); - static const nxt_str_t certificate_path = nxt_string("/tls/certificate"); + static const nxt_str_t listeners_path = nxt_string("/listeners"); + static const nxt_str_t certificate_path = nxt_string("/tls/certificate"); listeners = nxt_conf_get_path(nxt_controller_conf.root, &listeners_path); if (listeners != NULL) { next = 0; - for ( ;; ) { + for (;;) { listener = nxt_conf_next_object_member(listeners, &str, &next); if (listener == NULL) { break; @@ -1965,21 +1898,20 @@ nxt_controller_cert_in_use(nxt_str_t *name) #if (NXT_HAVE_NJS) static void -nxt_controller_process_script(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path) -{ - u_char *p; - nxt_int_t ret; - nxt_str_t name; - nxt_conn_t *c; - nxt_script_t *script; - nxt_buf_mem_t *bm; - nxt_conf_value_t *value; - nxt_controller_response_t resp; - u_char error[NXT_MAX_ERROR_STR]; +nxt_controller_process_script(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path) { + u_char *p; + nxt_int_t ret; + nxt_str_t name; + nxt_conn_t *c; + nxt_script_t *script; + nxt_buf_mem_t *bm; + nxt_conf_value_t *value; + nxt_controller_response_t resp; + u_char error[NXT_MAX_ERROR_STR]; name.length = path->length - 1; - name.start = path->start + 1; + name.start = path->start + 1; p = memchr(name.start, '/', name.length); @@ -1987,7 +1919,7 @@ nxt_controller_process_script(nxt_task_t *task, name.length = p - name.start; path->length -= p - path->start; - path->start = p; + path->start = p; } else { path = NULL; @@ -1998,7 +1930,6 @@ nxt_controller_process_script(nxt_task_t *task, c = req->conn; if (nxt_str_eq(&req->parser.method, "GET", 3)) { - if (name.length != 0) { value = nxt_script_info_get(&name); if (value == NULL) { @@ -2020,7 +1951,7 @@ nxt_controller_process_script(nxt_task_t *task, } resp.status = 200; - resp.conf = value; + resp.conf = value; nxt_controller_response(task, req, &resp); return; @@ -2038,8 +1969,8 @@ nxt_controller_process_script(nxt_task_t *task, bm = &c->read->mem; - script = nxt_script_new(task, &name, bm->pos, - nxt_buf_mem_used_size(bm), error); + script = nxt_script_new(task, &name, bm->pos, nxt_buf_mem_used_size(bm), + error); if (script == NULL) { goto invalid_script; } @@ -2053,12 +1984,11 @@ nxt_controller_process_script(nxt_task_t *task, } nxt_script_store_get(task, &name, c->mem_pool, - nxt_controller_process_script_save, req); + nxt_controller_process_script_save, req); return; } if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { - if (nxt_controller_script_in_use(&name)) { goto script_in_use; } @@ -2070,14 +2000,14 @@ nxt_controller_process_script(nxt_task_t *task, nxt_script_store_delete(task, &name, c->mem_pool); resp.status = 200; - resp.title = (u_char *) "JS module deleted."; + resp.title = (u_char *) "JS module deleted."; nxt_controller_response(task, req, &resp); return; } resp.status = 405; - resp.title = (u_char *) "Invalid method."; + resp.title = (u_char *) "Invalid method."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2086,7 +2016,7 @@ nxt_controller_process_script(nxt_task_t *task, invalid_name: resp.status = 400; - resp.title = (u_char *) "Invalid JS module name."; + resp.title = (u_char *) "Invalid JS module name."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2095,10 +2025,10 @@ nxt_controller_process_script(nxt_task_t *task, invalid_script: resp.status = 400; - resp.title = (u_char *) "Invalid JS module."; + resp.title = (u_char *) "Invalid JS module."; resp.offset = -1; - resp.detail.start = error; + resp.detail.start = error; resp.detail.length = nxt_strlen(error); nxt_controller_response(task, req, &resp); @@ -2107,7 +2037,7 @@ nxt_controller_process_script(nxt_task_t *task, exists_script: resp.status = 400; - resp.title = (u_char *) "JS module already exists."; + resp.title = (u_char *) "JS module already exists."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2116,7 +2046,7 @@ nxt_controller_process_script(nxt_task_t *task, script_in_use: resp.status = 400; - resp.title = (u_char *) "JS module is used in the configuration."; + resp.title = (u_char *) "JS module is used in the configuration."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2125,7 +2055,7 @@ nxt_controller_process_script(nxt_task_t *task, script_not_found: resp.status = 404; - resp.title = (u_char *) "JS module doesn't exist."; + resp.title = (u_char *) "JS module doesn't exist."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2134,7 +2064,7 @@ nxt_controller_process_script(nxt_task_t *task, not_found: resp.status = 404; - resp.title = (u_char *) "Invalid path."; + resp.title = (u_char *) "Invalid path."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2143,21 +2073,19 @@ nxt_controller_process_script(nxt_task_t *task, alloc_fail: resp.status = 500; - resp.title = (u_char *) "Memory allocation failed."; + resp.title = (u_char *) "Memory allocation failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); } - static void nxt_controller_process_script_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_conn_t *c; - nxt_buf_mem_t *mbuf; - nxt_controller_request_t *req; - nxt_controller_response_t resp; + void *data) { + nxt_conn_t *c; + nxt_buf_mem_t *mbuf; + nxt_controller_request_t *req; + nxt_controller_response_t resp; req = data; @@ -2165,7 +2093,7 @@ nxt_controller_process_script_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, if (msg == NULL || msg->port_msg.type == _NXT_PORT_MSG_RPC_ERROR) { resp.status = 500; - resp.title = (u_char *) "Failed to store script."; + resp.title = (u_char *) "Failed to store script."; nxt_controller_response(task, req, &resp); return; @@ -2182,26 +2110,22 @@ nxt_controller_process_script_save(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_memzero(&resp, sizeof(nxt_controller_response_t)); resp.status = 200; - resp.title = (u_char *) "JS module uploaded."; + resp.title = (u_char *) "JS module uploaded."; nxt_controller_response(task, req, &resp); } - static nxt_bool_t -nxt_controller_script_in_use(nxt_str_t *name) -{ +nxt_controller_script_in_use(nxt_str_t *name) { uint32_t i, n; nxt_str_t str; - nxt_conf_value_t *js_module, *element; + nxt_conf_value_t *js_module, *element; - static const nxt_str_t js_module_path = nxt_string("/settings/js_module"); + static const nxt_str_t js_module_path = nxt_string("/settings/js_module"); - js_module = nxt_conf_get_path(nxt_controller_conf.root, - &js_module_path); + js_module = nxt_conf_get_path(nxt_controller_conf.root, &js_module_path); if (js_module != NULL) { - if (nxt_conf_type(js_module) == NXT_CONF_ARRAY) { n = nxt_conf_array_elements_count(js_module); @@ -2229,16 +2153,14 @@ nxt_controller_script_in_use(nxt_str_t *name) return 0; } - static void -nxt_controller_script_cleanup(nxt_task_t *task, void *obj, void *data) -{ +nxt_controller_script_cleanup(nxt_task_t *task, void *obj, void *data) { pid_t main_pid; - nxt_array_t *scripts; - nxt_runtime_t *rt; + nxt_array_t *scripts; + nxt_runtime_t *rt; scripts = obj; - rt = data; + rt = data; main_pid = rt->port_by_type[NXT_PROCESS_MAIN]->pid; @@ -2252,15 +2174,14 @@ nxt_controller_script_cleanup(nxt_task_t *task, void *obj, void *data) static void nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_controller_request_t *req; - nxt_controller_response_t resp; + void *data) { + nxt_controller_request_t *req; + nxt_controller_response_t resp; req = data; nxt_debug(task, "controller conf ready: %*s", - nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos); + nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos); nxt_queue_remove(&req->link); @@ -2274,13 +2195,13 @@ nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_controller_conf_store(task, req->conf.root); resp.status = 200; - resp.title = (u_char *) "Reconfiguration done."; + resp.title = (u_char *) "Reconfiguration done."; } else { nxt_mp_destroy(req->conf.pool); resp.status = 500; - resp.title = (u_char *) "Failed to apply new configuration."; + resp.title = (u_char *) "Failed to apply new configuration."; resp.offset = -1; } @@ -2289,18 +2210,16 @@ nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_controller_flush_requests(task); } - static void -nxt_controller_process_control(nxt_task_t *task, - nxt_controller_request_t *req, nxt_str_t *path) -{ - uint32_t stream; - nxt_buf_t *b; - nxt_int_t rc; - nxt_port_t *router_port, *controller_port; - nxt_runtime_t *rt; - nxt_conf_value_t *value; - nxt_controller_response_t resp; +nxt_controller_process_control(nxt_task_t *task, nxt_controller_request_t *req, + nxt_str_t *path) { + uint32_t stream; + nxt_buf_t *b; + nxt_int_t rc; + nxt_port_t *router_port, *controller_port; + nxt_runtime_t *rt; + nxt_conf_value_t *value; + nxt_controller_response_t resp; static nxt_str_t applications = nxt_string("applications"); @@ -2311,12 +2230,11 @@ nxt_controller_process_control(nxt_task_t *task, } if (!nxt_str_start(path, "applications/", 13) - || memcmp(path->start + path->length - 8, "/restart", 8) != 0) - { + || memcmp(path->start + path->length - 8, "/restart", 8) != 0) { goto not_found; } - path->start += 13; + path->start += 13; path->length -= 13 + 8; if (nxt_controller_check_postpone_request(task)) { @@ -2349,18 +2267,17 @@ nxt_controller_process_control(nxt_task_t *task, rt = task->thread->runtime; controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; - router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; + router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; stream = nxt_port_rpc_register_handler(task, controller_port, - nxt_controller_app_restart_handler, - nxt_controller_app_restart_handler, - router_port->pid, req); + nxt_controller_app_restart_handler, nxt_controller_app_restart_handler, + router_port->pid, req); if (nxt_slow_path(stream == 0)) { goto alloc_fail; } - rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_APP_RESTART, - -1, stream, 0, b); + rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_APP_RESTART, -1, + stream, 0, b); if (nxt_slow_path(rc != NXT_OK)) { nxt_port_rpc_cancel(task, controller_port, stream); @@ -2374,7 +2291,7 @@ nxt_controller_process_control(nxt_task_t *task, not_allowed: resp.status = 405; - resp.title = (u_char *) "Method isn't allowed."; + resp.title = (u_char *) "Method isn't allowed."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2383,7 +2300,7 @@ nxt_controller_process_control(nxt_task_t *task, not_found: resp.status = 404; - resp.title = (u_char *) "Value doesn't exist."; + resp.title = (u_char *) "Value doesn't exist."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2392,7 +2309,7 @@ nxt_controller_process_control(nxt_task_t *task, alloc_fail: resp.status = 500; - resp.title = (u_char *) "Memory allocation failed."; + resp.title = (u_char *) "Memory allocation failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); @@ -2401,19 +2318,17 @@ nxt_controller_process_control(nxt_task_t *task, fail: resp.status = 500; - resp.title = (u_char *) "Send restart failed."; + resp.title = (u_char *) "Send restart failed."; resp.offset = -1; nxt_controller_response(task, req, &resp); } - static void nxt_controller_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_controller_request_t *req; - nxt_controller_response_t resp; + void *data) { + nxt_controller_request_t *req; + nxt_controller_response_t resp; req = data; @@ -2425,11 +2340,11 @@ nxt_controller_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) { resp.status = 200; - resp.title = (u_char *) "Ok"; + resp.title = (u_char *) "Ok"; } else { resp.status = 500; - resp.title = (u_char *) "Failed to restart app."; + resp.title = (u_char *) "Failed to restart app."; resp.offset = -1; } @@ -2438,17 +2353,15 @@ nxt_controller_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_controller_flush_requests(task); } - static void -nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) -{ - void *mem; - u_char *end; +nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) { + void *mem; + u_char *end; size_t size; nxt_fd_t fd; - nxt_buf_t *b; - nxt_port_t *main_port; - nxt_runtime_t *rt; + nxt_buf_t *b; + nxt_port_t *main_port; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -2480,8 +2393,7 @@ nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) b->mem.free = nxt_cpymem(b->mem.pos, &size, sizeof(size_t)); (void) nxt_port_socket_write(task, main_port, - NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_CLOSE_FD, - fd, 0, -1, b); + NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_CLOSE_FD, fd, 0, -1, b); return; @@ -2490,28 +2402,26 @@ nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) nxt_fd_close(fd); } - static void nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, - nxt_controller_response_t *resp) -{ - size_t size; - nxt_str_t status_line, str; - nxt_buf_t *b, *body; - nxt_conn_t *c; - nxt_uint_t n; - nxt_conf_value_t *value, *location; - nxt_conf_json_pretty_t pretty; - - static const nxt_str_t success_str = nxt_string("success"); - static const nxt_str_t error_str = nxt_string("error"); - static const nxt_str_t detail_str = nxt_string("detail"); - static const nxt_str_t location_str = nxt_string("location"); - static const nxt_str_t offset_str = nxt_string("offset"); - static const nxt_str_t line_str = nxt_string("line"); - static const nxt_str_t column_str = nxt_string("column"); - - static nxt_time_string_t date_cache = { + nxt_controller_response_t *resp) { + size_t size; + nxt_str_t status_line, str; + nxt_buf_t *b, *body; + nxt_conn_t *c; + nxt_uint_t n; + nxt_conf_value_t *value, *location; + nxt_conf_json_pretty_t pretty; + + static const nxt_str_t success_str = nxt_string("success"); + static const nxt_str_t error_str = nxt_string("error"); + static const nxt_str_t detail_str = nxt_string("detail"); + static const nxt_str_t location_str = nxt_string("location"); + static const nxt_str_t offset_str = nxt_string("offset"); + static const nxt_str_t line_str = nxt_string("line"); + static const nxt_str_t column_str = nxt_string("column"); + + static nxt_time_string_t date_cache = { (nxt_atomic_uint_t) -1, nxt_controller_date, "%s, %02d %s %4d %02d:%02d:%02d GMT", @@ -2521,7 +2431,6 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, }; switch (resp->status) { - case 200: nxt_str_set(&status_line, "200 OK"); break; @@ -2543,12 +2452,11 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, break; } - c = req->conn; + c = req->conn; value = resp->conf; if (value == NULL) { - n = 1 - + (resp->detail.length != 0) + n = 1 + (resp->detail.length != 0) + (resp->status >= 400 && resp->offset != -1); value = nxt_conf_create_object(c->mem_pool, n); @@ -2559,7 +2467,7 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, } str.length = nxt_strlen(resp->title); - str.start = resp->title; + str.start = resp->title; if (resp->status < 400) { nxt_conf_set_member_string(value, &success_str, &str, 0); @@ -2579,19 +2487,18 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, if (resp->status >= 400 && resp->offset != -1) { n++; - location = nxt_conf_create_object(c->mem_pool, - resp->line != 0 ? 3 : 1); + location + = nxt_conf_create_object(c->mem_pool, resp->line != 0 ? 3 : 1); nxt_conf_set_member(value, &location_str, location, n); nxt_conf_set_member_integer(location, &offset_str, resp->offset, 0); if (resp->line != 0) { - nxt_conf_set_member_integer(location, &line_str, - resp->line, 1); + nxt_conf_set_member_integer(location, &line_str, resp->line, 1); - nxt_conf_set_member_integer(location, &column_str, - resp->column, 2); + nxt_conf_set_member_integer(location, &column_str, resp->column, + 2); } } } @@ -2612,12 +2519,14 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, body->mem.free = nxt_cpymem(body->mem.free, "\r\n", 2); - size = nxt_length("HTTP/1.1 " "\r\n") + status_line.length - + nxt_length("Server: " NXT_SERVER "\r\n") + size = nxt_length("HTTP/1.1 " + "\r\n") + + status_line.length + nxt_length("Server: " NXT_SERVER "\r\n") + nxt_length("Date: Wed, 31 Dec 1986 16:40:00 GMT\r\n") + nxt_length("Content-Type: application/json\r\n") - + nxt_length("Content-Length: " "\r\n") + NXT_SIZE_T_LEN - + nxt_length("Connection: close\r\n") + + nxt_length("Content-Length: " + "\r\n") + + NXT_SIZE_T_LEN + nxt_length("Connection: close\r\n") + nxt_length("\r\n"); b = nxt_buf_mem_alloc(c->mem_pool, size, 0); @@ -2631,8 +2540,8 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, nxt_str_set(&str, "HTTP/1.1 "); b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); - b->mem.free = nxt_cpymem(b->mem.free, status_line.start, - status_line.length); + b->mem.free + = nxt_cpymem(b->mem.free, status_line.start, status_line.length); nxt_str_set(&str, "\r\n" "Server: " NXT_SERVER "\r\n" @@ -2640,8 +2549,8 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); - b->mem.free = nxt_thread_time_string(task->thread, &date_cache, - b->mem.free); + b->mem.free + = nxt_thread_time_string(task->thread, &date_cache, b->mem.free); nxt_str_set(&str, "\r\n" "Content-Type: application/json\r\n" @@ -2650,7 +2559,7 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); b->mem.free = nxt_sprintf(b->mem.free, b->mem.end, "%uz", - nxt_buf_mem_used_size(&body->mem)); + nxt_buf_mem_used_size(&body->mem)); nxt_str_set(&str, "\r\n" "Connection: close\r\n" @@ -2658,26 +2567,22 @@ nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); - c->write = b; + c->write = b; c->write_state = &nxt_controller_conn_write_state; nxt_conn_write(task->thread->engine, c); } - static u_char * nxt_controller_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, - size_t size, const char *format) -{ - static const char * const week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", - "Fri", "Sat" }; - - static const char * const month[] = { "Jan", "Feb", "Mar", "Apr", "May", - "Jun", "Jul", "Aug", "Sep", "Oct", - "Nov", "Dec" }; - - return nxt_sprintf(buf, buf + size, format, - week[tm->tm_wday], tm->tm_mday, - month[tm->tm_mon], tm->tm_year + 1900, - tm->tm_hour, tm->tm_min, tm->tm_sec); + size_t size, const char *format) { + static const char *const week[] + = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + + static const char *const month[] = {"Jan", "Feb", "Mar", "Apr", "May", + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + return nxt_sprintf(buf, buf + size, format, week[tm->tm_wday], tm->tm_mday, + month[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour, tm->tm_min, + tm->tm_sec); } diff --git a/src/nxt_credential.c b/src/nxt_credential.c index 1c9fa9a7a..e38252618 100644 --- a/src/nxt_credential.c +++ b/src/nxt_credential.c @@ -6,26 +6,23 @@ #include -static nxt_int_t nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, - nxt_credential_t *uc); - +static nxt_int_t +nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc); nxt_int_t nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, - const char *group) -{ - struct group *grp; - struct passwd *pwd; + const char *group) { + struct group *grp; + struct passwd *pwd; nxt_errno = 0; pwd = getpwnam(uc->user); if (nxt_slow_path(pwd == NULL)) { - if (nxt_errno == 0) { nxt_alert(task, "getpwnam(\"%s\") failed, user \"%s\" not found", - uc->user, uc->user); + uc->user, uc->user); } else { nxt_alert(task, "getpwnam(\"%s\") failed %E", uc->user, nxt_errno); } @@ -33,7 +30,7 @@ nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, return NXT_ERROR; } - uc->uid = pwd->pw_uid; + uc->uid = pwd->pw_uid; uc->base_gid = pwd->pw_gid; if (group != NULL && group[0] != '\0') { @@ -42,11 +39,10 @@ nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, grp = getgrnam(group); if (nxt_slow_path(grp == NULL)) { - if (nxt_errno == 0) { nxt_alert(task, - "getgrnam(\"%s\") failed, group \"%s\" not found", - group, group); + "getgrnam(\"%s\") failed, group \"%s\" not found", group, + group); } else { nxt_alert(task, "getgrnam(\"%s\") failed %E", group, nxt_errno); } @@ -58,7 +54,7 @@ nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, } nxt_debug(task, "about to get \"%s\" groups (uid:%d, base gid:%d)", - uc->user, uc->uid, uc->base_gid); + uc->user, uc->uid, uc->base_gid); if (nxt_credential_groups_get(task, mp, uc) != NXT_OK) { return NXT_ERROR; @@ -66,11 +62,11 @@ nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, #if (NXT_DEBUG) { - u_char *p, *end; - nxt_uint_t i; - u_char msg[NXT_MAX_ERROR_STR]; + u_char *p, *end; + nxt_uint_t i; + u_char msg[NXT_MAX_ERROR_STR]; - p = msg; + p = msg; end = msg + NXT_MAX_ERROR_STR; for (i = 0; i < uc->ngroups; i++) { @@ -93,20 +89,18 @@ nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, #define NXT_NGROUPS nxt_min(256, NGROUPS_MAX) - static nxt_int_t nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, - nxt_credential_t *uc) -{ - int ngroups; - gid_t groups[NXT_NGROUPS]; + nxt_credential_t *uc) { + int ngroups; + gid_t groups[NXT_NGROUPS]; ngroups = NXT_NGROUPS; if (getgrouplist(uc->user, uc->base_gid, groups, &ngroups) < 0) { if (nxt_slow_path(ngroups <= NXT_NGROUPS)) { nxt_alert(task, "getgrouplist(\"%s\", %d, ...) failed %E", uc->user, - uc->base_gid, nxt_errno); + uc->base_gid, nxt_errno); return NXT_ERROR; } @@ -124,11 +118,10 @@ nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, return NXT_ERROR; } - if (nxt_slow_path(getgrouplist(uc->user, uc->base_gid, uc->gids, - &ngroups) < 0)) { - + if (nxt_slow_path( + getgrouplist(uc->user, uc->base_gid, uc->gids, &ngroups) < 0)) { nxt_alert(task, "getgrouplist(\"%s\", %d) failed %E", uc->user, - uc->base_gid, nxt_errno); + uc->base_gid, nxt_errno); return NXT_ERROR; } @@ -180,11 +173,11 @@ nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, */ static nxt_int_t -nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc) -{ +nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, + nxt_credential_t *uc) { int nsaved, ngroups; nxt_int_t ret; - nxt_gid_t *saved; + nxt_gid_t *saved; nsaved = getgroups(0, NULL); @@ -198,7 +191,7 @@ nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc) if (nsaved > NGROUPS_MAX) { /* MacOSX case. */ - uc->gids = NULL; + uc->gids = NULL; uc->ngroups = 0; return NXT_OK; @@ -224,16 +217,16 @@ nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc) if (initgroups(uc->user, uc->base_gid) != 0) { if (nxt_errno == NXT_EPERM) { nxt_log(task, NXT_LOG_NOTICE, - "initgroups(%s, %d) failed %E, ignored", - uc->user, uc->base_gid, nxt_errno); + "initgroups(%s, %d) failed %E, ignored", uc->user, uc->base_gid, + nxt_errno); ret = NXT_OK; goto free; } else { - nxt_alert(task, "initgroups(%s, %d) failed %E", - uc->user, uc->base_gid, nxt_errno); + nxt_alert(task, "initgroups(%s, %d) failed %E", uc->user, + uc->base_gid, nxt_errno); goto restore; } } @@ -283,16 +276,16 @@ nxt_credential_groups_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc) nxt_int_t -nxt_credential_setuid(nxt_task_t *task, nxt_credential_t *uc) -{ +nxt_credential_setuid(nxt_task_t *task, nxt_credential_t *uc) { nxt_debug(task, "user cred set: \"%s\" uid:%d", uc->user, uc->uid); if (setuid(uc->uid) != 0) { - #if (NXT_HAVE_LINUX_NS) if (nxt_errno == EINVAL) { - nxt_log(task, NXT_LOG_ERR, "The uid %d (user \"%s\") isn't " - "valid in the application namespace.", uc->uid, uc->user); + nxt_log(task, NXT_LOG_ERR, + "The uid %d (user \"%s\") isn't " + "valid in the application namespace.", + uc->uid, uc->user); return NXT_ERROR; } #endif @@ -304,23 +297,22 @@ nxt_credential_setuid(nxt_task_t *task, nxt_credential_t *uc) return NXT_OK; } - nxt_int_t -nxt_credential_setgids(nxt_task_t *task, nxt_credential_t *uc) -{ - nxt_runtime_t *rt; +nxt_credential_setgids(nxt_task_t *task, nxt_credential_t *uc) { + nxt_runtime_t *rt; nxt_debug(task, "user cred set gids: base gid:%d, ngroups: %d", - uc->base_gid, uc->ngroups); + uc->base_gid, uc->ngroups); rt = task->thread->runtime; if (setgid(uc->base_gid) != 0) { - #if (NXT_HAVE_LINUX_NS) if (nxt_errno == EINVAL) { - nxt_log(task, NXT_LOG_ERR, "The gid %d isn't valid in the " - "application namespace.", uc->base_gid); + nxt_log(task, NXT_LOG_ERR, + "The gid %d isn't valid in the " + "application namespace.", + uc->base_gid); return NXT_ERROR; } #endif @@ -333,14 +325,15 @@ nxt_credential_setgids(nxt_task_t *task, nxt_credential_t *uc) return NXT_OK; } - if (nxt_slow_path(uc->ngroups > 0 - && setgroups(uc->ngroups, uc->gids) != 0)) { - + if (nxt_slow_path( + uc->ngroups > 0 && setgroups(uc->ngroups, uc->gids) != 0)) { #if (NXT_HAVE_LINUX_NS) if (nxt_errno == EINVAL) { - nxt_log(task, NXT_LOG_ERR, "The user \"%s\" (uid: %d) has " - "supplementary group ids not valid in the application " - "namespace.", uc->user, uc->uid); + nxt_log(task, NXT_LOG_ERR, + "The user \"%s\" (uid: %d) has " + "supplementary group ids not valid in the application " + "namespace.", + uc->user, uc->uid); return NXT_ERROR; } #endif diff --git a/src/nxt_credential.h b/src/nxt_credential.h index 243eba83e..0fa3eba95 100644 --- a/src/nxt_credential.h +++ b/src/nxt_credential.h @@ -7,24 +7,24 @@ #define _NXT_CREDENTIAL_H_INCLUDED_ -typedef uid_t nxt_uid_t; -typedef gid_t nxt_gid_t; +typedef uid_t nxt_uid_t; +typedef gid_t nxt_gid_t; typedef struct { - const char *user; + const char *user; nxt_uid_t uid; nxt_gid_t base_gid; nxt_uint_t ngroups; - nxt_gid_t *gids; + nxt_gid_t *gids; } nxt_credential_t; - -NXT_EXPORT nxt_int_t nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, - nxt_credential_t *uc, const char *group); -NXT_EXPORT nxt_int_t nxt_credential_setuid(nxt_task_t *task, - nxt_credential_t *uc); -NXT_EXPORT nxt_int_t nxt_credential_setgids(nxt_task_t *task, - nxt_credential_t *uc); +NXT_EXPORT nxt_int_t +nxt_credential_get(nxt_task_t *task, nxt_mp_t *mp, nxt_credential_t *uc, + const char *group); +NXT_EXPORT nxt_int_t +nxt_credential_setuid(nxt_task_t *task, nxt_credential_t *uc); +NXT_EXPORT nxt_int_t +nxt_credential_setgids(nxt_task_t *task, nxt_credential_t *uc); #endif /* _NXT_CREDENTIAL_H_INCLUDED_ */ diff --git a/src/nxt_cyassl.c b/src/nxt_cyassl.c index 91039e0c5..964c6e3de 100644 --- a/src/nxt_cyassl.c +++ b/src/nxt_cyassl.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Igor Sysoev @@ -8,49 +7,58 @@ #include #include - typedef struct { - CYASSL *session; + CYASSL *session; - int ssl_error; - uint8_t times; /* 2 bits */ + int ssl_error; + uint8_t times; /* 2 bits */ - nxt_buf_mem_t buffer; + nxt_buf_mem_t buffer; } nxt_cyassl_conn_t; - -static nxt_int_t nxt_cyassl_server_init(nxt_ssltls_conf_t *conf); -static void nxt_cyassl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, +static nxt_int_t +nxt_cyassl_server_init(nxt_ssltls_conf_t *conf); +static void +nxt_cyassl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, nxt_event_conn_t *c); -static void nxt_cyassl_session_cleanup(void *data); -static int nxt_cyassl_io_recv(CYASSL *ssl, char *buf, int size, void *data); -static int nxt_cyassl_io_send(CYASSL *ssl, char *buf, int size, void *data); -static void nxt_cyassl_conn_handshake(nxt_thread_t *thr, void *obj, void *data); -static void nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data); -static void nxt_cyassl_conn_io_shutdown(nxt_thread_t *thr, void *obj, - void *data); -static ssize_t nxt_cyassl_conn_io_write_chunk(nxt_thread_t *thr, - nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static ssize_t nxt_cyassl_conn_io_send(nxt_event_conn_t *c, void *buf, - size_t size); -static nxt_int_t nxt_cyassl_conn_test_error(nxt_thread_t *thr, - nxt_event_conn_t *c, int err, nxt_work_handler_t handler); -static void nxt_cdecl nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, - const char *fmt, ...); -static nxt_uint_t nxt_cyassl_log_error_level(nxt_event_conn_t *c, nxt_err_t err, - int ssl_error); -static void nxt_cdecl nxt_cyassl_log_error(nxt_uint_t level, nxt_log_t *log, - int ret, const char *fmt, ...); -static u_char *nxt_cyassl_copy_error(int err, u_char *p, u_char *end); - - -const nxt_ssltls_lib_t nxt_cyassl_lib = { +static void +nxt_cyassl_session_cleanup(void *data); +static int +nxt_cyassl_io_recv(CYASSL *ssl, char *buf, int size, void *data); +static int +nxt_cyassl_io_send(CYASSL *ssl, char *buf, int size, void *data); +static void +nxt_cyassl_conn_handshake(nxt_thread_t *thr, void *obj, void *data); +static void +nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data); +static void +nxt_cyassl_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data); +static ssize_t +nxt_cyassl_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, + nxt_buf_t *b, size_t limit); +static ssize_t +nxt_cyassl_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size); +static nxt_int_t +nxt_cyassl_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, int err, + nxt_work_handler_t handler); +static void nxt_cdecl +nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, const char *fmt, ...); +static nxt_uint_t +nxt_cyassl_log_error_level(nxt_event_conn_t *c, nxt_err_t err, int ssl_error); +static void nxt_cdecl +nxt_cyassl_log_error(nxt_uint_t level, nxt_log_t *log, int ret, const char *fmt, + ...); +static u_char * +nxt_cyassl_copy_error(int err, u_char *p, u_char *end); + + +const nxt_ssltls_lib_t nxt_cyassl_lib = { nxt_cyassl_server_init, NULL, }; -static nxt_event_conn_io_t nxt_cyassl_event_conn_io = { +static nxt_event_conn_io_t nxt_cyassl_event_conn_io = { NULL, NULL, @@ -67,13 +75,11 @@ static nxt_event_conn_io_t nxt_cyassl_event_conn_io = { nxt_cyassl_conn_io_shutdown, }; - static nxt_int_t -nxt_cyassl_start(void) -{ - int err; - nxt_thread_t *thr; - static nxt_bool_t started; +nxt_cyassl_start(void) { + int err; + nxt_thread_t *thr; + static nxt_bool_t started; if (nxt_fast_path(started)) { return NXT_OK; @@ -88,12 +94,12 @@ nxt_cyassl_start(void) err = CyaSSL_Init(); if (err != SSL_SUCCESS) { nxt_cyassl_log_error(NXT_LOG_ALERT, thr->log, err, - "CyaSSL_Init() failed"); + "CyaSSL_Init() failed"); return NXT_ERROR; } nxt_thread_log_error(NXT_LOG_INFO, "CyaSSL version: %s", - LIBCYASSL_VERSION_STRING); + LIBCYASSL_VERSION_STRING); /* CyaSSL_SetLoggingCb */ /* CyaSSL_SetAllocators */ @@ -101,14 +107,12 @@ nxt_cyassl_start(void) return NXT_OK; } - static nxt_int_t -nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) -{ +nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) { int err; - char *certificate, *key; - CYASSL_CTX *ctx; - nxt_thread_t *thr; + char *certificate, *key; + CYASSL_CTX *ctx; + nxt_thread_t *thr; thr = nxt_thread(); @@ -119,11 +123,11 @@ nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) ctx = CyaSSL_CTX_new(CyaSSLv23_server_method()); if (ctx == NULL) { nxt_cyassl_log_error(NXT_LOG_ALERT, thr->log, 0, - "CyaSSL_CTX_new() failed"); + "CyaSSL_CTX_new() failed"); return NXT_ERROR; } - conf->ctx = ctx; + conf->ctx = ctx; conf->conn_init = nxt_cyassl_conn_init; certificate = conf->certificate; @@ -131,8 +135,7 @@ nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) err = CyaSSL_CTX_use_certificate_file(ctx, certificate, SSL_FILETYPE_PEM); if (err != SSL_SUCCESS) { nxt_cyassl_log_error(NXT_LOG_ALERT, thr->log, err, - "CyaSSL_CTX_use_certificate_file(\"%s\") failed", - certificate); + "CyaSSL_CTX_use_certificate_file(\"%s\") failed", certificate); goto fail; } @@ -141,8 +144,7 @@ nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) err = CyaSSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM); if (err != SSL_SUCCESS) { nxt_cyassl_log_error(NXT_LOG_ALERT, thr->log, err, - "CyaSSL_CTX_use_PrivateKey_file(\"%s\") failed", - key); + "CyaSSL_CTX_use_PrivateKey_file(\"%s\") failed", key); goto fail; } @@ -150,8 +152,7 @@ nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) err = CyaSSL_CTX_set_cipher_list(ctx, conf->ciphers); if (err != SSL_SUCCESS) { nxt_cyassl_log_error(NXT_LOG_ALERT, thr->log, err, - "CyaSSL_CTX_set_cipher_list(\"%s\") failed", - conf->ciphers); + "CyaSSL_CTX_set_cipher_list(\"%s\") failed", conf->ciphers); goto fail; } } @@ -170,15 +171,13 @@ nxt_cyassl_server_init(nxt_ssltls_conf_t *conf) return NXT_ERROR; } - static void nxt_cyassl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, - nxt_event_conn_t *c) -{ - CYASSL *s; - CYASSL_CTX *ctx; - nxt_cyassl_conn_t *ssltls; - nxt_mem_pool_cleanup_t *mpcl; + nxt_event_conn_t *c) { + CYASSL *s; + CYASSL_CTX *ctx; + nxt_cyassl_conn_t *ssltls; + nxt_mem_pool_cleanup_t *mpcl; nxt_log_debug(c->socket.log, "cyassl conn init"); @@ -200,18 +199,18 @@ nxt_cyassl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, s = CyaSSL_new(ctx); if (s == NULL) { nxt_cyassl_log_error(NXT_LOG_ALERT, c->socket.log, 0, - "CyaSSL_new() failed"); + "CyaSSL_new() failed"); goto fail; } ssltls->session = s; - mpcl->handler = nxt_cyassl_session_cleanup; - mpcl->data = ssltls; + mpcl->handler = nxt_cyassl_session_cleanup; + mpcl->data = ssltls; CyaSSL_SetIOReadCtx(s, c); CyaSSL_SetIOWriteCtx(s, c); - c->io = &nxt_cyassl_event_conn_io; + c->io = &nxt_cyassl_event_conn_io; c->sendfile = NXT_CONN_SENDFILE_OFF; nxt_cyassl_conn_handshake(thr, c, c->socket.data); @@ -220,14 +219,12 @@ nxt_cyassl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, fail: nxt_event_conn_io_handle(thr, c->read_work_queue, - c->read_state->error_handler, c, c->socket.data); + c->read_state->error_handler, c, c->socket.data); } - static void -nxt_cyassl_session_cleanup(void *data) -{ - nxt_cyassl_conn_t *ssltls; +nxt_cyassl_session_cleanup(void *data) { + nxt_cyassl_conn_t *ssltls; ssltls = data; @@ -238,15 +235,13 @@ nxt_cyassl_session_cleanup(void *data) CyaSSL_free(ssltls->session); } - static int -nxt_cyassl_io_recv(CYASSL *ssl, char *buf, int size, void *data) -{ +nxt_cyassl_io_recv(CYASSL *ssl, char *buf, int size, void *data) { ssize_t n; - nxt_thread_t *thr; - nxt_event_conn_t *c; + nxt_thread_t *thr; + nxt_event_conn_t *c; - c = data; + c = data; thr = nxt_thread(); n = thr->engine->event->io->recv(c, (u_char *) buf, size, 0); @@ -266,15 +261,13 @@ nxt_cyassl_io_recv(CYASSL *ssl, char *buf, int size, void *data) return CYASSL_CBIO_ERR_GENERAL; } - static int -nxt_cyassl_io_send(CYASSL *ssl, char *buf, int size, void *data) -{ +nxt_cyassl_io_send(CYASSL *ssl, char *buf, int size, void *data) { ssize_t n; - nxt_thread_t *thr; - nxt_event_conn_t *c; + nxt_thread_t *thr; + nxt_event_conn_t *c; - c = data; + c = data; thr = nxt_thread(); n = thr->engine->event->io->send(c, (u_char *) buf, size); @@ -290,17 +283,15 @@ nxt_cyassl_io_send(CYASSL *ssl, char *buf, int size, void *data) return CYASSL_CBIO_ERR_GENERAL; } - static void -nxt_cyassl_conn_handshake(nxt_thread_t *thr, void *obj, void *data) -{ +nxt_cyassl_conn_handshake(nxt_thread_t *thr, void *obj, void *data) { int ret; nxt_int_t n; nxt_err_t err; - nxt_event_conn_t *c; - nxt_cyassl_conn_t *ssltls; + nxt_event_conn_t *c; + nxt_cyassl_conn_t *ssltls; - c = obj; + c = obj; ssltls = c->u.ssltls; nxt_log_debug(thr->log, "cyassl conn handshake: %d", ssltls->times); @@ -324,34 +315,32 @@ nxt_cyassl_conn_handshake(nxt_thread_t *thr, void *obj, void *data) if (n == NXT_ERROR) { nxt_cyassl_conn_error(c, err, "CyaSSL_negotiate(%d) failed", - c->socket.fd); + c->socket.fd); nxt_event_conn_io_handle(thr, c->read_work_queue, - c->read_state->error_handler, c, data); + c->read_state->error_handler, c, data); } else if (ssltls->ssl_error == SSL_ERROR_WANT_READ && ssltls->times < 2) { ssltls->times++; } } - static void -nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data) -{ - int ret; - nxt_buf_t *b; - nxt_err_t err; - nxt_int_t n; - nxt_event_conn_t *c; - nxt_cyassl_conn_t *ssltls; - nxt_work_handler_t handler; +nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data) { + int ret; + nxt_buf_t *b; + nxt_err_t err; + nxt_int_t n; + nxt_event_conn_t *c; + nxt_cyassl_conn_t *ssltls; + nxt_work_handler_t handler; c = obj; nxt_log_debug(thr->log, "cyassl conn read"); handler = c->read_state->ready_handler; - b = c->read; + b = c->read; /* b == NULL is used to test descriptor readiness. */ @@ -359,29 +348,28 @@ nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data) ssltls = c->u.ssltls; ret = CyaSSL_read(ssltls->session, b->mem.free, - b->mem.end - b->mem.free); + b->mem.end - b->mem.free); err = (ret <= 0) ? nxt_socket_errno : 0; - nxt_log_debug(thr->log, "CyaSSL_read(%d, %p, %uz): %d", - c->socket.fd, b->mem.free, b->mem.end - b->mem.free, ret); + nxt_log_debug(thr->log, "CyaSSL_read(%d, %p, %uz): %d", c->socket.fd, + b->mem.free, b->mem.end - b->mem.free, ret); if (ret > 0) { /* c->socket.read_ready is kept. */ b->mem.free += ret; - handler = c->read_state->ready_handler; + handler = c->read_state->ready_handler; } else { n = nxt_cyassl_conn_test_error(thr, c, ret, - nxt_cyassl_conn_io_read); + nxt_cyassl_conn_io_read); if (nxt_fast_path(n != NXT_ERROR)) { return; } nxt_cyassl_conn_error(c, err, "CyaSSL_read(%d, %p, %uz) failed", - c->socket.fd, b->mem.free, - b->mem.end - b->mem.free); + c->socket.fd, b->mem.free, b->mem.end - b->mem.free); handler = c->read_state->error_handler; } @@ -390,12 +378,10 @@ nxt_cyassl_conn_io_read(nxt_thread_t *thr, void *obj, void *data) nxt_event_conn_io_handle(thr, c->read_work_queue, handler, c, data); } - static ssize_t nxt_cyassl_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, - nxt_buf_t *b, size_t limit) -{ - nxt_cyassl_conn_t *ssltls; + nxt_buf_t *b, size_t limit) { + nxt_cyassl_conn_t *ssltls; nxt_log_debug(thr->log, "cyassl conn write chunk"); @@ -404,14 +390,12 @@ nxt_cyassl_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, return nxt_sendbuf_copy_coalesce(c, &ssltls->buffer, b, limit); } - static ssize_t -nxt_cyassl_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size) -{ +nxt_cyassl_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size) { int ret; nxt_err_t err; nxt_int_t n; - nxt_cyassl_conn_t *ssltls; + nxt_cyassl_conn_t *ssltls; nxt_log_debug(c->socket.log, "cyassl send"); @@ -420,38 +404,36 @@ nxt_cyassl_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size) ret = CyaSSL_write(ssltls->session, buf, size); if (ret <= 0) { - err = nxt_socket_errno; + err = nxt_socket_errno; c->socket.error = err; } else { err = 0; } - nxt_log_debug(c->socket.log, "CyaSSL_write(%d, %p, %uz): %d", - c->socket.fd, buf, size, ret); + nxt_log_debug(c->socket.log, "CyaSSL_write(%d, %p, %uz): %d", c->socket.fd, + buf, size, ret); if (ret > 0) { return ret; } n = nxt_cyassl_conn_test_error(nxt_thread(), c, ret, - nxt_event_conn_io_write); + nxt_event_conn_io_write); if (nxt_slow_path(n == NXT_ERROR)) { nxt_cyassl_conn_error(c, err, "CyaSSL_write(%d, %p, %uz) failed", - c->socket.fd, buf, size); + c->socket.fd, buf, size); } return n; } - static void -nxt_cyassl_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) -{ +nxt_cyassl_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) { int ret; - nxt_event_conn_t *c; - nxt_cyassl_conn_t *ssltls; + nxt_event_conn_t *c; + nxt_cyassl_conn_t *ssltls; c = obj; @@ -468,28 +450,25 @@ nxt_cyassl_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) } nxt_event_conn_io_handle(thr, c->write_work_queue, - c->write_state->close_handler, c, data); + c->write_state->close_handler, c, data); } - static nxt_int_t nxt_cyassl_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, int ret, - nxt_work_handler_t handler) -{ - nxt_work_queue_t *wq; - nxt_cyassl_conn_t *ssltls; + nxt_work_handler_t handler) { + nxt_work_queue_t *wq; + nxt_cyassl_conn_t *ssltls; - ssltls = c->u.ssltls; + ssltls = c->u.ssltls; ssltls->ssl_error = CyaSSL_get_error(ssltls->session, ret); nxt_log_debug(thr->log, "CyaSSL_get_error(): %d", ssltls->ssl_error); switch (ssltls->ssl_error) { - case SSL_ERROR_WANT_READ: nxt_event_fd_block_write(thr->engine, &c->socket); - c->socket.read_ready = 0; + c->socket.read_ready = 0; c->socket.read_handler = handler; if (nxt_event_fd_is_disabled(c->socket.read)) { @@ -501,7 +480,7 @@ nxt_cyassl_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, int ret, case SSL_ERROR_WANT_WRITE: nxt_event_fd_block_read(thr->engine, &c->socket); - c->socket.write_ready = 0; + c->socket.write_ready = 0; c->socket.write_handler = handler; if (nxt_event_fd_is_disabled(c->socket.write)) { @@ -514,11 +493,11 @@ nxt_cyassl_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, int ret, /* A "close notify" alert */ if (c->read_state != NULL) { - wq = c->read_work_queue; + wq = c->read_work_queue; handler = c->read_state->close_handler; } else { - wq = c->write_work_queue; + wq = c->write_work_queue; handler = c->write_state->close_handler; } @@ -531,14 +510,13 @@ nxt_cyassl_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, int ret, } } - static void nxt_cdecl -nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, const char *fmt, ...) -{ - u_char *p, *end; +nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, const char *fmt, + ...) { + u_char *p, *end; va_list args; nxt_uint_t level; - nxt_cyassl_conn_t *ssltls; + nxt_cyassl_conn_t *ssltls; u_char msg[NXT_MAX_ERROR_STR]; ssltls = c->u.ssltls; @@ -546,7 +524,6 @@ nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, const char *fmt, ...) level = nxt_cyassl_log_error_level(c, err, ssltls->ssl_error); if (nxt_log_level_enough(c->socket.log, level)) { - end = msg + sizeof(msg); va_start(args, fmt); @@ -563,14 +540,11 @@ nxt_cyassl_conn_error(nxt_event_conn_t *c, nxt_err_t err, const char *fmt, ...) } } - static nxt_uint_t -nxt_cyassl_log_error_level(nxt_event_conn_t *c, nxt_err_t err, int ssl_error) -{ +nxt_cyassl_log_error_level(nxt_event_conn_t *c, nxt_err_t err, int ssl_error) { switch (ssl_error) { - - case SOCKET_ERROR_E: /* -208 */ - case MATCH_SUITE_ERROR: /* -261 */ + case SOCKET_ERROR_E: /* -208 */ + case MATCH_SUITE_ERROR: /* -261 */ break; default: @@ -580,17 +554,14 @@ nxt_cyassl_log_error_level(nxt_event_conn_t *c, nxt_err_t err, int ssl_error) return NXT_LOG_INFO; } - static void nxt_cdecl -nxt_cyassl_log_error(nxt_uint_t level, nxt_log_t *log, int err, - const char *fmt, ...) -{ - u_char *p, *end; - va_list args; - u_char msg[NXT_MAX_ERROR_STR]; +nxt_cyassl_log_error(nxt_uint_t level, nxt_log_t *log, int err, const char *fmt, + ...) { + u_char *p, *end; + va_list args; + u_char msg[NXT_MAX_ERROR_STR]; if (nxt_log_level_enough(log, level)) { - end = msg + sizeof(msg); va_start(args, fmt); @@ -603,10 +574,8 @@ nxt_cyassl_log_error(nxt_uint_t level, nxt_log_t *log, int err, } } - static u_char * -nxt_cyassl_copy_error(int err, u_char *p, u_char *end) -{ +nxt_cyassl_copy_error(int err, u_char *p, u_char *end) { p = nxt_sprintf(p, end, " (SSL:%d ", err); CyaSSL_ERR_error_string_n(err, (char *) p, end - p); diff --git a/src/nxt_devpoll_engine.c b/src/nxt_devpoll_engine.c index 71ab11a38..cbb4167ca 100644 --- a/src/nxt_devpoll_engine.c +++ b/src/nxt_devpoll_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -21,48 +20,55 @@ */ -#define NXT_DEVPOLL_ADD 0 -#define NXT_DEVPOLL_UPDATE 1 -#define NXT_DEVPOLL_CHANGE 2 -#define NXT_DEVPOLL_DELETE 3 - - -static nxt_int_t nxt_devpoll_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_devpoll_free(nxt_event_engine_t *engine); -static void nxt_devpoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_devpoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static nxt_bool_t nxt_devpoll_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, +#define NXT_DEVPOLL_ADD 0 +#define NXT_DEVPOLL_UPDATE 1 +#define NXT_DEVPOLL_CHANGE 2 +#define NXT_DEVPOLL_DELETE 3 + + +static nxt_int_t +nxt_devpoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_devpoll_free(nxt_event_engine_t *engine); +static void +nxt_devpoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_devpoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op, nxt_uint_t events); -static nxt_int_t nxt_devpoll_commit_changes(nxt_event_engine_t *engine); -static void nxt_devpoll_change_error(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_devpoll_remove(nxt_event_engine_t *engine, nxt_fd_t fd); -static nxt_int_t nxt_devpoll_write(nxt_event_engine_t *engine, - struct pollfd *pfd, size_t n); -static void nxt_devpoll_poll(nxt_event_engine_t *engine, - nxt_msec_t timeout); +static nxt_int_t +nxt_devpoll_commit_changes(nxt_event_engine_t *engine); +static void +nxt_devpoll_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_devpoll_remove(nxt_event_engine_t *engine, nxt_fd_t fd); +static nxt_int_t +nxt_devpoll_write(nxt_event_engine_t *engine, struct pollfd *pfd, size_t n); +static void +nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); -const nxt_event_interface_t nxt_devpoll_engine = { +const nxt_event_interface_t nxt_devpoll_engine = { "devpoll", nxt_devpoll_create, nxt_devpoll_free, @@ -91,16 +97,14 @@ const nxt_event_interface_t nxt_devpoll_engine = { NXT_NO_SIGNAL_EVENTS, }; - static nxt_int_t nxt_devpoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - void *changes; + nxt_uint_t mevents) { + void *changes; - engine->u.devpoll.fd = -1; + engine->u.devpoll.fd = -1; engine->u.devpoll.mchanges = mchanges; - engine->u.devpoll.mevents = mevents; + engine->u.devpoll.mevents = mevents; changes = nxt_malloc(sizeof(nxt_devpoll_change_t) * mchanges); if (changes == NULL) { @@ -143,11 +147,9 @@ nxt_devpoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_devpoll_free(nxt_event_engine_t *engine) -{ - nxt_fd_t fd; +nxt_devpoll_free(nxt_event_engine_t *engine) { + nxt_fd_t fd; fd = engine->u.devpoll.fd; @@ -165,30 +167,24 @@ nxt_devpoll_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.devpoll, sizeof(nxt_devpoll_engine_t)); } - static void -nxt_devpoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_devpoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->write = NXT_EVENT_ACTIVE; nxt_devpoll_change(engine, ev, NXT_DEVPOLL_ADD, POLLIN | POLLOUT); } - static void -nxt_devpoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE || ev->write != NXT_EVENT_INACTIVE) { - - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_devpoll_change(engine, ev, NXT_DEVPOLL_DELETE, POLLREMOVE); } } - /* * Solaris does not automatically remove a closed file descriptor from * a "/dev/poll" set: ioctl(DP_ISPOLLED) for the descriptor returns 1, @@ -215,14 +211,12 @@ nxt_devpoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static nxt_bool_t -nxt_devpoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_devpoll_disable(engine, ev); return ev->changing; } - /* * Solaris poll(7d): * @@ -235,12 +229,10 @@ nxt_devpoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static void -nxt_devpoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_devpoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; if (ev->read != NXT_EVENT_BLOCKED) { - events = POLLIN; if (ev->write == NXT_EVENT_INACTIVE) { @@ -248,10 +240,10 @@ nxt_devpoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } else if (ev->write == NXT_EVENT_BLOCKED) { ev->write = NXT_EVENT_INACTIVE; - op = NXT_DEVPOLL_CHANGE; + op = NXT_DEVPOLL_CHANGE; } else { - op = NXT_DEVPOLL_UPDATE; + op = NXT_DEVPOLL_UPDATE; events = POLLIN | POLLOUT; } @@ -261,14 +253,11 @@ nxt_devpoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->read = NXT_EVENT_ACTIVE; } - static void -nxt_devpoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_devpoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; if (ev->write != NXT_EVENT_BLOCKED) { - events = POLLOUT; if (ev->read == NXT_EVENT_INACTIVE) { @@ -276,10 +265,10 @@ nxt_devpoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } else if (ev->read == NXT_EVENT_BLOCKED) { ev->read = NXT_EVENT_INACTIVE; - op = NXT_DEVPOLL_CHANGE; + op = NXT_DEVPOLL_CHANGE; } else { - op = NXT_DEVPOLL_UPDATE; + op = NXT_DEVPOLL_UPDATE; events = POLLIN | POLLOUT; } @@ -289,93 +278,79 @@ nxt_devpoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->write = NXT_EVENT_ACTIVE; } - static void -nxt_devpoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_devpoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->read = NXT_EVENT_INACTIVE; if (ev->write <= NXT_EVENT_BLOCKED) { ev->write = NXT_EVENT_INACTIVE; - op = NXT_DEVPOLL_DELETE; - events = POLLREMOVE; + op = NXT_DEVPOLL_DELETE; + events = POLLREMOVE; } else { - op = NXT_DEVPOLL_CHANGE; + op = NXT_DEVPOLL_CHANGE; events = POLLOUT; } nxt_devpoll_change(engine, ev, op, events); } - static void -nxt_devpoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_devpoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->write = NXT_EVENT_INACTIVE; if (ev->read <= NXT_EVENT_BLOCKED) { ev->read = NXT_EVENT_INACTIVE; - op = NXT_DEVPOLL_DELETE; - events = POLLREMOVE; + op = NXT_DEVPOLL_DELETE; + events = POLLREMOVE; } else { - op = NXT_DEVPOLL_CHANGE; + op = NXT_DEVPOLL_CHANGE; events = POLLIN; } nxt_devpoll_change(engine, ev, op, events); } - static void -nxt_devpoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_BLOCKED; } } - static void -nxt_devpoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_BLOCKED; } } - static void -nxt_devpoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_devpoll_enable_read(engine, ev); ev->read = NXT_EVENT_ONESHOT; } - static void -nxt_devpoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_devpoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_devpoll_enable_write(engine, ev); ev->write = NXT_EVENT_ONESHOT; } - static void nxt_devpoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - nxt_uint_t op, nxt_uint_t events) -{ - nxt_devpoll_change_t *change; + nxt_uint_t op, nxt_uint_t events) { + nxt_devpoll_change_t *change; nxt_debug(ev->task, "devpoll %d change fd:%d op:%ui ev:%04Xi", - engine->u.devpoll.fd, ev->fd, op, events); + engine->u.devpoll.fd, ev->fd, op, events); if (engine->u.devpoll.nchanges >= engine->u.devpoll.mchanges) { (void) nxt_devpoll_commit_changes(engine); @@ -383,47 +358,45 @@ nxt_devpoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, ev->changing = 1; - change = &engine->u.devpoll.changes[engine->u.devpoll.nchanges++]; - change->op = op; + change = &engine->u.devpoll.changes[engine->u.devpoll.nchanges++]; + change->op = op; change->events = events; - change->event = ev; + change->event = ev; } - static nxt_int_t -nxt_devpoll_commit_changes(nxt_event_engine_t *engine) -{ +nxt_devpoll_commit_changes(nxt_event_engine_t *engine) { size_t n; nxt_int_t ret, retval; - struct pollfd *pfd, *write_changes; - nxt_fd_event_t *ev; - nxt_devpoll_change_t *change, *end; + struct pollfd *pfd, *write_changes; + nxt_fd_event_t *ev; + nxt_devpoll_change_t *change, *end; - nxt_debug(&engine->task, "devpoll %d changes:%ui", - engine->u.devpoll.fd, engine->u.devpoll.nchanges); + nxt_debug(&engine->task, "devpoll %d changes:%ui", engine->u.devpoll.fd, + engine->u.devpoll.nchanges); - retval = NXT_OK; - n = 0; + retval = NXT_OK; + n = 0; write_changes = engine->u.devpoll.write_changes; - change = engine->u.devpoll.changes; - end = change + engine->u.devpoll.nchanges; + change = engine->u.devpoll.changes; + end = change + engine->u.devpoll.nchanges; do { ev = change->event; - nxt_debug(&engine->task, "devpoll fd:%d op:%d ev:%04Xd", - ev->fd, change->op, change->events); + nxt_debug(&engine->task, "devpoll fd:%d op:%d ev:%04Xd", ev->fd, + change->op, change->events); if (change->op == NXT_DEVPOLL_CHANGE) { - pfd = &write_changes[n++]; - pfd->fd = ev->fd; - pfd->events = POLLREMOVE; + pfd = &write_changes[n++]; + pfd->fd = ev->fd; + pfd->events = POLLREMOVE; pfd->revents = 0; } - pfd = &write_changes[n++]; - pfd->fd = ev->fd; - pfd->events = change->events; + pfd = &write_changes[n++]; + pfd->fd = ev->fd; + pfd->events = change->events; pfd->revents = 0; ev->changing = 0; @@ -433,12 +406,11 @@ nxt_devpoll_commit_changes(nxt_event_engine_t *engine) } while (change < end); change = engine->u.devpoll.changes; - end = change + engine->u.devpoll.nchanges; + end = change + engine->u.devpoll.nchanges; ret = nxt_devpoll_write(engine, write_changes, n); if (nxt_slow_path(ret != NXT_OK)) { - do { nxt_devpoll_change_error(engine, change->event); change++; @@ -462,7 +434,7 @@ nxt_devpoll_commit_changes(nxt_event_engine_t *engine) } else if (change->op == NXT_DEVPOLL_DELETE) { nxt_fd_event_hash_delete(&engine->task, &engine->u.devpoll.fd_hash, - ev->fd, 0); + ev->fd, 0); } /* Nothing tp do for NXT_DEVPOLL_UPDATE and NXT_DEVPOLL_CHANGE. */ @@ -476,36 +448,32 @@ nxt_devpoll_commit_changes(nxt_event_engine_t *engine) return retval; } - static void -nxt_devpoll_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_INACTIVE; +nxt_devpoll_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; - nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, - ev->task, ev, ev->data); + nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, ev->task, + ev, ev->data); nxt_fd_event_hash_delete(ev->task, &engine->u.devpoll.fd_hash, ev->fd, 1); nxt_devpoll_remove(engine, ev->fd); } - static void -nxt_devpoll_remove(nxt_event_engine_t *engine, nxt_fd_t fd) -{ - int n; - struct pollfd pfd; +nxt_devpoll_remove(nxt_event_engine_t *engine, nxt_fd_t fd) { + int n; + struct pollfd pfd; - pfd.fd = fd; - pfd.events = 0; + pfd.fd = fd; + pfd.events = 0; pfd.revents = 0; n = ioctl(engine->u.devpoll.fd, DP_ISPOLLED, &pfd); nxt_debug(&engine->task, "ioctl(%d, DP_ISPOLLED, %d): %d", - engine->u.devpoll.fd, fd, n); + engine->u.devpoll.fd, fd, n); if (n == 0) { /* The file descriptor is not in the set. */ @@ -514,27 +482,25 @@ nxt_devpoll_remove(nxt_event_engine_t *engine, nxt_fd_t fd) if (n == -1) { nxt_alert(&engine->task, "ioctl(%d, DP_ISPOLLED, %d) failed %E", - engine->u.devpoll.fd, fd, nxt_errno); + engine->u.devpoll.fd, fd, nxt_errno); /* Fall through. */ } /* n == 1: the file descriptor is in the set. */ - nxt_debug(&engine->task, "devpoll %d remove fd:%d", - engine->u.devpoll.fd, fd); + nxt_debug(&engine->task, "devpoll %d remove fd:%d", engine->u.devpoll.fd, + fd); - pfd.fd = fd; - pfd.events = POLLREMOVE; + pfd.fd = fd; + pfd.events = POLLREMOVE; pfd.revents = 0; nxt_devpoll_write(engine, &pfd, 1); } - static nxt_int_t -nxt_devpoll_write(nxt_event_engine_t *engine, struct pollfd *pfd, size_t n) -{ - int fd; +nxt_devpoll_write(nxt_event_engine_t *engine, struct pollfd *pfd, size_t n) { + int fd; fd = engine->u.devpoll.fd; @@ -551,18 +517,16 @@ nxt_devpoll_write(nxt_event_engine_t *engine, struct pollfd *pfd, size_t n) return NXT_ERROR; } - static void -nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ +nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { int nevents; nxt_fd_t fd; nxt_int_t i; nxt_err_t err; nxt_uint_t events, level; struct dvpoll dvp; - struct pollfd *pfd; - nxt_fd_event_t *ev; + struct pollfd *pfd; + nxt_fd_event_t *ev; if (engine->u.devpoll.nchanges != 0) { if (nxt_devpoll_commit_changes(engine) != NXT_OK) { @@ -572,10 +536,10 @@ nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_debug(&engine->task, "ioctl(%d, DP_POLL) timeout:%M", - engine->u.devpoll.fd, timeout); + engine->u.devpoll.fd, timeout); - dvp.dp_fds = engine->u.devpoll.events; - dvp.dp_nfds = engine->u.devpoll.mevents; + dvp.dp_fds = engine->u.devpoll.events; + dvp.dp_nfds = engine->u.devpoll.mevents; dvp.dp_timeout = timeout; nevents = ioctl(engine->u.devpoll.fd, DP_POLL, &dvp); @@ -584,47 +548,46 @@ nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) nxt_thread_time_update(engine->task.thread); - nxt_debug(&engine->task, "ioctl(%d, DP_POLL): %d", - engine->u.devpoll.fd, nevents); + nxt_debug(&engine->task, "ioctl(%d, DP_POLL): %d", engine->u.devpoll.fd, + nevents); if (nevents == -1) { level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT; nxt_log(&engine->task, level, "ioctl(%d, DP_POLL) failed %E", - engine->u.devpoll.fd, err); + engine->u.devpoll.fd, err); return; } for (i = 0; i < nevents; i++) { - - pfd = &engine->u.devpoll.events[i]; - fd = pfd->fd; + pfd = &engine->u.devpoll.events[i]; + fd = pfd->fd; events = pfd->revents; ev = nxt_fd_event_hash_get(&engine->task, &engine->u.devpoll.fd_hash, - fd); + fd); if (nxt_slow_path(ev == NULL)) { nxt_alert(&engine->task, - "ioctl(%d, DP_POLL) returned invalid " - "fd:%d ev:%04Xd rev:%04uXi", - engine->u.devpoll.fd, fd, pfd->events, events); + "ioctl(%d, DP_POLL) returned invalid " + "fd:%d ev:%04Xd rev:%04uXi", + engine->u.devpoll.fd, fd, pfd->events, events); nxt_devpoll_remove(engine, fd); continue; } - nxt_debug(ev->task, "devpoll: fd:%d ev:%04uXi rd:%d wr:%d", - fd, events, ev->read, ev->write); + nxt_debug(ev->task, "devpoll: fd:%d ev:%04uXi rd:%d wr:%d", fd, events, + ev->read, ev->write); if (nxt_slow_path(events & (POLLERR | POLLHUP | POLLNVAL)) != 0) { nxt_alert(ev->task, - "ioctl(%d, DP_POLL) error fd:%d ev:%04Xd rev:%04uXi", - engine->u.devpoll.fd, fd, pfd->events, events); + "ioctl(%d, DP_POLL) error fd:%d ev:%04Xd rev:%04uXi", + engine->u.devpoll.fd, fd, pfd->events, events); nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); continue; } @@ -633,12 +596,11 @@ nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->read != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } if (ev->read == NXT_EVENT_BLOCKED - || ev->read == NXT_EVENT_ONESHOT) - { + || ev->read == NXT_EVENT_ONESHOT) { nxt_devpoll_disable_read(engine, ev); } } @@ -648,12 +610,11 @@ nxt_devpoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->write != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } if (ev->write == NXT_EVENT_BLOCKED - || ev->write == NXT_EVENT_ONESHOT) - { + || ev->write == NXT_EVENT_ONESHOT) { nxt_devpoll_disable_write(engine, ev); } } diff --git a/src/nxt_djb_hash.c b/src/nxt_djb_hash.c index cd3158693..02beb2fc7 100644 --- a/src/nxt_djb_hash.c +++ b/src/nxt_djb_hash.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,14 +5,12 @@ #include - uint32_t -nxt_djb_hash(const void *data, size_t len) -{ +nxt_djb_hash(const void *data, size_t len) { uint32_t hash; - const u_char *p; + const u_char *p; - p = data; + p = data; hash = NXT_DJB_HASH_INIT; while (len != 0) { @@ -24,19 +21,17 @@ nxt_djb_hash(const void *data, size_t len) return hash; } - uint32_t -nxt_djb_hash_lowcase(const void *data, size_t len) -{ +nxt_djb_hash_lowcase(const void *data, size_t len) { u_char c; uint32_t hash; - const u_char *p; + const u_char *p; - p = data; + p = data; hash = NXT_DJB_HASH_INIT; while (len != 0) { - c = *p++; + c = *p++; hash = nxt_djb_hash_add(hash, nxt_lowcase(c)); len--; } diff --git a/src/nxt_djb_hash.h b/src/nxt_djb_hash.h index 43395e6ae..eb89d5b6b 100644 --- a/src/nxt_djb_hash.h +++ b/src/nxt_djb_hash.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,14 +10,16 @@ /* A fast and simple hash function by Daniel J. Bernstein. */ -NXT_EXPORT uint32_t nxt_djb_hash(const void *data, size_t len); -NXT_EXPORT uint32_t nxt_djb_hash_lowcase(const void *data, size_t len); +NXT_EXPORT uint32_t +nxt_djb_hash(const void *data, size_t len); +NXT_EXPORT uint32_t +nxt_djb_hash_lowcase(const void *data, size_t len); -#define NXT_DJB_HASH_INIT 5381 +#define NXT_DJB_HASH_INIT 5381 -#define nxt_djb_hash_add(hash, val) \ +#define nxt_djb_hash_add(hash, val) \ ((uint32_t) ((((hash) << 5) + (hash)) ^ (uint32_t) (val))) diff --git a/src/nxt_dyld.c b/src/nxt_dyld.c index 63e6be145..0e5a731ac 100644 --- a/src/nxt_dyld.c +++ b/src/nxt_dyld.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,11 +5,9 @@ #include - nxt_int_t -nxt_dyld_load(nxt_dyld_t *dyld) -{ - const char *err; +nxt_dyld_load(nxt_dyld_t *dyld) { + const char *err; dyld->handle = dlopen(dyld->name, RTLD_NOW | RTLD_GLOBAL); @@ -29,21 +26,19 @@ nxt_dyld_load(nxt_dyld_t *dyld) return NXT_ERROR; } - void * -nxt_dyld_symbol(nxt_dyld_t *dyld, const char *symbol) -{ - void *handle, *s; - const char *name; - const char *err; +nxt_dyld_symbol(nxt_dyld_t *dyld, const char *symbol) { + void *handle, *s; + const char *name; + const char *err; if (dyld == NXT_DYLD_ANY) { handle = RTLD_DEFAULT; - name = "RTLD_DEFAULT"; + name = "RTLD_DEFAULT"; } else { handle = dyld->handle; - name = dyld->name; + name = dyld->name; } s = dlsym(handle, symbol); @@ -63,11 +58,9 @@ nxt_dyld_symbol(nxt_dyld_t *dyld, const char *symbol) return s; } - nxt_int_t -nxt_dyld_unload(nxt_dyld_t *dyld) -{ - const char *err; +nxt_dyld_unload(nxt_dyld_t *dyld) { + const char *err; if (dlclose(dyld->handle) == 0) { nxt_thread_log_debug("dlclose(\"%s\")", dyld->name); diff --git a/src/nxt_dyld.h b/src/nxt_dyld.h index 65ce1874a..6b3433881 100644 --- a/src/nxt_dyld.h +++ b/src/nxt_dyld.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,23 +6,23 @@ #ifndef _NXT_UNIX_DYLD_H_INCLUDED_ #define _NXT_UNIX_DYLD_H_INCLUDED_ - typedef struct { - void *handle; - char *name; + void *handle; + char *name; } nxt_dyld_t; - -#define NXT_DYLD_ANY RTLD_DEFAULT +#define NXT_DYLD_ANY RTLD_DEFAULT -#define nxt_dyld_is_valid(dyld) \ - ((dyld)->handle != NULL) +#define nxt_dyld_is_valid(dyld) ((dyld)->handle != NULL) -NXT_EXPORT nxt_int_t nxt_dyld_load(nxt_dyld_t *dyld); -NXT_EXPORT void *nxt_dyld_symbol(nxt_dyld_t *dyld, const char *symbol); -NXT_EXPORT nxt_int_t nxt_dyld_unload(nxt_dyld_t *dyld); +NXT_EXPORT nxt_int_t +nxt_dyld_load(nxt_dyld_t *dyld); +NXT_EXPORT void * +nxt_dyld_symbol(nxt_dyld_t *dyld, const char *symbol); +NXT_EXPORT nxt_int_t +nxt_dyld_unload(nxt_dyld_t *dyld); #endif /* _NXT_UNIX_DYLD_H_INCLUDED_ */ diff --git a/src/nxt_epoll_engine.c b/src/nxt_epoll_engine.c index d53df1bc4..03e394e7b 100644 --- a/src/nxt_epoll_engine.c +++ b/src/nxt_epoll_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -32,79 +31,95 @@ #if (NXT_HAVE_EPOLL_EDGE) -static nxt_int_t nxt_epoll_edge_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); +static nxt_int_t +nxt_epoll_edge_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); #endif -static nxt_int_t nxt_epoll_level_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static nxt_int_t nxt_epoll_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents, nxt_conn_io_t *io, uint32_t mode); -static void nxt_epoll_test_accept4(nxt_event_engine_t *engine, - nxt_conn_io_t *io); -static void nxt_epoll_free(nxt_event_engine_t *engine); -static void nxt_epoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_epoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_epoll_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static nxt_bool_t nxt_epoll_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_enable_accept(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_epoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - int op, uint32_t events); -static void nxt_epoll_commit_changes(nxt_event_engine_t *engine); -static void nxt_epoll_error_handler(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_epoll_level_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static nxt_int_t +nxt_epoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents, nxt_conn_io_t *io, uint32_t mode); +static void +nxt_epoll_test_accept4(nxt_event_engine_t *engine, nxt_conn_io_t *io); +static void +nxt_epoll_free(nxt_event_engine_t *engine); +static void +nxt_epoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_epoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_epoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int op, + uint32_t events); +static void +nxt_epoll_commit_changes(nxt_event_engine_t *engine); +static void +nxt_epoll_error_handler(nxt_task_t *task, void *obj, void *data); #if (NXT_HAVE_SIGNALFD) -static nxt_int_t nxt_epoll_add_signal(nxt_event_engine_t *engine); -static void nxt_epoll_signalfd_handler(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_epoll_add_signal(nxt_event_engine_t *engine); +static void +nxt_epoll_signalfd_handler(nxt_task_t *task, void *obj, void *data); #endif #if (NXT_HAVE_EVENTFD) -static nxt_int_t nxt_epoll_enable_post(nxt_event_engine_t *engine, - nxt_work_handler_t handler); -static void nxt_epoll_eventfd_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo); +static nxt_int_t +nxt_epoll_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler); +static void +nxt_epoll_eventfd_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo); #endif -static void nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); +static void +nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); #if (NXT_HAVE_ACCEPT4) -static void nxt_epoll_conn_io_accept4(nxt_task_t *task, void *obj, - void *data); +static void +nxt_epoll_conn_io_accept4(nxt_task_t *task, void *obj, void *data); #endif #if (NXT_HAVE_EPOLL_EDGE) -static void nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, - void *data); -static void nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, - void *data); -static ssize_t nxt_epoll_edge_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); +static void +nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, void *data); +static void +nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_epoll_edge_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); -static nxt_conn_io_t nxt_epoll_edge_conn_io = { +static nxt_conn_io_t nxt_epoll_edge_conn_io = { .connect = nxt_epoll_edge_conn_io_connect, - .accept = nxt_conn_io_accept, + .accept = nxt_conn_io_accept, - .read = nxt_conn_io_read, + .read = nxt_conn_io_read, .recvbuf = nxt_epoll_edge_conn_io_recvbuf, - .recv = nxt_conn_io_recv, + .recv = nxt_conn_io_recv, - .write = nxt_conn_io_write, + .write = nxt_conn_io_write, .sendbuf = nxt_conn_io_sendbuf, #if (NXT_HAVE_LINUX_SENDFILE) @@ -114,11 +129,11 @@ static nxt_conn_io_t nxt_epoll_edge_conn_io = { #endif .writev = nxt_event_conn_io_writev, - .send = nxt_event_conn_io_send, + .send = nxt_event_conn_io_send, }; -const nxt_event_interface_t nxt_epoll_edge_engine = { +const nxt_event_interface_t nxt_epoll_edge_engine = { "epoll_edge", nxt_epoll_edge_create, nxt_epoll_free, @@ -164,7 +179,7 @@ const nxt_event_interface_t nxt_epoll_edge_engine = { #endif -const nxt_event_interface_t nxt_epoll_level_engine = { +const nxt_event_interface_t nxt_epoll_level_engine = { "epoll_level", nxt_epoll_level_create, nxt_epoll_free, @@ -212,10 +227,9 @@ const nxt_event_interface_t nxt_epoll_level_engine = { static nxt_int_t nxt_epoll_edge_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ + nxt_uint_t mevents) { return nxt_epoll_create(engine, mchanges, mevents, &nxt_epoll_edge_conn_io, - EPOLLET | EPOLLRDHUP); + EPOLLET | EPOLLRDHUP); } #endif @@ -223,21 +237,17 @@ nxt_epoll_edge_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, static nxt_int_t nxt_epoll_level_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - return nxt_epoll_create(engine, mchanges, mevents, - &nxt_unix_conn_io, 0); + nxt_uint_t mevents) { + return nxt_epoll_create(engine, mchanges, mevents, &nxt_unix_conn_io, 0); } - static nxt_int_t nxt_epoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents, nxt_conn_io_t *io, uint32_t mode) -{ - engine->u.epoll.fd = -1; - engine->u.epoll.mode = mode; + nxt_uint_t mevents, nxt_conn_io_t *io, uint32_t mode) { + engine->u.epoll.fd = -1; + engine->u.epoll.mode = mode; engine->u.epoll.mchanges = mchanges; - engine->u.epoll.mevents = mevents; + engine->u.epoll.mevents = mevents; #if (NXT_HAVE_SIGNALFD) engine->u.epoll.signalfd.fd = -1; #endif @@ -261,7 +271,6 @@ nxt_epoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, nxt_debug(&engine->task, "epoll_create(): %d", engine->u.epoll.fd); if (engine->signals != NULL) { - #if (NXT_HAVE_SIGNALFD) if (nxt_epoll_add_signal(engine) != NXT_OK) { @@ -282,14 +291,11 @@ nxt_epoll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_epoll_test_accept4(nxt_event_engine_t *engine, nxt_conn_io_t *io) -{ - static nxt_work_handler_t handler; +nxt_epoll_test_accept4(nxt_event_engine_t *engine, nxt_conn_io_t *io) { + static nxt_work_handler_t handler; if (handler == NULL) { - handler = io->accept; #if (NXT_HAVE_ACCEPT4) @@ -301,7 +307,7 @@ nxt_epoll_test_accept4(nxt_event_engine_t *engine, nxt_conn_io_t *io) } else { nxt_log(&engine->task, NXT_LOG_INFO, "accept4() failed %E", - NXT_ENOSYS); + NXT_ENOSYS); } #endif @@ -310,11 +316,9 @@ nxt_epoll_test_accept4(nxt_event_engine_t *engine, nxt_conn_io_t *io) io->accept = handler; } - static void -nxt_epoll_free(nxt_event_engine_t *engine) -{ - int fd; +nxt_epoll_free(nxt_event_engine_t *engine) { + int fd; nxt_debug(&engine->task, "epoll %d free", engine->u.epoll.fd); @@ -350,44 +354,35 @@ nxt_epoll_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.epoll, sizeof(nxt_epoll_engine_t)); } - static void -nxt_epoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_epoll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->write = NXT_EVENT_ACTIVE; nxt_epoll_change(engine, ev, EPOLL_CTL_ADD, - EPOLLIN | EPOLLOUT | engine->u.epoll.mode); + EPOLLIN | EPOLLOUT | engine->u.epoll.mode); } - static void -nxt_epoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_epoll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read > NXT_EVENT_DISABLED || ev->write > NXT_EVENT_DISABLED) { - - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_epoll_change(engine, ev, EPOLL_CTL_DEL, 0); } } - static void -nxt_epoll_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_epoll_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE || ev->write != NXT_EVENT_INACTIVE) { - - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_epoll_change(engine, ev, EPOLL_CTL_DEL, 0); } } - /* * Although calling close() on a file descriptor will remove any epoll * events that reference the descriptor, in this case the close() acquires @@ -398,23 +393,19 @@ nxt_epoll_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static nxt_bool_t -nxt_epoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_epoll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_epoll_delete(engine, ev); return ev->changing; } - static void -nxt_epoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; - uint32_t events; +nxt_epoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; + uint32_t events; if (ev->read != NXT_EVENT_BLOCKED) { - - op = EPOLL_CTL_MOD; + op = EPOLL_CTL_MOD; events = EPOLLIN | engine->u.epoll.mode; if (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) { @@ -430,16 +421,13 @@ nxt_epoll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->read = NXT_EVENT_ACTIVE; } - static void -nxt_epoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; - uint32_t events; +nxt_epoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; + uint32_t events; if (ev->write != NXT_EVENT_BLOCKED) { - - op = EPOLL_CTL_MOD; + op = EPOLL_CTL_MOD; events = EPOLLOUT | engine->u.epoll.mode; if (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) { @@ -455,69 +443,60 @@ nxt_epoll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->write = NXT_EVENT_ACTIVE; } - static void -nxt_epoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; - uint32_t events; +nxt_epoll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; + uint32_t events; ev->read = NXT_EVENT_INACTIVE; if (ev->write <= NXT_EVENT_DISABLED) { ev->write = NXT_EVENT_INACTIVE; - op = EPOLL_CTL_DEL; - events = 0; + op = EPOLL_CTL_DEL; + events = 0; } else { - op = EPOLL_CTL_MOD; + op = EPOLL_CTL_MOD; events = EPOLLOUT | engine->u.epoll.mode; } nxt_epoll_change(engine, ev, op, events); } - static void -nxt_epoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; - uint32_t events; +nxt_epoll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; + uint32_t events; ev->write = NXT_EVENT_INACTIVE; if (ev->read <= NXT_EVENT_DISABLED) { ev->read = NXT_EVENT_INACTIVE; - op = EPOLL_CTL_DEL; - events = 0; + op = EPOLL_CTL_DEL; + events = 0; } else { - op = EPOLL_CTL_MOD; + op = EPOLL_CTL_MOD; events = EPOLLIN | engine->u.epoll.mode; } nxt_epoll_change(engine, ev, op, events); } - static void -nxt_epoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_epoll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_BLOCKED; } } - static void -nxt_epoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_epoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_BLOCKED; } } - /* * NXT_EVENT_DISABLED state is used to track whether EPOLLONESHOT * event should be added or modified, epoll_ctl(2): @@ -532,39 +511,36 @@ nxt_epoll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static void -nxt_epoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; +nxt_epoll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; - op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ? - EPOLL_CTL_ADD : EPOLL_CTL_MOD; + op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) + ? EPOLL_CTL_ADD + : EPOLL_CTL_MOD; - ev->read = NXT_EVENT_ONESHOT; + ev->read = NXT_EVENT_ONESHOT; ev->write = NXT_EVENT_INACTIVE; nxt_epoll_change(engine, ev, op, EPOLLIN | EPOLLONESHOT); } - static void -nxt_epoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - int op; +nxt_epoll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + int op; - op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ? - EPOLL_CTL_ADD : EPOLL_CTL_MOD; + op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) + ? EPOLL_CTL_ADD + : EPOLL_CTL_MOD; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_ONESHOT; nxt_epoll_change(engine, ev, op, EPOLLOUT | EPOLLONESHOT); } - static void -nxt_epoll_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - uint32_t events; +nxt_epoll_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + uint32_t events; ev->read = NXT_EVENT_ACTIVE; @@ -577,7 +553,6 @@ nxt_epoll_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) nxt_epoll_change(engine, ev, EPOLL_CTL_ADD, events); } - /* * epoll changes are batched to improve instruction and data cache * locality of several epoll_ctl() calls followed by epoll_wait() call. @@ -585,12 +560,11 @@ nxt_epoll_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) static void nxt_epoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int op, - uint32_t events) -{ - nxt_epoll_change_t *change; + uint32_t events) { + nxt_epoll_change_t *change; nxt_debug(ev->task, "epoll %d set event: fd:%d op:%d ev:%XD", - engine->u.epoll.fd, ev->fd, op, events); + engine->u.epoll.fd, ev->fd, op, events); if (engine->u.epoll.nchanges >= engine->u.epoll.mchanges) { nxt_epoll_commit_changes(engine); @@ -598,42 +572,39 @@ nxt_epoll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int op, ev->changing = 1; - change = &engine->u.epoll.changes[engine->u.epoll.nchanges++]; - change->op = op; + change = &engine->u.epoll.changes[engine->u.epoll.nchanges++]; + change->op = op; change->event.events = events; change->event.data.ptr = ev; } - static void -nxt_epoll_commit_changes(nxt_event_engine_t *engine) -{ +nxt_epoll_commit_changes(nxt_event_engine_t *engine) { int ret; - nxt_fd_event_t *ev; - nxt_epoll_change_t *change, *end; + nxt_fd_event_t *ev; + nxt_epoll_change_t *change, *end; - nxt_debug(&engine->task, "epoll %d changes:%ui", - engine->u.epoll.fd, engine->u.epoll.nchanges); + nxt_debug(&engine->task, "epoll %d changes:%ui", engine->u.epoll.fd, + engine->u.epoll.nchanges); change = engine->u.epoll.changes; - end = change + engine->u.epoll.nchanges; + end = change + engine->u.epoll.nchanges; do { - ev = change->event.data.ptr; + ev = change->event.data.ptr; ev->changing = 0; nxt_debug(ev->task, "epoll_ctl(%d): fd:%d op:%d ev:%XD", - engine->u.epoll.fd, ev->fd, change->op, - change->event.events); + engine->u.epoll.fd, ev->fd, change->op, change->event.events); ret = epoll_ctl(engine->u.epoll.fd, change->op, ev->fd, &change->event); if (nxt_slow_path(ret != 0)) { nxt_alert(ev->task, "epoll_ctl(%d, %d, %d) failed %E", - engine->u.epoll.fd, change->op, ev->fd, nxt_errno); + engine->u.epoll.fd, change->op, ev->fd, nxt_errno); nxt_work_queue_add(&engine->fast_work_queue, - nxt_epoll_error_handler, ev->task, ev, ev->data); + nxt_epoll_error_handler, ev->task, ev, ev->data); engine->u.epoll.error = 1; } @@ -645,15 +616,13 @@ nxt_epoll_commit_changes(nxt_event_engine_t *engine) engine->u.epoll.nchanges = 0; } - static void -nxt_epoll_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fd_event_t *ev; +nxt_epoll_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fd_event_t *ev; ev = obj; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; ev->error_handler(ev->task, ev, data); @@ -663,10 +632,9 @@ nxt_epoll_error_handler(nxt_task_t *task, void *obj, void *data) #if (NXT_HAVE_SIGNALFD) static nxt_int_t -nxt_epoll_add_signal(nxt_event_engine_t *engine) -{ - int fd; - struct epoll_event ee; +nxt_epoll_add_signal(nxt_event_engine_t *engine) { + int fd; + struct epoll_event ee; if (sigprocmask(SIG_BLOCK, &engine->signals->sigmask, NULL) != 0) { nxt_alert(&engine->task, "sigprocmask(SIG_BLOCK) failed %E", nxt_errno); @@ -686,7 +654,7 @@ nxt_epoll_add_signal(nxt_event_engine_t *engine) if (fd == -1) { nxt_alert(&engine->task, "signalfd(%d) failed %E", - engine->u.epoll.signalfd.fd, nxt_errno); + engine->u.epoll.signalfd.fd, nxt_errno); return NXT_ERROR; } @@ -698,18 +666,18 @@ nxt_epoll_add_signal(nxt_event_engine_t *engine) nxt_debug(&engine->task, "signalfd(): %d", fd); - engine->u.epoll.signalfd.data = engine->signals->handler; + engine->u.epoll.signalfd.data = engine->signals->handler; engine->u.epoll.signalfd.read_work_queue = &engine->fast_work_queue; - engine->u.epoll.signalfd.read_handler = nxt_epoll_signalfd_handler; - engine->u.epoll.signalfd.log = engine->task.log; - engine->u.epoll.signalfd.task = &engine->task; + engine->u.epoll.signalfd.read_handler = nxt_epoll_signalfd_handler; + engine->u.epoll.signalfd.log = engine->task.log; + engine->u.epoll.signalfd.task = &engine->task; - ee.events = EPOLLIN; + ee.events = EPOLLIN; ee.data.ptr = &engine->u.epoll.signalfd; if (epoll_ctl(engine->u.epoll.fd, EPOLL_CTL_ADD, fd, &ee) != 0) { nxt_alert(&engine->task, "epoll_ctl(%d, %d, %d) failed %E", - engine->u.epoll.fd, EPOLL_CTL_ADD, fd, nxt_errno); + engine->u.epoll.fd, EPOLL_CTL_ADD, fd, nxt_errno); return NXT_ERROR; } @@ -717,16 +685,14 @@ nxt_epoll_add_signal(nxt_event_engine_t *engine) return NXT_OK; } - static void -nxt_epoll_signalfd_handler(nxt_task_t *task, void *obj, void *data) -{ - int n; - nxt_fd_event_t *ev; - nxt_work_handler_t handler; - struct signalfd_siginfo sfd; +nxt_epoll_signalfd_handler(nxt_task_t *task, void *obj, void *data) { + int n; + nxt_fd_event_t *ev; + nxt_work_handler_t handler; + struct signalfd_siginfo sfd; - ev = obj; + ev = obj; handler = data; nxt_debug(task, "signalfd handler"); @@ -751,10 +717,9 @@ nxt_epoll_signalfd_handler(nxt_task_t *task, void *obj, void *data) #if (NXT_HAVE_EVENTFD) static nxt_int_t -nxt_epoll_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) -{ - int ret; - struct epoll_event ee; +nxt_epoll_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) { + int ret; + struct epoll_event ee; engine->u.epoll.post_handler = handler; @@ -782,35 +747,33 @@ nxt_epoll_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) nxt_debug(&engine->task, "eventfd(): %d", engine->u.epoll.eventfd.fd); engine->u.epoll.eventfd.read_work_queue = &engine->fast_work_queue; - engine->u.epoll.eventfd.read_handler = nxt_epoll_eventfd_handler; - engine->u.epoll.eventfd.data = engine; - engine->u.epoll.eventfd.log = engine->task.log; - engine->u.epoll.eventfd.task = &engine->task; + engine->u.epoll.eventfd.read_handler = nxt_epoll_eventfd_handler; + engine->u.epoll.eventfd.data = engine; + engine->u.epoll.eventfd.log = engine->task.log; + engine->u.epoll.eventfd.task = &engine->task; - ee.events = EPOLLIN | EPOLLET; + ee.events = EPOLLIN | EPOLLET; ee.data.ptr = &engine->u.epoll.eventfd; ret = epoll_ctl(engine->u.epoll.fd, EPOLL_CTL_ADD, - engine->u.epoll.eventfd.fd, &ee); + engine->u.epoll.eventfd.fd, &ee); if (nxt_fast_path(ret == 0)) { return NXT_OK; } nxt_alert(&engine->task, "epoll_ctl(%d, %d, %d) failed %E", - engine->u.epoll.fd, EPOLL_CTL_ADD, engine->u.epoll.eventfd.fd, - nxt_errno); + engine->u.epoll.fd, EPOLL_CTL_ADD, engine->u.epoll.eventfd.fd, + nxt_errno); return NXT_ERROR; } - static void -nxt_epoll_eventfd_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_epoll_eventfd_handler(nxt_task_t *task, void *obj, void *data) { int n; uint64_t events; - nxt_event_engine_t *engine; + nxt_event_engine_t *engine; engine = data; @@ -830,24 +793,22 @@ nxt_epoll_eventfd_handler(nxt_task_t *task, void *obj, void *data) n = read(engine->u.epoll.eventfd.fd, &events, sizeof(uint64_t)); - nxt_debug(task, "read(%d): %d events:%uL", - engine->u.epoll.eventfd.fd, n, events); + nxt_debug(task, "read(%d): %d events:%uL", engine->u.epoll.eventfd.fd, + n, events); if (n != sizeof(uint64_t)) { nxt_alert(task, "read eventfd(%d) failed %E", - engine->u.epoll.eventfd.fd, nxt_errno); + engine->u.epoll.eventfd.fd, nxt_errno); } } engine->u.epoll.post_handler(task, NULL, NULL); } - static void -nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo) -{ - size_t ret; - uint64_t event; +nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo) { + size_t ret; + uint64_t event; /* * eventfd() presents along with signalfd(), so the function @@ -860,7 +821,7 @@ nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo) if (nxt_slow_path(ret != sizeof(uint64_t))) { nxt_alert(&engine->task, "write(%d) to eventfd failed %E", - engine->u.epoll.eventfd.fd, nxt_errno); + engine->u.epoll.eventfd.fd, nxt_errno); } } @@ -868,16 +829,15 @@ nxt_epoll_signal(nxt_event_engine_t *engine, nxt_uint_t signo) static void -nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ +nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { int nevents; uint32_t events; nxt_int_t i; nxt_err_t err; nxt_bool_t error; nxt_uint_t level; - nxt_fd_event_t *ev; - struct epoll_event *event; + nxt_fd_event_t *ev; + struct epoll_event *event; if (engine->u.epoll.nchanges != 0) { nxt_epoll_commit_changes(engine); @@ -886,14 +846,14 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (engine->u.epoll.error) { engine->u.epoll.error = 0; /* Error handlers have been enqueued on failure. */ - timeout = 0; + timeout = 0; } - nxt_debug(&engine->task, "epoll_wait(%d) timeout:%M", - engine->u.epoll.fd, timeout); + nxt_debug(&engine->task, "epoll_wait(%d) timeout:%M", engine->u.epoll.fd, + timeout); nevents = epoll_wait(engine->u.epoll.fd, engine->u.epoll.events, - engine->u.epoll.mevents, timeout); + engine->u.epoll.mevents, timeout); err = (nevents == -1) ? nxt_errno : 0; @@ -905,31 +865,28 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT; nxt_log(&engine->task, level, "epoll_wait(%d) failed %E", - engine->u.epoll.fd, err); + engine->u.epoll.fd, err); return; } for (i = 0; i < nevents; i++) { - - event = &engine->u.epoll.events[i]; + event = &engine->u.epoll.events[i]; events = event->events; - ev = event->data.ptr; + ev = event->data.ptr; - nxt_debug(ev->task, "epoll: fd:%d ev:%04XD d:%p rd:%d wr:%d", - ev->fd, events, ev, ev->read, ev->write); + nxt_debug(ev->task, "epoll: fd:%d ev:%04XD d:%p rd:%d wr:%d", ev->fd, + events, ev, ev->read, ev->write); /* * On error epoll may set EPOLLERR and EPOLLHUP only without EPOLLIN * or EPOLLOUT, so the "error" variable enqueues only error handler. */ - error = ((events & (EPOLLERR | EPOLLHUP)) != 0); + error = ((events & (EPOLLERR | EPOLLHUP)) != 0); ev->epoll_error = error; - if (error - && ev->read <= NXT_EVENT_BLOCKED - && ev->write <= NXT_EVENT_BLOCKED) - { + if (error && ev->read <= NXT_EVENT_BLOCKED + && ev->write <= NXT_EVENT_BLOCKED) { error = 0; } @@ -943,13 +900,12 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) ev->read_ready = 1; if (ev->read != NXT_EVENT_BLOCKED) { - if (ev->read == NXT_EVENT_ONESHOT) { ev->read = NXT_EVENT_DISABLED; } nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); error = 0; @@ -963,13 +919,12 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) ev->write_ready = 1; if (ev->write != NXT_EVENT_BLOCKED) { - if (ev->write == NXT_EVENT_ONESHOT) { ev->write = NXT_EVENT_DISABLED; } nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); error = 0; @@ -983,11 +938,10 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) continue; } - ev->read_ready = 1; + ev->read_ready = 1; ev->write_ready = 1; if (ev->read == NXT_EVENT_BLOCKED && ev->write == NXT_EVENT_BLOCKED) { - if (engine->u.epoll.mode == 0) { /* Level-triggered mode. */ nxt_epoll_disable(engine, ev); @@ -997,7 +951,7 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_work_queue_add(&engine->fast_work_queue, nxt_epoll_error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } } @@ -1005,27 +959,26 @@ nxt_epoll_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) #if (NXT_HAVE_ACCEPT4) static void -nxt_epoll_conn_io_accept4(nxt_task_t *task, void *obj, void *data) -{ +nxt_epoll_conn_io_accept4(nxt_task_t *task, void *obj, void *data) { socklen_t socklen; - nxt_conn_t *c; + nxt_conn_t *c; nxt_socket_t s; - struct sockaddr *sa; - nxt_listen_event_t *lev; + struct sockaddr *sa; + nxt_listen_event_t *lev; lev = obj; - c = lev->next; + c = lev->next; lev->ready--; lev->socket.read_ready = (lev->ready != 0); - sa = &c->remote->u.sockaddr; + sa = &c->remote->u.sockaddr; socklen = c->remote->socklen; /* * The returned socklen is ignored here, * see comment in nxt_conn_io_accept(). */ - s = accept4(lev->socket.fd, sa, &socklen, SOCK_NONBLOCK); + s = accept4(lev->socket.fd, sa, &socklen, SOCK_NONBLOCK); if (s != -1) { c->socket.fd = s; @@ -1055,22 +1008,20 @@ nxt_epoll_conn_io_accept4(nxt_task_t *task, void *obj, void *data) */ static void -nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_event_engine_t *engine; +nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_event_engine_t *engine; nxt_work_handler_t handler; - const nxt_event_conn_state_t *state; + const nxt_event_conn_state_t *state; c = obj; state = c->write_state; switch (nxt_socket_connect(task, c->socket.fd, c->remote)) { - case NXT_OK: c->socket.write_ready = 1; - handler = state->ready_handler; + handler = state->ready_handler; break; case NXT_AGAIN: @@ -1117,7 +1068,7 @@ nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, void *data) handler = state->error_handler; break; - default: /* NXT_DECLINED: connection refused. */ + default: /* NXT_DECLINED: connection refused. */ handler = state->close_handler; break; } @@ -1125,11 +1076,9 @@ nxt_epoll_edge_conn_io_connect(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(c->write_work_queue, handler, task, c, data); } - static void -nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1143,14 +1092,13 @@ nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, void *data) } nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, - task, c, data); + task, c, data); return; } nxt_conn_connect_test(task, c, data); } - /* * nxt_epoll_edge_conn_io_recvbuf() is just wrapper around * standard nxt_conn_io_recvbuf() to enforce to read a pending EOF @@ -1158,9 +1106,8 @@ nxt_epoll_edge_conn_connected(nxt_task_t *task, void *obj, void *data) */ static ssize_t -nxt_epoll_edge_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) -{ - ssize_t n; +nxt_epoll_edge_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) { + ssize_t n; n = nxt_conn_io_recvbuf(c, b); diff --git a/src/nxt_errno.c b/src/nxt_errno.c index 47479a82f..2efac062d 100644 --- a/src/nxt_errno.c +++ b/src/nxt_errno.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -22,23 +21,22 @@ * causing false bug reports. */ -static u_char *nxt_bootstrap_strerror(nxt_err_t err, u_char *errstr, - size_t size); -static u_char *nxt_runtime_strerror(nxt_err_t err, u_char *errstr, size_t size); - +static u_char * +nxt_bootstrap_strerror(nxt_err_t err, u_char *errstr, size_t size); +static u_char * +nxt_runtime_strerror(nxt_err_t err, u_char *errstr, size_t size); -nxt_strerror_t nxt_strerror = nxt_bootstrap_strerror; -static nxt_str_t *nxt_sys_errlist; -static nxt_uint_t nxt_sys_nerr; +nxt_strerror_t nxt_strerror = nxt_bootstrap_strerror; +static nxt_str_t *nxt_sys_errlist; +static nxt_uint_t nxt_sys_nerr; nxt_int_t -nxt_strerror_start(void) -{ - char *msg; - u_char *p; - size_t size, length, n; - nxt_uint_t err, invalid; +nxt_strerror_start(void) { + char *msg; + u_char *p; + size_t size, length, n; + nxt_uint_t err, invalid; /* The last entry. */ size = nxt_length("Unknown error"); @@ -49,7 +47,6 @@ nxt_strerror_start(void) */ for (invalid = 0; invalid < 100 && nxt_sys_nerr < 65536; nxt_sys_nerr++) { - nxt_set_errno(0); msg = strerror((int) nxt_sys_nerr); @@ -73,13 +70,12 @@ nxt_strerror_start(void) continue; } - length = nxt_strlen(msg); - size += length; + length = nxt_strlen(msg); + size += length; - if (length == 0 /* HP-UX empty strings. */ + if (length == 0 /* HP-UX empty strings. */ || nxt_errno == NXT_EINVAL - || memcmp(msg, "Unknown error", 13) == 0) - { + || memcmp(msg, "Unknown error", 13) == 0) { invalid++; continue; } @@ -87,8 +83,7 @@ nxt_strerror_start(void) #if (NXT_AIX) if (memcmp(msg, "Error ", 6) == 0 - && memcmp(msg + length - 10, " occurred.", 9) == 0) - { + && memcmp(msg + length - 10, " occurred.", 9) == 0) { invalid++; continue; } @@ -110,17 +105,17 @@ nxt_strerror_start(void) p = nxt_pointer_to(nxt_sys_errlist, n); for (err = 0; err < nxt_sys_nerr; err++) { - msg = strerror((int) err); + msg = strerror((int) err); length = nxt_strlen(msg); nxt_sys_errlist[err].length = length; - nxt_sys_errlist[err].start = p; + nxt_sys_errlist[err].start = p; p = nxt_cpymem(p, msg, length); } nxt_sys_errlist[err].length = 13; - nxt_sys_errlist[err].start = p; + nxt_sys_errlist[err].start = p; nxt_memcpy(p, "Unknown error", 13); nxt_strerror = nxt_runtime_strerror; @@ -128,19 +123,15 @@ nxt_strerror_start(void) return NXT_OK; } - static u_char * -nxt_bootstrap_strerror(nxt_err_t err, u_char *errstr, size_t size) -{ +nxt_bootstrap_strerror(nxt_err_t err, u_char *errstr, size_t size) { return nxt_cpystrn(errstr, (u_char *) strerror(err), size); } - static u_char * -nxt_runtime_strerror(nxt_err_t err, u_char *errstr, size_t size) -{ - nxt_str_t *msg; - nxt_uint_t n; +nxt_runtime_strerror(nxt_err_t err, u_char *errstr, size_t size) { + nxt_str_t *msg; + nxt_uint_t n; n = nxt_min((nxt_uint_t) err, nxt_sys_nerr); diff --git a/src/nxt_errno.h b/src/nxt_errno.h index f19d50bab..73025955a 100644 --- a/src/nxt_errno.h +++ b/src/nxt_errno.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,81 +7,78 @@ #define _NXT_UNIX_ERRNO_H_INCLUDED_ -typedef int nxt_err_t; - - -#define NXT_EPERM EPERM -#define NXT_ENOENT ENOENT -#define NXT_ENOPATH ENOENT -#define NXT_ESRCH ESRCH -#define NXT_EINTR EINTR -#define NXT_ENXIO ENXIO -#define NXT_ECHILD ECHILD -#define NXT_ENOMEM ENOMEM -#define NXT_EACCES EACCES -#define NXT_EBUSY EBUSY -#define NXT_EEXIST EEXIST -#define NXT_ELOOP ELOOP -#define NXT_EXDEV EXDEV -#define NXT_ENOTDIR ENOTDIR -#define NXT_EISDIR EISDIR -#define NXT_EINVAL EINVAL -#define NXT_ENOSPC ENOSPC -#define NXT_EPIPE EPIPE -#define NXT_EINPROGRESS EINPROGRESS -#define NXT_EOPNOTSUPP EOPNOTSUPP -#define NXT_EADDRINUSE EADDRINUSE -#define NXT_ECONNABORTED ECONNABORTED -#define NXT_ECONNRESET ECONNRESET -#define NXT_ENOTCONN ENOTCONN -#define NXT_ETIMEDOUT ETIMEDOUT -#define NXT_ECONNREFUSED ECONNREFUSED -#define NXT_ENAMETOOLONG ENAMETOOLONG -#define NXT_ENETDOWN ENETDOWN -#define NXT_ENETUNREACH ENETUNREACH -#define NXT_EHOSTDOWN EHOSTDOWN -#define NXT_EHOSTUNREACH EHOSTUNREACH -#define NXT_ENOSYS ENOSYS -#define NXT_ECANCELED ECANCELED -#define NXT_EILSEQ EILSEQ -#define NXT_ETIME ETIME -#define NXT_ENOMOREFILES 0 -#define NXT_ENOBUFS ENOBUFS -#define NXT_ERANGE ERANGE +typedef int nxt_err_t; + + +#define NXT_EPERM EPERM +#define NXT_ENOENT ENOENT +#define NXT_ENOPATH ENOENT +#define NXT_ESRCH ESRCH +#define NXT_EINTR EINTR +#define NXT_ENXIO ENXIO +#define NXT_ECHILD ECHILD +#define NXT_ENOMEM ENOMEM +#define NXT_EACCES EACCES +#define NXT_EBUSY EBUSY +#define NXT_EEXIST EEXIST +#define NXT_ELOOP ELOOP +#define NXT_EXDEV EXDEV +#define NXT_ENOTDIR ENOTDIR +#define NXT_EISDIR EISDIR +#define NXT_EINVAL EINVAL +#define NXT_ENOSPC ENOSPC +#define NXT_EPIPE EPIPE +#define NXT_EINPROGRESS EINPROGRESS +#define NXT_EOPNOTSUPP EOPNOTSUPP +#define NXT_EADDRINUSE EADDRINUSE +#define NXT_ECONNABORTED ECONNABORTED +#define NXT_ECONNRESET ECONNRESET +#define NXT_ENOTCONN ENOTCONN +#define NXT_ETIMEDOUT ETIMEDOUT +#define NXT_ECONNREFUSED ECONNREFUSED +#define NXT_ENAMETOOLONG ENAMETOOLONG +#define NXT_ENETDOWN ENETDOWN +#define NXT_ENETUNREACH ENETUNREACH +#define NXT_EHOSTDOWN EHOSTDOWN +#define NXT_EHOSTUNREACH EHOSTUNREACH +#define NXT_ENOSYS ENOSYS +#define NXT_ECANCELED ECANCELED +#define NXT_EILSEQ EILSEQ +#define NXT_ETIME ETIME +#define NXT_ENOMOREFILES 0 +#define NXT_ENOBUFS ENOBUFS +#define NXT_ERANGE ERANGE #if (NXT_HPUX) /* HP-UX uses EWOULDBLOCK instead of EAGAIN. */ -#define NXT_EAGAIN EWOULDBLOCK +#define NXT_EAGAIN EWOULDBLOCK #else -#define NXT_EAGAIN EAGAIN +#define NXT_EAGAIN EAGAIN #endif -#define NXT_OK 0 -#define NXT_ERROR (-1) -#define NXT_AGAIN (-2) -#define NXT_DECLINED (-3) -#define NXT_DONE (-4) +#define NXT_OK 0 +#define NXT_ERROR (-1) +#define NXT_AGAIN (-2) +#define NXT_DECLINED (-3) +#define NXT_DONE (-4) -#define nxt_errno \ - errno +#define nxt_errno errno -#define nxt_socket_errno \ - errno +#define nxt_socket_errno errno -#define nxt_set_errno(err) \ - errno = err +#define nxt_set_errno(err) errno = err -#define nxt_set_socket_errno(err) \ - errno = err +#define nxt_set_socket_errno(err) errno = err -nxt_int_t nxt_strerror_start(void); +nxt_int_t +nxt_strerror_start(void); typedef u_char *(*nxt_strerror_t)(nxt_err_t err, u_char *errstr, size_t size); -extern nxt_strerror_t nxt_strerror; +extern nxt_strerror_t nxt_strerror; #endif /* _NXT_UNIX_ERRNO_H_INCLUDED_ */ diff --git a/src/nxt_event_conn_job_sendfile.c b/src/nxt_event_conn_job_sendfile.c index 0f6f9353a..e80de9289 100644 --- a/src/nxt_event_conn_job_sendfile.c +++ b/src/nxt_event_conn_job_sendfile.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,43 +5,38 @@ #include - typedef struct { - nxt_job_t job; - nxt_buf_t *out; - size_t sent; - size_t limit; - nxt_work_handler_t ready_handler; + nxt_job_t job; + nxt_buf_t *out; + size_t sent; + size_t limit; + nxt_work_handler_t ready_handler; } nxt_job_sendfile_t; - -static void nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, - void *data); -static nxt_buf_t *nxt_event_conn_job_sendfile_completion(nxt_task_t *task, - nxt_conn_t *c, nxt_buf_t *b); - +static void +nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, void *data); +static nxt_buf_t * +nxt_event_conn_job_sendfile_completion(nxt_task_t *task, nxt_conn_t *c, + nxt_buf_t *b); void -nxt_event_conn_job_sendfile(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_event_conn_job_sendfile(nxt_task_t *task, nxt_conn_t *c) { nxt_fd_event_disable(task->thread->engine, &c->socket); /* A work item data is not used in nxt_event_conn_job_sendfile_start(). */ nxt_event_conn_job_sendfile_start(task, c, NULL); } - static void -nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_iobuf_t b; - nxt_job_sendfile_t *jbs; - nxt_sendbuf_coalesce_t sb; +nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_iobuf_t b; + nxt_job_sendfile_t *jbs; + nxt_sendbuf_coalesce_t sb; c = obj; @@ -64,23 +58,21 @@ nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, void *data) jbs->limit = nxt_event_conn_write_limit(c); if (jbs->limit != 0) { - - sb.buf = c->write; + sb.buf = c->write; sb.iobuf = &b; - sb.nmax = 1; - sb.sync = 0; - sb.size = 0; + sb.nmax = 1; + sb.sync = 0; + sb.size = 0; sb.limit = jbs->limit; if (nxt_sendbuf_mem_coalesce(c->socket.task, &sb) != 0 || !sb.sync) { - jbs->job.thread_pool = c->u.thread_pool; - jbs->job.log = c->socket.log; - jbs->out = c->write; - c->write = NULL; - jbs->ready_handler = nxt_event_conn_job_sendfile_return; + jbs->job.log = c->socket.log; + jbs->out = c->write; + c->write = NULL; + jbs->ready_handler = nxt_event_conn_job_sendfile_return; - c->block_read = 1; + c->block_read = 1; c->block_write = 1; nxt_job_start(task, &jbs->job, nxt_event_conn_job_sendfile_handler); @@ -91,23 +83,21 @@ nxt_event_conn_job_sendfile_start(nxt_task_t *task, void *obj, void *data) nxt_event_conn_job_sendfile_return(task, jbs, c); } - static void -nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data) { ssize_t ret; - nxt_buf_t *b; + nxt_buf_t *b; nxt_bool_t first; - nxt_conn_t *c; - nxt_job_sendfile_t *jbs; + nxt_conn_t *c; + nxt_job_sendfile_t *jbs; jbs = obj; - c = data; + c = data; nxt_debug(task, "event conn job sendfile fd:%d", c->socket.fd); first = c->socket.write_ready; - b = jbs->out; + b = jbs->out; do { ret = c->io->old_sendbuf(c, b, jbs->limit); @@ -120,7 +110,7 @@ nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data) goto done; } - jbs->sent += ret; + jbs->sent += ret; jbs->limit -= ret; b = nxt_sendbuf_update(b, ret); @@ -130,7 +120,6 @@ nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data) } if (jbs->limit == 0) { - if (c->rate == NULL) { jbs->limit = c->max_chunk; goto fast; @@ -153,28 +142,26 @@ nxt_event_conn_job_sendfile_handler(nxt_task_t *task, void *obj, void *data) fast: nxt_work_set(&jbs->job.work, nxt_event_conn_job_sendfile_handler, - jbs->job.task, jbs, c); + jbs->job.task, jbs, c); nxt_thread_pool_post(task->thread->thread_pool, &jbs->job.work); } - static void -nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, void *data) -{ +nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, void *data) { size_t sent; - nxt_buf_t *b; + nxt_buf_t *b; nxt_bool_t done; - nxt_conn_t *c; - nxt_job_sendfile_t *jbs; + nxt_conn_t *c; + nxt_job_sendfile_t *jbs; jbs = obj; - c = data; + c = data; - c->block_read = 0; + c->block_read = 0; c->block_write = 0; - sent = jbs->sent; + sent = jbs->sent; c->sent += sent; nxt_debug(task, "event conn sendfile sent:%z", sent); @@ -210,17 +197,16 @@ nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, void *data) } if (c->socket.error == 0 - && !nxt_event_conn_write_delayed(task->thread->engine, c, sent)) - { + && !nxt_event_conn_write_delayed(task->thread->engine, c, sent)) { nxt_conn_timer(task->thread->engine, c, c->write_state, - &c->write_timer); + &c->write_timer); nxt_fd_event_oneshot_write(task->thread->engine, &c->socket); } if (sent != 0) { nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, - task, c, c->socket.data); + task, c, c->socket.data); /* * Fall through if first operations were * successful but the last one failed. @@ -229,17 +215,14 @@ nxt_event_conn_job_sendfile_return(nxt_task_t *task, void *obj, void *data) if (nxt_slow_path(c->socket.error != 0)) { nxt_work_queue_add(c->write_work_queue, c->write_state->error_handler, - task, c, c->socket.data); + task, c, c->socket.data); } } - static nxt_buf_t * nxt_event_conn_job_sendfile_completion(nxt_task_t *task, nxt_conn_t *c, - nxt_buf_t *b) -{ + nxt_buf_t *b) { while (b != NULL) { - nxt_prefetch(b->next); if (nxt_buf_is_mem(b) && b->mem.pos != b->mem.free) { @@ -249,8 +232,8 @@ nxt_event_conn_job_sendfile_completion(nxt_task_t *task, nxt_conn_t *c, break; } - nxt_work_queue_add(c->write_work_queue, - b->completion_handler, task, b, b->parent); + nxt_work_queue_add(c->write_work_queue, b->completion_handler, task, b, + b->parent); b = b->next; } diff --git a/src/nxt_event_engine.c b/src/nxt_event_engine.c index 78c79bb1a..f55e7150e 100644 --- a/src/nxt_event_engine.c +++ b/src/nxt_event_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,45 +6,43 @@ #include -typedef struct nxt_mem_cache_block_s nxt_mem_cache_block_t; +typedef struct nxt_mem_cache_block_s nxt_mem_cache_block_t; struct nxt_mem_cache_block_s { - nxt_mem_cache_block_t *next; + nxt_mem_cache_block_t *next; }; - typedef struct { - nxt_mem_cache_block_t *free; + nxt_mem_cache_block_t *free; uint32_t size; uint32_t count; } nxt_mem_cache_t; - -static nxt_int_t nxt_event_engine_post_init(nxt_event_engine_t *engine); -static nxt_int_t nxt_event_engine_signal_pipe_create( - nxt_event_engine_t *engine); -static void nxt_event_engine_signal_pipe_close(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_engine_post_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_engine_signal_pipe_error(nxt_task_t *task, void *obj, - void *data); -static void nxt_event_engine_signal_handler(nxt_task_t *task, void *obj, - void *data); -static nxt_work_handler_t nxt_event_engine_queue_pop(nxt_event_engine_t *engine, - nxt_task_t **task, void **obj, void **data); - +static nxt_int_t +nxt_event_engine_post_init(nxt_event_engine_t *engine); +static nxt_int_t +nxt_event_engine_signal_pipe_create(nxt_event_engine_t *engine); +static void +nxt_event_engine_signal_pipe_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_engine_post_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_engine_signal_pipe_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_event_engine_signal_handler(nxt_task_t *task, void *obj, void *data); +static nxt_work_handler_t +nxt_event_engine_queue_pop(nxt_event_engine_t *engine, nxt_task_t **task, + void **obj, void **data); nxt_event_engine_t * nxt_event_engine_create(nxt_task_t *task, const nxt_event_interface_t *interface, const nxt_sig_event_t *signals, - nxt_uint_t flags, nxt_uint_t batch) -{ + nxt_uint_t flags, nxt_uint_t batch) { nxt_uint_t events; - nxt_thread_t *thread; - nxt_event_engine_t *engine; + nxt_thread_t *thread; + nxt_event_engine_t *engine; engine = nxt_zalloc(sizeof(nxt_event_engine_t)); if (engine == NULL) { @@ -57,8 +54,8 @@ nxt_event_engine_create(nxt_task_t *task, thread = task->thread; engine->task.thread = thread; - engine->task.log = thread->log; - engine->task.ident = nxt_task_next_ident(); + engine->task.log = thread->log; + engine->task.ident = nxt_task_next_ident(); engine->batch = batch; @@ -75,14 +72,14 @@ nxt_event_engine_create(nxt_task_t *task, nxt_work_queue_cache_create(&engine->work_queue_cache, 0); - engine->fast_work_queue.cache = &engine->work_queue_cache; - engine->accept_work_queue.cache = &engine->work_queue_cache; - engine->read_work_queue.cache = &engine->work_queue_cache; - engine->socket_work_queue.cache = &engine->work_queue_cache; - engine->connect_work_queue.cache = &engine->work_queue_cache; - engine->write_work_queue.cache = &engine->work_queue_cache; + engine->fast_work_queue.cache = &engine->work_queue_cache; + engine->accept_work_queue.cache = &engine->work_queue_cache; + engine->read_work_queue.cache = &engine->work_queue_cache; + engine->socket_work_queue.cache = &engine->work_queue_cache; + engine->connect_work_queue.cache = &engine->work_queue_cache; + engine->write_work_queue.cache = &engine->work_queue_cache; engine->shutdown_work_queue.cache = &engine->work_queue_cache; - engine->close_work_queue.cache = &engine->work_queue_cache; + engine->close_work_queue.cache = &engine->work_queue_cache; nxt_work_queue_name(&engine->fast_work_queue, "fast"); nxt_work_queue_name(&engine->accept_work_queue, "accept"); @@ -163,10 +160,8 @@ nxt_event_engine_create(nxt_task_t *task, return NULL; } - static nxt_int_t -nxt_event_engine_post_init(nxt_event_engine_t *engine) -{ +nxt_event_engine_post_init(nxt_event_engine_t *engine) { if (engine->event.enable_post != NULL) { return engine->event.enable_post(engine, nxt_event_engine_post_handler); } @@ -178,11 +173,9 @@ nxt_event_engine_post_init(nxt_event_engine_t *engine) return NXT_OK; } - static nxt_int_t -nxt_event_engine_signal_pipe_create(nxt_event_engine_t *engine) -{ - nxt_event_engine_pipe_t *pipe; +nxt_event_engine_signal_pipe_create(nxt_event_engine_t *engine) { + nxt_event_engine_pipe_t *pipe; pipe = nxt_zalloc(sizeof(nxt_event_engine_pipe_t)); if (pipe == NULL) { @@ -201,29 +194,26 @@ nxt_event_engine_signal_pipe_create(nxt_event_engine_t *engine) return NXT_ERROR; } - pipe->event.fd = pipe->fds[0]; - pipe->event.task = &engine->task; - pipe->event.read_work_queue = &engine->fast_work_queue; - pipe->event.read_handler = nxt_event_engine_signal_pipe; + pipe->event.fd = pipe->fds[0]; + pipe->event.task = &engine->task; + pipe->event.read_work_queue = &engine->fast_work_queue; + pipe->event.read_handler = nxt_event_engine_signal_pipe; pipe->event.write_work_queue = &engine->fast_work_queue; - pipe->event.error_handler = nxt_event_engine_signal_pipe_error; - pipe->event.log = engine->task.log; + pipe->event.error_handler = nxt_event_engine_signal_pipe_error; + pipe->event.log = engine->task.log; nxt_fd_event_enable_read(engine, &pipe->event); return NXT_OK; } - static void -nxt_event_engine_signal_pipe_free(nxt_event_engine_t *engine) -{ - nxt_event_engine_pipe_t *pipe; +nxt_event_engine_signal_pipe_free(nxt_event_engine_t *engine) { + nxt_event_engine_pipe_t *pipe; pipe = engine->pipe; if (pipe != NULL) { - if (pipe->event.read_work_queue != NULL) { nxt_fd_event_close(engine, &pipe->event); nxt_pipe_close(pipe->event.task, pipe->fds); @@ -233,11 +223,9 @@ nxt_event_engine_signal_pipe_free(nxt_event_engine_t *engine) } } - static void -nxt_event_engine_signal_pipe_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_event_engine_pipe_t *pipe; +nxt_event_engine_signal_pipe_close(nxt_task_t *task, void *obj, void *data) { + nxt_event_engine_pipe_t *pipe; pipe = obj; @@ -245,10 +233,8 @@ nxt_event_engine_signal_pipe_close(nxt_task_t *task, void *obj, void *data) nxt_free(pipe); } - void -nxt_event_engine_post(nxt_event_engine_t *engine, nxt_work_t *work) -{ +nxt_event_engine_post(nxt_event_engine_t *engine, nxt_work_t *work) { nxt_debug(&engine->task, "event engine post"); #if (NXT_DEBUG) @@ -262,11 +248,9 @@ nxt_event_engine_post(nxt_event_engine_t *engine, nxt_work_t *work) nxt_event_engine_signal(engine, 0); } - void -nxt_event_engine_signal(nxt_event_engine_t *engine, nxt_uint_t signo) -{ - u_char buf; +nxt_event_engine_signal(nxt_event_engine_t *engine, nxt_uint_t signo) { + u_char buf; nxt_debug(&engine->task, "event engine signal:%ui", signo); @@ -284,14 +268,12 @@ nxt_event_engine_signal(nxt_event_engine_t *engine, nxt_uint_t signo) (void) nxt_fd_write(engine->pipe->fds[1], &buf, 1); } - static void -nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, void *data) -{ +nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, void *data) { int i, n; u_char signo; nxt_bool_t post; - nxt_fd_event_t *ev; + nxt_fd_event_t *ev; u_char buf[128]; ev = obj; @@ -314,7 +296,7 @@ nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, void *data) } else { nxt_event_engine_signal_handler(task, - (void *) (uintptr_t) signo, NULL); + (void *) (uintptr_t) signo, NULL); } } @@ -325,50 +307,42 @@ nxt_event_engine_signal_pipe(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_event_engine_post_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_thread_t *thread; - nxt_event_engine_t *engine; +nxt_event_engine_post_handler(nxt_task_t *task, void *obj, void *data) { + nxt_thread_t *thread; + nxt_event_engine_t *engine; thread = task->thread; engine = thread->engine; nxt_locked_work_queue_move(thread, &engine->locked_work_queue, - &engine->fast_work_queue); + &engine->fast_work_queue); } - static void -nxt_event_engine_signal_pipe_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_event_engine_t *engine; - nxt_event_engine_pipe_t *pipe; +nxt_event_engine_signal_pipe_error(nxt_task_t *task, void *obj, void *data) { + nxt_event_engine_t *engine; + nxt_event_engine_pipe_t *pipe; engine = task->thread->engine; - pipe = engine->pipe; + pipe = engine->pipe; - nxt_alert(task, "engine pipe(%FD:%FD) event error", - pipe->fds[0], pipe->fds[1]); + nxt_alert(task, "engine pipe(%FD:%FD) event error", pipe->fds[0], + pipe->fds[1]); nxt_fd_event_close(engine, &pipe->event); nxt_pipe_close(pipe->event.task, pipe->fds); } - static void -nxt_event_engine_signal_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_event_engine_signal_handler(nxt_task_t *task, void *obj, void *data) { uintptr_t signo; - const nxt_sig_event_t *sigev; + const nxt_sig_event_t *sigev; signo = (uintptr_t) obj; - for (sigev = task->thread->engine->signals->sigev; - sigev->signo != 0; - sigev++) - { + for (sigev = task->thread->engine->signals->sigev; sigev->signo != 0; + sigev++) { if (signo == (nxt_uint_t) sigev->signo) { sigev->handler(task, (void *) signo, (void *) sigev->name); return; @@ -378,12 +352,10 @@ nxt_event_engine_signal_handler(nxt_task_t *task, void *obj, void *data) nxt_alert(task, "signal %ui handler not found", (nxt_uint_t) signo); } - nxt_int_t nxt_event_engine_change(nxt_event_engine_t *engine, - const nxt_event_interface_t *interface, nxt_uint_t batch) -{ - nxt_uint_t events; + const nxt_event_interface_t *interface, nxt_uint_t batch) { + nxt_uint_t events; engine->batch = batch; @@ -407,8 +379,8 @@ nxt_event_engine_change(nxt_event_engine_t *engine, * added above to engine fast work queue will be processed. */ nxt_work_queue_add(&engine->fast_work_queue, - nxt_event_engine_signal_pipe_close, - &engine->task, engine->pipe, NULL); + nxt_event_engine_signal_pipe_close, &engine->task, engine->pipe, + NULL); engine->pipe = NULL; } @@ -428,7 +400,6 @@ nxt_event_engine_change(nxt_event_engine_t *engine, } if (engine->signals != NULL) { - if (!engine->event.signal_support) { return nxt_event_engine_signals_start(engine); } @@ -443,10 +414,8 @@ nxt_event_engine_change(nxt_event_engine_t *engine, return NXT_OK; } - void -nxt_event_engine_free(nxt_event_engine_t *engine) -{ +nxt_event_engine_free(nxt_event_engine_t *engine) { nxt_thread_log_debug("free engine %p", engine); nxt_event_engine_signal_pipe_free(engine); @@ -461,27 +430,24 @@ nxt_event_engine_free(nxt_event_engine_t *engine) nxt_free(engine); } - static nxt_work_handler_t nxt_event_engine_queue_pop(nxt_event_engine_t *engine, nxt_task_t **task, - void **obj, void **data) -{ - nxt_work_queue_t *wq, *last; + void **obj, void **data) { + nxt_work_queue_t *wq, *last; - wq = engine->current_work_queue; + wq = engine->current_work_queue; last = wq; if (wq->head == NULL) { wq = &engine->fast_work_queue; if (wq->head == NULL) { - do { engine->current_work_queue++; wq = engine->current_work_queue; if (wq > &engine->close_work_queue) { - wq = &engine->fast_work_queue; + wq = &engine->fast_work_queue; engine->current_work_queue = wq; } @@ -504,15 +470,13 @@ nxt_event_engine_queue_pop(nxt_event_engine_t *engine, nxt_task_t **task, return nxt_work_queue_pop(wq, task, obj, data); } - void -nxt_event_engine_start(nxt_event_engine_t *engine) -{ - void *obj, *data; - nxt_task_t *task; - nxt_msec_t timeout, now; - nxt_thread_t *thr; - nxt_work_handler_t handler; +nxt_event_engine_start(nxt_event_engine_t *engine) { + void *obj, *data; + nxt_task_t *task; + nxt_msec_t timeout, now; + nxt_thread_t *thr; + nxt_work_handler_t handler; thr = nxt_thread(); @@ -528,9 +492,8 @@ nxt_event_engine_start(nxt_event_engine_t *engine) thr->log = engine->task.log; - for ( ;; ) { - - for ( ;; ) { + for (;;) { + for (;;) { handler = nxt_event_engine_queue_pop(engine, &task, &obj, &data); if (handler == NULL) { @@ -554,22 +517,19 @@ nxt_event_engine_start(nxt_event_engine_t *engine) } } - void * nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, - size_t size) -{ + size_t size) { uint32_t n; nxt_uint_t items; - nxt_array_t *mem_cache; - nxt_mem_cache_t *cache; - nxt_mem_cache_block_t *block; + nxt_array_t *mem_cache; + nxt_mem_cache_t *cache; + nxt_mem_cache_block_t *block; mem_cache = engine->mem_cache; - n = *hint; + n = *hint; if (n == NXT_EVENT_ENGINE_NO_MEM_HINT) { - if (mem_cache == NULL) { /* IPv4 nxt_sockaddr_t and HTTP/1 and HTTP/2 buffers. */ items = 3; @@ -581,7 +541,7 @@ nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, #endif mem_cache = nxt_array_create(engine->mem_pool, items, - sizeof(nxt_mem_cache_t)); + sizeof(nxt_mem_cache_t)); if (nxt_slow_path(mem_cache == NULL)) { return mem_cache; } @@ -601,8 +561,8 @@ nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, return cache; } - cache->free = NULL; - cache->size = size; + cache->free = NULL; + cache->size = size; cache->count = 0; found: @@ -626,24 +586,21 @@ nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, return nxt_mp_alloc(engine->mem_pool, size); } - void nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, void *p, - size_t size) -{ + size_t size) { uint32_t n; - nxt_array_t *mem_cache; - nxt_mem_cache_t *cache; - nxt_mem_cache_block_t *block; + nxt_array_t *mem_cache; + nxt_mem_cache_t *cache; + nxt_mem_cache_block_t *block; - block = p; + block = p; mem_cache = engine->mem_cache; - cache = mem_cache->elts; + cache = mem_cache->elts; n = hint; if (nxt_slow_path(n == NXT_EVENT_ENGINE_NO_MEM_HINT)) { - if (size != 0) { for (n = 0; n < mem_cache->nelts; n++) { if (cache[n].size == size) { @@ -651,8 +608,8 @@ nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, void *p, } } - nxt_alert(&engine->task, - "event engine mem free(%p, %z) not found", p, size); + nxt_alert(&engine->task, "event engine mem free(%p, %z) not found", + p, size); } goto done; @@ -675,11 +632,9 @@ nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, void *p, nxt_mp_free(engine->mem_pool, p); } - void * -nxt_event_engine_buf_mem_alloc(nxt_event_engine_t *engine, size_t size) -{ - nxt_buf_t *b; +nxt_event_engine_buf_mem_alloc(nxt_event_engine_t *engine, size_t size) { + nxt_buf_t *b; uint8_t hint; hint = NXT_EVENT_ENGINE_NO_MEM_HINT; @@ -691,37 +646,33 @@ nxt_event_engine_buf_mem_alloc(nxt_event_engine_t *engine, size_t size) nxt_memzero(b, NXT_BUF_MEM_SIZE); - b->cache_hint = hint; - b->data = engine; + b->cache_hint = hint; + b->data = engine; b->completion_handler = nxt_event_engine_buf_mem_completion; if (size != 0) { b->mem.start = nxt_pointer_to(b, NXT_BUF_MEM_SIZE); - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start; - b->mem.end = b->mem.start + size; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start; + b->mem.end = b->mem.start + size; } return b; } - void -nxt_event_engine_buf_mem_free(nxt_event_engine_t *engine, nxt_buf_t *b) -{ - size_t size; +nxt_event_engine_buf_mem_free(nxt_event_engine_t *engine, nxt_buf_t *b) { + size_t size; size = NXT_BUF_MEM_SIZE + nxt_buf_mem_size(&b->mem); nxt_event_engine_mem_free(engine, b->cache_hint, b, size); } - void -nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b, *next, *parent; - nxt_event_engine_t *engine; +nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b, *next, *parent; + nxt_event_engine_t *engine; b = obj; @@ -730,7 +681,7 @@ nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, void *data) engine = b->data; do { - next = b->next; + next = b->next; parent = b->parent; nxt_event_engine_buf_mem_free(engine, b); @@ -744,8 +695,8 @@ nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, void *data) #if (NXT_DEBUG) -void nxt_event_engine_thread_adopt(nxt_event_engine_t *engine) -{ +void +nxt_event_engine_thread_adopt(nxt_event_engine_t *engine) { nxt_work_queue_thread_adopt(&engine->fast_work_queue); nxt_work_queue_thread_adopt(&engine->accept_work_queue); nxt_work_queue_thread_adopt(&engine->read_work_queue); diff --git a/src/nxt_event_engine.h b/src/nxt_event_engine.h index 291ea749b..a9ae9fe4b 100644 --- a/src/nxt_event_engine.h +++ b/src/nxt_event_engine.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -13,17 +12,15 @@ * signals, etc. */ -#define NXT_FILE_EVENTS 1 -#define NXT_NO_FILE_EVENTS 0 - -#define NXT_SIGNAL_EVENTS 1 -#define NXT_NO_SIGNAL_EVENTS 0 +#define NXT_FILE_EVENTS 1 +#define NXT_NO_FILE_EVENTS 0 +#define NXT_SIGNAL_EVENTS 1 +#define NXT_NO_SIGNAL_EVENTS 0 typedef struct { - /* The canonical event set name. */ - const char *name; + const char *name; /* * Create an event set. The mchanges argument is a maximum number of @@ -31,30 +28,27 @@ typedef struct { * number of events to retrieve from the kernel at once, if underlying * event facility supports batch operations. */ - nxt_int_t (*create)(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); + nxt_int_t (*create)(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); /* Close and free an event set. */ - void (*free)(nxt_event_engine_t *engine); + void (*free)(nxt_event_engine_t *engine); /* * Add a file descriptor to an event set and enable the most * effective read and write event notification method provided * by underlying event facility. */ - void (*enable)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*enable)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* Disable file descriptor event notifications. */ - void (*disable)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*disable)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Delete a file descriptor from an event set. A possible usage * is a moving of the file descriptor from one event set to another. */ - void (*delete)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*delete)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Delete a file descriptor from an event set before closing the @@ -64,79 +58,67 @@ typedef struct { * Some facilities such as Solaris /dev/poll require to delete a file * descriptor explicitly. */ - nxt_bool_t (*close)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + nxt_bool_t (*close)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a file descriptor to an event set and enable the most effective * read event notification method provided by underlying event facility. */ - void (*enable_read)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*enable_read)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a file descriptor to an event set and enable the most effective * write event notification method provided by underlying event facility. */ - void (*enable_write)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*enable_write)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* Disable file descriptor read event notifications. */ - void (*disable_read)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*disable_read)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* Disable file descriptor write event notifications. */ - void (*disable_write)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*disable_write)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* Block file descriptor read event notifications. */ - void (*block_read)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*block_read)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* Block file descriptor write event notifications. */ - void (*block_write)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*block_write)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a file descriptor to an event set and enable an oneshot * read event notification method. */ - void (*oneshot_read)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*oneshot_read)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a file descriptor to an event set and enable an oneshot * write event notification method. */ - void (*oneshot_write)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*oneshot_write)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a listening socket descriptor to an event set and enable * a level-triggered read event notification method. */ - void (*enable_accept)(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); + void (*enable_accept)(nxt_event_engine_t *engine, nxt_fd_event_t *ev); /* * Add a file to an event set and enable a file change notification * events. */ - void (*enable_file)(nxt_event_engine_t *engine, - nxt_file_event_t *ev); + void (*enable_file)(nxt_event_engine_t *engine, nxt_file_event_t *ev); /* * Delete a file from an event set before closing the file descriptor. */ - void (*close_file)(nxt_event_engine_t *engine, - nxt_file_event_t *ev); + void (*close_file)(nxt_event_engine_t *engine, nxt_file_event_t *ev); /* * Enable post event notifications and set a post handler to handle * the zero signal. */ - nxt_int_t (*enable_post)(nxt_event_engine_t *engine, - nxt_work_handler_t handler); + nxt_int_t ( + *enable_post)(nxt_event_engine_t *engine, nxt_work_handler_t handler); /* * Signal an event set. If a signal number is non-zero then @@ -149,40 +131,38 @@ typedef struct { * a way to wake up the event set to process works posted to * the event engine locked work queue. */ - void (*signal)(nxt_event_engine_t *engine, - nxt_uint_t signo); + void (*signal)(nxt_event_engine_t *engine, nxt_uint_t signo); /* Poll an event set for new event notifications. */ - void (*poll)(nxt_event_engine_t *engine, - nxt_msec_t timeout); + void (*poll)(nxt_event_engine_t *engine, nxt_msec_t timeout); /* I/O operations suitable to underlying event facility. */ - nxt_conn_io_t *io; + nxt_conn_io_t *io; /* True if an event facility supports file change event notifications. */ - uint8_t file_support; /* 1 bit */ + uint8_t file_support; /* 1 bit */ /* True if an event facility supports signal event notifications. */ - uint8_t signal_support; /* 1 bit */ + uint8_t signal_support; /* 1 bit */ } nxt_event_interface_t; #if (NXT_HAVE_KQUEUE) typedef struct { - int fd; - int nchanges; - int mchanges; - int mevents; - nxt_pid_t pid; + int fd; + int nchanges; + int mchanges; + int mevents; + nxt_pid_t pid; - nxt_work_handler_t post_handler; + nxt_work_handler_t post_handler; - struct kevent *changes; - struct kevent *events; + struct kevent *changes; + struct kevent *events; } nxt_kqueue_engine_t; -extern const nxt_event_interface_t nxt_kqueue_engine; +extern const nxt_event_interface_t nxt_kqueue_engine; #endif @@ -190,37 +170,35 @@ extern const nxt_event_interface_t nxt_kqueue_engine; #if (NXT_HAVE_EPOLL) typedef struct { - int op; - struct epoll_event event; + int op; + struct epoll_event event; } nxt_epoll_change_t; - typedef struct { - int fd; - uint32_t mode; - nxt_uint_t nchanges; - nxt_uint_t mchanges; - int mevents; + int fd; + uint32_t mode; + nxt_uint_t nchanges; + nxt_uint_t mchanges; + int mevents; - uint8_t error; /* 1 bit */ + uint8_t error; /* 1 bit */ - nxt_epoll_change_t *changes; - struct epoll_event *events; + nxt_epoll_change_t *changes; + struct epoll_event *events; #if (NXT_HAVE_EVENTFD) - nxt_work_handler_t post_handler; - nxt_fd_event_t eventfd; - uint32_t neventfd; + nxt_work_handler_t post_handler; + nxt_fd_event_t eventfd; + uint32_t neventfd; #endif #if (NXT_HAVE_SIGNALFD) - nxt_fd_event_t signalfd; + nxt_fd_event_t signalfd; #endif } nxt_epoll_engine_t; - -extern const nxt_event_interface_t nxt_epoll_edge_engine; -extern const nxt_event_interface_t nxt_epoll_level_engine; +extern const nxt_event_interface_t nxt_epoll_edge_engine; +extern const nxt_event_interface_t nxt_epoll_level_engine; #endif @@ -228,25 +206,24 @@ extern const nxt_event_interface_t nxt_epoll_level_engine; #if (NXT_HAVE_EVENTPORT) typedef struct { - int events; - nxt_fd_event_t *event; + int events; + nxt_fd_event_t *event; } nxt_eventport_change_t; - typedef struct { - int fd; - nxt_uint_t nchanges; - nxt_uint_t mchanges; - u_int mevents; + int fd; + nxt_uint_t nchanges; + nxt_uint_t mchanges; + u_int mevents; - nxt_eventport_change_t *changes; - port_event_t *events; + nxt_eventport_change_t *changes; + port_event_t *events; - nxt_work_handler_t post_handler; - nxt_work_handler_t signal_handler; + nxt_work_handler_t post_handler; + nxt_work_handler_t signal_handler; } nxt_eventport_engine_t; -extern const nxt_event_interface_t nxt_eventport_engine; +extern const nxt_event_interface_t nxt_eventport_engine; #endif @@ -254,25 +231,24 @@ extern const nxt_event_interface_t nxt_eventport_engine; #if (NXT_HAVE_DEVPOLL) typedef struct { - uint8_t op; - short events; - nxt_fd_event_t *event; + uint8_t op; + short events; + nxt_fd_event_t *event; } nxt_devpoll_change_t; - typedef struct { - int fd; - int nchanges; - int mchanges; - int mevents; - - nxt_devpoll_change_t *changes; - struct pollfd *write_changes; - struct pollfd *events; - nxt_lvlhsh_t fd_hash; + int fd; + int nchanges; + int mchanges; + int mevents; + + nxt_devpoll_change_t *changes; + struct pollfd *write_changes; + struct pollfd *events; + nxt_lvlhsh_t fd_hash; } nxt_devpoll_engine_t; -extern const nxt_event_interface_t nxt_devpoll_engine; +extern const nxt_event_interface_t nxt_devpoll_engine; #endif @@ -280,249 +256,248 @@ extern const nxt_event_interface_t nxt_devpoll_engine; #if (NXT_HAVE_POLLSET) typedef struct { - uint8_t op; - uint8_t cmd; - short events; - nxt_fd_event_t *event; + uint8_t op; + uint8_t cmd; + short events; + nxt_fd_event_t *event; } nxt_pollset_change_t; - typedef struct { - pollset_t ps; - int nchanges; - int mchanges; - int mevents; - - nxt_pollset_change_t *changes; - struct poll_ctl *write_changes; - struct pollfd *events; - nxt_lvlhsh_t fd_hash; + pollset_t ps; + int nchanges; + int mchanges; + int mevents; + + nxt_pollset_change_t *changes; + struct poll_ctl *write_changes; + struct pollfd *events; + nxt_lvlhsh_t fd_hash; } nxt_pollset_engine_t; -extern const nxt_event_interface_t nxt_pollset_engine; +extern const nxt_event_interface_t nxt_pollset_engine; #endif typedef struct { - uint8_t op; - short events; - nxt_fd_event_t *event; + uint8_t op; + short events; + nxt_fd_event_t *event; } nxt_poll_change_t; - typedef struct { - nxt_uint_t max_nfds; - nxt_uint_t nfds; + nxt_uint_t max_nfds; + nxt_uint_t nfds; - nxt_uint_t nchanges; - nxt_uint_t mchanges; + nxt_uint_t nchanges; + nxt_uint_t mchanges; - nxt_poll_change_t *changes; - struct pollfd *set; + nxt_poll_change_t *changes; + struct pollfd *set; - nxt_lvlhsh_t fd_hash; + nxt_lvlhsh_t fd_hash; } nxt_poll_engine_t; -extern const nxt_event_interface_t nxt_poll_engine; - +extern const nxt_event_interface_t nxt_poll_engine; typedef struct { - int nfds; - uint32_t update_nfds; /* 1 bit */ + int nfds; + uint32_t update_nfds; /* 1 bit */ - nxt_fd_event_t **events; + nxt_fd_event_t **events; - fd_set main_read_fd_set; - fd_set main_write_fd_set; - fd_set work_read_fd_set; - fd_set work_write_fd_set; + fd_set main_read_fd_set; + fd_set main_write_fd_set; + fd_set work_read_fd_set; + fd_set work_write_fd_set; } nxt_select_engine_t; -extern const nxt_event_interface_t nxt_select_engine; +extern const nxt_event_interface_t nxt_select_engine; -nxt_int_t nxt_fd_event_hash_add(nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, - nxt_fd_event_t *ev); -void *nxt_fd_event_hash_get(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, - nxt_fd_t fd); -void nxt_fd_event_hash_delete(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, - nxt_fd_t fd, nxt_bool_t ignore); -void nxt_fd_event_hash_destroy(nxt_lvlhsh_t *lvlhsh); +nxt_int_t +nxt_fd_event_hash_add(nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, nxt_fd_event_t *ev); +void * +nxt_fd_event_hash_get(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd); +void +nxt_fd_event_hash_delete(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, + nxt_bool_t ignore); +void +nxt_fd_event_hash_destroy(nxt_lvlhsh_t *lvlhsh); -#define nxt_fd_event_disable(engine, ev) \ - (engine)->event.disable(engine, ev) +#define nxt_fd_event_disable(engine, ev) (engine)->event.disable(engine, ev) -#define nxt_fd_event_delete(engine, ev) \ - (engine)->event.delete(engine, ev) +#define nxt_fd_event_delete(engine, ev) (engine)->event.delete (engine, ev) -#define nxt_fd_event_close(engine, ev) \ - (engine)->event.close(engine, ev) +#define nxt_fd_event_close(engine, ev) (engine)->event.close(engine, ev) -#define nxt_fd_event_enable_read(engine, ev) \ +#define nxt_fd_event_enable_read(engine, ev) \ (engine)->event.enable_read(engine, ev) -#define nxt_fd_event_enable_write(engine, ev) \ +#define nxt_fd_event_enable_write(engine, ev) \ (engine)->event.enable_write(engine, ev) -#define nxt_fd_event_disable_read(engine, ev) \ +#define nxt_fd_event_disable_read(engine, ev) \ (engine)->event.disable_read(engine, ev) -#define nxt_fd_event_disable_write(engine, ev) \ +#define nxt_fd_event_disable_write(engine, ev) \ (engine)->event.disable_write(engine, ev) -#define nxt_fd_event_block_read(engine, ev) \ - do { \ - if (nxt_fd_event_is_active((ev)->read)) { \ - (engine)->event.block_read(engine, ev); \ - } \ +#define nxt_fd_event_block_read(engine, ev) \ + do { \ + if (nxt_fd_event_is_active((ev)->read)) { \ + (engine)->event.block_read(engine, ev); \ + } \ } while (0) -#define nxt_fd_event_block_write(engine, ev) \ - do { \ - if (nxt_fd_event_is_active((ev)->write)) { \ - (engine)->event.block_write(engine, ev); \ - } \ +#define nxt_fd_event_block_write(engine, ev) \ + do { \ + if (nxt_fd_event_is_active((ev)->write)) { \ + (engine)->event.block_write(engine, ev); \ + } \ } while (0) -#define nxt_fd_event_oneshot_read(engine, ev) \ +#define nxt_fd_event_oneshot_read(engine, ev) \ (engine)->event.oneshot_read(engine, ev) -#define nxt_fd_event_oneshot_write(engine, ev) \ +#define nxt_fd_event_oneshot_write(engine, ev) \ (engine)->event.oneshot_write(engine, ev) -#define nxt_fd_event_enable_accept(engine, ev) \ +#define nxt_fd_event_enable_accept(engine, ev) \ (engine)->event.enable_accept(engine, ev) -#define NXT_ENGINE_FIBERS 1 - +#define NXT_ENGINE_FIBERS 1 typedef struct { - nxt_fd_t fds[2]; - nxt_fd_event_t event; + nxt_fd_t fds[2]; + nxt_fd_event_t event; } nxt_event_engine_pipe_t; - struct nxt_event_engine_s { - nxt_task_t task; + nxt_task_t task; union { - nxt_poll_engine_t poll; - nxt_select_engine_t select; + nxt_poll_engine_t poll; + nxt_select_engine_t select; #if (NXT_HAVE_KQUEUE) - nxt_kqueue_engine_t kqueue; + nxt_kqueue_engine_t kqueue; #endif #if (NXT_HAVE_EPOLL) - nxt_epoll_engine_t epoll; + nxt_epoll_engine_t epoll; #endif #if (NXT_HAVE_EVENTPORT) nxt_eventport_engine_t eventport; #endif #if (NXT_HAVE_DEVPOLL) - nxt_devpoll_engine_t devpoll; + nxt_devpoll_engine_t devpoll; #endif #if (NXT_HAVE_POLLSET) - nxt_pollset_engine_t pollset; + nxt_pollset_engine_t pollset; #endif } u; - nxt_timers_t timers; + nxt_timers_t timers; - nxt_work_queue_cache_t work_queue_cache; - nxt_work_queue_t *current_work_queue; - nxt_work_queue_t fast_work_queue; - nxt_work_queue_t accept_work_queue; - nxt_work_queue_t read_work_queue; - nxt_work_queue_t socket_work_queue; - nxt_work_queue_t connect_work_queue; - nxt_work_queue_t write_work_queue; - nxt_work_queue_t shutdown_work_queue; - nxt_work_queue_t close_work_queue; + nxt_work_queue_cache_t work_queue_cache; + nxt_work_queue_t *current_work_queue; + nxt_work_queue_t fast_work_queue; + nxt_work_queue_t accept_work_queue; + nxt_work_queue_t read_work_queue; + nxt_work_queue_t socket_work_queue; + nxt_work_queue_t connect_work_queue; + nxt_work_queue_t write_work_queue; + nxt_work_queue_t shutdown_work_queue; + nxt_work_queue_t close_work_queue; - nxt_locked_work_queue_t locked_work_queue; + nxt_locked_work_queue_t locked_work_queue; - nxt_event_interface_t event; + nxt_event_interface_t event; /* * A pipe to pass event signals to the engine, if the engine's * underlying event facility does not support user events. */ - nxt_event_engine_pipe_t *pipe; + nxt_event_engine_pipe_t *pipe; - nxt_event_signals_t *signals; + nxt_event_signals_t *signals; - nxt_fiber_main_t *fibers; + nxt_fiber_main_t *fibers; /* The engine ID, the main engine has ID 0. */ - uint32_t id; + uint32_t id; - uint8_t shutdown; /* 1 bit */ + uint8_t shutdown; /* 1 bit */ - uint32_t batch; - uint32_t connections; - uint32_t max_connections; + uint32_t batch; + uint32_t connections; + uint32_t max_connections; - nxt_port_t *port; - nxt_mp_t *mem_pool; - nxt_queue_t joints; - nxt_queue_t listen_connections; - nxt_queue_t idle_connections; - nxt_array_t *mem_cache; + nxt_port_t *port; + nxt_mp_t *mem_pool; + nxt_queue_t joints; + nxt_queue_t listen_connections; + nxt_queue_t idle_connections; + nxt_array_t *mem_cache; - nxt_atomic_uint_t accepted_conns_cnt; - nxt_atomic_uint_t idle_conns_cnt; - nxt_atomic_uint_t closed_conns_cnt; - nxt_atomic_uint_t requests_cnt; + nxt_atomic_uint_t accepted_conns_cnt; + nxt_atomic_uint_t idle_conns_cnt; + nxt_atomic_uint_t closed_conns_cnt; + nxt_atomic_uint_t requests_cnt; - nxt_queue_link_t link; + nxt_queue_link_t link; // STUB: router link - nxt_queue_link_t link0; + nxt_queue_link_t link0; }; - -NXT_EXPORT nxt_event_engine_t *nxt_event_engine_create(nxt_task_t *task, +NXT_EXPORT nxt_event_engine_t * +nxt_event_engine_create(nxt_task_t *task, const nxt_event_interface_t *interface, const nxt_sig_event_t *signals, nxt_uint_t flags, nxt_uint_t batch); -NXT_EXPORT nxt_int_t nxt_event_engine_change(nxt_event_engine_t *engine, +NXT_EXPORT nxt_int_t +nxt_event_engine_change(nxt_event_engine_t *engine, const nxt_event_interface_t *interface, nxt_uint_t batch); -NXT_EXPORT void nxt_event_engine_free(nxt_event_engine_t *engine); -NXT_EXPORT void nxt_event_engine_start(nxt_event_engine_t *engine); +NXT_EXPORT void +nxt_event_engine_free(nxt_event_engine_t *engine); +NXT_EXPORT void +nxt_event_engine_start(nxt_event_engine_t *engine); -NXT_EXPORT void nxt_event_engine_post(nxt_event_engine_t *engine, - nxt_work_t *work); -NXT_EXPORT void nxt_event_engine_signal(nxt_event_engine_t *engine, - nxt_uint_t signo); +NXT_EXPORT void +nxt_event_engine_post(nxt_event_engine_t *engine, nxt_work_t *work); +NXT_EXPORT void +nxt_event_engine_signal(nxt_event_engine_t *engine, nxt_uint_t signo); -#define NXT_EVENT_ENGINE_NO_MEM_HINT 255 +#define NXT_EVENT_ENGINE_NO_MEM_HINT 255 -void *nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, +void * +nxt_event_engine_mem_alloc(nxt_event_engine_t *engine, uint8_t *hint, size_t size); -void nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, - void *p, size_t size); -void *nxt_event_engine_buf_mem_alloc(nxt_event_engine_t *engine, size_t size); -void nxt_event_engine_buf_mem_free(nxt_event_engine_t *engine, nxt_buf_t *b); -void nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, - void *data); - +void +nxt_event_engine_mem_free(nxt_event_engine_t *engine, uint8_t hint, void *p, + size_t size); +void * +nxt_event_engine_buf_mem_alloc(nxt_event_engine_t *engine, size_t size); +void +nxt_event_engine_buf_mem_free(nxt_event_engine_t *engine, nxt_buf_t *b); +void +nxt_event_engine_buf_mem_completion(nxt_task_t *task, void *obj, void *data); nxt_inline nxt_event_engine_t * -nxt_thread_event_engine(void) -{ - nxt_thread_t *thr; +nxt_thread_event_engine(void) { + nxt_thread_t *thr; thr = nxt_thread(); return thr->engine; @@ -530,7 +505,8 @@ nxt_thread_event_engine(void) #if (NXT_DEBUG) -NXT_EXPORT void nxt_event_engine_thread_adopt(nxt_event_engine_t *engine); +NXT_EXPORT void +nxt_event_engine_thread_adopt(nxt_event_engine_t *engine); #else diff --git a/src/nxt_eventport_engine.c b/src/nxt_eventport_engine.c index 7ac7f44de..1a15303fb 100644 --- a/src/nxt_eventport_engine.c +++ b/src/nxt_eventport_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -14,48 +13,54 @@ */ -static nxt_int_t nxt_eventport_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_eventport_free(nxt_event_engine_t *engine); -static void nxt_eventport_enable(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_disable(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static nxt_bool_t nxt_eventport_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_enable_event(nxt_event_engine_t *engine, - nxt_fd_event_t *ev, nxt_uint_t events); -static void nxt_eventport_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_disable_event(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static nxt_int_t nxt_eventport_commit_changes(nxt_event_engine_t *engine); -static void nxt_eventport_error_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_eventport_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_eventport_enable_accept(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static nxt_int_t nxt_eventport_enable_post(nxt_event_engine_t *engine, - nxt_work_handler_t handler); -static void nxt_eventport_signal(nxt_event_engine_t *engine, nxt_uint_t signo); -static void nxt_eventport_poll(nxt_event_engine_t *engine, - nxt_msec_t timeout); - - -const nxt_event_interface_t nxt_eventport_engine = { +static nxt_int_t +nxt_eventport_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_eventport_free(nxt_event_engine_t *engine); +static void +nxt_eventport_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_eventport_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_enable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev, + nxt_uint_t events); +static void +nxt_eventport_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_disable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_int_t +nxt_eventport_commit_changes(nxt_event_engine_t *engine); +static void +nxt_eventport_error_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_eventport_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_eventport_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_int_t +nxt_eventport_enable_post(nxt_event_engine_t *engine, + nxt_work_handler_t handler); +static void +nxt_eventport_signal(nxt_event_engine_t *engine, nxt_uint_t signo); +static void +nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); + + +const nxt_event_interface_t nxt_eventport_engine = { "eventport", nxt_eventport_create, nxt_eventport_free, @@ -84,16 +89,14 @@ const nxt_event_interface_t nxt_eventport_engine = { NXT_NO_SIGNAL_EVENTS, }; - static nxt_int_t nxt_eventport_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - nxt_eventport_change_t *changes; + nxt_uint_t mevents) { + nxt_eventport_change_t *changes; - engine->u.eventport.fd = -1; + engine->u.eventport.fd = -1; engine->u.eventport.mchanges = mchanges; - engine->u.eventport.mevents = mevents; + engine->u.eventport.mevents = mevents; changes = nxt_malloc(sizeof(nxt_eventport_change_t) * mchanges); if (changes == NULL) { @@ -128,19 +131,17 @@ nxt_eventport_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_eventport_free(nxt_event_engine_t *engine) -{ - int port; +nxt_eventport_free(nxt_event_engine_t *engine) { + int port; port = engine->u.eventport.fd; nxt_debug(&engine->task, "eventport %d free", port); if (port != -1 && close(port) != 0) { - nxt_alert(&engine->task, "eventport close(%d) failed %E", - port, nxt_errno); + nxt_alert(&engine->task, "eventport close(%d) failed %E", port, + nxt_errno); } nxt_free(engine->u.eventport.events); @@ -148,30 +149,24 @@ nxt_eventport_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.eventport, sizeof(nxt_eventport_engine_t)); } - static void -nxt_eventport_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_eventport_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->write = NXT_EVENT_ACTIVE; nxt_eventport_enable_event(engine, ev, POLLIN | POLLOUT); } - static void -nxt_eventport_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE || ev->write != NXT_EVENT_INACTIVE) { - - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_eventport_disable_event(engine, ev); } } - /* * port_dissociate(3): * @@ -179,45 +174,39 @@ nxt_eventport_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static nxt_bool_t -nxt_eventport_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_INACTIVE; +nxt_eventport_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; return ev->changing; } - static void -nxt_eventport_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t events; +nxt_eventport_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t events; if (ev->read != NXT_EVENT_BLOCKED) { - events = (ev->write == NXT_EVENT_INACTIVE) ? POLLIN - : (POLLIN | POLLOUT); + events + = (ev->write == NXT_EVENT_INACTIVE) ? POLLIN : (POLLIN | POLLOUT); nxt_eventport_enable_event(engine, ev, events); } ev->read = NXT_EVENT_ACTIVE; } - static void -nxt_eventport_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t events; +nxt_eventport_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t events; if (ev->write != NXT_EVENT_BLOCKED) { - events = (ev->read == NXT_EVENT_INACTIVE) ? POLLOUT - : (POLLIN | POLLOUT); + events + = (ev->read == NXT_EVENT_INACTIVE) ? POLLOUT : (POLLIN | POLLOUT); nxt_eventport_enable_event(engine, ev, events); } ev->write = NXT_EVENT_ACTIVE; } - /* * eventport changes are batched to improve instruction and data * cache locality of several port_associate() and port_dissociate() @@ -226,12 +215,11 @@ nxt_eventport_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) static void nxt_eventport_enable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - nxt_uint_t events) -{ - nxt_eventport_change_t *change; + nxt_uint_t events) { + nxt_eventport_change_t *change; nxt_debug(ev->task, "port %d set event: fd:%d ev:%04XD u:%p", - engine->u.eventport.fd, ev->fd, events, ev); + engine->u.eventport.fd, ev->fd, events, ev); if (engine->u.eventport.nchanges >= engine->u.eventport.mchanges) { (void) nxt_eventport_commit_changes(engine); @@ -241,13 +229,11 @@ nxt_eventport_enable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev, change = &engine->u.eventport.changes[engine->u.eventport.nchanges++]; change->events = events; - change->event = ev; + change->event = ev; } - static void -nxt_eventport_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->read = NXT_EVENT_INACTIVE; if (ev->write == NXT_EVENT_INACTIVE) { @@ -258,10 +244,8 @@ nxt_eventport_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static void -nxt_eventport_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->write = NXT_EVENT_INACTIVE; if (ev->read == NXT_EVENT_INACTIVE) { @@ -272,14 +256,12 @@ nxt_eventport_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static void -nxt_eventport_disable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_eventport_change_t *change; +nxt_eventport_disable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_eventport_change_t *change; - nxt_debug(ev->task, "port %d disable event : fd:%d", - engine->u.eventport.fd, ev->fd); + nxt_debug(ev->task, "port %d disable event : fd:%d", engine->u.eventport.fd, + ev->fd); if (engine->u.eventport.nchanges >= engine->u.eventport.mchanges) { (void) nxt_eventport_commit_changes(engine); @@ -289,44 +271,42 @@ nxt_eventport_disable_event(nxt_event_engine_t *engine, nxt_fd_event_t *ev) change = &engine->u.eventport.changes[engine->u.eventport.nchanges++]; change->events = 0; - change->event = ev; + change->event = ev; } - static nxt_int_t -nxt_eventport_commit_changes(nxt_event_engine_t *engine) -{ +nxt_eventport_commit_changes(nxt_event_engine_t *engine) { int ret, port; nxt_int_t retval; - nxt_fd_event_t *ev; - nxt_eventport_change_t *change, *end; + nxt_fd_event_t *ev; + nxt_eventport_change_t *change, *end; port = engine->u.eventport.fd; - nxt_debug(&engine->task, "eventport %d changes:%ui", - port, engine->u.eventport.nchanges); + nxt_debug(&engine->task, "eventport %d changes:%ui", port, + engine->u.eventport.nchanges); retval = NXT_OK; change = engine->u.eventport.changes; - end = change + engine->u.eventport.nchanges; + end = change + engine->u.eventport.nchanges; do { - ev = change->event; + ev = change->event; ev->changing = 0; if (change->events != 0) { - nxt_debug(ev->task, "port_associate(%d): fd:%d ev:%04XD u:%p", - port, ev->fd, change->events, ev); + nxt_debug(ev->task, "port_associate(%d): fd:%d ev:%04XD u:%p", port, + ev->fd, change->events, ev); - ret = port_associate(port, PORT_SOURCE_FD, - ev->fd, change->events, ev); + ret = port_associate(port, PORT_SOURCE_FD, ev->fd, change->events, + ev); if (nxt_fast_path(ret == 0)) { goto next; } nxt_alert(ev->task, "port_associate(%d, %d, %d, %04XD) failed %E", - port, PORT_SOURCE_FD, ev->fd, change->events, nxt_errno); + port, PORT_SOURCE_FD, ev->fd, change->events, nxt_errno); } else { nxt_debug(ev->task, "port_dissociate(%d): fd:%d", port, ev->fd); @@ -337,13 +317,12 @@ nxt_eventport_commit_changes(nxt_event_engine_t *engine) goto next; } - nxt_alert(ev->task, "port_dissociate(%d, %d, %d) failed %E", - port, PORT_SOURCE_FD, ev->fd, nxt_errno); + nxt_alert(ev->task, "port_dissociate(%d, %d, %d) failed %E", port, + PORT_SOURCE_FD, ev->fd, nxt_errno); } nxt_work_queue_add(&engine->fast_work_queue, - nxt_eventport_error_handler, - ev->task, ev, ev->data); + nxt_eventport_error_handler, ev->task, ev, ev->data); retval = NXT_ERROR; @@ -358,42 +337,34 @@ nxt_eventport_commit_changes(nxt_event_engine_t *engine) return retval; } - static void -nxt_eventport_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fd_event_t *ev; +nxt_eventport_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fd_event_t *ev; ev = obj; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; ev->error_handler(task, ev, data); } - static void -nxt_eventport_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_BLOCKED; } } - static void -nxt_eventport_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_BLOCKED; } } - static void -nxt_eventport_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read == NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_ACTIVE; @@ -401,10 +372,8 @@ nxt_eventport_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static void -nxt_eventport_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write == NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_ACTIVE; @@ -412,30 +381,24 @@ nxt_eventport_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static void -nxt_eventport_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_eventport_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->read = NXT_EVENT_LEVEL; nxt_eventport_enable_event(engine, ev, POLLIN); } - static nxt_int_t nxt_eventport_enable_post(nxt_event_engine_t *engine, - nxt_work_handler_t handler) -{ + nxt_work_handler_t handler) { engine->u.eventport.post_handler = handler; return NXT_OK; } - static void -nxt_eventport_signal(nxt_event_engine_t *engine, nxt_uint_t signo) -{ - int port; +nxt_eventport_signal(nxt_event_engine_t *engine, nxt_uint_t signo) { + int port; port = engine->u.eventport.fd; @@ -446,18 +409,16 @@ nxt_eventport_signal(nxt_event_engine_t *engine, nxt_uint_t signo) } } - static void -nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ - int n, events, signo; - uint_t nevents; - nxt_err_t err; - nxt_uint_t i, level; - timespec_t ts, *tp; - port_event_t *event; - nxt_fd_event_t *ev; - nxt_work_handler_t handler; +nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { + int n, events, signo; + uint_t nevents; + nxt_err_t err; + nxt_uint_t i, level; + timespec_t ts, *tp; + port_event_t *event; + nxt_fd_event_t *ev; + nxt_work_handler_t handler; if (engine->u.eventport.nchanges != 0) { if (nxt_eventport_commit_changes(engine) != NXT_OK) { @@ -470,13 +431,13 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) tp = NULL; } else { - ts.tv_sec = timeout / 1000; + ts.tv_sec = timeout / 1000; ts.tv_nsec = (timeout % 1000) * 1000000; - tp = &ts; + tp = &ts; } nxt_debug(&engine->task, "port_getn(%d) timeout: %M", - engine->u.eventport.fd, timeout); + engine->u.eventport.fd, timeout); /* * A trap for possible error when Solaris does not update nevents @@ -486,15 +447,15 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) * The details are in OpenSolaris mailing list thread "port_getn() * and timeouts - is this a bug or an undocumented feature?" */ - event = &engine->u.eventport.events[0]; + event = &engine->u.eventport.events[0]; event->portev_events = -1; /* invalid port events */ event->portev_source = -1; /* invalid port source */ event->portev_object = -1; - event->portev_user = (void *) -1; + event->portev_user = (void *) -1; nevents = 1; - n = port_getn(engine->u.eventport.fd, engine->u.eventport.events, - engine->u.eventport.mevents, &nevents, tp); + n = port_getn(engine->u.eventport.fd, engine->u.eventport.events, + engine->u.eventport.mevents, &nevents, tp); /* * 32-bit port_getn() on Solaris 10 x86 returns large negative @@ -508,7 +469,7 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (err == NXT_ETIME || err == NXT_EINTR) { if (nevents != 0) { nxt_alert(&engine->task, "port_getn(%d) failed %E, events:%ud", - engine->u.eventport.fd, err, nevents); + engine->u.eventport.fd, err, nevents); } } @@ -516,7 +477,7 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT; nxt_log(&engine->task, level, "port_getn(%d) failed %E", - engine->u.eventport.fd, err); + engine->u.eventport.fd, err); if (err != NXT_EINTR) { return; @@ -524,28 +485,26 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } } - nxt_debug(&engine->task, "port_getn(%d) events: %d", - engine->u.eventport.fd, nevents); + nxt_debug(&engine->task, "port_getn(%d) events: %d", engine->u.eventport.fd, + nevents); for (i = 0; i < nevents; i++) { event = &engine->u.eventport.events[i]; switch (event->portev_source) { - case PORT_SOURCE_FD: - ev = event->portev_user; + ev = event->portev_user; events = event->portev_events; nxt_debug(ev->task, "eventport: fd:%d ev:%04Xd u:%p rd:%d wr:%d", - event->portev_object, events, ev, ev->read, ev->write); + event->portev_object, events, ev, ev->read, ev->write); if (nxt_slow_path(events & (POLLERR | POLLHUP | POLLNVAL)) != 0) { nxt_alert(ev->task, "port_getn(%d) error fd:%d events:%04Xud", - engine->u.eventport.fd, ev->fd, events); + engine->u.eventport.fd, ev->fd, events); nxt_work_queue_add(&engine->fast_work_queue, - nxt_eventport_error_handler, - ev->task, ev, ev->data); + nxt_eventport_error_handler, ev->task, ev, ev->data); continue; } @@ -554,8 +513,7 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->read != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); - + ev->task, ev, ev->data); } if (ev->read != NXT_EVENT_LEVEL) { @@ -568,7 +526,7 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->write != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } ev->write = NXT_EVENT_INACTIVE; @@ -578,7 +536,7 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) * Reactivate counterpart direction, because the * eventport is oneshot notification facility. */ - events = (ev->read == NXT_EVENT_INACTIVE) ? 0 : POLLIN; + events = (ev->read == NXT_EVENT_INACTIVE) ? 0 : POLLIN; events |= (ev->write == NXT_EVENT_INACTIVE) ? 0 : POLLOUT; if (events != 0) { @@ -589,25 +547,24 @@ nxt_eventport_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) case PORT_SOURCE_USER: nxt_debug(&engine->task, "eventport: user ev:%d u:%p", - event->portev_events, event->portev_user); + event->portev_events, event->portev_user); signo = event->portev_events; handler = (signo == 0) ? engine->u.eventport.post_handler : engine->u.eventport.signal_handler; - nxt_work_queue_add(&engine->fast_work_queue, handler, - &engine->task, (void *) (uintptr_t) signo, NULL); + nxt_work_queue_add(&engine->fast_work_queue, handler, &engine->task, + (void *) (uintptr_t) signo, NULL); break; default: nxt_alert(&engine->task, - "unexpected port_getn(%d) event: " - "ev:%d src:%d obj:%p u:%p", - engine->u.eventport.fd, event->portev_events, - event->portev_source, event->portev_object, - event->portev_user); + "unexpected port_getn(%d) event: " + "ev:%d src:%d obj:%p u:%p", + engine->u.eventport.fd, event->portev_events, + event->portev_source, event->portev_object, event->portev_user); } } } diff --git a/src/nxt_external.c b/src/nxt_external.c index c724b9bd2..dee4e1bbb 100644 --- a/src/nxt_external.c +++ b/src/nxt_external.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -9,10 +8,11 @@ #include -static nxt_int_t nxt_external_start(nxt_task_t *task, nxt_process_data_t *data); +static nxt_int_t +nxt_external_start(nxt_task_t *task, nxt_process_data_t *data); -nxt_app_module_t nxt_external_module = { +nxt_app_module_t nxt_external_module = { 0, NULL, nxt_string("external"), @@ -24,13 +24,11 @@ nxt_app_module_t nxt_external_module = { }; -extern char **environ; - +extern char **environ; nxt_inline nxt_int_t -nxt_external_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd) -{ - int res, flags; +nxt_external_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd) { + int res, flags; if (fd == -1) { return NXT_OK; @@ -55,34 +53,31 @@ nxt_external_fd_no_cloexec(nxt_task_t *task, nxt_socket_t fd) return NXT_OK; } - static nxt_int_t -nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) -{ - char **argv; +nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) { + char **argv; u_char buf[256]; - u_char *p, *end; + u_char *p, *end; uint32_t index; size_t size; nxt_str_t str; nxt_int_t rc; nxt_uint_t i, argc; - nxt_port_t *my_port, *proto_port, *router_port; - nxt_runtime_t *rt; - nxt_conf_value_t *value; - nxt_common_app_conf_t *conf; - nxt_external_app_conf_t *c; + nxt_port_t *my_port, *proto_port, *router_port; + nxt_runtime_t *rt; + nxt_conf_value_t *value; + nxt_common_app_conf_t *conf; + nxt_external_app_conf_t *c; - rt = task->thread->runtime; + rt = task->thread->runtime; conf = data->app; - proto_port = rt->port_by_type[NXT_PROCESS_PROTOTYPE]; + proto_port = rt->port_by_type[NXT_PROCESS_PROTOTYPE]; router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; - my_port = nxt_runtime_port_find(rt, nxt_pid, 0); + my_port = nxt_runtime_port_find(rt, nxt_pid, 0); - if (nxt_slow_path(proto_port == NULL || my_port == NULL - || router_port == NULL)) - { + if (nxt_slow_path( + proto_port == NULL || my_port == NULL || router_port == NULL)) { return NXT_ERROR; } @@ -119,19 +114,17 @@ nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) end = buf + sizeof(buf); p = nxt_sprintf(buf, end, - "%s;%uD;" - "%PI,%ud,%d;" - "%PI,%ud,%d;" - "%PI,%ud,%d,%d;" - "%d,%d;" - "%d,%z,%uD,%Z", - NXT_VERSION, my_port->process->stream, - proto_port->pid, proto_port->id, proto_port->pair[1], - router_port->pid, router_port->id, router_port->pair[1], - my_port->pid, my_port->id, my_port->pair[0], - my_port->pair[1], - conf->shared_port_fd, conf->shared_queue_fd, - 2, conf->shm_limit, conf->request_limit); + "%s;%uD;" + "%PI,%ud,%d;" + "%PI,%ud,%d;" + "%PI,%ud,%d,%d;" + "%d,%d;" + "%d,%z,%uD,%Z", + NXT_VERSION, my_port->process->stream, proto_port->pid, proto_port->id, + proto_port->pair[1], router_port->pid, router_port->id, + router_port->pair[1], my_port->pid, my_port->id, my_port->pair[0], + my_port->pair[1], conf->shared_port_fd, conf->shared_queue_fd, 2, + conf->shm_limit, conf->request_limit); if (nxt_slow_path(p == end)) { nxt_alert(task, "internal error: buffer too small for NXT_UNIT_INIT"); @@ -139,12 +132,12 @@ nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - nxt_debug(task, "update "NXT_UNIT_INIT_ENV"=%s", buf); + nxt_debug(task, "update " NXT_UNIT_INIT_ENV "=%s", buf); rc = setenv(NXT_UNIT_INIT_ENV, (char *) buf, 1); if (nxt_slow_path(rc == -1)) { - nxt_alert(task, "setenv("NXT_UNIT_INIT_ENV", %s) failed %E", buf, - nxt_errno); + nxt_alert(task, "setenv(" NXT_UNIT_INIT_ENV ", %s) failed %E", buf, + nxt_errno); return NXT_ERROR; } @@ -155,8 +148,7 @@ nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) size = 0; if (c->arguments != NULL) { - - for (index = 0; /* void */ ; index++) { + for (index = 0; /* void */; index++) { value = nxt_conf_get_array_element(c->arguments, index); if (value == NULL) { break; @@ -176,12 +168,12 @@ nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) } argv[0] = c->executable; - i = 1; + i = 1; if (c->arguments != NULL) { p = (u_char *) &argv[argc]; - for (index = 0; /* void */ ; index++) { + for (index = 0; /* void */; index++) { value = nxt_conf_get_array_element(c->arguments, index); if (value == NULL) { break; @@ -191,7 +183,7 @@ nxt_external_start(nxt_task_t *task, nxt_process_data_t *data) nxt_conf_get_string(value, &str); - p = nxt_cpymem(p, str.start, str.length); + p = nxt_cpymem(p, str.start, str.length); *p++ = '\0'; } } diff --git a/src/nxt_fd_event.c b/src/nxt_fd_event.c index dffe1026f..a30ac0e21 100644 --- a/src/nxt_fd_event.c +++ b/src/nxt_fd_event.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,38 +6,35 @@ #include -static nxt_int_t nxt_fd_event_hash_test(nxt_lvlhsh_query_t *lhq, void *data); -static void nxt_fd_event_hash_error(nxt_task_t *task, nxt_fd_t fd); +static nxt_int_t +nxt_fd_event_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static void +nxt_fd_event_hash_error(nxt_task_t *task, nxt_fd_t fd); -static const nxt_lvlhsh_proto_t nxt_event_set_fd_hash_proto nxt_aligned(64) = -{ +static const nxt_lvlhsh_proto_t nxt_event_set_fd_hash_proto nxt_aligned(64) = { NXT_LVLHSH_LARGE_MEMALIGN, nxt_fd_event_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - /* nxt_murmur_hash2() is unique for 4 bytes. */ static nxt_int_t -nxt_fd_event_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_fd_event_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { return NXT_OK; } - nxt_int_t -nxt_fd_event_hash_add(nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, nxt_fd_event_t *ev) -{ - nxt_int_t ret; - nxt_lvlhsh_query_t lhq; +nxt_fd_event_hash_add(nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, nxt_fd_event_t *ev) { + nxt_int_t ret; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t)); - lhq.replace = 0; - lhq.value = ev; - lhq.proto = &nxt_event_set_fd_hash_proto; + lhq.replace = 0; + lhq.value = ev; + lhq.proto = &nxt_event_set_fd_hash_proto; ret = nxt_lvlhsh_insert(lvlhsh, &lhq); @@ -51,15 +47,13 @@ nxt_fd_event_hash_add(nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, nxt_fd_event_t *ev) return NXT_ERROR; } - void * -nxt_fd_event_hash_get(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd) -{ - nxt_int_t ret; - nxt_lvlhsh_query_t lhq; +nxt_fd_event_hash_get(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd) { + nxt_int_t ret; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t)); - lhq.proto = &nxt_event_set_fd_hash_proto; + lhq.proto = &nxt_event_set_fd_hash_proto; ret = nxt_lvlhsh_find(lvlhsh, &lhq); @@ -72,16 +66,14 @@ nxt_fd_event_hash_get(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd) return NULL; } - void nxt_fd_event_hash_delete(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, - nxt_bool_t ignore) -{ - nxt_int_t ret; - nxt_lvlhsh_query_t lhq; + nxt_bool_t ignore) { + nxt_int_t ret; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t)); - lhq.proto = &nxt_event_set_fd_hash_proto; + lhq.proto = &nxt_event_set_fd_hash_proto; ret = nxt_lvlhsh_delete(lvlhsh, &lhq); @@ -92,11 +84,9 @@ nxt_fd_event_hash_delete(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, nxt_fd_t fd, } } - void -nxt_fd_event_hash_destroy(nxt_lvlhsh_t *lvlhsh) -{ - nxt_fd_event_t *ev; +nxt_fd_event_hash_destroy(nxt_lvlhsh_t *lvlhsh) { + nxt_fd_event_t *ev; do { ev = nxt_lvlhsh_retrieve(lvlhsh, &nxt_event_set_fd_hash_proto, NULL); @@ -104,9 +94,7 @@ nxt_fd_event_hash_destroy(nxt_lvlhsh_t *lvlhsh) } while (ev != NULL); } - static void -nxt_fd_event_hash_error(nxt_task_t *task, nxt_fd_t fd) -{ +nxt_fd_event_hash_error(nxt_task_t *task, nxt_fd_t fd) { nxt_alert(task, "fd event %d not found in hash", fd); } diff --git a/src/nxt_fd_event.h b/src/nxt_fd_event.h index 3a8d94604..5389c94bb 100644 --- a/src/nxt_fd_event.h +++ b/src/nxt_fd_event.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_FD_EVENT_H_INCLUDED_ #define _NXT_FD_EVENT_H_INCLUDED_ - typedef enum { /* A completely inactive event. */ NXT_EVENT_INACTIVE = 0, @@ -43,75 +41,71 @@ typedef enum { NXT_EVENT_ACTIVE = NXT_EVENT_DEFAULT, } nxt_fd_event_state_t; +#define nxt_fd_event_is_disabled(state) ((state) < NXT_EVENT_ONESHOT) -#define nxt_fd_event_is_disabled(state) \ - ((state) < NXT_EVENT_ONESHOT) - - -#define nxt_fd_event_is_active(state) \ - ((state) >= NXT_EVENT_ONESHOT) +#define nxt_fd_event_is_active(state) ((state) >= NXT_EVENT_ONESHOT) struct nxt_fd_event_s { - void *data; + void *data; /* Both are int's. */ - nxt_socket_t fd; - nxt_err_t error; + nxt_socket_t fd; + nxt_err_t error; /* The flags should also be prefetched by nxt_work_queue_pop(). */ #if (NXT_64BIT) - nxt_fd_event_state_t read:8; /* 3 bits. */ - nxt_fd_event_state_t write:8; /* 3 bits. */ - uint8_t read_ready; - uint8_t write_ready; - uint8_t changing; - uint8_t closed; - uint8_t timedout; - uint8_t shutdown:1; + nxt_fd_event_state_t read :8; /* 3 bits. */ + nxt_fd_event_state_t write:8; /* 3 bits. */ + uint8_t read_ready; + uint8_t write_ready; + uint8_t changing; + uint8_t closed; + uint8_t timedout; + uint8_t shutdown:1; #if (NXT_HAVE_EPOLL) - uint8_t epoll_eof:1; - uint8_t epoll_error:1; + uint8_t epoll_eof :1; + uint8_t epoll_error:1; #endif #if (NXT_HAVE_KQUEUE) - uint8_t kq_eof:1; + uint8_t kq_eof:1; #endif #else /* NXT_32BIT */ - nxt_fd_event_state_t read:3; - nxt_fd_event_state_t write:3; - uint8_t read_ready:1; - uint8_t write_ready:1; - uint8_t changing:1; - uint8_t closed:1; - uint8_t timedout:1; - uint8_t shutdown:1; + nxt_fd_event_state_t read :3; + nxt_fd_event_state_t write :3; + uint8_t read_ready :1; + uint8_t write_ready:1; + uint8_t changing :1; + uint8_t closed :1; + uint8_t timedout :1; + uint8_t shutdown :1; #if (NXT_HAVE_EPOLL) - uint8_t epoll_eof:1; - uint8_t epoll_error:1; + uint8_t epoll_eof :1; + uint8_t epoll_error:1; #endif #if (NXT_HAVE_KQUEUE) - uint8_t kq_eof:1; + uint8_t kq_eof:1; #endif #endif /* NXT_64BIT */ #if (NXT_HAVE_KQUEUE) /* nxt_err_t is int. */ - nxt_err_t kq_errno; + nxt_err_t kq_errno; /* struct kevent.data is intptr_t, however int32_t is enough. */ - int32_t kq_available; + int32_t kq_available; #endif - nxt_task_t *task; + nxt_task_t *task; - nxt_work_queue_t *read_work_queue; - nxt_work_handler_t read_handler; - nxt_work_queue_t *write_work_queue; - nxt_work_handler_t write_handler; - nxt_work_handler_t error_handler; + nxt_work_queue_t *read_work_queue; + nxt_work_handler_t read_handler; + nxt_work_queue_t *write_work_queue; + nxt_work_handler_t write_handler; + nxt_work_handler_t error_handler; - nxt_log_t *log; + nxt_log_t *log; }; diff --git a/src/nxt_fiber.c b/src/nxt_fiber.c index d6cac8930..79c700fff 100644 --- a/src/nxt_fiber.c +++ b/src/nxt_fiber.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,48 +6,49 @@ #include -static char *nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib); -static void nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent); -static void nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib); -static void nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data); - +static char * +nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib); +static void +nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent); +static void +nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib); +static void +nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data); -#define nxt_fiber_enqueue(thr, task, fib) \ - nxt_work_queue_add(&(thr)->engine->fast_work_queue, \ - nxt_fiber_switch_handler, task, fib, NULL) +#define nxt_fiber_enqueue(thr, task, fib) \ + nxt_work_queue_add(&(thr)->engine->fast_work_queue, \ + nxt_fiber_switch_handler, task, fib, NULL) nxt_fiber_main_t * -nxt_fiber_main_create(nxt_event_engine_t *engine) -{ - nxt_fiber_main_t *fm; +nxt_fiber_main_create(nxt_event_engine_t *engine) { + nxt_fiber_main_t *fm; fm = nxt_zalloc(sizeof(nxt_fiber_main_t)); if (nxt_slow_path(fm == NULL)) { return NULL; } - fm->engine = engine; + fm->engine = engine; fm->stack_size = 512 * 1024 - nxt_pagesize; - fm->idle = NULL; + fm->idle = NULL; return fm; } - nxt_int_t -nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack) -{ - int ret; - jmp_buf parent; - nxt_fid_t fid; - nxt_fiber_t *fib; - nxt_thread_t *thr; - nxt_fiber_main_t *fm; +nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack) { + int ret; + jmp_buf parent; + nxt_fid_t fid; + nxt_fiber_t *fib; + nxt_thread_t *thr; + nxt_fiber_main_t *fm; thr = nxt_thread(); - fm = thr->engine->fibers; + fm = thr->engine->fibers; fid = ++fm->fid; @@ -59,15 +59,15 @@ nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack) fib = fm->idle; if (fib != NULL) { - fm->idle = fib->next; - fib->fid = fid; + fm->idle = fib->next; + fib->fid = fid; fib->start = start; - fib->data = data; - fib->main = fm; + fib->data = data; + fib->main = fm; fib->task.thread = thr; - fib->task.log = thr->log; - fib->task.ident = nxt_task_next_ident(); + fib->task.log = thr->log; + fib->task.ident = nxt_task_next_ident(); nxt_debug(&fib->task, "fiber create cached: %PF", fib->fid); @@ -83,20 +83,19 @@ nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack) return NXT_ERROR; } - fib->fid = fid; - fib->start = start; - fib->data = data; + fib->fid = fid; + fib->start = start; + fib->data = data; fib->stack_size = fm->stack_size; - fib->main = fm; + fib->main = fm; fib->task.thread = thr; - fib->task.log = thr->log; - fib->task.ident = nxt_task_next_ident(); + fib->task.log = thr->log; + fib->task.ident = nxt_task_next_ident(); fib->stack = nxt_fiber_create_stack(&fib->task, fib); if (nxt_fast_path(fib->stack != NULL)) { - if (_setjmp(parent) != 0) { nxt_log_debug(thr->log, "fiber create: %PF", fib->fid); return NXT_OK; @@ -121,27 +120,27 @@ nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack) #if (NXT_LINUX) static char * -nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) -{ - char *s; - size_t size; +nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) { + char *s; + size_t size; size = fib->stack_size + nxt_pagesize; s = mmap(NULL, size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, -1, 0); + MAP_PRIVATE | MAP_ANON | MAP_GROWSDOWN, -1, 0); if (nxt_slow_path(s == MAP_FAILED)) { - nxt_alert(task, "fiber stack " - "mmap(%uz, MAP_PRIVATE|MAP_ANON|MAP_GROWSDOWN) failed %E", - size, nxt_errno); + nxt_alert(task, + "fiber stack " + "mmap(%uz, MAP_PRIVATE|MAP_ANON|MAP_GROWSDOWN) failed %E", + size, nxt_errno); return NULL; } if (nxt_slow_path(mprotect(s, nxt_pagesize, PROT_NONE) != 0)) { - nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E", - size, nxt_errno); + nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E", size, + nxt_errno); return NULL; } @@ -156,10 +155,9 @@ nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) #else /* Generic version. */ static char * -nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) -{ - char *s; - size_t size; +nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) { + char *s; + size_t size; size = fib->stack_size + nxt_pagesize; @@ -167,14 +165,14 @@ nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) if (nxt_slow_path(s == MAP_FAILED)) { nxt_alert(task, "fiber stack mmap(%uz, MAP_PRIVATE|MAP_ANON) failed %E", - size, nxt_errno); + size, nxt_errno); return NULL; } if (nxt_slow_path(mprotect(s, nxt_pagesize, PROT_NONE) != 0)) { - nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E", - size, nxt_errno); + nxt_alert(task, "fiber stack mprotect(%uz, PROT_NONE) failed %E", size, + nxt_errno); return NULL; } @@ -196,14 +194,12 @@ nxt_fiber_create_stack(nxt_task_t *task, nxt_fiber_t *fib) * pointers as signed int's. The bug has been fixed in glibc 2.8. */ -static void nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, - uint32_t pl); - +static void +nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl); static void -nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) -{ - ucontext_t uc; +nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) { + ucontext_t uc; nxt_debug(&fib->task, "fiber switch to stack: %p", fib->stack); @@ -212,30 +208,28 @@ nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) return; } - uc.uc_link = NULL; - uc.uc_stack.ss_sp = fib->stack; + uc.uc_link = NULL; + uc.uc_stack.ss_sp = fib->stack; uc.uc_stack.ss_size = fib->stack_size; makecontext(&uc, (void (*)(void)) nxt_fiber_trampoline, 4, - (uint32_t) ((uintptr_t) fib >> 32), - (uint32_t) ((uintptr_t) fib & 0xFFFFFFFF), - (uint32_t) ((uintptr_t) parent >> 32), - (uint32_t) ((uintptr_t) parent & 0xFFFFFFFF)); + (uint32_t) ((uintptr_t) fib >> 32), + (uint32_t) ((uintptr_t) fib & 0xFFFFFFFF), + (uint32_t) ((uintptr_t) parent >> 32), + (uint32_t) ((uintptr_t) parent & 0xFFFFFFFF)); setcontext(&uc); nxt_alert(&fib->task, "setcontext() failed"); } - static void -nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl) -{ - jmp_buf *parent; - nxt_task_t *task; - nxt_fiber_t *fib; +nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl) { + jmp_buf *parent; + nxt_task_t *task; + nxt_fiber_t *fib; - fib = (nxt_fiber_t *) (((uintptr_t) fh << 32) + fl); + fib = (nxt_fiber_t *) (((uintptr_t) fh << 32) + fl); parent = (jmp_buf *) (((uintptr_t) ph << 32) + pl); task = &fib->task; @@ -263,13 +257,12 @@ nxt_fiber_trampoline(uint32_t fh, uint32_t fl, uint32_t ph, uint32_t pl) /* Generic ucontext version. */ -static void nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent); - +static void +nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent); static void -nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) -{ - ucontext_t uc; +nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) { + ucontext_t uc; nxt_debug(&fib->task, "fiber switch to stack: %p", fib->stack); @@ -278,8 +271,8 @@ nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) return; } - uc.uc_link = NULL; - uc.uc_stack.ss_sp = fib->stack; + uc.uc_link = NULL; + uc.uc_stack.ss_sp = fib->stack; uc.uc_stack.ss_size = fib->stack_size; makecontext(&uc, (void (*)(void)) nxt_fiber_trampoline, 2, fib, parent); @@ -293,11 +286,9 @@ nxt_fiber_switch_stack(nxt_fiber_t *fib, jmp_buf *parent) #endif } - static void -nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent) -{ - nxt_task_t *task; +nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent) { + nxt_task_t *task; task = &fib->task; @@ -328,9 +319,8 @@ nxt_fiber_trampoline(nxt_fiber_t *fib, jmp_buf *parent) static void -nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fiber_t *fib; +nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fiber_t *fib; fib = obj; @@ -338,10 +328,8 @@ nxt_fiber_switch_handler(nxt_task_t *task, void *obj, void *data) nxt_unreachable(); } - static void -nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib) -{ +nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib) { nxt_debug(task, "fiber switch: %PF", fib->fid); task->thread->fiber = fib; @@ -351,23 +339,18 @@ nxt_fiber_switch(nxt_task_t *task, nxt_fiber_t *fib) nxt_unreachable(); } - nxt_fiber_t * -nxt_fiber_self(nxt_thread_t *thr) -{ +nxt_fiber_self(nxt_thread_t *thr) { return (nxt_fast_path(thr != NULL)) ? thr->fiber : NULL; } - void -nxt_fiber_yield(nxt_task_t *task) -{ - nxt_fiber_t *fib; +nxt_fiber_yield(nxt_task_t *task) { + nxt_fiber_t *fib; fib = task->thread->fiber; if (_setjmp(fib->jmp) == 0) { - nxt_debug(task, "fiber yield"); nxt_fiber_enqueue(task->thread, &fib->main->engine->task, fib); @@ -380,24 +363,21 @@ nxt_fiber_yield(nxt_task_t *task) nxt_debug(task, "fiber yield return"); } - void -nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout) -{ - nxt_fiber_t *fib; +nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout) { + nxt_fiber_t *fib; fib = task->thread->fiber; fib->timer.work_queue = &task->thread->engine->fast_work_queue; - fib->timer.handler = nxt_fiber_timer_handler; - fib->timer.log = &nxt_main_log; + fib->timer.handler = nxt_fiber_timer_handler; + fib->timer.log = &nxt_main_log; task = &fib->task; nxt_timer_add(task->thread->engine, &fib->timer, timeout); if (_setjmp(fib->jmp) == 0) { - nxt_debug(task, "fiber sleep: %T", timeout); nxt_fiber_switch(task, &fib->main->fiber); @@ -408,12 +388,10 @@ nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout) nxt_debug(task, "fiber sleep return"); } - static void -nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fiber_t *fib; - nxt_timer_t *ev; +nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fiber_t *fib; + nxt_timer_t *ev; ev = obj; @@ -426,11 +404,9 @@ nxt_fiber_timer_handler(nxt_task_t *task, void *obj, void *data) nxt_unreachable(); } - void -nxt_fiber_wait(nxt_task_t *task) -{ - nxt_fiber_t *fib; +nxt_fiber_wait(nxt_task_t *task) { + nxt_fiber_t *fib; fib = task->thread->fiber; @@ -445,18 +421,16 @@ nxt_fiber_wait(nxt_task_t *task) nxt_debug(task, "fiber wait return"); } - void -nxt_fiber_exit(nxt_task_t *task, nxt_fiber_t *next, void *data) -{ - nxt_fiber_t *fib; +nxt_fiber_exit(nxt_task_t *task, nxt_fiber_t *next, void *data) { + nxt_fiber_t *fib; fib = task->thread->fiber; nxt_debug(task, "fiber exit"); /* TODO: limit idle fibers. */ - fib->next = fib->main->idle; + fib->next = fib->main->idle; fib->main->idle = fib; nxt_fiber_switch(task, next); diff --git a/src/nxt_fiber.h b/src/nxt_fiber.h index 7f1beace6..31fe5c414 100644 --- a/src/nxt_fiber.h +++ b/src/nxt_fiber.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,50 +7,55 @@ #define _NXT_FIBER_H_INCLUDED_ -typedef struct nxt_fiber_main_s nxt_fiber_main_t; -typedef void (*nxt_fiber_start_t)(void *data); +typedef struct nxt_fiber_main_s nxt_fiber_main_t; +typedef void (*nxt_fiber_start_t)(void *data); -typedef uint32_t nxt_fid_t; -#define nxt_fiber_id(f) (f)->fid; +typedef uint32_t nxt_fid_t; +#define nxt_fiber_id(f) (f)->fid; -typedef struct nxt_fiber_s nxt_fiber_t; +typedef struct nxt_fiber_s nxt_fiber_t; struct nxt_fiber_s { - jmp_buf jmp; - nxt_fid_t fid; - nxt_fiber_start_t start; - void *data; - char *stack; - size_t stack_size; - nxt_err_t err; + jmp_buf jmp; + nxt_fid_t fid; + nxt_fiber_start_t start; + void *data; + char *stack; + size_t stack_size; + nxt_err_t err; - nxt_task_t task; + nxt_task_t task; - nxt_fiber_main_t *main; - nxt_fiber_t *next; + nxt_fiber_main_t *main; + nxt_fiber_t *next; - nxt_timer_t timer; + nxt_timer_t timer; }; - struct nxt_fiber_main_s { - nxt_fiber_t fiber; - nxt_fiber_t *idle; - nxt_event_engine_t *engine; - size_t stack_size; - nxt_fid_t fid; + nxt_fiber_t fiber; + nxt_fiber_t *idle; + nxt_event_engine_t *engine; + size_t stack_size; + nxt_fid_t fid; }; - -nxt_fiber_main_t *nxt_fiber_main_create(nxt_event_engine_t *engine); -nxt_int_t nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack); -void nxt_fiber_yield(nxt_task_t *task); -void nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout); -void nxt_fiber_wait(nxt_task_t *task); -void nxt_fiber_exit(nxt_task_t *task, nxt_fiber_t *next, void *data); -NXT_EXPORT nxt_fiber_t *nxt_fiber_self(nxt_thread_t *thr); +nxt_fiber_main_t * +nxt_fiber_main_create(nxt_event_engine_t *engine); +nxt_int_t +nxt_fiber_create(nxt_fiber_start_t start, void *data, size_t stack); +void +nxt_fiber_yield(nxt_task_t *task); +void +nxt_fiber_sleep(nxt_task_t *task, nxt_msec_t timeout); +void +nxt_fiber_wait(nxt_task_t *task); +void +nxt_fiber_exit(nxt_task_t *task, nxt_fiber_t *next, void *data); +NXT_EXPORT nxt_fiber_t * +nxt_fiber_self(nxt_thread_t *thr); #endif /* _NXT_FIBER_H_INCLUDED_ */ diff --git a/src/nxt_file.c b/src/nxt_file.c index 4047d9d7c..7eca2fd05 100644 --- a/src/nxt_file.c +++ b/src/nxt_file.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,11 +5,9 @@ #include - nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, - nxt_uint_t create, nxt_file_access_t access) -{ + nxt_uint_t create, nxt_file_access_t access) { #ifdef __CYGWIN__ mode |= O_BINARY; #endif @@ -26,16 +23,16 @@ nxt_file_open(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, nxt_thread_time_update(task->thread); #endif - nxt_debug(task, "open(\"%FN\", 0x%uXi, 0x%uXi): %FD err:%d", - file->name, mode, access, file->fd, file->error); + nxt_debug(task, "open(\"%FN\", 0x%uXi, 0x%uXi): %FD err:%d", file->name, + mode, access, file->fd, file->error); if (file->fd != -1) { return NXT_OK; } if (file->log_level != 0) { - nxt_log(task, file->log_level, "open(\"%FN\") failed %E", - file->name, file->error); + nxt_log(task, file->log_level, "open(\"%FN\") failed %E", file->name, + file->error); } return NXT_ERROR; @@ -47,17 +44,16 @@ nxt_file_open(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, nxt_int_t nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access, nxt_fd_t dfd, - nxt_uint_t resolve) -{ - struct open_how how; + nxt_uint_t resolve) { + struct open_how how; nxt_memzero(&how, sizeof(how)); /* O_NONBLOCK is to prevent blocking on FIFOs, special devices, etc. */ mode |= (O_NONBLOCK | create); - how.flags = mode; - how.mode = access; + how.flags = mode; + how.mode = access; how.resolve = resolve; file->fd = syscall(SYS_openat2, dfd, file->name, &how, sizeof(how)); @@ -69,7 +65,7 @@ nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, #endif nxt_debug(task, "openat2(%FD, \"%FN\"): %FD err:%d", dfd, file->name, - file->fd, file->error); + file->fd, file->error); if (file->fd != -1) { return NXT_OK; @@ -77,7 +73,7 @@ nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, if (file->log_level != 0) { nxt_log(task, file->log_level, "openat2(%FD, \"%FN\") failed %E", dfd, - file->name, file->error); + file->name, file->error); } return NXT_ERROR; @@ -87,22 +83,19 @@ nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, void -nxt_file_close(nxt_task_t *task, nxt_file_t *file) -{ +nxt_file_close(nxt_task_t *task, nxt_file_t *file) { nxt_debug(task, "close(%FD)", file->fd); if (close(file->fd) != 0) { - nxt_alert(task, "close(%FD, \"%FN\") failed %E", - file->fd, file->name, nxt_errno); + nxt_alert(task, "close(%FD, \"%FN\") failed %E", file->fd, file->name, + nxt_errno); } } - ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf, size_t size, - nxt_off_t offset) -{ - ssize_t n; + nxt_off_t offset) { + ssize_t n; nxt_thread_debug(thr); @@ -112,25 +105,22 @@ nxt_file_write(nxt_file_t *file, const u_char *buf, size_t size, nxt_thread_time_debug_update(thr); - nxt_log_debug(thr->log, "pwrite(%FD, %p, %uz, %O): %z", - file->fd, buf, size, offset, n); + nxt_log_debug(thr->log, "pwrite(%FD, %p, %uz, %O): %z", file->fd, buf, size, + offset, n); if (nxt_fast_path(n >= 0)) { return n; } nxt_thread_log_alert("pwrite(%FD, \"%FN\", %p, %uz, %O) failed %E", - file->fd, file->name, buf, size, - offset, file->error); + file->fd, file->name, buf, size, offset, file->error); return NXT_ERROR; } - ssize_t -nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset) -{ - ssize_t n; +nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset) { + ssize_t n; nxt_thread_debug(thr); @@ -140,16 +130,15 @@ nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset) nxt_thread_time_debug_update(thr); - nxt_log_debug(thr->log, "pread(%FD, %p, %uz, %O): %z", - file->fd, buf, size, offset, n); + nxt_log_debug(thr->log, "pread(%FD, %p, %uz, %O): %z", file->fd, buf, size, + offset, n); if (nxt_fast_path(n >= 0)) { return n; } - nxt_thread_log_alert("pread(%FD, \"%FN\", %p, %uz, %O) failed %E", - file->fd, file->name, buf, size, - offset, file->error); + nxt_thread_log_alert("pread(%FD, \"%FN\", %p, %uz, %O) failed %E", file->fd, + file->name, buf, size, offset, file->error); return NXT_ERROR; } @@ -160,15 +149,14 @@ nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset) /* FreeBSD 8.0 fcntl(F_READAHEAD, size) enables read ahead up to the size. */ void -nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) -{ - int ret; - u_char buf; +nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) { + int ret; + u_char buf; ret = fcntl(file->fd, F_READAHEAD, (int) size); - nxt_thread_log_debug("fcntl(%FD, F_READAHEAD, %uz): %d", - file->fd, size, ret); + nxt_thread_log_debug("fcntl(%FD, F_READAHEAD, %uz): %d", file->fd, size, + ret); if (nxt_fast_path(ret != -1)) { (void) nxt_file_read(file, &buf, 1, offset); @@ -176,7 +164,7 @@ nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) } nxt_thread_log_alert("fcntl(%FD, \"%FN\", F_READAHEAD, %uz) failed %E", - file->fd, file->name, size, nxt_errno); + file->fd, file->name, size, nxt_errno); } #elif (NXT_HAVE_POSIX_FADVISE) @@ -198,23 +186,20 @@ nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) */ void -nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) -{ - nxt_err_t err; +nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) { + nxt_err_t err; err = posix_fadvise(file->fd, offset, size, POSIX_FADV_WILLNEED); nxt_thread_log_debug("posix_fadvise(%FD, \"%FN\", %O, %uz, %d): %d", - file->fd, file->name, offset, size, - POSIX_FADV_WILLNEED, err); + file->fd, file->name, offset, size, POSIX_FADV_WILLNEED, err); if (nxt_fast_path(err == 0)) { return; } nxt_thread_log_alert("posix_fadvise(%FD, \"%FN\", %O, %uz, %d) failed %E", - file->fd, file->name, offset, size, - POSIX_FADV_WILLNEED, err); + file->fd, file->name, offset, size, POSIX_FADV_WILLNEED, err); } #elif (NXT_HAVE_RDAHEAD) @@ -222,10 +207,9 @@ nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) /* MacOSX fcntl(F_RDAHEAD). */ void -nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) -{ - int ret; - u_char buf; +nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) { + int ret; + u_char buf; ret = fcntl(file->fd, F_RDAHEAD, 1); @@ -237,15 +221,14 @@ nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) } nxt_thread_log_alert("fcntl(%FD, \"%FN\", F_RDAHEAD, 1) failed %E", - file->fd, file->name, nxt_errno); + file->fd, file->name, nxt_errno); } #else void -nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) -{ - u_char buf; +nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) { + u_char buf; (void) nxt_file_read(file, &buf, 1, offset); } @@ -254,9 +237,8 @@ nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size) nxt_int_t -nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi) -{ - int n; +nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi) { + int n; if (file->fd == NXT_FILE_INVALID) { n = stat((char *) file->name, fi); @@ -271,7 +253,7 @@ nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi) if (file->log_level != 0) { nxt_thread_log_error(file->log_level, "stat(\"%FN\") failed %E", - file->name, file->error); + file->name, file->error); } return NXT_ERROR; @@ -289,17 +271,15 @@ nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi) /* Use NXT_LOG_ALERT because fstat() error on open file is strange. */ - nxt_thread_log_alert("fstat(%FD, \"%FN\") failed %E", - file->fd, file->name, file->error); + nxt_thread_log_alert("fstat(%FD, \"%FN\") failed %E", file->fd, + file->name, file->error); return NXT_ERROR; } } - nxt_int_t -nxt_file_delete(nxt_file_name_t *name) -{ +nxt_file_delete(nxt_file_name_t *name) { nxt_thread_log_debug("unlink(\"%FN\")", name); if (nxt_fast_path(unlink((char *) name) == 0)) { @@ -311,10 +291,8 @@ nxt_file_delete(nxt_file_name_t *name) return NXT_ERROR; } - nxt_int_t -nxt_file_set_access(nxt_file_name_t *name, nxt_file_access_t access) -{ +nxt_file_set_access(nxt_file_name_t *name, nxt_file_access_t access) { if (nxt_fast_path(chmod((char *) name, access) == 0)) { return NXT_OK; } @@ -324,22 +302,20 @@ nxt_file_set_access(nxt_file_name_t *name, nxt_file_access_t access) return NXT_ERROR; } - nxt_int_t -nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) -{ - int err; - char *buf; - long bufsize; - gid_t gid = ~0; - uid_t uid = ~0; +nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) { + int err; + char *buf; + long bufsize; + gid_t gid = ~0; + uid_t uid = ~0; if (owner == NULL && group == NULL) { return NXT_OK; } if (owner != NULL) { - struct passwd pwd, *result; + struct passwd pwd, *result; bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); if (bufsize == -1) { @@ -353,9 +329,8 @@ nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) err = getpwnam_r(owner, &pwd, buf, bufsize, &result); if (result == NULL) { - nxt_thread_log_alert("getpwnam_r(\"%s\", ...) failed %E %s", - owner, nxt_errno, - err == 0 ? "(User not found)" : ""); + nxt_thread_log_alert("getpwnam_r(\"%s\", ...) failed %E %s", owner, + nxt_errno, err == 0 ? "(User not found)" : ""); goto out_err_free; } @@ -365,7 +340,7 @@ nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) } if (group != NULL) { - struct group grp, *result; + struct group grp, *result; bufsize = sysconf(_SC_GETGR_R_SIZE_MAX); if (bufsize == -1) { @@ -379,9 +354,8 @@ nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) err = getgrnam_r(group, &grp, buf, bufsize, &result); if (result == NULL) { - nxt_thread_log_alert("getgrnam_r(\"%s\", ...) failed %E %s", - group, nxt_errno, - err == 0 ? "(Group not found)" : ""); + nxt_thread_log_alert("getgrnam_r(\"%s\", ...) failed %E %s", group, + nxt_errno, err == 0 ? "(Group not found)" : ""); goto out_err_free; } @@ -395,8 +369,8 @@ nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) } nxt_thread_log_alert("chown(\"%FN\", %l, %l) failed %E", name, - owner != NULL ? (long) uid : -1, - group != NULL ? (long) gid : -1, nxt_errno); + owner != NULL ? (long) uid : -1, group != NULL ? (long) gid : -1, + nxt_errno); return NXT_ERROR; @@ -406,11 +380,9 @@ nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group) return NXT_ERROR; } - nxt_int_t -nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name) -{ - int ret; +nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name) { + int ret; nxt_thread_log_debug("rename(\"%FN\", \"%FN\")", old_name, new_name); @@ -419,13 +391,12 @@ nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name) return NXT_OK; } - nxt_thread_log_alert("rename(\"%FN\", \"%FN\") failed %E", - old_name, new_name, nxt_errno); + nxt_thread_log_alert("rename(\"%FN\", \"%FN\") failed %E", old_name, + new_name, nxt_errno); return NXT_ERROR; } - /* * ioctl(FIONBIO) sets a non-blocking mode using one syscall, * thereas fcntl(F_SETFL, O_NONBLOCK) needs to learn the current state @@ -441,9 +412,8 @@ nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name) #if (NXT_HAVE_FIONBIO) nxt_int_t -nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) -{ - int nb; +nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) { + int nb; nb = 1; @@ -454,14 +424,11 @@ nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) nxt_alert(task, "ioctl(%d, FIONBIO) failed %E", fd, nxt_errno); return NXT_ERROR; - } - nxt_int_t -nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) -{ - int nb; +nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) { + int nb; nb = 0; @@ -474,12 +441,11 @@ nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) return NXT_ERROR; } -#else /* !(NXT_HAVE_FIONBIO) */ +#else /* !(NXT_HAVE_FIONBIO) */ nxt_int_t -nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) -{ - int flags; +nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) { + int flags; flags = fcntl(fd, F_GETFL); @@ -491,19 +457,17 @@ nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd) flags |= O_NONBLOCK; if (nxt_slow_path(fcntl(fd, F_SETFL, flags) == -1)) { - nxt_alert(task, "fcntl(%d, F_SETFL, O_NONBLOCK) failed %E", - fd, nxt_errno); + nxt_alert(task, "fcntl(%d, F_SETFL, O_NONBLOCK) failed %E", fd, + nxt_errno); return NXT_ERROR; } return NXT_OK; } - nxt_int_t -nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) -{ - int flags; +nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) { + int flags; flags = fcntl(fd, F_GETFL); @@ -515,8 +479,8 @@ nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) flags &= O_NONBLOCK; if (nxt_slow_path(fcntl(fd, F_SETFL, flags) == -1)) { - nxt_alert(task, "fcntl(%d, F_SETFL, !O_NONBLOCK) failed %E", - fd, nxt_errno); + nxt_alert(task, "fcntl(%d, F_SETFL, !O_NONBLOCK) failed %E", fd, + nxt_errno); return NXT_ERROR; } @@ -527,10 +491,9 @@ nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd) ssize_t -nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size) -{ - ssize_t n; - nxt_err_t err; +nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size) { + ssize_t n; + nxt_err_t err; n = write(fd, buf, size); @@ -545,12 +508,10 @@ nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size) return n; } - ssize_t -nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size) -{ - ssize_t n; - nxt_err_t err; +nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size) { + ssize_t n; + nxt_err_t err; n = read(fd, buf, size); @@ -559,7 +520,6 @@ nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size) nxt_thread_log_debug("read(%FD, %p, %uz): %z", fd, buf, size, n); if (nxt_slow_path(n <= 0)) { - if (err == NXT_EAGAIN) { return 0; } @@ -570,10 +530,8 @@ nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size) return n; } - void -nxt_fd_close(nxt_fd_t fd) -{ +nxt_fd_close(nxt_fd_t fd) { nxt_thread_log_debug("close(%FD)", fd); if (nxt_slow_path(close(fd) != 0)) { @@ -581,22 +539,20 @@ nxt_fd_close(nxt_fd_t fd) } } - FILE * -nxt_file_fopen(nxt_task_t *task, const char *pathname, const char *mode) -{ +nxt_file_fopen(nxt_task_t *task, const char *pathname, const char *mode) { int err; - FILE *fp; + FILE *fp; #if (NXT_DEBUG) nxt_thread_time_update(task->thread); #endif - fp = fopen(pathname, mode); + fp = fopen(pathname, mode); err = (fp == NULL) ? nxt_errno : 0; nxt_debug(task, "fopen(\"%s\", \"%s\"): fp:%p err:%d", pathname, mode, fp, - err); + err); if (nxt_fast_path(fp != NULL)) { return fp; @@ -607,10 +563,8 @@ nxt_file_fopen(nxt_task_t *task, const char *pathname, const char *mode) return NULL; } - void -nxt_file_fclose(nxt_task_t *task, FILE *fp) -{ +nxt_file_fclose(nxt_task_t *task, FILE *fp) { nxt_debug(task, "fclose(%p)", fp); if (nxt_slow_path(fclose(fp) == -1)) { @@ -618,75 +572,67 @@ nxt_file_fclose(nxt_task_t *task, FILE *fp) } } - /* * nxt_file_redirect() redirects the file to the fd descriptor. * Then the fd descriptor is closed. */ nxt_int_t -nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd) -{ +nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd) { nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", fd, file->fd, file->name); if (dup2(fd, file->fd) == -1) { - nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", - fd, file->fd, file->name, nxt_errno); + nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", fd, file->fd, + file->name, nxt_errno); return NXT_ERROR; } if (close(fd) != 0) { - nxt_thread_log_alert("close(%FD, \"%FN\") failed %E", - fd, file->name, nxt_errno); + nxt_thread_log_alert("close(%FD, \"%FN\") failed %E", fd, file->name, + nxt_errno); return NXT_ERROR; } return NXT_OK; } - /* nxt_file_stdout() redirects the stdout descriptor to the file. */ nxt_int_t -nxt_file_stdout(nxt_file_t *file) -{ - nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", - file->fd, STDOUT_FILENO, file->name); +nxt_file_stdout(nxt_file_t *file) { + nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", file->fd, STDOUT_FILENO, + file->name); if (dup2(file->fd, STDOUT_FILENO) != -1) { return NXT_OK; } - nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", - file->fd, STDOUT_FILENO, file->name, nxt_errno); + nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", file->fd, + STDOUT_FILENO, file->name, nxt_errno); return NXT_ERROR; } - /* nxt_file_stderr() redirects the stderr descriptor to the file. */ nxt_int_t -nxt_file_stderr(nxt_file_t *file) -{ - nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", - file->fd, STDERR_FILENO, file->name); +nxt_file_stderr(nxt_file_t *file) { + nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", file->fd, STDERR_FILENO, + file->name); if (dup2(file->fd, STDERR_FILENO) != -1) { return NXT_OK; } - nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", - file->fd, STDERR_FILENO, file->name, nxt_errno); + nxt_thread_log_alert("dup2(%FD, %FD, \"%FN\") failed %E", file->fd, + STDERR_FILENO, file->name, nxt_errno); return NXT_ERROR; } - nxt_int_t -nxt_stderr_start(void) -{ - int flags, fd; +nxt_stderr_start(void) { + int flags, fd; flags = fcntl(nxt_stderr, F_GETFL); @@ -720,11 +666,9 @@ nxt_stderr_start(void) return flags; } - nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp, nxt_bool_t nbread, - nxt_bool_t nbwrite) -{ + nxt_bool_t nbwrite) { if (pipe(pp) != 0) { nxt_alert(task, "pipe() failed %E", nxt_errno); @@ -748,10 +692,8 @@ nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp, nxt_bool_t nbread, return NXT_OK; } - void -nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp) -{ +nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp) { nxt_debug(task, "pipe close(%FD:%FD)", pp[0], pp[1]); if (close(pp[0]) != 0) { @@ -763,10 +705,8 @@ nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp) } } - size_t -nxt_dir_current(char *buf, size_t len) -{ +nxt_dir_current(char *buf, size_t len) { if (nxt_fast_path(getcwd(buf, len) != NULL)) { return nxt_strlen(buf); } diff --git a/src/nxt_file.h b/src/nxt_file.h index 0c03a5f90..c6d8a3c04 100644 --- a/src/nxt_file.h +++ b/src/nxt_file.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,62 +7,53 @@ #define _NXT_UNIX_FILE_H_INCLUDED_ -typedef int nxt_fd_t; - -#define NXT_FILE_INVALID -1 +typedef int nxt_fd_t; -typedef nxt_uint_t nxt_file_access_t; -typedef struct stat nxt_file_info_t; +#define NXT_FILE_INVALID -1 +typedef nxt_uint_t nxt_file_access_t; +typedef struct stat nxt_file_info_t; -#define NXT_FILE_SYSTEM_NAME_UTF8 1 -typedef u_char nxt_file_name_t; +#define NXT_FILE_SYSTEM_NAME_UTF8 1 +typedef u_char nxt_file_name_t; typedef struct { - size_t len; - nxt_file_name_t *start; + size_t len; + nxt_file_name_t *start; } nxt_file_name_str_t; - -#define nxt_file_name_str_set(file_name, mem_pool, name) \ +#define nxt_file_name_str_set(file_name, mem_pool, name) \ ((file_name) = (nxt_file_name_t *) (name), NXT_OK) -#define nxt_file_name_alloc(mem_pool, len) \ - nxt_mp_nget(mem_pool, len) +#define nxt_file_name_alloc(mem_pool, len) nxt_mp_nget(mem_pool, len) -#define nxt_file_name_copy(dst, src, len) \ - nxt_cpymem(dst, src, len) +#define nxt_file_name_copy(dst, src, len) nxt_cpymem(dst, src, len) -#define nxt_file_name_add(dst, src, len) \ - nxt_cpymem(dst, src, len) +#define nxt_file_name_add(dst, src, len) nxt_cpymem(dst, src, len) #if (NXT_HAVE_CASELESS_FILESYSTEM) /* MacOSX, Cygwin. */ -#define nxt_file_name_eq(fn1, fn2) \ - (nxt_strcasecmp(fn1, fn2) == 0) +#define nxt_file_name_eq(fn1, fn2) (nxt_strcasecmp(fn1, fn2) == 0) #else -#define nxt_file_name_eq(fn1, fn2) \ - (nxt_strcmp(fn1, fn2) == 0) +#define nxt_file_name_eq(fn1, fn2) (nxt_strcmp(fn1, fn2) == 0) #endif -#define nxt_file_name_is_absolute(name) \ - (name[0] == '/') +#define nxt_file_name_is_absolute(name) (name[0] == '/') -#define NXT_MAX_PATH_LEN MAXPATHLEN - +#define NXT_MAX_PATH_LEN MAXPATHLEN typedef enum { NXT_FILE_UNKNOWN = 0, @@ -71,63 +61,63 @@ typedef enum { NXT_FILE_DIRECTORY, } nxt_file_type_t; - typedef struct { - nxt_file_name_t *name; + nxt_file_name_t *name; /* Both are int's. */ - nxt_fd_t fd; - nxt_err_t error; + nxt_fd_t fd; + nxt_err_t error; -#define NXT_FILE_ACCESSED_LONG_AGO 0xFFFF +#define NXT_FILE_ACCESSED_LONG_AGO 0xFFFF /* * Number of seconds ago the file content was last * read. The maximum value is about 18 hours. */ - uint16_t accessed; + uint16_t accessed; - uint8_t type; /* nxt_file_type_t */ + uint8_t type; /* nxt_file_type_t */ /* * Log open() file error with given log level if it is non zero. * Note that zero log level is NXT_LOG_ALERT. */ - uint8_t log_level; + uint8_t log_level; - nxt_time_t mtime; - nxt_off_t size; + nxt_time_t mtime; + nxt_off_t size; } nxt_file_t; - -NXT_EXPORT nxt_int_t nxt_file_open(nxt_task_t *task, nxt_file_t *file, - nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access); +NXT_EXPORT nxt_int_t +nxt_file_open(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, + nxt_uint_t create, nxt_file_access_t access); #if (NXT_HAVE_OPENAT2) -NXT_EXPORT nxt_int_t nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, - nxt_uint_t mode, nxt_uint_t create, nxt_file_access_t access, nxt_fd_t dfd, +NXT_EXPORT nxt_int_t +nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode, + nxt_uint_t create, nxt_file_access_t access, nxt_fd_t dfd, nxt_uint_t resolve); #endif /* The file open access modes. */ -#define NXT_FILE_RDONLY O_RDONLY -#define NXT_FILE_WRONLY O_WRONLY -#define NXT_FILE_RDWR O_RDWR -#define NXT_FILE_APPEND (O_WRONLY | O_APPEND) +#define NXT_FILE_RDONLY O_RDONLY +#define NXT_FILE_WRONLY O_WRONLY +#define NXT_FILE_RDWR O_RDWR +#define NXT_FILE_APPEND (O_WRONLY | O_APPEND) #if (NXT_HAVE_OPENAT2) #if defined(O_DIRECTORY) -#define NXT_FILE_DIRECTORY O_DIRECTORY +#define NXT_FILE_DIRECTORY O_DIRECTORY #else -#define NXT_FILE_DIRECTORY 0 +#define NXT_FILE_DIRECTORY 0 #endif #if defined(O_SEARCH) -#define NXT_FILE_SEARCH (O_SEARCH|NXT_FILE_DIRECTORY) +#define NXT_FILE_SEARCH (O_SEARCH | NXT_FILE_DIRECTORY) #elif defined(O_EXEC) -#define NXT_FILE_SEARCH (O_EXEC|NXT_FILE_DIRECTORY) +#define NXT_FILE_SEARCH (O_EXEC | NXT_FILE_DIRECTORY) #else /* @@ -136,84 +126,95 @@ NXT_EXPORT nxt_int_t nxt_file_openat2(nxt_task_t *task, nxt_file_t *file, * modern system (i.e. glibc 2.14+) and run with a kernel older than 2.6.39. * Then O_PATH is unknown to the kernel and ignored, while O_RDONLY is used. */ -#define NXT_FILE_SEARCH (O_PATH|O_RDONLY|NXT_FILE_DIRECTORY) +#define NXT_FILE_SEARCH (O_PATH | O_RDONLY | NXT_FILE_DIRECTORY) #endif #endif /* NXT_HAVE_OPENAT2 */ /* The file creation modes. */ -#define NXT_FILE_CREATE_OR_OPEN O_CREAT -#define NXT_FILE_OPEN 0 -#define NXT_FILE_TRUNCATE (O_CREAT | O_TRUNC) +#define NXT_FILE_CREATE_OR_OPEN O_CREAT +#define NXT_FILE_OPEN 0 +#define NXT_FILE_TRUNCATE (O_CREAT | O_TRUNC) /* The file access rights. */ -#define NXT_FILE_DEFAULT_ACCESS 0644 -#define NXT_FILE_OWNER_ACCESS 0600 +#define NXT_FILE_DEFAULT_ACCESS 0644 +#define NXT_FILE_OWNER_ACCESS 0600 -NXT_EXPORT void nxt_file_close(nxt_task_t *task, nxt_file_t *file); -NXT_EXPORT ssize_t nxt_file_write(nxt_file_t *file, const u_char *buf, - size_t size, nxt_off_t offset); -NXT_EXPORT ssize_t nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, +NXT_EXPORT void +nxt_file_close(nxt_task_t *task, nxt_file_t *file); +NXT_EXPORT ssize_t +nxt_file_write(nxt_file_t *file, const u_char *buf, size_t size, nxt_off_t offset); -NXT_EXPORT void nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, - size_t size); -NXT_EXPORT nxt_int_t nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi); +NXT_EXPORT ssize_t +nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset); +NXT_EXPORT void +nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size); +NXT_EXPORT nxt_int_t +nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi); -#define nxt_is_dir(fi) \ - (S_ISDIR((fi)->st_mode)) +#define nxt_is_dir(fi) (S_ISDIR((fi)->st_mode)) -#define nxt_is_file(fi) \ - (S_ISREG((fi)->st_mode)) +#define nxt_is_file(fi) (S_ISREG((fi)->st_mode)) -#define nxt_file_size(fi) \ - (fi)->st_size +#define nxt_file_size(fi) (fi)->st_size -#define nxt_file_mtime(fi) \ - (fi)->st_mtime +#define nxt_file_mtime(fi) (fi)->st_mtime -NXT_EXPORT nxt_int_t nxt_file_delete(nxt_file_name_t *name); -NXT_EXPORT nxt_int_t nxt_file_set_access(nxt_file_name_t *name, - nxt_file_access_t access); -NXT_EXPORT nxt_int_t nxt_file_chown(nxt_file_name_t *name, const char *owner, - const char *group); -NXT_EXPORT nxt_int_t nxt_file_rename(nxt_file_name_t *old_name, - nxt_file_name_t *new_name); +NXT_EXPORT nxt_int_t +nxt_file_delete(nxt_file_name_t *name); +NXT_EXPORT nxt_int_t +nxt_file_set_access(nxt_file_name_t *name, nxt_file_access_t access); +NXT_EXPORT nxt_int_t +nxt_file_chown(nxt_file_name_t *name, const char *owner, const char *group); +NXT_EXPORT nxt_int_t +nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name); -NXT_EXPORT nxt_int_t nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd); -NXT_EXPORT nxt_int_t nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd); -NXT_EXPORT ssize_t nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size); -NXT_EXPORT ssize_t nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size); -NXT_EXPORT void nxt_fd_close(nxt_fd_t fd); +NXT_EXPORT nxt_int_t +nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd); +NXT_EXPORT nxt_int_t +nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd); +NXT_EXPORT ssize_t +nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size); +NXT_EXPORT ssize_t +nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size); +NXT_EXPORT void +nxt_fd_close(nxt_fd_t fd); -NXT_EXPORT FILE *nxt_file_fopen(nxt_task_t *task, const char *pathname, - const char *mode); -NXT_EXPORT void nxt_file_fclose(nxt_task_t *task, FILE *fp); +NXT_EXPORT FILE * +nxt_file_fopen(nxt_task_t *task, const char *pathname, const char *mode); +NXT_EXPORT void +nxt_file_fclose(nxt_task_t *task, FILE *fp); -NXT_EXPORT nxt_int_t nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd); -NXT_EXPORT nxt_int_t nxt_file_stdout(nxt_file_t *file); -NXT_EXPORT nxt_int_t nxt_file_stderr(nxt_file_t *file); -NXT_EXPORT nxt_int_t nxt_stderr_start(void); +NXT_EXPORT nxt_int_t +nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd); +NXT_EXPORT nxt_int_t +nxt_file_stdout(nxt_file_t *file); +NXT_EXPORT nxt_int_t +nxt_file_stderr(nxt_file_t *file); +NXT_EXPORT nxt_int_t +nxt_stderr_start(void); -#define nxt_stdout STDOUT_FILENO -#define nxt_stderr STDERR_FILENO +#define nxt_stdout STDOUT_FILENO +#define nxt_stderr STDERR_FILENO -#define nxt_write_console(fd, buf, size) \ - write(fd, buf, size) +#define nxt_write_console(fd, buf, size) write(fd, buf, size) -#define nxt_write_syslog(priority, message) \ - syslog(priority, "%s", message) +#define nxt_write_syslog(priority, message) syslog(priority, "%s", message) -NXT_EXPORT nxt_int_t nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp, - nxt_bool_t nbread, nxt_bool_t nbwrite); -NXT_EXPORT void nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp); +NXT_EXPORT nxt_int_t +nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp, nxt_bool_t nbread, + nxt_bool_t nbwrite); +NXT_EXPORT void +nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp); -NXT_EXPORT size_t nxt_dir_current(char *buf, size_t len); +NXT_EXPORT size_t +nxt_dir_current(char *buf, size_t len); #endif /* _NXT_UNIX_FILE_H_INCLUDED_ */ diff --git a/src/nxt_file_event.h b/src/nxt_file_event.h index d4255ce94..044cc202e 100644 --- a/src/nxt_file_event.h +++ b/src/nxt_file_event.h @@ -6,12 +6,11 @@ #ifndef _NXT_FILE_EVENT_H_INCLUDED_ #define _NXT_FILE_EVENT_H_INCLUDED_ - typedef struct { - void *data; - nxt_file_t *file; - nxt_work_handler_t handler; - nxt_task_t *task; + void *data; + nxt_file_t *file; + nxt_work_handler_t handler; + nxt_task_t *task; } nxt_file_event_t; diff --git a/src/nxt_file_name.c b/src/nxt_file_name.c index 935479ead..30c1510fd 100644 --- a/src/nxt_file_name.c +++ b/src/nxt_file_name.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,7 +5,6 @@ #include - /* * Supported formats: * %s null-terminated string @@ -18,26 +16,24 @@ nxt_int_t nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, - const char *format, ...) -{ + const char *format, ...) { u_char ch, *p; size_t length; va_list args; - nxt_str_t *v; + nxt_str_t *v; nxt_bool_t zero; - const char *fmt; - nxt_file_name_t *dst, *fn; + const char *fmt; + nxt_file_name_t *dst, *fn; va_start(args, format); - fmt = format; - zero = 0; + fmt = format; + zero = 0; length = 0; - for ( ;; ) { + for (;;) { ch = *fmt++; if (ch != '%') { - if (ch != '\0') { length++; continue; @@ -49,7 +45,6 @@ nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, ch = *fmt++; switch (ch) { - case 'V': v = va_arg(args, nxt_str_t *); @@ -117,16 +112,15 @@ nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, } file_name->start = fn; - dst = fn; + dst = fn; va_start(args, format); fmt = format; - for ( ;; ) { + for (;;) { ch = *fmt++; if (ch != '%') { - if (ch != '\0') { *dst++ = (nxt_file_name_t) ch; continue; @@ -138,7 +132,6 @@ nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, ch = *fmt++; switch (ch) { - case 'V': v = va_arg(args, nxt_str_t *); @@ -165,7 +158,7 @@ nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *file_name, ch = *fmt++; if (nxt_fast_path(ch == 's')) { - p = va_arg(args, u_char *); + p = va_arg(args, u_char *); dst = nxt_file_name_add(dst, p, length); } diff --git a/src/nxt_file_name.h b/src/nxt_file_name.h index f8e7a3ba9..ffeb3b802 100644 --- a/src/nxt_file_name.h +++ b/src/nxt_file_name.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,8 +7,9 @@ #define _NXT_FILE_NAME_H_INCLUDED_ -NXT_EXPORT nxt_int_t nxt_file_name_create(nxt_mp_t *mp, - nxt_file_name_str_t *fn, const char *format, ...); +NXT_EXPORT nxt_int_t +nxt_file_name_create(nxt_mp_t *mp, nxt_file_name_str_t *fn, const char *format, + ...); #endif /* _NXT_FILE_NAME_H_INCLUDED_ */ diff --git a/src/nxt_freebsd_sendfile.c b/src/nxt_freebsd_sendfile.c index a9535c109..eceafbe93 100644 --- a/src/nxt_freebsd_sendfile.c +++ b/src/nxt_freebsd_sendfile.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -15,39 +14,39 @@ #ifdef NXT_TEST_BUILD_FREEBSD_SENDFILE -ssize_t nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static int nxt_sys_sendfile(int fd, int s, off_t offset, size_t nbytes, - struct sf_hdtr *hdtr, off_t *sbytes, int flags) -{ +static int +nxt_sys_sendfile(int fd, int s, off_t offset, size_t nbytes, + struct sf_hdtr *hdtr, off_t *sbytes, int flags) { return -1; } #else -#define nxt_sys_sendfile sendfile +#define nxt_sys_sendfile sendfile #endif ssize_t nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, - size_t limit) -{ - size_t file_size; - ssize_t n; - nxt_buf_t *fb; - nxt_err_t err; - nxt_off_t sent; - nxt_uint_t nhd, ntr; - struct iovec hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX]; - struct sf_hdtr hdtr, *ht; - nxt_sendbuf_coalesce_t sb; - - sb.buf = b; + size_t limit) { + size_t file_size; + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_off_t sent; + nxt_uint_t nhd, ntr; + struct iovec hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX]; + struct sf_hdtr hdtr, *ht; + nxt_sendbuf_coalesce_t sb; + + sb.buf = b; sb.iobuf = hd; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; nhd = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -81,24 +80,23 @@ nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, nxt_memzero(&hdtr, sizeof(struct sf_hdtr)); if (nhd != 0) { - ht = &hdtr; + ht = &hdtr; hdtr.headers = hd; hdtr.hdr_cnt = nhd; } if (ntr != 0) { - ht = &hdtr; + ht = &hdtr; hdtr.trailers = tr; - hdtr.trl_cnt = ntr; + hdtr.trl_cnt = ntr; } nxt_debug(c->socket.task, "sendfile(%FD, %d, @%O, %uz) hd:%ui tr:%ui", - fb->file->fd, c->socket.fd, fb->file_pos, file_size, - nhd, ntr); + fb->file->fd, c->socket.fd, fb->file_pos, file_size, nhd, ntr); sent = 0; - n = nxt_sys_sendfile(fb->file->fd, c->socket.fd, fb->file_pos, - file_size, ht, &sent, 0); + n = nxt_sys_sendfile(fb->file->fd, c->socket.fd, fb->file_pos, file_size, + ht, &sent, 0); err = (n == -1) ? nxt_errno : 0; @@ -106,7 +104,6 @@ nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -130,7 +127,7 @@ nxt_freebsd_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, } else if (sent == 0) { nxt_log(c->socket.task, NXT_LOG_ERR, - "file \"%FN\" was truncated while sendfile()", fb->file->name); + "file \"%FN\" was truncated while sendfile()", fb->file->name); return NXT_ERROR; } diff --git a/src/nxt_fs.c b/src/nxt_fs.c index 8ea8e1867..16f30a456 100644 --- a/src/nxt_fs.c +++ b/src/nxt_fs.c @@ -6,31 +6,30 @@ #include -static nxt_int_t nxt_fs_mkdir(const u_char *dir, mode_t mode); - +static nxt_int_t +nxt_fs_mkdir(const u_char *dir, mode_t mode); nxt_int_t -nxt_fs_mkdir_p(const u_char *dir, mode_t mode) -{ - char *start, *end, *dst; - size_t dirlen; - nxt_int_t ret; - char path[PATH_MAX]; +nxt_fs_mkdir_p(const u_char *dir, mode_t mode) { + char *start, *end, *dst; + size_t dirlen; + nxt_int_t ret; + char path[PATH_MAX]; dirlen = nxt_strlen(dir); nxt_assert(dirlen < PATH_MAX && dirlen > 0); - dst = path; + dst = path; start = (char *) dir; while (*start != '\0') { end = strchr(start + 1, '/'); if (end == NULL) { - end = ((char *)dir + dirlen); + end = ((char *) dir + dirlen); } - dst = nxt_cpymem(dst, start, end - start); + dst = nxt_cpymem(dst, start, end - start); *dst = '\0'; ret = nxt_fs_mkdir((u_char *) path, mode); @@ -44,12 +43,10 @@ nxt_fs_mkdir_p(const u_char *dir, mode_t mode) return NXT_OK; } - nxt_int_t -nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode) -{ - char *ptr, *dir; - nxt_int_t ret; +nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode) { + char *ptr, *dir; + nxt_int_t ret; dir = nxt_strdup(path); if (nxt_slow_path(dir == NULL)) { @@ -64,7 +61,7 @@ nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode) } *ptr = '\0'; - ret = nxt_fs_mkdir_p((const u_char *) dir, mode); + ret = nxt_fs_mkdir_p((const u_char *) dir, mode); out_free: nxt_free(dir); @@ -72,10 +69,8 @@ nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode) return ret; } - static nxt_int_t -nxt_fs_mkdir(const u_char *dir, mode_t mode) -{ +nxt_fs_mkdir(const u_char *dir, mode_t mode) { if (nxt_fast_path(mkdir((const char *) dir, mode) == 0)) { return NXT_OK; } diff --git a/src/nxt_fs.h b/src/nxt_fs.h index a06e4d3d8..cffbbe24c 100644 --- a/src/nxt_fs.h +++ b/src/nxt_fs.h @@ -6,8 +6,10 @@ #define _NXT_FS_H_INCLUDED_ -nxt_int_t nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode); -nxt_int_t nxt_fs_mkdir_p(const u_char *dir, mode_t mode); +nxt_int_t +nxt_fs_mkdir_p_dirname(const u_char *path, mode_t mode); +nxt_int_t +nxt_fs_mkdir_p(const u_char *dir, mode_t mode); -#endif /* _NXT_FS_H_INCLUDED_ */ +#endif /* _NXT_FS_H_INCLUDED_ */ diff --git a/src/nxt_fs_mount.c b/src/nxt_fs_mount.c index d9b384e43..95eb2257d 100644 --- a/src/nxt_fs_mount.c +++ b/src/nxt_fs_mount.c @@ -13,23 +13,21 @@ #if (NXT_HAVE_LINUX_MOUNT) nxt_int_t -nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) -{ - int rc; - const char *fsname; - unsigned long flags; +nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) { + int rc; + const char *fsname; + unsigned long flags; flags = 0; switch (mnt->type) { case NXT_FS_BIND: if (nxt_slow_path(mnt->flags != 0)) { - nxt_log(task, NXT_LOG_WARN, - "bind mount ignores additional flags"); + nxt_log(task, NXT_LOG_WARN, "bind mount ignores additional flags"); } fsname = "bind"; - flags = MS_BIND | MS_REC; + flags = MS_BIND | MS_REC; break; case NXT_FS_PROC: @@ -63,11 +61,11 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) } rc = mount((const char *) mnt->src, (const char *) mnt->dst, fsname, flags, - mnt->data); + mnt->data); if (nxt_slow_path(rc < 0)) { nxt_alert(task, "mount(\"%s\", \"%s\", \"%s\", %ul, \"%s\") %E", - mnt->src, mnt->dst, fsname, flags, mnt->data, nxt_errno); + mnt->src, mnt->dst, fsname, flags, mnt->data, nxt_errno); return NXT_ERROR; } @@ -78,15 +76,14 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) #elif (NXT_HAVE_FREEBSD_NMOUNT) nxt_int_t -nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) -{ - int flags; - u_char *data, *p, *end; - size_t iovlen; - nxt_int_t ret; - const char *fsname; - struct iovec iov[128]; - char errmsg[256]; +nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) { + int flags; + u_char *data, *p, *end; + size_t iovlen; + nxt_int_t ret; + const char *fsname; + struct iovec iov[128]; + char errmsg[256]; if (nxt_slow_path((mnt->flags & NXT_FS_FLAGS_NODEV) && !mnt->builtin)) { nxt_alert(task, "nmount(2) doesn't support \"nodev\" option"); @@ -132,21 +129,21 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) } iov[0].iov_base = (void *) "fstype"; - iov[0].iov_len = 7; + iov[0].iov_len = 7; iov[1].iov_base = (void *) fsname; - iov[1].iov_len = nxt_strlen(fsname) + 1; + iov[1].iov_len = nxt_strlen(fsname) + 1; iov[2].iov_base = (void *) "fspath"; - iov[2].iov_len = 7; + iov[2].iov_len = 7; iov[3].iov_base = (void *) mnt->dst; - iov[3].iov_len = nxt_strlen(mnt->dst) + 1; + iov[3].iov_len = nxt_strlen(mnt->dst) + 1; iov[4].iov_base = (void *) "target"; - iov[4].iov_len = 7; + iov[4].iov_len = 7; iov[5].iov_base = (void *) mnt->src; - iov[5].iov_len = nxt_strlen(mnt->src) + 1; + iov[5].iov_len = nxt_strlen(mnt->src) + 1; iov[6].iov_base = (void *) "errmsg"; - iov[6].iov_len = 7; + iov[6].iov_len = 7; iov[7].iov_base = (void *) errmsg; - iov[7].iov_len = sizeof(errmsg); + iov[7].iov_len = sizeof(errmsg); iovlen = 8; @@ -161,7 +158,7 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) end = data - 1; do { - p = end + 1; + p = end + 1; end = nxt_strchr(p, '='); if (end == NULL) { break; @@ -170,7 +167,7 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) *end = '\0'; iov[iovlen].iov_base = (void *) p; - iov[iovlen].iov_len = (end - p) + 1; + iov[iovlen].iov_len = (end - p) + 1; iovlen++; @@ -182,7 +179,7 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) } iov[iovlen].iov_base = (void *) p; - iov[iovlen].iov_len = nxt_strlen(p) + 1; + iov[iovlen].iov_len = nxt_strlen(p) + 1; iovlen++; @@ -209,19 +206,17 @@ nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt) #if (NXT_HAVE_LINUX_UMOUNT2) void -nxt_fs_unmount(const u_char *path) -{ +nxt_fs_unmount(const u_char *path) { if (nxt_slow_path(umount2((const char *) path, MNT_DETACH) < 0)) { - nxt_thread_log_error(NXT_LOG_WARN, "umount2(%s, MNT_DETACH) %E", - path, nxt_errno); + nxt_thread_log_error(NXT_LOG_WARN, "umount2(%s, MNT_DETACH) %E", path, + nxt_errno); } } #elif (NXT_HAVE_UNMOUNT) void -nxt_fs_unmount(const u_char *path) -{ +nxt_fs_unmount(const u_char *path) { if (nxt_slow_path(unmount((const char *) path, MNT_FORCE) < 0)) { nxt_thread_log_error(NXT_LOG_WARN, "unmount(%s) %E", path, nxt_errno); } diff --git a/src/nxt_fs_mount.h b/src/nxt_fs_mount.h index e0fe6eb2c..fa9a42315 100644 --- a/src/nxt_fs_mount.h +++ b/src/nxt_fs_mount.h @@ -6,10 +6,9 @@ #define _NXT_FS_MOUNT_H_INCLUDED_ -typedef enum nxt_fs_type_s nxt_fs_type_t; -typedef enum nxt_fs_flags_s nxt_fs_flags_t; -typedef struct nxt_fs_mount_s nxt_fs_mount_t; - +typedef enum nxt_fs_type_s nxt_fs_type_t; +typedef enum nxt_fs_flags_s nxt_fs_flags_t; +typedef struct nxt_fs_mount_s nxt_fs_mount_t; enum nxt_fs_type_s { NXT_FS_UNKNOWN = 0, @@ -19,30 +18,29 @@ enum nxt_fs_type_s { NXT_FS_LAST, }; - enum nxt_fs_flags_s { - NXT_FS_FLAGS_NOSUID = 1 << 0, - NXT_FS_FLAGS_NOEXEC = 1 << 1, - NXT_FS_FLAGS_NOTIME = 1 << 2, - NXT_FS_FLAGS_NODEV = 1 << 3, - NXT_FS_FLAGS_RDONLY = 1 << 4, + NXT_FS_FLAGS_NOSUID = 1 << 0, + NXT_FS_FLAGS_NOEXEC = 1 << 1, + NXT_FS_FLAGS_NOTIME = 1 << 2, + NXT_FS_FLAGS_NODEV = 1 << 3, + NXT_FS_FLAGS_RDONLY = 1 << 4, }; - struct nxt_fs_mount_s { - u_char *src; - u_char *dst; - nxt_fs_type_t type; - u_char *name; - nxt_fs_flags_t flags; - u_char *data; - nxt_uint_t builtin; /* 1-bit */ - nxt_uint_t deps; /* 1-bit */ + u_char *src; + u_char *dst; + nxt_fs_type_t type; + u_char *name; + nxt_fs_flags_t flags; + u_char *data; + nxt_uint_t builtin; /* 1-bit */ + nxt_uint_t deps; /* 1-bit */ }; - -nxt_int_t nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt); -void nxt_fs_unmount(const u_char *path); +nxt_int_t +nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt); +void +nxt_fs_unmount(const u_char *path); -#endif /* _NXT_FS_MOUNT_H_INCLUDED_ */ +#endif /* _NXT_FS_MOUNT_H_INCLUDED_ */ diff --git a/src/nxt_gmtime.c b/src/nxt_gmtime.c index 9c3fa190c..1384a0ef3 100644 --- a/src/nxt_gmtime.c +++ b/src/nxt_gmtime.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,16 +5,14 @@ #include - /* The function is valid for positive nxt_time_t only. */ void -nxt_gmtime(nxt_time_t s, struct tm *tm) -{ - nxt_int_t yday; - nxt_uint_t daytime, mday, mon, year, days, leap; +nxt_gmtime(nxt_time_t s, struct tm *tm) { + nxt_int_t yday; + nxt_uint_t daytime, mday, mon, year, days, leap; - days = (nxt_uint_t) (s / 86400); + days = (nxt_uint_t) (s / 86400); daytime = (nxt_uint_t) (s % 86400); /* January 1, 1970 was Thursday. */ @@ -59,7 +56,7 @@ nxt_gmtime(nxt_time_t s, struct tm *tm) if (yday >= 306) { year++; - mon -= 11; + mon -= 11; yday -= 306; } else { @@ -68,12 +65,12 @@ nxt_gmtime(nxt_time_t s, struct tm *tm) } tm->tm_mday = mday; - tm->tm_mon = mon; + tm->tm_mon = mon; tm->tm_year = year - 1900; tm->tm_yday = yday; - tm->tm_hour = daytime / 3600; - daytime %= 3600; - tm->tm_min = daytime / 60; - tm->tm_sec = daytime % 60; + tm->tm_hour = daytime / 3600; + daytime %= 3600; + tm->tm_min = daytime / 60; + tm->tm_sec = daytime % 60; } diff --git a/src/nxt_gnutls.c b/src/nxt_gnutls.c index aab4699c6..3c3113ad9 100644 --- a/src/nxt_gnutls.c +++ b/src/nxt_gnutls.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,65 +6,75 @@ #include #include - typedef struct { - gnutls_session_t session; + gnutls_session_t session; - uint8_t times; /* 2 bits */ - uint8_t no_shutdown; /* 1 bit */ + uint8_t times; /* 2 bits */ + uint8_t no_shutdown; /* 1 bit */ - nxt_buf_mem_t buffer; + nxt_buf_mem_t buffer; } nxt_gnutls_conn_t; - typedef struct { - gnutls_priority_t ciphers; - gnutls_certificate_credentials_t certificate; + gnutls_priority_t ciphers; + gnutls_certificate_credentials_t certificate; } nxt_gnutls_ctx_t; - #if (NXT_HAVE_GNUTLS_SET_TIME) -time_t nxt_gnutls_time(time_t *tp); +time_t +nxt_gnutls_time(time_t *tp); #endif -static nxt_int_t nxt_gnutls_server_init(nxt_ssltls_conf_t *conf); -static nxt_int_t nxt_gnutls_set_ciphers(nxt_ssltls_conf_t *conf); +static nxt_int_t +nxt_gnutls_server_init(nxt_ssltls_conf_t *conf); +static nxt_int_t +nxt_gnutls_set_ciphers(nxt_ssltls_conf_t *conf); -static void nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, +static void +nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, nxt_event_conn_t *c); -static void nxt_gnutls_session_cleanup(void *data); -static ssize_t nxt_gnutls_pull(gnutls_transport_ptr_t data, void *buf, - size_t size); -static ssize_t nxt_gnutls_push(gnutls_transport_ptr_t data, const void *buf, - size_t size); +static void +nxt_gnutls_session_cleanup(void *data); +static ssize_t +nxt_gnutls_pull(gnutls_transport_ptr_t data, void *buf, size_t size); +static ssize_t +nxt_gnutls_push(gnutls_transport_ptr_t data, const void *buf, size_t size); #if (NXT_HAVE_GNUTLS_VEC_PUSH) -static ssize_t nxt_gnutls_vec_push(gnutls_transport_ptr_t data, - const giovec_t *iov, int iovcnt); +static ssize_t +nxt_gnutls_vec_push(gnutls_transport_ptr_t data, const giovec_t *iov, + int iovcnt); #endif -static void nxt_gnutls_conn_handshake(nxt_thread_t *thr, void *obj, void *data); -static void nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data); -static ssize_t nxt_gnutls_conn_io_write_chunk(nxt_thread_t *thr, - nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static ssize_t nxt_gnutls_conn_io_send(nxt_event_conn_t *c, void *buf, - size_t size); -static void nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, - void *data); -static nxt_int_t nxt_gnutls_conn_test_error(nxt_thread_t *thr, - nxt_event_conn_t *c, ssize_t err, nxt_work_handler_t handler); -static void nxt_cdecl nxt_gnutls_conn_log_error(nxt_event_conn_t *c, - ssize_t err, const char *fmt, ...); -static nxt_uint_t nxt_gnutls_log_error_level(nxt_event_conn_t *c, ssize_t err); -static void nxt_cdecl nxt_gnutls_log_error(nxt_uint_t level, nxt_log_t *log, - int err, const char *fmt, ...); - - -const nxt_ssltls_lib_t nxt_gnutls_lib = { +static void +nxt_gnutls_conn_handshake(nxt_thread_t *thr, void *obj, void *data); +static void +nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data); +static ssize_t +nxt_gnutls_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, + nxt_buf_t *b, size_t limit); +static ssize_t +nxt_gnutls_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size); +static void +nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data); +static nxt_int_t +nxt_gnutls_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, ssize_t err, + nxt_work_handler_t handler); +static void nxt_cdecl +nxt_gnutls_conn_log_error(nxt_event_conn_t *c, ssize_t err, const char *fmt, + ...); +static nxt_uint_t +nxt_gnutls_log_error_level(nxt_event_conn_t *c, ssize_t err); +static void nxt_cdecl +nxt_gnutls_log_error(nxt_uint_t level, nxt_log_t *log, int err, const char *fmt, + ...); + + +const nxt_ssltls_lib_t nxt_gnutls_lib = { nxt_gnutls_server_init, NULL, }; -static nxt_event_conn_io_t nxt_gnutls_event_conn_io = { +static nxt_event_conn_io_t nxt_gnutls_event_conn_io = { NULL, NULL, @@ -82,12 +91,10 @@ static nxt_event_conn_io_t nxt_gnutls_event_conn_io = { nxt_gnutls_conn_io_shutdown, }; - static nxt_int_t -nxt_gnutls_start(void) -{ - int ret; - static nxt_bool_t started; +nxt_gnutls_start(void) { + int ret; + static nxt_bool_t started; if (nxt_fast_path(started)) { return NXT_OK; @@ -100,12 +107,12 @@ nxt_gnutls_start(void) ret = gnutls_global_init(); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, nxt_thread_log(), ret, - "gnutls_global_init() failed"); + "gnutls_global_init() failed"); return NXT_ERROR; } nxt_thread_log_error(NXT_LOG_INFO, "GnuTLS version: %s", - gnutls_check_version(NULL)); + gnutls_check_version(NULL)); #if (NXT_HAVE_GNUTLS_SET_TIME) gnutls_global_set_time_function(nxt_gnutls_time); @@ -120,10 +127,9 @@ nxt_gnutls_start(void) /* GnuTLS 2.12.0 */ time_t -nxt_gnutls_time(time_t *tp) -{ +nxt_gnutls_time(time_t *tp) { time_t t; - nxt_thread_t *thr; + nxt_thread_t *thr; thr = nxt_thread(); nxt_log_debug(thr->log, "gnutls time"); @@ -141,12 +147,11 @@ nxt_gnutls_time(time_t *tp) static nxt_int_t -nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) -{ - int ret; - char *certificate, *key, *ca_certificate; - nxt_thread_t *thr; - nxt_gnutls_ctx_t *ctx; +nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) { + int ret; + char *certificate, *key, *ca_certificate; + nxt_thread_t *thr; + nxt_gnutls_ctx_t *ctx; if (nxt_slow_path(nxt_gnutls_start() != NXT_OK)) { return NXT_ERROR; @@ -160,7 +165,7 @@ nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) return NXT_ERROR; } - conf->ctx = ctx; + conf->ctx = ctx; conf->conn_init = nxt_gnutls_conn_init; thr = nxt_thread(); @@ -168,19 +173,19 @@ nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) ret = gnutls_certificate_allocate_credentials(&ctx->certificate); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, thr->log, ret, - "gnutls_certificate_allocate_credentials() failed"); + "gnutls_certificate_allocate_credentials() failed"); return NXT_ERROR; } certificate = conf->certificate; - key = conf->certificate_key; + key = conf->certificate_key; ret = gnutls_certificate_set_x509_key_file(ctx->certificate, certificate, - key, GNUTLS_X509_FMT_PEM); + key, GNUTLS_X509_FMT_PEM); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, thr->log, ret, - "gnutls_certificate_set_x509_key_file(\"%s\", \"%s\") failed", - certificate, key); + "gnutls_certificate_set_x509_key_file(\"%s\", \"%s\") failed", + certificate, key); goto certificate_fail; } @@ -192,8 +197,7 @@ nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) ca_certificate = conf->ca_certificate; ret = gnutls_certificate_set_x509_trust_file(ctx->certificate, - ca_certificate, - GNUTLS_X509_FMT_PEM); + ca_certificate, GNUTLS_X509_FMT_PEM); if (ret < 0) { nxt_gnutls_log_error(NXT_LOG_ALERT, thr->log, ret, "gnutls_certificate_set_x509_trust_file(\"%s\") failed", @@ -217,48 +221,42 @@ nxt_gnutls_server_init(nxt_ssltls_conf_t *conf) return NXT_ERROR; } - static nxt_int_t -nxt_gnutls_set_ciphers(nxt_ssltls_conf_t *conf) -{ - int ret; - const char *ciphers; - const char *err; - nxt_gnutls_ctx_t *ctx; +nxt_gnutls_set_ciphers(nxt_ssltls_conf_t *conf) { + int ret; + const char *ciphers; + const char *err; + nxt_gnutls_ctx_t *ctx; ciphers = (conf->ciphers != NULL) ? conf->ciphers : "NORMAL:!COMP-DEFLATE"; - ctx = conf->ctx; + ctx = conf->ctx; ret = gnutls_priority_init(&ctx->ciphers, ciphers, &err); switch (ret) { - case GNUTLS_E_SUCCESS: return NXT_OK; case GNUTLS_E_INVALID_REQUEST: nxt_gnutls_log_error(NXT_LOG_ALERT, nxt_thread_log(), ret, - "gnutls_priority_init(\"%s\") failed at \"%s\"", - ciphers, err); + "gnutls_priority_init(\"%s\") failed at \"%s\"", ciphers, err); return NXT_ERROR; default: nxt_gnutls_log_error(NXT_LOG_ALERT, nxt_thread_log(), ret, - "gnutls_priority_init() failed"); + "gnutls_priority_init() failed"); return NXT_ERROR; } } - static void nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, - nxt_event_conn_t *c) -{ + nxt_event_conn_t *c) { int ret; gnutls_session_t sess; - nxt_gnutls_ctx_t *ctx; - nxt_gnutls_conn_t *ssltls; - nxt_mem_pool_cleanup_t *mpcl; + nxt_gnutls_ctx_t *ctx; + nxt_gnutls_conn_t *ssltls; + nxt_mem_pool_cleanup_t *mpcl; nxt_log_debug(c->socket.log, "gnutls conn init"); @@ -278,20 +276,20 @@ nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, ret = gnutls_init(&ssltls->session, GNUTLS_SERVER); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, c->socket.log, ret, - "gnutls_init() failed"); + "gnutls_init() failed"); goto fail; } - sess = ssltls->session; + sess = ssltls->session; mpcl->handler = nxt_gnutls_session_cleanup; - mpcl->data = ssltls; + mpcl->data = ssltls; ctx = conf->ctx; ret = gnutls_priority_set(sess, ctx->ciphers); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, c->socket.log, ret, - "gnutls_priority_set() failed"); + "gnutls_priority_set() failed"); goto fail; } @@ -302,10 +300,10 @@ nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, gnutls_record_disable_padding(sess); ret = gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, - ctx->certificate); + ctx->certificate); if (ret != GNUTLS_E_SUCCESS) { nxt_gnutls_log_error(NXT_LOG_ALERT, c->socket.log, ret, - "gnutls_credentials_set() failed"); + "gnutls_credentials_set() failed"); goto fail; } @@ -320,7 +318,7 @@ nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, gnutls_transport_set_vec_push_function(sess, nxt_gnutls_vec_push); #endif - c->io = &nxt_gnutls_event_conn_io; + c->io = &nxt_gnutls_event_conn_io; c->sendfile = NXT_CONN_SENDFILE_OFF; nxt_gnutls_conn_handshake(thr, c, c->socket.data); @@ -329,14 +327,12 @@ nxt_gnutls_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, fail: nxt_event_conn_io_handle(thr, c->read_work_queue, - c->read_state->error_handler, c, c->socket.data); + c->read_state->error_handler, c, c->socket.data); } - static void -nxt_gnutls_session_cleanup(void *data) -{ - nxt_gnutls_conn_t *ssltls; +nxt_gnutls_session_cleanup(void *data) { + nxt_gnutls_conn_t *ssltls; ssltls = data; @@ -347,15 +343,13 @@ nxt_gnutls_session_cleanup(void *data) gnutls_deinit(ssltls->session); } - static ssize_t -nxt_gnutls_pull(gnutls_transport_ptr_t data, void *buf, size_t size) -{ +nxt_gnutls_pull(gnutls_transport_ptr_t data, void *buf, size_t size) { ssize_t n; - nxt_thread_t *thr; - nxt_event_conn_t *c; + nxt_thread_t *thr; + nxt_event_conn_t *c; - c = data; + c = data; thr = nxt_thread(); n = thr->engine->event->io->recv(c, buf, size, 0); @@ -368,15 +362,13 @@ nxt_gnutls_pull(gnutls_transport_ptr_t data, void *buf, size_t size) return n; } - static ssize_t -nxt_gnutls_push(gnutls_transport_ptr_t data, const void *buf, size_t size) -{ +nxt_gnutls_push(gnutls_transport_ptr_t data, const void *buf, size_t size) { ssize_t n; - nxt_thread_t *thr; - nxt_event_conn_t *c; + nxt_thread_t *thr; + nxt_event_conn_t *c; - c = data; + c = data; thr = nxt_thread(); n = thr->engine->event->io->send(c, (u_char *) buf, size); @@ -396,13 +388,12 @@ nxt_gnutls_push(gnutls_transport_ptr_t data, const void *buf, size_t size) static ssize_t nxt_gnutls_vec_push(gnutls_transport_ptr_t data, const giovec_t *iov, - int iovcnt) -{ + int iovcnt) { ssize_t n; - nxt_thread_t *thr; - nxt_event_conn_t *c; + nxt_thread_t *thr; + nxt_event_conn_t *c; - c = data; + c = data; thr = nxt_thread(); /* @@ -423,14 +414,13 @@ nxt_gnutls_vec_push(gnutls_transport_ptr_t data, const giovec_t *iov, static void -nxt_gnutls_conn_handshake(nxt_thread_t *thr, void *obj, void *data) -{ +nxt_gnutls_conn_handshake(nxt_thread_t *thr, void *obj, void *data) { int err; nxt_int_t ret; - nxt_event_conn_t *c; - nxt_gnutls_conn_t *ssltls; + nxt_event_conn_t *c; + nxt_gnutls_conn_t *ssltls; - c = obj; + c = obj; ssltls = c->u.ssltls; nxt_log_debug(thr->log, "gnutls conn handshake: %d", ssltls->times); @@ -454,33 +444,29 @@ nxt_gnutls_conn_handshake(nxt_thread_t *thr, void *obj, void *data) nxt_gnutls_conn_log_error(c, err, "gnutls_handshake() failed"); nxt_event_conn_io_handle(thr, c->read_work_queue, - c->read_state->error_handler, c, data); + c->read_state->error_handler, c, data); - } else if (err == GNUTLS_E_AGAIN - && ssltls->times < 2 - && gnutls_record_get_direction(ssltls->session) == 0) - { + } else if (err == GNUTLS_E_AGAIN && ssltls->times < 2 + && gnutls_record_get_direction(ssltls->session) == 0) { ssltls->times++; } } - static void -nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data) -{ - ssize_t n; - nxt_buf_t *b; - nxt_int_t ret; - nxt_event_conn_t *c; - nxt_gnutls_conn_t *ssltls; - nxt_work_handler_t handler; +nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data) { + ssize_t n; + nxt_buf_t *b; + nxt_int_t ret; + nxt_event_conn_t *c; + nxt_gnutls_conn_t *ssltls; + nxt_work_handler_t handler; c = obj; nxt_log_debug(thr->log, "gnutls conn read"); handler = c->read_state->ready_handler; - b = c->read; + b = c->read; /* b == NULL is used to test descriptor readiness. */ @@ -488,31 +474,30 @@ nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data) ssltls = c->u.ssltls; n = gnutls_record_recv(ssltls->session, b->mem.free, - b->mem.end - b->mem.free); + b->mem.end - b->mem.free); nxt_log_debug(thr->log, "gnutls_record_recv(%d, %p, %uz): %z", - c->socket.fd, b->mem.free, b->mem.end - b->mem.free, n); + c->socket.fd, b->mem.free, b->mem.end - b->mem.free, n); if (n > 0) { /* c->socket.read_ready is kept. */ b->mem.free += n; - handler = c->read_state->ready_handler; + handler = c->read_state->ready_handler; } else if (n == 0) { handler = c->read_state->close_handler; } else { ret = nxt_gnutls_conn_test_error(thr, c, n, - nxt_gnutls_conn_io_read); + nxt_gnutls_conn_io_read); if (nxt_fast_path(ret != NXT_ERROR)) { return; } nxt_gnutls_conn_log_error(c, n, - "gnutls_record_recv(%d, %p, %uz): failed", - c->socket.fd, b->mem.free, - b->mem.end - b->mem.free); + "gnutls_record_recv(%d, %p, %uz): failed", c->socket.fd, + b->mem.free, b->mem.end - b->mem.free); handler = c->read_state->error_handler; } @@ -521,12 +506,10 @@ nxt_gnutls_conn_io_read(nxt_thread_t *thr, void *obj, void *data) nxt_event_conn_io_handle(thr, c->read_work_queue, handler, c, data); } - static ssize_t nxt_gnutls_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, - nxt_buf_t *b, size_t limit) -{ - nxt_gnutls_conn_t *ssltls; + nxt_buf_t *b, size_t limit) { + nxt_gnutls_conn_t *ssltls; nxt_log_debug(thr->log, "gnutls conn write chunk"); @@ -535,49 +518,44 @@ nxt_gnutls_conn_io_write_chunk(nxt_thread_t *thr, nxt_event_conn_t *c, return nxt_sendbuf_copy_coalesce(c, &ssltls->buffer, b, limit); } - static ssize_t -nxt_gnutls_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size) -{ +nxt_gnutls_conn_io_send(nxt_event_conn_t *c, void *buf, size_t size) { ssize_t n; nxt_int_t ret; - nxt_gnutls_conn_t *ssltls; + nxt_gnutls_conn_t *ssltls; ssltls = c->u.ssltls; n = gnutls_record_send(ssltls->session, buf, size); nxt_log_debug(c->socket.log, "gnutls_record_send(%d, %p, %uz): %z", - c->socket.fd, buf, size, n); + c->socket.fd, buf, size, n); if (n > 0) { return n; } ret = nxt_gnutls_conn_test_error(nxt_thread(), c, n, - nxt_event_conn_io_write); + nxt_event_conn_io_write); if (nxt_slow_path(ret == NXT_ERROR)) { nxt_gnutls_conn_log_error(c, n, - "gnutls_record_send(%d, %p, %uz): failed", - c->socket.fd, buf, size); + "gnutls_record_send(%d, %p, %uz): failed", c->socket.fd, buf, size); } return ret; } - static void -nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) -{ - int err; - nxt_int_t ret; - nxt_event_conn_t *c; - nxt_gnutls_conn_t *ssltls; - nxt_work_handler_t handler; - gnutls_close_request_t how; - - c = obj; +nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) { + int err; + nxt_int_t ret; + nxt_event_conn_t *c; + nxt_gnutls_conn_t *ssltls; + nxt_work_handler_t handler; + gnutls_close_request_t how; + + c = obj; ssltls = c->u.ssltls; if (ssltls->session == NULL || ssltls->no_shutdown) { @@ -599,24 +577,24 @@ nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) err = gnutls_bye(ssltls->session, how); - nxt_log_debug(c->socket.log, "gnutls_bye(%d, %d): %d", - c->socket.fd, how, err); + nxt_log_debug(c->socket.log, "gnutls_bye(%d, %d): %d", c->socket.fd, how, + err); if (err == GNUTLS_E_SUCCESS) { handler = c->write_state->close_handler; } else { ret = nxt_gnutls_conn_test_error(thr, c, err, - nxt_gnutls_conn_io_shutdown); + nxt_gnutls_conn_io_shutdown); - if (ret != NXT_ERROR) { /* ret == NXT_AGAIN */ + if (ret != NXT_ERROR) { /* ret == NXT_AGAIN */ c->socket.error_handler = c->read_state->error_handler; nxt_event_timer_add(thr->engine, &c->read_timer, 5000); return; } nxt_gnutls_conn_log_error(c, err, "gnutls_bye(%d) failed", - c->socket.fd); + c->socket.fd); handler = c->write_state->error_handler; } @@ -626,20 +604,17 @@ nxt_gnutls_conn_io_shutdown(nxt_thread_t *thr, void *obj, void *data) nxt_event_conn_io_handle(thr, c->write_work_queue, handler, c, data); } - static nxt_int_t nxt_gnutls_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, ssize_t err, - nxt_work_handler_t handler) -{ + nxt_work_handler_t handler) { int ret; - nxt_gnutls_conn_t *ssltls; + nxt_gnutls_conn_t *ssltls; switch (err) { - case GNUTLS_E_REHANDSHAKE: case GNUTLS_E_AGAIN: ssltls = c->u.ssltls; - ret = gnutls_record_get_direction(ssltls->session); + ret = gnutls_record_get_direction(ssltls->session); nxt_log_debug(thr->log, "gnutls_record_get_direction(): %d", ret); @@ -648,7 +623,7 @@ nxt_gnutls_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, ssize_t err, nxt_event_fd_block_write(thr->engine, &c->socket); - c->socket.read_ready = 0; + c->socket.read_ready = 0; c->socket.read_handler = handler; if (nxt_event_fd_is_disabled(c->socket.read)) { @@ -660,7 +635,7 @@ nxt_gnutls_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, ssize_t err, nxt_event_fd_block_read(thr->engine, &c->socket); - c->socket.write_ready = 0; + c->socket.write_ready = 0; c->socket.write_handler = handler; if (nxt_event_fd_is_disabled(c->socket.write)) { @@ -671,51 +646,45 @@ nxt_gnutls_conn_test_error(nxt_thread_t *thr, nxt_event_conn_t *c, ssize_t err, return NXT_AGAIN; default: - c->socket.error = 1000; /* Nonexistent errno code. */ + c->socket.error = 1000; /* Nonexistent errno code. */ return NXT_ERROR; } } - static void -nxt_gnutls_conn_log_error(nxt_event_conn_t *c, ssize_t err, - const char *fmt, ...) -{ - va_list args; - nxt_uint_t level; - u_char *p, msg[NXT_MAX_ERROR_STR]; +nxt_gnutls_conn_log_error(nxt_event_conn_t *c, ssize_t err, const char *fmt, + ...) { + va_list args; + nxt_uint_t level; + u_char *p, msg[NXT_MAX_ERROR_STR]; level = nxt_gnutls_log_error_level(c, err); if (nxt_log_level_enough(c->socket.log, level)) { - va_start(args, fmt); p = nxt_vsprintf(msg, msg + sizeof(msg), fmt, args); va_end(args); - nxt_log_error(level, c->socket.log, "%*s (%d: %s)", - p - msg, msg, err, gnutls_strerror(err)); + nxt_log_error(level, c->socket.log, "%*s (%d: %s)", p - msg, msg, err, + gnutls_strerror(err)); } } - static nxt_uint_t -nxt_gnutls_log_error_level(nxt_event_conn_t *c, ssize_t err) -{ - nxt_gnutls_conn_t *ssltls; +nxt_gnutls_log_error_level(nxt_event_conn_t *c, ssize_t err) { + nxt_gnutls_conn_t *ssltls; switch (err) { - - case GNUTLS_E_UNKNOWN_CIPHER_SUITE: /* -21 */ + case GNUTLS_E_UNKNOWN_CIPHER_SUITE: /* -21 */ /* Disable gnutls_bye(), because it returns GNUTLS_E_INTERNAL_ERROR. */ - ssltls = c->u.ssltls; + ssltls = c->u.ssltls; ssltls->no_shutdown = 1; /* Fall through. */ - case GNUTLS_E_UNEXPECTED_PACKET_LENGTH: /* -9 */ - c->socket.error = 1000; /* Nonexistent errno code. */ + case GNUTLS_E_UNEXPECTED_PACKET_LENGTH: /* -9 */ + c->socket.error = 1000; /* Nonexistent errno code. */ break; default: @@ -725,18 +694,16 @@ nxt_gnutls_log_error_level(nxt_event_conn_t *c, ssize_t err) return NXT_LOG_INFO; } - static void -nxt_gnutls_log_error(nxt_uint_t level, nxt_log_t *log, int err, - const char *fmt, ...) -{ - va_list args; - u_char *p, msg[NXT_MAX_ERROR_STR]; +nxt_gnutls_log_error(nxt_uint_t level, nxt_log_t *log, int err, const char *fmt, + ...) { + va_list args; + u_char *p, msg[NXT_MAX_ERROR_STR]; va_start(args, fmt); p = nxt_vsprintf(msg, msg + sizeof(msg), fmt, args); va_end(args); - nxt_log_error(level, log, "%*s (%d: %s)", - p - msg, msg, err, gnutls_strerror(err)); + nxt_log_error(level, log, "%*s (%d: %s)", p - msg, msg, err, + gnutls_strerror(err)); } diff --git a/src/nxt_h1proto.c b/src/nxt_h1proto.c index 48c2697bc..2ee508904 100644 --- a/src/nxt_h1proto.c +++ b/src/nxt_h1proto.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -19,129 +18,175 @@ */ #if (NXT_TLS) -static ssize_t nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c); -static void nxt_http_conn_test(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_http_conn_test(nxt_task_t *task, void *obj, void *data); #endif -static ssize_t nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, - void *data); -static nxt_int_t nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, +static ssize_t +nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_http_request_t *r); -static nxt_int_t nxt_h1p_header_buffer_test(nxt_task_t *task, - nxt_h1proto_t *h1p, nxt_conn_t *c, nxt_socket_conf_t *skcf); -static nxt_int_t nxt_h1p_connection(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static void nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r); -static void nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, - void *data); -static void nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r); -static void nxt_h1p_request_header_send(nxt_task_t *task, - nxt_http_request_t *r, nxt_work_handler_t body_handler, void *data); -static void nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *out); -static nxt_buf_t *nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *out); -static nxt_off_t nxt_h1p_request_body_bytes_sent(nxt_task_t *task, - nxt_http_proto_t proto); -static void nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r, +static nxt_int_t +nxt_h1p_header_buffer_test(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c, + nxt_socket_conf_t *skcf); +static nxt_int_t +nxt_h1p_connection(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data); +static void +nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r); +static void +nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r); +static void +nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, + nxt_work_handler_t body_handler, void *data); +static void +nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); +static nxt_buf_t * +nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); +static nxt_off_t +nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto); +static void +nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last); -static void nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, - void *data); -static void nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj, - void *data); -nxt_inline void nxt_h1p_request_error(nxt_task_t *task, nxt_h1proto_t *h1p, +static void +nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj, void *data); +nxt_inline void +nxt_h1p_request_error(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_http_request_t *r); -static void nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto, +static void +nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto, nxt_socket_conf_joint_t *joint); -static void nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data); -static nxt_msec_t nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data); -static void nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, - nxt_conn_t *c); -static void nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, - void *data); -static void nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj, - void *data); -static nxt_msec_t nxt_h1p_idle_response_timer_value(nxt_conn_t *c, +static void +nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data); +static nxt_msec_t +nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data); +static void +nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c); +static void +nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj, void *data); +static nxt_msec_t +nxt_h1p_idle_response_timer_value(nxt_conn_t *c, uintptr_t data); +static void +nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data); + +static void +nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer); +static void +nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer); +static nxt_int_t +nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target); +static void +nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer); +static ssize_t +nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm); +static void +nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer); +static void +nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, + nxt_buf_t *out); +static void +nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data); +static nxt_msec_t +nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data); +static void +nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer); +static void +nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_h1p_peer_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data); -static void nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data); - -static void nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer); -static void nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer); -static nxt_int_t nxt_h1p_peer_request_target(nxt_http_request_t *r, - nxt_str_t *target); -static void nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer); -static ssize_t nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, - void *data); -static nxt_int_t nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, - nxt_buf_mem_t *bm); -static void nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer); -static void nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, nxt_buf_t *out); -static void nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data); -static nxt_msec_t nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data); -static void nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer); -static void nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data); -static nxt_int_t nxt_h1p_peer_transfer_encoding(void *ctx, - nxt_http_field_t *field, uintptr_t data); #if (NXT_TLS) -static const nxt_conn_state_t nxt_http_idle_state; -static const nxt_conn_state_t nxt_h1p_shutdown_state; +static const nxt_conn_state_t nxt_http_idle_state; +static const nxt_conn_state_t nxt_h1p_shutdown_state; #endif -static const nxt_conn_state_t nxt_h1p_idle_state; -static const nxt_conn_state_t nxt_h1p_header_parse_state; -static const nxt_conn_state_t nxt_h1p_read_body_state; -static const nxt_conn_state_t nxt_h1p_request_send_state; -static const nxt_conn_state_t nxt_h1p_timeout_response_state; -static const nxt_conn_state_t nxt_h1p_keepalive_state; -static const nxt_conn_state_t nxt_h1p_close_state; -static const nxt_conn_state_t nxt_h1p_peer_connect_state; -static const nxt_conn_state_t nxt_h1p_peer_header_send_state; -static const nxt_conn_state_t nxt_h1p_peer_header_body_send_state; -static const nxt_conn_state_t nxt_h1p_peer_header_read_state; -static const nxt_conn_state_t nxt_h1p_peer_header_read_timer_state; -static const nxt_conn_state_t nxt_h1p_peer_read_state; -static const nxt_conn_state_t nxt_h1p_peer_close_state; - - -const nxt_http_proto_table_t nxt_http_proto[3] = { +static const nxt_conn_state_t nxt_h1p_idle_state; +static const nxt_conn_state_t nxt_h1p_header_parse_state; +static const nxt_conn_state_t nxt_h1p_read_body_state; +static const nxt_conn_state_t nxt_h1p_request_send_state; +static const nxt_conn_state_t nxt_h1p_timeout_response_state; +static const nxt_conn_state_t nxt_h1p_keepalive_state; +static const nxt_conn_state_t nxt_h1p_close_state; +static const nxt_conn_state_t nxt_h1p_peer_connect_state; +static const nxt_conn_state_t nxt_h1p_peer_header_send_state; +static const nxt_conn_state_t nxt_h1p_peer_header_body_send_state; +static const nxt_conn_state_t nxt_h1p_peer_header_read_state; +static const nxt_conn_state_t nxt_h1p_peer_header_read_timer_state; +static const nxt_conn_state_t nxt_h1p_peer_read_state; +static const nxt_conn_state_t nxt_h1p_peer_close_state; + + +const nxt_http_proto_table_t nxt_http_proto[3] = { /* NXT_HTTP_PROTO_H1 */ { - .body_read = nxt_h1p_request_body_read, - .local_addr = nxt_h1p_request_local_addr, - .header_send = nxt_h1p_request_header_send, - .send = nxt_h1p_request_send, - .body_bytes_sent = nxt_h1p_request_body_bytes_sent, - .discard = nxt_h1p_request_discard, - .close = nxt_h1p_request_close, + .body_read = nxt_h1p_request_body_read, + .local_addr = nxt_h1p_request_local_addr, + .header_send = nxt_h1p_request_header_send, + .send = nxt_h1p_request_send, + .body_bytes_sent = nxt_h1p_request_body_bytes_sent, + .discard = nxt_h1p_request_discard, + .close = nxt_h1p_request_close, .peer_connect = nxt_h1p_peer_connect, .peer_header_send = nxt_h1p_peer_header_send, @@ -149,87 +194,81 @@ const nxt_http_proto_table_t nxt_http_proto[3] = { .peer_read = nxt_h1p_peer_read, .peer_close = nxt_h1p_peer_close, - .ws_frame_start = nxt_h1p_websocket_frame_start, + .ws_frame_start = nxt_h1p_websocket_frame_start, }, /* NXT_HTTP_PROTO_H2 */ /* NXT_HTTP_PROTO_DEVNULL */ }; -static nxt_lvlhsh_t nxt_h1p_fields_hash; - -static nxt_http_field_proc_t nxt_h1p_fields[] = { - { nxt_string("Connection"), &nxt_h1p_connection, 0 }, - { nxt_string("Upgrade"), &nxt_h1p_upgrade, 0 }, - { nxt_string("Sec-WebSocket-Key"), &nxt_h1p_websocket_key, 0 }, - { nxt_string("Sec-WebSocket-Version"), - &nxt_h1p_websocket_version, 0 }, - { nxt_string("Transfer-Encoding"), &nxt_h1p_transfer_encoding, 0 }, - - { nxt_string("Host"), &nxt_http_request_host, 0 }, - { nxt_string("Cookie"), &nxt_http_request_field, - offsetof(nxt_http_request_t, cookie) }, - { nxt_string("Referer"), &nxt_http_request_field, - offsetof(nxt_http_request_t, referer) }, - { nxt_string("User-Agent"), &nxt_http_request_field, - offsetof(nxt_http_request_t, user_agent) }, - { nxt_string("Content-Type"), &nxt_http_request_field, - offsetof(nxt_http_request_t, content_type) }, - { nxt_string("Content-Length"), &nxt_http_request_content_length, 0 }, - { nxt_string("Authorization"), &nxt_http_request_field, - offsetof(nxt_http_request_t, authorization) }, +static nxt_lvlhsh_t nxt_h1p_fields_hash; + +static nxt_http_field_proc_t nxt_h1p_fields[] = { + {nxt_string("Connection"), &nxt_h1p_connection, 0}, + {nxt_string("Upgrade"), &nxt_h1p_upgrade, 0}, + {nxt_string("Sec-WebSocket-Key"), &nxt_h1p_websocket_key, 0}, + {nxt_string("Sec-WebSocket-Version"), &nxt_h1p_websocket_version, 0}, + {nxt_string("Transfer-Encoding"), &nxt_h1p_transfer_encoding, 0}, + + {nxt_string("Host"), &nxt_http_request_host, 0}, + {nxt_string("Cookie"), &nxt_http_request_field, + offsetof(nxt_http_request_t, cookie)}, + {nxt_string("Referer"), &nxt_http_request_field, + offsetof(nxt_http_request_t, referer)}, + {nxt_string("User-Agent"), &nxt_http_request_field, + offsetof(nxt_http_request_t, user_agent)}, + {nxt_string("Content-Type"), &nxt_http_request_field, + offsetof(nxt_http_request_t, content_type)}, + {nxt_string("Content-Length"), &nxt_http_request_content_length, 0}, + {nxt_string("Authorization"), &nxt_http_request_field, + offsetof(nxt_http_request_t, authorization)}, }; -static nxt_lvlhsh_t nxt_h1p_peer_fields_hash; +static nxt_lvlhsh_t nxt_h1p_peer_fields_hash; -static nxt_http_field_proc_t nxt_h1p_peer_fields[] = { - { nxt_string("Connection"), &nxt_http_proxy_skip, 0 }, - { nxt_string("Transfer-Encoding"), &nxt_h1p_peer_transfer_encoding, 0 }, - { nxt_string("Server"), &nxt_http_proxy_skip, 0 }, - { nxt_string("Date"), &nxt_http_proxy_date, 0 }, - { nxt_string("Content-Length"), &nxt_http_proxy_content_length, 0 }, +static nxt_http_field_proc_t nxt_h1p_peer_fields[] = { + {nxt_string("Connection"), &nxt_http_proxy_skip, 0}, + {nxt_string("Transfer-Encoding"), &nxt_h1p_peer_transfer_encoding, 0}, + {nxt_string("Server"), &nxt_http_proxy_skip, 0}, + {nxt_string("Date"), &nxt_http_proxy_date, 0}, + {nxt_string("Content-Length"), &nxt_http_proxy_content_length, 0}, }; - nxt_int_t -nxt_h1p_init(nxt_task_t *task) -{ - nxt_int_t ret; +nxt_h1p_init(nxt_task_t *task) { + nxt_int_t ret; - ret = nxt_http_fields_hash(&nxt_h1p_fields_hash, - nxt_h1p_fields, nxt_nitems(nxt_h1p_fields)); + ret = nxt_http_fields_hash(&nxt_h1p_fields_hash, nxt_h1p_fields, + nxt_nitems(nxt_h1p_fields)); if (nxt_fast_path(ret == NXT_OK)) { ret = nxt_http_fields_hash(&nxt_h1p_peer_fields_hash, - nxt_h1p_peer_fields, - nxt_nitems(nxt_h1p_peer_fields)); + nxt_h1p_peer_fields, nxt_nitems(nxt_h1p_peer_fields)); } return ret; } - void -nxt_http_conn_init(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_socket_conf_t *skcf; - nxt_event_engine_t *engine; - nxt_listen_event_t *lev; - nxt_socket_conf_joint_t *joint; - - c = obj; +nxt_http_conn_init(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_socket_conf_t *skcf; + nxt_event_engine_t *engine; + nxt_listen_event_t *lev; + nxt_socket_conf_joint_t *joint; + + c = obj; lev = data; nxt_debug(task, "http conn init"); - joint = lev->socket.data; - skcf = joint->socket_conf; + joint = lev->socket.data; + skcf = joint->socket_conf; c->local = skcf->sockaddr; - engine = task->thread->engine; - c->read_work_queue = &engine->fast_work_queue; + engine = task->thread->engine; + c->read_work_queue = &engine->fast_work_queue; c->write_work_queue = &engine->fast_work_queue; c->read_state = &nxt_h1p_idle_state; @@ -246,9 +285,7 @@ nxt_http_conn_init(nxt_task_t *task, void *obj, void *data) #if (NXT_TLS) -static const nxt_conn_state_t nxt_http_idle_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_http_idle_state nxt_aligned(64) = { .ready_handler = nxt_http_conn_test, .close_handler = nxt_h1p_conn_close, .error_handler = nxt_h1p_conn_error, @@ -256,18 +293,16 @@ static const nxt_conn_state_t nxt_http_idle_state .io_read_handler = nxt_http_idle_io_read_handler, .timer_handler = nxt_h1p_idle_timeout, - .timer_value = nxt_h1p_conn_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), + .timer_value = nxt_h1p_conn_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), }; - static ssize_t -nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) { size_t size; ssize_t n; - nxt_buf_t *b; - nxt_socket_conf_joint_t *joint; + nxt_buf_t *b; + nxt_socket_conf_joint_t *joint; joint = c->listen->socket.data; @@ -306,24 +341,22 @@ nxt_http_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) return n; } - static void -nxt_http_conn_test(nxt_task_t *task, void *obj, void *data) -{ - u_char *p; - nxt_buf_t *b; - nxt_conn_t *c; - nxt_tls_conf_t *tls; - nxt_event_engine_t *engine; - nxt_socket_conf_joint_t *joint; +nxt_http_conn_test(nxt_task_t *task, void *obj, void *data) { + u_char *p; + nxt_buf_t *b; + nxt_conn_t *c; + nxt_tls_conf_t *tls; + nxt_event_engine_t *engine; + nxt_socket_conf_joint_t *joint; c = obj; nxt_debug(task, "h1p conn https test"); engine = task->thread->engine; - b = c->read; - p = b->mem.pos; + b = c->read; + p = b->mem.pos; c->read_state = &nxt_h1p_idle_state; @@ -339,7 +372,7 @@ nxt_http_conn_test(nxt_task_t *task, void *obj, void *data) #if (NXT_DEBUG) if (nxt_buf_mem_used_size(&b->mem) >= 11) { u_char major, minor; - const char *protocol; + const char *protocol; major = p[9]; minor = p[10]; @@ -349,9 +382,9 @@ nxt_http_conn_test(nxt_task_t *task, void *obj, void *data) protocol = "SSLv"; } else { - protocol = "TLSv"; - major -= 2; - minor -= 1; + protocol = "TLSv"; + major -= 2; + minor -= 1; } nxt_debug(task, "SSL/TLS: %s%ud.%ud", protocol, major, minor); @@ -381,29 +414,25 @@ nxt_http_conn_test(nxt_task_t *task, void *obj, void *data) #endif -static const nxt_conn_state_t nxt_h1p_idle_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_idle_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_proto_init, .close_handler = nxt_h1p_conn_close, .error_handler = nxt_h1p_conn_error, .io_read_handler = nxt_h1p_idle_io_read_handler, - .timer_handler = nxt_h1p_idle_timeout, - .timer_value = nxt_h1p_conn_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), + .timer_handler = nxt_h1p_idle_timeout, + .timer_value = nxt_h1p_conn_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), .timer_autoreset = 1, }; - static ssize_t -nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) { size_t size; ssize_t n; - nxt_buf_t *b; - nxt_socket_conf_joint_t *joint; + nxt_buf_t *b; + nxt_socket_conf_joint_t *joint; joint = c->listen->socket.data; @@ -441,12 +470,10 @@ nxt_h1p_idle_io_read_handler(nxt_task_t *task, nxt_conn_t *c) return n; } - static void -nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_h1proto_t *h1p; +nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_h1proto_t *h1p; c = obj; @@ -459,23 +486,21 @@ nxt_h1p_conn_proto_init(nxt_task_t *task, void *obj, void *data) } c->socket.data = h1p; - h1p->conn = c; + h1p->conn = c; nxt_h1p_conn_request_init(task, c, h1p); } - static void -nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_socket_conf_t *skcf; - nxt_http_request_t *r; - nxt_socket_conf_joint_t *joint; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_socket_conf_t *skcf; + nxt_http_request_t *r; + nxt_socket_conf_joint_t *joint; - c = obj; + c = obj; h1p = data; nxt_debug(task, "h1p conn request init"); @@ -486,7 +511,7 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) if (nxt_fast_path(r != NULL)) { h1p->request = r; - r->proto.h1 = h1p; + r->proto.h1 = h1p; /* r->protocol = NXT_HTTP_PROTO_H1 is done by zeroing. */ r->remote = c->remote; @@ -495,10 +520,10 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) r->tls = (c->u.tls != NULL); #endif - r->task = c->task; - task = &r->task; - c->socket.task = task; - c->read_timer.task = task; + r->task = c->task; + task = &r->task; + c->socket.task = task; + c->read_timer.task = task; c->write_timer.task = task; ret = nxt_http_parse_request_init(&h1p->parser, r->mem_pool); @@ -507,8 +532,8 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) joint = c->listen->socket.data; joint->count++; - r->conf = joint; - skcf = joint->socket_conf; + r->conf = joint; + skcf = joint->socket_conf; r->log_route = skcf->log_route; if (c->local == NULL) { @@ -531,30 +556,25 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) nxt_h1p_closing(task, c); } - -static const nxt_conn_state_t nxt_h1p_header_parse_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_header_parse_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_request_header_parse, .close_handler = nxt_h1p_conn_request_error, .error_handler = nxt_h1p_conn_request_error, .timer_handler = nxt_h1p_conn_request_timeout, - .timer_value = nxt_h1p_conn_request_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, header_read_timeout), + .timer_value = nxt_h1p_conn_request_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, header_read_timeout), }; - static void -nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_conn_t *c; - nxt_h1proto_t *h1p; + nxt_conn_t *c; + nxt_h1proto_t *h1p; nxt_http_status_t status; - nxt_http_request_t *r; + nxt_http_request_t *r; - c = obj; + c = obj; h1p = data; nxt_debug(task, "h1p conn header parse"); @@ -570,7 +590,6 @@ nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data) r = h1p->request; switch (ret) { - case NXT_DONE: /* * By default the keepalive mode is disabled in HTTP/1.0 and @@ -580,18 +599,17 @@ nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data) h1p->keepalive = (h1p->parser.version.s.minor != '0'); r->request_line.start = h1p->parser.method.start; - r->request_line.length = h1p->parser.request_line_end - - r->request_line.start; + r->request_line.length + = h1p->parser.request_line_end - r->request_line.start; if (nxt_slow_path(r->log_route)) { nxt_log(task, NXT_LOG_NOTICE, "http request line \"%V\"", - &r->request_line); + &r->request_line); } ret = nxt_h1p_header_process(task, h1p, r); if (nxt_fast_path(ret == NXT_OK)) { - #if (NXT_TLS) if (c->u.tls == NULL && r->conf->socket_conf->tls != NULL) { status = NXT_HTTP_TO_HTTPS; @@ -645,27 +663,25 @@ nxt_h1p_conn_request_header_parse(nxt_task_t *task, void *obj, void *data) nxt_http_request_error(task, r, status); } - static nxt_int_t nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, - nxt_http_request_t *r) -{ - u_char *m; - nxt_int_t ret; + nxt_http_request_t *r) { + u_char *m; + nxt_int_t ret; - r->target.start = h1p->parser.target_start; + r->target.start = h1p->parser.target_start; r->target.length = h1p->parser.target_end - h1p->parser.target_start; r->quoted_target = h1p->parser.quoted_target; if (h1p->parser.version.ui64 != 0) { - r->version.start = h1p->parser.version.str; + r->version.start = h1p->parser.version.str; r->version.length = sizeof(h1p->parser.version.str); } r->method = &h1p->parser.method; - r->path = &h1p->parser.path; - r->args = &h1p->parser.args; + r->path = &h1p->parser.path; + r->args = &h1p->parser.args; r->fields = h1p->parser.fields; @@ -677,11 +693,8 @@ nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, if (h1p->connection_upgrade && h1p->upgrade_websocket) { m = h1p->parser.method.start; - if (nxt_slow_path(h1p->parser.method.length != 3 - || m[0] != 'G' - || m[1] != 'E' - || m[2] != 'T')) - { + if (nxt_slow_path(h1p->parser.method.length != 3 || m[0] != 'G' + || m[1] != 'E' || m[2] != 'T')) { nxt_log(task, NXT_LOG_INFO, "h1p upgrade: bad method"); return NXT_HTTP_BAD_REQUEST; @@ -695,14 +708,14 @@ nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, if (nxt_slow_path(h1p->websocket_key == NULL)) { nxt_log(task, NXT_LOG_INFO, - "h1p upgrade: bad or absent websocket key"); + "h1p upgrade: bad or absent websocket key"); return NXT_HTTP_BAD_REQUEST; } if (nxt_slow_path(h1p->websocket_version_ok == 0)) { nxt_log(task, NXT_LOG_INFO, - "h1p upgrade: bad or absent websocket version"); + "h1p upgrade: bad or absent websocket version"); return NXT_HTTP_UPGRADE_REQUIRED; } @@ -713,13 +726,11 @@ nxt_h1p_header_process(nxt_task_t *task, nxt_h1proto_t *h1p, return ret; } - static nxt_int_t nxt_h1p_header_buffer_test(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c, - nxt_socket_conf_t *skcf) -{ + nxt_socket_conf_t *skcf) { size_t size, used; - nxt_buf_t *in, *b; + nxt_buf_t *in, *b; in = c->read; @@ -738,7 +749,7 @@ nxt_h1p_header_buffer_test(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c, b->mem.free = nxt_cpymem(b->mem.pos, in->mem.pos, used); - in->next = h1p->buffers; + in->next = h1p->buffers; h1p->buffers = in; h1p->nbuffers++; @@ -748,56 +759,46 @@ nxt_h1p_header_buffer_test(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c, return NXT_OK; } - static nxt_int_t -nxt_h1p_connection(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ - nxt_http_request_t *r; +nxt_h1p_connection(void *ctx, nxt_http_field_t *field, uintptr_t data) { + nxt_http_request_t *r; - r = ctx; + r = ctx; field->hopbyhop = 1; if (field->value_length == 5 - && nxt_memcasecmp(field->value, "close", 5) == 0) - { + && nxt_memcasecmp(field->value, "close", 5) == 0) { r->proto.h1->keepalive = 0; } else if (field->value_length == 10 - && nxt_memcasecmp(field->value, "keep-alive", 10) == 0) - { + && nxt_memcasecmp(field->value, "keep-alive", 10) == 0) { r->proto.h1->keepalive = 1; } else if (field->value_length == 7 - && nxt_memcasecmp(field->value, "upgrade", 7) == 0) - { + && nxt_memcasecmp(field->value, "upgrade", 7) == 0) { r->proto.h1->connection_upgrade = 1; } return NXT_OK; } - static nxt_int_t -nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ - nxt_http_request_t *r; +nxt_h1p_upgrade(void *ctx, nxt_http_field_t *field, uintptr_t data) { + nxt_http_request_t *r; r = ctx; if (field->value_length == 9 - && nxt_memcasecmp(field->value, "websocket", 9) == 0) - { + && nxt_memcasecmp(field->value, "websocket", 9) == 0) { r->proto.h1->upgrade_websocket = 1; } return NXT_OK; } - static nxt_int_t -nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ - nxt_http_request_t *r; +nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, uintptr_t data) { + nxt_http_request_t *r; r = ctx; @@ -808,42 +809,35 @@ nxt_h1p_websocket_key(void *ctx, nxt_http_field_t *field, uintptr_t data) return NXT_OK; } - static nxt_int_t -nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ - nxt_http_request_t *r; +nxt_h1p_websocket_version(void *ctx, nxt_http_field_t *field, uintptr_t data) { + nxt_http_request_t *r; r = ctx; - if (field->value_length == 2 - && field->value[0] == '1' && field->value[1] == '3') - { + if (field->value_length == 2 && field->value[0] == '1' + && field->value[1] == '3') { r->proto.h1->websocket_version_ok = 1; } return NXT_OK; } - static nxt_int_t -nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ +nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data) { nxt_http_te_t te; - nxt_http_request_t *r; + nxt_http_request_t *r; - r = ctx; - field->skip = 1; + r = ctx; + field->skip = 1; field->hopbyhop = 1; - if (field->value_length == 7 - && memcmp(field->value, "chunked", 7) == 0) - { + if (field->value_length == 7 && memcmp(field->value, "chunked", 7) == 0) { if (r->chunked_field != NULL) { return NXT_HTTP_BAD_REQUEST; } - te = NXT_HTTP_TE_CHUNKED; + te = NXT_HTTP_TE_CHUNKED; r->chunked_field = field; } else { @@ -855,28 +849,25 @@ nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data) return NXT_OK; } - static void -nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) -{ +nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) { size_t size, body_length, body_buffer_size, body_rest; ssize_t res; - nxt_buf_t *in, *b, *out, *chunk; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_socket_conf_t *skcf; + nxt_buf_t *in, *b, *out, *chunk; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_socket_conf_t *skcf; nxt_http_status_t status; static const nxt_str_t tmp_name_pattern = nxt_string("/req-XXXXXXXX"); - h1p = r->proto.h1; + h1p = r->proto.h1; skcf = r->conf->socket_conf; - nxt_debug(task, "h1p request body read %O te:%d", - r->content_length_n, h1p->transfer_encoding); + nxt_debug(task, "h1p request body read %O te:%d", r->content_length_n, + h1p->transfer_encoding); switch (h1p->transfer_encoding) { - case NXT_HTTP_TE_CHUNKED: if (!skcf->chunked_transform) { status = NXT_HTTP_LENGTH_REQUIRED; @@ -888,7 +879,7 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) goto error; } - r->chunked = 1; + r->chunked = 1; h1p->chunked_parse.mem_pool = r->mem_pool; break; @@ -901,9 +892,8 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) break; } - if (!r->chunked && - (r->content_length_n == -1 || r->content_length_n == 0)) - { + if (!r->chunked + && (r->content_length_n == -1 || r->content_length_n == 0)) { goto ready; } @@ -912,15 +902,14 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) body_buffer_size = nxt_min(skcf->body_buffer_size, body_length); if (body_length > body_buffer_size) { - nxt_str_t *tmp_path, tmp_name; + nxt_str_t *tmp_path, tmp_name; tmp_path = &skcf->body_temp_path; tmp_name.length = tmp_path->length + tmp_name_pattern.length; b = nxt_buf_file_alloc(r->mem_pool, - body_buffer_size + sizeof(nxt_file_t) - + tmp_name.length + 1, 0); + body_buffer_size + sizeof(nxt_file_t) + tmp_name.length + 1, 0); if (nxt_slow_path(b == NULL)) { status = NXT_HTTP_INTERNAL_SERVER_ERROR; goto error; @@ -930,17 +919,17 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) memcpy(tmp_name.start, tmp_path->start, tmp_path->length); memcpy(tmp_name.start + tmp_path->length, tmp_name_pattern.start, - tmp_name_pattern.length); + tmp_name_pattern.length); tmp_name.start[tmp_name.length] = '\0'; b->file = (nxt_file_t *) b->mem.start; nxt_memzero(b->file, sizeof(nxt_file_t)); - b->file->fd = -1; + b->file->fd = -1; b->file->size = body_length; b->mem.start += sizeof(nxt_file_t) + tmp_name.length + 1; - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start; b->file->fd = mkstemp((char *) tmp_name.start); if (nxt_slow_path(b->file->fd == -1)) { @@ -950,8 +939,8 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) goto error; } - nxt_debug(task, "create body tmp file \"%V\", %d", - &tmp_name, b->file->fd); + nxt_debug(task, "create body tmp file \"%V\", %d", &tmp_name, + b->file->fd); unlink((char *) tmp_name.start); @@ -1009,7 +998,7 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) } else { size = nxt_min(size, body_length); - res = nxt_fd_write(b->file->fd, in->mem.pos, size); + res = nxt_fd_write(b->file->fd, in->mem.pos, size); if (nxt_slow_path(res < (ssize_t) size)) { status = NXT_HTTP_INTERNAL_SERVER_ERROR; goto error; @@ -1018,27 +1007,27 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) b->file_end += size; in->mem.pos += size; - body_rest -= size; + body_rest -= size; } } else { - size = nxt_min(body_buffer_size, size); + size = nxt_min(body_buffer_size, size); b->mem.free = nxt_cpymem(b->mem.free, in->mem.pos, size); in->mem.pos += size; - body_rest -= size; + body_rest -= size; } } nxt_debug(task, "h1p body rest: %uz", body_rest); if (body_rest != 0) { - in->next = h1p->buffers; + in->next = h1p->buffers; h1p->buffers = in; h1p->nbuffers++; - c = h1p->conn; - c->read = b; + c = h1p->conn; + c->read = b; c->read_state = &nxt_h1p_read_body_state; nxt_conn_read(task->thread->engine, c); @@ -1047,9 +1036,9 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) if (nxt_buf_is_file(b)) { b->mem.start = NULL; - b->mem.end = NULL; - b->mem.pos = NULL; - b->mem.free = NULL; + b->mem.end = NULL; + b->mem.pos = NULL; + b->mem.free = NULL; } ready: @@ -1065,39 +1054,34 @@ nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r) nxt_http_request_error(task, r, status); } - -static const nxt_conn_state_t nxt_h1p_read_body_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_read_body_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_request_body_read, .close_handler = nxt_h1p_conn_request_error, .error_handler = nxt_h1p_conn_request_error, - .timer_handler = nxt_h1p_conn_request_timeout, - .timer_value = nxt_h1p_conn_request_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, body_read_timeout), + .timer_handler = nxt_h1p_conn_request_timeout, + .timer_value = nxt_h1p_conn_request_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, body_read_timeout), .timer_autoreset = 1, }; - static void -nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) { size_t size, body_rest; ssize_t res; - nxt_buf_t *b, *out, *chunk; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_socket_conf_t *skcf; - nxt_http_request_t *r; - nxt_event_engine_t *engine; + nxt_buf_t *b, *out, *chunk; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_socket_conf_t *skcf; + nxt_http_request_t *r; + nxt_event_engine_t *engine; - c = obj; + c = obj; h1p = data; nxt_debug(task, "h1p conn request body read"); - r = h1p->request; + r = h1p->request; skcf = r->conf->socket_conf; engine = task->thread->engine; @@ -1105,7 +1089,6 @@ nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) b = c->read; if (nxt_buf_is_file(b)) { - if (r->chunked) { body_rest = 1; @@ -1123,7 +1106,7 @@ nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) for (chunk = out; chunk != NULL; chunk = chunk->next) { size = nxt_buf_mem_used_size(&chunk->mem); - res = nxt_fd_write(b->file->fd, chunk->mem.pos, size); + res = nxt_fd_write(b->file->fd, chunk->mem.pos, size); if (nxt_slow_path(res < (ssize_t) size)) { nxt_h1p_request_error(task, h1p, r); return; @@ -1154,7 +1137,7 @@ nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) } b->file_end += size; - body_rest -= res; + body_rest -= res; b->mem.pos += size; @@ -1183,9 +1166,9 @@ nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) } else { if (nxt_buf_is_file(b)) { b->mem.start = NULL; - b->mem.end = NULL; - b->mem.pos = NULL; - b->mem.free = NULL; + b->mem.end = NULL; + b->mem.pos = NULL; + b->mem.free = NULL; } c->read = NULL; @@ -1194,27 +1177,23 @@ nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r) -{ +nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r) { r->local = nxt_conn_local_addr(task, r->proto.h1->conn); } - -#define NXT_HTTP_LAST_INFORMATIONAL \ +#define NXT_HTTP_LAST_INFORMATIONAL \ (NXT_HTTP_CONTINUE + nxt_nitems(nxt_http_informational) - 1) -static const nxt_str_t nxt_http_informational[] = { +static const nxt_str_t nxt_http_informational[] = { nxt_string("HTTP/1.1 100 Continue\r\n"), nxt_string("HTTP/1.1 101 Switching Protocols\r\n"), }; -#define NXT_HTTP_LAST_SUCCESS \ - (NXT_HTTP_OK + nxt_nitems(nxt_http_success) - 1) +#define NXT_HTTP_LAST_SUCCESS (NXT_HTTP_OK + nxt_nitems(nxt_http_success) - 1) -static const nxt_str_t nxt_http_success[] = { +static const nxt_str_t nxt_http_success[] = { nxt_string("HTTP/1.1 200 OK\r\n"), nxt_string("HTTP/1.1 201 Created\r\n"), nxt_string("HTTP/1.1 202 Accepted\r\n"), @@ -1225,10 +1204,10 @@ static const nxt_str_t nxt_http_success[] = { }; -#define NXT_HTTP_LAST_REDIRECTION \ +#define NXT_HTTP_LAST_REDIRECTION \ (NXT_HTTP_MULTIPLE_CHOICES + nxt_nitems(nxt_http_redirection) - 1) -static const nxt_str_t nxt_http_redirection[] = { +static const nxt_str_t nxt_http_redirection[] = { nxt_string("HTTP/1.1 300 Multiple Choices\r\n"), nxt_string("HTTP/1.1 301 Moved Permanently\r\n"), nxt_string("HTTP/1.1 302 Found\r\n"), @@ -1239,10 +1218,10 @@ static const nxt_str_t nxt_http_redirection[] = { }; -#define NXT_HTTP_LAST_CLIENT_ERROR \ +#define NXT_HTTP_LAST_CLIENT_ERROR \ (NXT_HTTP_BAD_REQUEST + nxt_nitems(nxt_http_client_error) - 1) -static const nxt_str_t nxt_http_client_error[] = { +static const nxt_str_t nxt_http_client_error[] = { nxt_string("HTTP/1.1 400 Bad Request\r\n"), nxt_string("HTTP/1.1 401 Unauthorized\r\n"), nxt_string("HTTP/1.1 402 Payment Required\r\n"), @@ -1278,19 +1257,19 @@ static const nxt_str_t nxt_http_client_error[] = { }; -#define NXT_HTTP_LAST_NGINX_ERROR \ +#define NXT_HTTP_LAST_NGINX_ERROR \ (NXT_HTTP_TO_HTTPS + nxt_nitems(nxt_http_nginx_error) - 1) -static const nxt_str_t nxt_http_nginx_error[] = { +static const nxt_str_t nxt_http_nginx_error[] = { nxt_string("HTTP/1.1 400 " "The plain HTTP request was sent to HTTPS port\r\n"), }; -#define NXT_HTTP_LAST_SERVER_ERROR \ +#define NXT_HTTP_LAST_SERVER_ERROR \ (NXT_HTTP_INTERNAL_SERVER_ERROR + nxt_nitems(nxt_http_server_error) - 1) -static const nxt_str_t nxt_http_server_error[] = { +static const nxt_str_t nxt_http_server_error[] = { nxt_string("HTTP/1.1 500 Internal Server Error\r\n"), nxt_string("HTTP/1.1 501 Not Implemented\r\n"), nxt_string("HTTP/1.1 502 Bad Gateway\r\n"), @@ -1300,29 +1279,28 @@ static const nxt_str_t nxt_http_server_error[] = { }; -#define UNKNOWN_STATUS_LENGTH nxt_length("HTTP/1.1 999 \r\n") +#define UNKNOWN_STATUS_LENGTH nxt_length("HTTP/1.1 999 \r\n") static void nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_work_handler_t body_handler, void *data) -{ - u_char *p; - size_t size; - nxt_buf_t *header; - nxt_str_t unknown_status; - nxt_int_t conn; - nxt_uint_t n; - nxt_bool_t http11; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - const nxt_str_t *status; - nxt_http_field_t *field; - u_char buf[UNKNOWN_STATUS_LENGTH]; - - static const char chunked[] = "Transfer-Encoding: chunked\r\n"; - static const char websocket_version[] = "Sec-WebSocket-Version: 13\r\n"; - - static const nxt_str_t connection[3] = { + nxt_work_handler_t body_handler, void *data) { + u_char *p; + size_t size; + nxt_buf_t *header; + nxt_str_t unknown_status; + nxt_int_t conn; + nxt_uint_t n; + nxt_bool_t http11; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + const nxt_str_t *status; + nxt_http_field_t *field; + u_char buf[UNKNOWN_STATUS_LENGTH]; + + static const char chunked[] = "Transfer-Encoding: chunked\r\n"; + static const char websocket_version[] = "Sec-WebSocket-Version: 13\r\n"; + + static const nxt_str_t connection[3] = { nxt_string("Connection: close\r\n"), nxt_string("Connection: keep-alive\r\n"), nxt_string("Upgrade: websocket\r\n" @@ -1333,8 +1311,8 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, nxt_debug(task, "h1p request header send"); r->header_sent = 1; - h1p = r->proto.h1; - n = r->status; + h1p = r->proto.h1; + n = r->status; if (n >= NXT_HTTP_CONTINUE && n <= NXT_HTTP_LAST_INFORMATIONAL) { status = &nxt_http_informational[n - NXT_HTTP_CONTINUE]; @@ -1343,8 +1321,7 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, status = &nxt_http_success[n - NXT_HTTP_OK]; } else if (n >= NXT_HTTP_MULTIPLE_CHOICES - && n <= NXT_HTTP_LAST_REDIRECTION) - { + && n <= NXT_HTTP_LAST_REDIRECTION) { status = &nxt_http_redirection[n - NXT_HTTP_MULTIPLE_CHOICES]; } else if (n >= NXT_HTTP_BAD_REQUEST && n <= NXT_HTTP_LAST_CLIENT_ERROR) { @@ -1354,49 +1331,44 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, status = &nxt_http_nginx_error[n - NXT_HTTP_TO_HTTPS]; } else if (n >= NXT_HTTP_INTERNAL_SERVER_ERROR - && n <= NXT_HTTP_LAST_SERVER_ERROR) - { + && n <= NXT_HTTP_LAST_SERVER_ERROR) { status = &nxt_http_server_error[n - NXT_HTTP_INTERNAL_SERVER_ERROR]; } else if (n <= NXT_HTTP_STATUS_MAX) { (void) nxt_sprintf(buf, buf + UNKNOWN_STATUS_LENGTH, - "HTTP/1.1 %03d \r\n", n); + "HTTP/1.1 %03d \r\n", n); unknown_status.length = UNKNOWN_STATUS_LENGTH; - unknown_status.start = buf; - status = &unknown_status; + unknown_status.start = buf; + status = &unknown_status; } else { status = &nxt_http_server_error[0]; } - size = status->length; + size = status->length; /* Trailing CRLF at the end of header. */ size += nxt_length("\r\n"); conn = -1; if (r->websocket_handshake && n == NXT_HTTP_SWITCHING_PROTOCOLS) { - h1p->websocket = 1; - h1p->keepalive = 0; - conn = 2; - size += NXT_WEBSOCKET_ACCEPT_SIZE + 2; + h1p->websocket = 1; + h1p->keepalive = 0; + conn = 2; + size += NXT_WEBSOCKET_ACCEPT_SIZE + 2; } else { http11 = nxt_h1p_is_http11(h1p); if (r->resp.content_length == NULL || r->resp.content_length->skip) { - if (http11) { - if (n != NXT_HTTP_NOT_MODIFIED - && n != NXT_HTTP_NO_CONTENT - && body_handler != NULL - && !h1p->websocket) - { - h1p->chunked = 1; - size += nxt_length(chunked); + if (n != NXT_HTTP_NOT_MODIFIED && n != NXT_HTTP_NO_CONTENT + && body_handler != NULL && !h1p->websocket) { + h1p->chunked = 1; + size += nxt_length(chunked); /* Trailing CRLF will be added by the first chunk header. */ - size -= nxt_length("\r\n"); + size -= nxt_length("\r\n"); } } else { @@ -1414,13 +1386,12 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, } nxt_list_each(field, r->resp.fields) { - if (!field->skip) { size += field->name_length + field->value_length; size += nxt_length(": \r\n"); } - - } nxt_list_loop; + } + nxt_list_loop; if (nxt_slow_path(n == NXT_HTTP_UPGRADE_REQUIRED)) { size += nxt_length(websocket_version); @@ -1435,15 +1406,16 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, p = nxt_cpymem(header->mem.free, status->start, status->length); nxt_list_each(field, r->resp.fields) { - if (!field->skip) { - p = nxt_cpymem(p, field->name, field->name_length); - *p++ = ':'; *p++ = ' '; - p = nxt_cpymem(p, field->value, field->value_length); - *p++ = '\r'; *p++ = '\n'; + p = nxt_cpymem(p, field->name, field->name_length); + *p++ = ':'; + *p++ = ' '; + p = nxt_cpymem(p, field->value, field->value_length); + *p++ = '\r'; + *p++ = '\n'; } - - } nxt_list_loop; + } + nxt_list_loop; if (conn >= 0) { p = nxt_cpymem(p, connection[conn].start, connection[conn].length); @@ -1453,7 +1425,8 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, nxt_websocket_accept(p, h1p->websocket_key->value); p += NXT_WEBSOCKET_ACCEPT_SIZE; - *p++ = '\r'; *p++ = '\n'; + *p++ = '\r'; + *p++ = '\n'; } if (nxt_slow_path(n == NXT_HTTP_UPGRADE_REQUIRED)) { @@ -1465,7 +1438,8 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, /* Trailing CRLF will be added by the first chunk header. */ } else { - *p++ = '\r'; *p++ = '\n'; + *p++ = '\r'; + *p++ = '\n'; } header->mem.free = p; @@ -1474,9 +1448,9 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, c = h1p->conn; - c->write = header; + c->write = header; h1p->conn_write_tail = &header->next; - c->write_state = &nxt_h1p_request_send_state; + c->write_state = &nxt_h1p_request_send_state; if (body_handler != NULL) { /* @@ -1484,8 +1458,8 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, * because the latter was inqueued by nxt_conn_write() * in engine->write_work_queue. */ - nxt_work_queue_add(&task->thread->engine->fast_work_queue, - body_handler, task, r, data); + nxt_work_queue_add(&task->thread->engine->fast_work_queue, body_handler, + task, r, data); } else { header->next = nxt_http_buf_last(r); @@ -1498,32 +1472,30 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r, } } - void -nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all) -{ - size_t size; - nxt_buf_t *b, *in, *next; - nxt_conn_t *c; +nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all) { + size_t size; + nxt_buf_t *b, *in, *next; + nxt_conn_t *c; nxt_debug(task, "h1p complete buffers"); - b = h1p->buffers; - c = h1p->conn; + b = h1p->buffers; + c = h1p->conn; in = c->read; if (b != NULL) { if (in == NULL) { /* A request with large body. */ - in = b; + in = b; c->read = in; - b = in->next; + b = in->next; in->next = NULL; } while (b != NULL) { - next = b->next; + next = b->next; b->next = NULL; b->completion_handler(task, b, b->parent); @@ -1531,7 +1503,7 @@ nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all) b = next; } - h1p->buffers = NULL; + h1p->buffers = NULL; h1p->nbuffers = 0; } @@ -1546,30 +1518,25 @@ nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all) } } - -static const nxt_conn_state_t nxt_h1p_request_send_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_request_send_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_sent, .error_handler = nxt_h1p_conn_request_error, - .timer_handler = nxt_h1p_conn_request_send_timeout, - .timer_value = nxt_h1p_conn_request_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, send_timeout), + .timer_handler = nxt_h1p_conn_request_send_timeout, + .timer_value = nxt_h1p_conn_request_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, send_timeout), .timer_autoreset = 1, }; - static void -nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) -{ - nxt_conn_t *c; - nxt_h1proto_t *h1p; +nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) { + nxt_conn_t *c; + nxt_h1proto_t *h1p; nxt_debug(task, "h1p request send"); h1p = r->proto.h1; - c = h1p->conn; + c = h1p->conn; if (h1p->chunked) { out = nxt_h1p_chunk_create(task, r, out); @@ -1580,7 +1547,7 @@ nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) } if (c->write == NULL) { - c->write = out; + c->write = out; c->write_state = &nxt_h1p_request_send_state; nxt_conn_write(task->thread->engine, c); @@ -1596,28 +1563,25 @@ nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) h1p->conn_write_tail = &out->next; } - static nxt_buf_t * -nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) -{ - nxt_off_t size; - nxt_buf_t *b, **prev, *header, *tail; +nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) { + nxt_off_t size; + nxt_buf_t *b, **prev, *header, *tail; - const size_t chunk_size = 2 * nxt_length("\r\n") + NXT_OFF_T_HEXLEN; - static const char tail_chunk[] = "\r\n0\r\n\r\n"; + const size_t chunk_size = 2 * nxt_length("\r\n") + NXT_OFF_T_HEXLEN; + static const char tail_chunk[] = "\r\n0\r\n\r\n"; size = 0; prev = &out; for (b = out; b != NULL; b = b->next) { - if (nxt_buf_is_last(b)) { tail = nxt_http_buf_mem(task, r, sizeof(tail_chunk)); if (nxt_slow_path(tail == NULL)) { return NULL; } - *prev = tail; + *prev = tail; tail->next = b; /* * The tail_chunk size with trailing zero is 8 bytes, so @@ -1630,7 +1594,7 @@ nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) } size += nxt_buf_used_size(b); - prev = &b->next; + prev = &b->next; } if (size == 0) { @@ -1643,17 +1607,15 @@ nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) } header->next = out; - header->mem.free = nxt_sprintf(header->mem.free, header->mem.end, - "\r\n%xO\r\n", size); + header->mem.free + = nxt_sprintf(header->mem.free, header->mem.end, "\r\n%xO\r\n", size); return header; } - static nxt_off_t -nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto) -{ +nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto) { nxt_off_t sent; - nxt_h1proto_t *h1p; + nxt_h1proto_t *h1p; h1p = proto.h1; @@ -1662,23 +1624,21 @@ nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto) return (sent > 0) ? sent : 0; } - static void nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *last) -{ - nxt_buf_t *b; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_work_queue_t *wq; + nxt_buf_t *last) { + nxt_buf_t *b; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_work_queue_t *wq; nxt_debug(task, "h1p request discard"); - h1p = r->proto.h1; + h1p = r->proto.h1; h1p->keepalive = 0; - c = h1p->conn; - b = c->write; + c = h1p->conn; + b = c->write; c->write = NULL; wq = &task->thread->engine->fast_work_queue; @@ -1687,12 +1647,10 @@ nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r, nxt_sendbuf_drain(task, wq, last); } - static void -nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_h1proto_t *h1p; - nxt_http_request_t *r; +nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data) { + nxt_h1proto_t *h1p; + nxt_http_request_t *r; h1p = data; @@ -1716,30 +1674,28 @@ nxt_h1p_conn_request_error(nxt_task_t *task, void *obj, void *data) nxt_h1p_request_error(task, h1p, r); } - static void -nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; +nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; timer = obj; nxt_debug(task, "h1p conn request timeout"); - c = nxt_read_timer_conn(timer); - c->block_read = 1; + c = nxt_read_timer_conn(timer); + c->block_read = 1; /* * Disable SO_LINGER off during socket closing * to send "408 Request Timeout" error response. */ c->socket.timedout = 0; - h1p = c->socket.data; + h1p = c->socket.data; h1p->keepalive = 0; - r = h1p->request; + r = h1p->request; if (r->fields == NULL) { (void) nxt_h1p_header_process(task, h1p, r); @@ -1748,66 +1704,58 @@ nxt_h1p_conn_request_timeout(nxt_task_t *task, void *obj, void *data) nxt_http_request_error(task, r, NXT_HTTP_REQUEST_TIMEOUT); } - static void -nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_h1proto_t *h1p; +nxt_h1p_conn_request_send_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_h1proto_t *h1p; timer = obj; nxt_debug(task, "h1p conn request send timeout"); - c = nxt_write_timer_conn(timer); + c = nxt_write_timer_conn(timer); c->block_write = 1; - h1p = c->socket.data; + h1p = c->socket.data; nxt_h1p_request_error(task, h1p, h1p->request); } - nxt_msec_t -nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data) -{ - nxt_h1proto_t *h1p; +nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data) { + nxt_h1proto_t *h1p; h1p = c->socket.data; return nxt_value_at(nxt_msec_t, h1p->request->conf->socket_conf, data); } - nxt_inline void nxt_h1p_request_error(nxt_task_t *task, nxt_h1proto_t *h1p, - nxt_http_request_t *r) -{ + nxt_http_request_t *r) { h1p->keepalive = 0; r->state->error_handler(task, r, h1p); } - static void nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto, - nxt_socket_conf_joint_t *joint) -{ - nxt_conn_t *c; - nxt_h1proto_t *h1p; + nxt_socket_conf_joint_t *joint) { + nxt_conn_t *c; + nxt_h1proto_t *h1p; nxt_debug(task, "h1p request close"); - h1p = proto.h1; + h1p = proto.h1; h1p->keepalive &= !h1p->request->inconsistent; - h1p->request = NULL; + h1p->request = NULL; nxt_router_conf_release(task, joint); - c = h1p->conn; - task = &c->task; - c->socket.task = task; - c->read_timer.task = task; + c = h1p->conn; + task = &c->task; + c->socket.task = task; + c->read_timer.task = task; c->write_timer.task = task; if (h1p->keepalive) { @@ -1818,12 +1766,10 @@ nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto, } } - static void -nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_event_engine_t *engine; +nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_event_engine_t *engine; c = obj; @@ -1838,11 +1784,9 @@ nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1853,11 +1797,9 @@ nxt_h1p_conn_close(nxt_task_t *task, void *obj, void *data) nxt_h1p_shutdown(task, c); } - static void -nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1868,11 +1810,9 @@ nxt_h1p_conn_error(nxt_task_t *task, void *obj, void *data) nxt_h1p_shutdown(task, c); } - static nxt_msec_t -nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data) -{ - nxt_socket_conf_joint_t *joint; +nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data) { + nxt_socket_conf_joint_t *joint; joint = c->listen->socket.data; @@ -1887,13 +1827,11 @@ nxt_h1p_conn_timer_value(nxt_conn_t *c, uintptr_t data) return 1; } - static void -nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c) -{ +nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c) { size_t size; - nxt_buf_t *in; - nxt_event_engine_t *engine; + nxt_buf_t *in; + nxt_event_engine_t *engine; nxt_debug(task, "h1p keepalive"); @@ -1925,41 +1863,34 @@ nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c) nxt_memmove(in->mem.start, in->mem.pos, size); - in->mem.pos = in->mem.start; + in->mem.pos = in->mem.start; in->mem.free = in->mem.start + size; nxt_h1p_conn_request_init(task, c, c->socket.data); } } - -static const nxt_conn_state_t nxt_h1p_keepalive_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_keepalive_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_request_init, .close_handler = nxt_h1p_conn_close, .error_handler = nxt_h1p_conn_error, .io_read_handler = nxt_h1p_idle_io_read_handler, - .timer_handler = nxt_h1p_idle_timeout, - .timer_value = nxt_h1p_conn_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), + .timer_handler = nxt_h1p_idle_timeout, + .timer_value = nxt_h1p_conn_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, idle_timeout), .timer_autoreset = 1, }; -const nxt_conn_state_t nxt_h1p_idle_close_state - nxt_aligned(64) = -{ +const nxt_conn_state_t nxt_h1p_idle_close_state nxt_aligned(64) = { .close_handler = nxt_h1p_idle_close, }; - static void -nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -1970,18 +1901,16 @@ nxt_h1p_idle_close(nxt_task_t *task, void *obj, void *data) nxt_h1p_idle_response(task, c); } - static void -nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; nxt_debug(task, "h1p idle timeout"); - c = nxt_read_timer_conn(timer); + c = nxt_read_timer_conn(timer); c->block_read = 1; nxt_conn_active(task->thread->engine, c); @@ -1989,24 +1918,20 @@ nxt_h1p_idle_timeout(nxt_task_t *task, void *obj, void *data) nxt_h1p_idle_response(task, c); } - -#define NXT_H1P_IDLE_TIMEOUT \ - "HTTP/1.1 408 Request Timeout\r\n" \ - "Server: " NXT_SERVER "\r\n" \ - "Connection: close\r\n" \ - "Content-Length: 0\r\n" \ +#define NXT_H1P_IDLE_TIMEOUT \ + "HTTP/1.1 408 Request Timeout\r\n" \ + "Server: " NXT_SERVER "\r\n" \ + "Connection: close\r\n" \ + "Content-Length: 0\r\n" \ "Date: " - static void -nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c) -{ - u_char *p; +nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c) { + u_char *p; size_t size; - nxt_buf_t *out, *last; + nxt_buf_t *out, *last; - size = nxt_length(NXT_H1P_IDLE_TIMEOUT) - + nxt_http_date_cache.size + size = nxt_length(NXT_H1P_IDLE_TIMEOUT) + nxt_http_date_cache.size + nxt_length("\r\n\r\n"); out = nxt_buf_mem_alloc(c->mem_pool, size, 0); @@ -2015,7 +1940,7 @@ nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c) } p = nxt_cpymem(out->mem.free, NXT_H1P_IDLE_TIMEOUT, - nxt_length(NXT_H1P_IDLE_TIMEOUT)); + nxt_length(NXT_H1P_IDLE_TIMEOUT)); p = nxt_thread_time_string(task->thread, &nxt_http_date_cache, p); @@ -2031,9 +1956,9 @@ nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c) nxt_buf_set_last(last); last->completion_handler = nxt_h1p_idle_response_sent; - last->parent = c; + last->parent = c; - c->write = out; + c->write = out; c->write_state = &nxt_h1p_timeout_response_state; nxt_conn_write(task->thread->engine, c); @@ -2044,22 +1969,17 @@ nxt_h1p_idle_response(nxt_task_t *task, nxt_conn_t *c) nxt_h1p_shutdown(task, c); } - -static const nxt_conn_state_t nxt_h1p_timeout_response_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_timeout_response_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_sent, .error_handler = nxt_h1p_idle_response_error, .timer_handler = nxt_h1p_idle_response_timeout, - .timer_value = nxt_h1p_idle_response_timer_value, + .timer_value = nxt_h1p_idle_response_timer_value, }; - static void -nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = data; @@ -2068,11 +1988,9 @@ nxt_h1p_idle_response_sent(nxt_task_t *task, void *obj, void *data) nxt_h1p_shutdown(task, c); } - static void -nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -2081,36 +1999,30 @@ nxt_h1p_idle_response_error(nxt_task_t *task, void *obj, void *data) nxt_h1p_shutdown(task, c); } - static void -nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_h1p_idle_response_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; nxt_debug(task, "h1p idle timeout response timeout"); - c = nxt_read_timer_conn(timer); + c = nxt_read_timer_conn(timer); c->block_write = 1; nxt_h1p_shutdown(task, c); } - static nxt_msec_t -nxt_h1p_idle_response_timer_value(nxt_conn_t *c, uintptr_t data) -{ +nxt_h1p_idle_response_timer_value(nxt_conn_t *c, uintptr_t data) { return 10 * 1000; } - static void -nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c) -{ - nxt_timer_t *timer; - nxt_h1proto_t *h1p; +nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c) { + nxt_timer_t *timer; + nxt_h1proto_t *h1p; nxt_debug(task, "h1p shutdown"); @@ -2137,25 +2049,21 @@ nxt_h1p_shutdown(nxt_task_t *task, nxt_conn_t *c) nxt_h1p_closing(task, c); } - static void -nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_h1p_websocket_timer_t *ws_timer; +nxt_h1p_conn_ws_shutdown(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_h1p_websocket_timer_t *ws_timer; nxt_debug(task, "h1p conn ws shutdown"); - timer = obj; + timer = obj; ws_timer = nxt_timer_data(timer, nxt_h1p_websocket_timer_t, timer); nxt_h1p_closing(task, ws_timer->h1p->conn); } - static void -nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c) { nxt_debug(task, "h1p closing"); c->socket.data = NULL; @@ -2177,9 +2085,7 @@ nxt_h1p_closing(nxt_task_t *task, nxt_conn_t *c) #if (NXT_TLS) -static const nxt_conn_state_t nxt_h1p_shutdown_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_shutdown_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_closing, .close_handler = nxt_h1p_conn_closing, .error_handler = nxt_h1p_conn_closing, @@ -2189,9 +2095,8 @@ static const nxt_conn_state_t nxt_h1p_shutdown_state static void -nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -2202,20 +2107,15 @@ nxt_h1p_conn_closing(nxt_task_t *task, void *obj, void *data) nxt_conn_close(task->thread->engine, c); } - -static const nxt_conn_state_t nxt_h1p_close_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_close_state nxt_aligned(64) = { .ready_handler = nxt_h1p_conn_free, }; - static void -nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_listen_event_t *lev; - nxt_event_engine_t *engine; +nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_listen_event_t *lev; + nxt_event_engine_t *engine; c = obj; @@ -2232,22 +2132,20 @@ nxt_h1p_conn_free(nxt_task_t *task, void *obj, void *data) nxt_router_listen_event_release(&engine->task, lev, NULL); } - static void -nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer) -{ - nxt_mp_t *mp; +nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer) { + nxt_mp_t *mp; nxt_int_t ret; - nxt_conn_t *c, *client; - nxt_h1proto_t *h1p; - nxt_fd_event_t *socket; - nxt_work_queue_t *wq; - nxt_http_request_t *r; + nxt_conn_t *c, *client; + nxt_h1proto_t *h1p; + nxt_fd_event_t *socket; + nxt_work_queue_t *wq; + nxt_http_request_t *r; nxt_debug(task, "h1p peer connect"); peer->status = NXT_HTTP_UNSET; - r = peer->request; + r = peer->request; mp = nxt_mp_create(1024, 128, 256, 32); @@ -2271,32 +2169,32 @@ nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer) } c->mem_pool = mp; - h1p->conn = c; + h1p->conn = c; peer->proto.h1 = h1p; - h1p->request = r; + h1p->request = r; c->socket.data = peer; - c->remote = peer->server->sockaddr; + c->remote = peer->server->sockaddr; c->socket.write_ready = 1; - c->write_state = &nxt_h1p_peer_connect_state; + c->write_state = &nxt_h1p_peer_connect_state; /* * TODO: queues should be implemented via client proto interface. */ client = r->proto.h1->conn; - socket = &client->socket; - wq = socket->read_work_queue; - c->read_work_queue = wq; + socket = &client->socket; + wq = socket->read_work_queue; + c->read_work_queue = wq; c->socket.read_work_queue = wq; - c->read_timer.work_queue = wq; + c->read_timer.work_queue = wq; - wq = socket->write_work_queue; - c->write_work_queue = wq; + wq = socket->write_work_queue; + c->write_work_queue = wq; c->socket.write_work_queue = wq; - c->write_timer.work_queue = wq; + c->write_timer.work_queue = wq; /* TODO END */ nxt_conn_connect(task->thread->engine, c); @@ -2310,25 +2208,20 @@ nxt_h1p_peer_connect(nxt_task_t *task, nxt_http_peer_t *peer) r->state->error_handler(task, r, peer); } - -static const nxt_conn_state_t nxt_h1p_peer_connect_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_peer_connect_state nxt_aligned(64) = { .ready_handler = nxt_h1p_peer_connected, .close_handler = nxt_h1p_peer_refused, .error_handler = nxt_h1p_peer_error, .timer_handler = nxt_h1p_peer_send_timeout, - .timer_value = nxt_h1p_peer_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), + .timer_value = nxt_h1p_peer_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), }; - static void -nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; peer = data; @@ -2338,36 +2231,32 @@ nxt_h1p_peer_connected(nxt_task_t *task, void *obj, void *data) r->state->ready_handler(task, r, peer); } - static void -nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_refused(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; peer = data; nxt_debug(task, "h1p peer refused"); - //peer->status = NXT_HTTP_SERVICE_UNAVAILABLE; + // peer->status = NXT_HTTP_SERVICE_UNAVAILABLE; peer->status = NXT_HTTP_BAD_GATEWAY; r = peer->request; r->state->error_handler(task, r, peer); } - static void -nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) -{ - u_char *p; +nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) { + u_char *p; size_t size; nxt_int_t ret; nxt_str_t target; - nxt_buf_t *header, *body; - nxt_conn_t *c; - nxt_http_field_t *field; - nxt_http_request_t *r; + nxt_buf_t *header, *body; + nxt_conn_t *c; + nxt_http_field_t *field; + nxt_http_request_t *r; nxt_debug(task, "h1p peer header send"); @@ -2379,18 +2268,16 @@ nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) } size = r->method->length + sizeof(" ") + target.length - + sizeof(" HTTP/1.1\r\n") - + sizeof("Connection: close\r\n") + + sizeof(" HTTP/1.1\r\n") + sizeof("Connection: close\r\n") + sizeof("\r\n"); nxt_list_each(field, r->fields) { - if (!field->hopbyhop) { size += field->name_length + field->value_length; size += nxt_length(": \r\n"); } - - } nxt_list_loop; + } + nxt_list_loop; header = nxt_http_buf_mem(task, r, size); if (nxt_slow_path(header == NULL)) { @@ -2399,29 +2286,31 @@ nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) p = header->mem.free; - p = nxt_cpymem(p, r->method->start, r->method->length); + p = nxt_cpymem(p, r->method->start, r->method->length); *p++ = ' '; - p = nxt_cpymem(p, target.start, target.length); - p = nxt_cpymem(p, " HTTP/1.1\r\n", 11); - p = nxt_cpymem(p, "Connection: close\r\n", 19); + p = nxt_cpymem(p, target.start, target.length); + p = nxt_cpymem(p, " HTTP/1.1\r\n", 11); + p = nxt_cpymem(p, "Connection: close\r\n", 19); nxt_list_each(field, r->fields) { - if (!field->hopbyhop) { - p = nxt_cpymem(p, field->name, field->name_length); - *p++ = ':'; *p++ = ' '; - p = nxt_cpymem(p, field->value, field->value_length); - *p++ = '\r'; *p++ = '\n'; + p = nxt_cpymem(p, field->name, field->name_length); + *p++ = ':'; + *p++ = ' '; + p = nxt_cpymem(p, field->value, field->value_length); + *p++ = '\r'; + *p++ = '\n'; } + } + nxt_list_loop; - } nxt_list_loop; - - *p++ = '\r'; *p++ = '\n'; + *p++ = '\r'; + *p++ = '\n'; header->mem.free = p; - size = p - header->mem.pos; + size = p - header->mem.pos; - c = peer->proto.h1->conn; - c->write = header; + c = peer->proto.h1->conn; + c->write = header; c->write_state = &nxt_h1p_peer_header_send_state; if (r->body != NULL) { @@ -2439,7 +2328,7 @@ nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) header->next = body; if (nxt_buf_is_file(r->body)) { - body->file = r->body->file; + body->file = r->body->file; body->file_end = r->body->file_end; } else { @@ -2448,7 +2337,7 @@ nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) size += nxt_buf_used_size(body); -// nxt_mp_retain(r->mem_pool); + // nxt_mp_retain(r->mem_pool); } if (size > 16384) { @@ -2465,11 +2354,9 @@ nxt_h1p_peer_header_send(nxt_task_t *task, nxt_http_peer_t *peer) r->state->error_handler(task, r, peer); } - static nxt_int_t -nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) -{ - u_char *p; +nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) { + u_char *p; size_t size, encode; if (!r->uri_changed) { @@ -2483,8 +2370,7 @@ nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) } if (r->quoted_target) { - encode = nxt_encode_complex_uri(NULL, r->path->start, - r->path->length); + encode = nxt_encode_complex_uri(NULL, r->path->start, r->path->length); } else { encode = 0; } @@ -2498,7 +2384,7 @@ nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) if (r->quoted_target) { p = (u_char *) nxt_encode_complex_uri(target->start, r->path->start, - r->path->length); + r->path->length); } else { p = nxt_cpymem(target->start, r->path->start, r->path->length); @@ -2506,7 +2392,7 @@ nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) if (r->args->length > 0) { *p++ = '?'; - p = nxt_cpymem(p, r->args->start, r->args->length); + p = nxt_cpymem(p, r->args->start, r->args->length); } target->length = p - target->start; @@ -2514,41 +2400,36 @@ nxt_h1p_peer_request_target(nxt_http_request_t *r, nxt_str_t *target) return NXT_OK; } - -static const nxt_conn_state_t nxt_h1p_peer_header_send_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_peer_header_send_state nxt_aligned(64) = { .ready_handler = nxt_h1p_peer_header_sent, .error_handler = nxt_h1p_peer_error, .timer_handler = nxt_h1p_peer_send_timeout, - .timer_value = nxt_h1p_peer_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), + .timer_value = nxt_h1p_peer_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), }; -static const nxt_conn_state_t nxt_h1p_peer_header_body_send_state - nxt_aligned(64) = -{ - .ready_handler = nxt_h1p_peer_header_sent, - .error_handler = nxt_h1p_peer_error, +static const nxt_conn_state_t nxt_h1p_peer_header_body_send_state nxt_aligned( + 64) + = { + .ready_handler = nxt_h1p_peer_header_sent, + .error_handler = nxt_h1p_peer_error, - .timer_handler = nxt_h1p_peer_send_timeout, - .timer_value = nxt_h1p_peer_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, proxy_send_timeout), - .timer_autoreset = 1, + .timer_handler = nxt_h1p_peer_send_timeout, + .timer_value = nxt_h1p_peer_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, proxy_send_timeout), + .timer_autoreset = 1, }; - static void -nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_http_peer_t *peer; - nxt_http_request_t *r; - nxt_event_engine_t *engine; +nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_http_peer_t *peer; + nxt_http_request_t *r; + nxt_event_engine_t *engine; - c = obj; + c = obj; peer = data; nxt_debug(task, "h1p peer header sent"); @@ -2566,11 +2447,9 @@ nxt_h1p_peer_header_sent(nxt_task_t *task, void *obj, void *data) r->state->ready_handler(task, r, peer); } - static void -nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer) -{ - nxt_conn_t *c; +nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer) { + nxt_conn_t *c; nxt_debug(task, "h1p peer header read"); @@ -2586,10 +2465,7 @@ nxt_h1p_peer_header_read(nxt_task_t *task, nxt_http_peer_t *peer) nxt_conn_read(task->thread->engine, c); } - -static const nxt_conn_state_t nxt_h1p_peer_header_read_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_peer_header_read_state nxt_aligned(64) = { .ready_handler = nxt_h1p_peer_header_read_done, .close_handler = nxt_h1p_peer_closed, .error_handler = nxt_h1p_peer_error, @@ -2598,34 +2474,32 @@ static const nxt_conn_state_t nxt_h1p_peer_header_read_state }; -static const nxt_conn_state_t nxt_h1p_peer_header_read_timer_state - nxt_aligned(64) = -{ - .ready_handler = nxt_h1p_peer_header_read_done, - .close_handler = nxt_h1p_peer_closed, - .error_handler = nxt_h1p_peer_error, +static const nxt_conn_state_t nxt_h1p_peer_header_read_timer_state nxt_aligned( + 64) + = { + .ready_handler = nxt_h1p_peer_header_read_done, + .close_handler = nxt_h1p_peer_closed, + .error_handler = nxt_h1p_peer_error, - .io_read_handler = nxt_h1p_peer_io_read_handler, + .io_read_handler = nxt_h1p_peer_io_read_handler, - .timer_handler = nxt_h1p_peer_read_timeout, - .timer_value = nxt_h1p_peer_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), + .timer_handler = nxt_h1p_peer_read_timeout, + .timer_value = nxt_h1p_peer_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, proxy_timeout), }; - static ssize_t -nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c) { size_t size; ssize_t n; - nxt_buf_t *b; - nxt_http_peer_t *peer; - nxt_socket_conf_t *skcf; - nxt_http_request_t *r; + nxt_buf_t *b; + nxt_http_peer_t *peer; + nxt_socket_conf_t *skcf; + nxt_http_request_t *r; peer = c->socket.data; - r = peer->request; - b = c->read; + r = peer->request; + b = c->read; if (b == NULL) { skcf = r->conf->socket_conf; @@ -2655,19 +2529,17 @@ nxt_h1p_peer_io_read_handler(nxt_task_t *task, nxt_conn_t *c) return n; } - static void -nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_buf_t *b; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_http_peer_t *peer; - nxt_http_request_t *r; - nxt_event_engine_t *engine; + nxt_buf_t *b; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_http_peer_t *peer; + nxt_http_request_t *r; + nxt_event_engine_t *engine; - c = obj; + c = obj; peer = data; nxt_debug(task, "h1p peer header read done"); @@ -2687,12 +2559,11 @@ nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data) } switch (ret) { - case NXT_DONE: peer->fields = peer->proto.h1->parser.fields; - ret = nxt_http_fields_process(peer->fields, - &nxt_h1p_peer_fields_hash, r); + ret = nxt_http_fields_process(peer->fields, &nxt_h1p_peer_fields_hash, + r); if (nxt_slow_path(ret != NXT_OK)) { peer->status = NXT_HTTP_INTERNAL_SERVER_ERROR; break; @@ -2746,13 +2617,11 @@ nxt_h1p_peer_header_read_done(nxt_task_t *task, void *obj, void *data) r->state->error_handler(task, r, peer); } - static nxt_int_t -nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm) -{ - u_char *p; - size_t length; - nxt_int_t status; +nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm) { + u_char *p; + size_t length; + nxt_int_t status; if (peer->status < 0) { length = nxt_buf_mem_used_size(bm); @@ -2763,9 +2632,8 @@ nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm) p = bm->pos; - if (nxt_slow_path(memcmp(p, "HTTP/1.", 7) != 0 - || (p[7] != '0' && p[7] != '1'))) - { + if (nxt_slow_path( + memcmp(p, "HTTP/1.", 7) != 0 || (p[7] != '0' && p[7] != '1'))) { return NXT_ERROR; } @@ -2775,7 +2643,7 @@ nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm) return NXT_ERROR; } - p += 12; + p += 12; length -= 12; p = memchr(p, '\n', length); @@ -2784,70 +2652,61 @@ nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm) return NXT_AGAIN; } - bm->pos = p + 1; + bm->pos = p + 1; peer->status = status; } return nxt_http_parse_fields(&peer->proto.h1->parser, bm); } - static void -nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer) -{ - nxt_conn_t *c; +nxt_h1p_peer_read(nxt_task_t *task, nxt_http_peer_t *peer) { + nxt_conn_t *c; nxt_debug(task, "h1p peer read"); - c = peer->proto.h1->conn; + c = peer->proto.h1->conn; c->read_state = &nxt_h1p_peer_read_state; nxt_conn_read(task->thread->engine, c); } - -static const nxt_conn_state_t nxt_h1p_peer_read_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_peer_read_state nxt_aligned(64) = { .ready_handler = nxt_h1p_peer_read_done, .close_handler = nxt_h1p_peer_closed, .error_handler = nxt_h1p_peer_error, .io_read_handler = nxt_h1p_peer_io_read_handler, - .timer_handler = nxt_h1p_peer_read_timeout, - .timer_value = nxt_h1p_peer_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, proxy_read_timeout), + .timer_handler = nxt_h1p_peer_read_timeout, + .timer_value = nxt_h1p_peer_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, proxy_read_timeout), .timer_autoreset = 1, }; - static void -nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *out; - nxt_conn_t *c; - nxt_http_peer_t *peer; +nxt_h1p_peer_read_done(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *out; + nxt_conn_t *c; + nxt_http_peer_t *peer; - c = obj; + c = obj; peer = data; nxt_debug(task, "h1p peer read done"); - out = c->read; + out = c->read; c->read = NULL; nxt_h1p_peer_body_process(task, peer, out); } - static void nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, - nxt_buf_t *out) -{ + nxt_buf_t *out) { size_t length; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; h1p = peer->proto.h1; @@ -2856,7 +2715,7 @@ nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, if (h1p->chunked_parse.chunk_error || h1p->chunked_parse.error) { peer->status = NXT_HTTP_BAD_GATEWAY; - r = peer->request; + r = peer->request; r->state->error_handler(task, r, peer); return; } @@ -2867,7 +2726,7 @@ nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, } } else if (h1p->remainder > 0) { - length = nxt_buf_chain_length(out); + length = nxt_buf_chain_length(out); h1p->remainder -= length; if (h1p->remainder == 0) { @@ -2882,12 +2741,10 @@ nxt_h1p_peer_body_process(nxt_task_t *task, nxt_http_peer_t *peer, r->state->ready_handler(task, r, peer); } - static void -nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; peer = data; @@ -2896,8 +2753,8 @@ nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data) r = peer->request; if (peer->header_received) { - peer->body = nxt_http_buf_last(r); - peer->closed = 1; + peer->body = nxt_http_buf_last(r); + peer->closed = 1; r->inconsistent = (peer->proto.h1->remainder != 0); r->state->ready_handler(task, r, peer); @@ -2909,12 +2766,10 @@ nxt_h1p_peer_closed(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; peer = data; @@ -2926,79 +2781,71 @@ nxt_h1p_peer_error(nxt_task_t *task, void *obj, void *data) r->state->error_handler(task, r, peer); } - static void -nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_send_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_http_peer_t *peer; + nxt_http_request_t *r; timer = obj; nxt_debug(task, "h1p peer send timeout"); - c = nxt_write_timer_conn(timer); + c = nxt_write_timer_conn(timer); c->block_write = 1; - c->block_read = 1; + c->block_read = 1; - peer = c->socket.data; + peer = c->socket.data; peer->status = NXT_HTTP_GATEWAY_TIMEOUT; r = peer->request; r->state->error_handler(task, r, peer); } - static void -nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_h1p_peer_read_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_http_peer_t *peer; + nxt_http_request_t *r; timer = obj; nxt_debug(task, "h1p peer read timeout"); - c = nxt_read_timer_conn(timer); + c = nxt_read_timer_conn(timer); c->block_write = 1; - c->block_read = 1; + c->block_read = 1; - peer = c->socket.data; + peer = c->socket.data; peer->status = NXT_HTTP_GATEWAY_TIMEOUT; r = peer->request; r->state->error_handler(task, r, peer); } - static nxt_msec_t -nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data) -{ - nxt_http_peer_t *peer; +nxt_h1p_peer_timer_value(nxt_conn_t *c, uintptr_t data) { + nxt_http_peer_t *peer; peer = c->socket.data; return nxt_value_at(nxt_msec_t, peer->request->conf->socket_conf, data); } - static void -nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer) -{ - nxt_conn_t *c; +nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer) { + nxt_conn_t *c; nxt_debug(task, "h1p peer close"); peer->closed = 1; - c = peer->proto.h1->conn; - task = &c->task; - c->socket.task = task; - c->read_timer.task = task; + c = peer->proto.h1->conn; + task = &c->task; + c->socket.task = task; + c->read_timer.task = task; c->write_timer.task = task; if (c->socket.fd != -1) { @@ -3011,18 +2858,13 @@ nxt_h1p_peer_close(nxt_task_t *task, nxt_http_peer_t *peer) } } - -static const nxt_conn_state_t nxt_h1p_peer_close_state - nxt_aligned(64) = -{ +static const nxt_conn_state_t nxt_h1p_peer_close_state nxt_aligned(64) = { .ready_handler = nxt_h1p_peer_free, }; - static void -nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -3031,19 +2873,15 @@ nxt_h1p_peer_free(nxt_task_t *task, void *obj, void *data) nxt_conn_free(task, c); } - static nxt_int_t nxt_h1p_peer_transfer_encoding(void *ctx, nxt_http_field_t *field, - uintptr_t data) -{ - nxt_http_request_t *r; + uintptr_t data) { + nxt_http_request_t *r; - r = ctx; + r = ctx; field->skip = 1; - if (field->value_length == 7 - && memcmp(field->value, "chunked", 7) == 0) - { + if (field->value_length == 7 && memcmp(field->value, "chunked", 7) == 0) { r->peer->proto.h1->chunked = 1; } diff --git a/src/nxt_h1proto.h b/src/nxt_h1proto.h index b324db8d1..7aa50fdab 100644 --- a/src/nxt_h1proto.h +++ b/src/nxt_h1proto.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -15,43 +14,41 @@ typedef struct nxt_h1p_websocket_timer_s nxt_h1p_websocket_timer_t; - struct nxt_h1proto_s { - nxt_http_request_parse_t parser; - nxt_http_chunk_parse_t chunked_parse; - nxt_off_t remainder; - - uint8_t nbuffers; - uint8_t header_buffer_slot; - uint8_t large_buffer_slot; - uint8_t keepalive; /* 1 bit */ - uint8_t chunked; /* 1 bit */ - uint8_t websocket; /* 1 bit */ - uint8_t connection_upgrade; /* 1 bit */ - uint8_t upgrade_websocket; /* 1 bit */ - uint8_t websocket_version_ok; /* 1 bit */ - nxt_http_te_t transfer_encoding:8; /* 2 bits */ - - uint8_t websocket_cont_expected; /* 1 bit */ - uint8_t websocket_closed; /* 1 bit */ - - uint32_t header_size; + nxt_http_request_parse_t parser; + nxt_http_chunk_parse_t chunked_parse; + nxt_off_t remainder; + + uint8_t nbuffers; + uint8_t header_buffer_slot; + uint8_t large_buffer_slot; + uint8_t keepalive; /* 1 bit */ + uint8_t chunked; /* 1 bit */ + uint8_t websocket; /* 1 bit */ + uint8_t connection_upgrade; /* 1 bit */ + uint8_t upgrade_websocket; /* 1 bit */ + uint8_t websocket_version_ok; /* 1 bit */ + nxt_http_te_t transfer_encoding:8; /* 2 bits */ + + uint8_t websocket_cont_expected; /* 1 bit */ + uint8_t websocket_closed; /* 1 bit */ + + uint32_t header_size; nxt_http_field_t *websocket_key; nxt_h1p_websocket_timer_t *websocket_timer; - nxt_http_request_t *request; - nxt_buf_t *buffers; + nxt_http_request_t *request; + nxt_buf_t *buffers; - nxt_buf_t **conn_write_tail; + nxt_buf_t **conn_write_tail; /* * All fields before the conn field will * be zeroed in a keep-alive connection. */ - nxt_conn_t *conn; + nxt_conn_t *conn; }; -#define nxt_h1p_is_http11(h1p) \ - ((h1p)->parser.version.s.minor != '0') +#define nxt_h1p_is_http11(h1p) ((h1p)->parser.version.s.minor != '0') -#endif /* _NXT_H1PROTO_H_INCLUDED_ */ +#endif /* _NXT_H1PROTO_H_INCLUDED_ */ diff --git a/src/nxt_h1proto_websocket.c b/src/nxt_h1proto_websocket.c index 7be190f6c..9c0518134 100644 --- a/src/nxt_h1proto_websocket.c +++ b/src/nxt_h1proto_websocket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,77 +10,79 @@ #include typedef struct { - uint16_t code; - uint8_t args; - nxt_str_t desc; + uint16_t code; + uint8_t args; + nxt_str_t desc; } nxt_ws_error_t; -static void nxt_h1p_conn_ws_keepalive(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, - void *data); -static void nxt_h1p_conn_ws_keepalive_disable(nxt_task_t *task, - nxt_h1proto_t *h1p); -static void nxt_h1p_conn_ws_keepalive_enable(nxt_task_t *task, - nxt_h1proto_t *h1p); -static void nxt_h1p_conn_ws_frame_process(nxt_task_t *task, nxt_conn_t *c, +static void +nxt_h1p_conn_ws_keepalive(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_ws_keepalive_disable(nxt_task_t *task, nxt_h1proto_t *h1p); +static void +nxt_h1p_conn_ws_keepalive_enable(nxt_task_t *task, nxt_h1proto_t *h1p); +static void +nxt_h1p_conn_ws_frame_process(nxt_task_t *task, nxt_conn_t *c, nxt_h1proto_t *h1p, nxt_websocket_header_t *wsh); -static void nxt_h1p_conn_ws_error(nxt_task_t *task, void *obj, void *data); -static ssize_t nxt_h1p_ws_io_read_handler(nxt_task_t *task, nxt_conn_t *c); -static void nxt_h1p_conn_ws_timeout(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_ws_frame_payload_read(nxt_task_t *task, void *obj, - void *data); -static void hxt_h1p_send_ws_error(nxt_task_t *task, nxt_http_request_t *r, +static void +nxt_h1p_conn_ws_error(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_h1p_ws_io_read_handler(nxt_task_t *task, nxt_conn_t *c); +static void +nxt_h1p_conn_ws_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_ws_frame_payload_read(nxt_task_t *task, void *obj, void *data); +static void +hxt_h1p_send_ws_error(nxt_task_t *task, nxt_http_request_t *r, const nxt_ws_error_t *err, ...); -static void nxt_h1p_conn_ws_error_sent(nxt_task_t *task, void *obj, void *data); -static void nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data); - -static const nxt_conn_state_t nxt_h1p_read_ws_frame_header_state; -static const nxt_conn_state_t nxt_h1p_read_ws_frame_payload_state; - -static const nxt_ws_error_t nxt_ws_err_out_of_memory = { - NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR, - 0, nxt_string("Out of memory") }; -static const nxt_ws_error_t nxt_ws_err_too_big = { - NXT_WEBSOCKET_CR_MESSAGE_TOO_BIG, - 1, nxt_string("Message too big: %uL bytes") }; -static const nxt_ws_error_t nxt_ws_err_invalid_close_code = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 1, nxt_string("Close code %ud is not valid") }; -static const nxt_ws_error_t nxt_ws_err_going_away = { - NXT_WEBSOCKET_CR_GOING_AWAY, - 0, nxt_string("Remote peer is going away") }; -static const nxt_ws_error_t nxt_ws_err_not_masked = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 0, nxt_string("Not masked client frame") }; -static const nxt_ws_error_t nxt_ws_err_ctrl_fragmented = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 0, nxt_string("Fragmented control frame") }; -static const nxt_ws_error_t nxt_ws_err_ctrl_too_big = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 1, nxt_string("Control frame too big: %uL bytes") }; -static const nxt_ws_error_t nxt_ws_err_invalid_close_len = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 0, nxt_string("Close frame payload length cannot be 1") }; -static const nxt_ws_error_t nxt_ws_err_invalid_opcode = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 1, nxt_string("Unrecognized opcode %ud") }; -static const nxt_ws_error_t nxt_ws_err_cont_expected = { - NXT_WEBSOCKET_CR_PROTOCOL_ERROR, - 1, nxt_string("Continuation expected, but %ud opcode received") }; +static void +nxt_h1p_conn_ws_error_sent(nxt_task_t *task, void *obj, void *data); +static void +nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data); + +static const nxt_conn_state_t nxt_h1p_read_ws_frame_header_state; +static const nxt_conn_state_t nxt_h1p_read_ws_frame_payload_state; + +static const nxt_ws_error_t nxt_ws_err_out_of_memory + = {NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR, 0, nxt_string("Out of memory")}; +static const nxt_ws_error_t nxt_ws_err_too_big + = {NXT_WEBSOCKET_CR_MESSAGE_TOO_BIG, 1, + nxt_string("Message too big: %uL bytes")}; +static const nxt_ws_error_t nxt_ws_err_invalid_close_code + = {NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 1, + nxt_string("Close code %ud is not valid")}; +static const nxt_ws_error_t nxt_ws_err_going_away + = {NXT_WEBSOCKET_CR_GOING_AWAY, 0, nxt_string("Remote peer is going away")}; +static const nxt_ws_error_t nxt_ws_err_not_masked = { + NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 0, nxt_string("Not masked client frame")}; +static const nxt_ws_error_t nxt_ws_err_ctrl_fragmented = { + NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 0, nxt_string("Fragmented control frame")}; +static const nxt_ws_error_t nxt_ws_err_ctrl_too_big + = {NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 1, + nxt_string("Control frame too big: %uL bytes")}; +static const nxt_ws_error_t nxt_ws_err_invalid_close_len + = {NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 0, + nxt_string("Close frame payload length cannot be 1")}; +static const nxt_ws_error_t nxt_ws_err_invalid_opcode = { + NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 1, nxt_string("Unrecognized opcode %ud")}; +static const nxt_ws_error_t nxt_ws_err_cont_expected + = {NXT_WEBSOCKET_CR_PROTOCOL_ERROR, 1, + nxt_string("Continuation expected, but %ud opcode received")}; void nxt_h1p_websocket_first_frame_start(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *ws_frame) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_h1proto_t *h1p; - nxt_websocket_conf_t *websocket_conf; + nxt_buf_t *ws_frame) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_h1proto_t *h1p; + nxt_websocket_conf_t *websocket_conf; nxt_debug(task, "h1p ws first frame start"); h1p = r->proto.h1; - c = h1p->conn; + c = h1p->conn; if (!c->tcp_nodelay) { nxt_conn_tcp_nodelay_on(task, c); @@ -90,37 +91,35 @@ nxt_h1p_websocket_first_frame_start(nxt_task_t *task, nxt_http_request_t *r, websocket_conf = &r->conf->socket_conf->websocket_conf; if (nxt_slow_path(websocket_conf->keepalive_interval != 0)) { - h1p->websocket_timer = nxt_mp_zget(c->mem_pool, - sizeof(nxt_h1p_websocket_timer_t)); + h1p->websocket_timer + = nxt_mp_zget(c->mem_pool, sizeof(nxt_h1p_websocket_timer_t)); if (nxt_slow_path(h1p->websocket_timer == NULL)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_out_of_memory); return; } - h1p->websocket_timer->keepalive_interval = - websocket_conf->keepalive_interval; + h1p->websocket_timer->keepalive_interval + = websocket_conf->keepalive_interval; h1p->websocket_timer->h1p = h1p; - timer = &h1p->websocket_timer->timer; - timer->task = &c->task; + timer = &h1p->websocket_timer->timer; + timer->task = &c->task; timer->work_queue = &task->thread->engine->fast_work_queue; - timer->log = &c->log; - timer->bias = NXT_TIMER_DEFAULT_BIAS; - timer->handler = nxt_h1p_conn_ws_keepalive; + timer->log = &c->log; + timer->bias = NXT_TIMER_DEFAULT_BIAS; + timer->handler = nxt_h1p_conn_ws_keepalive; } nxt_h1p_websocket_frame_start(task, r, ws_frame); } - void nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *ws_frame) -{ + nxt_buf_t *ws_frame) { size_t size; - nxt_buf_t *in; - nxt_conn_t *c; - nxt_h1proto_t *h1p; + nxt_buf_t *in; + nxt_conn_t *c; + nxt_h1proto_t *h1p; nxt_debug(task, "h1p ws frame start"); @@ -130,12 +129,12 @@ nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r, return; } - c = h1p->conn; + c = h1p->conn; c->read = ws_frame; nxt_h1p_complete_buffers(task, h1p, 0); - in = c->read; + in = c->read; c->read_state = &nxt_h1p_read_ws_frame_header_state; if (in == NULL) { @@ -149,29 +148,27 @@ nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r, nxt_memmove(in->mem.start, in->mem.pos, size); - in->mem.pos = in->mem.start; + in->mem.pos = in->mem.start; in->mem.free = in->mem.start + size; nxt_h1p_conn_ws_frame_header_read(task, c, h1p); } } - static void -nxt_h1p_conn_ws_keepalive(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *out; - nxt_timer_t *timer; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; - nxt_websocket_header_t *wsh; - nxt_h1p_websocket_timer_t *ws_timer; +nxt_h1p_conn_ws_keepalive(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *out; + nxt_timer_t *timer; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; + nxt_websocket_header_t *wsh; + nxt_h1p_websocket_timer_t *ws_timer; nxt_debug(task, "h1p conn ws keepalive"); - timer = obj; + timer = obj; ws_timer = nxt_timer_data(timer, nxt_h1p_websocket_timer_t, timer); - h1p = ws_timer->h1p; + h1p = ws_timer->h1p; r = h1p->request; if (nxt_slow_path(r == NULL)) { @@ -187,44 +184,40 @@ nxt_h1p_conn_ws_keepalive(nxt_task_t *task, void *obj, void *data) out->mem.start[0] = 0; out->mem.start[1] = 0; - wsh = (nxt_websocket_header_t *) out->mem.start; + wsh = (nxt_websocket_header_t *) out->mem.start; out->mem.free = nxt_websocket_frame_init(wsh, 0); - wsh->fin = 1; + wsh->fin = 1; wsh->opcode = NXT_WEBSOCKET_OP_PING; nxt_http_request_send(task, r, out); } +static const nxt_conn_state_t nxt_h1p_read_ws_frame_header_state nxt_aligned(64) + = { + .ready_handler = nxt_h1p_conn_ws_frame_header_read, + .close_handler = nxt_h1p_conn_ws_error, + .error_handler = nxt_h1p_conn_ws_error, -static const nxt_conn_state_t nxt_h1p_read_ws_frame_header_state - nxt_aligned(64) = -{ - .ready_handler = nxt_h1p_conn_ws_frame_header_read, - .close_handler = nxt_h1p_conn_ws_error, - .error_handler = nxt_h1p_conn_ws_error, - - .io_read_handler = nxt_h1p_ws_io_read_handler, + .io_read_handler = nxt_h1p_ws_io_read_handler, - .timer_handler = nxt_h1p_conn_ws_timeout, - .timer_value = nxt_h1p_conn_request_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, websocket_conf.read_timeout), - .timer_autoreset = 1, + .timer_handler = nxt_h1p_conn_ws_timeout, + .timer_value = nxt_h1p_conn_request_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, websocket_conf.read_timeout), + .timer_autoreset = 1, }; - static void -nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) { size_t size, hsize, frame_size, max_frame_size; uint64_t payload_len; - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; - nxt_event_engine_t *engine; - nxt_websocket_header_t *wsh; + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; + nxt_event_engine_t *engine; + nxt_websocket_header_t *wsh; - c = obj; + c = obj; h1p = data; nxt_h1p_conn_ws_keepalive_disable(task, h1p); @@ -275,22 +268,20 @@ nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) if (nxt_slow_path(wsh->opcode != NXT_WEBSOCKET_OP_PING && wsh->opcode != NXT_WEBSOCKET_OP_PONG - && wsh->opcode != NXT_WEBSOCKET_OP_CLOSE)) - { + && wsh->opcode != NXT_WEBSOCKET_OP_CLOSE)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_invalid_opcode, - wsh->opcode); + wsh->opcode); return; } if (nxt_slow_path(wsh->payload_len > 125)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_ctrl_too_big, - nxt_websocket_frame_payload_len(wsh)); + nxt_websocket_frame_payload_len(wsh)); return; } if (nxt_slow_path(wsh->opcode == NXT_WEBSOCKET_OP_CLOSE - && wsh->payload_len == 1)) - { + && wsh->payload_len == 1)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_invalid_close_len); return; } @@ -299,16 +290,15 @@ nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) if (h1p->websocket_cont_expected) { if (nxt_slow_path(wsh->opcode != NXT_WEBSOCKET_OP_CONT)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_cont_expected, - wsh->opcode); + wsh->opcode); return; } } else { if (nxt_slow_path(wsh->opcode != NXT_WEBSOCKET_OP_BINARY - && wsh->opcode != NXT_WEBSOCKET_OP_TEXT)) - { + && wsh->opcode != NXT_WEBSOCKET_OP_TEXT)) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_invalid_opcode, - wsh->opcode); + wsh->opcode); return; } } @@ -320,9 +310,8 @@ nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) payload_len = nxt_websocket_frame_payload_len(wsh); - if (nxt_slow_path(hsize > max_frame_size - || payload_len > (max_frame_size - hsize))) - { + if (nxt_slow_path( + hsize > max_frame_size || payload_len > (max_frame_size - hsize))) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_too_big, payload_len); return; } @@ -346,18 +335,16 @@ nxt_h1p_conn_ws_frame_header_read(nxt_task_t *task, void *obj, void *data) nxt_buf_t *b = nxt_buf_mem_alloc(c->mem_pool, frame_size - size, 0); c->read->next = b; - c->read = b; + c->read = b; } nxt_conn_read(engine, c); nxt_h1p_conn_ws_keepalive_enable(task, h1p); } - static void -nxt_h1p_conn_ws_keepalive_disable(nxt_task_t *task, nxt_h1proto_t *h1p) -{ - nxt_timer_t *timer; +nxt_h1p_conn_ws_keepalive_disable(nxt_task_t *task, nxt_h1proto_t *h1p) { + nxt_timer_t *timer; if (h1p->websocket_timer == NULL) { return; @@ -373,11 +360,9 @@ nxt_h1p_conn_ws_keepalive_disable(nxt_task_t *task, nxt_h1proto_t *h1p) nxt_timer_disable(task->thread->engine, timer); } - static void -nxt_h1p_conn_ws_keepalive_enable(nxt_task_t *task, nxt_h1proto_t *h1p) -{ - nxt_timer_t *timer; +nxt_h1p_conn_ws_keepalive_enable(nxt_task_t *task, nxt_h1proto_t *h1p) { + nxt_timer_t *timer; if (h1p->websocket_timer == NULL) { return; @@ -391,18 +376,16 @@ nxt_h1p_conn_ws_keepalive_enable(nxt_task_t *task, nxt_h1proto_t *h1p) } nxt_timer_add(task->thread->engine, timer, - h1p->websocket_timer->keepalive_interval); + h1p->websocket_timer->keepalive_interval); } - static void nxt_h1p_conn_ws_frame_process(nxt_task_t *task, nxt_conn_t *c, - nxt_h1proto_t *h1p, nxt_websocket_header_t *wsh) -{ + nxt_h1proto_t *h1p, nxt_websocket_header_t *wsh) { size_t hsize; - uint8_t *p, *mask; + uint8_t *p, *mask; uint16_t code; - nxt_http_request_t *r; + nxt_http_request_t *r; r = h1p->request; @@ -416,17 +399,16 @@ nxt_h1p_conn_ws_frame_process(nxt_task_t *task, nxt_conn_t *c, if (nxt_slow_path(wsh->opcode == NXT_WEBSOCKET_OP_CLOSE)) { if (wsh->payload_len >= 2) { hsize = nxt_websocket_frame_header_size(wsh); - mask = nxt_pointer_to(wsh, hsize - 4); - p = nxt_pointer_to(wsh, hsize); + mask = nxt_pointer_to(wsh, hsize - 4); + p = nxt_pointer_to(wsh, hsize); code = ((p[0] ^ mask[0]) << 8) + (p[1] ^ mask[1]); if (nxt_slow_path(code < 1000 || code >= 5000 || (code > 1003 && code < 1007) - || (code > 1014 && code < 3000))) - { + || (code > 1014 && code < 3000))) { hxt_h1p_send_ws_error(task, r, &nxt_ws_err_invalid_close_code, - code); + code); return; } } @@ -437,12 +419,10 @@ nxt_h1p_conn_ws_frame_process(nxt_task_t *task, nxt_conn_t *c, r->state->ready_handler(task, r, NULL); } - static void -nxt_h1p_conn_ws_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_h1proto_t *h1p; - nxt_http_request_t *r; +nxt_h1p_conn_ws_error(nxt_task_t *task, void *obj, void *data) { + nxt_h1proto_t *h1p; + nxt_http_request_t *r; h1p = data; @@ -457,13 +437,11 @@ nxt_h1p_conn_ws_error(nxt_task_t *task, void *obj, void *data) } } - static ssize_t -nxt_h1p_ws_io_read_handler(nxt_task_t *task, nxt_conn_t *c) -{ +nxt_h1p_ws_io_read_handler(nxt_task_t *task, nxt_conn_t *c) { size_t size; ssize_t n; - nxt_buf_t *b; + nxt_buf_t *b; b = c->read; @@ -491,28 +469,26 @@ nxt_h1p_ws_io_read_handler(nxt_task_t *task, nxt_conn_t *c) return n; } - static void -nxt_h1p_conn_ws_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; +nxt_h1p_conn_ws_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; timer = obj; nxt_debug(task, "h1p conn ws timeout"); - c = nxt_read_timer_conn(timer); - c->block_read = 1; + c = nxt_read_timer_conn(timer); + c->block_read = 1; /* * Disable SO_LINGER off during socket closing * to send "408 Request Timeout" error response. */ c->socket.timedout = 0; - h1p = c->socket.data; + h1p = c->socket.data; h1p->keepalive = 0; r = h1p->request; @@ -523,31 +499,28 @@ nxt_h1p_conn_ws_timeout(nxt_task_t *task, void *obj, void *data) hxt_h1p_send_ws_error(task, r, &nxt_ws_err_going_away); } - -static const nxt_conn_state_t nxt_h1p_read_ws_frame_payload_state - nxt_aligned(64) = -{ - .ready_handler = nxt_h1p_conn_ws_frame_payload_read, - .close_handler = nxt_h1p_conn_ws_error, - .error_handler = nxt_h1p_conn_ws_error, - - .timer_handler = nxt_h1p_conn_ws_timeout, - .timer_value = nxt_h1p_conn_request_timer_value, - .timer_data = offsetof(nxt_socket_conf_t, websocket_conf.read_timeout), - .timer_autoreset = 1, +static const nxt_conn_state_t nxt_h1p_read_ws_frame_payload_state nxt_aligned( + 64) + = { + .ready_handler = nxt_h1p_conn_ws_frame_payload_read, + .close_handler = nxt_h1p_conn_ws_error, + .error_handler = nxt_h1p_conn_ws_error, + + .timer_handler = nxt_h1p_conn_ws_timeout, + .timer_value = nxt_h1p_conn_request_timer_value, + .timer_data = offsetof(nxt_socket_conf_t, websocket_conf.read_timeout), + .timer_autoreset = 1, }; - static void -nxt_h1p_conn_ws_frame_payload_read(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; - nxt_event_engine_t *engine; - nxt_websocket_header_t *wsh; - - c = obj; +nxt_h1p_conn_ws_frame_payload_read(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; + nxt_event_engine_t *engine; + nxt_websocket_header_t *wsh; + + c = obj; h1p = data; nxt_h1p_conn_ws_keepalive_disable(task, h1p); @@ -573,25 +546,23 @@ nxt_h1p_conn_ws_frame_payload_read(nxt_task_t *task, void *obj, void *data) nxt_h1p_conn_ws_keepalive_enable(task, h1p); } - static void hxt_h1p_send_ws_error(nxt_task_t *task, nxt_http_request_t *r, - const nxt_ws_error_t *err, ...) -{ - u_char *p; + const nxt_ws_error_t *err, ...) { + u_char *p; va_list args; - nxt_buf_t *out; + nxt_buf_t *out; nxt_str_t desc; - nxt_websocket_header_t *wsh; + nxt_websocket_header_t *wsh; u_char buf[125]; if (nxt_slow_path(err->args)) { va_start(args, err); p = nxt_vsprintf(buf, buf + sizeof(buf), (char *) err->desc.start, - args); + args); va_end(args); - desc.start = buf; + desc.start = buf; desc.length = p - buf; } else { @@ -610,16 +581,16 @@ hxt_h1p_send_ws_error(nxt_task_t *task, nxt_http_request_t *r, out->mem.start[1] = 0; wsh = (nxt_websocket_header_t *) out->mem.start; - p = nxt_websocket_frame_init(wsh, sizeof(err->code) + desc.length); + p = nxt_websocket_frame_init(wsh, sizeof(err->code) + desc.length); - wsh->fin = 1; + wsh->fin = 1; wsh->opcode = NXT_WEBSOCKET_OP_CLOSE; *p++ = (err->code >> 8) & 0xFF; *p++ = err->code & 0xFF; out->mem.free = nxt_cpymem(p, desc.start, desc.length); - out->next = nxt_http_buf_last(r); + out->next = nxt_http_buf_last(r); if (out->next != NULL) { out->next->completion_handler = nxt_h1p_conn_ws_error_sent; @@ -628,11 +599,9 @@ hxt_h1p_send_ws_error(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_send(task, r, out); } - static void -nxt_h1p_conn_ws_error_sent(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; +nxt_h1p_conn_ws_error_sent(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; r = data; @@ -641,14 +610,12 @@ nxt_h1p_conn_ws_error_sent(nxt_task_t *task, void *obj, void *data) r->state->error_handler(task, r, r->proto.any); } - static void -nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data) -{ +nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data) { uint8_t payload_len, i; - nxt_buf_t *b, *out, *next; - nxt_http_request_t *r; - nxt_websocket_header_t *wsh; + nxt_buf_t *b, *out, *next; + nxt_http_request_t *r; + nxt_websocket_header_t *wsh; uint8_t mask[4]; nxt_debug(task, "h1p conn ws pong"); @@ -656,7 +623,7 @@ nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data) r = obj; b = r->ws_frame; - wsh = (nxt_websocket_header_t *) b->mem.pos; + wsh = (nxt_websocket_header_t *) b->mem.pos; payload_len = wsh->payload_len; b->mem.pos += 2; @@ -674,19 +641,19 @@ nxt_h1p_conn_ws_pong(nxt_task_t *task, void *obj, void *data) out->mem.start[0] = 0; out->mem.start[1] = 0; - wsh = (nxt_websocket_header_t *) out->mem.start; + wsh = (nxt_websocket_header_t *) out->mem.start; out->mem.free = nxt_websocket_frame_init(wsh, payload_len); - wsh->fin = 1; + wsh->fin = 1; wsh->opcode = NXT_WEBSOCKET_OP_PONG; for (i = 0; i < payload_len; i++) { while (nxt_buf_mem_used_size(&b->mem) == 0) { - next = b->next; + next = b->next; b->next = NULL; nxt_work_queue_add(&task->thread->engine->fast_work_queue, - b->completion_handler, task, b, b->parent); + b->completion_handler, task, b, b->parent); b = next; } diff --git a/src/nxt_hash.h b/src/nxt_hash.h index 01c727d24..34166c59a 100644 --- a/src/nxt_hash.h +++ b/src/nxt_hash.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,38 +6,31 @@ #ifndef _NXT_HASH_H_INCLUDED_ #define _NXT_HASH_H_INCLUDED_ - typedef struct { nxt_lvlhsh_t lvlhsh; - const nxt_lvlhsh_proto_t *proto; - void *pool; + const nxt_lvlhsh_proto_t *proto; + void *pool; } nxt_hash_t; - nxt_inline nxt_int_t -nxt_hash_find(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) -{ +nxt_hash_find(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) { lhq->proto = h->proto; return nxt_lvlhsh_find(&h->lvlhsh, lhq); } - nxt_inline nxt_int_t -nxt_hash_insert(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) -{ +nxt_hash_insert(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) { lhq->proto = h->proto; - lhq->pool = h->pool; + lhq->pool = h->pool; return nxt_lvlhsh_insert(&h->lvlhsh, lhq); } - nxt_inline nxt_int_t -nxt_hash_delete(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) -{ +nxt_hash_delete(nxt_hash_t *h, nxt_lvlhsh_query_t *lhq) { lhq->proto = h->proto; - lhq->pool = h->pool; + lhq->pool = h->pool; return nxt_lvlhsh_delete(&h->lvlhsh, lhq); } diff --git a/src/nxt_hpux_sendfile.c b/src/nxt_hpux_sendfile.c index df200b64c..987489bf3 100644 --- a/src/nxt_hpux_sendfile.c +++ b/src/nxt_hpux_sendfile.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,12 +8,13 @@ #ifdef NXT_TEST_BUILD_HPUX_SENDFILE -ssize_t nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static ssize_t nxt_sys_sendfile(int s, int fd, off_t offset, size_t nbytes, - const struct iovec *hdtrl, int flags) -{ +static ssize_t +nxt_sys_sendfile(int s, int fd, off_t offset, size_t nbytes, + const struct iovec *hdtrl, int flags) { return -1; } @@ -22,30 +22,31 @@ static ssize_t nxt_sys_sendfile(int s, int fd, off_t offset, size_t nbytes, /* sendfile() is not declared if _XOPEN_SOURCE_EXTENDED is defined. */ -sbsize_t sendfile(int s, int fd, off_t offset, bsize_t nbytes, - const struct iovec *hdtrl, int flags); +sbsize_t +sendfile(int s, int fd, off_t offset, bsize_t nbytes, const struct iovec *hdtrl, + int flags); -#define nxt_sys_sendfile sendfile +#define nxt_sys_sendfile sendfile #endif ssize_t -nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) -{ - size_t file_size; - ssize_t n; - nxt_buf_t *fb; - nxt_err_t err; - nxt_uint_t nhd, ntr; - struct iovec iov[NXT_IOBUF_MAX], *hdtrl; - nxt_sendbuf_coalesce_t sb; - - sb.buf = b; +nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, + size_t limit) { + size_t file_size; + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_uint_t nhd, ntr; + struct iovec iov[NXT_IOBUF_MAX], *hdtrl; + nxt_sendbuf_coalesce_t sb; + + sb.buf = b; sb.iobuf = iov; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; nhd = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -67,7 +68,7 @@ nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) } sb.iobuf = &iov[1]; - sb.nmax = 1; + sb.nmax = 1; ntr = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -77,9 +78,9 @@ nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) */ if (nhd == 0) { - hdtrl = NULL; + hdtrl = NULL; iov[0].iov_base = NULL; - iov[0].iov_len = 0; + iov[0].iov_len = 0; } else { hdtrl = iov; @@ -87,18 +88,17 @@ nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) if (ntr == 0) { iov[1].iov_base = NULL; - iov[1].iov_len = 0; + iov[1].iov_len = 0; } else { hdtrl = iov; } nxt_debug(c->socket.task, "sendfile(%d, %FD, @%O, %uz) hd:%ui tr:%ui", - c->socket.fd, fb->file->fd, fb->file_pos, file_size, - nhd, ntr); + c->socket.fd, fb->file->fd, fb->file_pos, file_size, nhd, ntr); - n = nxt_sys_sendfile(c->socket.fd, fb->file->fd, fb->file_pos, - file_size, hdtrl, 0); + n = nxt_sys_sendfile(c->socket.fd, fb->file->fd, fb->file_pos, file_size, + hdtrl, 0); err = (n == -1) ? nxt_errno : 0; @@ -106,7 +106,6 @@ nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -117,9 +116,9 @@ nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "sendfile(%d, %FD, @%O, %uz) failed \"%FN\" hd:%ui tr:%ui", - c->socket.fd, fb->file_pos, file_size, &fb->file->name, - nhd, ntr); + "sendfile(%d, %FD, @%O, %uz) failed \"%FN\" hd:%ui tr:%ui", + c->socket.fd, fb->file_pos, file_size, &fb->file->name, nhd, + ntr); return NXT_ERROR; } diff --git a/src/nxt_http.h b/src/nxt_http.h index 5369c8e16..6ddb19023 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,215 +8,205 @@ #include - typedef enum { - NXT_HTTP_UNSET = -1, + NXT_HTTP_UNSET = -1, NXT_HTTP_INVALID = 0, - NXT_HTTP_CONTINUE = 100, + NXT_HTTP_CONTINUE = 100, NXT_HTTP_SWITCHING_PROTOCOLS = 101, - NXT_HTTP_OK = 200, + NXT_HTTP_OK = 200, NXT_HTTP_NO_CONTENT = 204, - NXT_HTTP_MULTIPLE_CHOICES = 300, - NXT_HTTP_MOVED_PERMANENTLY = 301, - NXT_HTTP_FOUND = 302, - NXT_HTTP_SEE_OTHER = 303, - NXT_HTTP_NOT_MODIFIED = 304, + NXT_HTTP_MULTIPLE_CHOICES = 300, + NXT_HTTP_MOVED_PERMANENTLY = 301, + NXT_HTTP_FOUND = 302, + NXT_HTTP_SEE_OTHER = 303, + NXT_HTTP_NOT_MODIFIED = 304, NXT_HTTP_TEMPORARY_REDIRECT = 307, NXT_HTTP_PERMANENT_REDIRECT = 308, - NXT_HTTP_BAD_REQUEST = 400, - NXT_HTTP_FORBIDDEN = 403, - NXT_HTTP_NOT_FOUND = 404, - NXT_HTTP_METHOD_NOT_ALLOWED = 405, - NXT_HTTP_REQUEST_TIMEOUT = 408, - NXT_HTTP_LENGTH_REQUIRED = 411, - NXT_HTTP_PAYLOAD_TOO_LARGE = 413, - NXT_HTTP_URI_TOO_LONG = 414, - NXT_HTTP_UPGRADE_REQUIRED = 426, + NXT_HTTP_BAD_REQUEST = 400, + NXT_HTTP_FORBIDDEN = 403, + NXT_HTTP_NOT_FOUND = 404, + NXT_HTTP_METHOD_NOT_ALLOWED = 405, + NXT_HTTP_REQUEST_TIMEOUT = 408, + NXT_HTTP_LENGTH_REQUIRED = 411, + NXT_HTTP_PAYLOAD_TOO_LARGE = 413, + NXT_HTTP_URI_TOO_LONG = 414, + NXT_HTTP_UPGRADE_REQUIRED = 426, NXT_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431, NXT_HTTP_TO_HTTPS = 497, NXT_HTTP_INTERNAL_SERVER_ERROR = 500, - NXT_HTTP_NOT_IMPLEMENTED = 501, - NXT_HTTP_BAD_GATEWAY = 502, - NXT_HTTP_SERVICE_UNAVAILABLE = 503, - NXT_HTTP_GATEWAY_TIMEOUT = 504, + NXT_HTTP_NOT_IMPLEMENTED = 501, + NXT_HTTP_BAD_GATEWAY = 502, + NXT_HTTP_SERVICE_UNAVAILABLE = 503, + NXT_HTTP_GATEWAY_TIMEOUT = 504, NXT_HTTP_VERSION_NOT_SUPPORTED = 505, - NXT_HTTP_SERVER_ERROR_MAX = 599, + NXT_HTTP_SERVER_ERROR_MAX = 599, NXT_HTTP_STATUS_MAX = 999, } nxt_http_status_t; - typedef enum { - NXT_HTTP_TE_NONE = 0, - NXT_HTTP_TE_CHUNKED = 1, + NXT_HTTP_TE_NONE = 0, + NXT_HTTP_TE_CHUNKED = 1, NXT_HTTP_TE_UNSUPPORTED = 2, } nxt_http_te_t; - typedef enum { NXT_HTTP_PROTO_H1 = 0, NXT_HTTP_PROTO_H2, NXT_HTTP_PROTO_DEVNULL, } nxt_http_protocol_t; - typedef struct { - nxt_work_handler_t ready_handler; - nxt_work_handler_t error_handler; + nxt_work_handler_t ready_handler; + nxt_work_handler_t error_handler; } nxt_http_request_state_t; -typedef struct nxt_h1proto_s nxt_h1proto_t; +typedef struct nxt_h1proto_s nxt_h1proto_t; struct nxt_h1p_websocket_timer_s { - nxt_timer_t timer; - nxt_h1proto_t *h1p; - nxt_msec_t keepalive_interval; + nxt_timer_t timer; + nxt_h1proto_t *h1p; + nxt_msec_t keepalive_interval; }; - typedef union { - void *any; - nxt_h1proto_t *h1; + void *any; + nxt_h1proto_t *h1; } nxt_http_proto_t; - -#define nxt_http_field_name_set(_field, _name) \ - do { \ - (_field)->name_length = nxt_length(_name); \ - (_field)->name = (u_char *) _name; \ +#define nxt_http_field_name_set(_field, _name) \ + do { \ + (_field)->name_length = nxt_length(_name); \ + (_field)->name = (u_char *) _name; \ } while (0) -#define nxt_http_field_set(_field, _name, _value) \ - do { \ - (_field)->name_length = nxt_length(_name); \ - (_field)->value_length = nxt_length(_value); \ - (_field)->name = (u_char *) _name; \ - (_field)->value = (u_char *) _value; \ +#define nxt_http_field_set(_field, _name, _value) \ + do { \ + (_field)->name_length = nxt_length(_name); \ + (_field)->value_length = nxt_length(_value); \ + (_field)->name = (u_char *) _name; \ + (_field)->value = (u_char *) _value; \ } while (0) - typedef struct { - nxt_list_t *fields; - nxt_http_field_t *date; - nxt_http_field_t *content_type; - nxt_http_field_t *content_length; - nxt_off_t content_length_n; + nxt_list_t *fields; + nxt_http_field_t *date; + nxt_http_field_t *content_type; + nxt_http_field_t *content_length; + nxt_off_t content_length_n; } nxt_http_response_t; -typedef struct nxt_upstream_server_s nxt_upstream_server_t; +typedef struct nxt_upstream_server_s nxt_upstream_server_t; typedef struct { - nxt_http_proto_t proto; - nxt_http_request_t *request; - nxt_upstream_server_t *server; - nxt_list_t *fields; - nxt_buf_t *body; - - nxt_http_status_t status:16; - nxt_http_protocol_t protocol:8; /* 2 bits */ - uint8_t header_received; /* 1 bit */ - uint8_t closed; /* 1 bit */ + nxt_http_proto_t proto; + nxt_http_request_t *request; + nxt_upstream_server_t *server; + nxt_list_t *fields; + nxt_buf_t *body; + + nxt_http_status_t status :16; + nxt_http_protocol_t protocol:8; /* 2 bits */ + uint8_t header_received; /* 1 bit */ + uint8_t closed; /* 1 bit */ } nxt_http_peer_t; - struct nxt_http_request_s { - nxt_http_proto_t proto; - nxt_socket_conf_joint_t *conf; - - nxt_mp_t *mem_pool; - - nxt_buf_t *body; - nxt_buf_t *ws_frame; - nxt_buf_t *out; - const nxt_http_request_state_t *state; - - nxt_nsec_t start_time; - - nxt_str_t host; - nxt_str_t server_name; - nxt_str_t request_line; - nxt_str_t target; - nxt_str_t version; - nxt_str_t *method; - nxt_str_t *path; - nxt_str_t *args; - - nxt_str_t args_decoded; - nxt_array_t *arguments; /* of nxt_http_name_value_t */ - nxt_array_t *cookies; /* of nxt_http_name_value_t */ - nxt_list_t *fields; - nxt_http_field_t *content_type; - nxt_http_field_t *content_length; - nxt_http_field_t *chunked_field; - nxt_http_field_t *cookie; - nxt_http_field_t *referer; - nxt_http_field_t *user_agent; - nxt_http_field_t *authorization; - nxt_off_t content_length_n; - - nxt_sockaddr_t *remote; - nxt_sockaddr_t *local; - nxt_task_t task; - - nxt_timer_t timer; - void *timer_data; - - nxt_tstr_query_t *tstr_query; - nxt_tstr_cache_t tstr_cache; - - nxt_http_action_t *action; - void *req_rpc_data; + nxt_http_proto_t proto; + nxt_socket_conf_joint_t *conf; + + nxt_mp_t *mem_pool; + + nxt_buf_t *body; + nxt_buf_t *ws_frame; + nxt_buf_t *out; + const nxt_http_request_state_t *state; + + nxt_nsec_t start_time; + + nxt_str_t host; + nxt_str_t server_name; + nxt_str_t request_line; + nxt_str_t target; + nxt_str_t version; + nxt_str_t *method; + nxt_str_t *path; + nxt_str_t *args; + + nxt_str_t args_decoded; + nxt_array_t *arguments; /* of nxt_http_name_value_t */ + nxt_array_t *cookies; /* of nxt_http_name_value_t */ + nxt_list_t *fields; + nxt_http_field_t *content_type; + nxt_http_field_t *content_length; + nxt_http_field_t *chunked_field; + nxt_http_field_t *cookie; + nxt_http_field_t *referer; + nxt_http_field_t *user_agent; + nxt_http_field_t *authorization; + nxt_off_t content_length_n; + + nxt_sockaddr_t *remote; + nxt_sockaddr_t *local; + nxt_task_t task; + + nxt_timer_t timer; + void *timer_data; + + nxt_tstr_query_t *tstr_query; + nxt_tstr_cache_t tstr_cache; + + nxt_http_action_t *action; + void *req_rpc_data; #if (NXT_HAVE_REGEX) - nxt_regex_match_t *regex_match; + nxt_regex_match_t *regex_match; #endif - nxt_http_peer_t *peer; - nxt_buf_t *last; + nxt_http_peer_t *peer; + nxt_buf_t *last; - nxt_queue_link_t app_link; /* nxt_app_t.ack_waiting_req */ - nxt_event_engine_t *engine; - nxt_work_t err_work; + nxt_queue_link_t app_link; /* nxt_app_t.ack_waiting_req */ + nxt_event_engine_t *engine; + nxt_work_t err_work; - nxt_http_response_t resp; + nxt_http_response_t resp; - nxt_http_status_t status:16; + nxt_http_status_t status:16; - uint8_t log_route; /* 1 bit */ - uint8_t quoted_target; /* 1 bit */ - uint8_t uri_changed; /* 1 bit */ + uint8_t log_route; /* 1 bit */ + uint8_t quoted_target; /* 1 bit */ + uint8_t uri_changed; /* 1 bit */ - uint8_t pass_count; /* 8 bits */ - uint8_t app_target; - nxt_http_protocol_t protocol:8; /* 2 bits */ - uint8_t tls; /* 1 bit */ - uint8_t logged; /* 1 bit */ - uint8_t header_sent; /* 1 bit */ - uint8_t inconsistent; /* 1 bit */ - uint8_t error; /* 1 bit */ - uint8_t websocket_handshake; /* 1 bit */ - uint8_t chunked; /* 1 bit */ + uint8_t pass_count; /* 8 bits */ + uint8_t app_target; + nxt_http_protocol_t protocol:8; /* 2 bits */ + uint8_t tls; /* 1 bit */ + uint8_t logged; /* 1 bit */ + uint8_t header_sent; /* 1 bit */ + uint8_t inconsistent; /* 1 bit */ + uint8_t error; /* 1 bit */ + uint8_t websocket_handshake; /* 1 bit */ + uint8_t chunked; /* 1 bit */ }; - typedef struct { - uint16_t hash; - uint16_t name_length; - uint32_t value_length; - u_char *name; - u_char *value; + uint16_t hash; + uint16_t name_length; + uint32_t value_length; + u_char *name; + u_char *value; } nxt_http_name_value_t; - typedef enum { NXT_HTTP_URI_ENCODING_NONE = 0, NXT_HTTP_URI_ENCODING, @@ -225,53 +214,50 @@ typedef enum { } nxt_http_uri_encoding_t; -typedef struct nxt_http_route_s nxt_http_route_t; -typedef struct nxt_http_route_rule_s nxt_http_route_rule_t; -typedef struct nxt_http_route_addr_rule_s nxt_http_route_addr_rule_t; - +typedef struct nxt_http_route_s nxt_http_route_t; +typedef struct nxt_http_route_rule_s nxt_http_route_rule_t; +typedef struct nxt_http_route_addr_rule_s nxt_http_route_addr_rule_t; typedef struct { - nxt_conf_value_t *rewrite; - nxt_conf_value_t *set_headers; - nxt_conf_value_t *pass; - nxt_conf_value_t *ret; - nxt_conf_value_t *location; - nxt_conf_value_t *proxy; - nxt_conf_value_t *share; - nxt_conf_value_t *index; - nxt_str_t chroot; - nxt_conf_value_t *follow_symlinks; - nxt_conf_value_t *traverse_mounts; - nxt_conf_value_t *types; - nxt_conf_value_t *fallback; + nxt_conf_value_t *rewrite; + nxt_conf_value_t *set_headers; + nxt_conf_value_t *pass; + nxt_conf_value_t *ret; + nxt_conf_value_t *location; + nxt_conf_value_t *proxy; + nxt_conf_value_t *share; + nxt_conf_value_t *index; + nxt_str_t chroot; + nxt_conf_value_t *follow_symlinks; + nxt_conf_value_t *traverse_mounts; + nxt_conf_value_t *types; + nxt_conf_value_t *fallback; } nxt_http_action_conf_t; - struct nxt_http_action_s { - nxt_http_action_t *(*handler)(nxt_task_t *task, - nxt_http_request_t *r, - nxt_http_action_t *action); + nxt_http_action_t *(*handler)(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); + union { - void *conf; - nxt_http_route_t *route; - nxt_upstream_t *upstream; - uint32_t upstream_number; - nxt_tstr_t *tstr; - nxt_str_t *pass; + void *conf; + nxt_http_route_t *route; + nxt_upstream_t *upstream; + uint32_t upstream_number; + nxt_tstr_t *tstr; + nxt_str_t *pass; } u; - nxt_tstr_t *rewrite; - nxt_array_t *set_headers; /* of nxt_http_field_t */ - nxt_http_action_t *fallback; + nxt_tstr_t *rewrite; + nxt_array_t *set_headers; /* of nxt_http_field_t */ + nxt_http_action_t *fallback; }; - typedef struct { - void (*body_read)(nxt_task_t *task, nxt_http_request_t *r); - void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r); - void (*header_send)(nxt_task_t *task, nxt_http_request_t *r, + void (*body_read)(nxt_task_t *task, nxt_http_request_t *r); + void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r); + void (*header_send)(nxt_task_t *task, nxt_http_request_t *r, nxt_work_handler_t body_handler, void *data); - void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); + void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto); void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last); void (*close)(nxt_task_t *task, nxt_http_proto_t proto, @@ -287,163 +273,206 @@ typedef struct { nxt_buf_t *ws_frame); } nxt_http_proto_table_t; - typedef struct { - nxt_str_t *header; - uint32_t header_hash; + nxt_str_t *header; + uint32_t header_hash; } nxt_http_forward_header_t; - struct nxt_http_forward_s { nxt_http_forward_header_t client_ip; nxt_http_forward_header_t protocol; - nxt_http_route_addr_rule_t *source; - uint8_t recursive; /* 1 bit */ + nxt_http_route_addr_rule_t *source; + uint8_t recursive; /* 1 bit */ }; - -#define NXT_HTTP_DATE_LEN nxt_length("Wed, 31 Dec 1986 16:40:00 GMT") +#define NXT_HTTP_DATE_LEN nxt_length("Wed, 31 Dec 1986 16:40:00 GMT") nxt_inline u_char * -nxt_http_date(u_char *buf, struct tm *tm) -{ - static const char * const week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", - "Fri", "Sat" }; +nxt_http_date(u_char *buf, struct tm *tm) { + static const char *const week[] + = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; - static const char * const month[] = { "Jan", "Feb", "Mar", "Apr", "May", - "Jun", "Jul", "Aug", "Sep", "Oct", - "Nov", "Dec" }; + static const char *const month[] = {"Jan", "Feb", "Mar", "Apr", "May", + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; return nxt_sprintf(buf, buf + NXT_HTTP_DATE_LEN, - "%s, %02d %s %4d %02d:%02d:%02d GMT", - week[tm->tm_wday], tm->tm_mday, - month[tm->tm_mon], tm->tm_year + 1900, - tm->tm_hour, tm->tm_min, tm->tm_sec); + "%s, %02d %s %4d %02d:%02d:%02d GMT", week[tm->tm_wday], tm->tm_mday, + month[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour, tm->tm_min, + tm->tm_sec); } - -nxt_int_t nxt_http_init(nxt_task_t *task); -nxt_int_t nxt_h1p_init(nxt_task_t *task); -nxt_int_t nxt_http_response_hash_init(nxt_task_t *task); - -void nxt_http_conn_init(nxt_task_t *task, void *obj, void *data); -nxt_http_request_t *nxt_http_request_create(nxt_task_t *task); -void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r, +nxt_int_t +nxt_http_init(nxt_task_t *task); +nxt_int_t +nxt_h1p_init(nxt_task_t *task); +nxt_int_t +nxt_http_response_hash_init(nxt_task_t *task); + +void +nxt_http_conn_init(nxt_task_t *task, void *obj, void *data); +nxt_http_request_t * +nxt_http_request_create(nxt_task_t *task); +void +nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r, nxt_http_status_t status); -void nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r); -void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r); +void +nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, nxt_work_handler_t body_handler, void *data); -void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *ws_frame); -void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *out); -nxt_buf_t *nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, - size_t size); -nxt_buf_t *nxt_http_buf_last(nxt_http_request_t *r); -void nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data); -void nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data); - -nxt_int_t nxt_http_request_host(void *ctx, nxt_http_field_t *field, - uintptr_t data); -nxt_int_t nxt_http_request_field(void *ctx, nxt_http_field_t *field, - uintptr_t offset); -nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field, +void +nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); +nxt_buf_t * +nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size); +nxt_buf_t * +nxt_http_buf_last(nxt_http_request_t *r); +void +nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data); +void +nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data); + +nxt_int_t +nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data); +nxt_int_t +nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset); +nxt_int_t +nxt_http_request_content_length(void *ctx, nxt_http_field_t *field, uintptr_t data); -nxt_array_t *nxt_http_arguments_parse(nxt_http_request_t *r); -nxt_array_t *nxt_http_cookies_parse(nxt_http_request_t *r); - -int64_t nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, - nxt_bool_t case_sensitive, uint8_t encoding); -int64_t nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name); -int64_t nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name); -int64_t nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name); - -nxt_http_routes_t *nxt_http_routes_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *routes_conf); -nxt_http_action_t *nxt_http_action_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_str_t *pass); -nxt_int_t nxt_http_routes_resolve(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf); -nxt_int_t nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass, - nxt_str_t *segments, nxt_uint_t n); -nxt_http_action_t *nxt_http_pass_application(nxt_task_t *task, - nxt_router_conf_t *rtcf, nxt_str_t *name); -nxt_http_route_addr_rule_t *nxt_http_route_addr_rule_create( - nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv); -nxt_int_t nxt_http_route_addr_rule(nxt_http_request_t *r, +nxt_array_t * +nxt_http_arguments_parse(nxt_http_request_t *r); +nxt_array_t * +nxt_http_cookies_parse(nxt_http_request_t *r); + +int64_t +nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, + uint8_t encoding); +int64_t +nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name); +int64_t +nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name); +int64_t +nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name); + +nxt_http_routes_t * +nxt_http_routes_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_conf_value_t *routes_conf); +nxt_http_action_t * +nxt_http_action_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_str_t *pass); +nxt_int_t +nxt_http_routes_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); +nxt_int_t +nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass, nxt_str_t *segments, + nxt_uint_t n); +nxt_http_action_t * +nxt_http_pass_application(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_str_t *name); +nxt_http_route_addr_rule_t * +nxt_http_route_addr_rule_create(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *cv); +nxt_int_t +nxt_http_route_addr_rule(nxt_http_request_t *r, nxt_http_route_addr_rule_t *addr_rule, nxt_sockaddr_t *sockaddr); -nxt_http_route_rule_t *nxt_http_route_types_rule_create(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *types); -nxt_int_t nxt_http_route_test_rule(nxt_http_request_t *r, - nxt_http_route_rule_t *rule, u_char *start, size_t length); - -nxt_int_t nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, +nxt_http_route_rule_t * +nxt_http_route_types_rule_create(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *types); +nxt_int_t +nxt_http_route_test_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule, + u_char *start, size_t length); + +nxt_int_t +nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv, nxt_http_action_t *action); -void nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r, nxt_http_action_t *action); -nxt_int_t nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, +nxt_int_t +nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *conf); -nxt_int_t nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf, - nxt_upstream_t ***upstream_joint); +nxt_int_t +nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf, + nxt_upstream_t ***upstream_joint); -nxt_int_t nxt_http_rewrite_init(nxt_router_conf_t *rtcf, - nxt_http_action_t *action, nxt_http_action_conf_t *acf); -nxt_int_t nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r); +nxt_int_t +nxt_http_rewrite_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, + nxt_http_action_conf_t *acf); +nxt_int_t +nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r); -nxt_int_t nxt_http_set_headers_init(nxt_router_conf_t *rtcf, - nxt_http_action_t *action, nxt_http_action_conf_t *acf); -nxt_int_t nxt_http_set_headers(nxt_http_request_t *r); +nxt_int_t +nxt_http_set_headers_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, + nxt_http_action_conf_t *acf); +nxt_int_t +nxt_http_set_headers(nxt_http_request_t *r); -nxt_int_t nxt_http_return_init(nxt_router_conf_t *rtcf, - nxt_http_action_t *action, nxt_http_action_conf_t *acf); +nxt_int_t +nxt_http_return_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, + nxt_http_action_conf_t *acf); -nxt_int_t nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, +nxt_int_t +nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_http_action_t *action, nxt_http_action_conf_t *acf); -nxt_int_t nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash); -nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash, +nxt_int_t +nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash); +nxt_int_t +nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash, const nxt_str_t *exten, nxt_str_t *type); -nxt_str_t *nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, - const nxt_str_t *exten); +nxt_str_t * +nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten); -nxt_http_action_t *nxt_http_application_handler(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); -nxt_int_t nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name, +nxt_http_action_t * +nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r, nxt_http_action_t *action); -nxt_http_action_t *nxt_upstream_proxy_handler(nxt_task_t *task, - nxt_http_request_t *r, nxt_upstream_t *upstream); +nxt_int_t +nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name, + nxt_http_action_t *action); +nxt_http_action_t * +nxt_upstream_proxy_handler(nxt_task_t *task, nxt_http_request_t *r, + nxt_upstream_t *upstream); -nxt_int_t nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action, +nxt_int_t +nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action, nxt_http_action_conf_t *acf); -nxt_int_t nxt_http_proxy_date(void *ctx, nxt_http_field_t *field, - uintptr_t data); -nxt_int_t nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field, +nxt_int_t +nxt_http_proxy_date(void *ctx, nxt_http_field_t *field, uintptr_t data); +nxt_int_t +nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field, uintptr_t data); -nxt_int_t nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field, - uintptr_t data); -nxt_buf_t *nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r, +nxt_int_t +nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field, uintptr_t data); +nxt_buf_t * +nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r, size_t size); -void nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *b); -extern nxt_time_string_t nxt_http_date_cache; +extern nxt_time_string_t nxt_http_date_cache; -extern nxt_lvlhsh_t nxt_response_fields_hash; +extern nxt_lvlhsh_t nxt_response_fields_hash; -extern const nxt_http_proto_table_t nxt_http_proto[]; +extern const nxt_http_proto_table_t nxt_http_proto[]; -void nxt_h1p_websocket_first_frame_start(nxt_task_t *task, - nxt_http_request_t *r, nxt_buf_t *ws_frame); -void nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_h1p_websocket_first_frame_start(nxt_task_t *task, nxt_http_request_t *r, + nxt_buf_t *ws_frame); +void +nxt_h1p_websocket_frame_start(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *ws_frame); -void nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, - nxt_bool_t all); -nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data); +void +nxt_h1p_complete_buffers(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_bool_t all); +nxt_msec_t +nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data); -int nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r, +int +nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r, nxt_tstr_cond_t *cond); -extern const nxt_conn_state_t nxt_h1p_idle_close_state; +extern const nxt_conn_state_t nxt_h1p_idle_close_state; -#endif /* _NXT_HTTP_H_INCLUDED_ */ +#endif /* _NXT_HTTP_H_INCLUDED_ */ diff --git a/src/nxt_http_chunk_parse.c b/src/nxt_http_chunk_parse.c index a43ce75b8..2d3678e0d 100644 --- a/src/nxt_http_chunk_parse.c +++ b/src/nxt_http_chunk_parse.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,30 +6,30 @@ #include -#define NXT_HTTP_CHUNK_MIDDLE 0 -#define NXT_HTTP_CHUNK_END_ON_BORDER 1 -#define NXT_HTTP_CHUNK_END 2 +#define NXT_HTTP_CHUNK_MIDDLE 0 +#define NXT_HTTP_CHUNK_END_ON_BORDER 1 +#define NXT_HTTP_CHUNK_END 2 -#define nxt_size_is_sufficient(cs) \ +#define nxt_size_is_sufficient(cs) \ (cs < ((__typeof__(cs)) 1 << (sizeof(cs) * 8 - 4))) -static nxt_int_t nxt_http_chunk_buffer(nxt_http_chunk_parse_t *hcp, - nxt_buf_t ***tail, nxt_buf_t *in); - +static nxt_int_t +nxt_http_chunk_buffer(nxt_http_chunk_parse_t *hcp, nxt_buf_t ***tail, + nxt_buf_t *in); -static void nxt_http_chunk_buf_completion(nxt_task_t *task, void *obj, - void *data); +static void +nxt_http_chunk_buf_completion(nxt_task_t *task, void *obj, void *data); nxt_buf_t * nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, - nxt_buf_t *in) -{ - u_char c, ch; - nxt_int_t ret; - nxt_buf_t *b, *out, *next, **tail; + nxt_buf_t *in) { + u_char c, ch; + nxt_int_t ret; + nxt_buf_t *b, *out, *next, **tail; + enum { sw_start = 0, sw_chunk_size, @@ -41,13 +40,12 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, } state; next = NULL; - out = NULL; + out = NULL; tail = &out; state = hcp->state; for (b = in; b != NULL; b = next) { - while (b->mem.pos < b->mem.free) { /* * The sw_chunk state is tested outside the switch @@ -77,7 +75,6 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, ch = *b->mem.pos++; switch (state) { - case sw_start: state = sw_chunk_size; @@ -125,14 +122,13 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, case sw_chunk_size_linefeed: if (nxt_fast_path(ch == '\n')) { - if (hcp->chunk_size != 0) { state = sw_chunk; continue; } hcp->last = 1; - state = sw_chunk_end_newline; + state = sw_chunk_end_newline; continue; } @@ -148,7 +144,6 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, case sw_chunk_end_linefeed: if (nxt_fast_path(ch == '\n')) { - if (!hcp->last) { state = sw_start; continue; @@ -171,13 +166,12 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, if (b->retain == 0) { /* No chunk data was found in a buffer. */ nxt_work_queue_add(&task->thread->engine->fast_work_queue, - b->completion_handler, task, b, b->parent); - + b->completion_handler, task, b, b->parent); } next: - next = b->next; + next = b->next; b->next = NULL; } @@ -192,16 +186,14 @@ nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, return out; } - static nxt_int_t nxt_http_chunk_buffer(nxt_http_chunk_parse_t *hcp, nxt_buf_t ***tail, - nxt_buf_t *in) -{ - u_char *p; + nxt_buf_t *in) { + u_char *p; size_t size; - nxt_buf_t *b; + nxt_buf_t *b; - p = in->mem.pos; + p = in->mem.pos; size = in->mem.free - p; b = nxt_buf_mem_alloc(hcp->mem_pool, 0, 0); @@ -210,28 +202,28 @@ nxt_http_chunk_buffer(nxt_http_chunk_parse_t *hcp, nxt_buf_t ***tail, } **tail = b; - *tail = &b->next; + *tail = &b->next; nxt_mp_retain(hcp->mem_pool); b->completion_handler = nxt_http_chunk_buf_completion; b->parent = in; in->retain++; - b->mem.pos = p; + b->mem.pos = p; b->mem.start = p; if (hcp->chunk_size < size) { - p += hcp->chunk_size; - in->mem.pos = p; + p += hcp->chunk_size; + in->mem.pos = p; b->mem.free = p; - b->mem.end = p; + b->mem.end = p; return NXT_HTTP_CHUNK_END; } b->mem.free = in->mem.free; - b->mem.end = in->mem.free; + b->mem.end = in->mem.free; hcp->chunk_size -= size; @@ -242,12 +234,10 @@ nxt_http_chunk_buffer(nxt_http_chunk_parse_t *hcp, nxt_buf_t ***tail, return NXT_HTTP_CHUNK_MIDDLE; } - static void -nxt_http_chunk_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b, *next, *parent; +nxt_http_chunk_buf_completion(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b, *next, *parent; b = obj; @@ -256,9 +246,9 @@ nxt_http_chunk_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_assert(data == b->parent); do { - next = b->next; + next = b->next; parent = b->parent; - mp = b->data; + mp = b->data; nxt_mp_free(mp, b); nxt_mp_release(mp); diff --git a/src/nxt_http_error.c b/src/nxt_http_error.c index 370b12dbc..7f5c55c6a 100644 --- a/src/nxt_http_error.c +++ b/src/nxt_http_error.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,27 +7,24 @@ #include -static void nxt_http_request_send_error_body(nxt_task_t *task, void *r, - void *data); +static void +nxt_http_request_send_error_body(nxt_task_t *task, void *r, void *data); -static const nxt_http_request_state_t nxt_http_request_send_error_body_state; +static const nxt_http_request_state_t nxt_http_request_send_error_body_state; -static const char error[] = - "" - "Error %03d" - "

Error %03d.\r\n"; +static const char error[] = "" + "Error %03d" + "

Error %03d.\r\n"; /* Two %03d (4 chars) patterns are replaced by status code (3 chars). */ -#define NXT_HTTP_ERROR_LEN (nxt_length(error) - 2) - +#define NXT_HTTP_ERROR_LEN (nxt_length(error) - 2) void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_status_t status) -{ - nxt_http_field_t *content_type; + nxt_http_status_t status) { + nxt_http_field_t *content_type; nxt_debug(task, "http request error: %d", status); @@ -52,13 +48,13 @@ nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r, nxt_http_field_set(content_type, "Content-Type", "text/html"); - r->resp.content_length = NULL; + r->resp.content_length = NULL; r->resp.content_length_n = NXT_HTTP_ERROR_LEN; r->state = &nxt_http_request_send_error_body_state; - nxt_http_request_header_send(task, r, - nxt_http_request_send_error_body, NULL); + nxt_http_request_header_send(task, r, nxt_http_request_send_error_body, + NULL); return; fail: @@ -66,19 +62,16 @@ nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error_handler(task, r, r->proto.any); } - -static const nxt_http_request_state_t nxt_http_request_send_error_body_state - nxt_aligned(64) = -{ - .error_handler = nxt_http_request_error_handler, +static const nxt_http_request_state_t + nxt_http_request_send_error_body_state nxt_aligned(64) + = { + .error_handler = nxt_http_request_error_handler, }; - static void -nxt_http_request_send_error_body(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *out; - nxt_http_request_t *r; +nxt_http_request_send_error_body(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *out; + nxt_http_request_t *r; r = obj; @@ -89,8 +82,8 @@ nxt_http_request_send_error_body(nxt_task_t *task, void *obj, void *data) goto fail; } - out->mem.free = nxt_sprintf(out->mem.pos, out->mem.end, error, - r->status, r->status); + out->mem.free + = nxt_sprintf(out->mem.pos, out->mem.end, error, r->status, r->status); out->next = nxt_http_buf_last(r); diff --git a/src/nxt_http_js.c b/src/nxt_http_js.c index 34689fba1..1e09dc6b3 100644 --- a/src/nxt_http_js.c +++ b/src/nxt_http_js.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,27 +7,32 @@ #include -static njs_int_t nxt_http_js_ext_uri(njs_vm_t *vm, njs_object_prop_t *prop, +static njs_int_t +nxt_http_js_ext_uri(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, + njs_value_t *setval, njs_value_t *retval); +static njs_int_t +nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, + njs_value_t *setval, njs_value_t *retval); +static njs_int_t +nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop, + njs_value_t *value, njs_value_t *setval, njs_value_t *retval); +static njs_int_t +nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, + njs_value_t *value, njs_value_t *setval, njs_value_t *retval); +static njs_int_t +nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); -static njs_int_t nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, +static njs_int_t +nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, + njs_value_t *keys); +static njs_int_t +nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); -static njs_int_t nxt_http_js_ext_remote_addr(njs_vm_t *vm, - njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, - njs_value_t *retval); -static njs_int_t nxt_http_js_ext_get_args(njs_vm_t *vm, - njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, - njs_value_t *retval); -static njs_int_t nxt_http_js_ext_get_header(njs_vm_t *vm, - njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, - njs_value_t *retval); -static njs_int_t nxt_http_js_ext_keys_header(njs_vm_t *vm, - njs_value_t *value, njs_value_t *keys); -static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm, - njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, - njs_value_t *retval); -static njs_int_t nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, +static njs_int_t +nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys); -static njs_int_t nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop, +static njs_int_t +nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval, njs_value_t *retval); @@ -100,19 +104,15 @@ static njs_external_t nxt_http_js_proto[] = { }, }; - void -nxt_http_register_js_proto(nxt_js_conf_t *jcf) -{ +nxt_http_register_js_proto(nxt_js_conf_t *jcf) { nxt_js_set_proto(jcf, nxt_http_js_proto, njs_nitems(nxt_http_js_proto)); } - static njs_int_t -nxt_http_js_ext_uri(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ - nxt_http_request_t *r; +nxt_http_js_ext_uri(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, + njs_value_t *setval, njs_value_t *retval) { + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -121,15 +121,13 @@ nxt_http_js_ext_uri(njs_vm_t *vm, njs_object_prop_t *prop, } return njs_vm_value_string_create(vm, retval, r->path->start, - r->path->length); + r->path->length); } - static njs_int_t -nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ - nxt_http_request_t *r; +nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, njs_value_t *value, + njs_value_t *setval, njs_value_t *retval) { + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -138,15 +136,13 @@ nxt_http_js_ext_host(njs_vm_t *vm, njs_object_prop_t *prop, } return njs_vm_value_string_create(vm, retval, r->host.start, - r->host.length); + r->host.length); } - static njs_int_t nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ - nxt_http_request_t *r; + njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -155,20 +151,17 @@ nxt_http_js_ext_remote_addr(njs_vm_t *vm, njs_object_prop_t *prop, } return njs_vm_value_string_create(vm, retval, - nxt_sockaddr_address(r->remote), - r->remote->address_length); + nxt_sockaddr_address(r->remote), r->remote->address_length); } - static njs_int_t nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ - u_char *start; + njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { + u_char *start; njs_int_t ret; - njs_value_t *args; + njs_value_t *args; njs_opaque_value_t val; - nxt_http_request_t *r; + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -179,7 +172,7 @@ nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, args = njs_value_arg(&val); start = (r->args->start != NULL) ? r->args->start : (u_char *) ""; - ret = njs_vm_query_string_parse(vm, start, start + r->args->length, args); + ret = njs_vm_query_string_parse(vm, start, start + r->args->length, args); if (ret == NJS_ERROR) { return NJS_ERROR; @@ -190,15 +183,13 @@ nxt_http_js_ext_get_args(njs_vm_t *vm, njs_object_prop_t *prop, return NJS_OK; } - static njs_int_t nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ + njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { njs_int_t rc; njs_str_t key; - nxt_http_field_t *f; - nxt_http_request_t *r; + nxt_http_field_t *f; + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -213,28 +204,25 @@ nxt_http_js_ext_get_header(njs_vm_t *vm, njs_object_prop_t *prop, } nxt_list_each(f, r->fields) { - if (key.length == f->name_length - && memcmp(key.start, f->name, f->name_length) == 0) - { + && memcmp(key.start, f->name, f->name_length) == 0) { return njs_vm_value_string_create(vm, retval, f->value, - f->value_length); + f->value_length); } - - } nxt_list_loop; + } + nxt_list_loop; njs_value_undefined_set(retval); return NJS_DECLINED; } - static njs_int_t -nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) -{ +nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, + njs_value_t *keys) { njs_int_t rc; - nxt_http_field_t *f; - nxt_http_request_t *r; + nxt_http_field_t *f; + nxt_http_request_t *r; rc = njs_vm_array_alloc(vm, keys, 4); if (rc != NJS_OK) { @@ -247,7 +235,6 @@ nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) } nxt_list_each(f, r->fields) { - value = njs_vm_array_push(vm, keys); if (value == NULL) { return NJS_ERROR; @@ -257,22 +244,20 @@ nxt_http_js_ext_keys_header(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) if (rc != NJS_OK) { return NJS_ERROR; } - - } nxt_list_loop; + } + nxt_list_loop; return NJS_OK; } - static njs_int_t nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ + njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { njs_int_t rc; njs_str_t key; - nxt_array_t *cookies; - nxt_http_request_t *r; - nxt_http_name_value_t *nv, *start, *end; + nxt_array_t *cookies; + nxt_http_request_t *r; + nxt_http_name_value_t *nv, *start, *end; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -292,15 +277,13 @@ nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, } start = cookies->elts; - end = start + cookies->nelts; + end = start + cookies->nelts; for (nv = start; nv < end; nv++) { - if (key.length == nv->name_length - && memcmp(key.start, nv->name, nv->name_length) == 0) - { + && memcmp(key.start, nv->name, nv->name_length) == 0) { return njs_vm_value_string_create(vm, retval, nv->value, - nv->value_length); + nv->value_length); } } @@ -309,14 +292,13 @@ nxt_http_js_ext_get_cookie(njs_vm_t *vm, njs_object_prop_t *prop, return NJS_DECLINED; } - static njs_int_t -nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) -{ +nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, + njs_value_t *keys) { njs_int_t rc; - nxt_array_t *cookies; - nxt_http_request_t *r; - nxt_http_name_value_t *nv, *start, *end; + nxt_array_t *cookies; + nxt_http_request_t *r; + nxt_http_name_value_t *nv, *start, *end; rc = njs_vm_array_alloc(vm, keys, 4); if (rc != NJS_OK) { @@ -334,10 +316,9 @@ nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) } start = cookies->elts; - end = start + cookies->nelts; + end = start + cookies->nelts; for (nv = start; nv < end; nv++) { - value = njs_vm_array_push(vm, keys); if (value == NULL) { return NJS_ERROR; @@ -352,16 +333,14 @@ nxt_http_js_ext_keys_cookie(njs_vm_t *vm, njs_value_t *value, njs_value_t *keys) return NJS_OK; } - static njs_int_t nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop, - njs_value_t *value, njs_value_t *setval, njs_value_t *retval) -{ + njs_value_t *value, njs_value_t *setval, njs_value_t *retval) { njs_int_t rc; njs_str_t key; nxt_str_t name, *vv; - nxt_router_conf_t *rtcf; - nxt_http_request_t *r; + nxt_router_conf_t *rtcf; + nxt_http_request_t *r; r = njs_vm_external(vm, nxt_js_proto_id, value); if (r == NULL) { @@ -377,7 +356,7 @@ nxt_http_js_ext_get_var(njs_vm_t *vm, njs_object_prop_t *prop, rtcf = r->conf->socket_conf->router_conf; - name.start = key.start; + name.start = key.start; name.length = key.length; vv = nxt_var_get(&r->task, rtcf->tstr_state, &r->tstr_cache.var, &name, r); diff --git a/src/nxt_http_parse.c b/src/nxt_http_parse.c index dd490e729..e370deda3 100644 --- a/src/nxt_http_parse.c +++ b/src/nxt_http_parse.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Valentin V. Bartenev @@ -7,78 +6,140 @@ #include -static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); -static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); -static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); -static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); -static u_char *nxt_http_lookup_field_end(u_char *p, const u_char *end); -static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); - -static nxt_int_t nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_int_t +nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); +static nxt_int_t +nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); +static nxt_int_t +nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); +static nxt_int_t +nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); +static u_char * +nxt_http_lookup_field_end(u_char *p, const u_char *end); +static nxt_int_t +nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); -static nxt_int_t nxt_http_field_hash_collision(nxt_lvlhsh_query_t *lhq, - void *data); +static nxt_int_t +nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_int_t +nxt_http_field_hash_collision(nxt_lvlhsh_query_t *lhq, void *data); -#define NXT_HTTP_MAX_FIELD_NAME 0xFF -#define NXT_HTTP_MAX_FIELD_VALUE NXT_INT32_T_MAX -#define NXT_HTTP_FIELD_LVLHSH_SHIFT 5 +#define NXT_HTTP_MAX_FIELD_NAME 0xFF +#define NXT_HTTP_MAX_FIELD_VALUE NXT_INT32_T_MAX +#define NXT_HTTP_FIELD_LVLHSH_SHIFT 5 typedef enum { - NXT_HTTP_TARGET_SPACE = 1, /* \s */ - NXT_HTTP_TARGET_HASH, /* # */ + NXT_HTTP_TARGET_SPACE = 1, /* \s */ + NXT_HTTP_TARGET_HASH, /* # */ NXT_HTTP_TARGET_AGAIN, - NXT_HTTP_TARGET_BAD, /* \0\r\n */ + NXT_HTTP_TARGET_BAD, /* \0\r\n */ /* traps below are used for extended check only */ - NXT_HTTP_TARGET_SLASH = 5, /* / */ - NXT_HTTP_TARGET_DOT, /* . */ - NXT_HTTP_TARGET_ARGS_MARK, /* ? */ - NXT_HTTP_TARGET_QUOTE_MARK, /* % */ + NXT_HTTP_TARGET_SLASH = 5, /* / */ + NXT_HTTP_TARGET_DOT, /* . */ + NXT_HTTP_TARGET_ARGS_MARK, /* ? */ + NXT_HTTP_TARGET_QUOTE_MARK, /* % */ } nxt_http_target_traps_e; - -static const uint8_t nxt_http_target_chars[256] nxt_aligned(64) = { +static const uint8_t nxt_http_target_chars[256] nxt_aligned(64) = { /* \0 \n \r */ - 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 4, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 4, + 0, + 0, + 4, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, /* \s ! " # $ % & ' ( ) * + , - . / */ - 1, 0, 0, 2, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 6, 5, + 1, + 0, + 0, + 2, + 0, + 8, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 6, + 5, /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, }; - nxt_inline nxt_http_target_traps_e -nxt_http_parse_target(u_char **pos, const u_char *end) -{ - u_char *p; - nxt_uint_t trap; +nxt_http_parse_target(u_char **pos, const u_char *end) { + u_char *p; + nxt_uint_t trap; p = *pos; while (nxt_fast_path(end - p >= 10)) { +#define nxt_target_test_char(ch) \ + \ + trap = nxt_http_target_chars[ch]; \ + \ + if (nxt_slow_path(trap != 0)) { \ + *pos = &(ch); \ + return trap; \ + } -#define nxt_target_test_char(ch) \ - \ - trap = nxt_http_target_chars[ch]; \ - \ - if (nxt_slow_path(trap != 0)) { \ - *pos = &(ch); \ - return trap; \ - } - -/* enddef */ + /* enddef */ nxt_target_test_char(p[0]); nxt_target_test_char(p[1]); @@ -97,16 +158,15 @@ nxt_http_parse_target(u_char **pos, const u_char *end) } while (p != end) { - nxt_target_test_char(*p); p++; + nxt_target_test_char(*p); + p++; } return NXT_HTTP_TARGET_AGAIN; } - nxt_int_t -nxt_http_parse_request_init(nxt_http_request_parse_t *rp, nxt_mp_t *mp) -{ +nxt_http_parse_request_init(nxt_http_request_parse_t *rp, nxt_mp_t *mp) { rp->mem_pool = mp; rp->fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t)); @@ -119,11 +179,9 @@ nxt_http_parse_request_init(nxt_http_request_parse_t *rp, nxt_mp_t *mp) return NXT_OK; } - nxt_int_t -nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) -{ - nxt_int_t rc; +nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) { + nxt_int_t rc; if (rp->handler == NULL) { rp->handler = &nxt_http_parse_request_line; @@ -136,11 +194,9 @@ nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) return rc; } - nxt_int_t -nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) -{ - nxt_int_t rc; +nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) { + nxt_int_t rc; if (rp->handler == NULL) { rp->handler = &nxt_http_parse_field_name; @@ -153,36 +209,32 @@ nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b) return rc; } - static nxt_int_t nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, - const u_char *end) -{ - u_char *p, ch, *after_slash, *args; - nxt_int_t rc; - nxt_bool_t rest; - nxt_http_ver_t ver; - nxt_http_target_traps_e trap; + const u_char *end) { + u_char *p, ch, *after_slash, *args; + nxt_int_t rc; + nxt_bool_t rest; + nxt_http_ver_t ver; + nxt_http_target_traps_e trap; - static const nxt_http_ver_t http11 = { "HTTP/1.1" }; - static const nxt_http_ver_t http10 = { "HTTP/1.0" }; + static const nxt_http_ver_t http11 = {"HTTP/1.1"}; + static const nxt_http_ver_t http10 = {"HTTP/1.0"}; p = *pos; rp->method.start = p; - for ( ;; ) { - + for (;;) { while (nxt_fast_path(end - p >= 8)) { +#define nxt_method_test_char(ch) \ + \ + if (nxt_slow_path((ch) < 'A' || (ch) > 'Z')) { \ + p = &(ch); \ + goto method_unusual_char; \ + } -#define nxt_method_test_char(ch) \ - \ - if (nxt_slow_path((ch) < 'A' || (ch) > 'Z')) { \ - p = &(ch); \ - goto method_unusual_char; \ - } - -/* enddef */ + /* enddef */ nxt_method_test_char(p[0]); nxt_method_test_char(p[1]); @@ -198,7 +250,8 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, } while (p != end) { - nxt_method_test_char(*p); p++; + nxt_method_test_char(*p); + p++; } rp->method.length = p - rp->method.start; @@ -251,12 +304,12 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, rp->target_start = p; after_slash = p + 1; - args = NULL; - rest = 0; + args = NULL; + rest = 0; continue_target: - for ( ;; ) { + for (;;) { p++; trap = nxt_http_parse_target(&p, end); @@ -311,7 +364,7 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, rest = 1; - for ( ;; ) { + for (;;) { p++; trap = nxt_http_parse_target(&p, end); @@ -343,7 +396,6 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, space_after_target: if (nxt_slow_path(end - p < 10)) { - do { p++; @@ -354,7 +406,6 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, } while (*p == ' '); if (memcmp(p, "HTTP/", nxt_min(end - p, 5)) == 0) { - switch (end - p) { case 8: if (p[7] < '0' || p[7] > '9') { @@ -376,7 +427,7 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, } } - //rp->space_in_target = 1; + // rp->space_in_target = 1; if (rest) { goto rest_of_target; @@ -388,14 +439,13 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, /* " HTTP/1.1\r\n" or " HTTP/1.1\n" */ if (nxt_slow_path(p[9] != '\r' && p[9] != '\n')) { - if (p[1] == ' ') { /* surplus space after tartet */ p++; goto space_after_target; } - //rp->space_in_target = 1; + // rp->space_in_target = 1; if (rest) { goto rest_of_target; @@ -406,16 +456,13 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, nxt_memcpy(ver.str, &p[1], 8); - if (nxt_fast_path(ver.ui64 == http11.ui64 - || ver.ui64 == http10.ui64 + if (nxt_fast_path(ver.ui64 == http11.ui64 || ver.ui64 == http10.ui64 || (memcmp(ver.str, "HTTP/1.", 7) == 0 - && ver.s.minor >= '0' && ver.s.minor <= '9'))) - { + && ver.s.minor >= '0' && ver.s.minor <= '9'))) { rp->version.ui64 = ver.ui64; p += 9; if (nxt_fast_path(*p == '\r')) { - if (nxt_slow_path(p + 1 == end)) { return NXT_AGAIN; } @@ -448,7 +495,7 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, rp->path.length = args - rp->target_start - 1; rp->args.length = rp->target_end - args; - rp->args.start = args; + rp->args.start = args; } else { rp->path.length = rp->target_end - rp->target_start; @@ -457,11 +504,9 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, return nxt_http_parse_field_name(rp, pos, end); } - if (memcmp(ver.s.prefix, "HTTP/", 5) == 0 - && ver.s.major >= '0' && ver.s.major <= '9' - && ver.s.point == '.' - && ver.s.minor >= '0' && ver.s.minor <= '9') - { + if (memcmp(ver.s.prefix, "HTTP/", 5) == 0 && ver.s.major >= '0' + && ver.s.major <= '9' && ver.s.point == '.' && ver.s.minor >= '0' + && ver.s.minor <= '9') { rp->version.ui64 = ver.ui64; return NXT_HTTP_PARSE_UNSUPPORTED_VERSION; } @@ -469,12 +514,10 @@ nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos, return NXT_HTTP_PARSE_INVALID; } - static nxt_int_t nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos, - const u_char *end) -{ - u_char *p, ch; + const u_char *end) { + u_char *p, ch; p = *pos; @@ -507,52 +550,58 @@ nxt_http_parse_unusual_target(nxt_http_request_parse_t *rp, u_char **pos, return NXT_HTTP_PARSE_INVALID; } - static nxt_int_t nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, - const u_char *end) -{ - u_char *p, c; - size_t len; - uint32_t hash; - - static const u_char normal[256] nxt_aligned(64) = - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" - /* \s ! " # $ % & ' ( ) * + , . / : ; < = > ? */ - "\0\1\0\1\1\1\1\1\0\0\1\1\0" "-" "\1\0" "0123456789" "\0\0\0\0\0\0" - - /* @ [ \ ] ^ _ */ - "\0" "abcdefghijklmnopqrstuvwxyz" "\0\0\0\1\1" - /* ` { | } ~ */ - "\1" "abcdefghijklmnopqrstuvwxyz" "\0\1\0\1\0" - - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" - "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; - - p = *pos + rp->field_name.length; + const u_char *end) { + u_char *p, c; + size_t len; + uint32_t hash; + + static const u_char normal[256] nxt_aligned(64) + = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + /* \s ! " # $ % & ' ( ) * + , . / : ; < = > ? + */ + "\0\1\0\1\1\1\1\1\0\0\1\1\0" + "-" + "\1\0" + "0123456789" + "\0\0\0\0\0\0" + + /* @ [ \ ] ^ _ */ + "\0" + "abcdefghijklmnopqrstuvwxyz" + "\0\0\0\1\1" + /* ` { | } ~ */ + "\1" + "abcdefghijklmnopqrstuvwxyz" + "\0\1\0\1\0" + + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; + + p = *pos + rp->field_name.length; hash = rp->field_hash; while (nxt_fast_path(end - p >= 8)) { - -#define nxt_field_name_test_char(ch) \ - \ - c = normal[ch]; \ - \ - if (nxt_slow_path(c <= '\1')) { \ - if (c == '\0') { \ - p = &(ch); \ - goto name_end; \ - } \ - \ - rp->skip_field = rp->discard_unsafe_fields; \ - c = ch; \ - } \ - \ - hash = nxt_http_field_hash_char(hash, c); - -/* enddef */ +#define nxt_field_name_test_char(ch) \ + \ + c = normal[ch]; \ + \ + if (nxt_slow_path(c <= '\1')) { \ + if (c == '\0') { \ + p = &(ch); \ + goto name_end; \ + } \ + \ + rp->skip_field = rp->discard_unsafe_fields; \ + c = ch; \ + } \ + \ + hash = nxt_http_field_hash_char(hash, c); + + /* enddef */ nxt_field_name_test_char(p[0]); nxt_field_name_test_char(p[1]); @@ -568,7 +617,8 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, } while (nxt_fast_path(p != end)) { - nxt_field_name_test_char(*p); p++; + nxt_field_name_test_char(*p); + p++; } len = p - *pos; @@ -577,7 +627,7 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, return NXT_HTTP_PARSE_TOO_LARGE_FIELD; } - rp->field_hash = hash; + rp->field_hash = hash; rp->field_name.length = len; rp->handler = &nxt_http_parse_field_name; @@ -600,7 +650,7 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, rp->field_hash = hash; rp->field_name.length = len; - rp->field_name.start = *pos; + rp->field_name.start = *pos; *pos = p + 1; @@ -614,19 +664,17 @@ nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos, return nxt_http_parse_field_end(rp, pos, end); } - static nxt_int_t nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, - const u_char *end) -{ - u_char *p, *start, ch; + const u_char *end) { + u_char *p, *start, ch; size_t len; p = *pos; - for ( ;; ) { + for (;;) { if (nxt_slow_path(p == end)) { - *pos = p; + *pos = p; rp->handler = &nxt_http_parse_field_value; return NXT_AGAIN; } @@ -644,7 +692,7 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, p += rp->field_value.length; - for ( ;; ) { + for (;;) { p = nxt_http_lookup_field_end(p, end); if (nxt_slow_path(p == end)) { @@ -657,7 +705,7 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, } rp->field_value.length = len; - rp->handler = &nxt_http_parse_field_value; + rp->handler = &nxt_http_parse_field_value; return NXT_AGAIN; } @@ -677,7 +725,6 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, *pos = p; if (nxt_fast_path(p != start)) { - while (p[-1] == ' ' || p[-1] == '\t') { p--; } @@ -690,24 +737,21 @@ nxt_http_parse_field_value(nxt_http_request_parse_t *rp, u_char **pos, } rp->field_value.length = len; - rp->field_value.start = start; + rp->field_value.start = start; return nxt_http_parse_field_end(rp, pos, end); } - static u_char * -nxt_http_lookup_field_end(u_char *p, const u_char *end) -{ +nxt_http_lookup_field_end(u_char *p, const u_char *end) { while (nxt_fast_path(end - p >= 16)) { +#define nxt_field_end_test_char(ch) \ + \ + if (nxt_slow_path((ch) < 0x20)) { \ + return &(ch); \ + } -#define nxt_field_end_test_char(ch) \ - \ - if (nxt_slow_path((ch) < 0x20)) { \ - return &(ch); \ - } - -/* enddef */ + /* enddef */ nxt_field_end_test_char(p[0]); nxt_field_end_test_char(p[1]); @@ -733,7 +777,6 @@ nxt_http_lookup_field_end(u_char *p, const u_char *end) } while (nxt_fast_path(end - p >= 4)) { - nxt_field_end_test_char(p[0]); nxt_field_end_test_char(p[1]); nxt_field_end_test_char(p[2]); @@ -744,13 +787,16 @@ nxt_http_lookup_field_end(u_char *p, const u_char *end) switch (end - p) { case 3: - nxt_field_end_test_char(*p); p++; + nxt_field_end_test_char(*p); + p++; /* Fall through. */ case 2: - nxt_field_end_test_char(*p); p++; + nxt_field_end_test_char(*p); + p++; /* Fall through. */ case 1: - nxt_field_end_test_char(*p); p++; + nxt_field_end_test_char(*p); + p++; /* Fall through. */ case 0: break; @@ -761,13 +807,11 @@ nxt_http_lookup_field_end(u_char *p, const u_char *end) return p; } - static nxt_int_t nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos, - const u_char *end) -{ - u_char *p; - nxt_http_field_t *field; + const u_char *end) { + u_char *p; + nxt_http_field_t *field; p = *pos; @@ -794,19 +838,19 @@ nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos, return NXT_ERROR; } - field->hash = nxt_http_field_hash_end(rp->field_hash); - field->skip = 0; + field->hash = nxt_http_field_hash_end(rp->field_hash); + field->skip = 0; field->hopbyhop = 0; - field->name_length = rp->field_name.length; + field->name_length = rp->field_name.length; field->value_length = rp->field_value.length; - field->name = rp->field_name.start; - field->value = rp->field_value.start; + field->name = rp->field_name.start; + field->value = rp->field_value.start; } rp->field_hash = NXT_HTTP_FIELD_HASH_INIT; - rp->field_name.length = 0; + rp->field_name.length = 0; rp->field_value.length = 0; rp->handler = &nxt_http_parse_field_name; @@ -819,36 +863,33 @@ nxt_http_parse_field_end(nxt_http_request_parse_t *rp, u_char **pos, return NXT_HTTP_PARSE_INVALID; } - -#define nxt_http_is_normal(c) \ +#define nxt_http_is_normal(c) \ (nxt_fast_path((nxt_http_normal[c / 8] & (1 << (c & 7))) != 0)) -static const uint8_t nxt_http_normal[32] nxt_aligned(32) = { +static const uint8_t nxt_http_normal[32] nxt_aligned(32) = { - /* \0 \r \n */ - 0xFE, 0xDB, 0xFF, 0xFF, /* 1111 1110 1101 1011 1111 1111 1111 1111 */ + /* \0 \r \n */ + 0xFE, 0xDB, 0xFF, 0xFF, /* 1111 1110 1101 1011 1111 1111 1111 1111 */ - /* '&%$ #"! /.-, |*)( 7654 3210 ?>=< ;:98 */ - 0xD6, 0x37, 0xFF, 0x7F, /* 1101 0110 0011 0111 1111 1111 0111 1111 */ + /* '&%$ #"! /.-, |*)( 7654 3210 ?>=< ;:98 */ + 0xD6, 0x37, 0xFF, 0x7F, /* 1101 0110 0011 0111 1111 1111 0111 1111 */ - /* GFED CBA@ ONML KJIH WVUT SRQP _^]\ [ZYX */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + /* GFED CBA@ ONML KJIH WVUT SRQP _^]\ [ZYX */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - /* gfed cba` onml kjih wvut srqp ~}| {zyx */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + /* gfed cba` onml kjih wvut srqp ~}| {zyx */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xFF, 0xFF, 0xFF, 0xFF, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; - nxt_int_t -nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) -{ - u_char *p, *u, c, ch, high, *args; +nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) { + u_char *p, *u, c, ch, high, *args; enum { sw_normal = 0, @@ -861,9 +902,9 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) nxt_prefetch(nxt_http_normal); - state = sw_normal; + state = sw_normal; saved_state = sw_normal; - p = rp->target_start; + p = rp->target_start; u = nxt_mp_alloc(rp->mem_pool, rp->target_end - p + 1); if (nxt_slow_path(u == NULL)) { @@ -871,19 +912,17 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) } rp->path.length = 0; - rp->path.start = u; + rp->path.start = u; high = '\0'; args = NULL; while (p < rp->target_end) { - ch = *p++; again: switch (state) { - case sw_normal: if (nxt_http_is_normal(ch)) { @@ -894,11 +933,11 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) switch (ch) { case '/': state = sw_slash; - *u++ = ch; + *u++ = ch; continue; case '%': saved_state = state; - state = sw_quoted; + state = sw_quoted; continue; case '?': args = p; @@ -916,7 +955,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (nxt_http_is_normal(ch)) { state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } @@ -925,11 +964,11 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) continue; case '.': state = sw_dot; - *u++ = ch; + *u++ = ch; continue; case '%': saved_state = state; - state = sw_quoted; + state = sw_quoted; continue; case '?': args = p; @@ -938,7 +977,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) goto done; default: state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } @@ -948,7 +987,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (nxt_http_is_normal(ch)) { state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } @@ -959,11 +998,11 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) continue; case '.': state = sw_dot_dot; - *u++ = ch; + *u++ = ch; continue; case '%': saved_state = state; - state = sw_quoted; + state = sw_quoted; continue; case '?': u--; @@ -974,7 +1013,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) goto done; default: state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } @@ -984,18 +1023,17 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (nxt_http_is_normal(ch)) { state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } switch (ch) { - case '/': case '?': case '#': u -= 5; - for ( ;; ) { + for (;;) { if (u < rp->path.start) { return NXT_HTTP_PARSE_INVALID; } @@ -1022,12 +1060,12 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) case '%': saved_state = state; - state = sw_quoted; + state = sw_quoted; continue; default: state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } @@ -1037,14 +1075,14 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) rp->quoted_target = 1; if (ch >= '0' && ch <= '9') { - high = (u_char) (ch - '0'); + high = (u_char) (ch - '0'); state = sw_quoted_second; continue; } c = (u_char) (ch | 0x20); if (c >= 'a' && c <= 'f') { - high = (u_char) (c - 'a' + 10); + high = (u_char) (c - 'a' + 10); state = sw_quoted_second; continue; } @@ -1057,7 +1095,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (ch == '%') { state = sw_normal; - *u++ = '%'; + *u++ = '%'; if (rp->encoded_slashes) { *u++ = '2'; @@ -1069,7 +1107,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (ch == '#') { state = sw_normal; - *u++ = '#'; + *u++ = '#'; continue; } @@ -1087,15 +1125,15 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (ch == '?') { state = sw_normal; - *u++ = ch; + *u++ = ch; continue; } if (ch == '/' && rp->encoded_slashes) { state = sw_normal; - *u++ = '%'; - *u++ = '2'; - *u++ = p[-1]; /* 'f' or 'F' */ + *u++ = '%'; + *u++ = '2'; + *u++ = p[-1]; /* 'f' or 'F' */ continue; } @@ -1127,7 +1165,7 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) if (args != NULL) { rp->args.length = p - args; - rp->args.start = args; + rp->args.start = args; } done: @@ -1137,20 +1175,17 @@ nxt_http_parse_complex_target(nxt_http_request_parse_t *rp) return NXT_OK; } - -const nxt_lvlhsh_proto_t nxt_http_fields_hash_proto nxt_aligned(64) = { +const nxt_lvlhsh_proto_t nxt_http_fields_hash_proto nxt_aligned(64) = { NXT_LVLHSH_BUCKET_SIZE(64), - { NXT_HTTP_FIELD_LVLHSH_SHIFT, 0, 0, 0, 0, 0, 0, 0 }, + {NXT_HTTP_FIELD_LVLHSH_SHIFT, 0, 0, 0, 0, 0, 0, 0}, nxt_http_field_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - static nxt_int_t -nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_http_field_proc_t *field; +nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_http_field_proc_t *field; field = data; @@ -1161,41 +1196,37 @@ nxt_http_field_hash_test(nxt_lvlhsh_query_t *lhq, void *data) return NXT_DECLINED; } - static nxt_int_t -nxt_http_field_hash_collision(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_http_field_hash_collision(nxt_lvlhsh_query_t *lhq, void *data) { return NXT_OK; } - nxt_int_t -nxt_http_fields_hash(nxt_lvlhsh_t *hash, - nxt_http_field_proc_t items[], nxt_uint_t count) -{ - u_char ch; - uint32_t key; - nxt_str_t *name; - nxt_int_t ret; - nxt_uint_t i, j; - nxt_lvlhsh_query_t lhq; +nxt_http_fields_hash(nxt_lvlhsh_t *hash, nxt_http_field_proc_t items[], + nxt_uint_t count) { + u_char ch; + uint32_t key; + nxt_str_t *name; + nxt_int_t ret; + nxt_uint_t i, j; + nxt_lvlhsh_query_t lhq; lhq.replace = 0; - lhq.proto = &nxt_http_fields_hash_proto; - lhq.pool = NULL; + lhq.proto = &nxt_http_fields_hash_proto; + lhq.pool = NULL; for (i = 0; i < count; i++) { - key = NXT_HTTP_FIELD_HASH_INIT; + key = NXT_HTTP_FIELD_HASH_INIT; name = &items[i].name; for (j = 0; j < name->length; j++) { - ch = nxt_lowcase(name->start[j]); + ch = nxt_lowcase(name->start[j]); key = nxt_http_field_hash_char(key, ch); } lhq.key_hash = nxt_http_field_hash_end(key) & 0xFFFF; - lhq.key = *name; - lhq.value = &items[i]; + lhq.key = *name; + lhq.value = &items[i]; ret = nxt_lvlhsh_insert(hash, &lhq); @@ -1207,39 +1238,37 @@ nxt_http_fields_hash(nxt_lvlhsh_t *hash, return NXT_OK; } - nxt_uint_t nxt_http_fields_hash_collisions(nxt_lvlhsh_t *hash, - nxt_http_field_proc_t items[], nxt_uint_t count, nxt_bool_t level) -{ - u_char ch; - uint32_t key, mask; - nxt_str_t *name; - nxt_uint_t colls, i, j; - nxt_lvlhsh_proto_t proto; - nxt_lvlhsh_query_t lhq; - - proto = nxt_http_fields_hash_proto; + nxt_http_field_proc_t items[], nxt_uint_t count, nxt_bool_t level) { + u_char ch; + uint32_t key, mask; + nxt_str_t *name; + nxt_uint_t colls, i, j; + nxt_lvlhsh_proto_t proto; + nxt_lvlhsh_query_t lhq; + + proto = nxt_http_fields_hash_proto; proto.test = nxt_http_field_hash_collision; lhq.replace = 0; - lhq.proto = &proto; + lhq.proto = &proto; mask = level ? (1 << NXT_HTTP_FIELD_LVLHSH_SHIFT) - 1 : 0xFFFF; colls = 0; for (i = 0; i < count; i++) { - key = NXT_HTTP_FIELD_HASH_INIT; + key = NXT_HTTP_FIELD_HASH_INIT; name = &items[i].name; for (j = 0; j < name->length; j++) { - ch = nxt_lowcase(name->start[j]); + ch = nxt_lowcase(name->start[j]); key = nxt_http_field_hash_char(key, ch); } lhq.key_hash = nxt_http_field_hash_end(key) & mask; - lhq.value = &items[i]; + lhq.value = &items[i]; if (nxt_lvlhsh_insert(hash, &lhq) == NXT_DECLINED) { colls++; @@ -1249,21 +1278,18 @@ nxt_http_fields_hash_collisions(nxt_lvlhsh_t *hash, return colls; } - nxt_int_t -nxt_http_fields_process(nxt_list_t *fields, nxt_lvlhsh_t *hash, void *ctx) -{ +nxt_http_fields_process(nxt_list_t *fields, nxt_lvlhsh_t *hash, void *ctx) { nxt_int_t ret; - nxt_http_field_t *field; + nxt_http_field_t *field; nxt_list_each(field, fields) { - ret = nxt_http_field_process(field, hash, ctx); if (nxt_slow_path(ret != NXT_OK)) { return ret; } - - } nxt_list_loop; + } + nxt_list_loop; return NXT_OK; } diff --git a/src/nxt_http_parse.h b/src/nxt_http_parse.h index 9e2f6fabb..f67e6a72a 100644 --- a/src/nxt_http_parse.h +++ b/src/nxt_http_parse.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Valentin V. Bartenev @@ -7,7 +6,6 @@ #ifndef _NXT_HTTP_PARSER_H_INCLUDED_ #define _NXT_HTTP_PARSER_H_INCLUDED_ - typedef enum { NXT_HTTP_PARSE_INVALID = 1, NXT_HTTP_PARSE_UNSUPPORTED_VERSION, @@ -15,133 +13,128 @@ typedef enum { } nxt_http_parse_error_t; -typedef struct nxt_http_request_parse_s nxt_http_request_parse_t; -typedef struct nxt_http_field_s nxt_http_field_t; -typedef struct nxt_http_fields_hash_s nxt_http_fields_hash_t; - +typedef struct nxt_http_request_parse_s nxt_http_request_parse_t; +typedef struct nxt_http_field_s nxt_http_field_t; +typedef struct nxt_http_fields_hash_s nxt_http_fields_hash_t; typedef union { - u_char str[8]; - uint64_t ui64; + u_char str[8]; + uint64_t ui64; struct { - u_char prefix[5]; - u_char major; - u_char point; - u_char minor; + u_char prefix[5]; + u_char major; + u_char point; + u_char minor; } s; } nxt_http_ver_t; - struct nxt_http_request_parse_s { - nxt_int_t (*handler)(nxt_http_request_parse_t *rp, - u_char **pos, const u_char *end); + nxt_int_t (*handler)(nxt_http_request_parse_t *rp, u_char **pos, + const u_char *end); - nxt_str_t method; + nxt_str_t method; - u_char *target_start; - u_char *target_end; - u_char *request_line_end; + u_char *target_start; + u_char *target_end; + u_char *request_line_end; - nxt_str_t path; - nxt_str_t args; + nxt_str_t path; + nxt_str_t args; - nxt_http_ver_t version; + nxt_http_ver_t version; - nxt_list_t *fields; - nxt_mp_t *mem_pool; + nxt_list_t *fields; + nxt_mp_t *mem_pool; - nxt_str_t field_name; - nxt_str_t field_value; + nxt_str_t field_name; + nxt_str_t field_value; - uint32_t field_hash; + uint32_t field_hash; - uint8_t skip_field; /* 1 bit */ - uint8_t discard_unsafe_fields; /* 1 bit */ + uint8_t skip_field; /* 1 bit */ + uint8_t discard_unsafe_fields; /* 1 bit */ /* target with "/." */ - uint8_t complex_target; /* 1 bit */ + uint8_t complex_target; /* 1 bit */ /* target with "%" */ - uint8_t quoted_target; /* 1 bit */ + uint8_t quoted_target; /* 1 bit */ #if 0 /* target with " " */ uint8_t space_in_target; /* 1 bit */ #endif /* Preserve encoded '/' (%2F) and '%' (%25). */ - uint8_t encoded_slashes; /* 1 bit */ + uint8_t encoded_slashes; /* 1 bit */ }; - typedef nxt_int_t (*nxt_http_field_handler_t)(void *ctx, - nxt_http_field_t *field, - uintptr_t data); - + nxt_http_field_t *field, uintptr_t data); typedef struct { - nxt_str_t name; - nxt_http_field_handler_t handler; - uintptr_t data; + nxt_str_t name; + nxt_http_field_handler_t handler; + uintptr_t data; } nxt_http_field_proc_t; - struct nxt_http_field_s { - uint16_t hash; - uint8_t skip:1; - uint8_t hopbyhop:1; - uint8_t name_length; - uint32_t value_length; - u_char *name; - u_char *value; + uint16_t hash; + uint8_t skip :1; + uint8_t hopbyhop:1; + uint8_t name_length; + uint32_t value_length; + u_char *name; + u_char *value; }; - typedef struct { - nxt_mp_t *mem_pool; - uint64_t chunk_size; - uint8_t state; - uint8_t last; /* 1 bit */ - uint8_t chunk_error; /* 1 bit */ - uint8_t error; /* 1 bit */ + nxt_mp_t *mem_pool; + uint64_t chunk_size; + uint8_t state; + uint8_t last; /* 1 bit */ + uint8_t chunk_error; /* 1 bit */ + uint8_t error; /* 1 bit */ } nxt_http_chunk_parse_t; - -#define NXT_HTTP_FIELD_HASH_INIT 159406U -#define nxt_http_field_hash_char(h, c) (((h) << 4) + (h) + (c)) -#define nxt_http_field_hash_end(h) (((h) >> 16) ^ (h)) +#define NXT_HTTP_FIELD_HASH_INIT 159406U +#define nxt_http_field_hash_char(h, c) (((h) << 4) + (h) + (c)) +#define nxt_http_field_hash_end(h) (((h) >> 16) ^ (h)) -nxt_int_t nxt_http_parse_request_init(nxt_http_request_parse_t *rp, - nxt_mp_t *mp); -nxt_int_t nxt_http_parse_request(nxt_http_request_parse_t *rp, - nxt_buf_mem_t *b); -nxt_int_t nxt_http_parse_fields(nxt_http_request_parse_t *rp, - nxt_buf_mem_t *b); +nxt_int_t +nxt_http_parse_request_init(nxt_http_request_parse_t *rp, nxt_mp_t *mp); +nxt_int_t +nxt_http_parse_request(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b); +nxt_int_t +nxt_http_parse_fields(nxt_http_request_parse_t *rp, nxt_buf_mem_t *b); -nxt_int_t nxt_http_fields_hash(nxt_lvlhsh_t *hash, - nxt_http_field_proc_t items[], nxt_uint_t count); -nxt_uint_t nxt_http_fields_hash_collisions(nxt_lvlhsh_t *hash, +nxt_int_t +nxt_http_fields_hash(nxt_lvlhsh_t *hash, nxt_http_field_proc_t items[], + nxt_uint_t count); +nxt_uint_t +nxt_http_fields_hash_collisions(nxt_lvlhsh_t *hash, nxt_http_field_proc_t items[], nxt_uint_t count, nxt_bool_t level); -nxt_int_t nxt_http_fields_process(nxt_list_t *fields, nxt_lvlhsh_t *hash, - void *ctx); +nxt_int_t +nxt_http_fields_process(nxt_list_t *fields, nxt_lvlhsh_t *hash, void *ctx); -nxt_int_t nxt_http_parse_complex_target(nxt_http_request_parse_t *rp); -nxt_buf_t *nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, +nxt_int_t +nxt_http_parse_complex_target(nxt_http_request_parse_t *rp); +nxt_buf_t * +nxt_http_chunk_parse(nxt_task_t *task, nxt_http_chunk_parse_t *hcp, nxt_buf_t *in); -extern const nxt_lvlhsh_proto_t nxt_http_fields_hash_proto; +extern const nxt_lvlhsh_proto_t nxt_http_fields_hash_proto; nxt_inline nxt_int_t -nxt_http_field_process(nxt_http_field_t *field, nxt_lvlhsh_t *hash, void *ctx) -{ +nxt_http_field_process(nxt_http_field_t *field, nxt_lvlhsh_t *hash, void *ctx) { nxt_lvlhsh_query_t lhq; - nxt_http_field_proc_t *proc; + nxt_http_field_proc_t *proc; lhq.proto = &nxt_http_fields_hash_proto; - lhq.key_hash = field->hash; + lhq.key_hash = field->hash; lhq.key.length = field->name_length; - lhq.key.start = field->name; + lhq.key.start = field->name; if (nxt_lvlhsh_find(hash, &lhq) != NXT_OK) { return NXT_OK; diff --git a/src/nxt_http_proxy.c b/src/nxt_http_proxy.c index 7f6ad6866..6cb0e5923 100644 --- a/src/nxt_http_proxy.c +++ b/src/nxt_http_proxy.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,62 +7,64 @@ #include #include - struct nxt_upstream_proxy_s { - nxt_sockaddr_t *sockaddr; + nxt_sockaddr_t *sockaddr; uint8_t protocol; }; - -static void nxt_http_proxy_server_get(nxt_task_t *task, - nxt_upstream_server_t *us); -static void nxt_http_proxy_upstream_ready(nxt_task_t *task, - nxt_upstream_server_t *us); -static void nxt_http_proxy_upstream_error(nxt_task_t *task, - nxt_upstream_server_t *us); -static nxt_http_action_t *nxt_http_proxy(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); -static void nxt_http_proxy_header_send(nxt_task_t *task, void *obj, void *data); -static void nxt_http_proxy_header_sent(nxt_task_t *task, void *obj, void *data); -static void nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data); -static void nxt_http_proxy_send_body(nxt_task_t *task, void *obj, void *data); -static void nxt_http_proxy_buf_mem_completion(nxt_task_t *task, void *obj, - void *data); -static void nxt_http_proxy_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_server_get(nxt_task_t *task, nxt_upstream_server_t *us); +static void +nxt_http_proxy_upstream_ready(nxt_task_t *task, nxt_upstream_server_t *us); +static void +nxt_http_proxy_upstream_error(nxt_task_t *task, nxt_upstream_server_t *us); +static nxt_http_action_t * +nxt_http_proxy(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); +static void +nxt_http_proxy_header_send(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_header_sent(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_send_body(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_buf_mem_completion(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_proxy_error(nxt_task_t *task, void *obj, void *data); -static const nxt_http_request_state_t nxt_http_proxy_header_send_state; -static const nxt_http_request_state_t nxt_http_proxy_header_sent_state; -static const nxt_http_request_state_t nxt_http_proxy_header_read_state; -static const nxt_http_request_state_t nxt_http_proxy_read_state; +static const nxt_http_request_state_t nxt_http_proxy_header_send_state; +static const nxt_http_request_state_t nxt_http_proxy_header_sent_state; +static const nxt_http_request_state_t nxt_http_proxy_header_read_state; +static const nxt_http_request_state_t nxt_http_proxy_read_state; -static const nxt_upstream_server_proto_t nxt_upstream_simple_proto = { +static const nxt_upstream_server_proto_t nxt_upstream_simple_proto = { .get = nxt_http_proxy_server_get, }; -static const nxt_upstream_peer_state_t nxt_upstream_proxy_state = { +static const nxt_upstream_peer_state_t nxt_upstream_proxy_state = { .ready = nxt_http_proxy_upstream_ready, .error = nxt_http_proxy_upstream_error, }; - nxt_int_t nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action, - nxt_http_action_conf_t *acf) -{ + nxt_http_action_conf_t *acf) { nxt_str_t name; - nxt_sockaddr_t *sa; - nxt_upstream_t *up; - nxt_upstream_proxy_t *proxy; + nxt_sockaddr_t *sa; + nxt_upstream_t *up; + nxt_upstream_proxy_t *proxy; sa = NULL; nxt_conf_get_string(acf->proxy, &name); if (nxt_str_start(&name, "http://", 7)) { name.length -= 7; - name.start += 7; + name.start += 7; sa = nxt_sockaddr_parse(mp, &name); if (nxt_slow_path(sa == NULL)) { @@ -80,8 +81,8 @@ nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action, } up->name.length = sa->length; - up->name.start = nxt_sockaddr_start(sa); - up->proto = &nxt_upstream_simple_proto; + up->name.start = nxt_sockaddr_start(sa); + up->proto = &nxt_upstream_simple_proto; proxy = nxt_mp_alloc(mp, sizeof(nxt_upstream_proxy_t)); if (nxt_slow_path(proxy == NULL)) { @@ -90,21 +91,19 @@ nxt_http_proxy_init(nxt_mp_t *mp, nxt_http_action_t *action, proxy->sockaddr = sa; proxy->protocol = NXT_HTTP_PROTO_H1; - up->type.proxy = proxy; + up->type.proxy = proxy; action->u.upstream = up; - action->handler = nxt_http_proxy; + action->handler = nxt_http_proxy; } return NXT_OK; } - static nxt_http_action_t * nxt_http_proxy(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ - nxt_upstream_t *u; + nxt_http_action_t *action) { + nxt_upstream_t *u; u = action->u.upstream; @@ -113,13 +112,11 @@ nxt_http_proxy(nxt_task_t *task, nxt_http_request_t *r, return nxt_upstream_proxy_handler(task, r, u); } - nxt_http_action_t * nxt_upstream_proxy_handler(nxt_task_t *task, nxt_http_request_t *r, - nxt_upstream_t *upstream) -{ - nxt_http_peer_t *peer; - nxt_upstream_server_t *us; + nxt_upstream_t *upstream) { + nxt_http_peer_t *peer; + nxt_upstream_server_t *us; us = nxt_mp_zalloc(r->mem_pool, sizeof(nxt_upstream_server_t)); if (nxt_slow_path(us == NULL)) { @@ -134,13 +131,13 @@ nxt_upstream_proxy_handler(nxt_task_t *task, nxt_http_request_t *r, } peer->request = r; - r->peer = peer; + r->peer = peer; nxt_mp_retain(r->mem_pool); - us->state = &nxt_upstream_proxy_state; + us->state = &nxt_upstream_proxy_state; us->peer.http = peer; - peer->server = us; + peer->server = us; us->upstream = upstream; upstream->proto->get(task, us); @@ -148,11 +145,9 @@ nxt_upstream_proxy_handler(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static void -nxt_http_proxy_server_get(nxt_task_t *task, nxt_upstream_server_t *us) -{ - nxt_upstream_proxy_t *proxy; +nxt_http_proxy_server_get(nxt_task_t *task, nxt_upstream_server_t *us) { + nxt_upstream_proxy_t *proxy; proxy = us->upstream->type.proxy; @@ -162,11 +157,9 @@ nxt_http_proxy_server_get(nxt_task_t *task, nxt_upstream_server_t *us) us->state->ready(task, us); } - static void -nxt_http_proxy_upstream_ready(nxt_task_t *task, nxt_upstream_server_t *us) -{ - nxt_http_peer_t *peer; +nxt_http_proxy_upstream_ready(nxt_task_t *task, nxt_upstream_server_t *us) { + nxt_http_peer_t *peer; peer = us->peer.http; @@ -177,11 +170,9 @@ nxt_http_proxy_upstream_ready(nxt_task_t *task, nxt_upstream_server_t *us) nxt_http_proto[peer->protocol].peer_connect(task, peer); } - static void -nxt_http_proxy_upstream_error(nxt_task_t *task, nxt_upstream_server_t *us) -{ - nxt_http_request_t *r; +nxt_http_proxy_upstream_error(nxt_task_t *task, nxt_upstream_server_t *us) { + nxt_http_request_t *r; r = us->peer.http->request; @@ -190,67 +181,58 @@ nxt_http_proxy_upstream_error(nxt_task_t *task, nxt_upstream_server_t *us) nxt_http_request_error(task, r, NXT_HTTP_BAD_GATEWAY); } - -static const nxt_http_request_state_t nxt_http_proxy_header_send_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_proxy_header_send, - .error_handler = nxt_http_proxy_error, +static const nxt_http_request_state_t + nxt_http_proxy_header_send_state nxt_aligned(64) + = { + .ready_handler = nxt_http_proxy_header_send, + .error_handler = nxt_http_proxy_error, }; - static void -nxt_http_proxy_header_send(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_http_proxy_header_send(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; - r = obj; - peer = data; + r = obj; + peer = data; r->state = &nxt_http_proxy_header_sent_state; nxt_http_proto[peer->protocol].peer_header_send(task, peer); } - -static const nxt_http_request_state_t nxt_http_proxy_header_sent_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_proxy_header_sent, - .error_handler = nxt_http_proxy_error, +static const nxt_http_request_state_t + nxt_http_proxy_header_sent_state nxt_aligned(64) + = { + .ready_handler = nxt_http_proxy_header_sent, + .error_handler = nxt_http_proxy_error, }; - static void -nxt_http_proxy_header_sent(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_http_proxy_header_sent(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; - r = obj; - peer = data; + r = obj; + peer = data; r->state = &nxt_http_proxy_header_read_state; nxt_http_proto[peer->protocol].peer_header_read(task, peer); } - -static const nxt_http_request_state_t nxt_http_proxy_header_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_proxy_header_read, - .error_handler = nxt_http_proxy_error, +static const nxt_http_request_state_t + nxt_http_proxy_header_read_state nxt_aligned(64) + = { + .ready_handler = nxt_http_proxy_header_read, + .error_handler = nxt_http_proxy_error, }; - static void -nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_field_t *f, *field; - nxt_http_request_t *r; +nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_field_t *f, *field; + nxt_http_request_t *r; - r = obj; + r = obj; peer = data; r->status = peer->status; @@ -258,10 +240,9 @@ nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data) nxt_debug(task, "http proxy status: %d", peer->status); nxt_list_each(field, peer->fields) { - nxt_debug(task, "http proxy header: \"%*s: %*s\"", - (size_t) field->name_length, field->name, - (size_t) field->value_length, field->value); + (size_t) field->name_length, field->name, + (size_t) field->value_length, field->value); if (!field->skip) { f = nxt_list_add(r->resp.fields); @@ -272,33 +253,29 @@ nxt_http_proxy_header_read(nxt_task_t *task, void *obj, void *data) *f = *field; } - - } nxt_list_loop; + } + nxt_list_loop; r->state = &nxt_http_proxy_read_state; nxt_http_request_header_send(task, r, nxt_http_proxy_send_body, peer); } - -static const nxt_http_request_state_t nxt_http_proxy_read_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_proxy_send_body, - .error_handler = nxt_http_proxy_error, +static const nxt_http_request_state_t nxt_http_proxy_read_state nxt_aligned(64) + = { + .ready_handler = nxt_http_proxy_send_body, + .error_handler = nxt_http_proxy_error, }; - static void -nxt_http_proxy_send_body(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *out; - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_http_proxy_send_body(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *out; + nxt_http_peer_t *peer; + nxt_http_request_t *r; - r = obj; + r = obj; peer = data; - out = peer->body; + out = peer->body; if (out != NULL) { peer->body = NULL; @@ -315,17 +292,15 @@ nxt_http_proxy_send_body(nxt_task_t *task, void *obj, void *data) } } - nxt_buf_t * nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r, - size_t size) -{ - nxt_buf_t *b; + size_t size) { + nxt_buf_t *b; b = nxt_event_engine_buf_mem_alloc(task->thread->engine, size); if (nxt_fast_path(b != NULL)) { b->completion_handler = nxt_http_proxy_buf_mem_completion; - b->parent = r; + b->parent = r; nxt_mp_retain(r->mem_pool); } else { @@ -335,13 +310,11 @@ nxt_http_proxy_buf_mem_alloc(nxt_task_t *task, nxt_http_request_t *r, return b; } - static void -nxt_http_proxy_buf_mem_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b, *next; - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_http_proxy_buf_mem_completion(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b, *next; + nxt_http_peer_t *peer; + nxt_http_request_t *r; b = obj; r = data; @@ -361,24 +334,20 @@ nxt_http_proxy_buf_mem_completion(nxt_task_t *task, void *obj, void *data) } } - void nxt_http_proxy_buf_mem_free(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *b) -{ + nxt_buf_t *b) { nxt_event_engine_buf_mem_free(task->thread->engine, b); nxt_mp_release(r->mem_pool); } - static void -nxt_http_proxy_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_peer_t *peer; - nxt_http_request_t *r; +nxt_http_proxy_error(nxt_task_t *task, void *obj, void *data) { + nxt_http_peer_t *peer; + nxt_http_request_t *r; - r = obj; + r = obj; peer = r->peer; if (!peer->closed) { @@ -389,11 +358,9 @@ nxt_http_proxy_error(nxt_task_t *task, void *obj, void *data) nxt_http_request_error(&r->task, r, peer->status); } - nxt_int_t -nxt_http_proxy_date(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ - nxt_http_request_t *r; +nxt_http_proxy_date(void *ctx, nxt_http_field_t *field, uintptr_t data) { + nxt_http_request_t *r; r = ctx; @@ -402,13 +369,11 @@ nxt_http_proxy_date(void *ctx, nxt_http_field_t *field, uintptr_t data) return NXT_OK; } - nxt_int_t nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field, - uintptr_t data) -{ + uintptr_t data) { nxt_off_t n; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; @@ -423,10 +388,8 @@ nxt_http_proxy_content_length(void *ctx, nxt_http_field_t *field, return NXT_OK; } - nxt_int_t -nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ +nxt_http_proxy_skip(void *ctx, nxt_http_field_t *field, uintptr_t data) { field->skip = 1; return NXT_OK; diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c index a7e9ff69a..410a994ee 100644 --- a/src/nxt_http_request.c +++ b/src/nxt_http_request.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,51 +7,66 @@ #include -static nxt_int_t nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp); -static void nxt_http_request_start(nxt_task_t *task, void *obj, void *data); -static nxt_int_t nxt_http_request_forward(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_forward_t *forward); -static void nxt_http_request_forward_client_ip(nxt_http_request_t *r, +static nxt_int_t +nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp); +static void +nxt_http_request_start(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_forward_t *forward); +static void +nxt_http_request_forward_client_ip(nxt_http_request_t *r, nxt_http_forward_t *forward, nxt_array_t *fields); -static nxt_sockaddr_t *nxt_http_request_client_ip_sockaddr( - nxt_http_request_t *r, u_char *start, size_t len); -static void nxt_http_request_forward_protocol(nxt_http_request_t *r, - nxt_http_field_t *field); -static void nxt_http_request_ready(nxt_task_t *task, void *obj, void *data); -static void nxt_http_request_proto_info(nxt_task_t *task, - nxt_http_request_t *r); -static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, - void *data); -static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data); - -static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, const char *format); - -static nxt_http_name_value_t *nxt_http_argument(nxt_array_t *array, - u_char *name, size_t name_length, uint32_t hash, u_char *start, - const u_char *end); -static nxt_int_t nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, - const u_char *end); -static nxt_http_name_value_t *nxt_http_cookie(nxt_array_t *array, u_char *name, - size_t name_length, u_char *start, const u_char *end); - - -#define NXT_HTTP_COOKIE_HASH \ - (nxt_http_field_hash_end( \ - nxt_http_field_hash_char( \ - nxt_http_field_hash_char( \ - nxt_http_field_hash_char( \ - nxt_http_field_hash_char( \ - nxt_http_field_hash_char( \ - nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT, \ - 'c'), 'o'), 'o'), 'k'), 'i'), 'e')) & 0xFFFF) - - -static const nxt_http_request_state_t nxt_http_request_init_state; -static const nxt_http_request_state_t nxt_http_request_body_state; - - -nxt_time_string_t nxt_http_date_cache = { +static nxt_sockaddr_t * +nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start, + size_t len); +static void +nxt_http_request_forward_protocol(nxt_http_request_t *r, + nxt_http_field_t *field); +static void +nxt_http_request_ready(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r); +static void +nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_request_done(nxt_task_t *task, void *obj, void *data); + +static u_char * +nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm, + size_t size, const char *format); + +static nxt_http_name_value_t * +nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length, + uint32_t hash, u_char *start, const u_char *end); +static nxt_int_t +nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end); +static nxt_http_name_value_t * +nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length, + u_char *start, const u_char *end); + + +#define NXT_HTTP_COOKIE_HASH \ + (nxt_http_field_hash_end(nxt_http_field_hash_char( \ + nxt_http_field_hash_char( \ + nxt_http_field_hash_char( \ + nxt_http_field_hash_char( \ + nxt_http_field_hash_char( \ + nxt_http_field_hash_char(NXT_HTTP_FIELD_HASH_INIT, \ + 'c'), \ + 'o'), \ + 'o'), \ + 'k'), \ + 'i'), \ + 'e')) \ + & 0xFFFF) + + +static const nxt_http_request_state_t nxt_http_request_init_state; +static const nxt_http_request_state_t nxt_http_request_body_state; + + +nxt_time_string_t nxt_http_date_cache = { (nxt_atomic_uint_t) -1, nxt_http_date_cache_handler, NULL, @@ -61,11 +75,9 @@ nxt_time_string_t nxt_http_date_cache = { NXT_THREAD_TIME_SEC, }; - nxt_int_t -nxt_http_init(nxt_task_t *task) -{ - nxt_int_t ret; +nxt_http_init(nxt_task_t *task) { + nxt_int_t ret; ret = nxt_h1p_init(task); @@ -76,13 +88,11 @@ nxt_http_init(nxt_task_t *task) return nxt_http_response_hash_init(task); } - nxt_int_t -nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ +nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data) { nxt_int_t ret; nxt_str_t host; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; @@ -91,7 +101,7 @@ nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data) } host.length = field->value_length; - host.start = field->value; + host.start = field->value; ret = nxt_http_validate_host(&host, r->mem_pool); @@ -102,13 +112,11 @@ nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data) return ret; } - static nxt_int_t -nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) -{ - u_char *h, ch; - size_t i, dot_pos, host_length; - nxt_bool_t lowcase; +nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) { + u_char *h, ch; + size_t i, dot_pos, host_length; + nxt_bool_t lowcase; enum { sw_usual, @@ -116,13 +124,13 @@ nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) sw_rest } state; - dot_pos = host->length; + dot_pos = host->length; host_length = host->length; h = host->start; lowcase = 0; - state = sw_usual; + state = sw_usual; for (i = 0; i < host->length; i++) { ch = h[i]; @@ -133,7 +141,6 @@ nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) } switch (ch) { - case '.': if (dot_pos == i - 1) { return NXT_HTTP_BAD_REQUEST; @@ -145,7 +152,7 @@ nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) case ':': if (state == sw_usual) { host_length = i; - state = sw_rest; + state = sw_rest; } break; @@ -160,7 +167,7 @@ nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) case ']': if (state == sw_literal) { host_length = i + 1; - state = sw_rest; + state = sw_rest; } break; @@ -195,11 +202,9 @@ nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) return NXT_OK; } - nxt_int_t -nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) -{ - nxt_http_request_t *r; +nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) { + nxt_http_request_t *r; r = ctx; @@ -208,13 +213,11 @@ nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) return NXT_OK; } - nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field, - uintptr_t data) -{ + uintptr_t data) { nxt_off_t n, max_body_size; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; @@ -239,13 +242,11 @@ nxt_http_request_content_length(void *ctx, nxt_http_field_t *field, return NXT_HTTP_BAD_REQUEST; } - nxt_http_request_t * -nxt_http_request_create(nxt_task_t *task) -{ - nxt_mp_t *mp; - nxt_buf_t *last; - nxt_http_request_t *r; +nxt_http_request_create(nxt_task_t *task) { + nxt_mp_t *mp; + nxt_buf_t *last; + nxt_http_request_t *r; mp = nxt_mp_create(4096, 128, 512, 32); if (nxt_slow_path(mp == NULL)) { @@ -270,13 +271,13 @@ nxt_http_request_create(nxt_task_t *task) nxt_buf_set_sync(last); nxt_buf_set_last(last); last->completion_handler = nxt_http_request_done; - last->parent = r; - r->last = last; + last->parent = r; + r->last = last; - r->mem_pool = mp; - r->content_length_n = -1; + r->mem_pool = mp; + r->content_length_n = -1; r->resp.content_length_n = -1; - r->state = &nxt_http_request_init_state; + r->state = &nxt_http_request_init_state; r->start_time = nxt_thread_monotonic_time(task->thread); @@ -293,21 +294,18 @@ nxt_http_request_create(nxt_task_t *task) return NULL; } - -static const nxt_http_request_state_t nxt_http_request_init_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_request_start, - .error_handler = nxt_http_request_close_handler, +static const nxt_http_request_state_t nxt_http_request_init_state nxt_aligned( + 64) + = { + .ready_handler = nxt_http_request_start, + .error_handler = nxt_http_request_close_handler, }; - static void -nxt_http_request_start(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_request_start(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_socket_conf_t *skcf; - nxt_http_request_t *r; + nxt_socket_conf_t *skcf; + nxt_http_request_t *r; r = obj; @@ -337,15 +335,13 @@ nxt_http_request_start(nxt_task_t *task, void *obj, void *data) nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - static nxt_int_t nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_forward_t *forward) -{ + nxt_http_forward_t *forward) { nxt_int_t ret; - nxt_array_t *client_ip_fields; - nxt_http_field_t *f, **fields, *protocol_field; - nxt_http_forward_header_t *client_ip, *protocol; + nxt_array_t *client_ip_fields; + nxt_http_field_t *f, **fields, *protocol_field; + nxt_http_forward_header_t *client_ip, *protocol; ret = nxt_http_route_addr_rule(r, forward->source, r->remote); if (ret <= 0) { @@ -353,11 +349,11 @@ nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, } client_ip = &forward->client_ip; - protocol = &forward->protocol; + protocol = &forward->protocol; if (client_ip->header != NULL) { - client_ip_fields = nxt_array_create(r->mem_pool, 1, - sizeof(nxt_http_field_t *)); + client_ip_fields + = nxt_array_create(r->mem_pool, 1, sizeof(nxt_http_field_t *)); if (nxt_slow_path(client_ip_fields == NULL)) { return NXT_ERROR; } @@ -369,13 +365,12 @@ nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, protocol_field = NULL; nxt_list_each(f, r->fields) { - if (client_ip_fields != NULL - && f->hash == client_ip->header_hash + if (client_ip_fields != NULL && f->hash == client_ip->header_hash && f->value_length > 0 && f->name_length == client_ip->header->length && nxt_memcasecmp(f->name, client_ip->header->start, - client_ip->header->length) == 0) - { + client_ip->header->length) + == 0) { fields = nxt_array_add(client_ip_fields); if (nxt_slow_path(fields == NULL)) { return NXT_ERROR; @@ -384,17 +379,16 @@ nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, *fields = f; } - if (protocol->header != NULL - && protocol_field == NULL - && f->hash == protocol->header_hash - && f->value_length > 0 + if (protocol->header != NULL && protocol_field == NULL + && f->hash == protocol->header_hash && f->value_length > 0 && f->name_length == protocol->header->length && nxt_memcasecmp(f->name, protocol->header->start, - protocol->header->length) == 0) - { + protocol->header->length) + == 0) { protocol_field = f; } - } nxt_list_loop; + } + nxt_list_loop; if (client_ip_fields != NULL) { nxt_http_request_forward_client_ip(r, forward, client_ip_fields); @@ -407,24 +401,22 @@ nxt_http_request_forward(nxt_task_t *task, nxt_http_request_t *r, return NXT_OK; } - static void nxt_http_request_forward_client_ip(nxt_http_request_t *r, - nxt_http_forward_t *forward, nxt_array_t *fields) -{ + nxt_http_forward_t *forward, nxt_array_t *fields) { u_char *start, *p; - nxt_int_t ret, i, len; + nxt_int_t ret, i, len; nxt_sockaddr_t *sa, *prev_sa; - nxt_http_field_t **f; + nxt_http_field_t **f; prev_sa = r->remote; - f = (nxt_http_field_t **) fields->elts; + f = (nxt_http_field_t **) fields->elts; i = fields->nelts; while (i-- > 0) { start = f[i]->value; - len = f[i]->value_length; + len = f[i]->value_length; do { for (p = start + len - 1; p > start; p--, len--) { @@ -461,21 +453,19 @@ nxt_http_request_forward_client_ip(nxt_http_request_t *r, } prev_sa = sa; - len = p - 1 - start; + len = p - 1 - start; } while (len > 0); } } - static nxt_sockaddr_t * nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start, - size_t len) -{ + size_t len) { nxt_str_t addr; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; - addr.start = start; + addr.start = start; addr.length = len; sa = nxt_sockaddr_parse_optport(r->mem_pool, &addr); @@ -484,34 +474,32 @@ nxt_http_request_client_ip_sockaddr(nxt_http_request_t *r, u_char *start, } switch (sa->u.sockaddr.sa_family) { - case AF_INET: - if (sa->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) { - return NULL; - } + case AF_INET: + if (sa->u.sockaddr_in.sin_addr.s_addr == INADDR_ANY) { + return NULL; + } - break; + break; #if (NXT_INET6) - case AF_INET6: - if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sockaddr_in6.sin6_addr)) { - return NULL; - } + case AF_INET6: + if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sockaddr_in6.sin6_addr)) { + return NULL; + } - break; + break; #endif /* NXT_INET6 */ - default: - return NULL; + default: + return NULL; } return sa; } - static void nxt_http_request_forward_protocol(nxt_http_request_t *r, - nxt_http_field_t *field) -{ + nxt_http_field_t *field) { if (field->value_length == 4) { if (nxt_memcasecmp(field->value, "http", 4) == 0) { r->tls = 0; @@ -529,21 +517,18 @@ nxt_http_request_forward_protocol(nxt_http_request_t *r, } } - -static const nxt_http_request_state_t nxt_http_request_body_state - nxt_aligned(64) = -{ - .ready_handler = nxt_http_request_ready, - .error_handler = nxt_http_request_close_handler, +static const nxt_http_request_state_t nxt_http_request_body_state nxt_aligned( + 64) + = { + .ready_handler = nxt_http_request_ready, + .error_handler = nxt_http_request_close_handler, }; - static nxt_int_t -nxt_http_request_chunked_transform(nxt_http_request_t *r) -{ +nxt_http_request_chunked_transform(nxt_http_request_t *r) { size_t size; - u_char *p, *end; - nxt_http_field_t *f; + u_char *p, *end; + nxt_http_field_t *f; r->chunked_field->skip = 1; @@ -561,25 +546,23 @@ nxt_http_request_chunked_transform(nxt_http_request_t *r) return NXT_ERROR; } - f->value = p; - end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%uz", size); + f->value = p; + end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%uz", size); f->value_length = end - p; - r->content_length = f; + r->content_length = f; r->content_length_n = size; return NXT_OK; } - static void -nxt_http_request_ready(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_request_ready(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_http_action_t *action; - nxt_http_request_t *r; + nxt_http_action_t *action; + nxt_http_request_t *r; - r = obj; + r = obj; action = r->conf->socket_conf->action; if (r->chunked) { @@ -593,15 +576,12 @@ nxt_http_request_ready(nxt_task_t *task, void *obj, void *data) nxt_http_request_action(task, r, action); } - void nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ - nxt_int_t ret; + nxt_http_action_t *action) { + nxt_int_t ret; if (nxt_fast_path(action != NULL)) { - do { ret = nxt_http_rewrite(task, r); if (nxt_slow_path(ret != NXT_OK)) { @@ -624,11 +604,9 @@ nxt_http_request_action(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - nxt_http_action_t * nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { nxt_debug(task, "http application handler"); /* @@ -649,33 +627,27 @@ nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static void -nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r) -{ +nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r) { if (nxt_fast_path(r->proto.any != NULL)) { nxt_http_proto[r->protocol].local_addr(task, r); } } - void -nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) -{ +nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) { if (nxt_fast_path(r->proto.any != NULL)) { nxt_http_proto[r->protocol].body_read(task, r); } } - void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_work_handler_t body_handler, void *data) -{ - u_char *p, *end, *server_string; + nxt_work_handler_t body_handler, void *data) { + u_char *p, *end, *server_string; nxt_int_t ret; - nxt_http_field_t *server, *date, *content_length; - nxt_socket_conf_t *skcf; + nxt_http_field_t *server, *date, *content_length; + nxt_socket_conf_t *skcf; ret = nxt_http_set_headers(r); if (nxt_slow_path(ret != NXT_OK)) { @@ -692,11 +664,11 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, goto fail; } - skcf = r->conf->socket_conf; + skcf = r->conf->socket_conf; server_string = (u_char *) (skcf->server_version ? NXT_SERVER : NXT_NAME); nxt_http_field_name_set(server, "Server"); - server->value = server_string; + server->value = server_string; server->value_length = nxt_strlen(server_string); if (r->resp.date == NULL) { @@ -714,15 +686,14 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p); - date->value = p; + date->value = p; date->value_length = nxt_http_date_cache.size; r->resp.date = date; } if (r->resp.content_length_n != -1 - && (r->resp.content_length == NULL || r->resp.content_length->skip)) - { + && (r->resp.content_length == NULL || r->resp.content_length->skip)) { content_length = nxt_list_zero_add(r->resp.fields); if (nxt_slow_path(content_length == NULL)) { goto fail; @@ -753,35 +724,29 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *ws_frame) -{ + nxt_buf_t *ws_frame) { if (r->proto.any != NULL) { nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame); } } - void -nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) -{ +nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) { if (nxt_fast_path(r->proto.any != NULL)) { nxt_http_proto[r->protocol].send(task, r, out); } } - nxt_buf_t * -nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size) -{ - nxt_buf_t *b; +nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size) { + nxt_buf_t *b; b = nxt_buf_mem_alloc(r->mem_pool, size, 0); if (nxt_fast_path(b != NULL)) { b->completion_handler = nxt_http_request_mem_buf_completion; - b->parent = r; + b->parent = r; nxt_mp_retain(r->mem_pool); } else { @@ -791,12 +756,10 @@ nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size) return b; } - static void -nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *b, *next; - nxt_http_request_t *r; +nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *b, *next; + nxt_http_request_t *r; b = obj; r = data; @@ -811,23 +774,19 @@ nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data) } while (b != NULL); } - nxt_buf_t * -nxt_http_buf_last(nxt_http_request_t *r) -{ - nxt_buf_t *last; +nxt_http_buf_last(nxt_http_request_t *r) { + nxt_buf_t *last; - last = r->last; + last = r->last; r->last = NULL; return last; } - static void -nxt_http_request_done(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; +nxt_http_request_done(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; r = data; @@ -836,14 +795,12 @@ nxt_http_request_done(nxt_task_t *task, void *obj, void *data) nxt_http_request_close_handler(task, r, r->proto.any); } - void -nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data) { nxt_http_proto_t proto; - nxt_http_request_t *r; + nxt_http_request_t *r; - r = obj; + r = obj; proto.any = data; nxt_debug(task, "http request error handler"); @@ -855,18 +812,16 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data) } } - void -nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) { nxt_http_proto_t proto; - nxt_router_conf_t *rtcf; - nxt_http_request_t *r; + nxt_router_conf_t *rtcf; + nxt_http_request_t *r; nxt_http_protocol_t protocol; - nxt_socket_conf_joint_t *conf; - nxt_router_access_log_t *access_log; + nxt_socket_conf_joint_t *conf; + nxt_router_access_log_t *access_log; - r = obj; + r = obj; proto.any = data; conf = r->conf; @@ -890,8 +845,7 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) r->proto.any = NULL; if (r->body != NULL && nxt_buf_is_file(r->body) - && r->body->file->fd != -1) - { + && r->body->file->fd != -1) { nxt_fd_close(r->body->file->fd); r->body->file->fd = -1; @@ -910,24 +864,20 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) } } - static u_char * nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm, - size_t size, const char *format) -{ + size_t size, const char *format) { return nxt_http_date(buf, tm); } - nxt_array_t * -nxt_http_arguments_parse(nxt_http_request_t *r) -{ +nxt_http_arguments_parse(nxt_http_request_t *r) { size_t name_length; - u_char *p, *dst, *dst_start, *start, *end, *name; + u_char *p, *dst, *dst_start, *start, *end, *name; uint8_t d0, d1; uint32_t hash; - nxt_array_t *args; - nxt_http_name_value_t *nv; + nxt_array_t *args; + nxt_http_name_value_t *nv; if (r->arguments != NULL) { return r->arguments; @@ -942,8 +892,8 @@ nxt_http_arguments_parse(nxt_http_request_t *r) goto end; } - hash = NXT_HTTP_FIELD_HASH_INIT; - name = NULL; + hash = NXT_HTTP_FIELD_HASH_INIT; + name = NULL; name_length = 0; dst_start = nxt_mp_nget(r->mem_pool, r->args->length); @@ -954,7 +904,7 @@ nxt_http_arguments_parse(nxt_http_request_t *r) r->args_decoded.start = dst_start; start = r->args->start; - end = start + r->args->length; + end = start + r->args->length; for (p = start, dst = dst_start; p < end; p++, dst++) { *dst = *p; @@ -963,8 +913,8 @@ nxt_http_arguments_parse(nxt_http_request_t *r) case '=': if (name == NULL) { name_length = dst - dst_start; - name = dst_start; - dst_start = dst + 1; + name = dst_start; + dst_start = dst + 1; } continue; @@ -972,16 +922,16 @@ nxt_http_arguments_parse(nxt_http_request_t *r) case '&': if (name_length != 0 || dst != dst_start) { nv = nxt_http_argument(args, name, name_length, hash, dst_start, - dst); + dst); if (nxt_slow_path(nv == NULL)) { return NULL; } } - hash = NXT_HTTP_FIELD_HASH_INIT; + hash = NXT_HTTP_FIELD_HASH_INIT; name_length = 0; - name = NULL; - dst_start = dst + 1; + name = NULL; + dst_start = dst + 1; continue; @@ -1002,8 +952,8 @@ nxt_http_arguments_parse(nxt_http_request_t *r) break; } - p += 2; - *dst = (d0 << 4) + d1; + p += 2; + *dst = (d0 << 4) + d1; break; } @@ -1029,13 +979,11 @@ nxt_http_arguments_parse(nxt_http_request_t *r) return args; } - static nxt_http_name_value_t * nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length, - uint32_t hash, u_char *start, const u_char *end) -{ + uint32_t hash, u_char *start, const u_char *end) { size_t length; - nxt_http_name_value_t *nv; + nxt_http_name_value_t *nv; nv = nxt_array_add(array); if (nxt_slow_path(nv == NULL)) { @@ -1048,25 +996,23 @@ nxt_http_argument(nxt_array_t *array, u_char *name, size_t name_length, if (name == NULL) { name_length = length; - name = start; - length = 0; + name = start; + length = 0; } - nv->name_length = name_length; + nv->name_length = name_length; nv->value_length = length; - nv->name = name; - nv->value = start; + nv->name = name; + nv->value = start; return nv; } - nxt_array_t * -nxt_http_cookies_parse(nxt_http_request_t *r) -{ +nxt_http_cookies_parse(nxt_http_request_t *r) { nxt_int_t ret; - nxt_array_t *cookies; - nxt_http_field_t *f; + nxt_array_t *cookies; + nxt_http_field_t *f; if (r->cookies != NULL) { return r->cookies; @@ -1078,46 +1024,43 @@ nxt_http_cookies_parse(nxt_http_request_t *r) } nxt_list_each(f, r->fields) { - - if (f->hash != NXT_HTTP_COOKIE_HASH - || f->name_length != 6 - || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0) - { + if (f->hash != NXT_HTTP_COOKIE_HASH || f->name_length != 6 + || nxt_strncasecmp(f->name, (u_char *) "Cookie", 6) != 0) { continue; } ret = nxt_http_cookie_parse(cookies, f->value, - f->value + f->value_length); + f->value + f->value_length); if (ret != NXT_OK) { return NULL; } - - } nxt_list_loop; + } + nxt_list_loop; r->cookies = cookies; return cookies; } - static nxt_int_t -nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end) -{ +nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end) { size_t name_length; u_char c, *p, *name; - nxt_http_name_value_t *nv; + nxt_http_name_value_t *nv; - name = NULL; + name = NULL; name_length = 0; for (p = start; p < end; p++) { c = *p; if (c == '=' && name == NULL) { - while (start[0] == ' ') { start++; } + while (start[0] == ' ') { + start++; + } name_length = p - start; - name = start; + name = start; start = p + 1; @@ -1129,9 +1072,9 @@ nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end) } } - name = NULL; + name = NULL; start = p + 1; - } + } } if (name != NULL) { @@ -1144,14 +1087,12 @@ nxt_http_cookie_parse(nxt_array_t *cookies, u_char *start, const u_char *end) return NXT_OK; } - static nxt_http_name_value_t * nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length, - u_char *start, const u_char *end) -{ + u_char *start, const u_char *end) { u_char c, *p; uint32_t hash; - nxt_http_name_value_t *nv; + nxt_http_name_value_t *nv; nv = nxt_array_add(array); if (nxt_slow_path(nv == NULL)) { @@ -1159,35 +1100,35 @@ nxt_http_cookie(nxt_array_t *array, u_char *name, size_t name_length, } nv->name_length = name_length; - nv->name = name; + nv->name = name; hash = NXT_HTTP_FIELD_HASH_INIT; for (p = name; p < name + name_length; p++) { - c = *p; + c = *p; hash = nxt_http_field_hash_char(hash, c); } nv->hash = nxt_http_field_hash_end(hash) & 0xFFFF; - while (start < end && end[-1] == ' ') { end--; } + while (start < end && end[-1] == ' ') { + end--; + } nv->value_length = end - start; - nv->value = start; + nv->value = start; return nv; } - int64_t nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, - uint8_t encoding) -{ - u_char c, *p, *src, *start, *end, plus; - uint8_t d0, d1; - uint32_t hash; - nxt_str_t str; - nxt_uint_t i; + uint8_t encoding) { + u_char c, *p, *src, *start, *end, plus; + uint8_t d0, d1; + uint32_t hash; + nxt_str_t str; + nxt_uint_t i; str.length = name->length; @@ -1202,10 +1143,10 @@ nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, if (encoding == NXT_HTTP_URI_ENCODING_NONE) { for (i = 0; i < name->length; i++) { - c = name->start[i]; + c = name->start[i]; *p++ = c; - c = case_sensitive ? c : nxt_lowcase(c); + c = case_sensitive ? c : nxt_lowcase(c); hash = nxt_http_field_hash_char(hash, c); } @@ -1215,7 +1156,7 @@ nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, plus = (encoding == NXT_HTTP_URI_ENCODING_PLUS) ? ' ' : '+'; start = name->start; - end = start + name->length; + end = start + name->length; for (src = start; src < end; src++) { c = *src; @@ -1226,20 +1167,20 @@ nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, return -1; } - d0 = nxt_hex2int[src[1]]; - d1 = nxt_hex2int[src[2]]; + d0 = nxt_hex2int[src[1]]; + d1 = nxt_hex2int[src[2]]; src += 2; if (nxt_slow_path((d0 | d1) >= 16)) { return -1; } - c = (d0 << 4) + d1; + c = (d0 << 4) + d1; *p++ = c; break; case '+': - c = plus; + c = plus; *p++ = c; break; @@ -1248,7 +1189,7 @@ nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, break; } - c = case_sensitive ? c : nxt_lowcase(c); + c = case_sensitive ? c : nxt_lowcase(c); hash = nxt_http_field_hash_char(hash, c); } @@ -1261,20 +1202,16 @@ nxt_http_field_hash(nxt_mp_t *mp, nxt_str_t *name, nxt_bool_t case_sensitive, return nxt_http_field_hash_end(hash) & 0xFFFF; } - int64_t -nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name) -{ +nxt_http_argument_hash(nxt_mp_t *mp, nxt_str_t *name) { return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_PLUS); } - int64_t -nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name) -{ - u_char c, *p; - uint32_t i, hash; - nxt_str_t str; +nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name) { + u_char c, *p; + uint32_t i, hash; + nxt_str_t str; str.length = name->length; @@ -1283,7 +1220,7 @@ nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name) return -1; } - p = str.start; + p = str.start; hash = NXT_HTTP_FIELD_HASH_INIT; for (i = 0; i < name->length; i++) { @@ -1308,35 +1245,30 @@ nxt_http_header_hash(nxt_mp_t *mp, nxt_str_t *name) return nxt_http_field_hash_end(hash) & 0xFFFF; } - int64_t -nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name) -{ +nxt_http_cookie_hash(nxt_mp_t *mp, nxt_str_t *name) { return nxt_http_field_hash(mp, name, 1, NXT_HTTP_URI_ENCODING_NONE); } - int nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r, - nxt_tstr_cond_t *cond) -{ + nxt_tstr_cond_t *cond) { nxt_int_t ret; nxt_str_t str; nxt_bool_t expr; - nxt_router_conf_t *rtcf; + nxt_router_conf_t *rtcf; rtcf = r->conf->socket_conf->router_conf; expr = 1; if (cond->expr != NULL) { - if (nxt_tstr_is_const(cond->expr)) { nxt_tstr_str(cond->expr, &str); } else { ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { return -1; } @@ -1347,12 +1279,9 @@ nxt_http_cond_value(nxt_task_t *task, nxt_http_request_t *r, } } - if (str.length == 0 - || nxt_str_eq(&str, "0", 1) - || nxt_str_eq(&str, "false", 5) - || nxt_str_eq(&str, "null", 4) - || nxt_str_eq(&str, "undefined", 9)) - { + if (str.length == 0 || nxt_str_eq(&str, "0", 1) + || nxt_str_eq(&str, "false", 5) || nxt_str_eq(&str, "null", 4) + || nxt_str_eq(&str, "undefined", 9)) { expr = 0; } } diff --git a/src/nxt_http_response.c b/src/nxt_http_response.c index 55a4686c2..4cbecef85 100644 --- a/src/nxt_http_response.c +++ b/src/nxt_http_response.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,45 +7,40 @@ #include -static nxt_int_t nxt_http_response_status(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_http_response_skip(void *ctx, nxt_http_field_t *field, - uintptr_t data); -static nxt_int_t nxt_http_response_field(void *ctx, nxt_http_field_t *field, - uintptr_t offset); - - -nxt_lvlhsh_t nxt_response_fields_hash; - -static nxt_http_field_proc_t nxt_response_fields[] = { - { nxt_string("Status"), &nxt_http_response_status, 0 }, - { nxt_string("Server"), &nxt_http_response_skip, 0 }, - { nxt_string("Date"), &nxt_http_response_field, - offsetof(nxt_http_request_t, resp.date) }, - { nxt_string("Connection"), &nxt_http_response_skip, 0 }, - { nxt_string("Content-Type"), &nxt_http_response_field, - offsetof(nxt_http_request_t, resp.content_type) }, - { nxt_string("Content-Length"), &nxt_http_response_field, - offsetof(nxt_http_request_t, resp.content_length) }, - { nxt_string("Upgrade"), &nxt_http_response_skip, 0 }, - { nxt_string("Sec-WebSocket-Accept"), &nxt_http_response_skip, 0 }, +static nxt_int_t +nxt_http_response_status(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_http_response_skip(void *ctx, nxt_http_field_t *field, uintptr_t data); +static nxt_int_t +nxt_http_response_field(void *ctx, nxt_http_field_t *field, uintptr_t offset); + + +nxt_lvlhsh_t nxt_response_fields_hash; + +static nxt_http_field_proc_t nxt_response_fields[] = { + {nxt_string("Status"), &nxt_http_response_status, 0}, + {nxt_string("Server"), &nxt_http_response_skip, 0}, + {nxt_string("Date"), &nxt_http_response_field, + offsetof(nxt_http_request_t, resp.date)}, + {nxt_string("Connection"), &nxt_http_response_skip, 0}, + {nxt_string("Content-Type"), &nxt_http_response_field, + offsetof(nxt_http_request_t, resp.content_type)}, + {nxt_string("Content-Length"), &nxt_http_response_field, + offsetof(nxt_http_request_t, resp.content_length)}, + {nxt_string("Upgrade"), &nxt_http_response_skip, 0}, + {nxt_string("Sec-WebSocket-Accept"), &nxt_http_response_skip, 0}, }; - nxt_int_t -nxt_http_response_hash_init(nxt_task_t *task) -{ - return nxt_http_fields_hash(&nxt_response_fields_hash, - nxt_response_fields, nxt_nitems(nxt_response_fields)); +nxt_http_response_hash_init(nxt_task_t *task) { + return nxt_http_fields_hash(&nxt_response_fields_hash, nxt_response_fields, + nxt_nitems(nxt_response_fields)); } - nxt_int_t -nxt_http_response_status(void *ctx, nxt_http_field_t *field, - uintptr_t data) -{ +nxt_http_response_status(void *ctx, nxt_http_field_t *field, uintptr_t data) { nxt_int_t status; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; @@ -64,20 +58,16 @@ nxt_http_response_status(void *ctx, nxt_http_field_t *field, return NXT_ERROR; } - nxt_int_t -nxt_http_response_skip(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ +nxt_http_response_skip(void *ctx, nxt_http_field_t *field, uintptr_t data) { field->skip = 1; return NXT_OK; } - nxt_int_t -nxt_http_response_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) -{ - nxt_http_request_t *r; +nxt_http_response_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) { + nxt_http_request_t *r; r = ctx; diff --git a/src/nxt_http_return.c b/src/nxt_http_return.c index a3551683e..15751d7d1 100644 --- a/src/nxt_http_return.c +++ b/src/nxt_http_return.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,38 +5,36 @@ #include #include - typedef struct { - nxt_http_status_t status; - nxt_tstr_t *location; - nxt_str_t encoded; + nxt_http_status_t status; + nxt_tstr_t *location; + nxt_str_t encoded; } nxt_http_return_conf_t; - typedef struct { - nxt_str_t location; - nxt_str_t encoded; + nxt_str_t location; + nxt_str_t encoded; } nxt_http_return_ctx_t; - -static nxt_http_action_t *nxt_http_return(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); -static nxt_int_t nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded, +static nxt_http_action_t * +nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); +static nxt_int_t +nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded, const nxt_str_t *location); -static void nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r, +static void +nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_return_ctx_t *ctx); -static const nxt_http_request_state_t nxt_http_return_send_state; - +static const nxt_http_request_state_t nxt_http_return_send_state; nxt_int_t nxt_http_return_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, - nxt_http_action_conf_t *acf) -{ - nxt_mp_t *mp; + nxt_http_action_conf_t *acf) { + nxt_mp_t *mp; nxt_str_t str; - nxt_http_return_conf_t *conf; + nxt_http_return_conf_t *conf; mp = rtcf->mem_pool; @@ -47,7 +44,7 @@ nxt_http_return_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, } action->handler = nxt_http_return; - action->u.conf = conf; + action->u.conf = conf; conf->status = nxt_conf_get_number(acf->ret); @@ -70,20 +67,18 @@ nxt_http_return_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, return NXT_OK; } - nxt_http_action_t * nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { nxt_int_t ret; - nxt_router_conf_t *rtcf; - nxt_http_return_ctx_t *ctx; - nxt_http_return_conf_t *conf; + nxt_router_conf_t *rtcf; + nxt_http_return_ctx_t *ctx; + nxt_http_return_conf_t *conf; conf = action->u.conf; #if (NXT_DEBUG) - nxt_str_t loc; + nxt_str_t loc; if (conf->location == NULL) { nxt_str_set(&loc, ""); @@ -96,8 +91,7 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, #endif if (conf->status >= NXT_HTTP_BAD_REQUEST - && conf->status <= NXT_HTTP_SERVER_ERROR_MAX) - { + && conf->status <= NXT_HTTP_SERVER_ERROR_MAX) { nxt_http_request_error(task, r, conf->status); return NULL; } @@ -112,7 +106,7 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, } } - r->status = conf->status; + r->status = conf->status; r->resp.content_length_n = 0; if (ctx == NULL || nxt_tstr_is_const(conf->location)) { @@ -124,13 +118,13 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, rtcf = r->conf->socket_conf->router_conf; ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } ret = nxt_tstr_query(task, r->tstr_query, conf->location, - &ctx->location); + &ctx->location); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } @@ -146,12 +140,10 @@ nxt_http_return(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static nxt_int_t nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded, - const nxt_str_t *location) -{ - nxt_uint_t encode; + const nxt_str_t *location) { + nxt_uint_t encode; if (nxt_is_complex_uri_encoded(location->start, location->length)) { *encoded = *location; @@ -172,18 +164,16 @@ nxt_http_return_encode(nxt_mp_t *mp, nxt_str_t *encoded, return NXT_OK; } - static void nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_return_ctx_t *ctx) -{ + nxt_http_return_ctx_t *ctx) { nxt_int_t ret; - nxt_http_field_t *field; + nxt_http_field_t *field; if (ctx != NULL) { if (ctx->location.length > 0) { ret = nxt_http_return_encode(r->mem_pool, &ctx->encoded, - &ctx->location); + &ctx->location); if (nxt_slow_path(ret == NXT_ERROR)) { goto fail; } @@ -196,7 +186,7 @@ nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_field_name_set(field, "Location"); - field->value = ctx->encoded.start; + field->value = ctx->encoded.start; field->value_length = ctx->encoded.length; } @@ -211,9 +201,7 @@ nxt_http_return_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - -static const nxt_http_request_state_t nxt_http_return_send_state - nxt_aligned(64) = -{ - .error_handler = nxt_http_request_error_handler, +static const nxt_http_request_state_t nxt_http_return_send_state nxt_aligned(64) + = { + .error_handler = nxt_http_request_error_handler, }; diff --git a/src/nxt_http_rewrite.c b/src/nxt_http_rewrite.c index 5de15ed7c..6d44b35ba 100644 --- a/src/nxt_http_rewrite.c +++ b/src/nxt_http_rewrite.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Zhidao HONG * Copyright (C) NGINX, Inc. @@ -7,12 +6,10 @@ #include #include - nxt_int_t nxt_http_rewrite_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, - nxt_http_action_conf_t *acf) -{ - nxt_str_t str; + nxt_http_action_conf_t *acf) { + nxt_str_t str; nxt_conf_get_string(acf->rewrite, &str); @@ -24,15 +21,13 @@ nxt_http_rewrite_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, return NXT_OK; } - nxt_int_t -nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) -{ - nxt_int_t ret; - nxt_str_t str; - nxt_router_conf_t *rtcf; - nxt_http_action_t *action; - nxt_http_request_parse_t rp; +nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) { + nxt_int_t ret; + nxt_str_t str; + nxt_router_conf_t *rtcf; + nxt_http_action_t *action; + nxt_http_request_parse_t rp; action = r->action; @@ -47,7 +42,7 @@ nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) rtcf = r->conf->socket_conf->router_conf; ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -63,7 +58,7 @@ nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) rp.mem_pool = r->mem_pool; rp.target_start = str.start; - rp.target_end = str.start + str.length; + rp.target_end = str.start + str.length; ret = nxt_http_parse_complex_target(&rp); if (nxt_slow_path(ret != NXT_OK)) { @@ -77,7 +72,7 @@ nxt_http_rewrite(nxt_task_t *task, nxt_http_request_t *r) *r->path = rp.path; - r->uri_changed = 1; + r->uri_changed = 1; r->quoted_target = rp.quoted_target; if (nxt_slow_path(r->log_route)) { diff --git a/src/nxt_http_route.c b/src/nxt_http_route.c index bd0646f3b..6f170197e 100644 --- a/src/nxt_http_route.c +++ b/src/nxt_http_route.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -10,7 +9,6 @@ #include #include - typedef enum { NXT_HTTP_ROUTE_TABLE = 0, NXT_HTTP_ROUTE_STRING, @@ -24,7 +22,6 @@ typedef enum { NXT_HTTP_ROUTE_DESTINATION, } nxt_http_route_object_t; - typedef enum { NXT_HTTP_ROUTE_PATTERN_EXACT = 0, NXT_HTTP_ROUTE_PATTERN_BEGIN, @@ -32,222 +29,227 @@ typedef enum { NXT_HTTP_ROUTE_PATTERN_SUBSTRING, } nxt_http_route_pattern_type_t; - typedef enum { NXT_HTTP_ROUTE_PATTERN_NOCASE = 0, NXT_HTTP_ROUTE_PATTERN_LOWCASE, NXT_HTTP_ROUTE_PATTERN_UPCASE, } nxt_http_route_pattern_case_t; - typedef struct { - nxt_conf_value_t *host; - nxt_conf_value_t *uri; - nxt_conf_value_t *method; - nxt_conf_value_t *headers; - nxt_conf_value_t *arguments; - nxt_conf_value_t *cookies; - nxt_conf_value_t *scheme; - nxt_conf_value_t *query; - nxt_conf_value_t *source; - nxt_conf_value_t *destination; - nxt_conf_value_t *condition; + nxt_conf_value_t *host; + nxt_conf_value_t *uri; + nxt_conf_value_t *method; + nxt_conf_value_t *headers; + nxt_conf_value_t *arguments; + nxt_conf_value_t *cookies; + nxt_conf_value_t *scheme; + nxt_conf_value_t *query; + nxt_conf_value_t *source; + nxt_conf_value_t *destination; + nxt_conf_value_t *condition; } nxt_http_route_match_conf_t; - typedef struct { - u_char *start; - uint32_t length; - nxt_http_route_pattern_type_t type:8; + u_char *start; + uint32_t length; + nxt_http_route_pattern_type_t type:8; } nxt_http_route_pattern_slice_t; - typedef struct { union { - nxt_array_t *pattern_slices; + nxt_array_t *pattern_slices; #if (NXT_HAVE_REGEX) - nxt_regex_t *regex; + nxt_regex_t *regex; #endif } u; - uint32_t min_length; - uint8_t case_sensitive; /* 1 bit */ - uint8_t negative; /* 1 bit */ - uint8_t any; /* 1 bit */ + uint32_t min_length; + + uint8_t case_sensitive; /* 1 bit */ + uint8_t negative; /* 1 bit */ + uint8_t any; /* 1 bit */ #if (NXT_HAVE_REGEX) - uint8_t regex; /* 1 bit */ + uint8_t regex; /* 1 bit */ #endif } nxt_http_route_pattern_t; - typedef struct { - uint16_t hash; - uint16_t name_length; - uint32_t value_length; - u_char *name; - u_char *value; + uint16_t hash; + uint16_t name_length; + uint32_t value_length; + u_char *name; + u_char *value; } nxt_http_cookie_t; - struct nxt_http_route_rule_s { /* The object must be the first field. */ - nxt_http_route_object_t object:8; - uint32_t items; + nxt_http_route_object_t object:8; + uint32_t items; union { - uintptr_t offset; + uintptr_t offset; struct { - u_char *start; - uint16_t hash; - uint16_t length; + u_char *start; + uint16_t hash; + uint16_t length; } name; } u; - nxt_http_route_pattern_t pattern[]; + nxt_http_route_pattern_t pattern[]; }; - typedef struct { - uint32_t items; - nxt_http_route_rule_t *rule[]; + uint32_t items; + nxt_http_route_rule_t *rule[]; } nxt_http_route_ruleset_t; - typedef struct { /* The object must be the first field. */ - nxt_http_route_object_t object:8; - uint32_t items; - nxt_http_route_ruleset_t *ruleset[]; + nxt_http_route_object_t object:8; + uint32_t items; + nxt_http_route_ruleset_t *ruleset[]; } nxt_http_route_table_t; - struct nxt_http_route_addr_rule_s { /* The object must be the first field. */ - nxt_http_route_object_t object:8; - uint32_t items; - nxt_http_route_addr_pattern_t addr_pattern[]; + nxt_http_route_object_t object:8; + uint32_t items; + nxt_http_route_addr_pattern_t addr_pattern[]; }; - typedef union { - nxt_http_route_rule_t *rule; - nxt_http_route_table_t *table; - nxt_http_route_addr_rule_t *addr_rule; + nxt_http_route_rule_t *rule; + nxt_http_route_table_t *table; + nxt_http_route_addr_rule_t *addr_rule; } nxt_http_route_test_t; - typedef struct { - uint32_t items; - nxt_tstr_cond_t condition; - nxt_http_action_t action; - nxt_http_route_test_t test[]; + uint32_t items; + nxt_tstr_cond_t condition; + nxt_http_action_t action; + nxt_http_route_test_t test[]; } nxt_http_route_match_t; - struct nxt_http_route_s { - nxt_str_t name; - uint32_t items; - nxt_http_route_match_t *match[]; + nxt_str_t name; + uint32_t items; + nxt_http_route_match_t *match[]; }; - struct nxt_http_routes_s { - uint32_t items; - nxt_http_route_t *route[]; + uint32_t items; + nxt_http_route_t *route[]; }; - -static nxt_http_route_t *nxt_http_route_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv); -static nxt_http_route_match_t *nxt_http_route_match_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *cv); -static nxt_http_route_table_t *nxt_http_route_table_create(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *table_cv, nxt_http_route_object_t object, - nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding); -static nxt_http_route_ruleset_t *nxt_http_route_ruleset_create(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *ruleset_cv, nxt_http_route_object_t object, +static nxt_http_route_t * +nxt_http_route_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_conf_value_t *cv); +static nxt_http_route_match_t * +nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_conf_value_t *cv); +static nxt_http_route_table_t * +nxt_http_route_table_create(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *table_cv, nxt_http_route_object_t object, nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding); -static nxt_http_route_rule_t *nxt_http_route_rule_name_create(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *rule_cv, nxt_str_t *name, +static nxt_http_route_ruleset_t * +nxt_http_route_ruleset_create(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *ruleset_cv, nxt_http_route_object_t object, nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding); -static nxt_http_route_rule_t *nxt_http_route_rule_create(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *cv, nxt_bool_t case_sensitive, - nxt_http_route_pattern_case_t pattern_case, +static nxt_http_route_rule_t * +nxt_http_route_rule_name_create(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *rule_cv, nxt_str_t *name, nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding); -static int nxt_http_pattern_compare(const void *one, const void *two); -static int nxt_http_addr_pattern_compare(const void *one, const void *two); -static nxt_int_t nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, +static nxt_http_route_rule_t * +nxt_http_route_rule_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv, + nxt_bool_t case_sensitive, nxt_http_route_pattern_case_t pattern_case, + nxt_http_uri_encoding_t encoding); +static int +nxt_http_pattern_compare(const void *one, const void *two); +static int +nxt_http_addr_pattern_compare(const void *one, const void *two); +static nxt_int_t +nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv, nxt_http_route_pattern_t *pattern, nxt_http_route_pattern_case_t pattern_case, - nxt_http_uri_encoding_t encoding); -static nxt_int_t nxt_http_route_decode_str(nxt_str_t *str, - nxt_http_uri_encoding_t encoding); -static nxt_int_t nxt_http_route_pattern_slice(nxt_array_t *slices, - nxt_str_t *test, - nxt_http_route_pattern_type_t type, - nxt_http_uri_encoding_t encoding, + nxt_http_uri_encoding_t encoding); +static nxt_int_t +nxt_http_route_decode_str(nxt_str_t *str, nxt_http_uri_encoding_t encoding); +static nxt_int_t +nxt_http_route_pattern_slice(nxt_array_t *slices, nxt_str_t *test, + nxt_http_route_pattern_type_t type, nxt_http_uri_encoding_t encoding, nxt_http_route_pattern_case_t pattern_case); -static nxt_int_t nxt_http_route_resolve(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_http_route_t *route); -static nxt_int_t nxt_http_action_resolve(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_http_action_t *action); -static nxt_http_action_t *nxt_http_pass_var(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); -static void nxt_http_pass_query(nxt_task_t *task, nxt_http_request_t *r, +static nxt_int_t +nxt_http_route_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_http_route_t *route); +static nxt_int_t +nxt_http_action_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_http_action_t *action); -static nxt_int_t nxt_http_pass_find(nxt_mp_t *mp, nxt_router_conf_t *rtcf, - nxt_str_t *pass, nxt_http_action_t *action); -static nxt_int_t nxt_http_route_find(nxt_http_routes_t *routes, nxt_str_t *name, +static nxt_http_action_t * +nxt_http_pass_var(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); +static void +nxt_http_pass_query(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); +static nxt_int_t +nxt_http_pass_find(nxt_mp_t *mp, nxt_router_conf_t *rtcf, nxt_str_t *pass, + nxt_http_action_t *action); +static nxt_int_t +nxt_http_route_find(nxt_http_routes_t *routes, nxt_str_t *name, nxt_http_action_t *action); -static nxt_http_action_t *nxt_http_route_handler(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *start); -static nxt_http_action_t *nxt_http_route_match(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_route_match_t *match); -static nxt_int_t nxt_http_route_table(nxt_http_request_t *r, - nxt_http_route_table_t *table); -static nxt_int_t nxt_http_route_ruleset(nxt_http_request_t *r, - nxt_http_route_ruleset_t *ruleset); -static nxt_int_t nxt_http_route_rule(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_header(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_arguments(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_test_argument(nxt_http_request_t *r, - nxt_http_route_rule_t *rule, nxt_array_t *array); -static nxt_int_t nxt_http_route_scheme(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_query(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_cookies(nxt_http_request_t *r, - nxt_http_route_rule_t *rule); -static nxt_int_t nxt_http_route_test_cookie(nxt_http_request_t *r, - nxt_http_route_rule_t *rule, nxt_array_t *array); -static nxt_int_t nxt_http_route_pattern(nxt_http_request_t *r, - nxt_http_route_pattern_t *pattern, u_char *start, size_t length); -static nxt_int_t nxt_http_route_memcmp(u_char *start, u_char *test, - size_t length, nxt_bool_t case_sensitive); - +static nxt_http_action_t * +nxt_http_route_handler(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *start); +static nxt_http_action_t * +nxt_http_route_match(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_route_match_t *match); +static nxt_int_t +nxt_http_route_table(nxt_http_request_t *r, nxt_http_route_table_t *table); +static nxt_int_t +nxt_http_route_ruleset(nxt_http_request_t *r, + nxt_http_route_ruleset_t *ruleset); +static nxt_int_t +nxt_http_route_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_header(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_test_argument(nxt_http_request_t *r, nxt_http_route_rule_t *rule, + nxt_array_t *array); +static nxt_int_t +nxt_http_route_scheme(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_query(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule); +static nxt_int_t +nxt_http_route_test_cookie(nxt_http_request_t *r, nxt_http_route_rule_t *rule, + nxt_array_t *array); +static nxt_int_t +nxt_http_route_pattern(nxt_http_request_t *r, nxt_http_route_pattern_t *pattern, + u_char *start, size_t length); +static nxt_int_t +nxt_http_route_memcmp(u_char *start, u_char *test, size_t length, + nxt_bool_t case_sensitive); nxt_http_routes_t * nxt_http_routes_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *routes_conf) -{ + nxt_conf_value_t *routes_conf) { size_t size; uint32_t i, n, next; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_str_t name, *string; nxt_bool_t object; - nxt_conf_value_t *route_conf; - nxt_http_route_t *route; - nxt_http_routes_t *routes; + nxt_conf_value_t *route_conf; + nxt_http_route_t *route; + nxt_http_routes_t *routes; object = (nxt_conf_type(routes_conf) == NXT_CONF_OBJECT); - n = object ? nxt_conf_object_members_count(routes_conf) : 1; - size = sizeof(nxt_http_routes_t) + n * sizeof(nxt_http_route_t *); + n = object ? nxt_conf_object_members_count(routes_conf) : 1; + size = sizeof(nxt_http_routes_t) + n * sizeof(nxt_http_route_t *); mp = tmcf->router_conf->mem_pool; @@ -286,19 +288,15 @@ nxt_http_routes_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, routes->route[0] = route; route->name.length = 0; - route->name.start = NULL; + route->name.start = NULL; } return routes; } - -static nxt_conf_map_t nxt_http_route_match_conf[] = { - { - nxt_string("scheme"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_route_match_conf_t, scheme) - }, +static nxt_conf_map_t nxt_http_route_match_conf[] = { + {nxt_string("scheme"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_route_match_conf_t, scheme)}, { nxt_string("host"), NXT_CONF_MAP_PTR, @@ -360,18 +358,16 @@ static nxt_conf_map_t nxt_http_route_match_conf[] = { }, }; - static nxt_http_route_t * nxt_http_route_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *cv) -{ + nxt_conf_value_t *cv) { size_t size; uint32_t i, n; - nxt_conf_value_t *value; - nxt_http_route_t *route; - nxt_http_route_match_t *match, **m; + nxt_conf_value_t *value; + nxt_http_route_t *route; + nxt_http_route_match_t *match, **m; - n = nxt_conf_array_elements_count(cv); + n = nxt_conf_array_elements_count(cv); size = sizeof(nxt_http_route_t) + n * sizeof(nxt_http_route_match_t *); route = nxt_mp_alloc(tmcf->router_conf->mem_pool, size); @@ -380,7 +376,7 @@ nxt_http_route_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } route->items = n; - m = &route->match[0]; + m = &route->match[0]; for (i = 0; i < n; i++) { value = nxt_conf_get_array_element(cv, i); @@ -396,36 +392,34 @@ nxt_http_route_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return route; } - static nxt_http_route_match_t * nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *cv) -{ - size_t size; - uint32_t n; - nxt_mp_t *mp; - nxt_int_t ret; - nxt_str_t str; - nxt_conf_value_t *match_conf, *action_conf, *condition; - nxt_router_conf_t *rtcf; - nxt_http_route_test_t *test; - nxt_http_route_rule_t *rule; - nxt_http_route_table_t *table; - nxt_http_route_match_t *match; - nxt_http_route_addr_rule_t *addr_rule; - nxt_http_route_match_conf_t mtcf; - - static const nxt_str_t if_path = nxt_string("/if"); - static const nxt_str_t match_path = nxt_string("/match"); - static const nxt_str_t action_path = nxt_string("/action"); + nxt_conf_value_t *cv) { + size_t size; + uint32_t n; + nxt_mp_t *mp; + nxt_int_t ret; + nxt_str_t str; + nxt_conf_value_t *match_conf, *action_conf, *condition; + nxt_router_conf_t *rtcf; + nxt_http_route_test_t *test; + nxt_http_route_rule_t *rule; + nxt_http_route_table_t *table; + nxt_http_route_match_t *match; + nxt_http_route_addr_rule_t *addr_rule; + nxt_http_route_match_conf_t mtcf; + + static const nxt_str_t if_path = nxt_string("/if"); + static const nxt_str_t match_path = nxt_string("/match"); + static const nxt_str_t action_path = nxt_string("/action"); match_conf = nxt_conf_get_path(cv, &match_path); - n = (match_conf != NULL) ? nxt_conf_object_members_count(match_conf) : 0; + n = (match_conf != NULL) ? nxt_conf_object_members_count(match_conf) : 0; size = sizeof(nxt_http_route_match_t) + n * sizeof(nxt_http_route_test_t *); rtcf = tmcf->router_conf; - mp = rtcf->mem_pool; + mp = rtcf->mem_pool; condition = NULL; @@ -461,9 +455,9 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_memzero(&mtcf, sizeof(mtcf)); - ret = nxt_conf_map_object(tmcf->mem_pool, - match_conf, nxt_http_route_match_conf, - nxt_nitems(nxt_http_route_match_conf), &mtcf); + ret = nxt_conf_map_object(tmcf->mem_pool, match_conf, + nxt_http_route_match_conf, nxt_nitems(nxt_http_route_match_conf), + &mtcf); if (ret != NXT_OK) { return NULL; } @@ -481,63 +475,58 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (mtcf.scheme != NULL) { rule = nxt_http_route_rule_create(task, mp, mtcf.scheme, 1, - NXT_HTTP_ROUTE_PATTERN_NOCASE, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_PATTERN_NOCASE, NXT_HTTP_URI_ENCODING_NONE); if (rule == NULL) { return NULL; } rule->object = NXT_HTTP_ROUTE_SCHEME; - test->rule = rule; + test->rule = rule; test++; } if (mtcf.host != NULL) { rule = nxt_http_route_rule_create(task, mp, mtcf.host, 1, - NXT_HTTP_ROUTE_PATTERN_LOWCASE, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_PATTERN_LOWCASE, NXT_HTTP_URI_ENCODING_NONE); if (rule == NULL) { return NULL; } rule->u.offset = offsetof(nxt_http_request_t, host); - rule->object = NXT_HTTP_ROUTE_STRING; - test->rule = rule; + rule->object = NXT_HTTP_ROUTE_STRING; + test->rule = rule; test++; } if (mtcf.uri != NULL) { rule = nxt_http_route_rule_create(task, mp, mtcf.uri, 1, - NXT_HTTP_ROUTE_PATTERN_NOCASE, - NXT_HTTP_URI_ENCODING); + NXT_HTTP_ROUTE_PATTERN_NOCASE, NXT_HTTP_URI_ENCODING); if (rule == NULL) { return NULL; } rule->u.offset = offsetof(nxt_http_request_t, path); - rule->object = NXT_HTTP_ROUTE_STRING_PTR; - test->rule = rule; + rule->object = NXT_HTTP_ROUTE_STRING_PTR; + test->rule = rule; test++; } if (mtcf.method != NULL) { rule = nxt_http_route_rule_create(task, mp, mtcf.method, 1, - NXT_HTTP_ROUTE_PATTERN_UPCASE, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_PATTERN_UPCASE, NXT_HTTP_URI_ENCODING_NONE); if (rule == NULL) { return NULL; } rule->u.offset = offsetof(nxt_http_request_t, method); - rule->object = NXT_HTTP_ROUTE_STRING_PTR; - test->rule = rule; + rule->object = NXT_HTTP_ROUTE_STRING_PTR; + test->rule = rule; test++; } if (mtcf.headers != NULL) { table = nxt_http_route_table_create(task, mp, mtcf.headers, - NXT_HTTP_ROUTE_HEADER, 0, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_HEADER, 0, NXT_HTTP_URI_ENCODING_NONE); if (table == NULL) { return NULL; } @@ -548,8 +537,7 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (mtcf.arguments != NULL) { table = nxt_http_route_table_create(task, mp, mtcf.arguments, - NXT_HTTP_ROUTE_ARGUMENT, 1, - NXT_HTTP_URI_ENCODING_PLUS); + NXT_HTTP_ROUTE_ARGUMENT, 1, NXT_HTTP_URI_ENCODING_PLUS); if (table == NULL) { return NULL; } @@ -560,8 +548,7 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (mtcf.cookies != NULL) { table = nxt_http_route_table_create(task, mp, mtcf.cookies, - NXT_HTTP_ROUTE_COOKIE, 1, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_COOKIE, 1, NXT_HTTP_URI_ENCODING_NONE); if (table == NULL) { return NULL; } @@ -572,14 +559,13 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (mtcf.query != NULL) { rule = nxt_http_route_rule_create(task, mp, mtcf.query, 1, - NXT_HTTP_ROUTE_PATTERN_NOCASE, - NXT_HTTP_URI_ENCODING_PLUS); + NXT_HTTP_ROUTE_PATTERN_NOCASE, NXT_HTTP_URI_ENCODING_PLUS); if (rule == NULL) { return NULL; } rule->object = NXT_HTTP_ROUTE_QUERY; - test->rule = rule; + test->rule = rule; test++; } @@ -590,7 +576,7 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } addr_rule->object = NXT_HTTP_ROUTE_SOURCE; - test->addr_rule = addr_rule; + test->addr_rule = addr_rule; test++; } @@ -601,97 +587,55 @@ nxt_http_route_match_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } addr_rule->object = NXT_HTTP_ROUTE_DESTINATION; - test->addr_rule = addr_rule; + test->addr_rule = addr_rule; test++; } return match; } - -static nxt_conf_map_t nxt_http_route_action_conf[] = { - { - nxt_string("rewrite"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, rewrite) - }, - { - nxt_string("response_headers"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, set_headers) - }, - { - nxt_string("pass"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, pass) - }, - { - nxt_string("return"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, ret) - }, - { - nxt_string("location"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, location) - }, - { - nxt_string("proxy"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, proxy) - }, - { - nxt_string("share"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, share) - }, - { - nxt_string("index"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, index) - }, - { - nxt_string("chroot"), - NXT_CONF_MAP_STR, - offsetof(nxt_http_action_conf_t, chroot) - }, - { - nxt_string("follow_symlinks"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, follow_symlinks) - }, - { - nxt_string("traverse_mounts"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, traverse_mounts) - }, - { - nxt_string("types"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, types) - }, - { - nxt_string("fallback"), - NXT_CONF_MAP_PTR, - offsetof(nxt_http_action_conf_t, fallback) - }, +static nxt_conf_map_t nxt_http_route_action_conf[] = { + {nxt_string("rewrite"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, rewrite)}, + {nxt_string("response_headers"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, set_headers)}, + {nxt_string("pass"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, pass)}, + {nxt_string("return"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, ret)}, + {nxt_string("location"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, location)}, + {nxt_string("proxy"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, proxy)}, + {nxt_string("share"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, share)}, + {nxt_string("index"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, index)}, + {nxt_string("chroot"), NXT_CONF_MAP_STR, + offsetof(nxt_http_action_conf_t, chroot)}, + {nxt_string("follow_symlinks"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, follow_symlinks)}, + {nxt_string("traverse_mounts"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, traverse_mounts)}, + {nxt_string("types"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, types)}, + {nxt_string("fallback"), NXT_CONF_MAP_PTR, + offsetof(nxt_http_action_conf_t, fallback)}, }; - nxt_int_t nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *cv, nxt_http_action_t *action) -{ - nxt_mp_t *mp; - nxt_int_t ret; - nxt_str_t pass; - nxt_router_conf_t *rtcf; - nxt_http_action_conf_t acf; + nxt_conf_value_t *cv, nxt_http_action_t *action) { + nxt_mp_t *mp; + nxt_int_t ret; + nxt_str_t pass; + nxt_router_conf_t *rtcf; + nxt_http_action_conf_t acf; nxt_memzero(&acf, sizeof(acf)); ret = nxt_conf_map_object(tmcf->mem_pool, cv, nxt_http_route_action_conf, - nxt_nitems(nxt_http_route_action_conf), &acf); + nxt_nitems(nxt_http_route_action_conf), &acf); if (ret != NXT_OK) { return ret; } @@ -699,7 +643,7 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_memzero(action, sizeof(nxt_http_action_t)); rtcf = tmcf->router_conf; - mp = rtcf->mem_pool; + mp = rtcf->mem_pool; if (acf.rewrite != NULL) { ret = nxt_http_rewrite_init(rtcf, action, &acf); @@ -737,19 +681,17 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_http_route_table_t * nxt_http_route_table_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *table_cv, nxt_http_route_object_t object, - nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding) -{ + nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding) { size_t size; uint32_t i, n; - nxt_conf_value_t *ruleset_cv; - nxt_http_route_table_t *table; - nxt_http_route_ruleset_t *ruleset; + nxt_conf_value_t *ruleset_cv; + nxt_http_route_table_t *table; + nxt_http_route_ruleset_t *ruleset; - n = nxt_conf_array_elements_count_or_1(table_cv); + n = nxt_conf_array_elements_count_or_1(table_cv); size = sizeof(nxt_http_route_table_t) + n * sizeof(nxt_http_route_ruleset_t *); @@ -758,14 +700,14 @@ nxt_http_route_table_create(nxt_task_t *task, nxt_mp_t *mp, return NULL; } - table->items = n; + table->items = n; table->object = NXT_HTTP_ROUTE_TABLE; for (i = 0; i < n; i++) { ruleset_cv = nxt_conf_get_array_element_or_itself(table_cv, i); ruleset = nxt_http_route_ruleset_create(task, mp, ruleset_cv, object, - case_sensitive, encoding); + case_sensitive, encoding); if (nxt_slow_path(ruleset == NULL)) { return NULL; } @@ -776,20 +718,18 @@ nxt_http_route_table_create(nxt_task_t *task, nxt_mp_t *mp, return table; } - static nxt_http_route_ruleset_t * nxt_http_route_ruleset_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *ruleset_cv, nxt_http_route_object_t object, - nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding) -{ + nxt_bool_t case_sensitive, nxt_http_uri_encoding_t encoding) { size_t size; uint32_t i, n, next; nxt_str_t name; - nxt_conf_value_t *rule_cv; - nxt_http_route_rule_t *rule; - nxt_http_route_ruleset_t *ruleset; + nxt_conf_value_t *rule_cv; + nxt_http_route_rule_t *rule; + nxt_http_route_ruleset_t *ruleset; - n = nxt_conf_object_members_count(ruleset_cv); + n = nxt_conf_object_members_count(ruleset_cv); size = sizeof(nxt_http_route_ruleset_t) + n * sizeof(nxt_http_route_rule_t *); @@ -812,30 +752,27 @@ nxt_http_route_ruleset_create(nxt_task_t *task, nxt_mp_t *mp, rule_cv = nxt_conf_next_object_member(ruleset_cv, &name, &next); rule = nxt_http_route_rule_name_create(task, mp, rule_cv, &name, - case_sensitive, encoding); + case_sensitive, encoding); if (nxt_slow_path(rule == NULL)) { return NULL; } - rule->object = object; + rule->object = object; ruleset->rule[i] = rule; } return ruleset; } - static nxt_http_route_rule_t * nxt_http_route_rule_name_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *rule_cv, nxt_str_t *name, nxt_bool_t case_sensitive, - nxt_http_uri_encoding_t encoding) -{ + nxt_http_uri_encoding_t encoding) { int64_t hash; - nxt_http_route_rule_t *rule; + nxt_http_route_rule_t *rule; rule = nxt_http_route_rule_create(task, mp, rule_cv, case_sensitive, - NXT_HTTP_ROUTE_PATTERN_NOCASE, - encoding); + NXT_HTTP_ROUTE_PATTERN_NOCASE, encoding); if (nxt_slow_path(rule == NULL)) { return NULL; } @@ -845,28 +782,25 @@ nxt_http_route_rule_name_create(nxt_task_t *task, nxt_mp_t *mp, return NULL; } - rule->u.name.hash = hash; - rule->u.name.start = name->start; + rule->u.name.hash = hash; + rule->u.name.start = name->start; rule->u.name.length = name->length; return rule; } - static nxt_http_route_rule_t * -nxt_http_route_rule_create(nxt_task_t *task, nxt_mp_t *mp, - nxt_conf_value_t *cv, nxt_bool_t case_sensitive, - nxt_http_route_pattern_case_t pattern_case, - nxt_http_uri_encoding_t encoding) -{ +nxt_http_route_rule_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv, + nxt_bool_t case_sensitive, nxt_http_route_pattern_case_t pattern_case, + nxt_http_uri_encoding_t encoding) { size_t size; uint32_t i, n; nxt_int_t ret; - nxt_conf_value_t *value; - nxt_http_route_rule_t *rule; - nxt_http_route_pattern_t *pattern; + nxt_conf_value_t *value; + nxt_http_route_rule_t *rule; + nxt_http_route_pattern_t *pattern; - n = nxt_conf_array_elements_count_or_1(cv); + n = nxt_conf_array_elements_count_or_1(cv); size = sizeof(nxt_http_route_rule_t) + n * sizeof(nxt_http_route_pattern_t); rule = nxt_mp_alloc(mp, size); @@ -882,10 +816,10 @@ nxt_http_route_rule_create(nxt_task_t *task, nxt_mp_t *mp, for (i = 0; i < n; i++) { pattern[i].case_sensitive = case_sensitive; - value = nxt_conf_get_array_element_or_itself(cv, i); + value = nxt_conf_get_array_element_or_itself(cv, i); ret = nxt_http_route_pattern_create(task, mp, value, &pattern[i], - pattern_case, encoding); + pattern_case, encoding); if (nxt_slow_path(ret != NXT_OK)) { return NULL; } @@ -894,16 +828,14 @@ nxt_http_route_rule_create(nxt_task_t *task, nxt_mp_t *mp, return rule; } - nxt_http_route_addr_rule_t * nxt_http_route_addr_rule_create(nxt_task_t *task, nxt_mp_t *mp, - nxt_conf_value_t *cv) -{ + nxt_conf_value_t *cv) { size_t size; uint32_t i, n; - nxt_conf_value_t *value; - nxt_http_route_addr_rule_t *addr_rule; - nxt_http_route_addr_pattern_t *pattern; + nxt_conf_value_t *value; + nxt_http_route_addr_rule_t *addr_rule; + nxt_http_route_addr_pattern_t *pattern; n = nxt_conf_array_elements_count_or_1(cv); @@ -919,7 +851,7 @@ nxt_http_route_addr_rule_create(nxt_task_t *task, nxt_mp_t *mp, for (i = 0; i < n; i++) { pattern = &addr_rule->addr_pattern[i]; - value = nxt_conf_get_array_element_or_itself(cv, i); + value = nxt_conf_get_array_element_or_itself(cv, i); if (nxt_http_route_addr_pattern_parse(mp, pattern, value) != NXT_OK) { return NULL; @@ -935,23 +867,18 @@ nxt_http_route_addr_rule_create(nxt_task_t *task, nxt_mp_t *mp, return addr_rule; } - nxt_http_route_rule_t * nxt_http_route_types_rule_create(nxt_task_t *task, nxt_mp_t *mp, - nxt_conf_value_t *types) -{ + nxt_conf_value_t *types) { return nxt_http_route_rule_create(task, mp, types, 0, - NXT_HTTP_ROUTE_PATTERN_LOWCASE, - NXT_HTTP_URI_ENCODING_NONE); + NXT_HTTP_ROUTE_PATTERN_LOWCASE, NXT_HTTP_URI_ENCODING_NONE); } - static int -nxt_http_pattern_compare(const void *one, const void *two) -{ +nxt_http_pattern_compare(const void *one, const void *two) { nxt_str_t test; nxt_bool_t negative1, negative2; - nxt_conf_value_t *value; + nxt_conf_value_t *value; value = (nxt_conf_value_t *) one; nxt_conf_get_string(value, &test); @@ -964,11 +891,9 @@ nxt_http_pattern_compare(const void *one, const void *two) return (negative2 - negative1); } - static int -nxt_http_addr_pattern_compare(const void *one, const void *two) -{ - const nxt_http_route_addr_pattern_t *p1, *p2; +nxt_http_addr_pattern_compare(const void *one, const void *two) { + const nxt_http_route_addr_pattern_t *p1, *p2; p1 = one; p2 = two; @@ -976,32 +901,30 @@ nxt_http_addr_pattern_compare(const void *one, const void *two) return (p2->base.negative - p1->base.negative); } - static nxt_int_t nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *cv, nxt_http_route_pattern_t *pattern, nxt_http_route_pattern_case_t pattern_case, - nxt_http_uri_encoding_t encoding) -{ - u_char c, *p, *end; - nxt_str_t test, tmp; - nxt_int_t ret; - nxt_array_t *slices; + nxt_http_uri_encoding_t encoding) { + u_char c, *p, *end; + nxt_str_t test, tmp; + nxt_int_t ret; + nxt_array_t *slices; #if (NXT_HAVE_REGEX) - nxt_regex_t *re; - nxt_regex_err_t err; + nxt_regex_t *re; + nxt_regex_err_t err; #endif nxt_http_route_pattern_type_t type; - nxt_http_route_pattern_slice_t *slice; + nxt_http_route_pattern_slice_t *slice; type = NXT_HTTP_ROUTE_PATTERN_EXACT; nxt_conf_get_string(cv, &test); pattern->u.pattern_slices = NULL; - pattern->negative = 0; - pattern->any = 1; - pattern->min_length = 0; + pattern->negative = 0; + pattern->any = 1; + pattern->min_length = 0; #if (NXT_HAVE_REGEX) pattern->regex = 0; #endif @@ -1011,7 +934,7 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, test.length--; pattern->negative = 1; - pattern->any = 0; + pattern->any = 0; } if (test.length > 0 && test.start[0] == '~') { @@ -1023,7 +946,7 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, if (nxt_slow_path(re == NULL)) { if (err.offset < test.length) { nxt_alert(task, "nxt_regex_compile(%V) failed: %s at offset %d", - &test, err.msg, (int) err.offset); + &test, err.msg, (int) err.offset); return NXT_ERROR; } @@ -1033,7 +956,7 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, } pattern->u.regex = re; - pattern->regex = 1; + pattern->regex = 1; return NXT_OK; @@ -1055,8 +978,8 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, return NXT_ERROR; } - slice->type = NXT_HTTP_ROUTE_PATTERN_EXACT; - slice->start = NULL; + slice->type = NXT_HTTP_ROUTE_PATTERN_EXACT; + slice->start = NULL; slice->length = 0; return NXT_OK; @@ -1077,22 +1000,22 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, if (p == NULL) { /* No '*' found - EXACT pattern. */ tmp.length = test.length; - type = NXT_HTTP_ROUTE_PATTERN_EXACT; + type = NXT_HTTP_ROUTE_PATTERN_EXACT; - test.start += test.length; - test.length = 0; + test.start += test.length; + test.length = 0; } else { /* '*' found - BEGIN pattern. */ tmp.length = p - test.start; - type = NXT_HTTP_ROUTE_PATTERN_BEGIN; + type = NXT_HTTP_ROUTE_PATTERN_BEGIN; - test.start = p + 1; + test.start = p + 1; test.length -= tmp.length + 1; } ret = nxt_http_route_pattern_slice(slices, &tmp, type, encoding, - pattern_case); + pattern_case); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -1114,15 +1037,14 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, } } - tmp.start = p; + tmp.start = p; tmp.length = end - p; test.length -= tmp.length; - end = p; + end = p; ret = nxt_http_route_pattern_slice(slices, &tmp, - NXT_HTTP_ROUTE_PATTERN_END, - encoding, pattern_case); + NXT_HTTP_ROUTE_PATTERN_END, encoding, pattern_case); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -1130,7 +1052,7 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, pattern->min_length += tmp.length; } - tmp.start = test.start; + tmp.start = test.start; tmp.length = 0; p = tmp.start; @@ -1149,22 +1071,20 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, } ret = nxt_http_route_pattern_slice(slices, &tmp, - NXT_HTTP_ROUTE_PATTERN_SUBSTRING, - encoding, pattern_case); + NXT_HTTP_ROUTE_PATTERN_SUBSTRING, encoding, pattern_case); if (nxt_slow_path(ret != NXT_OK)) { return ret; } pattern->min_length += tmp.length; - tmp.start = p; + tmp.start = p; tmp.length = 0; } if (tmp.length != 0) { ret = nxt_http_route_pattern_slice(slices, &tmp, - NXT_HTTP_ROUTE_PATTERN_SUBSTRING, - encoding, pattern_case); + NXT_HTTP_ROUTE_PATTERN_SUBSTRING, encoding, pattern_case); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -1175,11 +1095,9 @@ nxt_http_route_pattern_create(nxt_task_t *task, nxt_mp_t *mp, return NXT_OK; } - static nxt_int_t -nxt_http_route_decode_str(nxt_str_t *str, nxt_http_uri_encoding_t encoding) -{ - u_char *start, *end; +nxt_http_route_decode_str(nxt_str_t *str, nxt_http_uri_encoding_t encoding) { + u_char *start, *end; switch (encoding) { case NXT_HTTP_URI_ENCODING_NONE: @@ -1214,16 +1132,13 @@ nxt_http_route_decode_str(nxt_str_t *str, nxt_http_uri_encoding_t encoding) return NXT_OK; } - static nxt_int_t -nxt_http_route_pattern_slice(nxt_array_t *slices, - nxt_str_t *test, nxt_http_route_pattern_type_t type, - nxt_http_uri_encoding_t encoding, - nxt_http_route_pattern_case_t pattern_case) -{ - u_char *start; +nxt_http_route_pattern_slice(nxt_array_t *slices, nxt_str_t *test, + nxt_http_route_pattern_type_t type, nxt_http_uri_encoding_t encoding, + nxt_http_route_pattern_case_t pattern_case) { + u_char *start; nxt_int_t ret; - nxt_http_route_pattern_slice_t *slice; + nxt_http_route_pattern_slice_t *slice; ret = nxt_http_route_decode_str(test, encoding); if (nxt_slow_path(ret != NXT_OK)) { @@ -1236,7 +1151,6 @@ nxt_http_route_pattern_slice(nxt_array_t *slices, } switch (pattern_case) { - case NXT_HTTP_ROUTE_PATTERN_UPCASE: nxt_memcpy_upcase(start, test->start, test->length); break; @@ -1255,26 +1169,24 @@ nxt_http_route_pattern_slice(nxt_array_t *slices, return NXT_ERROR; } - slice->type = type; - slice->start = start; + slice->type = type; + slice->start = start; slice->length = test->length; return NXT_OK; } - nxt_int_t -nxt_http_routes_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) -{ +nxt_http_routes_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) { nxt_int_t ret; - nxt_http_route_t **route, **end; - nxt_http_routes_t *routes; + nxt_http_route_t **route, **end; + nxt_http_routes_t *routes; routes = tmcf->router_conf->routes; if (routes != NULL) { route = &routes->route[0]; - end = route + routes->items; + end = route + routes->items; while (route < end) { ret = nxt_http_route_resolve(task, tmcf, *route); @@ -1289,16 +1201,14 @@ nxt_http_routes_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) return NXT_OK; } - static nxt_int_t nxt_http_route_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_http_route_t *route) -{ - nxt_int_t ret; - nxt_http_route_match_t **match, **end; + nxt_http_route_t *route) { + nxt_int_t ret; + nxt_http_route_match_t **match, **end; match = &route->match[0]; - end = match + route->items; + end = match + route->items; while (match < end) { ret = nxt_http_action_resolve(task, tmcf, &(*match)->action); @@ -1312,13 +1222,11 @@ nxt_http_route_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_int_t nxt_http_action_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_http_action_t *action) -{ - nxt_int_t ret; - nxt_str_t pass; + nxt_http_action_t *action) { + nxt_int_t ret; + nxt_str_t pass; if (action->handler != NULL) { if (action->fallback != NULL) { @@ -1332,7 +1240,7 @@ nxt_http_action_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_tstr_str(action->u.tstr, &pass); ret = nxt_http_pass_find(tmcf->mem_pool, tmcf->router_conf, &pass, - action); + action); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -1344,15 +1252,13 @@ nxt_http_action_resolve(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_http_action_t * nxt_http_pass_var(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { nxt_int_t ret; nxt_str_t str; - nxt_tstr_t *tstr; - nxt_router_conf_t *rtcf; + nxt_tstr_t *tstr; + nxt_router_conf_t *rtcf; tstr = action->u.tstr; @@ -1363,13 +1269,13 @@ nxt_http_pass_var(nxt_task_t *task, nxt_http_request_t *r, rtcf = r->conf->socket_conf->router_conf; ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, &r->tstr_cache, - r, r->mem_pool); + r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } action = nxt_mp_zget(r->mem_pool, - sizeof(nxt_http_action_t) + sizeof(nxt_str_t)); + sizeof(nxt_http_action_t) + sizeof(nxt_str_t)); if (nxt_slow_path(action == NULL)) { goto fail; } @@ -1391,13 +1297,11 @@ nxt_http_pass_var(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static void nxt_http_pass_query(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { nxt_int_t ret; - nxt_router_conf_t *rtcf; + nxt_router_conf_t *rtcf; nxt_http_status_t status; rtcf = r->conf->socket_conf->router_conf; @@ -1417,13 +1321,11 @@ nxt_http_pass_query(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_action(task, r, action); } - static nxt_int_t nxt_http_pass_find(nxt_mp_t *mp, nxt_router_conf_t *rtcf, nxt_str_t *pass, - nxt_http_action_t *action) -{ - nxt_int_t ret; - nxt_str_t segments[3]; + nxt_http_action_t *action) { + nxt_int_t ret; + nxt_str_t segments[3]; ret = nxt_http_pass_segments(mp, pass, segments, 3); if (nxt_slow_path(ret != NXT_OK)) { @@ -1432,7 +1334,7 @@ nxt_http_pass_find(nxt_mp_t *mp, nxt_router_conf_t *rtcf, nxt_str_t *pass, if (nxt_str_eq(&segments[0], "applications", 12)) { return nxt_router_application_init(rtcf, &segments[1], &segments[2], - action); + action); } if (segments[2].length == 0) { @@ -1448,13 +1350,11 @@ nxt_http_pass_find(nxt_mp_t *mp, nxt_router_conf_t *rtcf, nxt_str_t *pass, return NXT_DECLINED; } - nxt_int_t nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass, nxt_str_t *segments, - nxt_uint_t n) -{ - u_char *p; - nxt_str_t rest; + nxt_uint_t n) { + u_char *p; + nxt_str_t rest; if (nxt_slow_path(nxt_str_dup(mp, &rest, pass) == NULL)) { return NXT_ERROR; @@ -1473,13 +1373,13 @@ nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass, nxt_str_t *segments, } segments->length = p - rest.start; - segments->start = rest.start; + segments->start = rest.start; rest.length -= segments->length + 1; - rest.start = p + 1; + rest.start = p + 1; } else { - n = 0; + n = 0; *segments = rest; } @@ -1500,19 +1400,17 @@ nxt_http_pass_segments(nxt_mp_t *mp, nxt_str_t *pass, nxt_str_t *segments, return NXT_OK; } - static nxt_int_t nxt_http_route_find(nxt_http_routes_t *routes, nxt_str_t *name, - nxt_http_action_t *action) -{ - nxt_http_route_t **route, **end; + nxt_http_action_t *action) { + nxt_http_route_t **route, **end; if (routes == NULL) { return NXT_DECLINED; } route = &routes->route[0]; - end = route + routes->items; + end = route + routes->items; while (route < end) { if (nxt_strstr_eq(&(*route)->name, name)) { @@ -1528,18 +1426,16 @@ nxt_http_route_find(nxt_http_routes_t *routes, nxt_str_t *name, return NXT_DECLINED; } - nxt_http_action_t * nxt_http_action_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_str_t *pass) -{ - nxt_mp_t *mp; + nxt_str_t *pass) { + nxt_mp_t *mp; nxt_int_t ret; - nxt_router_conf_t *rtcf; - nxt_http_action_t *action; + nxt_router_conf_t *rtcf; + nxt_http_action_t *action; rtcf = tmcf->router_conf; - mp = rtcf->mem_pool; + mp = rtcf->mem_pool; action = nxt_mp_zalloc(mp, sizeof(nxt_http_action_t)); if (nxt_slow_path(action == NULL)) { @@ -1561,14 +1457,12 @@ nxt_http_action_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return action; } - /* COMPATIBILITY: listener application. */ nxt_http_action_t * nxt_http_pass_application(nxt_task_t *task, nxt_router_conf_t *rtcf, - nxt_str_t *name) -{ - nxt_http_action_t *action; + nxt_str_t *name) { + nxt_http_action_t *action; action = nxt_mp_zalloc(rtcf->mem_pool, sizeof(nxt_http_action_t)); if (nxt_slow_path(action == NULL)) { @@ -1580,14 +1474,12 @@ nxt_http_pass_application(nxt_task_t *task, nxt_router_conf_t *rtcf, return action; } - static nxt_http_action_t * nxt_http_route_handler(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *start) -{ - size_t i; - nxt_http_route_t *route; - nxt_http_action_t *action; + nxt_http_action_t *start) { + size_t i; + nxt_http_route_t *route; + nxt_http_action_t *action; route = start->u.route; @@ -1596,7 +1488,7 @@ nxt_http_route_handler(nxt_task_t *task, nxt_http_request_t *r, if (nxt_slow_path(r->log_route)) { uint32_t lvl = (action == NULL) ? NXT_LOG_INFO : NXT_LOG_NOTICE; - const char *sel = (action == NULL) ? "discarded" : "selected"; + const char *sel = (action == NULL) ? "discarded" : "selected"; if (route->name.length == 0) { nxt_log(task, lvl, "\"routes/%z\" %s", i, sel); @@ -1606,7 +1498,6 @@ nxt_http_route_handler(nxt_task_t *task, nxt_http_request_t *r, } if (action != NULL) { - if (action != NXT_HTTP_ACTION_ERROR) { r->action = action; } @@ -1620,13 +1511,11 @@ nxt_http_route_handler(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static nxt_http_action_t * nxt_http_route_match(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_route_match_t *match) -{ + nxt_http_route_match_t *match) { nxt_int_t ret; - nxt_http_route_test_t *test, *end; + nxt_http_route_test_t *test, *end; ret = nxt_http_cond_value(task, r, &match->condition); if (ret <= 0) { @@ -1635,7 +1524,7 @@ nxt_http_route_match(nxt_task_t *task, nxt_http_request_t *r, } test = &match->test[0]; - end = test + match->items; + end = test + match->items; while (test < end) { switch (test->rule->object) { @@ -1668,16 +1557,14 @@ nxt_http_route_match(nxt_task_t *task, nxt_http_request_t *r, return &match->action; } - static nxt_int_t -nxt_http_route_table(nxt_http_request_t *r, nxt_http_route_table_t *table) -{ - nxt_int_t ret; - nxt_http_route_ruleset_t **ruleset, **end; +nxt_http_route_table(nxt_http_request_t *r, nxt_http_route_table_t *table) { + nxt_int_t ret; + nxt_http_route_ruleset_t **ruleset, **end; - ret = 1; + ret = 1; ruleset = &table->ruleset[0]; - end = ruleset + table->items; + end = ruleset + table->items; while (ruleset < end) { ret = nxt_http_route_ruleset(r, *ruleset); @@ -1692,15 +1579,14 @@ nxt_http_route_table(nxt_http_request_t *r, nxt_http_route_table_t *table) return ret; } - static nxt_int_t -nxt_http_route_ruleset(nxt_http_request_t *r, nxt_http_route_ruleset_t *ruleset) -{ - nxt_int_t ret; - nxt_http_route_rule_t **rule, **end; +nxt_http_route_ruleset(nxt_http_request_t *r, + nxt_http_route_ruleset_t *ruleset) { + nxt_int_t ret; + nxt_http_route_rule_t **rule, **end; rule = &ruleset->rule[0]; - end = rule + ruleset->items; + end = rule + ruleset->items; while (rule < end) { ret = nxt_http_route_rule(r, *rule); @@ -1715,17 +1601,14 @@ nxt_http_route_ruleset(nxt_http_request_t *r, nxt_http_route_ruleset_t *ruleset) return 1; } - static nxt_int_t -nxt_http_route_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ - void *p, **pp; - u_char *start; +nxt_http_route_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { + void *p, **pp; + u_char *start; size_t length; - nxt_str_t *s; + nxt_str_t *s; switch (rule->object) { - case NXT_HTTP_ROUTE_HEADER: return nxt_http_route_header(r, rule); @@ -1753,7 +1636,7 @@ nxt_http_route_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule) } else { /* NXT_HTTP_ROUTE_STRING_PTR */ pp = p; - s = *pp; + s = *pp; if (s == NULL) { return 0; @@ -1761,40 +1644,37 @@ nxt_http_route_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule) } length = s->length; - start = s->start; + start = s->start; return nxt_http_route_test_rule(r, rule, start, length); } - static nxt_int_t nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p, - nxt_sockaddr_t *sa) -{ + nxt_sockaddr_t *sa) { #if (NXT_INET6) - uint32_t i; + uint32_t i; #endif - in_port_t in_port; - nxt_int_t match; - struct sockaddr_in *sin; + in_port_t in_port; + nxt_int_t match; + struct sockaddr_in *sin; #if (NXT_INET6) - struct sockaddr_in6 *sin6; + struct sockaddr_in6 *sin6; #endif - nxt_http_route_addr_base_t *base; + nxt_http_route_addr_base_t *base; base = &p->base; switch (sa->u.sockaddr.sa_family) { - case AF_INET: - match = (base->addr_family == AF_INET - || base->addr_family == AF_UNSPEC); + match + = (base->addr_family == AF_INET || base->addr_family == AF_UNSPEC); if (!match) { break; } - sin = &sa->u.sockaddr_in; + sin = &sa->u.sockaddr_in; in_port = ntohs(sin->sin_port); match = (in_port >= base->port.start && in_port <= base->port.end); @@ -1803,26 +1683,27 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p, } switch (base->match_type) { - case NXT_HTTP_ROUTE_ADDR_ANY: break; case NXT_HTTP_ROUTE_ADDR_EXACT: match = (memcmp(&sin->sin_addr, &p->addr.v4.start, - sizeof(struct in_addr)) + sizeof(struct in_addr)) == 0); break; case NXT_HTTP_ROUTE_ADDR_RANGE: match = (memcmp(&sin->sin_addr, &p->addr.v4.start, - sizeof(struct in_addr)) >= 0 + sizeof(struct in_addr)) + >= 0 && memcmp(&sin->sin_addr, &p->addr.v4.end, - sizeof(struct in_addr)) <= 0); + sizeof(struct in_addr)) + <= 0); break; case NXT_HTTP_ROUTE_ADDR_CIDR: - match = ((sin->sin_addr.s_addr & p->addr.v4.end) - == p->addr.v4.start); + match + = ((sin->sin_addr.s_addr & p->addr.v4.end) == p->addr.v4.start); break; default: @@ -1834,13 +1715,13 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p, #if (NXT_INET6) case AF_INET6: - match = (base->addr_family == AF_INET6 - || base->addr_family == AF_UNSPEC); + match + = (base->addr_family == AF_INET6 || base->addr_family == AF_UNSPEC); if (!match) { break; } - sin6 = &sa->u.sockaddr_in6; + sin6 = &sa->u.sockaddr_in6; in_port = ntohs(sin6->sin6_port); match = (in_port >= base->port.start && in_port <= base->port.end); @@ -1849,28 +1730,29 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p, } switch (base->match_type) { - case NXT_HTTP_ROUTE_ADDR_ANY: break; case NXT_HTTP_ROUTE_ADDR_EXACT: match = (memcmp(&sin6->sin6_addr, &p->addr.v6.start, - sizeof(struct in6_addr)) + sizeof(struct in6_addr)) == 0); break; case NXT_HTTP_ROUTE_ADDR_RANGE: match = (memcmp(&sin6->sin6_addr, &p->addr.v6.start, - sizeof(struct in6_addr)) >= 0 + sizeof(struct in6_addr)) + >= 0 && memcmp(&sin6->sin6_addr, &p->addr.v6.end, - sizeof(struct in6_addr)) <= 0); + sizeof(struct in6_addr)) + <= 0); break; case NXT_HTTP_ROUTE_ADDR_CIDR: for (i = 0; i < 16; i++) { - match = ((sin6->sin6_addr.s6_addr[i] - & p->addr.v6.end.s6_addr[i]) - == p->addr.v6.start.s6_addr[i]); + match + = ((sin6->sin6_addr.s6_addr[i] & p->addr.v6.end.s6_addr[i]) + == p->addr.v6.start.s6_addr[i]); if (!match) { break; @@ -1901,14 +1783,12 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p, return match ^ base->negative; } - nxt_int_t nxt_http_route_addr_rule(nxt_http_request_t *r, - nxt_http_route_addr_rule_t *addr_rule, nxt_sockaddr_t *sa) -{ + nxt_http_route_addr_rule_t *addr_rule, nxt_sockaddr_t *sa) { uint32_t n; nxt_bool_t matches; - nxt_http_route_addr_pattern_t *p; + nxt_http_route_addr_pattern_t *p; n = addr_rule->items; @@ -1941,22 +1821,18 @@ nxt_http_route_addr_rule(nxt_http_request_t *r, return p->base.negative; } - static nxt_int_t -nxt_http_route_header(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ +nxt_http_route_header(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { nxt_int_t ret; - nxt_http_field_t *f; + nxt_http_field_t *f; ret = 0; nxt_list_each(f, r->fields) { - if (rule->u.name.hash != f->hash || rule->u.name.length != f->name_length || nxt_strncasecmp(rule->u.name.start, f->name, f->name_length) - != 0) - { + != 0) { continue; } @@ -1968,17 +1844,15 @@ nxt_http_route_header(nxt_http_request_t *r, nxt_http_route_rule_t *rule) if (ret == 0) { return ret; } - - } nxt_list_loop; + } + nxt_list_loop; return ret; } - static nxt_int_t -nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ - nxt_array_t *arguments; +nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { + nxt_array_t *arguments; arguments = nxt_http_arguments_parse(r); if (nxt_slow_path(arguments == NULL)) { @@ -1988,27 +1862,23 @@ nxt_http_route_arguments(nxt_http_request_t *r, nxt_http_route_rule_t *rule) return nxt_http_route_test_argument(r, rule, arguments); } - static nxt_int_t -nxt_http_route_test_argument(nxt_http_request_t *r, - nxt_http_route_rule_t *rule, nxt_array_t *array) -{ +nxt_http_route_test_argument(nxt_http_request_t *r, nxt_http_route_rule_t *rule, + nxt_array_t *array) { nxt_int_t ret; - nxt_http_name_value_t *nv, *end; + nxt_http_name_value_t *nv, *end; ret = 0; - nv = array->elts; + nv = array->elts; end = nv + array->nelts; while (nv < end) { - if (rule->u.name.hash == nv->hash && rule->u.name.length == nv->name_length - && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) - { + && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) { ret = nxt_http_route_test_rule(r, rule, nv->value, - nv->value_length); + nv->value_length); if (nxt_slow_path(ret == NXT_ERROR)) { return NXT_ERROR; } @@ -2024,24 +1894,20 @@ nxt_http_route_test_argument(nxt_http_request_t *r, return ret; } - static nxt_int_t -nxt_http_route_scheme(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ +nxt_http_route_scheme(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { nxt_bool_t https; - nxt_http_route_pattern_slice_t *pattern_slice; + nxt_http_route_pattern_slice_t *pattern_slice; pattern_slice = rule->pattern[0].u.pattern_slices->elts; - https = (pattern_slice->length == nxt_length("https")); + https = (pattern_slice->length == nxt_length("https")); return (r->tls == https); } - static nxt_int_t -nxt_http_route_query(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ - nxt_array_t *arguments; +nxt_http_route_query(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { + nxt_array_t *arguments; arguments = nxt_http_arguments_parse(r); if (nxt_slow_path(arguments == NULL)) { @@ -2049,14 +1915,12 @@ nxt_http_route_query(nxt_http_request_t *r, nxt_http_route_rule_t *rule) } return nxt_http_route_test_rule(r, rule, r->args_decoded.start, - r->args_decoded.length); + r->args_decoded.length); } - static nxt_int_t -nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule) -{ - nxt_array_t *cookies; +nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule) { + nxt_array_t *cookies; cookies = nxt_http_cookies_parse(r); if (nxt_slow_path(cookies == NULL)) { @@ -2066,27 +1930,23 @@ nxt_http_route_cookies(nxt_http_request_t *r, nxt_http_route_rule_t *rule) return nxt_http_route_test_cookie(r, rule, cookies); } - static nxt_int_t -nxt_http_route_test_cookie(nxt_http_request_t *r, - nxt_http_route_rule_t *rule, nxt_array_t *array) -{ +nxt_http_route_test_cookie(nxt_http_request_t *r, nxt_http_route_rule_t *rule, + nxt_array_t *array) { nxt_int_t ret; - nxt_http_name_value_t *nv, *end; + nxt_http_name_value_t *nv, *end; ret = 0; - nv = array->elts; + nv = array->elts; end = nv + array->nelts; while (nv < end) { - if (rule->u.name.hash == nv->hash && rule->u.name.length == nv->name_length - && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) - { + && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) { ret = nxt_http_route_test_rule(r, rule, nv->value, - nv->value_length); + nv->value_length); if (nxt_slow_path(ret == NXT_ERROR)) { return NXT_ERROR; } @@ -2102,17 +1962,15 @@ nxt_http_route_test_cookie(nxt_http_request_t *r, return ret; } - nxt_int_t nxt_http_route_test_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule, - u_char *start, size_t length) -{ + u_char *start, size_t length) { nxt_int_t ret; - nxt_http_route_pattern_t *pattern, *end; + nxt_http_route_pattern_t *pattern, *end; - ret = 1; + ret = 1; pattern = &rule->pattern[0]; - end = pattern + rule->items; + end = pattern + rule->items; while (pattern < end) { ret = nxt_http_route_pattern(r, pattern, start, length); @@ -2133,16 +1991,14 @@ nxt_http_route_test_rule(nxt_http_request_t *r, nxt_http_route_rule_t *rule, return ret; } - static nxt_int_t nxt_http_route_pattern(nxt_http_request_t *r, nxt_http_route_pattern_t *pattern, - u_char *start, size_t length) -{ - u_char *p, *end, *test; + u_char *start, size_t length) { + u_char *p, *end, *test; size_t test_length; uint32_t i; - nxt_array_t *pattern_slices; - nxt_http_route_pattern_slice_t *pattern_slice; + nxt_array_t *pattern_slices; + nxt_http_route_pattern_slice_t *pattern_slice; #if (NXT_HAVE_REGEX) if (pattern->regex) { @@ -2168,23 +2024,22 @@ nxt_http_route_pattern(nxt_http_request_t *r, nxt_http_route_pattern_t *pattern, nxt_assert(pattern->u.pattern_slices != NULL); pattern_slices = pattern->u.pattern_slices; - pattern_slice = pattern_slices->elts; - end = start + length; + pattern_slice = pattern_slices->elts; + end = start + length; for (i = 0; i < pattern_slices->nelts; i++, pattern_slice++) { - test = pattern_slice->start; + test = pattern_slice->start; test_length = pattern_slice->length; switch (pattern_slice->type) { case NXT_HTTP_ROUTE_PATTERN_EXACT: - return ((length == pattern->min_length) && - nxt_http_route_memcmp(start, test, test_length, - pattern->case_sensitive)); + return ((length == pattern->min_length) + && nxt_http_route_memcmp(start, test, test_length, + pattern->case_sensitive)); case NXT_HTTP_ROUTE_PATTERN_BEGIN: if (nxt_http_route_memcmp(start, test, test_length, - pattern->case_sensitive)) - { + pattern->case_sensitive)) { start += test_length; break; } @@ -2195,8 +2050,7 @@ nxt_http_route_pattern(nxt_http_request_t *r, nxt_http_route_pattern_t *pattern, p = end - test_length; if (nxt_http_route_memcmp(p, test, test_length, - pattern->case_sensitive)) - { + pattern->case_sensitive)) { end = p; break; } @@ -2222,12 +2076,10 @@ nxt_http_route_pattern(nxt_http_request_t *r, nxt_http_route_pattern_t *pattern, return 1; } - static nxt_int_t nxt_http_route_memcmp(u_char *start, u_char *test, size_t test_length, - nxt_bool_t case_sensitive) -{ - nxt_int_t n; + nxt_bool_t case_sensitive) { + nxt_int_t n; if (case_sensitive) { n = memcmp(start, test, test_length); diff --git a/src/nxt_http_route_addr.c b/src/nxt_http_route_addr.c index 5a0d7679d..b8e8b21a9 100644 --- a/src/nxt_http_route_addr.c +++ b/src/nxt_http_route_addr.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Axel Duch * Copyright (C) NGINX, Inc. @@ -9,19 +8,19 @@ #if (NXT_INET6) -static nxt_bool_t nxt_valid_ipv6_blocks(u_char *c, size_t len); +static nxt_bool_t +nxt_valid_ipv6_blocks(u_char *c, size_t len); #endif nxt_int_t nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, - nxt_http_route_addr_pattern_t *pattern, nxt_conf_value_t *cv) -{ - u_char *delim; + nxt_http_route_addr_pattern_t *pattern, nxt_conf_value_t *cv) { + u_char *delim; nxt_int_t ret, cidr_prefix; nxt_str_t addr, port; - nxt_http_route_addr_base_t *base; - nxt_http_route_addr_range_t *inet; + nxt_http_route_addr_base_t *base; + nxt_http_route_addr_range_t *inet; if (nxt_conf_type(cv) != NXT_CONF_STRING) { return NXT_ADDR_PATTERN_CV_TYPE_ERROR; @@ -58,20 +57,20 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, nxt_str_null(&port); if (addr.start[0] == '*' && addr.start[1] == ':') { - port.start = addr.start + 2; - port.length = addr.length - 2; + port.start = addr.start + 2; + port.length = addr.length - 2; base->addr_family = AF_UNSPEC; - base->match_type = NXT_HTTP_ROUTE_ADDR_ANY; + base->match_type = NXT_HTTP_ROUTE_ADDR_ANY; goto parse_port; } if (nxt_inet6_probe(&addr)) { #if (NXT_INET6) - u_char *end; + u_char *end; uint8_t i; nxt_int_t len; - nxt_http_route_in6_addr_range_t *inet6; + nxt_http_route_in6_addr_range_t *inet6; base->addr_family = AF_INET6; @@ -86,9 +85,9 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, return NXT_ADDR_PATTERN_FORMAT_ERROR; } - addr.length = port.start - addr.start; - port.start += nxt_length("]:"); - port.length = end - port.start; + addr.length = port.start - addr.start; + port.start += nxt_length("]:"); + port.length = end - port.start; } inet6 = &pattern->addr.v6; @@ -115,9 +114,9 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, return NXT_ADDR_PATTERN_FORMAT_ERROR; } - if (nxt_slow_path(memcmp(&inet6->start, &inet6->end, - sizeof(struct in6_addr)) > 0)) - { + if (nxt_slow_path( + memcmp(&inet6->start, &inet6->end, sizeof(struct in6_addr)) + > 0)) { return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR; } @@ -129,15 +128,14 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, delim = memchr(addr.start, '/', addr.length); if (delim != NULL) { cidr_prefix = nxt_int_parse(delim + 1, - addr.start + addr.length - (delim + 1)); + addr.start + addr.length - (delim + 1)); if (nxt_slow_path(cidr_prefix < 0 || cidr_prefix > 128)) { return NXT_ADDR_PATTERN_CIDR_ERROR; } addr.length = delim - addr.start; - if (nxt_slow_path(!nxt_valid_ipv6_blocks(addr.start, - addr.length))) - { + if (nxt_slow_path( + !nxt_valid_ipv6_blocks(addr.start, addr.length))) { return NXT_ADDR_PATTERN_FORMAT_ERROR; } @@ -162,8 +160,8 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, for (i = 0; i < sizeof(struct in6_addr); i++) { if (cidr_prefix >= 8) { - inet6->end.s6_addr[i] = 0xFF; - cidr_prefix -= 8; + inet6->end.s6_addr[i] = 0xFF; + cidr_prefix -= 8; continue; } @@ -171,13 +169,13 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, if (cidr_prefix > 0) { inet6->end.s6_addr[i] = 0xFF & (0xFF << (8 - cidr_prefix)); inet6->start.s6_addr[i] &= inet6->end.s6_addr[i]; - cidr_prefix = 0; + cidr_prefix = 0; continue; } inet6->start.s6_addr[i] = 0; - inet6->end.s6_addr[i] = 0; + inet6->end.s6_addr[i] = 0; } goto parse_port; @@ -203,7 +201,7 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, delim = memchr(addr.start, ':', addr.length); if (delim != NULL) { - port.start = delim + 1; + port.start = delim + 1; port.length = addr.start + addr.length - port.start; addr.length = delim - addr.start; } @@ -217,15 +215,14 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, return NXT_ADDR_PATTERN_FORMAT_ERROR; } - inet->end = nxt_inet_addr(delim + 1, - addr.start + addr.length - (delim + 1)); + inet->end + = nxt_inet_addr(delim + 1, addr.start + addr.length - (delim + 1)); if (nxt_slow_path(inet->end == INADDR_NONE)) { return NXT_ADDR_PATTERN_FORMAT_ERROR; } - if (nxt_slow_path(memcmp(&inet->start, &inet->end, - sizeof(struct in_addr)) > 0)) - { + if (nxt_slow_path( + memcmp(&inet->start, &inet->end, sizeof(struct in_addr)) > 0)) { return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR; } @@ -236,14 +233,14 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, delim = memchr(addr.start, '/', addr.length); if (delim != NULL) { - cidr_prefix = nxt_int_parse(delim + 1, - addr.start + addr.length - (delim + 1)); + cidr_prefix + = nxt_int_parse(delim + 1, addr.start + addr.length - (delim + 1)); if (nxt_slow_path(cidr_prefix < 0 || cidr_prefix > 32)) { return NXT_ADDR_PATTERN_CIDR_ERROR; } addr.length = delim - addr.start; - inet->end = htonl(0xFFFFFFFF & (0xFFFFFFFFULL << (32 - cidr_prefix))); + inet->end = htonl(0xFFFFFFFF & (0xFFFFFFFFULL << (32 - cidr_prefix))); inet->start = nxt_inet_addr(addr.start, addr.length) & inet->end; if (nxt_slow_path(inet->start == INADDR_NONE)) { @@ -278,7 +275,7 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, } base->port.start = 0; - base->port.end = 65535; + base->port.end = 65535; return NXT_OK; } @@ -306,7 +303,7 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, } base->port.start = ret; - base->port.end = ret; + base->port.end = ret; } return NXT_OK; @@ -316,12 +313,11 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, #if (NXT_INET6) static nxt_bool_t -nxt_valid_ipv6_blocks(u_char *c, size_t len) -{ - u_char *end; - nxt_uint_t colon_gap; +nxt_valid_ipv6_blocks(u_char *c, size_t len) { + u_char *end; + nxt_uint_t colon_gap; - end = c + len; + end = c + len; colon_gap = 0; while (c != end) { diff --git a/src/nxt_http_route_addr.h b/src/nxt_http_route_addr.h index 2deda6f80..7b067af3e 100644 --- a/src/nxt_http_route_addr.h +++ b/src/nxt_http_route_addr.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Axel Duch * Copyright (C) NGINX, Inc. @@ -9,7 +8,6 @@ #ifndef _NXT_HTTP_ROUTE_ADDR_H_INCLUDED_ #define _NXT_HTTP_ROUTE_ADDR_H_INCLUDED_ - enum { NXT_HTTP_ROUTE_ADDR_ANY = 0, NXT_HTTP_ROUTE_ADDR_RANGE, @@ -17,7 +15,6 @@ enum { NXT_HTTP_ROUTE_ADDR_CIDR, }; - enum { NXT_ADDR_PATTERN_PORT_ERROR = NXT_OK + 1, NXT_ADDR_PATTERN_CV_TYPE_ERROR, @@ -29,46 +26,44 @@ enum { NXT_ADDR_PATTERN_NO_UNIX_ERROR, }; - typedef struct { - in_addr_t start; - in_addr_t end; + in_addr_t start; + in_addr_t end; } nxt_http_route_addr_range_t; #if (NXT_INET6) typedef struct { - struct in6_addr start; - struct in6_addr end; + struct in6_addr start; + struct in6_addr end; } nxt_http_route_in6_addr_range_t; #endif typedef struct { - uint8_t match_type:2; - uint8_t negative:1; - uint8_t addr_family; + uint8_t match_type:2; + uint8_t negative :1; + uint8_t addr_family; struct { - uint16_t start; - uint16_t end; + uint16_t start; + uint16_t end; } port; } nxt_http_route_addr_base_t; - typedef struct { - nxt_http_route_addr_base_t base; + nxt_http_route_addr_base_t base; union { - nxt_http_route_addr_range_t v4; + nxt_http_route_addr_range_t v4; #if (NXT_INET6) - nxt_http_route_in6_addr_range_t v6; + nxt_http_route_in6_addr_range_t v6; #endif } addr; } nxt_http_route_addr_pattern_t; - -NXT_EXPORT nxt_int_t nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, +NXT_EXPORT nxt_int_t +nxt_http_route_addr_pattern_parse(nxt_mp_t *mp, nxt_http_route_addr_pattern_t *pattern, nxt_conf_value_t *cv); #endif /* _NXT_HTTP_ROUTE_ADDR_H_INCLUDED_ */ diff --git a/src/nxt_http_set_headers.c b/src/nxt_http_set_headers.c index 7fd6aba52..ac368278b 100644 --- a/src/nxt_http_set_headers.c +++ b/src/nxt_http_set_headers.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Zhidao HONG * Copyright (C) NGINX, Inc. @@ -7,25 +6,22 @@ #include #include - typedef struct { - nxt_str_t name; - nxt_tstr_t *value; + nxt_str_t name; + nxt_tstr_t *value; } nxt_http_header_val_t; - nxt_int_t nxt_http_set_headers_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, - nxt_http_action_conf_t *acf) - { + nxt_http_action_conf_t *acf) { uint32_t next; nxt_str_t str, name; - nxt_array_t *headers; - nxt_conf_value_t *value; - nxt_http_header_val_t *hv; + nxt_array_t *headers; + nxt_conf_value_t *value; + nxt_http_header_val_t *hv; - headers = nxt_array_create(rtcf->mem_pool, 4, - sizeof(nxt_http_header_val_t)); + headers + = nxt_array_create(rtcf->mem_pool, 4, sizeof(nxt_http_header_val_t)); if (nxt_slow_path(headers == NULL)) { return NXT_ERROR; } @@ -34,7 +30,7 @@ nxt_http_set_headers_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, next = 0; - for ( ;; ) { + for (;;) { value = nxt_conf_next_object_member(acf->set_headers, &name, &next); if (value == NULL) { break; @@ -67,40 +63,34 @@ nxt_http_set_headers_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action, return NXT_OK; } - static nxt_http_field_t * -nxt_http_resp_header_find(nxt_http_request_t *r, u_char *name, size_t length) -{ - nxt_http_field_t *f; +nxt_http_resp_header_find(nxt_http_request_t *r, u_char *name, size_t length) { + nxt_http_field_t *f; nxt_list_each(f, r->resp.fields) { - if (f->skip) { continue; } if (length == f->name_length - && nxt_memcasecmp(name, f->name, f->name_length) == 0) - { + && nxt_memcasecmp(name, f->name, f->name_length) == 0) { return f; } - - } nxt_list_loop; + } + nxt_list_loop; return NULL; } - nxt_int_t -nxt_http_set_headers(nxt_http_request_t *r) -{ +nxt_http_set_headers(nxt_http_request_t *r) { nxt_int_t ret; nxt_uint_t i, n; - nxt_str_t *value; - nxt_http_field_t *f; - nxt_router_conf_t *rtcf; - nxt_http_action_t *action; - nxt_http_header_val_t *hv, *header; + nxt_str_t *value; + nxt_http_field_t *f; + nxt_router_conf_t *rtcf; + nxt_http_action_t *action; + nxt_http_header_val_t *hv, *header; action = r->action; @@ -115,7 +105,7 @@ nxt_http_set_headers(nxt_http_request_t *r) rtcf = r->conf->socket_conf->router_conf; header = action->set_headers->elts; - n = action->set_headers->nelts; + n = action->set_headers->nelts; value = nxt_mp_zalloc(r->mem_pool, sizeof(nxt_str_t) * n); if (nxt_slow_path(value == NULL)) { @@ -134,7 +124,7 @@ nxt_http_set_headers(nxt_http_request_t *r) } else { ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -152,18 +142,17 @@ nxt_http_set_headers(nxt_http_request_t *r) f = nxt_http_resp_header_find(r, hv->name.start, hv->name.length); if (value[i].start != NULL) { - if (f == NULL) { f = nxt_list_zero_add(r->resp.fields); if (nxt_slow_path(f == NULL)) { return NXT_ERROR; } - f->name = hv->name.start; + f->name = hv->name.start; f->name_length = hv->name.length; } - f->value = value[i].start; + f->value = value[i].start; f->value_length = value[i].length; } else if (f != NULL) { diff --git a/src/nxt_http_static.c b/src/nxt_http_static.c index 67591595a..6eebf427f 100644 --- a/src/nxt_http_static.c +++ b/src/nxt_http_static.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,84 +5,85 @@ #include #include - typedef struct { - nxt_tstr_t *tstr; + nxt_tstr_t *tstr; #if (NXT_HAVE_OPENAT2) - u_char *fname; + u_char *fname; #endif - uint8_t is_const; /* 1 bit */ + uint8_t is_const; /* 1 bit */ } nxt_http_static_share_t; - typedef struct { - nxt_uint_t nshares; - nxt_http_static_share_t *shares; - nxt_str_t index; + nxt_uint_t nshares; + nxt_http_static_share_t *shares; + nxt_str_t index; #if (NXT_HAVE_OPENAT2) - nxt_tstr_t *chroot; - nxt_uint_t resolve; + nxt_tstr_t *chroot; + nxt_uint_t resolve; #endif - nxt_http_route_rule_t *types; + nxt_http_route_rule_t *types; } nxt_http_static_conf_t; - typedef struct { - nxt_http_action_t *action; - nxt_str_t share; + nxt_http_action_t *action; + nxt_str_t share; #if (NXT_HAVE_OPENAT2) - nxt_str_t chroot; + nxt_str_t chroot; #endif - uint32_t share_idx; - uint8_t need_body; /* 1 bit */ + uint32_t share_idx; + uint8_t need_body; /* 1 bit */ } nxt_http_static_ctx_t; - -#define NXT_HTTP_STATIC_BUF_COUNT 2 -#define NXT_HTTP_STATIC_BUF_SIZE (128 * 1024) +#define NXT_HTTP_STATIC_BUF_COUNT 2 +#define NXT_HTTP_STATIC_BUF_SIZE (128 * 1024) -static nxt_http_action_t *nxt_http_static(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); -static void nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, +static nxt_http_action_t * +nxt_http_static(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); +static void +nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, nxt_http_static_ctx_t *ctx); -static void nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, +static void +nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_static_ctx_t *ctx); -static void nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r, +static void +nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r, nxt_http_static_ctx_t *ctx, nxt_http_status_t status); #if (NXT_HAVE_OPENAT2) -static u_char *nxt_http_static_chroot_match(u_char *chr, u_char *shr); +static u_char * +nxt_http_static_chroot_match(u_char *chr, u_char *shr); #endif -static void nxt_http_static_extract_extension(nxt_str_t *path, - nxt_str_t *exten); -static void nxt_http_static_body_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_http_static_buf_completion(nxt_task_t *task, void *obj, - void *data); - -static nxt_int_t nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq, - void *data); -static void *nxt_http_static_mtypes_hash_alloc(void *data, size_t size); -static void nxt_http_static_mtypes_hash_free(void *data, void *p); +static void +nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten); +static void +nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static void * +nxt_http_static_mtypes_hash_alloc(void *data, size_t size); +static void +nxt_http_static_mtypes_hash_free(void *data, void *p); -static const nxt_http_request_state_t nxt_http_static_send_state; +static const nxt_http_request_state_t nxt_http_static_send_state; nxt_int_t nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_http_action_t *action, nxt_http_action_conf_t *acf) -{ + nxt_http_action_t *action, nxt_http_action_conf_t *acf) { uint32_t i; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_str_t str, *ret; - nxt_tstr_t *tstr; - nxt_conf_value_t *cv; - nxt_router_conf_t *rtcf; - nxt_http_static_conf_t *conf; + nxt_tstr_t *tstr; + nxt_conf_value_t *cv; + nxt_router_conf_t *rtcf; + nxt_http_static_conf_t *conf; rtcf = tmcf->router_conf; - mp = rtcf->mem_pool; + mp = rtcf->mem_pool; conf = nxt_mp_zget(mp, sizeof(nxt_http_static_conf_t)); if (nxt_slow_path(conf == NULL)) { @@ -91,11 +91,11 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } action->handler = nxt_http_static; - action->u.conf = conf; + action->u.conf = conf; conf->nshares = nxt_conf_array_elements_count_or_1(acf->share); - conf->shares = nxt_mp_zget(mp, sizeof(nxt_http_static_share_t) - * conf->nshares); + conf->shares + = nxt_mp_zget(mp, sizeof(nxt_http_static_share_t) * conf->nshares); if (nxt_slow_path(conf->shares == NULL)) { return NXT_ERROR; } @@ -109,7 +109,7 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_ERROR; } - conf->shares[i].tstr = tstr; + conf->shares[i].tstr = tstr; conf->shares[i].is_const = nxt_tstr_is_const(tstr); } @@ -125,11 +125,11 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, #if (NXT_HAVE_OPENAT2) if (acf->chroot.length > 0) { - nxt_str_t chr, shr; - nxt_bool_t is_const; + nxt_str_t chr, shr; + nxt_bool_t is_const; - conf->chroot = nxt_tstr_compile(rtcf->tstr_state, &acf->chroot, - NXT_TSTR_STRZ); + conf->chroot + = nxt_tstr_compile(rtcf->tstr_state, &acf->chroot, NXT_TSTR_STRZ); if (nxt_slow_path(conf->chroot == NULL)) { return NXT_ERROR; } @@ -143,21 +143,19 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_tstr_str(conf->chroot, &chr); nxt_tstr_str(conf->shares[i].tstr, &shr); - conf->shares[i].fname = nxt_http_static_chroot_match(chr.start, - shr.start); + conf->shares[i].fname + = nxt_http_static_chroot_match(chr.start, shr.start); } } } if (acf->follow_symlinks != NULL - && !nxt_conf_get_boolean(acf->follow_symlinks)) - { + && !nxt_conf_get_boolean(acf->follow_symlinks)) { conf->resolve |= RESOLVE_NO_SYMLINKS; } if (acf->traverse_mounts != NULL - && !nxt_conf_get_boolean(acf->traverse_mounts)) - { + && !nxt_conf_get_boolean(acf->traverse_mounts)) { conf->resolve |= RESOLVE_NO_XDEV; } #endif @@ -176,22 +174,19 @@ nxt_http_static_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } return nxt_http_action_init(task, tmcf, acf->fallback, - action->fallback); + action->fallback); } return NXT_OK; } - static nxt_http_action_t * nxt_http_static(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { nxt_bool_t need_body; - nxt_http_static_ctx_t *ctx; + nxt_http_static_ctx_t *ctx; if (nxt_slow_path(!nxt_str_eq(r->method, "GET", 3))) { - if (!nxt_str_eq(r->method, "HEAD", 4)) { if (action->fallback != NULL) { if (nxt_slow_path(r->log_route)) { @@ -216,7 +211,7 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - ctx->action = action; + ctx->action = action; ctx->need_body = need_body; nxt_http_static_iterate(task, r, ctx); @@ -224,29 +219,27 @@ nxt_http_static(nxt_task_t *task, nxt_http_request_t *r, return NULL; } - static void nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_static_ctx_t *ctx) -{ + nxt_http_static_ctx_t *ctx) { nxt_int_t ret; - nxt_router_conf_t *rtcf; - nxt_http_static_conf_t *conf; - nxt_http_static_share_t *share; + nxt_router_conf_t *rtcf; + nxt_http_static_conf_t *conf; + nxt_http_static_share_t *share; conf = ctx->action->u.conf; share = &conf->shares[ctx->share_idx]; #if (NXT_DEBUG) - nxt_str_t shr; - nxt_str_t idx; + nxt_str_t shr; + nxt_str_t idx; nxt_tstr_str(share->tstr, &shr); idx = conf->index; #if (NXT_HAVE_OPENAT2) - nxt_str_t chr; + nxt_str_t chr; if (conf->chroot != NULL) { nxt_tstr_str(conf->chroot, &chr); @@ -255,8 +248,8 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, nxt_str_set(&chr, ""); } - nxt_debug(task, "http static: \"%V\", index: \"%V\" (chroot: \"%V\")", - &shr, &idx, &chr); + nxt_debug(task, "http static: \"%V\", index: \"%V\" (chroot: \"%V\")", &shr, + &idx, &chr); #else nxt_debug(task, "http static: \"%V\", index: \"%V\"", &shr, &idx); #endif @@ -275,7 +268,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, rtcf = r->conf->socket_conf->router_conf; ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } @@ -288,7 +281,7 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, #if (NXT_HAVE_OPENAT2) if (conf->chroot != NULL && ctx->share_idx == 0) { ret = nxt_tstr_query(task, r->tstr_query, conf->chroot, - &ctx->chroot); + &ctx->chroot); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } @@ -305,35 +298,33 @@ nxt_http_static_iterate(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - static void nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_static_ctx_t *ctx) -{ + nxt_http_static_ctx_t *ctx) { size_t length, encode; - u_char *p, *fname; + u_char *p, *fname; struct tm tm; - nxt_buf_t *fb; + nxt_buf_t *fb; nxt_int_t ret; - nxt_str_t *shr, *index, exten, *mtype; + nxt_str_t *shr, *index, exten, *mtype; nxt_uint_t level; - nxt_file_t *f, file; + nxt_file_t *f, file; nxt_file_info_t fi; - nxt_http_field_t *field; + nxt_http_field_t *field; nxt_http_status_t status; - nxt_router_conf_t *rtcf; - nxt_http_action_t *action; + nxt_router_conf_t *rtcf; + nxt_http_action_t *action; nxt_work_handler_t body_handler; - nxt_http_static_conf_t *conf; + nxt_http_static_conf_t *conf; action = ctx->action; - conf = action->u.conf; - rtcf = r->conf->socket_conf->router_conf; + conf = action->u.conf; + rtcf = r->conf->socket_conf->router_conf; - f = NULL; + f = NULL; mtype = NULL; - shr = &ctx->share; + shr = &ctx->share; index = &conf->index; if (shr->start[shr->length - 1] == '/') { @@ -346,9 +337,9 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, goto fail; } - p = fname; - p = nxt_cpymem(p, shr->start, shr->length); - p = nxt_cpymem(p, index->start, index->length); + p = fname; + p = nxt_cpymem(p, shr->start, shr->length); + p = nxt_cpymem(p, index->start, index->length); *p = '\0'; } else { @@ -360,7 +351,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, mtype = nxt_http_static_mtype_get(&rtcf->mtypes_hash, &exten); ret = nxt_http_route_test_rule(r, conf->types, mtype->start, - mtype->length); + mtype->length); if (nxt_slow_path(ret == NXT_ERROR)) { goto fail; } @@ -380,30 +371,30 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, #if (NXT_HAVE_OPENAT2) if (conf->resolve != 0 || ctx->chroot.length > 0) { - nxt_str_t *chr; + nxt_str_t *chr; nxt_uint_t resolve; - nxt_http_static_share_t *share; + nxt_http_static_share_t *share; share = &conf->shares[ctx->share_idx]; resolve = conf->resolve; - chr = &ctx->chroot; + chr = &ctx->chroot; if (chr->length > 0) { resolve |= RESOLVE_IN_ROOT; fname = share->is_const - ? share->fname - : nxt_http_static_chroot_match(chr->start, file.name); + ? share->fname + : nxt_http_static_chroot_match(chr->start, file.name); if (fname != NULL) { file.name = chr->start; ret = nxt_file_open(task, &file, NXT_FILE_SEARCH, NXT_FILE_OPEN, - 0); + 0); } else { file.error = NXT_EACCES; - ret = NXT_ERROR; + ret = NXT_ERROR; } } else if (fname[0] == '/') { @@ -412,19 +403,19 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, } else { file.name = (u_char *) "."; - file.fd = AT_FDCWD; - ret = NXT_OK; + file.fd = AT_FDCWD; + ret = NXT_OK; } if (nxt_fast_path(ret == NXT_OK)) { - nxt_file_t af; + nxt_file_t af; af = file; nxt_memzero(&file, sizeof(nxt_file_t)); file.name = fname; - ret = nxt_file_openat2(task, &file, NXT_FILE_RDONLY, - NXT_FILE_OPEN, 0, af.fd, resolve); + ret = nxt_file_openat2(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, + 0, af.fd, resolve); if (af.fd != AT_FDCWD) { nxt_file_close(task, &af); @@ -440,14 +431,12 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, #endif if (nxt_slow_path(ret != NXT_OK)) { - switch (file.error) { - - /* - * For Unix domain sockets "errno" is set to: - * - ENXIO on Linux; - * - EOPNOTSUPP on *BSD, MacOSX, and Solaris. - */ + /* + * For Unix domain sockets "errno" is set to: + * - ENXIO on Linux; + * - EOPNOTSUPP on *BSD, MacOSX, and Solaris. + */ case NXT_ENOENT: case NXT_ENOTDIR: @@ -457,7 +446,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, #else case NXT_EOPNOTSUPP: #endif - level = NXT_LOG_ERR; + level = NXT_LOG_ERR; status = NXT_HTTP_NOT_FOUND; break; @@ -466,27 +455,27 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, case NXT_ELOOP: case NXT_EXDEV: #endif - level = NXT_LOG_ERR; + level = NXT_LOG_ERR; status = NXT_HTTP_FORBIDDEN; break; default: - level = NXT_LOG_ALERT; + level = NXT_LOG_ALERT; status = NXT_HTTP_INTERNAL_SERVER_ERROR; break; } if (status != NXT_HTTP_NOT_FOUND) { #if (NXT_HAVE_OPENAT2) - nxt_str_t *chr = &ctx->chroot; + nxt_str_t *chr = &ctx->chroot; if (chr->length > 0) { nxt_log(task, level, "opening \"%s\" at \"%V\" failed %E", - fname, chr, file.error); + fname, chr, file.error); } else { - nxt_log(task, level, "opening \"%s\" failed %E", - fname, file.error); + nxt_log(task, level, "opening \"%s\" failed %E", fname, + file.error); } #else @@ -516,7 +505,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, } if (nxt_fast_path(nxt_is_file(&fi))) { - r->status = NXT_HTTP_OK; + r->status = NXT_HTTP_OK; r->resp.content_length_n = nxt_file_size(&fi); field = nxt_list_zero_add(r->resp.fields); @@ -533,7 +522,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, nxt_localtime(nxt_file_mtime(&fi), &tm); - field->value = p; + field->value = p; field->value_length = nxt_http_date(p, &tm) - p; field = nxt_list_zero_add(r->resp.fields); @@ -550,10 +539,9 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, goto fail; } - field->value = p; + field->value = p; field->value_length = nxt_sprintf(p, p + length, "\"%xT-%xO\"", - nxt_file_mtime(&fi), - nxt_file_size(&fi)) + nxt_file_mtime(&fi), nxt_file_size(&fi)) - p; if (exten.start == NULL) { @@ -572,7 +560,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_field_name_set(field, "Content-Type"); - field->value = mtype->start; + field->value = mtype->start; field->value_length = mtype->length; } @@ -582,7 +570,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, goto fail; } - fb->file = f; + fb->file = f; fb->file_end = nxt_file_size(&fi); r->out = fb; @@ -598,11 +586,10 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, /* Not a file. */ nxt_file_close(task, f); - if (nxt_slow_path(!nxt_is_dir(&fi) - || shr->start[shr->length - 1] == '/')) - { + if (nxt_slow_path( + !nxt_is_dir(&fi) || shr->start[shr->length - 1] == '/')) { nxt_log(task, NXT_LOG_ERR, "\"%FN\" is not a regular file", - f->name); + f->name); nxt_http_static_next(task, r, ctx, NXT_HTTP_NOT_FOUND); return; @@ -610,7 +597,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, f = NULL; - r->status = NXT_HTTP_MOVED_PERMANENTLY; + r->status = NXT_HTTP_MOVED_PERMANENTLY; r->resp.content_length_n = 0; field = nxt_list_zero_add(r->resp.fields); @@ -632,7 +619,7 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, goto fail; } - field->value = p; + field->value = p; field->value_length = length; if (encode > 0) { @@ -666,16 +653,14 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r, nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); } - static void nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_static_ctx_t *ctx, nxt_http_status_t status) -{ - nxt_http_action_t *action; - nxt_http_static_conf_t *conf; + nxt_http_static_ctx_t *ctx, nxt_http_status_t status) { + nxt_http_action_t *action; + nxt_http_static_conf_t *conf; action = ctx->action; - conf = action->u.conf; + conf = action->u.conf; ctx->share_idx++; @@ -701,8 +686,7 @@ nxt_http_static_next(nxt_task_t *task, nxt_http_request_t *r, #if (NXT_HAVE_OPENAT2) static u_char * -nxt_http_static_chroot_match(u_char *chr, u_char *shr) -{ +nxt_http_static_chroot_match(u_char *chr, u_char *shr) { if (*chr != *shr) { return NULL; } @@ -710,7 +694,7 @@ nxt_http_static_chroot_match(u_char *chr, u_char *shr) chr++; shr++; - for ( ;; ) { + for (;;) { if (*shr == '\0') { return NULL; } @@ -756,12 +740,11 @@ nxt_http_static_chroot_match(u_char *chr, u_char *shr) static void -nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten) -{ - u_char ch, *p, *end; +nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten) { + u_char ch, *p, *end; end = path->start + path->length; - p = end; + p = end; while (p > path->start) { p--; @@ -779,27 +762,25 @@ nxt_http_static_extract_extension(nxt_str_t *path, nxt_str_t *exten) extension: exten->length = end - p; - exten->start = p; + exten->start = p; } - static void -nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data) { size_t alloc; - nxt_buf_t *fb, *b, **next, *out; + nxt_buf_t *fb, *b, **next, *out; nxt_off_t rest; nxt_int_t n; - nxt_work_queue_t *wq; - nxt_http_request_t *r; + nxt_work_queue_t *wq; + nxt_http_request_t *r; - r = obj; + r = obj; fb = r->out; rest = fb->file_end - fb->file_pos; - out = NULL; + out = NULL; next = &out; - n = 0; + n = 0; do { alloc = nxt_min(rest, NXT_HTTP_STATIC_BUF_SIZE); @@ -810,12 +791,12 @@ nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data) } b->completion_handler = nxt_http_static_buf_completion; - b->parent = r; + b->parent = r; nxt_mp_retain(r->mem_pool); *next = b; - next = &b->next; + next = &b->next; rest -= alloc; @@ -829,7 +810,7 @@ nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data) fail: while (out != NULL) { - b = out; + b = out; out = b->next; nxt_mp_free(r->mem_pool, b); @@ -837,21 +818,17 @@ nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data) } } - -static const nxt_http_request_state_t nxt_http_static_send_state - nxt_aligned(64) = -{ - .error_handler = nxt_http_request_error_handler, +static const nxt_http_request_state_t nxt_http_static_send_state nxt_aligned(64) + = { + .error_handler = nxt_http_request_error_handler, }; - static void -nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data) { ssize_t n, size; - nxt_buf_t *b, *fb, *next; + nxt_buf_t *b, *fb, *next; nxt_off_t rest; - nxt_http_request_t *r; + nxt_http_request_t *r; b = obj; r = data; @@ -886,10 +863,10 @@ nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data) } else { fb->file_pos += n; - b->next = NULL; + b->next = NULL; } - b->mem.pos = b->mem.start; + b->mem.pos = b->mem.start; b->mem.free = b->mem.pos + n; nxt_http_request_send(task, r, b); @@ -918,84 +895,82 @@ nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data) } } - nxt_int_t -nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash) -{ - nxt_str_t *type, exten; - nxt_int_t ret; - nxt_uint_t i; +nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash) { + nxt_str_t *type, exten; + nxt_int_t ret; + nxt_uint_t i; static const struct { nxt_str_t type; - const char *exten; + const char *exten; } default_types[] = { - { nxt_string("text/html"), ".html" }, - { nxt_string("text/html"), ".htm" }, - { nxt_string("text/css"), ".css" }, - - { nxt_string("image/svg+xml"), ".svg" }, - { nxt_string("image/webp"), ".webp" }, - { nxt_string("image/png"), ".png" }, - { nxt_string("image/apng"), ".apng" }, - { nxt_string("image/jpeg"), ".jpeg" }, - { nxt_string("image/jpeg"), ".jpg" }, - { nxt_string("image/gif"), ".gif" }, - { nxt_string("image/x-icon"), ".ico" }, - - { nxt_string("image/avif"), ".avif" }, - { nxt_string("image/avif-sequence"), ".avifs" }, - - { nxt_string("font/woff"), ".woff" }, - { nxt_string("font/woff2"), ".woff2" }, - { nxt_string("font/otf"), ".otf" }, - { nxt_string("font/ttf"), ".ttf" }, - - { nxt_string("text/plain"), ".txt" }, - { nxt_string("text/markdown"), ".md" }, - { nxt_string("text/x-rst"), ".rst" }, - - { nxt_string("application/javascript"), ".js" }, - { nxt_string("application/json"), ".json" }, - { nxt_string("application/xml"), ".xml" }, - { nxt_string("application/rss+xml"), ".rss" }, - { nxt_string("application/atom+xml"), ".atom" }, - { nxt_string("application/pdf"), ".pdf" }, - - { nxt_string("application/zip"), ".zip" }, - - { nxt_string("audio/mpeg"), ".mp3" }, - { nxt_string("audio/ogg"), ".ogg" }, - { nxt_string("audio/midi"), ".midi" }, - { nxt_string("audio/midi"), ".mid" }, - { nxt_string("audio/flac"), ".flac" }, - { nxt_string("audio/aac"), ".aac" }, - { nxt_string("audio/wav"), ".wav" }, - - { nxt_string("video/mpeg"), ".mpeg" }, - { nxt_string("video/mpeg"), ".mpg" }, - { nxt_string("video/mp4"), ".mp4" }, - { nxt_string("video/webm"), ".webm" }, - { nxt_string("video/x-msvideo"), ".avi" }, - - { nxt_string("application/octet-stream"), ".exe" }, - { nxt_string("application/octet-stream"), ".bin" }, - { nxt_string("application/octet-stream"), ".dll" }, - { nxt_string("application/octet-stream"), ".iso" }, - { nxt_string("application/octet-stream"), ".img" }, - { nxt_string("application/octet-stream"), ".msi" }, - - { nxt_string("application/octet-stream"), ".deb" }, - { nxt_string("application/octet-stream"), ".rpm" }, - - { nxt_string("application/x-httpd-php"), ".php" }, + {nxt_string("text/html"), ".html"}, + {nxt_string("text/html"), ".htm"}, + {nxt_string("text/css"), ".css"}, + + {nxt_string("image/svg+xml"), ".svg"}, + {nxt_string("image/webp"), ".webp"}, + {nxt_string("image/png"), ".png"}, + {nxt_string("image/apng"), ".apng"}, + {nxt_string("image/jpeg"), ".jpeg"}, + {nxt_string("image/jpeg"), ".jpg"}, + {nxt_string("image/gif"), ".gif"}, + {nxt_string("image/x-icon"), ".ico"}, + + {nxt_string("image/avif"), ".avif"}, + {nxt_string("image/avif-sequence"), ".avifs"}, + + {nxt_string("font/woff"), ".woff"}, + {nxt_string("font/woff2"), ".woff2"}, + {nxt_string("font/otf"), ".otf"}, + {nxt_string("font/ttf"), ".ttf"}, + + {nxt_string("text/plain"), ".txt"}, + {nxt_string("text/markdown"), ".md"}, + {nxt_string("text/x-rst"), ".rst"}, + + {nxt_string("application/javascript"), ".js"}, + {nxt_string("application/json"), ".json"}, + {nxt_string("application/xml"), ".xml"}, + {nxt_string("application/rss+xml"), ".rss"}, + {nxt_string("application/atom+xml"), ".atom"}, + {nxt_string("application/pdf"), ".pdf"}, + + {nxt_string("application/zip"), ".zip"}, + + {nxt_string("audio/mpeg"), ".mp3"}, + {nxt_string("audio/ogg"), ".ogg"}, + {nxt_string("audio/midi"), ".midi"}, + {nxt_string("audio/midi"), ".mid"}, + {nxt_string("audio/flac"), ".flac"}, + {nxt_string("audio/aac"), ".aac"}, + {nxt_string("audio/wav"), ".wav"}, + + {nxt_string("video/mpeg"), ".mpeg"}, + {nxt_string("video/mpeg"), ".mpg"}, + {nxt_string("video/mp4"), ".mp4"}, + {nxt_string("video/webm"), ".webm"}, + {nxt_string("video/x-msvideo"), ".avi"}, + + {nxt_string("application/octet-stream"), ".exe"}, + {nxt_string("application/octet-stream"), ".bin"}, + {nxt_string("application/octet-stream"), ".dll"}, + {nxt_string("application/octet-stream"), ".iso"}, + {nxt_string("application/octet-stream"), ".img"}, + {nxt_string("application/octet-stream"), ".msi"}, + + {nxt_string("application/octet-stream"), ".deb"}, + {nxt_string("application/octet-stream"), ".rpm"}, + + {nxt_string("application/x-httpd-php"), ".php"}, }; for (i = 0; i < nxt_nitems(default_types); i++) { type = (nxt_str_t *) &default_types[i].type; - exten.start = (u_char *) default_types[i].exten; + exten.start = (u_char *) default_types[i].exten; exten.length = nxt_strlen(exten.start); ret = nxt_http_static_mtypes_hash_add(mp, hash, &exten, type); @@ -1007,29 +982,25 @@ nxt_http_static_mtypes_init(nxt_mp_t *mp, nxt_lvlhsh_t *hash) return NXT_OK; } - -static const nxt_lvlhsh_proto_t nxt_http_static_mtypes_hash_proto - nxt_aligned(64) = -{ - NXT_LVLHSH_DEFAULT, - nxt_http_static_mtypes_hash_test, - nxt_http_static_mtypes_hash_alloc, - nxt_http_static_mtypes_hash_free, +static const nxt_lvlhsh_proto_t nxt_http_static_mtypes_hash_proto nxt_aligned( + 64) + = { + NXT_LVLHSH_DEFAULT, + nxt_http_static_mtypes_hash_test, + nxt_http_static_mtypes_hash_alloc, + nxt_http_static_mtypes_hash_free, }; - typedef struct { nxt_str_t exten; - nxt_str_t *type; + nxt_str_t *type; } nxt_http_static_mtype_t; - nxt_int_t nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash, - const nxt_str_t *exten, nxt_str_t *type) -{ + const nxt_str_t *exten, nxt_str_t *type) { nxt_lvlhsh_query_t lhq; - nxt_http_static_mtype_t *mtype; + nxt_http_static_mtype_t *mtype; mtype = nxt_mp_get(mp, sizeof(nxt_http_static_mtype_t)); if (nxt_slow_path(mtype == NULL)) { @@ -1037,30 +1008,28 @@ nxt_http_static_mtypes_hash_add(nxt_mp_t *mp, nxt_lvlhsh_t *hash, } mtype->exten = *exten; - mtype->type = type; + mtype->type = type; - lhq.key = *exten; + lhq.key = *exten; lhq.key_hash = nxt_djb_hash_lowcase(lhq.key.start, lhq.key.length); - lhq.replace = 1; - lhq.value = mtype; - lhq.proto = &nxt_http_static_mtypes_hash_proto; - lhq.pool = mp; + lhq.replace = 1; + lhq.value = mtype; + lhq.proto = &nxt_http_static_mtypes_hash_proto; + lhq.pool = mp; return nxt_lvlhsh_insert(hash, &lhq); } - nxt_str_t * -nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten) -{ +nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten) { nxt_lvlhsh_query_t lhq; - nxt_http_static_mtype_t *mtype; + nxt_http_static_mtype_t *mtype; - static nxt_str_t empty = nxt_string(""); + static nxt_str_t empty = nxt_string(""); - lhq.key = *exten; + lhq.key = *exten; lhq.key_hash = nxt_djb_hash_lowcase(lhq.key.start, lhq.key.length); - lhq.proto = &nxt_http_static_mtypes_hash_proto; + lhq.proto = &nxt_http_static_mtypes_hash_proto; if (nxt_lvlhsh_find(hash, &lhq) == NXT_OK) { mtype = lhq.value; @@ -1070,27 +1039,21 @@ nxt_http_static_mtype_get(nxt_lvlhsh_t *hash, const nxt_str_t *exten) return ∅ } - static nxt_int_t -nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_http_static_mtype_t *mtype; +nxt_http_static_mtypes_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_http_static_mtype_t *mtype; mtype = data; return nxt_strcasestr_eq(&lhq->key, &mtype->exten) ? NXT_OK : NXT_DECLINED; } - static void * -nxt_http_static_mtypes_hash_alloc(void *data, size_t size) -{ +nxt_http_static_mtypes_hash_alloc(void *data, size_t size) { return nxt_mp_align(data, size, size); } - static void -nxt_http_static_mtypes_hash_free(void *data, void *p) -{ +nxt_http_static_mtypes_hash_free(void *data, void *p) { nxt_mp_free(data, p); } diff --git a/src/nxt_http_variables.c b/src/nxt_http_variables.c index 3a1746b3a..348437e09 100644 --- a/src/nxt_http_variables.c +++ b/src/nxt_http_variables.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,143 +7,168 @@ #include -static nxt_int_t nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_request_uri(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, +static nxt_int_t +nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_host(nxt_task_t *task, nxt_str_t *str, void *ctx, +static nxt_int_t +nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_request_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_remote_addr(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_time_local(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static u_char *nxt_http_log_date(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, const char *format); -static nxt_int_t nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_status(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_referer(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_user_agent(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_response_connection(nxt_task_t *task, - nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_response_content_length(nxt_task_t *task, - nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_response_transfer_encoding(nxt_task_t *task, - nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, +static nxt_int_t +nxt_http_var_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_host(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_remote_addr(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_header(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data); -static nxt_int_t nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, +static nxt_int_t +nxt_http_var_time_local(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static u_char * +nxt_http_log_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, + const char *format); +static nxt_int_t +nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static nxt_int_t +nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static nxt_int_t +nxt_http_var_status(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static nxt_int_t +nxt_http_var_referer(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_user_agent(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static nxt_int_t +nxt_http_var_response_connection(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); +static nxt_int_t +nxt_http_var_response_content_length(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); -static nxt_int_t nxt_http_var_response_header(nxt_task_t *task, nxt_str_t *str, +static nxt_int_t +nxt_http_var_response_transfer_encoding(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_header(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data); +static nxt_int_t +nxt_http_var_response_header(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data); -static nxt_var_decl_t nxt_http_vars[] = { +static nxt_var_decl_t nxt_http_vars[] = { { - .name = nxt_string("dollar"), - .handler = nxt_http_var_dollar, + .name = nxt_string("dollar"), + .handler = nxt_http_var_dollar, .cacheable = 1, - }, { - .name = nxt_string("request_time"), - .handler = nxt_http_var_request_time, + }, + { + .name = nxt_string("request_time"), + .handler = nxt_http_var_request_time, .cacheable = 1, - }, { - .name = nxt_string("method"), - .handler = nxt_http_var_method, + }, + { + .name = nxt_string("method"), + .handler = nxt_http_var_method, .cacheable = 1, - }, { - .name = nxt_string("request_uri"), - .handler = nxt_http_var_request_uri, + }, + { + .name = nxt_string("request_uri"), + .handler = nxt_http_var_request_uri, .cacheable = 1, - }, { - .name = nxt_string("uri"), - .handler = nxt_http_var_uri, + }, + { + .name = nxt_string("uri"), + .handler = nxt_http_var_uri, .cacheable = 0, - }, { - .name = nxt_string("host"), - .handler = nxt_http_var_host, + }, + { + .name = nxt_string("host"), + .handler = nxt_http_var_host, .cacheable = 1, - }, { - .name = nxt_string("remote_addr"), - .handler = nxt_http_var_remote_addr, + }, + { + .name = nxt_string("remote_addr"), + .handler = nxt_http_var_remote_addr, .cacheable = 1, - }, { - .name = nxt_string("time_local"), - .handler = nxt_http_var_time_local, + }, + { + .name = nxt_string("time_local"), + .handler = nxt_http_var_time_local, .cacheable = 1, - }, { - .name = nxt_string("request_line"), - .handler = nxt_http_var_request_line, + }, + { + .name = nxt_string("request_line"), + .handler = nxt_http_var_request_line, .cacheable = 1, - }, { - .name = nxt_string("request_id"), - .handler = nxt_http_var_request_id, + }, + { + .name = nxt_string("request_id"), + .handler = nxt_http_var_request_id, .cacheable = 1, - }, { - .name = nxt_string("status"), - .handler = nxt_http_var_status, + }, + { + .name = nxt_string("status"), + .handler = nxt_http_var_status, .cacheable = 1, - }, { - .name = nxt_string("body_bytes_sent"), - .handler = nxt_http_var_body_bytes_sent, + }, + { + .name = nxt_string("body_bytes_sent"), + .handler = nxt_http_var_body_bytes_sent, .cacheable = 1, - }, { - .name = nxt_string("header_referer"), - .handler = nxt_http_var_referer, + }, + { + .name = nxt_string("header_referer"), + .handler = nxt_http_var_referer, .cacheable = 1, - }, { - .name = nxt_string("response_header_connection"), - .handler = nxt_http_var_response_connection, + }, + { + .name = nxt_string("response_header_connection"), + .handler = nxt_http_var_response_connection, .cacheable = 1, - }, { - .name = nxt_string("response_header_content_length"), - .handler = nxt_http_var_response_content_length, + }, + { + .name = nxt_string("response_header_content_length"), + .handler = nxt_http_var_response_content_length, .cacheable = 1, - }, { - .name = nxt_string("response_header_transfer_encoding"), - .handler = nxt_http_var_response_transfer_encoding, + }, + { + .name = nxt_string("response_header_transfer_encoding"), + .handler = nxt_http_var_response_transfer_encoding, .cacheable = 1, - }, { - .name = nxt_string("header_user_agent"), - .handler = nxt_http_var_user_agent, + }, + { + .name = nxt_string("header_user_agent"), + .handler = nxt_http_var_user_agent, .cacheable = 1, }, }; - nxt_int_t -nxt_http_register_variables(void) -{ +nxt_http_register_variables(void) { return nxt_var_register(nxt_http_vars, nxt_nitems(nxt_http_vars)); } - nxt_int_t -nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) -{ - int64_t hash; - nxt_str_t str, *lower; +nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) { + int64_t hash; + nxt_str_t str, *lower; if (nxt_str_start(name, "response_header_", 16)) { - ref->handler = nxt_http_var_response_header; + ref->handler = nxt_http_var_response_header; ref->cacheable = 0; - str.start = name->start + 16; + str.start = name->start + 16; str.length = name->length - 16; if (str.length == 0) { @@ -164,10 +188,10 @@ nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) } if (nxt_str_start(name, "header_", 7)) { - ref->handler = nxt_http_var_header; + ref->handler = nxt_http_var_header; ref->cacheable = 1; - str.start = name->start + 7; + str.start = name->start + 7; str.length = name->length - 7; if (str.length == 0) { @@ -180,10 +204,10 @@ nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) } } else if (nxt_str_start(name, "arg_", 4)) { - ref->handler = nxt_http_var_arg; + ref->handler = nxt_http_var_arg; ref->cacheable = 1; - str.start = name->start + 4; + str.start = name->start + 4; str.length = name->length - 4; if (str.length == 0) { @@ -196,10 +220,10 @@ nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) } } else if (nxt_str_start(name, "cookie_", 7)) { - ref->handler = nxt_http_var_cookie; + ref->handler = nxt_http_var_cookie; ref->cacheable = 1; - str.start = name->start + 7; + str.start = name->start + 7; str.length = name->length - 7; if (str.length == 0) { @@ -223,29 +247,25 @@ nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name) return NXT_OK; } - static nxt_int_t -nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ +nxt_http_var_dollar(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { nxt_str_set(str, "$"); return NXT_OK; } - static nxt_int_t nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - u_char *p; + void *data) { + u_char *p; nxt_msec_t ms; nxt_nsec_t now; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; now = nxt_thread_monotonic_time(task->thread); - ms = (now - r->start_time) / 1000000; + ms = (now - r->start_time) / 1000000; str->start = nxt_mp_nget(r->mem_pool, NXT_TIME_T_LEN + 4); if (nxt_slow_path(str->start == NULL)) { @@ -253,18 +273,16 @@ nxt_http_var_request_time(nxt_task_t *task, nxt_str_t *str, void *ctx, } p = nxt_sprintf(str->start, str->start + NXT_TIME_T_LEN, "%T.%03M", - (nxt_time_t) ms / 1000, ms % 1000); + (nxt_time_t) ms / 1000, ms % 1000); str->length = p - str->start; return NXT_OK; } - static nxt_int_t -nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; @@ -273,12 +291,10 @@ nxt_http_var_method(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t nxt_http_var_request_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - nxt_http_request_t *r; + void *data) { + nxt_http_request_t *r; r = ctx; @@ -287,11 +303,9 @@ nxt_http_var_request_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, return NXT_OK; } - static nxt_int_t -nxt_http_var_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; @@ -300,11 +314,9 @@ nxt_http_var_uri(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t -nxt_http_var_host(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_host(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; @@ -313,28 +325,25 @@ nxt_http_var_host(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t nxt_http_var_remote_addr(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - nxt_http_request_t *r; + void *data) { + nxt_http_request_t *r; r = ctx; str->length = r->remote->address_length; - str->start = nxt_sockaddr_address(r->remote); + str->start = nxt_sockaddr_address(r->remote); return NXT_OK; } - static nxt_int_t -nxt_http_var_time_local(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_time_local(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data) { + nxt_http_request_t *r; - static nxt_time_string_t date_cache = { + static nxt_time_string_t date_cache = { (nxt_atomic_uint_t) -1, nxt_http_log_date, "%02d/%s/%4d:%02d:%02d:%02d %c%02d%02d", @@ -358,40 +367,34 @@ nxt_http_var_time_local(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static u_char * -nxt_http_log_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, - size_t size, const char *format) -{ - u_char sign; - time_t gmtoff; +nxt_http_log_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size, + const char *format) { + u_char sign; + time_t gmtoff; - static const char * const month[] = { "Jan", "Feb", "Mar", "Apr", "May", - "Jun", "Jul", "Aug", "Sep", "Oct", - "Nov", "Dec" }; + static const char *const month[] = {"Jan", "Feb", "Mar", "Apr", "May", + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; gmtoff = nxt_timezone(tm) / 60; if (gmtoff < 0) { gmtoff = -gmtoff; - sign = '-'; + sign = '-'; } else { sign = '+'; } - return nxt_sprintf(buf, buf + size, format, - tm->tm_mday, month[tm->tm_mon], tm->tm_year + 1900, - tm->tm_hour, tm->tm_min, tm->tm_sec, - sign, gmtoff / 60, gmtoff % 60); + return nxt_sprintf(buf, buf + size, format, tm->tm_mday, month[tm->tm_mon], + tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, sign, + gmtoff / 60, gmtoff % 60); } - static nxt_int_t nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - nxt_http_request_t *r; + void *data) { + nxt_http_request_t *r; r = ctx; @@ -400,13 +403,11 @@ nxt_http_var_request_line(nxt_task_t *task, nxt_str_t *str, void *ctx, return NXT_OK; } - static nxt_int_t nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - nxt_random_t *rand; - nxt_http_request_t *r; + void *data) { + nxt_random_t *rand; + nxt_http_request_t *r; r = ctx; @@ -420,20 +421,17 @@ nxt_http_var_request_id(nxt_task_t *task, nxt_str_t *str, void *ctx, rand = &task->thread->random; (void) nxt_sprintf(str->start, str->start + 32, "%08xD%08xD%08xD%08xD", - nxt_random(rand), nxt_random(rand), - nxt_random(rand), nxt_random(rand)); + nxt_random(rand), nxt_random(rand), nxt_random(rand), nxt_random(rand)); return NXT_OK; } - static nxt_int_t nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - u_char *p; + void *data) { + u_char *p; nxt_off_t bytes; - nxt_http_request_t *r; + nxt_http_request_t *r; r = ctx; @@ -451,11 +449,9 @@ nxt_http_var_body_bytes_sent(nxt_task_t *task, nxt_str_t *str, void *ctx, return NXT_OK; } - static nxt_int_t -nxt_http_var_status(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_status(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; @@ -471,16 +467,14 @@ nxt_http_var_status(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t -nxt_http_var_referer(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_referer(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; if (r->referer != NULL) { - str->start = r->referer->value; + str->start = r->referer->value; str->length = r->referer->value_length; } else { @@ -490,16 +484,15 @@ nxt_http_var_referer(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t -nxt_http_var_user_agent(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_http_request_t *r; +nxt_http_var_user_agent(nxt_task_t *task, nxt_str_t *str, void *ctx, + void *data) { + nxt_http_request_t *r; r = ctx; if (r->user_agent != NULL) { - str->start = r->user_agent->value; + str->start = r->user_agent->value; str->length = r->user_agent->value_length; } else { @@ -509,23 +502,21 @@ nxt_http_var_user_agent(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t nxt_http_var_response_connection(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ + void *data) { nxt_int_t conn; nxt_bool_t http11; - nxt_h1proto_t *h1p; - nxt_http_request_t *r; + nxt_h1proto_t *h1p; + nxt_http_request_t *r; - static const nxt_str_t connection[3] = { + static const nxt_str_t connection[3] = { nxt_string("close"), nxt_string("keep-alive"), nxt_string("Upgrade"), }; - r = ctx; + r = ctx; h1p = r->proto.h1; conn = -1; @@ -551,19 +542,17 @@ nxt_http_var_response_connection(nxt_task_t *task, nxt_str_t *str, void *ctx, return NXT_OK; } - static nxt_int_t nxt_http_var_response_content_length(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data) -{ - u_char *p; - nxt_http_request_t *r; + void *ctx, void *data) { + u_char *p; + nxt_http_request_t *r; r = ctx; if (r->resp.content_length != NULL) { str->length = r->resp.content_length->value_length; - str->start = r->resp.content_length->value; + str->start = r->resp.content_length->value; return NXT_OK; } @@ -574,8 +563,8 @@ nxt_http_var_response_content_length(nxt_task_t *task, nxt_str_t *str, return NXT_ERROR; } - p = nxt_sprintf(str->start, str->start + NXT_OFF_T_LEN, - "%O", r->resp.content_length_n); + p = nxt_sprintf(str->start, str->start + NXT_OFF_T_LEN, "%O", + r->resp.content_length_n); str->length = p - str->start; @@ -587,12 +576,10 @@ nxt_http_var_response_content_length(nxt_task_t *task, nxt_str_t *str, return NXT_OK; } - static nxt_int_t nxt_http_var_response_transfer_encoding(nxt_task_t *task, nxt_str_t *str, - void *ctx, void *data) -{ - nxt_http_request_t *r; + void *ctx, void *data) { + nxt_http_request_t *r; r = ctx; @@ -606,16 +593,14 @@ nxt_http_var_response_transfer_encoding(nxt_task_t *task, nxt_str_t *str, return NXT_OK; } - static nxt_int_t -nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_array_t *args; - nxt_var_field_t *vf; - nxt_http_request_t *r; - nxt_http_name_value_t *nv, *start; +nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_array_t *args; + nxt_var_field_t *vf; + nxt_http_request_t *r; + nxt_http_name_value_t *nv, *start; - r = ctx; + r = ctx; vf = data; args = nxt_http_arguments_parse(r); @@ -624,15 +609,12 @@ nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) } start = args->elts; - nv = start + args->nelts - 1; + nv = start + args->nelts - 1; while (nv >= start) { - - if (vf->hash == nv->hash - && vf->name.length == nv->name_length - && memcmp(vf->name.start, nv->name, nv->name_length) == 0) - { - str->start = nv->value; + if (vf->hash == nv->hash && vf->name.length == nv->name_length + && memcmp(vf->name.start, nv->name, nv->name_length) == 0) { + str->start = nv->value; str->length = nv->value_length; return NXT_OK; @@ -646,46 +628,39 @@ nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static nxt_int_t -nxt_http_var_header(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_var_field_t *vf; - nxt_http_field_t *f; - nxt_http_request_t *r; +nxt_http_var_header(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_var_field_t *vf; + nxt_http_field_t *f; + nxt_http_request_t *r; - r = ctx; + r = ctx; vf = data; nxt_list_each(f, r->fields) { - - if (vf->hash == f->hash - && vf->name.length == f->name_length - && nxt_strncasecmp(vf->name.start, f->name, f->name_length) == 0) - { - str->start = f->value; + if (vf->hash == f->hash && vf->name.length == f->name_length + && nxt_strncasecmp(vf->name.start, f->name, f->name_length) == 0) { + str->start = f->value; str->length = f->value_length; return NXT_OK; } - - } nxt_list_loop; + } + nxt_list_loop; nxt_str_null(str); return NXT_OK; } - static nxt_int_t -nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) -{ - nxt_array_t *cookies; - nxt_var_field_t *vf; - nxt_http_request_t *r; - nxt_http_name_value_t *nv, *end; +nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) { + nxt_array_t *cookies; + nxt_var_field_t *vf; + nxt_http_request_t *r; + nxt_http_name_value_t *nv, *end; - r = ctx; + r = ctx; vf = data; cookies = nxt_http_cookies_parse(r); @@ -693,16 +668,13 @@ nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_ERROR; } - nv = cookies->elts; + nv = cookies->elts; end = nv + cookies->nelts; while (nv < end) { - - if (vf->hash == nv->hash - && vf->name.length == nv->name_length - && memcmp(vf->name.start, nv->name, nv->name_length) == 0) - { - str->start = nv->value; + if (vf->hash == nv->hash && vf->name.length == nv->name_length + && memcmp(vf->name.start, nv->name, nv->name_length) == 0) { + str->start = nv->value; str->length = nv->value_length; return NXT_OK; @@ -716,12 +688,10 @@ nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, void *data) return NXT_OK; } - static int -nxt_http_field_name_cmp(nxt_str_t *name, nxt_http_field_t *field) -{ - size_t i; - u_char c1, c2; +nxt_http_field_name_cmp(nxt_str_t *name, nxt_http_field_t *field) { + size_t i; + u_char c1, c2; if (name->length != field->name_length) { return 1; @@ -746,32 +716,29 @@ nxt_http_field_name_cmp(nxt_str_t *name, nxt_http_field_t *field) return 0; } - static nxt_int_t nxt_http_var_response_header(nxt_task_t *task, nxt_str_t *str, void *ctx, - void *data) -{ - nxt_str_t *name; - nxt_http_field_t *f; - nxt_http_request_t *r; + void *data) { + nxt_str_t *name; + nxt_http_field_t *f; + nxt_http_request_t *r; - r = ctx; + r = ctx; name = data; nxt_list_each(f, r->resp.fields) { - if (f->skip) { continue; } if (nxt_http_field_name_cmp(name, f) == 0) { - str->start = f->value; + str->start = f->value; str->length = f->value_length; return NXT_OK; } - - } nxt_list_loop; + } + nxt_list_loop; nxt_str_null(str); diff --git a/src/nxt_http_websocket.c b/src/nxt_http_websocket.c index 1968633ea..844c9433e 100644 --- a/src/nxt_http_websocket.c +++ b/src/nxt_http_websocket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,31 +11,28 @@ #include -static void nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data); -static void nxt_http_websocket_error_handler(nxt_task_t *task, void *obj, - void *data); +static void +nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data); +static void +nxt_http_websocket_error_handler(nxt_task_t *task, void *obj, void *data); -const nxt_http_request_state_t nxt_http_websocket - nxt_aligned(64) = -{ +const nxt_http_request_state_t nxt_http_websocket nxt_aligned(64) = { .ready_handler = nxt_http_websocket_client, .error_handler = nxt_http_websocket_error_handler, }; - static void -nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) -{ +nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) { size_t frame_size, used_size, copy_size, buf_free_size; size_t chunk_copy_size; - nxt_buf_t *out, *buf, **out_tail, *b, *next; + nxt_buf_t *out, *buf, **out_tail, *b, *next; nxt_int_t res; - nxt_http_request_t *r; - nxt_request_rpc_data_t *req_rpc_data; - nxt_websocket_header_t *wsh; + nxt_http_request_t *r; + nxt_request_rpc_data_t *req_rpc_data; + nxt_websocket_header_t *wsh; - r = obj; + r = obj; req_rpc_data = r->req_rpc_data; if (nxt_slow_path(req_rpc_data == NULL)) { @@ -50,12 +46,12 @@ nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) wsh = (nxt_websocket_header_t *) r->ws_frame->mem.pos; frame_size = nxt_websocket_frame_header_size(wsh) - + nxt_websocket_frame_payload_len(wsh); + + nxt_websocket_frame_payload_len(wsh); - buf = NULL; + buf = NULL; buf_free_size = 0; - out = NULL; - out_tail = &out; + out = NULL; + out_tail = &out; b = r->ws_frame; @@ -68,29 +64,29 @@ nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) buf_free_size = nxt_min(frame_size, PORT_MMAP_DATA_SIZE); buf = nxt_port_mmap_get_buf(task, &req_rpc_data->app->outgoing, - buf_free_size); + buf_free_size); *out_tail = buf; - out_tail = &buf->next; + out_tail = &buf->next; } chunk_copy_size = nxt_min(buf_free_size, copy_size); - buf->mem.free = nxt_cpymem(buf->mem.free, b->mem.pos, - chunk_copy_size); + buf->mem.free + = nxt_cpymem(buf->mem.free, b->mem.pos, chunk_copy_size); - copy_size -= chunk_copy_size; - b->mem.pos += chunk_copy_size; + copy_size -= chunk_copy_size; + b->mem.pos += chunk_copy_size; buf_free_size -= chunk_copy_size; } frame_size -= copy_size; - next = b->next; - b->next = NULL; + next = b->next; + b->next = NULL; if (nxt_buf_mem_used_size(&b->mem) == 0) { nxt_work_queue_add(&task->thread->engine->fast_work_queue, - b->completion_handler, task, b, b->parent); + b->completion_handler, task, b, b->parent); r->ws_frame = next; } @@ -99,9 +95,8 @@ nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) } res = nxt_port_socket_write(task, req_rpc_data->app_port, - NXT_PORT_MSG_WEBSOCKET, -1, - req_rpc_data->stream, - task->thread->engine->port->id, out); + NXT_PORT_MSG_WEBSOCKET, -1, req_rpc_data->stream, + task->thread->engine->port->id, out); if (nxt_slow_path(res != NXT_OK)) { // TODO: handle } @@ -114,7 +109,7 @@ nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) if (used_size > 0) { nxt_memmove(b->mem.start, b->mem.pos, used_size); - b->mem.pos = b->mem.start; + b->mem.pos = b->mem.start; b->mem.free = b->mem.start + used_size; } } @@ -122,16 +117,14 @@ nxt_http_websocket_client(nxt_task_t *task, void *obj, void *data) nxt_http_request_ws_frame_start(task, r, r->ws_frame); } - static void -nxt_http_websocket_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; - nxt_request_rpc_data_t *req_rpc_data; +nxt_http_websocket_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; + nxt_request_rpc_data_t *req_rpc_data; nxt_debug(task, "http websocket error handler"); - r = obj; + r = obj; req_rpc_data = r->req_rpc_data; if (req_rpc_data == NULL) { @@ -145,9 +138,8 @@ nxt_http_websocket_error_handler(nxt_task_t *task, void *obj, void *data) } (void) nxt_port_socket_write(task, req_rpc_data->app_port, - NXT_PORT_MSG_WEBSOCKET_LAST, - -1, req_rpc_data->stream, - task->thread->engine->port->id, NULL); + NXT_PORT_MSG_WEBSOCKET_LAST, -1, req_rpc_data->stream, + task->thread->engine->port->id, NULL); close_handler: diff --git a/src/nxt_isolation.c b/src/nxt_isolation.c index 909a43f4b..29a9055ab 100644 --- a/src/nxt_isolation.c +++ b/src/nxt_isolation.c @@ -13,71 +13,84 @@ #endif -static nxt_int_t nxt_isolation_set(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); +static nxt_int_t +nxt_isolation_set(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); #if (NXT_HAVE_CGROUP) -static nxt_int_t nxt_isolation_set_cgroup(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); +static nxt_int_t +nxt_isolation_set_cgroup(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); #endif #if (NXT_HAVE_LINUX_NS) -static nxt_int_t nxt_isolation_set_namespaces(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); -static nxt_int_t nxt_isolation_clone_flags(nxt_task_t *task, - nxt_conf_value_t *namespaces, nxt_clone_t *clone); +static nxt_int_t +nxt_isolation_set_namespaces(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); +static nxt_int_t +nxt_isolation_clone_flags(nxt_task_t *task, nxt_conf_value_t *namespaces, + nxt_clone_t *clone); #endif #if (NXT_HAVE_CLONE_NEWUSER) -static nxt_int_t nxt_isolation_set_creds(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); -static nxt_int_t nxt_isolation_credential_map(nxt_task_t *task, - nxt_mp_t *mem_pool, nxt_conf_value_t *map_array, - nxt_clone_credential_map_t *map); -static nxt_int_t nxt_isolation_vldt_creds(nxt_task_t *task, +static nxt_int_t +nxt_isolation_set_creds(nxt_task_t *task, nxt_conf_value_t *isolation, nxt_process_t *process); +static nxt_int_t +nxt_isolation_credential_map(nxt_task_t *task, nxt_mp_t *mem_pool, + nxt_conf_value_t *map_array, nxt_clone_credential_map_t *map); +static nxt_int_t +nxt_isolation_vldt_creds(nxt_task_t *task, nxt_process_t *process); #endif #if (NXT_HAVE_ISOLATION_ROOTFS) -static nxt_int_t nxt_isolation_set_rootfs(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); -static nxt_int_t nxt_isolation_set_automount(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); -static nxt_int_t nxt_isolation_set_mounts(nxt_task_t *task, - nxt_process_t *process, nxt_str_t *app_type); -static nxt_int_t nxt_isolation_set_lang_mounts(nxt_task_t *task, - nxt_process_t *process, nxt_array_t *syspaths); -static int nxt_cdecl nxt_isolation_mount_compare(const void *v1, - const void *v2); -static void nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_isolation_set_rootfs(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); +static nxt_int_t +nxt_isolation_set_automount(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); +static nxt_int_t +nxt_isolation_set_mounts(nxt_task_t *task, nxt_process_t *process, + nxt_str_t *app_type); +static nxt_int_t +nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, + nxt_array_t *syspaths); +static int nxt_cdecl +nxt_isolation_mount_compare(const void *v1, const void *v2); +static void +nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process); #if (NXT_HAVE_LINUX_PIVOT_ROOT) && (NXT_HAVE_CLONE_NEWNS) -static nxt_int_t nxt_isolation_pivot_root(nxt_task_t *task, const char *rootfs); -static nxt_int_t nxt_isolation_make_private_mount(nxt_task_t *task, - const char *rootfs); -nxt_inline int nxt_pivot_root(const char *new_root, const char *old_root); +static nxt_int_t +nxt_isolation_pivot_root(nxt_task_t *task, const char *rootfs); +static nxt_int_t +nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs); +nxt_inline int +nxt_pivot_root(const char *new_root, const char *old_root); #endif -static nxt_int_t nxt_isolation_chroot(nxt_task_t *task, const char *path); +static nxt_int_t +nxt_isolation_chroot(nxt_task_t *task, const char *path); #endif #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) -static nxt_int_t nxt_isolation_set_new_privs(nxt_task_t *task, - nxt_conf_value_t *isolation, nxt_process_t *process); +static nxt_int_t +nxt_isolation_set_new_privs(nxt_task_t *task, nxt_conf_value_t *isolation, + nxt_process_t *process); #endif nxt_int_t nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, - nxt_mp_t *mp) -{ + nxt_mp_t *mp) { nxt_int_t cap_setid; nxt_int_t ret; - nxt_runtime_t *rt; - nxt_common_app_conf_t *app_conf; + nxt_runtime_t *rt; + nxt_common_app_conf_t *app_conf; - rt = task->thread->runtime; - app_conf = process->data.app; + rt = task->thread->runtime; + app_conf = process->data.app; cap_setid = rt->capabilities.setid; #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) @@ -99,7 +112,7 @@ nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, if (cap_setid) { ret = nxt_process_creds_set(task, process, &app_conf->user, - &app_conf->group); + &app_conf->group); if (nxt_slow_path(ret != NXT_OK)) { return ret; @@ -107,21 +120,22 @@ nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, } else { if (!nxt_str_eq(&app_conf->user, (u_char *) rt->user_cred.user, - nxt_strlen(rt->user_cred.user))) - { - nxt_alert(task, "cannot set user \"%V\" for app \"%V\": " - "missing capabilities", &app_conf->user, &app_conf->name); + nxt_strlen(rt->user_cred.user))) { + nxt_alert(task, + "cannot set user \"%V\" for app \"%V\": " + "missing capabilities", + &app_conf->user, &app_conf->name); return NXT_ERROR; } if (app_conf->group.length > 0 && !nxt_str_eq(&app_conf->group, (u_char *) rt->group, - nxt_strlen(rt->group))) - { - nxt_alert(task, "cannot set group \"%V\" for app \"%V\": " - "missing capabilities", &app_conf->group, - &app_conf->name); + nxt_strlen(rt->group))) { + nxt_alert(task, + "cannot set group \"%V\" for app \"%V\": " + "missing capabilities", + &app_conf->group, &app_conf->name); return NXT_ERROR; } @@ -129,7 +143,7 @@ nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, #if (NXT_HAVE_ISOLATION_ROOTFS) if (process->isolation.rootfs != NULL) { - nxt_int_t has_mnt; + nxt_int_t has_mnt; ret = nxt_isolation_set_mounts(task, process, &app_conf->type); if (nxt_slow_path(ret != NXT_OK)) { @@ -144,8 +158,8 @@ nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, if (process->user_cred->uid == 0 && !has_mnt) { nxt_log(task, NXT_LOG_WARN, - "setting user \"root\" with \"rootfs\" is unsafe without " - "\"mount\" namespace isolation"); + "setting user \"root\" with \"rootfs\" is unsafe without " + "\"mount\" namespace isolation"); } } #endif @@ -160,53 +174,45 @@ nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, return NXT_OK; } - static nxt_int_t nxt_isolation_set(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ + nxt_process_t *process) { #if (NXT_HAVE_CGROUP) - if (nxt_slow_path(nxt_isolation_set_cgroup(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_cgroup(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } #endif #if (NXT_HAVE_LINUX_NS) - if (nxt_slow_path(nxt_isolation_set_namespaces(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_namespaces(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } #endif #if (NXT_HAVE_CLONE_NEWUSER) - if (nxt_slow_path(nxt_isolation_set_creds(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_creds(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } #endif #if (NXT_HAVE_ISOLATION_ROOTFS) - if (nxt_slow_path(nxt_isolation_set_rootfs(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_rootfs(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } - if (nxt_slow_path(nxt_isolation_set_automount(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_automount(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } #endif #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) - if (nxt_slow_path(nxt_isolation_set_new_privs(task, isolation, process) - != NXT_OK)) - { + if (nxt_slow_path( + nxt_isolation_set_new_privs(task, isolation, process) != NXT_OK)) { return NXT_ERROR; } #endif @@ -219,13 +225,12 @@ nxt_isolation_set(nxt_task_t *task, nxt_conf_value_t *isolation, static nxt_int_t nxt_isolation_set_cgroup(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ + nxt_process_t *process) { nxt_str_t str; - nxt_conf_value_t *obj; + nxt_conf_value_t *obj; - static const nxt_str_t cgname = nxt_string("cgroup"); - static const nxt_str_t path = nxt_string("path"); + static const nxt_str_t cgname = nxt_string("cgroup"); + static const nxt_str_t path = nxt_string("path"); obj = nxt_conf_get_object_member(isolation, &cgname, NULL); if (obj == NULL) { @@ -238,8 +243,8 @@ nxt_isolation_set_cgroup(nxt_task_t *task, nxt_conf_value_t *isolation, } nxt_conf_get_string(obj, &str); - process->isolation.cgroup.path = nxt_mp_alloc(process->mem_pool, - str.length + 1); + process->isolation.cgroup.path + = nxt_mp_alloc(process->mem_pool, str.length + 1); nxt_memcpy(process->isolation.cgroup.path, str.start, str.length); process->isolation.cgroup.path[str.length] = '\0'; @@ -255,12 +260,11 @@ nxt_isolation_set_cgroup(nxt_task_t *task, nxt_conf_value_t *isolation, static nxt_int_t nxt_isolation_set_namespaces(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ + nxt_process_t *process) { nxt_int_t ret; - nxt_conf_value_t *obj; + nxt_conf_value_t *obj; - static const nxt_str_t nsname = nxt_string("namespaces"); + static const nxt_str_t nsname = nxt_string("namespaces"); obj = nxt_conf_get_object_member(isolation, &nsname, NULL); if (obj != NULL) { @@ -280,11 +284,10 @@ nxt_isolation_set_namespaces(nxt_task_t *task, nxt_conf_value_t *isolation, static nxt_int_t nxt_isolation_set_creds(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ + nxt_process_t *process) { nxt_int_t ret; - nxt_clone_t *clone; - nxt_conf_value_t *array; + nxt_clone_t *clone; + nxt_conf_value_t *array; static const nxt_str_t uidname = nxt_string("uidmap"); static const nxt_str_t gidname = nxt_string("gidmap"); @@ -294,7 +297,7 @@ nxt_isolation_set_creds(nxt_task_t *task, nxt_conf_value_t *isolation, array = nxt_conf_get_object_member(isolation, &uidname, NULL); if (array != NULL) { ret = nxt_isolation_credential_map(task, process->mem_pool, array, - &clone->uidmap); + &clone->uidmap); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -304,7 +307,7 @@ nxt_isolation_set_creds(nxt_task_t *task, nxt_conf_value_t *isolation, array = nxt_conf_get_object_member(isolation, &gidname, NULL); if (array != NULL) { ret = nxt_isolation_credential_map(task, process->mem_pool, array, - &clone->gidmap); + &clone->gidmap); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -314,16 +317,14 @@ nxt_isolation_set_creds(nxt_task_t *task, nxt_conf_value_t *isolation, return NXT_OK; } - static nxt_int_t nxt_isolation_credential_map(nxt_task_t *task, nxt_mp_t *mp, - nxt_conf_value_t *map_array, nxt_clone_credential_map_t *map) -{ + nxt_conf_value_t *map_array, nxt_clone_credential_map_t *map) { nxt_int_t ret; nxt_uint_t i; - nxt_conf_value_t *obj; + nxt_conf_value_t *obj; - static const nxt_conf_map_t nxt_clone_map_entry_conf[] = { + static const nxt_conf_map_t nxt_clone_map_entry_conf[] = { { nxt_string("container"), NXT_CONF_MAP_INT64, @@ -358,8 +359,7 @@ nxt_isolation_credential_map(nxt_task_t *task, nxt_mp_t *mp, obj = nxt_conf_get_array_element(map_array, i); ret = nxt_conf_map_object(mp, obj, nxt_clone_map_entry_conf, - nxt_nitems(nxt_clone_map_entry_conf), - map->map + i); + nxt_nitems(nxt_clone_map_entry_conf), map->map + i); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "clone map entry map error"); return NXT_ERROR; @@ -369,13 +369,11 @@ nxt_isolation_credential_map(nxt_task_t *task, nxt_mp_t *mp, return NXT_OK; } - static nxt_int_t -nxt_isolation_vldt_creds(nxt_task_t *task, nxt_process_t *process) -{ +nxt_isolation_vldt_creds(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret; - nxt_clone_t *clone; - nxt_credential_t *creds; + nxt_clone_t *clone; + nxt_credential_t *creds; clone = &process->isolation.clone; creds = process->user_cred; @@ -386,15 +384,17 @@ nxt_isolation_vldt_creds(nxt_task_t *task, nxt_process_t *process) if (!nxt_is_clone_flag_set(clone->flags, NEWUSER)) { if (nxt_slow_path(clone->uidmap.size > 0)) { - nxt_log(task, NXT_LOG_ERR, "\"uidmap\" is set but " - "\"isolation.namespaces.credential\" is false or unset"); + nxt_log(task, NXT_LOG_ERR, + "\"uidmap\" is set but " + "\"isolation.namespaces.credential\" is false or unset"); return NXT_ERROR; } if (nxt_slow_path(clone->gidmap.size > 0)) { - nxt_log(task, NXT_LOG_ERR, "\"gidmap\" is set but " - "\"isolation.namespaces.credential\" is false or unset"); + nxt_log(task, NXT_LOG_ERR, + "\"gidmap\" is set but " + "\"isolation.namespaces.credential\" is false or unset"); return NXT_ERROR; } @@ -417,16 +417,15 @@ nxt_isolation_vldt_creds(nxt_task_t *task, nxt_process_t *process) static nxt_int_t nxt_isolation_clone_flags(nxt_task_t *task, nxt_conf_value_t *namespaces, - nxt_clone_t *clone) -{ + nxt_clone_t *clone) { uint32_t index; nxt_str_t name; nxt_int_t flag; - nxt_conf_value_t *value; + nxt_conf_value_t *value; index = 0; - for ( ;; ) { + for (;;) { value = nxt_conf_next_object_member(namespaces, &name, &index); if (value == NULL) { @@ -491,20 +490,21 @@ nxt_isolation_clone_flags(nxt_task_t *task, nxt_conf_value_t *namespaces, static nxt_int_t nxt_isolation_set_rootfs(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ + nxt_process_t *process) { nxt_str_t str; - nxt_conf_value_t *obj; + nxt_conf_value_t *obj; - static const nxt_str_t rootfs_name = nxt_string("rootfs"); + static const nxt_str_t rootfs_name = nxt_string("rootfs"); obj = nxt_conf_get_object_member(isolation, &rootfs_name, NULL); if (obj != NULL) { nxt_conf_get_string(obj, &str); if (nxt_slow_path(str.length <= 1 || str.start[0] != '/')) { - nxt_log(task, NXT_LOG_ERR, "rootfs requires an absolute path other " - "than \"/\" but given \"%V\"", &str); + nxt_log(task, NXT_LOG_ERR, + "rootfs requires an absolute path other " + "than \"/\" but given \"%V\"", + &str); return NXT_ERROR; } @@ -513,8 +513,8 @@ nxt_isolation_set_rootfs(nxt_task_t *task, nxt_conf_value_t *isolation, str.length--; } - process->isolation.rootfs = nxt_mp_alloc(process->mem_pool, - str.length + 1); + process->isolation.rootfs + = nxt_mp_alloc(process->mem_pool, str.length + 1); if (nxt_slow_path(process->isolation.rootfs == NULL)) { return NXT_ERROR; @@ -528,24 +528,22 @@ nxt_isolation_set_rootfs(nxt_task_t *task, nxt_conf_value_t *isolation, return NXT_OK; } - static nxt_int_t nxt_isolation_set_automount(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ - nxt_conf_value_t *conf, *value; - nxt_process_automount_t *automount; + nxt_process_t *process) { + nxt_conf_value_t *conf, *value; + nxt_process_automount_t *automount; - static const nxt_str_t automount_name = nxt_string("automount"); - static const nxt_str_t langdeps_name = nxt_string("language_deps"); - static const nxt_str_t tmp_name = nxt_string("tmpfs"); - static const nxt_str_t proc_name = nxt_string("procfs"); + static const nxt_str_t automount_name = nxt_string("automount"); + static const nxt_str_t langdeps_name = nxt_string("language_deps"); + static const nxt_str_t tmp_name = nxt_string("tmpfs"); + static const nxt_str_t proc_name = nxt_string("procfs"); automount = &process->isolation.automount; automount->language_deps = 1; - automount->tmpfs = 1; - automount->procfs = 1; + automount->tmpfs = 1; + automount->procfs = 1; conf = nxt_conf_get_object_member(isolation, &automount_name, NULL); if (conf != NULL) { @@ -568,18 +566,16 @@ nxt_isolation_set_automount(nxt_task_t *task, nxt_conf_value_t *isolation, return NXT_OK; } - static nxt_int_t nxt_isolation_set_mounts(nxt_task_t *task, nxt_process_t *process, - nxt_str_t *app_type) -{ + nxt_str_t *app_type) { nxt_int_t ret, cap_chroot; - nxt_runtime_t *rt; - nxt_app_lang_module_t *lang; + nxt_runtime_t *rt; + nxt_app_lang_module_t *lang; - rt = task->thread->runtime; + rt = task->thread->runtime; cap_chroot = rt->capabilities.chroot; - lang = nxt_app_lang_module(rt, app_type); + lang = nxt_app_lang_module(rt, app_type); nxt_assert(lang != NULL); @@ -604,17 +600,15 @@ nxt_isolation_set_mounts(nxt_task_t *task, nxt_process_t *process, return NXT_OK; } - static nxt_int_t nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, - nxt_array_t *lang_mounts) -{ - u_char *p; + nxt_array_t *lang_mounts) { + u_char *p; size_t i, n, rootfs_len, len; - nxt_mp_t *mp; - nxt_array_t *mounts; - const u_char *rootfs; - nxt_fs_mount_t *mnt, *lang_mnt; + nxt_mp_t *mp; + nxt_array_t *mounts; + const u_char *rootfs; + nxt_fs_mount_t *mnt, *lang_mnt; mp = process->mem_pool; @@ -624,11 +618,11 @@ nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, return NXT_ERROR; } - n = mounts->nelts; - mnt = mounts->elts; + n = mounts->nelts; + mnt = mounts->elts; lang_mnt = lang_mounts->elts; - rootfs = process->isolation.rootfs; + rootfs = process->isolation.rootfs; rootfs_len = nxt_strlen(rootfs); for (i = 0; i < n; i++) { @@ -639,8 +633,8 @@ nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, return NXT_ERROR; } - p = nxt_cpymem(mnt[i].dst, rootfs, rootfs_len); - p = nxt_cpymem(p, lang_mnt[i].dst, len); + p = nxt_cpymem(mnt[i].dst, rootfs, rootfs_len); + p = nxt_cpymem(p, lang_mnt[i].dst, len); *p = '\0'; } @@ -650,23 +644,22 @@ nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, return NXT_ERROR; } - mnt->src = (u_char *) "tmpfs"; + mnt->src = (u_char *) "tmpfs"; mnt->name = (u_char *) "tmpfs"; mnt->type = NXT_FS_TMP; - mnt->flags = (NXT_FS_FLAGS_NOSUID - | NXT_FS_FLAGS_NODEV - | NXT_FS_FLAGS_NOEXEC); - mnt->data = (u_char *) "size=1m,mode=1777"; + mnt->flags + = (NXT_FS_FLAGS_NOSUID | NXT_FS_FLAGS_NODEV | NXT_FS_FLAGS_NOEXEC); + mnt->data = (u_char *) "size=1m,mode=1777"; mnt->builtin = 1; - mnt->deps = 0; + mnt->deps = 0; mnt->dst = nxt_mp_nget(mp, rootfs_len + nxt_length("/tmp") + 1); if (nxt_slow_path(mnt->dst == NULL)) { return NXT_ERROR; } - p = nxt_cpymem(mnt->dst, rootfs, rootfs_len); - p = nxt_cpymem(p, "/tmp", 4); + p = nxt_cpymem(mnt->dst, rootfs, rootfs_len); + p = nxt_cpymem(p, "/tmp", 4); *p = '\0'; } @@ -678,35 +671,33 @@ nxt_isolation_set_lang_mounts(nxt_task_t *task, nxt_process_t *process, mnt->name = (u_char *) "proc"; mnt->type = NXT_FS_PROC; - mnt->src = (u_char *) "none"; - mnt->dst = nxt_mp_nget(mp, rootfs_len + nxt_length("/proc") + 1); + mnt->src = (u_char *) "none"; + mnt->dst = nxt_mp_nget(mp, rootfs_len + nxt_length("/proc") + 1); if (nxt_slow_path(mnt->dst == NULL)) { return NXT_ERROR; } - p = nxt_cpymem(mnt->dst, rootfs, rootfs_len); - p = nxt_cpymem(p, "/proc", 5); + p = nxt_cpymem(mnt->dst, rootfs, rootfs_len); + p = nxt_cpymem(p, "/proc", 5); *p = '\0'; - mnt->data = (u_char *) ""; - mnt->flags = NXT_FS_FLAGS_NOEXEC | NXT_FS_FLAGS_NOSUID; + mnt->data = (u_char *) ""; + mnt->flags = NXT_FS_FLAGS_NOEXEC | NXT_FS_FLAGS_NOSUID; mnt->builtin = 1; - mnt->deps = 0; + mnt->deps = 0; } qsort(mounts->elts, mounts->nelts, sizeof(nxt_fs_mount_t), - nxt_isolation_mount_compare); + nxt_isolation_mount_compare); process->isolation.mounts = mounts; return NXT_OK; } - static int nxt_cdecl -nxt_isolation_mount_compare(const void *v1, const void *v2) -{ - const nxt_fs_mount_t *mnt1, *mnt2; +nxt_isolation_mount_compare(const void *v1, const void *v2) { + const nxt_fs_mount_t *mnt1, *mnt2; mnt1 = v1; mnt2 = v2; @@ -714,15 +705,13 @@ nxt_isolation_mount_compare(const void *v1, const void *v2) return nxt_strlen(mnt1->src) > nxt_strlen(mnt2->src); } - void -nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process) -{ +nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process) { size_t n; - nxt_array_t *mounts; - nxt_runtime_t *rt; - nxt_fs_mount_t *mnt; - nxt_process_automount_t *automount; + nxt_array_t *mounts; + nxt_runtime_t *rt; + nxt_fs_mount_t *mnt; + nxt_process_automount_t *automount; rt = task->thread->runtime; @@ -733,9 +722,9 @@ nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process) nxt_debug(task, "unmount all (%s)", process->name); automount = &process->isolation.automount; - mounts = process->isolation.mounts; - n = mounts->nelts; - mnt = mounts->elts; + mounts = process->isolation.mounts; + n = mounts->nelts; + mnt = mounts->elts; while (n > 0) { n--; @@ -748,22 +737,20 @@ nxt_isolation_unmount_all(nxt_task_t *task, nxt_process_t *process) } } - nxt_int_t -nxt_isolation_prepare_rootfs(nxt_task_t *task, nxt_process_t *process) -{ +nxt_isolation_prepare_rootfs(nxt_task_t *task, nxt_process_t *process) { size_t i, n; nxt_int_t ret; struct stat st; - nxt_array_t *mounts; - const u_char *dst; - nxt_fs_mount_t *mnt; - nxt_process_automount_t *automount; + nxt_array_t *mounts; + const u_char *dst; + nxt_fs_mount_t *mnt; + nxt_process_automount_t *automount; automount = &process->isolation.automount; - mounts = process->isolation.mounts; + mounts = process->isolation.mounts; - n = mounts->nelts; + n = mounts->nelts; mnt = mounts->elts; for (i = 0; i < n; i++) { @@ -774,8 +761,7 @@ nxt_isolation_prepare_rootfs(nxt_task_t *task, nxt_process_t *process) } if (nxt_slow_path(mnt[i].type == NXT_FS_BIND - && stat((const char *) mnt[i].src, &st) != 0)) - { + && stat((const char *) mnt[i].src, &st) != 0)) { nxt_log(task, NXT_LOG_WARN, "host path not found: %s", mnt[i].src); continue; } @@ -809,10 +795,9 @@ nxt_isolation_prepare_rootfs(nxt_task_t *task, nxt_process_t *process) #if (NXT_HAVE_LINUX_PIVOT_ROOT) && (NXT_HAVE_CLONE_NEWNS) nxt_int_t -nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) -{ - char *rootfs; - nxt_int_t ret; +nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) { + char *rootfs; + nxt_int_t ret; rootfs = (char *) process->isolation.rootfs; @@ -835,22 +820,20 @@ nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) return ret; } - /* * pivot_root(2) can only be safely used with containers, otherwise it can * umount(2) the global root filesystem and screw up the machine. */ static nxt_int_t -nxt_isolation_pivot_root(nxt_task_t *task, const char *path) -{ +nxt_isolation_pivot_root(nxt_task_t *task, const char *path) { /* * This implementation makes use of a kernel trick that works for ages * and now documented in Linux kernel 5. * https://lore.kernel.org/linux-man/87r24piwhm.fsf@x220.int.ebiederm.org/T/ */ - if (nxt_slow_path(mount("", "/", "", MS_SLAVE|MS_REC, "") != 0)) { + if (nxt_slow_path(mount("", "/", "", MS_SLAVE | MS_REC, "") != 0)) { nxt_alert(task, "mount(\"/\", MS_SLAVE|MS_REC) failed: %E", nxt_errno); return NXT_ERROR; } @@ -859,7 +842,7 @@ nxt_isolation_pivot_root(nxt_task_t *task, const char *path) return NXT_ERROR; } - if (nxt_slow_path(mount(path, path, "bind", MS_BIND|MS_REC, "") != 0)) { + if (nxt_slow_path(mount(path, path, "bind", MS_BIND | MS_REC, "") != 0)) { nxt_alert(task, "error bind mounting rootfs %E", nxt_errno); return NXT_ERROR; } @@ -891,24 +874,22 @@ nxt_isolation_pivot_root(nxt_task_t *task, const char *path) return NXT_OK; } - static nxt_int_t -nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) -{ - char *parent_mnt; - FILE *procfile; - u_char **mounts; +nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) { + char *parent_mnt; + FILE *procfile; + u_char **mounts; size_t len; - uint8_t *shared; + uint8_t *shared; nxt_int_t ret, index, nmounts; - struct mntent *ent; + struct mntent *ent; - static const char *mount_path = "/proc/self/mounts"; + static const char *mount_path = "/proc/self/mounts"; - ret = NXT_ERROR; - ent = NULL; - shared = NULL; - procfile = NULL; + ret = NXT_ERROR; + ent = NULL; + shared = NULL; + procfile = NULL; parent_mnt = NULL; nmounts = 256; @@ -934,7 +915,7 @@ nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) again: - for ( ; index < nmounts; index++) { + for (; index < nmounts; index++) { ent = getmntent(procfile); if (ent == NULL) { nmounts = index; @@ -949,7 +930,7 @@ nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) /* there are still entries to be read */ nmounts *= 2; - mounts = nxt_realloc(mounts, nmounts); + mounts = nxt_realloc(mounts, nmounts); if (nxt_slow_path(mounts == NULL)) { goto fail; } @@ -985,7 +966,7 @@ nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) len--; } - for ( ;; ) { + for (;;) { for (index = 0; index < nmounts; index++) { if (nxt_strcmp(mounts[index], parent_mnt) == 0) { goto found; @@ -1020,7 +1001,7 @@ nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) if (shared[index]) { if (nxt_slow_path(mount("", parent_mnt, "", MS_PRIVATE, "") != 0)) { nxt_alert(task, "mount(\"\", \"%s\", MS_PRIVATE) %E", parent_mnt, - nxt_errno); + nxt_errno); goto fail; } @@ -1053,10 +1034,8 @@ nxt_isolation_make_private_mount(nxt_task_t *task, const char *rootfs) return ret; } - nxt_inline int -nxt_pivot_root(const char *new_root, const char *old_root) -{ +nxt_pivot_root(const char *new_root, const char *old_root) { return syscall(SYS_pivot_root, new_root, old_root); } @@ -1065,9 +1044,8 @@ nxt_pivot_root(const char *new_root, const char *old_root) nxt_int_t -nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) -{ - char *rootfs; +nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) { + char *rootfs; rootfs = (char *) process->isolation.rootfs; @@ -1089,8 +1067,7 @@ nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process) static nxt_int_t -nxt_isolation_chroot(nxt_task_t *task, const char *path) -{ +nxt_isolation_chroot(nxt_task_t *task, const char *path) { if (nxt_slow_path(chroot(path) < 0)) { nxt_alert(task, "chroot(%s) %E", path, nxt_errno); return NXT_ERROR; @@ -1106,11 +1083,10 @@ nxt_isolation_chroot(nxt_task_t *task, const char *path) static nxt_int_t nxt_isolation_set_new_privs(nxt_task_t *task, nxt_conf_value_t *isolation, - nxt_process_t *process) -{ - nxt_conf_value_t *obj; + nxt_process_t *process) { + nxt_conf_value_t *obj; - static const nxt_str_t new_privs_name = nxt_string("new_privs"); + static const nxt_str_t new_privs_name = nxt_string("new_privs"); obj = nxt_conf_get_object_member(isolation, &new_privs_name, NULL); if (obj != NULL) { diff --git a/src/nxt_isolation.h b/src/nxt_isolation.h index b1bfc33c7..032d913b8 100644 --- a/src/nxt_isolation.h +++ b/src/nxt_isolation.h @@ -6,13 +6,15 @@ #define _NXT_ISOLATION_H_INCLUDED_ -nxt_int_t nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, +nxt_int_t +nxt_isolation_main_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp); #if (NXT_HAVE_ISOLATION_ROOTFS) -nxt_int_t nxt_isolation_prepare_rootfs(nxt_task_t *task, - nxt_process_t *process); -nxt_int_t nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process); +nxt_int_t +nxt_isolation_prepare_rootfs(nxt_task_t *task, nxt_process_t *process); +nxt_int_t +nxt_isolation_change_root(nxt_task_t *task, nxt_process_t *process); #endif #endif /* _NXT_ISOLATION_H_INCLUDED_ */ diff --git a/src/nxt_java.c b/src/nxt_java.c index 75c8ee198..2df382bb6 100644 --- a/src/nxt_java.c +++ b/src/nxt_java.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -29,33 +28,41 @@ #include NXT_JAVA_MOUNTS_H -static nxt_int_t nxt_java_setup(nxt_task_t *task, nxt_process_t *process, +static nxt_int_t +nxt_java_setup(nxt_task_t *task, nxt_process_t *process, nxt_common_app_conf_t *conf); -static nxt_int_t nxt_java_start(nxt_task_t *task, - nxt_process_data_t *data); -static void nxt_java_request_handler(nxt_unit_request_info_t *req); -static void nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws); -static void nxt_java_close_handler(nxt_unit_request_info_t *req); -static int nxt_java_ready_handler(nxt_unit_ctx_t *ctx); -static void *nxt_java_thread_func(void *main_ctx); -static int nxt_java_init_threads(nxt_java_app_conf_t *c); -static void nxt_java_join_threads(nxt_unit_ctx_t *ctx, - nxt_java_app_conf_t *c); - -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static nxt_int_t +nxt_java_start(nxt_task_t *task, nxt_process_data_t *data); +static void +nxt_java_request_handler(nxt_unit_request_info_t *req); +static void +nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws); +static void +nxt_java_close_handler(nxt_unit_request_info_t *req); +static int +nxt_java_ready_handler(nxt_unit_ctx_t *ctx); +static void * +nxt_java_thread_func(void *main_ctx); +static int +nxt_java_init_threads(nxt_java_app_conf_t *c); +static void +nxt_java_join_threads(nxt_unit_ctx_t *ctx, nxt_java_app_conf_t *c); + +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -char *nxt_java_modules; +char *nxt_java_modules; -static pthread_t *nxt_java_threads; -static pthread_attr_t *nxt_java_thread_attr; +static pthread_t *nxt_java_threads; +static pthread_attr_t *nxt_java_thread_attr; -#define NXT_STRING(x) _NXT_STRING(x) -#define _NXT_STRING(x) #x +#define NXT_STRING(x) _NXT_STRING(x) +#define _NXT_STRING(x) #x -NXT_EXPORT nxt_app_module_t nxt_app_module = { +NXT_EXPORT nxt_app_module_t nxt_app_module = { sizeof(compat), compat, nxt_string("java"), @@ -67,22 +74,20 @@ NXT_EXPORT nxt_app_module_t nxt_app_module = { }; typedef struct { - JavaVM *jvm; + JavaVM *jvm; jobject cl; jobject ctx; - nxt_java_app_conf_t *conf; + nxt_java_app_conf_t *conf; } nxt_java_data_t; - static nxt_int_t nxt_java_setup(nxt_task_t *task, nxt_process_t *process, - nxt_common_app_conf_t *conf) -{ - char *path, *relpath, *p, *rootfs; + nxt_common_app_conf_t *conf) { + char *path, *relpath, *p, *rootfs; size_t jars_dir_len, rootfs_len; - const char *unit_jars; + const char *unit_jars; - rootfs = (char *) process->isolation.rootfs; + rootfs = (char *) process->isolation.rootfs; rootfs_len = 0; unit_jars = conf->u.java.unit_jars; @@ -101,7 +106,7 @@ nxt_java_setup(nxt_task_t *task, nxt_process_t *process, if (rootfs != NULL) { jars_dir_len = strlen(unit_jars); - rootfs_len = strlen(rootfs); + rootfs_len = strlen(rootfs); path = nxt_malloc(jars_dir_len + rootfs_len + 1); if (nxt_slow_path(path == NULL)) { @@ -109,8 +114,8 @@ nxt_java_setup(nxt_task_t *task, nxt_process_t *process, return NXT_ERROR; } - p = nxt_cpymem(path, process->isolation.rootfs, rootfs_len); - p = nxt_cpymem(p, relpath, jars_dir_len); + p = nxt_cpymem(path, process->isolation.rootfs, rootfs_len); + p = nxt_cpymem(p, relpath, jars_dir_len); *p = '\0'; free(relpath); @@ -140,16 +145,14 @@ nxt_java_setup(nxt_task_t *task, nxt_process_t *process, return NXT_ERROR; } - static char ** -nxt_java_module_jars(const char *jars[], int jar_count) -{ - char **res, *jurl; - uint8_t pathsep; - nxt_int_t modules_len, jlen, i; - const char **jar; - - res = nxt_malloc(jar_count * sizeof(char*)); +nxt_java_module_jars(const char *jars[], int jar_count) { + char **res, *jurl; + uint8_t pathsep; + nxt_int_t modules_len, jlen, i; + const char **jar; + + res = nxt_malloc(jar_count * sizeof(char *)); if (res == NULL) { return NULL; } @@ -160,8 +163,7 @@ nxt_java_module_jars(const char *jars[], int jar_count) for (i = 0, jar = jars; *jar != NULL; jar++) { jlen = nxt_length("file:") + modules_len - + (!pathsep ? nxt_length("/") : 0) - + nxt_strlen(*jar) + 1; + + (!pathsep ? nxt_length("/") : 0) + nxt_strlen(*jar) + 1; jurl = nxt_malloc(jlen); if (jurl == NULL) { @@ -177,44 +179,42 @@ nxt_java_module_jars(const char *jars[], int jar_count) *jurl++ = '/'; } - jurl = nxt_cpymem(jurl, *jar, nxt_strlen(*jar)); + jurl = nxt_cpymem(jurl, *jar, nxt_strlen(*jar)); *jurl++ = '\0'; } return res; } - static nxt_int_t -nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) { jint rc; - char *opt, *real_path; - char **classpath_arr, **unit_jars, **system_jars; - JavaVM *jvm; - JNIEnv *env; + char *opt, *real_path; + char **classpath_arr, **unit_jars, **system_jars; + JavaVM *jvm; + JNIEnv *env; jobject cl, classpath; nxt_str_t str; nxt_int_t opt_len, real_path_len; nxt_uint_t i, unit_jars_count, classpath_count; nxt_uint_t system_jars_count; - JavaVMOption *jvm_opt; + JavaVMOption *jvm_opt; JavaVMInitArgs jvm_args; - nxt_unit_ctx_t *ctx; + nxt_unit_ctx_t *ctx; nxt_unit_init_t java_init; nxt_java_data_t java_data; - nxt_conf_value_t *value; - nxt_java_app_conf_t *c; - nxt_common_app_conf_t *app_conf; + nxt_conf_value_t *value; + nxt_java_app_conf_t *c; + nxt_common_app_conf_t *app_conf; - //setenv("ASAN_OPTIONS", "handle_segv=0", 1); + // setenv("ASAN_OPTIONS", "handle_segv=0", 1); - jvm_args.version = JNI_VERSION_1_6; - jvm_args.nOptions = 0; + jvm_args.version = JNI_VERSION_1_6; + jvm_args.nOptions = 0; jvm_args.ignoreUnrecognized = 0; app_conf = data->app; - c = &app_conf->u.java; + c = &app_conf->u.java; if (c->options != NULL) { jvm_args.nOptions += nxt_conf_array_elements_count(c->options); @@ -247,8 +247,7 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) } if (c->options != NULL) { - - for (i = 0; /* void */ ; i++) { + for (i = 0; /* void */; i++) { value = nxt_conf_get_array_element(c->options, i); if (value == NULL) { break; @@ -271,9 +270,9 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) if (c->classpath != NULL) { classpath_count = nxt_conf_array_elements_count(c->classpath); - classpath_arr = nxt_malloc(classpath_count * sizeof(char *)); + classpath_arr = nxt_malloc(classpath_count * sizeof(char *)); - for (i = 0; /* void */ ; i++) { + for (i = 0; /* void */; i++) { value = nxt_conf_get_array_element(c->classpath, i); if (value == NULL) { break; @@ -317,7 +316,7 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) } } else { - real_path = (char *) str.start; /* I love this cast! */ + real_path = (char *) str.start; /* I love this cast! */ real_path_len = str.length; } @@ -327,13 +326,13 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) opt = nxt_cpymem(opt, "file:", nxt_length("file:")); } - opt = nxt_cpymem(opt, real_path, real_path_len); + opt = nxt_cpymem(opt, real_path, real_path_len); *opt = '\0'; } } else { classpath_count = 0; - classpath_arr = NULL; + classpath_arr = NULL; } rc = JNI_CreateJavaVM(&jvm, (void **) &env, &jvm_args); @@ -412,9 +411,9 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) goto env_failed; } - java_data.jvm = jvm; - java_data.cl = cl; - java_data.ctx = nxt_java_startContext(env, c->webapp, classpath); + java_data.jvm = jvm; + java_data.cl = cl; + java_data.ctx = nxt_java_startContext(env, c->webapp, classpath); java_data.conf = c; if ((*env)->ExceptionCheck(env)) { @@ -430,13 +429,13 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) nxt_unit_default_init(task, &java_init, app_conf); - java_init.callbacks.request_handler = nxt_java_request_handler; + java_init.callbacks.request_handler = nxt_java_request_handler; java_init.callbacks.websocket_handler = nxt_java_websocket_handler; - java_init.callbacks.close_handler = nxt_java_close_handler; - java_init.callbacks.ready_handler = nxt_java_ready_handler; - java_init.request_data_size = sizeof(nxt_java_request_data_t); - java_init.data = &java_data; - java_init.ctx_data = env; + java_init.callbacks.close_handler = nxt_java_close_handler; + java_init.callbacks.ready_handler = nxt_java_ready_handler; + java_init.request_data_size = sizeof(nxt_java_request_data_t); + java_init.data = &java_data; + java_init.ctx_data = env; ctx = nxt_unit_init(&java_init); if (nxt_slow_path(ctx == NULL)) { @@ -471,18 +470,16 @@ nxt_java_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - static void -nxt_java_request_handler(nxt_unit_request_info_t *req) -{ - JNIEnv *env; +nxt_java_request_handler(nxt_unit_request_info_t *req) { + JNIEnv *env; jobject jreq, jresp; - nxt_java_data_t *java_data; - nxt_java_request_data_t *data; + nxt_java_data_t *java_data; + nxt_java_request_data_t *data; java_data = req->unit->data; - env = req->ctx->data; - data = req->data; + env = req->ctx->data; + data = req->data; jreq = nxt_java_newRequest(env, java_data->ctx, req); if (jreq == NULL) { @@ -513,10 +510,10 @@ nxt_java_request_handler(nxt_unit_request_info_t *req) } data->header_size = 10 * 1024; - data->buf_size = 32 * 1024; /* from Jetty */ - data->jreq = jreq; - data->jresp = jresp; - data->buf = NULL; + data->buf_size = 32 * 1024; /* from Jetty */ + data->jreq = jreq; + data->jresp = jresp; + data->buf = NULL; nxt_unit_request_group_dup_fields(req); @@ -542,7 +539,7 @@ nxt_java_request_handler(nxt_unit_request_info_t *req) } if (nxt_unit_response_is_websocket(req)) { - data->jreq = (*env)->NewGlobalRef(env, jreq); + data->jreq = (*env)->NewGlobalRef(env, jreq); data->jresp = (*env)->NewGlobalRef(env, jresp); } else { @@ -553,16 +550,14 @@ nxt_java_request_handler(nxt_unit_request_info_t *req) (*env)->DeleteLocalRef(env, jreq); } - static void -nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws) -{ - void *b; - JNIEnv *env; +nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws) { + void *b; + JNIEnv *env; jobject jbuf; - nxt_java_request_data_t *data; + nxt_java_request_data_t *data; - env = ws->req->ctx->data; + env = ws->req->ctx->data; data = ws->req->data; b = malloc(ws->payload_len); @@ -572,7 +567,7 @@ nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws) jbuf = (*env)->NewDirectByteBuffer(env, b, ws->payload_len); if (jbuf != NULL) { nxt_java_Request_websocket(env, data->jreq, jbuf, - ws->header->opcode, ws->header->fin); + ws->header->opcode, ws->header->fin); if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionDescribe(env); @@ -588,14 +583,12 @@ nxt_java_websocket_handler(nxt_unit_websocket_frame_t *ws) nxt_unit_websocket_done(ws); } - static void -nxt_java_close_handler(nxt_unit_request_info_t *req) -{ - JNIEnv *env; - nxt_java_request_data_t *data; +nxt_java_close_handler(nxt_unit_request_info_t *req) { + JNIEnv *env; + nxt_java_request_data_t *data; - env = req->ctx->data; + env = req->ctx->data; data = req->data; nxt_java_Request_close(env, data->jreq); @@ -606,17 +599,15 @@ nxt_java_close_handler(nxt_unit_request_info_t *req) nxt_unit_request_done(req, NXT_UNIT_OK); } - static int -nxt_java_ready_handler(nxt_unit_ctx_t *ctx) -{ +nxt_java_ready_handler(nxt_unit_ctx_t *ctx) { int res; uint32_t i; - nxt_java_data_t *java_data; - nxt_java_app_conf_t *c; + nxt_java_data_t *java_data; + nxt_java_app_conf_t *c; java_data = ctx->unit->data; - c = java_data->conf; + c = java_data->conf; if (c->threads <= 1) { return NXT_UNIT_OK; @@ -624,14 +615,14 @@ nxt_java_ready_handler(nxt_unit_ctx_t *ctx) for (i = 0; i < c->threads - 1; i++) { res = pthread_create(&nxt_java_threads[i], nxt_java_thread_attr, - nxt_java_thread_func, ctx); + nxt_java_thread_func, ctx); if (nxt_fast_path(res == 0)) { nxt_unit_debug(ctx, "thread #%d created", (int) (i + 1)); } else { nxt_unit_alert(ctx, "thread #%d create failed: %s (%d)", - (int) (i + 1), strerror(res), res); + (int) (i + 1), strerror(res), res); return NXT_UNIT_ERROR; } @@ -640,22 +631,20 @@ nxt_java_ready_handler(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static void * -nxt_java_thread_func(void *data) -{ +nxt_java_thread_func(void *data) { int rc; - JavaVM *jvm; - JNIEnv *env; - nxt_unit_ctx_t *main_ctx, *ctx; - nxt_java_data_t *java_data; + JavaVM *jvm; + JNIEnv *env; + nxt_unit_ctx_t *main_ctx, *ctx; + nxt_java_data_t *java_data; main_ctx = data; nxt_unit_debug(main_ctx, "worker thread start"); java_data = main_ctx->unit->data; - jvm = java_data->jvm; + jvm = java_data->jvm; rc = (*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL); if (rc != JNI_OK) { @@ -683,12 +672,10 @@ nxt_java_thread_func(void *data) return NULL; } - static int -nxt_java_init_threads(nxt_java_app_conf_t *c) -{ - int res; - static pthread_attr_t attr; +nxt_java_init_threads(nxt_java_app_conf_t *c) { + int res; + static pthread_attr_t attr; if (c->threads <= 1) { return NXT_UNIT_OK; @@ -698,7 +685,7 @@ nxt_java_init_threads(nxt_java_app_conf_t *c) res = pthread_attr_init(&attr); if (nxt_slow_path(res != 0)) { nxt_unit_alert(NULL, "thread attr init failed: %s (%d)", - strerror(res), res); + strerror(res), res); return NXT_UNIT_ERROR; } @@ -706,7 +693,7 @@ nxt_java_init_threads(nxt_java_app_conf_t *c) res = pthread_attr_setstacksize(&attr, c->thread_stack_size); if (nxt_slow_path(res != 0)) { nxt_unit_alert(NULL, "thread attr set stack size failed: %s (%d)", - strerror(res), res); + strerror(res), res); return NXT_UNIT_ERROR; } @@ -714,8 +701,8 @@ nxt_java_init_threads(nxt_java_app_conf_t *c) nxt_java_thread_attr = &attr; } - nxt_java_threads = nxt_unit_malloc(NULL, - sizeof(pthread_t) * (c->threads - 1)); + nxt_java_threads + = nxt_unit_malloc(NULL, sizeof(pthread_t) * (c->threads - 1)); if (nxt_slow_path(nxt_java_threads == NULL)) { nxt_unit_alert(NULL, "Failed to allocate thread id array"); @@ -727,12 +714,10 @@ nxt_java_init_threads(nxt_java_app_conf_t *c) return NXT_UNIT_OK; } - static void -nxt_java_join_threads(nxt_unit_ctx_t *ctx, nxt_java_app_conf_t *c) -{ - int res; - uint32_t i; +nxt_java_join_threads(nxt_unit_ctx_t *ctx, nxt_java_app_conf_t *c) { + int res; + uint32_t i; if (nxt_java_threads == NULL) { return; @@ -749,12 +734,10 @@ nxt_java_join_threads(nxt_unit_ctx_t *ctx, nxt_java_app_conf_t *c) nxt_unit_debug(ctx, "thread #%d joined", (int) i); } else { - nxt_unit_alert(ctx, "thread #%d join failed: %s (%d)", - (int) i, strerror(res), res); + nxt_unit_alert(ctx, "thread #%d join failed: %s (%d)", (int) i, + strerror(res), res); } } nxt_unit_free(ctx, nxt_java_threads); } - - diff --git a/src/nxt_job.c b/src/nxt_job.c index 560739537..8b954abe2 100644 --- a/src/nxt_job.c +++ b/src/nxt_job.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,16 +6,15 @@ #include -static void nxt_job_thread_trampoline(nxt_task_t *task, void *obj, void *data); -static void nxt_job_thread_return_handler(nxt_task_t *task, void *obj, - void *data); - +static void +nxt_job_thread_trampoline(nxt_task_t *task, void *obj, void *data); +static void +nxt_job_thread_return_handler(nxt_task_t *task, void *obj, void *data); void * -nxt_job_create(nxt_mp_t *mp, size_t size) -{ +nxt_job_create(nxt_mp_t *mp, size_t size) { size_t cache_size; - nxt_job_t *job; + nxt_job_t *job; if (mp == NULL) { mp = nxt_mp_create(1024, 128, 256, 32); @@ -24,11 +22,11 @@ nxt_job_create(nxt_mp_t *mp, size_t size) return NULL; } - job = nxt_mp_zget(mp, size); + job = nxt_mp_zget(mp, size); cache_size = 0; } else { - job = nxt_mp_zalloc(mp, size); + job = nxt_mp_zalloc(mp, size); cache_size = size; } @@ -37,7 +35,7 @@ nxt_job_create(nxt_mp_t *mp, size_t size) } job->cache_size = (uint16_t) cache_size; - job->mem_pool = mp; + job->mem_pool = mp; nxt_job_set_name(job, "job"); /* Allow safe nxt_queue_remove() in nxt_job_destroy(). */ @@ -46,10 +44,8 @@ nxt_job_create(nxt_mp_t *mp, size_t size) return job; } - void -nxt_job_init(nxt_job_t *job, size_t size) -{ +nxt_job_init(nxt_job_t *job, size_t size) { nxt_memzero(job, size); nxt_job_set_name(job, "job"); @@ -57,18 +53,15 @@ nxt_job_init(nxt_job_t *job, size_t size) nxt_queue_self(&job->link); } - void -nxt_job_destroy(nxt_task_t *task, void *data) -{ - nxt_job_t *job; +nxt_job_destroy(nxt_task_t *task, void *data) { + nxt_job_t *job; job = data; nxt_queue_remove(&job->link); if (job->cache_size == 0) { - if (job->mem_pool != NULL) { nxt_mp_destroy(job->mem_pool); } @@ -106,17 +99,16 @@ nxt_job_cleanup_add(nxt_mp_t *mp, nxt_job_t *job) */ void -nxt_job_start(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) -{ +nxt_job_start(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) { nxt_debug(task, "%s start", job->name); if (job->thread_pool != NULL) { - nxt_int_t ret; + nxt_int_t ret; job->engine = task->thread->engine; - nxt_work_set(&job->work, nxt_job_thread_trampoline, - job->task, job, (void *) handler); + nxt_work_set(&job->work, nxt_job_thread_trampoline, job->task, job, + (void *) handler); ret = nxt_thread_pool_post(job->thread_pool, &job->work); @@ -130,16 +122,14 @@ nxt_job_start(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) handler(job->task, job, job->data); } - /* A trampoline function is called by a thread pool thread. */ static void -nxt_job_thread_trampoline(nxt_task_t *task, void *obj, void *data) -{ - nxt_job_t *job; - nxt_work_handler_t handler; +nxt_job_thread_trampoline(nxt_task_t *task, void *obj, void *data) { + nxt_job_t *job; + nxt_work_handler_t handler; - job = obj; + job = obj; handler = (nxt_work_handler_t) data; nxt_debug(task, "%s thread", job->name); @@ -152,17 +142,15 @@ nxt_job_thread_trampoline(nxt_task_t *task, void *obj, void *data) } } - void -nxt_job_return(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) -{ +nxt_job_return(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) { nxt_debug(task, "%s return", job->name); if (job->engine != NULL) { /* A return function is called in thread pool thread context. */ - nxt_work_set(&job->work, nxt_job_thread_return_handler, - job->task, job, (void *) handler); + nxt_work_set(&job->work, nxt_job_thread_return_handler, job->task, job, + (void *) handler); nxt_event_engine_post(job->engine, &job->work); @@ -174,18 +162,16 @@ nxt_job_return(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler) handler = job->abort_handler; } - nxt_work_queue_add(&task->thread->engine->fast_work_queue, - handler, job->task, job, job->data); + nxt_work_queue_add(&task->thread->engine->fast_work_queue, handler, + job->task, job, job->data); } - static void -nxt_job_thread_return_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_job_t *job; - nxt_work_handler_t handler; +nxt_job_thread_return_handler(nxt_task_t *task, void *obj, void *data) { + nxt_job_t *job; + nxt_work_handler_t handler; - job = obj; + job = obj; handler = (nxt_work_handler_t) data; job->task->thread = task->thread; diff --git a/src/nxt_job.h b/src/nxt_job.h index 0495c4844..b1cc3d218 100644 --- a/src/nxt_job.h +++ b/src/nxt_job.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_JOB_H_INCLUDED_ #define _NXT_JOB_H_INCLUDED_ - /* * A job may run by separate thread, so each job should have its * its own mem_pool. A job itself is allocated from this mem_pool. @@ -31,50 +29,51 @@ typedef struct { - void *data; + void *data; - nxt_task_t *task; + nxt_task_t *task; - nxt_work_handler_t abort_handler; + nxt_work_handler_t abort_handler; - uint16_t cache_size; - uint8_t cancel; /* 1 bit */ + uint16_t cache_size; + uint8_t cancel; /* 1 bit */ - nxt_mp_t *mem_pool; - nxt_queue_link_t link; + nxt_mp_t *mem_pool; + nxt_queue_link_t link; - nxt_thread_pool_t *thread_pool; - nxt_event_engine_t *engine; - nxt_log_t *log; + nxt_thread_pool_t *thread_pool; + nxt_event_engine_t *engine; + nxt_log_t *log; - nxt_work_t work; + nxt_work_t work; #if (NXT_DEBUG) - const char *name; + const char *name; #endif } nxt_job_t; +NXT_EXPORT void * +nxt_job_create(nxt_mp_t *mp, size_t size); +NXT_EXPORT void +nxt_job_init(nxt_job_t *job, size_t size); +NXT_EXPORT void +nxt_job_destroy(nxt_task_t *task, void *data); +NXT_EXPORT nxt_int_t +nxt_job_cleanup_add(nxt_mp_t *mp, nxt_job_t *job); -NXT_EXPORT void *nxt_job_create(nxt_mp_t *mp, size_t size); -NXT_EXPORT void nxt_job_init(nxt_job_t *job, size_t size); -NXT_EXPORT void nxt_job_destroy(nxt_task_t *task, void *data); -NXT_EXPORT nxt_int_t nxt_job_cleanup_add(nxt_mp_t *mp, nxt_job_t *job); - -NXT_EXPORT void nxt_job_start(nxt_task_t *task, nxt_job_t *job, - nxt_work_handler_t handler); -NXT_EXPORT void nxt_job_return(nxt_task_t *task, nxt_job_t *job, - nxt_work_handler_t handler); +NXT_EXPORT void +nxt_job_start(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler); +NXT_EXPORT void +nxt_job_return(nxt_task_t *task, nxt_job_t *job, nxt_work_handler_t handler); -#define nxt_job_cancel(job) \ - (job)->cancel = 1 +#define nxt_job_cancel(job) (job)->cancel = 1 #if (NXT_DEBUG) -#define nxt_job_set_name(job, text) \ - (job)->name = text +#define nxt_job_set_name(job, text) (job)->name = text #else diff --git a/src/nxt_job_cache_file.c b/src/nxt_job_cache_file.c index 13f01a453..ad8e76fc3 100644 --- a/src/nxt_job_cache_file.c +++ b/src/nxt_job_cache_file.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,16 +6,14 @@ #include - nxt_job_cache_file_t * -nxt_job_cache_file_create(nxt_mp_t *mp) -{ - nxt_job_cache_file_t *jbc; +nxt_job_cache_file_create(nxt_mp_t *mp) { + nxt_job_cache_file_t *jbc; jbc = nxt_job_create(mp, sizeof(nxt_job_cache_file_t)); if (nxt_fast_path(jbc != NULL)) { - jbc->file.fd = NXT_FILE_INVALID; + jbc->file.fd = NXT_FILE_INVALID; jbc->read_required = nxt_job_file_read_required; } diff --git a/src/nxt_js.c b/src/nxt_js.c index 0482482ae..d9d089ba8 100644 --- a/src/nxt_js.c +++ b/src/nxt_js.c @@ -1,47 +1,42 @@ - /* * Copyright (C) NGINX, Inc. */ #include - struct nxt_js_s { - uint32_t index; + uint32_t index; }; - typedef struct { - nxt_str_t name; - nxt_str_t text; + nxt_str_t name; + nxt_str_t text; } nxt_js_module_t; - struct nxt_js_conf_s { - nxt_mp_t *pool; - njs_vm_t *vm; - njs_uint_t protos; - njs_external_t *proto; - nxt_str_t init; - nxt_array_t *modules; /* of nxt_js_module_t */ - nxt_array_t *funcs; - uint8_t test; /* 1 bit */ + nxt_mp_t *pool; + njs_vm_t *vm; + njs_uint_t protos; + njs_external_t *proto; + nxt_str_t init; + nxt_array_t *modules; /* of nxt_js_module_t */ + nxt_array_t *funcs; + uint8_t test; /* 1 bit */ }; - njs_mod_t * -nxt_js_module_loader(njs_vm_t *vm, njs_external_ptr_t external, njs_str_t *name) -{ +nxt_js_module_loader(njs_vm_t *vm, njs_external_ptr_t external, + njs_str_t *name) { nxt_str_t text; nxt_uint_t i, n; - nxt_js_conf_t *jcf; - nxt_js_module_t *modules, *module; + nxt_js_conf_t *jcf; + nxt_js_module_t *modules, *module; jcf = external; module = NULL; - n = jcf->modules->nelts; + n = jcf->modules->nelts; modules = jcf->modules->elts; for (i = 0; i < n; i++) { @@ -65,17 +60,14 @@ nxt_js_module_loader(njs_vm_t *vm, njs_external_ptr_t external, njs_str_t *name) nxt_memcpy(text.start, module->text.start, text.length); return njs_vm_compile_module(vm, name, &text.start, - &text.start[text.length]); + &text.start[text.length]); } - -njs_int_t nxt_js_proto_id; - +njs_int_t nxt_js_proto_id; nxt_js_conf_t * -nxt_js_conf_new(nxt_mp_t *mp, nxt_bool_t test) -{ - nxt_js_conf_t *jcf; +nxt_js_conf_new(nxt_mp_t *mp, nxt_bool_t test) { + nxt_js_conf_t *jcf; jcf = nxt_mp_zget(mp, sizeof(nxt_js_conf_t)); if (nxt_slow_path(jcf == NULL)) { @@ -98,41 +90,35 @@ nxt_js_conf_new(nxt_mp_t *mp, nxt_bool_t test) return jcf; } - void -nxt_js_conf_release(nxt_js_conf_t *jcf) -{ +nxt_js_conf_release(nxt_js_conf_t *jcf) { njs_vm_destroy(jcf->vm); } - void -nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, njs_uint_t n) -{ +nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, njs_uint_t n) { jcf->protos = n; - jcf->proto = proto; + jcf->proto = proto; } - static njs_vm_t * -nxt_js_vm_create(nxt_js_conf_t *jcf) -{ - u_char *p; +nxt_js_vm_create(nxt_js_conf_t *jcf) { + u_char *p; size_t size; - njs_vm_t *vm; + njs_vm_t *vm; nxt_uint_t i; njs_vm_opt_t opts; - nxt_js_module_t *module, *mod; + nxt_js_module_t *module, *mod; - static const nxt_str_t import_str = nxt_string("import"); - static const nxt_str_t from_str = nxt_string("from"); - static const nxt_str_t global_str = nxt_string("globalThis"); + static const nxt_str_t import_str = nxt_string("import"); + static const nxt_str_t from_str = nxt_string("from"); + static const nxt_str_t global_str = nxt_string("globalThis"); njs_vm_opt_init(&opts); opts.backtrace = 1; - opts.file.start = (u_char *) "default"; + opts.file.start = (u_char *) "default"; opts.file.length = 7; if (jcf->test || jcf->modules->nelts == 0) { @@ -141,17 +127,17 @@ nxt_js_vm_create(nxt_js_conf_t *jcf) opts.external = jcf; - size = 0; + size = 0; module = jcf->modules->elts; for (i = 0; i < jcf->modules->nelts; i++) { mod = &module[i]; - size += import_str.length + 1 + mod->name.length + 1 - + from_str.length + 2 + mod->name.length + 3; + size += import_str.length + 1 + mod->name.length + 1 + from_str.length + + 2 + mod->name.length + 3; - size += global_str.length + 1 + mod->name.length + 3 - + mod->name.length + 2; + size += global_str.length + 1 + mod->name.length + 3 + mod->name.length + + 2; } p = nxt_mp_nget(jcf->pool, size); @@ -160,35 +146,35 @@ nxt_js_vm_create(nxt_js_conf_t *jcf) } jcf->init.length = size; - jcf->init.start = p; + jcf->init.start = p; for (i = 0; i < jcf->modules->nelts; i++) { mod = &module[i]; - p = nxt_cpymem(p, import_str.start, import_str.length); + p = nxt_cpymem(p, import_str.start, import_str.length); *p++ = ' '; - p = nxt_cpymem(p, mod->name.start, mod->name.length); + p = nxt_cpymem(p, mod->name.start, mod->name.length); *p++ = ' '; - p = nxt_cpymem(p, from_str.start, from_str.length); + p = nxt_cpymem(p, from_str.start, from_str.length); *p++ = ' '; *p++ = '\"'; - p = nxt_cpymem(p, mod->name.start, mod->name.length); + p = nxt_cpymem(p, mod->name.start, mod->name.length); *p++ = '\"'; *p++ = ';'; *p++ = '\n'; - p = nxt_cpymem(p, global_str.start, global_str.length); + p = nxt_cpymem(p, global_str.start, global_str.length); *p++ = '.'; - p = nxt_cpymem(p, mod->name.start, mod->name.length); + p = nxt_cpymem(p, mod->name.start, mod->name.length); *p++ = ' '; *p++ = '='; *p++ = ' '; - p = nxt_cpymem(p, mod->name.start, mod->name.length); + p = nxt_cpymem(p, mod->name.start, mod->name.length); *p++ = ';'; *p++ = '\n'; } @@ -204,11 +190,9 @@ nxt_js_vm_create(nxt_js_conf_t *jcf) return vm; } - nxt_int_t -nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, nxt_str_t *text) -{ - nxt_js_module_t *module; +nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, nxt_str_t *text) { + nxt_js_module_t *module; module = nxt_array_add(jcf->modules); if (nxt_slow_path(module == NULL)) { @@ -218,7 +202,7 @@ nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, nxt_str_t *text) module->name = *name; module->text.length = text->length; - module->text.start = nxt_mp_nget(jcf->pool, text->length); + module->text.start = nxt_mp_nget(jcf->pool, text->length); if (nxt_slow_path(module->text.start == NULL)) { return NXT_ERROR; } @@ -228,26 +212,24 @@ nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, nxt_str_t *text) return NXT_OK; } - nxt_js_t * -nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags) -{ +nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags) { size_t size; - u_char *p, *start; - nxt_js_t *js; - nxt_str_t *func; + u_char *p, *start; + nxt_js_t *js; + nxt_str_t *func; - static const nxt_str_t func_str = - nxt_string("function(uri, host, remoteAddr, " - "args, headers, cookies, vars) {" - " return "); + static const nxt_str_t func_str + = nxt_string("function(uri, host, remoteAddr, " + "args, headers, cookies, vars) {" + " return "); /* * Append a newline character if newline is true. * Append a terminating null character if strz is true. */ - static const nxt_str_t newline_str = nxt_string(" + '\\x0A'"); - static const nxt_str_t strz_str = nxt_string(" + '\\x00'"); + static const nxt_str_t newline_str = nxt_string(" + '\\x0A'"); + static const nxt_str_t strz_str = nxt_string(" + '\\x00'"); size = func_str.length + str->length + 1; @@ -289,7 +271,7 @@ nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags) return NULL; } - func->start = start; + func->start = start; func->length = p - start; js->index = jcf->funcs->nelts - 1; @@ -297,15 +279,13 @@ nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags) return js; } - nxt_int_t -nxt_js_compile(nxt_js_conf_t *jcf) -{ - size_t size; - u_char *p, *start; - njs_int_t ret; - nxt_str_t *func; - nxt_uint_t i; +nxt_js_compile(nxt_js_conf_t *jcf) { + size_t size; + u_char *p, *start; + njs_int_t ret; + nxt_str_t *func; + nxt_uint_t i; if (jcf->test) { return NXT_OK; @@ -328,20 +308,20 @@ nxt_js_compile(nxt_js_conf_t *jcf) return NXT_ERROR; } - p = nxt_cpymem(start, jcf->init.start, jcf->init.length); + p = nxt_cpymem(start, jcf->init.start, jcf->init.length); *p++ = '['; func = jcf->funcs->elts; for (i = 0; i < jcf->funcs->nelts; i++) { - p = nxt_cpymem(p, func[i].start, func[i].length); + p = nxt_cpymem(p, func[i].start, func[i].length); *p++ = ','; } *p++ = ']'; - nxt_js_proto_id = njs_vm_external_prototype(jcf->vm, jcf->proto, - jcf->protos); + nxt_js_proto_id + = njs_vm_external_prototype(jcf->vm, jcf->proto, jcf->protos); if (nxt_slow_path(nxt_js_proto_id < 0)) { return NXT_ERROR; } @@ -351,13 +331,11 @@ nxt_js_compile(nxt_js_conf_t *jcf) return (ret == NJS_OK) ? NXT_OK : NXT_ERROR; } - nxt_int_t -nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error) -{ - u_char *start; - njs_vm_t *vm; - njs_int_t ret; +nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error) { + u_char *start; + njs_vm_t *vm; + njs_int_t ret; vm = nxt_js_vm_create(jcf); if (nxt_slow_path(vm == NULL)) { @@ -389,20 +367,18 @@ nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error) return NXT_ERROR; } - nxt_int_t nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache, - nxt_js_t *js, nxt_str_t *str, void *ctx) -{ - njs_vm_t *vm; - njs_int_t ret; - njs_str_t res; - njs_uint_t i, n; - njs_value_t *value; - njs_function_t *func; - njs_opaque_value_t retval, opaque_value, arguments[7]; - - static const njs_str_t js_args[] = { + nxt_js_t *js, nxt_str_t *str, void *ctx) { + njs_vm_t *vm; + njs_int_t ret; + njs_str_t res; + njs_uint_t i, n; + njs_value_t *value; + njs_function_t *func; + njs_opaque_value_t retval, opaque_value, arguments[7]; + + static const njs_str_t js_args[] = { njs_str("uri"), njs_str("host"), njs_str("remoteAddr"), @@ -429,10 +405,10 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache, } value = njs_vm_array_prop(vm, &cache->array, js->index, &opaque_value); - func = njs_value_function(value); + func = njs_value_function(value); ret = njs_vm_external_create(vm, njs_value_arg(&opaque_value), - nxt_js_proto_id, ctx, 0); + nxt_js_proto_id, ctx, 0); if (nxt_slow_path(ret != NJS_OK)) { return NXT_ERROR; } @@ -441,14 +417,14 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache, for (i = 0; i < n; i++) { value = njs_vm_object_prop(vm, njs_value_arg(&opaque_value), - &js_args[i], &arguments[i]); + &js_args[i], &arguments[i]); if (nxt_slow_path(value == NULL)) { return NXT_ERROR; } } ret = njs_vm_invoke(vm, func, njs_value_arg(&arguments), n, - njs_value_arg(&retval)); + njs_value_arg(&retval)); if (ret != NJS_OK) { ret = njs_vm_exception_string(vm, &res); @@ -462,34 +438,30 @@ nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache, ret = njs_vm_value_string(vm, &res, njs_value_arg(&retval)); str->length = res.length; - str->start = res.start; + str->start = res.start; return NXT_OK; } - void -nxt_js_release(nxt_js_cache_t *cache) -{ +nxt_js_release(nxt_js_cache_t *cache) { if (cache->vm != NULL) { njs_vm_destroy(cache->vm); } } - nxt_int_t -nxt_js_error(njs_vm_t *vm, u_char *error) -{ - njs_int_t ret; - njs_str_t res; - nxt_str_t err; +nxt_js_error(njs_vm_t *vm, u_char *error) { + njs_int_t ret; + njs_str_t res; + nxt_str_t err; ret = njs_vm_exception_string(vm, &res); if (nxt_slow_path(ret != NJS_OK)) { return NXT_ERROR; } - err.start = res.start; + err.start = res.start; err.length = res.length; nxt_sprintf(error, error + NXT_MAX_ERROR_STR, "\"%V\"%Z", &err); diff --git a/src/nxt_js.h b/src/nxt_js.h index 53262563e..210a37b9f 100644 --- a/src/nxt_js.h +++ b/src/nxt_js.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,33 +10,41 @@ #include -typedef struct nxt_js_s nxt_js_t; -typedef struct nxt_js_conf_s nxt_js_conf_t; - +typedef struct nxt_js_s nxt_js_t; +typedef struct nxt_js_conf_s nxt_js_conf_t; typedef struct { - njs_vm_t *vm; - njs_value_t array; + njs_vm_t *vm; + njs_value_t array; } nxt_js_cache_t; - -njs_mod_t *nxt_js_module_loader(njs_vm_t *vm, njs_external_ptr_t external, +njs_mod_t * +nxt_js_module_loader(njs_vm_t *vm, njs_external_ptr_t external, njs_str_t *name); -nxt_js_conf_t *nxt_js_conf_new(nxt_mp_t *mp, nxt_bool_t test); -void nxt_js_conf_release(nxt_js_conf_t *jcf); -void nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, nxt_uint_t n); -nxt_int_t nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, - nxt_str_t *text); -nxt_js_t *nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags); -nxt_int_t nxt_js_compile(nxt_js_conf_t *jcf); -nxt_int_t nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error); -nxt_int_t nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, - nxt_js_cache_t *cache, nxt_js_t *js, nxt_str_t *str, void *ctx); -void nxt_js_release(nxt_js_cache_t *cache); -nxt_int_t nxt_js_error(njs_vm_t *vm, u_char *error); - - -extern njs_int_t nxt_js_proto_id; +nxt_js_conf_t * +nxt_js_conf_new(nxt_mp_t *mp, nxt_bool_t test); +void +nxt_js_conf_release(nxt_js_conf_t *jcf); +void +nxt_js_set_proto(nxt_js_conf_t *jcf, njs_external_t *proto, nxt_uint_t n); +nxt_int_t +nxt_js_add_module(nxt_js_conf_t *jcf, nxt_str_t *name, nxt_str_t *text); +nxt_js_t * +nxt_js_add_tpl(nxt_js_conf_t *jcf, nxt_str_t *str, nxt_uint_t flags); +nxt_int_t +nxt_js_compile(nxt_js_conf_t *jcf); +nxt_int_t +nxt_js_test(nxt_js_conf_t *jcf, nxt_str_t *str, u_char *error); +nxt_int_t +nxt_js_call(nxt_task_t *task, nxt_js_conf_t *jcf, nxt_js_cache_t *cache, + nxt_js_t *js, nxt_str_t *str, void *ctx); +void +nxt_js_release(nxt_js_cache_t *cache); +nxt_int_t +nxt_js_error(njs_vm_t *vm, u_char *error); + + +extern njs_int_t nxt_js_proto_id; #endif /* NXT_HAVE_NJS */ diff --git a/src/nxt_kqueue_engine.c b/src/nxt_kqueue_engine.c index a7a5a29ed..d268b721a 100644 --- a/src/nxt_kqueue_engine.c +++ b/src/nxt_kqueue_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -33,92 +32,104 @@ * deallocation and probable subsequent allocation with a lock acquiring. */ #ifdef EV_DISPATCH -#define NXT_KEVENT_ONESHOT EV_DISPATCH +#define NXT_KEVENT_ONESHOT EV_DISPATCH #else -#define NXT_KEVENT_ONESHOT EV_ONESHOT +#define NXT_KEVENT_ONESHOT EV_ONESHOT #endif #if (NXT_NETBSD) /* NetBSD defines the kevent.udata field as intptr_t. */ -#define nxt_kevent_set_udata(udata) (intptr_t) (udata) -#define nxt_kevent_get_udata(udata) (void *) (udata) +#define nxt_kevent_set_udata(udata) (intptr_t) (udata) +#define nxt_kevent_get_udata(udata) (void *) (udata) #else -#define nxt_kevent_set_udata(udata) (void *) (udata) -#define nxt_kevent_get_udata(udata) (udata) +#define nxt_kevent_set_udata(udata) (void *) (udata) +#define nxt_kevent_get_udata(udata) (udata) #endif -static nxt_int_t nxt_kqueue_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_kqueue_free(nxt_event_engine_t *engine); -static void nxt_kqueue_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_kqueue_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static nxt_bool_t nxt_kqueue_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_enable_accept(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_kqueue_enable_file(nxt_event_engine_t *engine, - nxt_file_event_t *ev); -static void nxt_kqueue_close_file(nxt_event_engine_t *engine, - nxt_file_event_t *ev); -static void nxt_kqueue_fd_set(nxt_event_engine_t *engine, nxt_fd_event_t *ev, +static nxt_int_t +nxt_kqueue_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_kqueue_free(nxt_event_engine_t *engine); +static void +nxt_kqueue_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_kqueue_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_kqueue_enable_file(nxt_event_engine_t *engine, nxt_file_event_t *ev); +static void +nxt_kqueue_close_file(nxt_event_engine_t *engine, nxt_file_event_t *ev); +static void +nxt_kqueue_fd_set(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_int_t filter, nxt_uint_t flags); -static struct kevent *nxt_kqueue_get_kevent(nxt_event_engine_t *engine); -static void nxt_kqueue_error(nxt_event_engine_t *engine); -static void nxt_kqueue_fd_error_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_kqueue_file_error_handler(nxt_task_t *task, void *obj, - void *data); -static nxt_int_t nxt_kqueue_add_signal(nxt_event_engine_t *engine, - const nxt_sig_event_t *sigev); +static struct kevent * +nxt_kqueue_get_kevent(nxt_event_engine_t *engine); +static void +nxt_kqueue_error(nxt_event_engine_t *engine); +static void +nxt_kqueue_fd_error_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_kqueue_file_error_handler(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_kqueue_add_signal(nxt_event_engine_t *engine, const nxt_sig_event_t *sigev); #if (NXT_HAVE_EVFILT_USER) -static nxt_int_t nxt_kqueue_enable_post(nxt_event_engine_t *engine, - nxt_work_handler_t handler); -static void nxt_kqueue_signal(nxt_event_engine_t *engine, nxt_uint_t signo); +static nxt_int_t +nxt_kqueue_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler); +static void +nxt_kqueue_signal(nxt_event_engine_t *engine, nxt_uint_t signo); #endif -static void nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); +static void +nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); -static void nxt_kqueue_conn_io_connect(nxt_task_t *task, void *obj, - void *data); -static void nxt_kqueue_conn_connected(nxt_task_t *task, void *obj, - void *data); -static void nxt_kqueue_listen_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, - void *data); -static void nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, - void *data); -static ssize_t nxt_kqueue_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); +static void +nxt_kqueue_conn_io_connect(nxt_task_t *task, void *obj, void *data); +static void +nxt_kqueue_conn_connected(nxt_task_t *task, void *obj, void *data); +static void +nxt_kqueue_listen_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, void *data); +static void +nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_kqueue_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); -static nxt_conn_io_t nxt_kqueue_conn_io = { +static nxt_conn_io_t nxt_kqueue_conn_io = { .connect = nxt_kqueue_conn_io_connect, - .accept = nxt_kqueue_conn_io_accept, + .accept = nxt_kqueue_conn_io_accept, - .read = nxt_kqueue_conn_io_read, + .read = nxt_kqueue_conn_io_read, .recvbuf = nxt_kqueue_conn_io_recvbuf, - .recv = nxt_conn_io_recv, + .recv = nxt_conn_io_recv, - .write = nxt_conn_io_write, + .write = nxt_conn_io_write, .sendbuf = nxt_conn_io_sendbuf, #if (NXT_HAVE_FREEBSD_SENDFILE) @@ -130,11 +141,11 @@ static nxt_conn_io_t nxt_kqueue_conn_io = { #endif .writev = nxt_event_conn_io_writev, - .send = nxt_event_conn_io_send, + .send = nxt_event_conn_io_send, }; -const nxt_event_interface_t nxt_kqueue_engine = { +const nxt_event_interface_t nxt_kqueue_engine = { "kqueue", nxt_kqueue_create, nxt_kqueue_free, @@ -171,14 +182,13 @@ const nxt_event_interface_t nxt_kqueue_engine = { static nxt_int_t nxt_kqueue_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - const nxt_sig_event_t *sigev; + nxt_uint_t mevents) { + const nxt_sig_event_t *sigev; - engine->u.kqueue.fd = -1; + engine->u.kqueue.fd = -1; engine->u.kqueue.mchanges = mchanges; - engine->u.kqueue.mevents = mevents; - engine->u.kqueue.pid = nxt_pid; + engine->u.kqueue.mevents = mevents; + engine->u.kqueue.pid = nxt_pid; engine->u.kqueue.changes = nxt_malloc(sizeof(struct kevent) * mchanges); if (engine->u.kqueue.changes == NULL) { @@ -215,11 +225,9 @@ nxt_kqueue_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_kqueue_free(nxt_event_engine_t *engine) -{ - nxt_fd_t fd; +nxt_kqueue_free(nxt_event_engine_t *engine) { + nxt_fd_t fd; fd = engine->u.kqueue.fd; @@ -229,8 +237,8 @@ nxt_kqueue_free(nxt_event_engine_t *engine) /* kqueue is not inherited by fork() */ if (close(fd) != 0) { - nxt_alert(&engine->task, "kqueue close(%d) failed %E", - fd, nxt_errno); + nxt_alert(&engine->task, "kqueue close(%d) failed %E", fd, + nxt_errno); } } @@ -240,23 +248,19 @@ nxt_kqueue_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.kqueue, sizeof(nxt_kqueue_engine_t)); } - static void -nxt_kqueue_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_kqueue_enable_read(engine, ev); nxt_kqueue_enable_write(engine, ev); } - /* * EV_DISABLE is better because it eliminates in-kernel memory * deallocation and probable subsequent allocation with a lock acquiring. */ static void -nxt_kqueue_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_INACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_READ, EV_DISABLE); @@ -268,10 +272,8 @@ nxt_kqueue_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static void -nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_INACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_READ, EV_DELETE); @@ -283,7 +285,6 @@ nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - /* * kqueue(2): * @@ -294,11 +295,10 @@ nxt_kqueue_delete(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static nxt_bool_t -nxt_kqueue_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - struct kevent *kev, *end; +nxt_kqueue_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + struct kevent *kev, *end; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; end = &engine->u.kqueue.changes[engine->u.kqueue.nchanges]; @@ -312,7 +312,6 @@ nxt_kqueue_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) return 0; } - /* * The kqueue event engine uses only three states: inactive, blocked, and * active. An active oneshot event is marked as it is in the default @@ -321,163 +320,137 @@ nxt_kqueue_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static void -nxt_kqueue_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read == NXT_EVENT_INACTIVE) { nxt_kqueue_fd_set(engine, ev, EVFILT_READ, - EV_ADD | EV_ENABLE | EV_CLEAR); + EV_ADD | EV_ENABLE | EV_CLEAR); } ev->read = NXT_EVENT_ACTIVE; } - static void -nxt_kqueue_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write == NXT_EVENT_INACTIVE) { nxt_kqueue_fd_set(engine, ev, EVFILT_WRITE, - EV_ADD | EV_ENABLE | EV_CLEAR); + EV_ADD | EV_ENABLE | EV_CLEAR); } ev->write = NXT_EVENT_ACTIVE; } - static void -nxt_kqueue_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->read = NXT_EVENT_INACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_READ, EV_DISABLE); } - static void -nxt_kqueue_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->write = NXT_EVENT_INACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_WRITE, EV_DISABLE); } - static void -nxt_kqueue_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_BLOCKED; } } - static void -nxt_kqueue_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_BLOCKED; } } - static void -nxt_kqueue_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->write = NXT_EVENT_ACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_WRITE, - EV_ADD | EV_ENABLE | NXT_KEVENT_ONESHOT); + EV_ADD | EV_ENABLE | NXT_KEVENT_ONESHOT); } - static void -nxt_kqueue_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_kqueue_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { ev->write = NXT_EVENT_ACTIVE; nxt_kqueue_fd_set(engine, ev, EVFILT_WRITE, - EV_ADD | EV_ENABLE | NXT_KEVENT_ONESHOT); + EV_ADD | EV_ENABLE | NXT_KEVENT_ONESHOT); } - static void -nxt_kqueue_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_kqueue_enable_accept(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->read_handler = nxt_kqueue_listen_handler; nxt_kqueue_fd_set(engine, ev, EVFILT_READ, EV_ADD | EV_ENABLE); } - static void -nxt_kqueue_enable_file(nxt_event_engine_t *engine, nxt_file_event_t *ev) -{ - struct kevent *kev; +nxt_kqueue_enable_file(nxt_event_engine_t *engine, nxt_file_event_t *ev) { + struct kevent *kev; - const nxt_int_t flags = EV_ADD | EV_ENABLE | EV_ONESHOT; - const nxt_uint_t fflags = NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND - | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE; + const nxt_int_t flags = EV_ADD | EV_ENABLE | EV_ONESHOT; + const nxt_uint_t fflags = NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND + | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE; nxt_debug(&engine->task, "kevent(%d) set: id:%d ft:%i fl:%04Xd, ff:%04XuD", - engine->u.kqueue.fd, ev->file->fd, EVFILT_VNODE, flags, fflags); + engine->u.kqueue.fd, ev->file->fd, EVFILT_VNODE, flags, fflags); kev = nxt_kqueue_get_kevent(engine); - kev->ident = ev->file->fd; + kev->ident = ev->file->fd; kev->filter = EVFILT_VNODE; - kev->flags = flags; + kev->flags = flags; kev->fflags = fflags; - kev->data = 0; - kev->udata = nxt_kevent_set_udata(ev); + kev->data = 0; + kev->udata = nxt_kevent_set_udata(ev); } - static void -nxt_kqueue_close_file(nxt_event_engine_t *engine, nxt_file_event_t *ev) -{ +nxt_kqueue_close_file(nxt_event_engine_t *engine, nxt_file_event_t *ev) { /* TODO: pending event. */ } - static void nxt_kqueue_fd_set(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - nxt_int_t filter, nxt_uint_t flags) -{ - struct kevent *kev; + nxt_int_t filter, nxt_uint_t flags) { + struct kevent *kev; nxt_debug(ev->task, "kevent(%d) set event: id:%d ft:%i fl:%04Xui", - engine->u.kqueue.fd, ev->fd, filter, flags); + engine->u.kqueue.fd, ev->fd, filter, flags); kev = nxt_kqueue_get_kevent(engine); - kev->ident = ev->fd; + kev->ident = ev->fd; kev->filter = filter; - kev->flags = flags; + kev->flags = flags; kev->fflags = 0; - kev->data = 0; - kev->udata = nxt_kevent_set_udata(ev); + kev->data = 0; + kev->udata = nxt_kevent_set_udata(ev); } - static struct kevent * -nxt_kqueue_get_kevent(nxt_event_engine_t *engine) -{ - int ret, nchanges; +nxt_kqueue_get_kevent(nxt_event_engine_t *engine) { + int ret, nchanges; nchanges = engine->u.kqueue.nchanges; if (nxt_slow_path(nchanges >= engine->u.kqueue.mchanges)) { - - nxt_debug(&engine->task, "kevent(%d) changes:%d", - engine->u.kqueue.fd, nchanges); + nxt_debug(&engine->task, "kevent(%d) changes:%d", engine->u.kqueue.fd, + nchanges); ret = kevent(engine->u.kqueue.fd, engine->u.kqueue.changes, nchanges, - NULL, 0, NULL); + NULL, 0, NULL); if (nxt_slow_path(ret != 0)) { nxt_alert(&engine->task, "kevent(%d) failed %E", - engine->u.kqueue.fd, nxt_errno); + engine->u.kqueue.fd, nxt_errno); nxt_kqueue_error(engine); } @@ -488,43 +461,37 @@ nxt_kqueue_get_kevent(nxt_event_engine_t *engine) return &engine->u.kqueue.changes[engine->u.kqueue.nchanges++]; } - static void -nxt_kqueue_error(nxt_event_engine_t *engine) -{ - struct kevent *kev, *end; - nxt_fd_event_t *ev; - nxt_file_event_t *fev; - nxt_work_queue_t *wq; +nxt_kqueue_error(nxt_event_engine_t *engine) { + struct kevent *kev, *end; + nxt_fd_event_t *ev; + nxt_file_event_t *fev; + nxt_work_queue_t *wq; - wq = &engine->fast_work_queue; + wq = &engine->fast_work_queue; end = &engine->u.kqueue.changes[engine->u.kqueue.nchanges]; for (kev = engine->u.kqueue.changes; kev < end; kev++) { - switch (kev->filter) { - case EVFILT_READ: case EVFILT_WRITE: ev = nxt_kevent_get_udata(kev->udata); - nxt_work_queue_add(wq, nxt_kqueue_fd_error_handler, - ev->task, ev, ev->data); + nxt_work_queue_add(wq, nxt_kqueue_fd_error_handler, ev->task, ev, + ev->data); break; case EVFILT_VNODE: fev = nxt_kevent_get_udata(kev->udata); - nxt_work_queue_add(wq, nxt_kqueue_file_error_handler, - fev->task, fev, fev->data); + nxt_work_queue_add(wq, nxt_kqueue_file_error_handler, fev->task, + fev, fev->data); break; } } } - static void -nxt_kqueue_fd_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fd_event_t *ev; +nxt_kqueue_fd_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fd_event_t *ev; ev = obj; @@ -533,22 +500,20 @@ nxt_kqueue_fd_error_handler(nxt_task_t *task, void *obj, void *data) if (ev->kq_eof && ev->kq_errno != 0) { ev->error = ev->kq_errno; nxt_log(task, nxt_socket_error_level(ev->kq_errno), - "kevent() reported error on descriptor %d %E", - ev->fd, ev->kq_errno); + "kevent() reported error on descriptor %d %E", ev->fd, + ev->kq_errno); } - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; ev->error = ev->kq_errno; ev->error_handler(task, ev, data); } - static void -nxt_kqueue_file_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_file_event_t *ev; +nxt_kqueue_file_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_file_event_t *ev; ev = obj; @@ -557,13 +522,12 @@ nxt_kqueue_file_error_handler(nxt_task_t *task, void *obj, void *data) ev->handler(task, ev, data); } - static nxt_int_t -nxt_kqueue_add_signal(nxt_event_engine_t *engine, const nxt_sig_event_t *sigev) -{ - int signo; - struct kevent kev; - struct sigaction sa; +nxt_kqueue_add_signal(nxt_event_engine_t *engine, + const nxt_sig_event_t *sigev) { + int signo; + struct kevent kev; + struct sigaction sa; signo = sigev->signo; @@ -584,15 +548,15 @@ nxt_kqueue_add_signal(nxt_event_engine_t *engine, const nxt_sig_event_t *sigev) return NXT_ERROR; } - nxt_debug(&engine->task, "kevent(%d) signo:%d (%s)", - engine->u.kqueue.fd, signo, sigev->name); + nxt_debug(&engine->task, "kevent(%d) signo:%d (%s)", engine->u.kqueue.fd, + signo, sigev->name); - kev.ident = signo; + kev.ident = signo; kev.filter = EVFILT_SIGNAL; - kev.flags = EV_ADD; + kev.flags = EV_ADD; kev.fflags = 0; - kev.data = 0; - kev.udata = nxt_kevent_set_udata(sigev); + kev.data = 0; + kev.udata = nxt_kevent_set_udata(sigev); if (kevent(engine->u.kqueue.fd, &kev, 1, NULL, 0, NULL) == 0) { return NXT_OK; @@ -607,18 +571,17 @@ nxt_kqueue_add_signal(nxt_event_engine_t *engine, const nxt_sig_event_t *sigev) #if (NXT_HAVE_EVFILT_USER) static nxt_int_t -nxt_kqueue_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) -{ - struct kevent kev; +nxt_kqueue_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) { + struct kevent kev; /* EVFILT_USER must be added to a kqueue before it can be triggered. */ - kev.ident = 0; + kev.ident = 0; kev.filter = EVFILT_USER; - kev.flags = EV_ADD | EV_CLEAR; + kev.flags = EV_ADD | EV_CLEAR; kev.fflags = 0; - kev.data = 0; - kev.udata = NULL; + kev.data = 0; + kev.udata = NULL; engine->u.kqueue.post_handler = handler; @@ -626,33 +589,31 @@ nxt_kqueue_enable_post(nxt_event_engine_t *engine, nxt_work_handler_t handler) return NXT_OK; } - nxt_alert(&engine->task, "kevent(%d) failed %E", - engine->u.kqueue.fd, nxt_errno); + nxt_alert(&engine->task, "kevent(%d) failed %E", engine->u.kqueue.fd, + nxt_errno); return NXT_ERROR; } - static void -nxt_kqueue_signal(nxt_event_engine_t *engine, nxt_uint_t signo) -{ - struct kevent kev; +nxt_kqueue_signal(nxt_event_engine_t *engine, nxt_uint_t signo) { + struct kevent kev; /* * kqueue has a builtin signal processing support, so the function * is used only to post events and the signo argument is ignored. */ - kev.ident = 0; + kev.ident = 0; kev.filter = EVFILT_USER; - kev.flags = 0; + kev.flags = 0; kev.fflags = NOTE_TRIGGER; - kev.data = 0; - kev.udata = NULL; + kev.data = 0; + kev.udata = NULL; if (kevent(engine->u.kqueue.fd, &kev, 1, NULL, 0, NULL) != 0) { - nxt_alert(&engine->task, "kevent(%d) failed %E", - engine->u.kqueue.fd, nxt_errno); + nxt_alert(&engine->task, "kevent(%d) failed %E", engine->u.kqueue.fd, + nxt_errno); } } @@ -660,38 +621,37 @@ nxt_kqueue_signal(nxt_event_engine_t *engine, nxt_uint_t signo) static void -nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ - int nevents; - void *obj, *data; - nxt_int_t i; - nxt_err_t err; - nxt_uint_t level; - nxt_bool_t error, eof; - nxt_task_t *task; - struct kevent *kev; - nxt_fd_event_t *ev; - nxt_sig_event_t *sigev; - struct timespec ts, *tp; - nxt_file_event_t *fev; - nxt_work_queue_t *wq; - nxt_work_handler_t handler; +nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { + int nevents; + void *obj, *data; + nxt_int_t i; + nxt_err_t err; + nxt_uint_t level; + nxt_bool_t error, eof; + nxt_task_t *task; + struct kevent *kev; + nxt_fd_event_t *ev; + nxt_sig_event_t *sigev; + struct timespec ts, *tp; + nxt_file_event_t *fev; + nxt_work_queue_t *wq; + nxt_work_handler_t handler; if (timeout == NXT_INFINITE_MSEC) { tp = NULL; } else { - ts.tv_sec = timeout / 1000; + ts.tv_sec = timeout / 1000; ts.tv_nsec = (timeout % 1000) * 1000000; - tp = &ts; + tp = &ts; } nxt_debug(&engine->task, "kevent(%d) changes:%d timeout:%M", - engine->u.kqueue.fd, engine->u.kqueue.nchanges, timeout); + engine->u.kqueue.fd, engine->u.kqueue.nchanges, timeout); - nevents = kevent(engine->u.kqueue.fd, - engine->u.kqueue.changes, engine->u.kqueue.nchanges, - engine->u.kqueue.events, engine->u.kqueue.mevents, tp); + nevents = kevent(engine->u.kqueue.fd, engine->u.kqueue.changes, + engine->u.kqueue.nchanges, engine->u.kqueue.events, + engine->u.kqueue.mevents, tp); err = (nevents == -1) ? nxt_errno : 0; @@ -703,7 +663,7 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT; nxt_log(&engine->task, level, "kevent(%d) failed %E", - engine->u.kqueue.fd, err); + engine->u.kqueue.fd, err); if (err != NXT_EINTR) { nxt_kqueue_error(engine); @@ -715,40 +675,38 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) engine->u.kqueue.nchanges = 0; for (i = 0; i < nevents; i++) { - error = 0; kev = &engine->u.kqueue.events[i]; nxt_debug(&engine->task, - (kev->ident > 0x8000000 && kev->ident != (uintptr_t) -1) ? - "kevent: id:%p ft:%d fl:%04Xd ff:%d d:%d ud:%p": - "kevent: id:%d ft:%d fl:%04Xd ff:%d d:%d ud:%p", - kev->ident, kev->filter, kev->flags, kev->fflags, - kev->data, kev->udata); + (kev->ident > 0x8000000 && kev->ident != (uintptr_t) -1) + ? "kevent: id:%p ft:%d fl:%04Xd ff:%d d:%d ud:%p" + : "kevent: id:%d ft:%d fl:%04Xd ff:%d d:%d ud:%p", + kev->ident, kev->filter, kev->flags, kev->fflags, kev->data, + kev->udata); if (nxt_slow_path(kev->flags & EV_ERROR)) { nxt_alert(&engine->task, - "kevent(%d) error %E on ident:%d filter:%d", - engine->u.kqueue.fd, kev->data, kev->ident, kev->filter); + "kevent(%d) error %E on ident:%d filter:%d", + engine->u.kqueue.fd, kev->data, kev->ident, kev->filter); error = 1; } - task = &engine->task; - wq = &engine->fast_work_queue; + task = &engine->task; + wq = &engine->fast_work_queue; handler = nxt_kqueue_fd_error_handler; - obj = nxt_kevent_get_udata(kev->udata); + obj = nxt_kevent_get_udata(kev->udata); switch (kev->filter) { - case EVFILT_READ: - ev = obj; - ev->read_ready = 1; - ev->kq_available = (int32_t) kev->data; - err = kev->fflags; - eof = (kev->flags & EV_EOF) != 0; - ev->kq_errno = err; - ev->kq_eof |= eof; + ev = obj; + ev->read_ready = 1; + ev->kq_available = (int32_t) kev->data; + err = kev->fflags; + eof = (kev->flags & EV_EOF) != 0; + ev->kq_errno = err; + ev->kq_eof |= eof; if (ev->read <= NXT_EVENT_BLOCKED) { nxt_debug(ev->task, "blocked read event fd:%d", ev->fd); @@ -765,7 +723,7 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (nxt_fast_path(!error)) { handler = ev->read_handler; - wq = ev->read_work_queue; + wq = ev->read_work_queue; } task = ev->task; @@ -774,12 +732,12 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) break; case EVFILT_WRITE: - ev = obj; - ev->write_ready = 1; - err = kev->fflags; - eof = (kev->flags & EV_EOF) != 0; - ev->kq_errno = err; - ev->kq_eof |= eof; + ev = obj; + ev->write_ready = 1; + err = kev->fflags; + eof = (kev->flags & EV_EOF) != 0; + ev->kq_errno = err; + ev->kq_eof |= eof; if (ev->write <= NXT_EVENT_BLOCKED) { nxt_debug(ev->task, "blocked write event fd:%d", ev->fd); @@ -796,7 +754,7 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (nxt_fast_path(!error)) { handler = ev->write_handler; - wq = ev->write_work_queue; + wq = ev->write_work_queue; } task = ev->task; @@ -805,24 +763,24 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) break; case EVFILT_VNODE: - fev = obj; + fev = obj; handler = fev->handler; - task = fev->task; - data = fev->data; + task = fev->task; + data = fev->data; break; case EVFILT_SIGNAL: - sigev = obj; - obj = (void *) kev->ident; + sigev = obj; + obj = (void *) kev->ident; handler = sigev->handler; - data = (void *) sigev->name; + data = (void *) sigev->name; break; #if (NXT_HAVE_EVFILT_USER) case EVFILT_USER: handler = engine->u.kqueue.post_handler; - data = NULL; + data = NULL; break; #endif @@ -831,8 +789,8 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) #if (NXT_DEBUG) nxt_alert(&engine->task, - "unexpected kevent(%d) filter %d on ident %d", - engine->u.kqueue.fd, kev->filter, kev->ident); + "unexpected kevent(%d) filter %d on ident %d", + engine->u.kqueue.fd, kev->filter, kev->ident); #endif continue; @@ -842,29 +800,26 @@ nxt_kqueue_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } } - /* * nxt_kqueue_event_conn_io_connect() eliminates the * getsockopt() syscall to test pending connect() error. */ static void -nxt_kqueue_conn_io_connect(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_event_engine_t *engine; +nxt_kqueue_conn_io_connect(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_event_engine_t *engine; nxt_work_handler_t handler; - const nxt_event_conn_state_t *state; + const nxt_event_conn_state_t *state; c = obj; state = c->write_state; switch (nxt_socket_connect(task, c->socket.fd, c->remote)) { - case NXT_OK: c->socket.write_ready = 1; - handler = state->ready_handler; + handler = state->ready_handler; break; case NXT_AGAIN: @@ -889,11 +844,9 @@ nxt_kqueue_conn_io_connect(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(c->write_work_queue, handler, task, c, data); } - static void -nxt_kqueue_conn_connected(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_kqueue_conn_connected(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -905,38 +858,34 @@ nxt_kqueue_conn_connected(nxt_task_t *task, void *obj, void *data) nxt_timer_disable(task->thread->engine, &c->write_timer); } - nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, - task, c, data); + nxt_work_queue_add(c->write_work_queue, c->write_state->ready_handler, task, + c, data); } - static void -nxt_kqueue_listen_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_listen_event_t *lev; +nxt_kqueue_listen_handler(nxt_task_t *task, void *obj, void *data) { + nxt_listen_event_t *lev; lev = obj; - nxt_debug(task, "kevent fd:%d avail:%D", - lev->socket.fd, lev->socket.kq_available); + nxt_debug(task, "kevent fd:%d avail:%D", lev->socket.fd, + lev->socket.kq_available); lev->ready = nxt_min(lev->batch, (uint32_t) lev->socket.kq_available); nxt_kqueue_conn_io_accept(task, lev, data); } - static void -nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, void *data) -{ +nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, void *data) { socklen_t socklen; - nxt_conn_t *c; + nxt_conn_t *c; nxt_socket_t s; - struct sockaddr *sa; - nxt_listen_event_t *lev; + struct sockaddr *sa; + nxt_listen_event_t *lev; lev = obj; - c = lev->next; + c = lev->next; lev->ready--; lev->socket.read_ready = (lev->ready != 0); @@ -944,13 +893,13 @@ nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, void *data) lev->socket.kq_available--; lev->socket.read_ready = (lev->socket.kq_available != 0); - sa = &c->remote->u.sockaddr; + sa = &c->remote->u.sockaddr; socklen = c->remote->socklen; /* * The returned socklen is ignored here, * see comment in nxt_conn_io_accept(). */ - s = accept(lev->socket.fd, sa, &socklen); + s = accept(lev->socket.fd, sa, &socklen); if (s != -1) { c->socket.fd = s; @@ -964,16 +913,14 @@ nxt_kqueue_conn_io_accept(nxt_task_t *task, void *obj, void *data) nxt_conn_accept_error(task, lev, "accept", nxt_errno); } - /* * nxt_kqueue_conn_io_read() is just a wrapper to eliminate the * readv() or recv() syscall if a remote side just closed connection. */ static void -nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; +nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; c = obj; @@ -984,14 +931,13 @@ nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, void *data) c->socket.closed = 1; nxt_work_queue_add(c->read_work_queue, c->read_state->close_handler, - task, c, data); + task, c, data); return; } nxt_conn_io_read(task, c, data); } - /* * nxt_kqueue_conn_io_recvbuf() is just wrapper around standard * nxt_conn_io_recvbuf() to eliminate the readv() or recv() syscalls @@ -999,9 +945,8 @@ nxt_kqueue_conn_io_read(nxt_task_t *task, void *obj, void *data) */ static ssize_t -nxt_kqueue_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) -{ - ssize_t n; +nxt_kqueue_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) { + ssize_t n; if (c->socket.kq_available == 0 && c->socket.kq_eof) { c->socket.closed = 1; @@ -1017,11 +962,11 @@ nxt_kqueue_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) c->socket.kq_available = 0; } - nxt_debug(c->socket.task, "kevent fd:%d avail:%D eof:%d", - c->socket.fd, c->socket.kq_available, c->socket.kq_eof); + nxt_debug(c->socket.task, "kevent fd:%d avail:%D eof:%d", c->socket.fd, + c->socket.kq_available, c->socket.kq_eof); - c->socket.read_ready = (c->socket.kq_available != 0 - || c->socket.kq_eof); + c->socket.read_ready + = (c->socket.kq_available != 0 || c->socket.kq_eof); } return n; diff --git a/src/nxt_lib.c b/src/nxt_lib.c index de23ce0ab..09074de7b 100644 --- a/src/nxt_lib.c +++ b/src/nxt_lib.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,10 +6,10 @@ #include -nxt_uint_t nxt_ncpu = 1; -nxt_uint_t nxt_pagesize; -nxt_task_t nxt_main_task; -nxt_atomic_t nxt_task_ident; +nxt_uint_t nxt_ncpu = 1; +nxt_uint_t nxt_pagesize; +nxt_task_t nxt_main_task; +nxt_atomic_t nxt_task_ident; nxt_thread_declare_data(nxt_thread_t, nxt_thread_context); @@ -30,18 +29,17 @@ const char *malloc_conf = "junk:true"; nxt_int_t -nxt_lib_start(const char *app, char **argv, char ***envp) -{ +nxt_lib_start(const char *app, char **argv, char ***envp) { int n = 0; nxt_int_t flags; nxt_bool_t update; - nxt_thread_t *thread; + nxt_thread_t *thread; flags = nxt_stderr_start(); nxt_log_start(app); - nxt_pid = getpid(); + nxt_pid = getpid(); nxt_ppid = getppid(); nxt_euid = geteuid(); nxt_egid = getegid(); @@ -65,16 +63,16 @@ nxt_lib_start(const char *app, char **argv, char ***envp) /* Thread log is required for nxt_malloc() in nxt_strerror_start(). */ nxt_thread_init_data(nxt_thread_context); - thread = nxt_thread(); + thread = nxt_thread(); thread->log = &nxt_main_log; - thread->handle = nxt_thread_handle(); + thread->handle = nxt_thread_handle(); thread->time.signal = -1; nxt_thread_time_update(thread); nxt_main_task.thread = thread; - nxt_main_task.log = thread->log; - nxt_main_task.ident = nxt_task_next_ident(); + nxt_main_task.log = thread->log; + nxt_main_task.ident = nxt_task_next_ident(); if (nxt_strerror_start() != NXT_OK) { return NXT_ERROR; @@ -93,7 +91,7 @@ nxt_lib_start(const char *app, char **argv, char ***envp) if (n > 0) { int err; size_t size; - cpu_set_t *set; + cpu_set_t *set; set = CPU_ALLOC(n); if (set == NULL) { @@ -142,10 +140,8 @@ nxt_lib_start(const char *app, char **argv, char ***envp) return NXT_OK; } - void -nxt_lib_stop(void) -{ +nxt_lib_stop(void) { /* TODO: stop engines */ #if 0 diff --git a/src/nxt_linux_sendfile.c b/src/nxt_linux_sendfile.c index 0e772ffb3..9d21aa325 100644 --- a/src/nxt_linux_sendfile.c +++ b/src/nxt_linux_sendfile.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -19,47 +18,46 @@ #ifdef NXT_TEST_BUILD_LINUX_SENDFILE -#define MSG_NOSIGNAL 0x4000 -#define MSG_MORE 0x8000 +#define MSG_NOSIGNAL 0x4000 +#define MSG_MORE 0x8000 -ssize_t nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static ssize_t nxt_sys_sendfile(int out_fd, int in_fd, off_t *offset, - size_t count) -{ +static ssize_t +nxt_sys_sendfile(int out_fd, int in_fd, off_t *offset, size_t count) { return -1; } #else -#define nxt_sys_sendfile sendfile +#define nxt_sys_sendfile sendfile #endif -static ssize_t nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, - nxt_uint_t flags); -static ssize_t nxt_linux_sendmsg(nxt_event_conn_t *c, - nxt_sendbuf_coalesce_t *sb, nxt_uint_t niov, nxt_uint_t flags); - +static ssize_t +nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags); +static ssize_t +nxt_linux_sendmsg(nxt_event_conn_t *c, nxt_sendbuf_coalesce_t *sb, + nxt_uint_t niov, nxt_uint_t flags); ssize_t nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, - size_t limit) -{ - size_t size; - ssize_t n; - nxt_buf_t *fb; - nxt_err_t err; - nxt_off_t offset; - nxt_uint_t niov, flags; - struct iovec iov[NXT_IOBUF_MAX]; - nxt_sendbuf_coalesce_t sb; - - sb.buf = b; + size_t limit) { + size_t size; + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_off_t offset; + nxt_uint_t niov, flags; + struct iovec iov[NXT_IOBUF_MAX]; + nxt_sendbuf_coalesce_t sb; + + sb.buf = b; sb.iobuf = iov; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; niov = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -71,7 +69,6 @@ nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, fb = (sb.buf != NULL && nxt_buf_is_file(sb.buf)) ? sb.buf : NULL; if (niov != 0) { - flags = MSG_NOSIGNAL; if (fb != NULL) { @@ -95,8 +92,8 @@ nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size = nxt_sendbuf_file_coalesce(&sb); - nxt_debug(c->socket.task, "sendfile(%d, %FD, @%O, %uz)", - c->socket.fd, fb->file->fd, fb->file_pos, size); + nxt_debug(c->socket.task, "sendfile(%d, %FD, @%O, %uz)", c->socket.fd, + fb->file->fd, fb->file_pos, size); offset = fb->file_pos; @@ -108,7 +105,6 @@ nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -119,9 +115,8 @@ nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "sendfile(%d, %FD, %O, %uz) failed %E \"%FN\"", - c->socket.fd, fb->file->fd, fb->file_pos, size, - err, fb->file->name); + "sendfile(%d, %FD, %O, %uz) failed %E \"%FN\"", c->socket.fd, + fb->file->fd, fb->file_pos, size, err, fb->file->name); return NXT_ERROR; } @@ -138,23 +133,20 @@ nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, return n; } - static ssize_t -nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags) -{ - ssize_t n; - nxt_err_t err; +nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags) { + ssize_t n; + nxt_err_t err; n = send(c->socket.fd, buf, size, flags); err = (n == -1) ? nxt_errno : 0; - nxt_debug(c->socket.task, "send(%d, %p, %uz, 0x%uXi): %z", - c->socket.fd, buf, size, flags, n); + nxt_debug(c->socket.task, "send(%d, %p, %uz, 0x%uXi): %z", c->socket.fd, + buf, size, flags, n); if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -165,8 +157,8 @@ nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "send(%d, %p, %uz, 0x%uXi) failed %E", - c->socket.fd, buf, size, flags, err); + "send(%d, %p, %uz, 0x%uXi) failed %E", c->socket.fd, buf, size, + flags, err); return NXT_ERROR; } @@ -183,33 +175,30 @@ nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags) return n; } - static ssize_t nxt_linux_sendmsg(nxt_event_conn_t *c, nxt_sendbuf_coalesce_t *sb, - nxt_uint_t niov, nxt_uint_t flags) -{ - ssize_t n; - nxt_err_t err; - struct msghdr msg; - - msg.msg_name = NULL; - msg.msg_namelen = 0; - msg.msg_iov = sb->iobuf; - msg.msg_iovlen = niov; - msg.msg_control = NULL; + nxt_uint_t niov, nxt_uint_t flags) { + ssize_t n; + nxt_err_t err; + struct msghdr msg; + + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_iov = sb->iobuf; + msg.msg_iovlen = niov; + msg.msg_control = NULL; msg.msg_controllen = 0; - msg.msg_flags = 0; + msg.msg_flags = 0; n = sendmsg(c->socket.fd, &msg, flags); err = (n == -1) ? nxt_errno : 0; - nxt_debug(c->socket.task, "sendmsg(%d, %ui, 0x%uXi): %z", - c->socket.fd, niov, flags, n); + nxt_debug(c->socket.task, "sendmsg(%d, %ui, 0x%uXi): %z", c->socket.fd, + niov, flags, n); if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -220,8 +209,8 @@ nxt_linux_sendmsg(nxt_event_conn_t *c, nxt_sendbuf_coalesce_t *sb, default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "sendmsg(%d, %ui, 0x%uXi) failed %E", - c->socket.fd, niov, flags, err); + "sendmsg(%d, %ui, 0x%uXi) failed %E", c->socket.fd, niov, flags, + err); return NXT_ERROR; } diff --git a/src/nxt_list.c b/src/nxt_list.c index cab925cbd..0a5eaae2d 100644 --- a/src/nxt_list.c +++ b/src/nxt_list.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,51 +5,46 @@ #include - nxt_list_t * -nxt_list_create(nxt_mp_t *mp, nxt_uint_t n, size_t size) -{ - nxt_list_t *list; +nxt_list_create(nxt_mp_t *mp, nxt_uint_t n, size_t size) { + nxt_list_t *list; list = nxt_mp_get(mp, sizeof(nxt_list_t) + n * size); if (nxt_fast_path(list != NULL)) { - list->last = &list->part; - list->size = size; - list->nalloc = n; - list->mem_pool = mp; - list->part.next = NULL; + list->last = &list->part; + list->size = size; + list->nalloc = n; + list->mem_pool = mp; + list->part.next = NULL; list->part.nelts = 0; } return list; } - void * -nxt_list_add(nxt_list_t *list) -{ - void *elt; - nxt_list_part_t *last; +nxt_list_add(nxt_list_t *list) { + void *elt; + nxt_list_part_t *last; last = list->last; if (last->nelts == list->nalloc) { - /* The last list part is filled up, allocating a new list part. */ last = nxt_mp_get(list->mem_pool, - sizeof(nxt_list_part_t) + list->nalloc * list->size); + sizeof(nxt_list_part_t) + list->nalloc * list->size); if (nxt_slow_path(last == NULL)) { return NULL; } - last->next = NULL; + last->next = NULL; last->nelts = 0; list->last->next = last; - list->last = last; + list->last = last; } elt = nxt_pointer_to(nxt_list_data(last), last->nelts * list->size); @@ -59,11 +53,9 @@ nxt_list_add(nxt_list_t *list) return elt; } - void * -nxt_list_zero_add(nxt_list_t *list) -{ - void *p; +nxt_list_zero_add(nxt_list_t *list) { + void *p; p = nxt_list_add(list); @@ -74,10 +66,8 @@ nxt_list_zero_add(nxt_list_t *list) return p; } - void * -nxt_list_next(nxt_list_t *list, nxt_list_next_t *next) -{ +nxt_list_next(nxt_list_t *list, nxt_list_next_t *next) { if (next->part != NULL) { next->elt++; @@ -101,7 +91,6 @@ nxt_list_next(nxt_list_t *list, nxt_list_next_t *next) if (next->part->nelts != 0) { return nxt_list_data(next->part); } - } return NULL; diff --git a/src/nxt_list.h b/src/nxt_list.h index dc948e038..852e40cae 100644 --- a/src/nxt_list.h +++ b/src/nxt_list.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,51 +7,43 @@ #define _NXT_LIST_H_INCLUDED_ -typedef struct nxt_list_part_s nxt_list_part_t; +typedef struct nxt_list_part_s nxt_list_part_t; struct nxt_list_part_s { - nxt_list_part_t *next; - uintptr_t nelts; - char data[]; + nxt_list_part_t *next; + uintptr_t nelts; + char data[]; }; - typedef struct { - nxt_list_part_t *last; + nxt_list_part_t *last; #if (NXT_64BIT) - uint32_t size; - uint32_t nalloc; + uint32_t size; + uint32_t nalloc; #else - uint16_t size; - uint16_t nalloc; + uint16_t size; + uint16_t nalloc; #endif - nxt_mp_t *mem_pool; - nxt_list_part_t part; + nxt_mp_t *mem_pool; + nxt_list_part_t part; } nxt_list_t; - typedef struct { - nxt_list_part_t *part; - uintptr_t elt; + nxt_list_part_t *part; + uintptr_t elt; } nxt_list_next_t; - -#define nxt_list_part(list) \ - (&(list)->part) +#define nxt_list_part(list) (&(list)->part) -#define nxt_list_data(part) \ - ((void *) part->data) +#define nxt_list_data(part) ((void *) part->data) -#define nxt_list_first(list) \ - nxt_list_data(nxt_list_part(list)) - +#define nxt_list_first(list) nxt_list_data(nxt_list_part(list)) nxt_inline void * -nxt_list_elt(nxt_list_t *list, nxt_uint_t n) -{ - nxt_list_part_t *part; +nxt_list_elt(nxt_list_t *list, nxt_uint_t n) { + nxt_list_part_t *part; if (nxt_fast_path((list) != NULL)) { part = nxt_list_part(list); @@ -62,52 +53,54 @@ nxt_list_elt(nxt_list_t *list, nxt_uint_t n) return nxt_pointer_to(nxt_list_data(part), n * (list)->size); } - n -= (nxt_uint_t) part->nelts; - part = part->next; + n -= (nxt_uint_t) part->nelts; + part = part->next; } } return NULL; } - -#define nxt_list_each(elt, list) \ - do { \ - if (nxt_fast_path((list) != NULL)) { \ - void *_end; \ - nxt_list_part_t *_part = nxt_list_part(list); \ - \ - do { \ - elt = nxt_list_data(_part); \ - \ - for (_end = (elt + _part->nelts); elt != _end; elt++) { \ - -#define nxt_list_loop \ - } \ - \ - _part = _part->next; \ - \ - } while (_part != NULL); \ - } \ - } while (0) - - -NXT_EXPORT nxt_list_t *nxt_list_create(nxt_mp_t *mp, nxt_uint_t n, size_t size); -NXT_EXPORT void *nxt_list_add(nxt_list_t *list); -NXT_EXPORT void *nxt_list_zero_add(nxt_list_t *list); - -NXT_EXPORT void *nxt_list_next(nxt_list_t *list, nxt_list_next_t *next); - - -#define nxt_list_next_value(list, next) \ +#define nxt_list_each(elt, list) \ + do { \ + if (nxt_fast_path((list) != NULL)) { \ + void *_end; \ + nxt_list_part_t *_part = nxt_list_part(list); \ + \ + do { \ + elt = nxt_list_data(_part); \ + \ + for (_end = (elt + _part->nelts); elt != _end; elt++) { +#define nxt_list_loop \ + } \ + \ + _part = _part->next; \ + } \ + while (_part != NULL) \ + ; \ + } \ + } \ + while (0) + + +NXT_EXPORT nxt_list_t * +nxt_list_create(nxt_mp_t *mp, nxt_uint_t n, size_t size); +NXT_EXPORT void * +nxt_list_add(nxt_list_t *list); +NXT_EXPORT void * +nxt_list_zero_add(nxt_list_t *list); + +NXT_EXPORT void * +nxt_list_next(nxt_list_t *list, nxt_list_next_t *next); + + +#define nxt_list_next_value(list, next) \ (nxt_pointer_to(nxt_list_data((next)->part), (next)->elt * (list)->size)) - nxt_inline nxt_uint_t -nxt_list_nelts(nxt_list_t *list) -{ +nxt_list_nelts(nxt_list_t *list) { nxt_uint_t n; - nxt_list_part_t *part; + nxt_list_part_t *part; n = 0; @@ -115,8 +108,8 @@ nxt_list_nelts(nxt_list_t *list) part = nxt_list_part(list); do { - n += (nxt_uint_t) part->nelts; - part = part->next; + n += (nxt_uint_t) part->nelts; + part = part->next; } while (part != NULL); } diff --git a/src/nxt_listen_socket.c b/src/nxt_listen_socket.c index 4fe3e20b6..032f9575c 100644 --- a/src/nxt_listen_socket.c +++ b/src/nxt_listen_socket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,13 +6,11 @@ #include -static u_char *nxt_listen_socket_log_handler(void *ctx, u_char *pos, - u_char *last); - +static u_char * +nxt_listen_socket_log_handler(void *ctx, u_char *pos, u_char *last); nxt_int_t -nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, int backlog) -{ +nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, int backlog) { nxt_debug(task, "listen(%d, %d)", s, backlog); if (nxt_fast_path(listen(s, backlog) == 0)) { @@ -25,34 +22,32 @@ nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, int backlog) return NXT_ERROR; } - nxt_int_t nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, - nxt_listen_socket_t *ls) -{ - nxt_log_t log, *old; - nxt_uint_t family; - nxt_socket_t s; - nxt_thread_t *thr; - nxt_sockaddr_t *sa; + nxt_listen_socket_t *ls) { + nxt_log_t log, *old; + nxt_uint_t family; + nxt_socket_t s; + nxt_thread_t *thr; + nxt_sockaddr_t *sa; #if (NXT_HAVE_UNIX_DOMAIN) - int ret; - u_char *p; - nxt_err_t err; - nxt_socket_t ts; - nxt_sockaddr_t *orig_sa; - nxt_file_name_t *name, *tmp; - nxt_file_access_t access; + int ret; + u_char *p; + nxt_err_t err; + nxt_socket_t ts; + nxt_sockaddr_t *orig_sa; + nxt_file_name_t *name, *tmp; + nxt_file_access_t access; #endif sa = ls->sockaddr; - thr = nxt_thread(); - old = thr->log; - log = *thr->log; + thr = nxt_thread(); + old = thr->log; + log = *thr->log; log.ctx_handler = nxt_listen_socket_log_handler; - log.ctx = sa; - thr->log = &log; + log.ctx = sa; + thr->log = &log; family = sa->u.sockaddr.sa_family; @@ -68,13 +63,13 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, #if (NXT_INET6 && defined IPV6_V6ONLY) if (family == AF_INET6 && ls->ipv6only) { - int ipv6only; + int ipv6only; ipv6only = (ls->ipv6only == 1); /* Ignore possible error. TODO: why? */ (void) nxt_socket_setsockopt(task, s, IPPROTO_IPV6, IPV6_V6ONLY, - ipv6only); + ipv6only); } #endif @@ -92,10 +87,8 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, #if (NXT_HAVE_UNIX_DOMAIN) - if (family == AF_UNIX - && sa->type == SOCK_STREAM - && sa->u.sockaddr_un.sun_path[0] != '\0') - { + if (family == AF_UNIX && sa->type == SOCK_STREAM + && sa->u.sockaddr_un.sun_path[0] != '\0') { orig_sa = sa; sa = nxt_sockaddr_alloc(mp, sa->socklen + 4, sa->length + 4); @@ -103,11 +96,11 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, goto fail; } - sa->type = SOCK_STREAM; + sa->type = SOCK_STREAM; sa->u.sockaddr_un.sun_family = AF_UNIX; p = nxt_cpystr((u_char *) sa->u.sockaddr_un.sun_path, - (u_char *) orig_sa->u.sockaddr_un.sun_path); + (u_char *) orig_sa->u.sockaddr_un.sun_path); nxt_memcpy(p, ".tmp", 4); nxt_sockaddr_text(sa); @@ -127,18 +120,18 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, #if (NXT_HAVE_UNIX_DOMAIN) if (family == AF_UNIX) { - const char *user; - const char *group; - nxt_runtime_t *rt = thr->runtime; + const char *user; + const char *group; + nxt_runtime_t *rt = thr->runtime; - name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; + name = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; access = rt->control_mode > 0 ? rt->control_mode : 0600; if (nxt_file_set_access(name, access) != NXT_OK) { goto listen_fail; } - user = rt->control_user; + user = rt->control_user; group = rt->control_group; if (nxt_file_chown(name, user, group) != NXT_OK) { @@ -151,8 +144,8 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, nxt_debug(task, "listen(%d, %d)", s, ls->backlog); if (listen(s, ls->backlog) != 0) { - nxt_alert(task, "listen(%d, %d) failed %E", - s, ls->backlog, nxt_socket_errno); + nxt_alert(task, "listen(%d, %d) failed %E", s, ls->backlog, + nxt_socket_errno); goto listen_fail; } @@ -171,22 +164,20 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_close(task, ts); if (ret == 0) { - nxt_alert(task, "connect(%d, %*s) socket already in use", - ts, (size_t) orig_sa->length, - nxt_sockaddr_start(orig_sa)); + nxt_alert(task, "connect(%d, %*s) socket already in use", ts, + (size_t) orig_sa->length, nxt_sockaddr_start(orig_sa)); goto listen_fail; } if (err != NXT_ENOENT && err != NXT_ECONNREFUSED) { - nxt_alert(task, "connect(%d, %*s) failed %E", - ts, (size_t) orig_sa->length, - nxt_sockaddr_start(orig_sa), err); + nxt_alert(task, "connect(%d, %*s) failed %E", ts, + (size_t) orig_sa->length, nxt_sockaddr_start(orig_sa), err); goto listen_fail; } - tmp = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; + tmp = (nxt_file_name_t *) sa->u.sockaddr_un.sun_path; name = (nxt_file_name_t *) orig_sa->u.sockaddr_un.sun_path; if (nxt_file_rename(tmp, name) != NXT_OK) { @@ -197,7 +188,7 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, #endif ls->socket = s; - thr->log = old; + thr->log = old; return NXT_OK; @@ -224,28 +215,26 @@ nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, return NXT_ERROR; } - nxt_int_t nxt_listen_socket_update(nxt_task_t *task, nxt_listen_socket_t *ls, - nxt_listen_socket_t *prev) -{ + nxt_listen_socket_t *prev) { nxt_log_t log, *old; - nxt_thread_t *thr; + nxt_thread_t *thr; ls->socket = prev->socket; - thr = nxt_thread(); - old = thr->log; - log = *thr->log; + thr = nxt_thread(); + old = thr->log; + log = *thr->log; log.ctx_handler = nxt_listen_socket_log_handler; - log.ctx = ls->sockaddr; - thr->log = &log; + log.ctx = ls->sockaddr; + thr->log = &log; nxt_debug(task, "listen(%d, %d)", ls->socket, ls->backlog); if (listen(ls->socket, ls->backlog) != 0) { - nxt_alert(task, "listen(%d, %d) failed %E", - ls->socket, ls->backlog, nxt_socket_errno); + nxt_alert(task, "listen(%d, %d) failed %E", ls->socket, ls->backlog, + nxt_socket_errno); goto fail; } @@ -260,16 +249,13 @@ nxt_listen_socket_update(nxt_task_t *task, nxt_listen_socket_t *ls, return NXT_ERROR; } - void -nxt_listen_socket_remote_size(nxt_listen_socket_t *ls) -{ +nxt_listen_socket_remote_size(nxt_listen_socket_t *ls) { switch (ls->sockaddr->u.sockaddr.sa_family) { - #if (NXT_INET6) case AF_INET6: - ls->socklen = sizeof(struct sockaddr_in6); + ls->socklen = sizeof(struct sockaddr_in6); ls->address_length = NXT_INET6_ADDR_STR_LEN; break; @@ -285,7 +271,7 @@ nxt_listen_socket_remote_size(nxt_listen_socket_t *ls) * a bound remote socket correctly ls->socklen should be larger, see * comment in nxt_socket.h. */ - ls->socklen = offsetof(struct sockaddr_un, sun_path) + 1; + ls->socklen = offsetof(struct sockaddr_un, sun_path) + 1; ls->address_length = nxt_length("unix:"); break; @@ -294,18 +280,16 @@ nxt_listen_socket_remote_size(nxt_listen_socket_t *ls) default: case AF_INET: - ls->socklen = sizeof(struct sockaddr_in); + ls->socklen = sizeof(struct sockaddr_in); ls->address_length = NXT_INET_ADDR_STR_LEN; break; } } - size_t -nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) -{ - size_t size; +nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) { + size_t size; /* * The first nxt_sockaddr_t is intended for mandatory remote sockaddr @@ -317,11 +301,10 @@ nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) */ switch (ls->sockaddr->u.sockaddr.sa_family) { - #if (NXT_INET6) case AF_INET6: - ls->socklen = sizeof(struct sockaddr_in6); + ls->socklen = sizeof(struct sockaddr_in6); ls->address_length = NXT_INET6_ADDR_STR_LEN; size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in6) @@ -344,8 +327,8 @@ nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) * a bound remote socket correctly ls->socklen should be at least * sizeof(struct sockaddr_un), see comment in nxt_socket.h. */ - ls->socklen = 3; - size = ls->socklen + nxt_length("unix:"); + ls->socklen = 3; + size = ls->socklen + nxt_length("unix:"); ls->address_length = nxt_length("unix:"); break; @@ -353,7 +336,7 @@ nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) #endif default: - ls->socklen = sizeof(struct sockaddr_in); + ls->socklen = sizeof(struct sockaddr_in); ls->address_length = NXT_INET_ADDR_STR_LEN; size = offsetof(nxt_sockaddr_t, u) + sizeof(struct sockaddr_in) @@ -369,26 +352,23 @@ nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls) #if (NXT_TLS) if (ls->tls) { - size += 4 * sizeof(void *) /* SSL/TLS connection */ + size += 4 * sizeof(void *) /* SSL/TLS connection */ + sizeof(nxt_buf_mem_t) - + sizeof(nxt_work_t); /* nxt_mp_cleanup */ + + sizeof(nxt_work_t); /* nxt_mp_cleanup */ } #endif return size // + sizeof(nxt_mem_pool_t) - + sizeof(nxt_conn_t) - + sizeof(nxt_log_t); + + sizeof(nxt_conn_t) + sizeof(nxt_log_t); } - static u_char * -nxt_listen_socket_log_handler(void *ctx, u_char *pos, u_char *end) -{ - nxt_sockaddr_t *sa; +nxt_listen_socket_log_handler(void *ctx, u_char *pos, u_char *end) { + nxt_sockaddr_t *sa; sa = ctx; return nxt_sprintf(pos, end, " while creating listening socket on %*s", - (size_t) sa->length, nxt_sockaddr_start(sa)); + (size_t) sa->length, nxt_sockaddr_start(sa)); } diff --git a/src/nxt_listen_socket.h b/src/nxt_listen_socket.h index 8bf320bc1..3b663a995 100644 --- a/src/nxt_listen_socket.h +++ b/src/nxt_listen_socket.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,31 +6,30 @@ #ifndef _NXT_LISTEN_SOCKET_H_INCLUDED_ #define _NXT_LISTEN_SOCKET_H_INCLUDED_ - typedef struct { /* nxt_socket_t is int. */ - nxt_socket_t socket; - int backlog; + nxt_socket_t socket; + int backlog; - nxt_work_queue_t *work_queue; - nxt_work_handler_t handler; + nxt_work_queue_t *work_queue; + nxt_work_handler_t handler; - nxt_sockaddr_t *sockaddr; + nxt_sockaddr_t *sockaddr; - uint32_t count; + uint32_t count; - uint8_t flags; - uint8_t read_after_accept; /* 1 bit */ + uint8_t flags; + uint8_t read_after_accept; /* 1 bit */ #if (NXT_TLS) - uint8_t tls; /* 1 bit */ + uint8_t tls; /* 1 bit */ #endif #if (NXT_INET6 && defined IPV6_V6ONLY) - uint8_t ipv6only; /* 2 bits */ + uint8_t ipv6only; /* 2 bits */ #endif - uint8_t socklen; - uint8_t address_length; + uint8_t socklen; + uint8_t address_length; } nxt_listen_socket_t; @@ -40,26 +38,30 @@ typedef struct { * A backlog is limited by system-wide sysctl {net.core,kern.ipc}.somaxconn. * This is supported by Linux, FreeBSD 2.2, OpenBSD 2.0, and MacOSX. */ -#define NXT_LISTEN_BACKLOG -1 +#define NXT_LISTEN_BACKLOG -1 #else /* * Solaris and NetBSD treat negative value as 0. * 511 is a safe default. */ -#define NXT_LISTEN_BACKLOG 511 +#define NXT_LISTEN_BACKLOG 511 #endif -NXT_EXPORT nxt_int_t nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, - int backlog); +NXT_EXPORT nxt_int_t +nxt_listen_socket(nxt_task_t *task, nxt_socket_t s, int backlog); -NXT_EXPORT nxt_int_t nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, +NXT_EXPORT nxt_int_t +nxt_listen_socket_create(nxt_task_t *task, nxt_mp_t *mp, nxt_listen_socket_t *ls); -NXT_EXPORT nxt_int_t nxt_listen_socket_update(nxt_task_t *task, - nxt_listen_socket_t *ls, nxt_listen_socket_t *prev); -NXT_EXPORT void nxt_listen_socket_remote_size(nxt_listen_socket_t *ls); -NXT_EXPORT size_t nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls); +NXT_EXPORT nxt_int_t +nxt_listen_socket_update(nxt_task_t *task, nxt_listen_socket_t *ls, + nxt_listen_socket_t *prev); +NXT_EXPORT void +nxt_listen_socket_remote_size(nxt_listen_socket_t *ls); +NXT_EXPORT size_t +nxt_listen_socket_pool_min_size(nxt_listen_socket_t *ls); #endif /* _NXT_LISTEN_SOCKET_H_INCLUDED_ */ diff --git a/src/nxt_log.c b/src/nxt_log.c index 587568164..131d36967 100644 --- a/src/nxt_log.c +++ b/src/nxt_log.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,77 +6,60 @@ #include -nxt_uint_t nxt_debug; -nxt_uint_t nxt_trace; - +nxt_uint_t nxt_debug; +nxt_uint_t nxt_trace; -nxt_log_t nxt_main_log = { - NXT_LOG_INFO, - 0, - nxt_log_handler, - NULL, - NULL -}; +nxt_log_t nxt_main_log = {NXT_LOG_INFO, 0, nxt_log_handler, NULL, NULL}; -nxt_str_t nxt_log_levels[6] = { - nxt_string("alert"), - nxt_string("error"), - nxt_string("warn"), - nxt_string("notice"), - nxt_string("info"), - nxt_string("debug") -}; +nxt_str_t nxt_log_levels[6] + = {nxt_string("alert"), nxt_string("error"), nxt_string("warn"), + nxt_string("notice"), nxt_string("info"), nxt_string("debug")}; -static const u_char *nxt_log_prefix; +static const u_char *nxt_log_prefix; void -nxt_log_start(const char *prefix) -{ +nxt_log_start(const char *prefix) { if (prefix != NULL && *prefix != '\0') { nxt_log_prefix = (u_char *) prefix; } } - /* STUB */ nxt_log_t * -nxt_log_set_ctx(nxt_log_t *log, nxt_log_ctx_handler_t handler, void *ctx) -{ - nxt_log_t *old; - nxt_thread_t *thr; +nxt_log_set_ctx(nxt_log_t *log, nxt_log_ctx_handler_t handler, void *ctx) { + nxt_log_t *old; + nxt_thread_t *thr; thr = nxt_thread(); old = thr->log; - log->level = old->level; - log->handler = old->handler; + log->level = old->level; + log->handler = old->handler; log->ctx_handler = handler; - log->ctx = ctx; + log->ctx = ctx; thr->log = log; return old; } - void nxt_cdecl -nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) -{ - u_char *p, *end; +nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) { + u_char *p, *end; #if 0 u_char *syslogmsg; #endif - va_list args; - u_char msg[NXT_MAX_ERROR_STR]; + va_list args; + u_char msg[NXT_MAX_ERROR_STR]; - p = msg; + p = msg; end = msg + NXT_MAX_ERROR_STR; if (nxt_log_prefix != NULL) { - p = nxt_cpystrn(p, nxt_log_prefix, end - p); + p = nxt_cpystrn(p, nxt_log_prefix, end - p); *p++ = ':'; *p++ = ' '; } @@ -87,7 +69,7 @@ nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...) #endif p = nxt_sprintf(p, end, (log->ident != 0) ? "[%V] *%D " : "[%V] ", - &nxt_log_levels[level], log->ident); + &nxt_log_levels[level], log->ident); va_start(args, fmt); p = nxt_vsprintf(p, end, fmt, args); diff --git a/src/nxt_log.h b/src/nxt_log.h index aa2fe673e..12a4a93b9 100644 --- a/src/nxt_log.h +++ b/src/nxt_log.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,120 +7,118 @@ #define _NXT_LOG_H_INCLUDED_ -#define NXT_LOG_ALERT 0 -#define NXT_LOG_ERR 1 -#define NXT_LOG_WARN 2 -#define NXT_LOG_NOTICE 3 -#define NXT_LOG_INFO 4 -#define NXT_LOG_DEBUG 5 +#define NXT_LOG_ALERT 0 +#define NXT_LOG_ERR 1 +#define NXT_LOG_WARN 2 +#define NXT_LOG_NOTICE 3 +#define NXT_LOG_INFO 4 +#define NXT_LOG_DEBUG 5 -#define NXT_MAX_ERROR_STR 2048 +#define NXT_MAX_ERROR_STR 2048 -typedef void nxt_cdecl (*nxt_log_handler_t)(nxt_uint_t level, nxt_log_t *log, - const char *fmt, ...); +typedef void nxt_cdecl ( + *nxt_log_handler_t)(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...); typedef u_char *(*nxt_log_ctx_handler_t)(void *ctx, u_char *pos, u_char *end); - struct nxt_log_s { - uint32_t level; - uint32_t ident; - nxt_log_handler_t handler; - nxt_log_ctx_handler_t ctx_handler; - void *ctx; + uint32_t level; + uint32_t ident; + nxt_log_handler_t handler; + nxt_log_ctx_handler_t ctx_handler; + void *ctx; }; +NXT_EXPORT void +nxt_log_start(const char *name); +NXT_EXPORT nxt_log_t * +nxt_log_set_ctx(nxt_log_t *log, nxt_log_ctx_handler_t handler, void *ctx); -NXT_EXPORT void nxt_log_start(const char *name); -NXT_EXPORT nxt_log_t *nxt_log_set_ctx(nxt_log_t *log, - nxt_log_ctx_handler_t handler, void *ctx); - -NXT_EXPORT void nxt_cdecl nxt_log_handler(nxt_uint_t level, nxt_log_t *log, - const char *fmt, ...); +NXT_EXPORT void nxt_cdecl +nxt_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...); -#define nxt_log_level_enough(log, level) \ - ((log)->level >= (level)) +#define nxt_log_level_enough(log, level) ((log)->level >= (level)) -#define nxt_alert(task, ...) \ - do { \ - nxt_log_t *_log = (task)->log; \ - \ - _log->handler(NXT_LOG_ALERT, _log, __VA_ARGS__); \ +#define nxt_alert(task, ...) \ + do { \ + nxt_log_t *_log = (task)->log; \ + \ + _log->handler(NXT_LOG_ALERT, _log, __VA_ARGS__); \ } while (0) -#define nxt_log(task, _level, ...) \ - do { \ - nxt_log_t *_log = (task)->log; \ - nxt_uint_t _level_ = (_level); \ - \ - if (nxt_slow_path(_log->level >= _level_)) { \ - _log->handler(_level_, _log, __VA_ARGS__); \ - } \ +#define nxt_log(task, _level, ...) \ + do { \ + nxt_log_t *_log = (task)->log; \ + nxt_uint_t _level_ = (_level); \ + \ + if (nxt_slow_path(_log->level >= _level_)) { \ + _log->handler(_level_, _log, __VA_ARGS__); \ + } \ } while (0) -#define nxt_trace(task, ...) \ - do { \ - nxt_log_t *_log = (task)->log; \ - \ - if (nxt_slow_path(_log->level >= NXT_LOG_NOTICE || nxt_trace)) { \ - _log->handler(NXT_LOG_NOTICE, _log, __VA_ARGS__); \ - } \ +#define nxt_trace(task, ...) \ + do { \ + nxt_log_t *_log = (task)->log; \ + \ + if (nxt_slow_path(_log->level >= NXT_LOG_NOTICE || nxt_trace)) { \ + _log->handler(NXT_LOG_NOTICE, _log, __VA_ARGS__); \ + } \ } while (0) -#define nxt_log_alert(_log, ...) \ - do { \ - nxt_log_t *_log_ = (_log); \ - \ - _log_->handler(NXT_LOG_ALERT, _log_, __VA_ARGS__); \ +#define nxt_log_alert(_log, ...) \ + do { \ + nxt_log_t *_log_ = (_log); \ + \ + _log_->handler(NXT_LOG_ALERT, _log_, __VA_ARGS__); \ } while (0) -#define nxt_log_error(_level, _log, ...) \ - do { \ - nxt_log_t *_log_ = (_log); \ - nxt_uint_t _level_ = (_level); \ - \ - if (nxt_slow_path(_log_->level >= _level_)) { \ - _log_->handler(_level_, _log_, __VA_ARGS__); \ - } \ +#define nxt_log_error(_level, _log, ...) \ + do { \ + nxt_log_t *_log_ = (_log); \ + nxt_uint_t _level_ = (_level); \ + \ + if (nxt_slow_path(_log_->level >= _level_)) { \ + _log_->handler(_level_, _log_, __VA_ARGS__); \ + } \ } while (0) #if (NXT_DEBUG) -#define nxt_debug(task, ...) \ - do { \ - nxt_log_t *_log = (task)->log; \ - \ - if (nxt_slow_path(_log->level == NXT_LOG_DEBUG || nxt_debug)) { \ - _log->handler(NXT_LOG_DEBUG, _log, __VA_ARGS__); \ - } \ +#define nxt_debug(task, ...) \ + do { \ + nxt_log_t *_log = (task)->log; \ + \ + if (nxt_slow_path(_log->level == NXT_LOG_DEBUG || nxt_debug)) { \ + _log->handler(NXT_LOG_DEBUG, _log, __VA_ARGS__); \ + } \ } while (0) -#define nxt_log_debug(_log, ...) \ - do { \ - nxt_log_t *_log_ = (_log); \ - \ - if (nxt_slow_path(_log_->level == NXT_LOG_DEBUG || nxt_debug)) { \ - _log_->handler(NXT_LOG_DEBUG, _log_, __VA_ARGS__); \ - } \ +#define nxt_log_debug(_log, ...) \ + do { \ + nxt_log_t *_log_ = (_log); \ + \ + if (nxt_slow_path(_log_->level == NXT_LOG_DEBUG || nxt_debug)) { \ + _log_->handler(NXT_LOG_DEBUG, _log_, __VA_ARGS__); \ + } \ } while (0) -#define nxt_assert(c) \ - do { \ - if (nxt_slow_path(!(c))) { \ - nxt_thread_log_alert("%s:%d assertion failed: %s", \ - __FILE__, __LINE__, #c); \ - nxt_abort(); \ - } \ +#define nxt_assert(c) \ + do { \ + if (nxt_slow_path(!(c))) { \ + nxt_thread_log_alert("%s:%d assertion failed: %s", __FILE__, \ + __LINE__, #c); \ + nxt_abort(); \ + } \ } while (0) #else @@ -137,8 +134,7 @@ NXT_EXPORT void nxt_cdecl nxt_log_handler(nxt_uint_t level, nxt_log_t *log, #if (NXT_DEBUG_ALLOC) -#define nxt_debug_alloc(...) \ - nxt_thread_log_debug(__VA_ARGS__) +#define nxt_debug_alloc(...) nxt_thread_log_debug(__VA_ARGS__) #else @@ -147,22 +143,20 @@ NXT_EXPORT void nxt_cdecl nxt_log_handler(nxt_uint_t level, nxt_log_t *log, #endif -#define nxt_main_log_alert(...) \ - nxt_log_alert(&nxt_main_log, __VA_ARGS__) +#define nxt_main_log_alert(...) nxt_log_alert(&nxt_main_log, __VA_ARGS__) -#define nxt_main_log_error(level, ...) \ +#define nxt_main_log_error(level, ...) \ nxt_log_error(level, &nxt_main_log, __VA_ARGS__) -#define nxt_main_log_debug(...) \ - nxt_log_debug(&nxt_main_log, __VA_ARGS__) +#define nxt_main_log_debug(...) nxt_log_debug(&nxt_main_log, __VA_ARGS__) -NXT_EXPORT extern nxt_uint_t nxt_debug; -NXT_EXPORT extern nxt_uint_t nxt_trace; -NXT_EXPORT extern nxt_log_t nxt_main_log; -NXT_EXPORT extern nxt_str_t nxt_log_levels[]; +NXT_EXPORT extern nxt_uint_t nxt_debug; +NXT_EXPORT extern nxt_uint_t nxt_trace; +NXT_EXPORT extern nxt_log_t nxt_main_log; +NXT_EXPORT extern nxt_str_t nxt_log_levels[]; #endif /* _NXT_LOG_H_INCLUDED_ */ diff --git a/src/nxt_log_moderation.c b/src/nxt_log_moderation.c index 95f9cbfec..f8a9a071e 100644 --- a/src/nxt_log_moderation.c +++ b/src/nxt_log_moderation.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,17 +6,15 @@ #include -static void nxt_log_moderate_timer_handler(nxt_task_t *task, void *obj, - void *data); - +static void +nxt_log_moderate_timer_handler(nxt_task_t *task, void *obj, void *data); nxt_bool_t -nxt_log_moderate_allow(nxt_log_moderation_t *mod) -{ +nxt_log_moderate_allow(nxt_log_moderation_t *mod) { nxt_uint_t n; nxt_time_t now; nxt_bool_t allow, timer; - nxt_thread_t *thr; + nxt_thread_t *thr; thr = nxt_thread(); now = nxt_thread_time(thr); @@ -30,17 +27,15 @@ nxt_log_moderate_allow(nxt_log_moderation_t *mod) n = mod->count++; if (now != mod->last) { - if (n <= mod->limit) { - mod->last = now; + mod->last = now; mod->count = 1; - allow = 1; + allow = 1; } /* "n > mod->limit" means that timer has already been set. */ } else { - if (n < mod->limit) { allow = 1; @@ -50,7 +45,7 @@ nxt_log_moderate_allow(nxt_log_moderation_t *mod) * capable to fail an operation 2^32 times per second. * This can be fixed by storing mod->count as uint64_t. */ - timer = 1; + timer = 1; mod->pid = nxt_pid; } } @@ -59,9 +54,9 @@ nxt_log_moderate_allow(nxt_log_moderation_t *mod) if (timer) { mod->timer.work_queue = &thr->engine->fast_work_queue; - mod->timer.handler = nxt_log_moderate_timer_handler; - mod->timer.log = &nxt_main_log; - mod->timer.task = &nxt_main_task; + mod->timer.handler = nxt_log_moderate_timer_handler; + mod->timer.log = &nxt_main_log; + mod->timer.task = &nxt_main_task; nxt_timer_add(thr->engine, &mod->timer, 1000); } @@ -69,29 +64,27 @@ nxt_log_moderate_allow(nxt_log_moderation_t *mod) return allow; } - static void -nxt_log_moderate_timer_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_log_moderate_timer_handler(nxt_task_t *task, void *obj, void *data) { nxt_bool_t msg; - nxt_timer_t *ev; + nxt_timer_t *ev; nxt_atomic_uint_t n; - nxt_log_moderation_t *mod; + nxt_log_moderation_t *mod; - ev = obj; + ev = obj; mod = nxt_timer_data(ev, nxt_log_moderation_t, timer); nxt_thread_spin_lock(&mod->lock); - mod->last = nxt_thread_time(task->thread); - n = mod->count; + mod->last = nxt_thread_time(task->thread); + n = mod->count; mod->count = 0; - msg = (mod->pid == nxt_pid); + msg = (mod->pid == nxt_pid); nxt_thread_spin_unlock(&mod->lock); if (msg) { - nxt_log_error(mod->level, &nxt_main_log, "%s %uA times", - mod->msg, n - mod->limit); + nxt_log_error(mod->level, &nxt_main_log, "%s %uA times", mod->msg, + n - mod->limit); } } diff --git a/src/nxt_log_moderation.h b/src/nxt_log_moderation.h index c6033201f..0c3d8c53c 100644 --- a/src/nxt_log_moderation.h +++ b/src/nxt_log_moderation.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,43 +6,42 @@ #ifndef _NXT_LOG_MODERATION_H_INCLUDED_ #define _NXT_LOG_MODERATION_H_INCLUDED_ - typedef struct { - uint32_t level; - uint32_t limit; - const char *msg; - nxt_thread_spinlock_t lock; - nxt_pid_t pid; - nxt_uint_t count; - nxt_time_t last; - nxt_timer_t timer; + uint32_t level; + uint32_t limit; + const char *msg; + nxt_thread_spinlock_t lock; + nxt_pid_t pid; + nxt_uint_t count; + nxt_time_t last; + nxt_timer_t timer; } nxt_log_moderation_t; - -#define NXT_LOG_MODERATION 0, -1, 0, 0, NXT_TIMER +#define NXT_LOG_MODERATION 0, -1, 0, 0, NXT_TIMER -#define nxt_log_alert_moderate(_mod, _log, ...) \ - do { \ - nxt_log_t *_log_ = _log; \ - \ - if (nxt_log_moderate_allow(_mod)) { \ - _log_->handler(NXT_LOG_ALERT, _log_, __VA_ARGS__); \ - } \ +#define nxt_log_alert_moderate(_mod, _log, ...) \ + do { \ + nxt_log_t *_log_ = _log; \ + \ + if (nxt_log_moderate_allow(_mod)) { \ + _log_->handler(NXT_LOG_ALERT, _log_, __VA_ARGS__); \ + } \ } while (0) -#define nxt_log_moderate(_mod, _level, _log, ...) \ - do { \ - nxt_log_t *_log_ = _log; \ - \ - if (_log_->level >= (_level) && nxt_log_moderate_allow(_mod)) { \ - _log_->handler(_level, _log_, __VA_ARGS__); \ - } \ +#define nxt_log_moderate(_mod, _level, _log, ...) \ + do { \ + nxt_log_t *_log_ = _log; \ + \ + if (_log_->level >= (_level) && nxt_log_moderate_allow(_mod)) { \ + _log_->handler(_level, _log_, __VA_ARGS__); \ + } \ } while (0) -nxt_bool_t nxt_log_moderate_allow(nxt_log_moderation_t *mod); +nxt_bool_t +nxt_log_moderate_allow(nxt_log_moderation_t *mod); #endif /* _NXT_LOG_MODERATION_H_INCLUDED_ */ diff --git a/src/nxt_lvlhsh.c b/src/nxt_lvlhsh.c index 7a8b3ddae..8df784fe5 100644 --- a/src/nxt_lvlhsh.c +++ b/src/nxt_lvlhsh.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -43,149 +42,138 @@ * several levels. */ -#define nxt_lvlhsh_is_bucket(p) \ - ((uintptr_t) (p) & 1) +#define nxt_lvlhsh_is_bucket(p) ((uintptr_t) (p) & 1) -#define nxt_lvlhsh_count_inc(n) \ - n = (void *) ((uintptr_t) (n) + 2) +#define nxt_lvlhsh_count_inc(n) n = (void *) ((uintptr_t) (n) + 2) -#define nxt_lvlhsh_count_dec(n) \ - n = (void *) ((uintptr_t) (n) - 2) +#define nxt_lvlhsh_count_dec(n) n = (void *) ((uintptr_t) (n) - 2) -#define nxt_lvlhsh_level_size(proto, nlvl) \ - ((uintptr_t) 1 << proto->shift[nlvl]) +#define nxt_lvlhsh_level_size(proto, nlvl) ((uintptr_t) 1 << proto->shift[nlvl]) -#define nxt_lvlhsh_level(lvl, mask) \ - (void **) ((uintptr_t) lvl & (~mask << 2)) +#define nxt_lvlhsh_level(lvl, mask) (void **) ((uintptr_t) lvl & (~mask << 2)) -#define nxt_lvlhsh_level_entries(lvl, mask) \ - ((uintptr_t) lvl & (mask << 1)) +#define nxt_lvlhsh_level_entries(lvl, mask) ((uintptr_t) lvl & (mask << 1)) -#define nxt_lvlhsh_store_bucket(slot, bkt) \ +#define nxt_lvlhsh_store_bucket(slot, bkt) \ slot = (void **) ((uintptr_t) bkt | 2 | 1) -#define nxt_lvlhsh_bucket_size(proto) \ - proto->bucket_size +#define nxt_lvlhsh_bucket_size(proto) proto->bucket_size -#define nxt_lvlhsh_bucket(proto, bkt) \ +#define nxt_lvlhsh_bucket(proto, bkt) \ (uint32_t *) ((uintptr_t) bkt & ~(uintptr_t) proto->bucket_mask) -#define nxt_lvlhsh_bucket_entries(proto, bkt) \ +#define nxt_lvlhsh_bucket_entries(proto, bkt) \ (((uintptr_t) bkt & (uintptr_t) proto->bucket_mask) >> 1) -#define nxt_lvlhsh_bucket_end(proto, bkt) \ - &bkt[proto->bucket_end] +#define nxt_lvlhsh_bucket_end(proto, bkt) &bkt[proto->bucket_end] -#define nxt_lvlhsh_free_entry(e) \ - (!(nxt_lvlhsh_valid_entry(e))) +#define nxt_lvlhsh_free_entry(e) (!(nxt_lvlhsh_valid_entry(e))) -#define nxt_lvlhsh_next_bucket(proto, bkt) \ - ((void **) &bkt[proto->bucket_end]) +#define nxt_lvlhsh_next_bucket(proto, bkt) ((void **) &bkt[proto->bucket_end]) #if (NXT_64BIT) -#define nxt_lvlhsh_valid_entry(e) \ - (((e)[0] | (e)[1]) != 0) +#define nxt_lvlhsh_valid_entry(e) (((e)[0] | (e)[1]) != 0) -#define nxt_lvlhsh_entry_value(e) \ - (void *) (((uintptr_t) (e)[1] << 32) + (e)[0]) +#define nxt_lvlhsh_entry_value(e) (void *) (((uintptr_t) (e)[1] << 32) + (e)[0]) -#define nxt_lvlhsh_set_entry_value(e, n) \ - (e)[0] = (uint32_t) (uintptr_t) n; \ +#define nxt_lvlhsh_set_entry_value(e, n) \ + (e)[0] = (uint32_t) (uintptr_t) n; \ (e)[1] = (uint32_t) ((uintptr_t) n >> 32) -#define nxt_lvlhsh_entry_key(e) \ - (e)[2] +#define nxt_lvlhsh_entry_key(e) (e)[2] -#define nxt_lvlhsh_set_entry_key(e, n) \ - (e)[2] = n +#define nxt_lvlhsh_set_entry_key(e, n) (e)[2] = n #else -#define nxt_lvlhsh_valid_entry(e) \ - ((e)[0] != 0) +#define nxt_lvlhsh_valid_entry(e) ((e)[0] != 0) -#define nxt_lvlhsh_entry_value(e) \ - (void *) (e)[0] +#define nxt_lvlhsh_entry_value(e) (void *) (e)[0] -#define nxt_lvlhsh_set_entry_value(e, n) \ - (e)[0] = (uint32_t) n +#define nxt_lvlhsh_set_entry_value(e, n) (e)[0] = (uint32_t) n -#define nxt_lvlhsh_entry_key(e) \ - (e)[1] +#define nxt_lvlhsh_entry_key(e) (e)[1] -#define nxt_lvlhsh_set_entry_key(e, n) \ - (e)[1] = n +#define nxt_lvlhsh_set_entry_key(e, n) (e)[1] = n #endif -#define NXT_LVLHSH_BUCKET_DONE ((void *) -1) - +#define NXT_LVLHSH_BUCKET_DONE ((void *) -1) typedef struct { - const nxt_lvlhsh_proto_t *proto; - void *pool; - uint32_t retrieve; /* 1 bit */ + const nxt_lvlhsh_proto_t *proto; + void *pool; + uint32_t retrieve; /* 1 bit */ } nxt_lvlhsh_peek_t; - -static nxt_int_t nxt_lvlhsh_level_find(nxt_lvlhsh_query_t *lhq, void **lvl, - uint32_t key, nxt_uint_t nlvl); -static nxt_int_t nxt_lvlhsh_bucket_find(nxt_lvlhsh_query_t *lhq, void **bkt); -static nxt_int_t nxt_lvlhsh_new_bucket(nxt_lvlhsh_query_t *lhq, void **slot); -static nxt_int_t nxt_lvlhsh_level_insert(nxt_lvlhsh_query_t *lhq, - void **slot, uint32_t key, nxt_uint_t nlvl); -static nxt_int_t nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, - void **slot, uint32_t key, nxt_int_t nlvl); -static nxt_int_t nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, - void **slot, nxt_uint_t nlvl, uint32_t *bucket); -static nxt_int_t nxt_lvlhsh_level_convertion_insert(nxt_lvlhsh_query_t *lhq, - void **parent, uint32_t key, nxt_uint_t nlvl); -static nxt_int_t nxt_lvlhsh_bucket_convertion_insert(nxt_lvlhsh_query_t *lhq, - void **slot, uint32_t key, nxt_int_t nlvl); -static nxt_int_t nxt_lvlhsh_free_level(nxt_lvlhsh_query_t *lhq, void **level, - nxt_uint_t size); -static nxt_int_t nxt_lvlhsh_level_delete(nxt_lvlhsh_query_t *lhq, void **slot, +static nxt_int_t +nxt_lvlhsh_level_find(nxt_lvlhsh_query_t *lhq, void **lvl, uint32_t key, + nxt_uint_t nlvl); +static nxt_int_t +nxt_lvlhsh_bucket_find(nxt_lvlhsh_query_t *lhq, void **bkt); +static nxt_int_t +nxt_lvlhsh_new_bucket(nxt_lvlhsh_query_t *lhq, void **slot); +static nxt_int_t +nxt_lvlhsh_level_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, + nxt_uint_t nlvl); +static nxt_int_t +nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, + nxt_int_t nlvl); +static nxt_int_t +nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, void **slot, + nxt_uint_t nlvl, uint32_t *bucket); +static nxt_int_t +nxt_lvlhsh_level_convertion_insert(nxt_lvlhsh_query_t *lhq, void **parent, uint32_t key, nxt_uint_t nlvl); -static nxt_int_t nxt_lvlhsh_bucket_delete(nxt_lvlhsh_query_t *lhq, void **bkt); -static void *nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, - nxt_uint_t nlvl, nxt_uint_t shift); -static void *nxt_lvlhsh_bucket_each(nxt_lvlhsh_each_t *lhe); -static void *nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **level, +static nxt_int_t +nxt_lvlhsh_bucket_convertion_insert(nxt_lvlhsh_query_t *lhq, void **slot, + uint32_t key, nxt_int_t nlvl); +static nxt_int_t +nxt_lvlhsh_free_level(nxt_lvlhsh_query_t *lhq, void **level, nxt_uint_t size); +static nxt_int_t +nxt_lvlhsh_level_delete(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, nxt_uint_t nlvl); -static void *nxt_lvlhsh_bucket_peek(nxt_lvlhsh_peek_t *peek, void **bkt); - +static nxt_int_t +nxt_lvlhsh_bucket_delete(nxt_lvlhsh_query_t *lhq, void **bkt); +static void * +nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, nxt_uint_t nlvl, + nxt_uint_t shift); +static void * +nxt_lvlhsh_bucket_each(nxt_lvlhsh_each_t *lhe); +static void * +nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **level, nxt_uint_t nlvl); +static void * +nxt_lvlhsh_bucket_peek(nxt_lvlhsh_peek_t *peek, void **bkt); nxt_int_t -nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) -{ - void *slot; +nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) { + void *slot; slot = lh->slot; if (nxt_fast_path(slot != NULL)) { - if (nxt_lvlhsh_is_bucket(slot)) { return nxt_lvlhsh_bucket_find(lhq, slot); } @@ -196,23 +184,20 @@ nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) return NXT_DECLINED; } - static nxt_int_t nxt_lvlhsh_level_find(nxt_lvlhsh_query_t *lhq, void **lvl, uint32_t key, - nxt_uint_t nlvl) -{ - void **slot; - uintptr_t mask; - nxt_uint_t shift; + nxt_uint_t nlvl) { + void **slot; + uintptr_t mask; + nxt_uint_t shift; shift = lhq->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << shift) - 1; + mask = ((uintptr_t) 1 << shift) - 1; - lvl = nxt_lvlhsh_level(lvl, mask); + lvl = nxt_lvlhsh_level(lvl, mask); slot = lvl[key & mask]; if (slot != NULL) { - if (nxt_lvlhsh_is_bucket(slot)) { return nxt_lvlhsh_bucket_find(lhq, slot); } @@ -223,25 +208,22 @@ nxt_lvlhsh_level_find(nxt_lvlhsh_query_t *lhq, void **lvl, uint32_t key, return NXT_DECLINED; } - static nxt_int_t -nxt_lvlhsh_bucket_find(nxt_lvlhsh_query_t *lhq, void **bkt) -{ - void *value; - uint32_t *bucket, *e; - nxt_uint_t n; +nxt_lvlhsh_bucket_find(nxt_lvlhsh_query_t *lhq, void **bkt) { + void *value; + uint32_t *bucket, *e; + nxt_uint_t n; do { bucket = nxt_lvlhsh_bucket(lhq->proto, bkt); - n = nxt_lvlhsh_bucket_entries(lhq->proto, bkt); - e = bucket; + n = nxt_lvlhsh_bucket_entries(lhq->proto, bkt); + e = bucket; do { if (nxt_lvlhsh_valid_entry(e)) { n--; if (nxt_lvlhsh_entry_key(e) == lhq->key_hash) { - value = nxt_lvlhsh_entry_value(e); if (lhq->proto->test(lhq, value) == NXT_OK) { @@ -263,14 +245,11 @@ nxt_lvlhsh_bucket_find(nxt_lvlhsh_query_t *lhq, void **bkt) return NXT_DECLINED; } - nxt_int_t -nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) -{ - uint32_t key; +nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) { + uint32_t key; if (nxt_fast_path(lh->slot != NULL)) { - key = lhq->key_hash; if (nxt_lvlhsh_is_bucket(lh->slot)) { @@ -283,16 +262,13 @@ nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) return nxt_lvlhsh_new_bucket(lhq, &lh->slot); } - static nxt_int_t -nxt_lvlhsh_new_bucket(nxt_lvlhsh_query_t *lhq, void **slot) -{ - uint32_t *bucket; +nxt_lvlhsh_new_bucket(nxt_lvlhsh_query_t *lhq, void **slot) { + uint32_t *bucket; bucket = lhq->proto->alloc(lhq->pool, nxt_lvlhsh_bucket_size(lhq->proto)); if (nxt_fast_path(bucket != NULL)) { - nxt_lvlhsh_set_entry_value(bucket, lhq->value); nxt_lvlhsh_set_entry_key(bucket, lhq->key_hash); @@ -306,20 +282,18 @@ nxt_lvlhsh_new_bucket(nxt_lvlhsh_query_t *lhq, void **slot) return NXT_ERROR; } - static nxt_int_t nxt_lvlhsh_level_insert(nxt_lvlhsh_query_t *lhq, void **parent, uint32_t key, - nxt_uint_t nlvl) -{ - void **slot, **lvl; - nxt_int_t ret; - uintptr_t mask; - nxt_uint_t shift; + nxt_uint_t nlvl) { + void **slot, **lvl; + nxt_int_t ret; + uintptr_t mask; + nxt_uint_t shift; shift = lhq->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << shift) - 1; + mask = ((uintptr_t) 1 << shift) - 1; - lvl = nxt_lvlhsh_level(*parent, mask); + lvl = nxt_lvlhsh_level(*parent, mask); slot = &lvl[key & mask]; if (*slot != NULL) { @@ -341,40 +315,35 @@ nxt_lvlhsh_level_insert(nxt_lvlhsh_query_t *lhq, void **parent, uint32_t key, return ret; } - static nxt_int_t nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, - nxt_int_t nlvl) -{ - void **bkt, **vacant_bucket, *value; - uint32_t *bucket, *e, *vacant_entry; + nxt_int_t nlvl) { + void **bkt, **vacant_bucket, *value; + uint32_t *bucket, *e, *vacant_entry; nxt_int_t ret; uintptr_t n; - const void *new_value; - const nxt_lvlhsh_proto_t *proto; + const void *new_value; + const nxt_lvlhsh_proto_t *proto; - bkt = slot; - vacant_entry = NULL; + bkt = slot; + vacant_entry = NULL; vacant_bucket = NULL; - proto = lhq->proto; + proto = lhq->proto; /* Search for duplicate entry in bucket chain. */ do { bucket = nxt_lvlhsh_bucket(proto, *bkt); - n = nxt_lvlhsh_bucket_entries(proto, *bkt); - e = bucket; + n = nxt_lvlhsh_bucket_entries(proto, *bkt); + e = bucket; do { if (nxt_lvlhsh_valid_entry(e)) { - if (nxt_lvlhsh_entry_key(e) == lhq->key_hash) { - value = nxt_lvlhsh_entry_value(e); if (proto->test(lhq, value) == NXT_OK) { - - new_value = lhq->value; + new_value = lhq->value; lhq->value = value; if (lhq->replace) { @@ -395,7 +364,7 @@ nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, * and continue to search for duplicate entry. */ if (vacant_entry == NULL) { - vacant_entry = e; + vacant_entry = e; vacant_bucket = bkt; } } @@ -410,7 +379,7 @@ nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, * and continue to search for duplicate entry. */ if (vacant_entry == NULL) { - vacant_entry = e; + vacant_entry = e; vacant_bucket = bkt; } } @@ -432,7 +401,6 @@ nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, nlvl++; if (nxt_fast_path(proto->shift[nlvl] != 0)) { - ret = nxt_lvlhsh_convert_bucket_to_level(lhq, slot, nlvl, bucket); if (nxt_fast_path(ret == NXT_OK)) { @@ -447,20 +415,18 @@ nxt_lvlhsh_bucket_insert(nxt_lvlhsh_query_t *lhq, void **slot, uint32_t key, return nxt_lvlhsh_new_bucket(lhq, bkt); } - static nxt_int_t nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, void **slot, - nxt_uint_t nlvl, uint32_t *bucket) -{ - void *lvl, **level; - uint32_t *e, *end, key; + nxt_uint_t nlvl, uint32_t *bucket) { + void *lvl, **level; + uint32_t *e, *end, key; nxt_int_t ret; nxt_uint_t i, shift, size; nxt_lvlhsh_query_t q; - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; proto = lhq->proto; - size = nxt_lvlhsh_level_size(proto, nlvl); + size = nxt_lvlhsh_level_size(proto, nlvl); lvl = proto->alloc(lhq->pool, size * (sizeof(void *))); @@ -486,11 +452,10 @@ nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, void **slot, end = nxt_lvlhsh_bucket_end(proto, bucket); for (e = bucket; e < end; e += NXT_LVLHSH_ENTRY_SIZE) { - - q.proto = proto; - q.pool = lhq->pool; - q.value = nxt_lvlhsh_entry_value(e); - key = nxt_lvlhsh_entry_key(e); + q.proto = proto; + q.pool = lhq->pool; + q.value = nxt_lvlhsh_entry_value(e); + key = nxt_lvlhsh_entry_key(e); q.key_hash = key; ret = nxt_lvlhsh_level_convertion_insert(&q, &lvl, key >> shift, nlvl); @@ -507,20 +472,18 @@ nxt_lvlhsh_convert_bucket_to_level(nxt_lvlhsh_query_t *lhq, void **slot, return NXT_OK; } - static nxt_int_t nxt_lvlhsh_level_convertion_insert(nxt_lvlhsh_query_t *lhq, void **parent, - uint32_t key, nxt_uint_t nlvl) -{ - void **slot, **lvl; - nxt_int_t ret; - uintptr_t mask; - nxt_uint_t shift; + uint32_t key, nxt_uint_t nlvl) { + void **slot, **lvl; + nxt_int_t ret; + uintptr_t mask; + nxt_uint_t shift; shift = lhq->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << shift) - 1; + mask = ((uintptr_t) 1 << shift) - 1; - lvl = nxt_lvlhsh_level(*parent, mask); + lvl = nxt_lvlhsh_level(*parent, mask); slot = &lvl[key & mask]; if (*slot == NULL) { @@ -538,7 +501,6 @@ nxt_lvlhsh_level_convertion_insert(nxt_lvlhsh_query_t *lhq, void **parent, return nxt_lvlhsh_bucket_convertion_insert(lhq, slot, key >> shift, nlvl); } - /* * The special bucket insertion procedure is required because during * convertion lhq->key contains garbage values and the test function @@ -548,24 +510,22 @@ nxt_lvlhsh_level_convertion_insert(nxt_lvlhsh_query_t *lhq, void **parent, static nxt_int_t nxt_lvlhsh_bucket_convertion_insert(nxt_lvlhsh_query_t *lhq, void **slot, - uint32_t key, nxt_int_t nlvl) -{ - void **bkt; - uint32_t *bucket, *e; + uint32_t key, nxt_int_t nlvl) { + void **bkt; + uint32_t *bucket, *e; nxt_int_t ret; uintptr_t n; - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; - bkt = slot; + bkt = slot; proto = lhq->proto; do { bucket = nxt_lvlhsh_bucket(proto, *bkt); - n = nxt_lvlhsh_bucket_entries(proto, *bkt); - e = bucket + n * NXT_LVLHSH_ENTRY_SIZE; + n = nxt_lvlhsh_bucket_entries(proto, *bkt); + e = bucket + n * NXT_LVLHSH_ENTRY_SIZE; if (nxt_fast_path(e < nxt_lvlhsh_bucket_end(proto, bucket))) { - nxt_lvlhsh_set_entry_value(e, lhq->value); nxt_lvlhsh_set_entry_key(e, lhq->key_hash); nxt_lvlhsh_count_inc(*bkt); @@ -582,7 +542,6 @@ nxt_lvlhsh_bucket_convertion_insert(nxt_lvlhsh_query_t *lhq, void **slot, nlvl++; if (nxt_fast_path(proto->shift[nlvl] != 0)) { - ret = nxt_lvlhsh_convert_bucket_to_level(lhq, slot, nlvl, bucket); if (nxt_fast_path(ret == NXT_OK)) { @@ -597,17 +556,14 @@ nxt_lvlhsh_bucket_convertion_insert(nxt_lvlhsh_query_t *lhq, void **slot, return nxt_lvlhsh_new_bucket(lhq, bkt); } - static nxt_int_t -nxt_lvlhsh_free_level(nxt_lvlhsh_query_t *lhq, void **level, nxt_uint_t size) -{ +nxt_lvlhsh_free_level(nxt_lvlhsh_query_t *lhq, void **level, nxt_uint_t size) { nxt_uint_t i; - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; proto = lhq->proto; for (i = 0; i < size; i++) { - if (level[i] != NULL) { /* * Chained buckets are not possible here, since even @@ -623,12 +579,9 @@ nxt_lvlhsh_free_level(nxt_lvlhsh_query_t *lhq, void **level, nxt_uint_t size) return NXT_ERROR; } - nxt_int_t -nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) -{ +nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) { if (nxt_fast_path(lh->slot != NULL)) { - if (nxt_lvlhsh_is_bucket(lh->slot)) { return nxt_lvlhsh_bucket_delete(lhq, &lh->slot); } @@ -639,30 +592,27 @@ nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq) return NXT_DECLINED; } - static nxt_int_t nxt_lvlhsh_level_delete(nxt_lvlhsh_query_t *lhq, void **parent, uint32_t key, - nxt_uint_t nlvl) -{ - void **slot, **lvl; - uintptr_t mask; - nxt_int_t ret; - nxt_uint_t shift; + nxt_uint_t nlvl) { + void **slot, **lvl; + uintptr_t mask; + nxt_int_t ret; + nxt_uint_t shift; shift = lhq->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << shift) - 1; + mask = ((uintptr_t) 1 << shift) - 1; - lvl = nxt_lvlhsh_level(*parent, mask); + lvl = nxt_lvlhsh_level(*parent, mask); slot = &lvl[key & mask]; if (*slot != NULL) { - if (nxt_lvlhsh_is_bucket(*slot)) { ret = nxt_lvlhsh_bucket_delete(lhq, slot); } else { key >>= shift; - ret = nxt_lvlhsh_level_delete(lhq, slot, key, nlvl + 1); + ret = nxt_lvlhsh_level_delete(lhq, slot, key, nlvl + 1); } if (*slot == NULL) { @@ -680,31 +630,26 @@ nxt_lvlhsh_level_delete(nxt_lvlhsh_query_t *lhq, void **parent, uint32_t key, return NXT_DECLINED; } - static nxt_int_t -nxt_lvlhsh_bucket_delete(nxt_lvlhsh_query_t *lhq, void **bkt) -{ - void *value; - uint32_t *bucket, *e; +nxt_lvlhsh_bucket_delete(nxt_lvlhsh_query_t *lhq, void **bkt) { + void *value; + uint32_t *bucket, *e; uintptr_t n; - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; proto = lhq->proto; do { bucket = nxt_lvlhsh_bucket(proto, *bkt); - n = nxt_lvlhsh_bucket_entries(proto, *bkt); - e = bucket; + n = nxt_lvlhsh_bucket_entries(proto, *bkt); + e = bucket; do { if (nxt_lvlhsh_valid_entry(e)) { - if (nxt_lvlhsh_entry_key(e) == lhq->key_hash) { - value = nxt_lvlhsh_entry_value(e); if (proto->test(lhq, value) == NXT_OK) { - if (nxt_lvlhsh_bucket_entries(proto, *bkt) == 1) { *bkt = *nxt_lvlhsh_next_bucket(proto, bucket); proto->free(lhq->pool, bucket); @@ -734,11 +679,9 @@ nxt_lvlhsh_bucket_delete(nxt_lvlhsh_query_t *lhq, void **bkt) return NXT_DECLINED; } - void * -nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe) -{ - void **slot; +nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe) { + void **slot; if (lhe->bucket == NXT_LVLHSH_BUCKET_DONE) { slot = lh->slot; @@ -749,7 +692,6 @@ nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe) } else { if (nxt_slow_path(lhe->bucket == NULL)) { - /* The first iteration only. */ slot = lh->slot; @@ -763,9 +705,9 @@ nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe) goto level; } - lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot); + lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot); lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, slot); - lhe->entry = 0; + lhe->entry = 0; } return nxt_lvlhsh_bucket_each(lhe); @@ -776,32 +718,28 @@ nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe) return nxt_lvlhsh_level_each(lhe, slot, 0, 0); } - static void * nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, nxt_uint_t nlvl, - nxt_uint_t shift) -{ - void **slot, *value; - uintptr_t mask; - nxt_uint_t n, level_shift; + nxt_uint_t shift) { + void **slot, *value; + uintptr_t mask; + nxt_uint_t n, level_shift; level_shift = lhe->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << level_shift) - 1; + mask = ((uintptr_t) 1 << level_shift) - 1; level = nxt_lvlhsh_level(level, mask); do { - n = (lhe->current >> shift) & mask; + n = (lhe->current >> shift) & mask; slot = level[n]; if (slot != NULL) { if (nxt_lvlhsh_is_bucket(slot)) { - if (lhe->bucket != NXT_LVLHSH_BUCKET_DONE) { - - lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot); + lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot); lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, slot); - lhe->entry = 0; + lhe->entry = 0; return nxt_lvlhsh_bucket_each(lhe); } @@ -810,7 +748,7 @@ nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, nxt_uint_t nlvl, } else { value = nxt_lvlhsh_level_each(lhe, slot, nlvl + 1, - shift + level_shift); + shift + level_shift); if (value != NULL) { return value; } @@ -818,7 +756,7 @@ nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, nxt_uint_t nlvl, } lhe->current &= ~(mask << shift); - n = ((n + 1) & mask) << shift; + n = ((n + 1) & mask) << shift; lhe->current |= n; } while (n != 0); @@ -826,16 +764,14 @@ nxt_lvlhsh_level_each(nxt_lvlhsh_each_t *lhe, void **level, nxt_uint_t nlvl, return NULL; } - static nxt_noinline void * -nxt_lvlhsh_bucket_each(nxt_lvlhsh_each_t *lhe) -{ - void *value, **next; - uint32_t *bucket; +nxt_lvlhsh_bucket_each(nxt_lvlhsh_each_t *lhe) { + void *value, **next; + uint32_t *bucket; /* At least one valid entry must present here. */ do { - bucket = &lhe->bucket[lhe->entry]; + bucket = &lhe->bucket[lhe->entry]; lhe->entry += NXT_LVLHSH_ENTRY_SIZE; } while (nxt_lvlhsh_free_entry(bucket)); @@ -851,24 +787,21 @@ nxt_lvlhsh_bucket_each(nxt_lvlhsh_each_t *lhe) : nxt_lvlhsh_bucket(lhe->proto, next); lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, next); - lhe->entry = 0; + lhe->entry = 0; } return value; } - void * -nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto) -{ - void **slot; - nxt_lvlhsh_peek_t peek; +nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto) { + void **slot; + nxt_lvlhsh_peek_t peek; slot = lh->slot; if (slot != NULL) { - - peek.proto = proto; + peek.proto = proto; peek.retrieve = 0; if (nxt_lvlhsh_is_bucket(slot)) { @@ -881,16 +814,14 @@ nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto) return NULL; } - static void * -nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **parent, nxt_uint_t nlvl) -{ - void **slot, **level, *value; - uintptr_t mask; - nxt_uint_t n, shift; +nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **parent, nxt_uint_t nlvl) { + void **slot, **level, *value; + uintptr_t mask; + nxt_uint_t n, shift; shift = peek->proto->shift[nlvl]; - mask = ((uintptr_t) 1 << shift) - 1; + mask = ((uintptr_t) 1 << shift) - 1; level = nxt_lvlhsh_level(*parent, mask); @@ -898,11 +829,10 @@ nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **parent, nxt_uint_t nlvl) /* At least one valid level slot must present here. */ - for ( ;; ) { + for (;;) { slot = &level[n]; if (*slot != NULL) { - if (nxt_lvlhsh_is_bucket(*slot)) { value = nxt_lvlhsh_bucket_peek(peek, slot); @@ -930,22 +860,18 @@ nxt_lvlhsh_level_peek(nxt_lvlhsh_peek_t *peek, void **parent, nxt_uint_t nlvl) } } - static nxt_noinline void * -nxt_lvlhsh_bucket_peek(nxt_lvlhsh_peek_t *peek, void **bkt) -{ - void *value; - uint32_t *bucket, *entry; - const nxt_lvlhsh_proto_t *proto; +nxt_lvlhsh_bucket_peek(nxt_lvlhsh_peek_t *peek, void **bkt) { + void *value; + uint32_t *bucket, *entry; + const nxt_lvlhsh_proto_t *proto; bucket = nxt_lvlhsh_bucket(peek->proto, *bkt); /* At least one valid entry must present here. */ - for (entry = bucket; - nxt_lvlhsh_free_entry(entry); - entry += NXT_LVLHSH_ENTRY_SIZE) - { + for (entry = bucket; nxt_lvlhsh_free_entry(entry); + entry += NXT_LVLHSH_ENTRY_SIZE) { /* void */ } @@ -967,20 +893,17 @@ nxt_lvlhsh_bucket_peek(nxt_lvlhsh_peek_t *peek, void **bkt) return value; } - void * nxt_lvlhsh_retrieve(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, - void *pool) -{ - void **slot; - nxt_lvlhsh_peek_t peek; + void *pool) { + void **slot; + nxt_lvlhsh_peek_t peek; slot = lh->slot; if (slot != NULL) { - - peek.proto = proto; - peek.pool = pool; + peek.proto = proto; + peek.pool = pool; peek.retrieve = 1; if (nxt_lvlhsh_is_bucket(slot)) { diff --git a/src/nxt_lvlhsh.h b/src/nxt_lvlhsh.h index c051081c3..3d12dddfb 100644 --- a/src/nxt_lvlhsh.h +++ b/src/nxt_lvlhsh.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,118 +7,110 @@ #define _NXT_LEVEL_HASH_H_INCLUDED_ -typedef struct nxt_lvlhsh_query_s nxt_lvlhsh_query_t; +typedef struct nxt_lvlhsh_query_s nxt_lvlhsh_query_t; typedef nxt_int_t (*nxt_lvlhsh_test_t)(nxt_lvlhsh_query_t *lhq, void *data); -typedef void *(*nxt_lvlhsh_alloc_t)(void *ctx, size_t size); -typedef void (*nxt_lvlhsh_free_t)(void *ctx, void *p); +typedef void *(*nxt_lvlhsh_alloc_t)(void *ctx, size_t size); +typedef void (*nxt_lvlhsh_free_t)(void *ctx, void *p); #if (NXT_64BIT) -#define NXT_LVLHSH_DEFAULT_BUCKET_SIZE 128 -#define NXT_LVLHSH_ENTRY_SIZE 3 -#define NXT_LVLHSH_BATCH_ALLOC 16 +#define NXT_LVLHSH_DEFAULT_BUCKET_SIZE 128 +#define NXT_LVLHSH_ENTRY_SIZE 3 +#define NXT_LVLHSH_BATCH_ALLOC 16 /* 3 is shift of 64-bit pointer. */ -#define NXT_LVLHSH_MEMALIGN_SHIFT (NXT_MAX_MEMALIGN_SHIFT - 3) +#define NXT_LVLHSH_MEMALIGN_SHIFT (NXT_MAX_MEMALIGN_SHIFT - 3) #else -#define NXT_LVLHSH_DEFAULT_BUCKET_SIZE 64 -#define NXT_LVLHSH_ENTRY_SIZE 2 -#define NXT_LVLHSH_BATCH_ALLOC 8 +#define NXT_LVLHSH_DEFAULT_BUCKET_SIZE 64 +#define NXT_LVLHSH_ENTRY_SIZE 2 +#define NXT_LVLHSH_BATCH_ALLOC 8 /* 2 is shift of 32-bit pointer. */ -#define NXT_LVLHSH_MEMALIGN_SHIFT (NXT_MAX_MEMALIGN_SHIFT - 2) +#define NXT_LVLHSH_MEMALIGN_SHIFT (NXT_MAX_MEMALIGN_SHIFT - 2) #endif #if (NXT_LVLHSH_MEMALIGN_SHIFT < 10) -#define NXT_LVLHSH_MAX_MEMALIGN_SHIFT NXT_LVLHSH_MEMALIGN_SHIFT +#define NXT_LVLHSH_MAX_MEMALIGN_SHIFT NXT_LVLHSH_MEMALIGN_SHIFT #else -#define NXT_LVLHSH_MAX_MEMALIGN_SHIFT 10 +#define NXT_LVLHSH_MAX_MEMALIGN_SHIFT 10 #endif -#define NXT_LVLHSH_BUCKET_END(bucket_size) \ - (((bucket_size) - sizeof(void *)) \ - / (NXT_LVLHSH_ENTRY_SIZE * sizeof(uint32_t)) \ - * NXT_LVLHSH_ENTRY_SIZE) +#define NXT_LVLHSH_BUCKET_END(bucket_size) \ + (((bucket_size) - sizeof(void *)) \ + / (NXT_LVLHSH_ENTRY_SIZE * sizeof(uint32_t)) * NXT_LVLHSH_ENTRY_SIZE) -#define NXT_LVLHSH_BUCKET_SIZE(bucket_size) \ +#define NXT_LVLHSH_BUCKET_SIZE(bucket_size) \ NXT_LVLHSH_BUCKET_END(bucket_size), bucket_size, (bucket_size - 1) -#define NXT_LVLHSH_DEFAULT \ - NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ - { 4, 4, 4, 4, 4, 4, 4, 0 } - +#define NXT_LVLHSH_DEFAULT \ + NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ + {4, 4, 4, 4, 4, 4, 4, 0} -#define NXT_LVLHSH_LARGE_SLAB \ - NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ - { 10, 4, 4, 4, 4, 4, 4, 0 } +#define NXT_LVLHSH_LARGE_SLAB \ + NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ + {10, 4, 4, 4, 4, 4, 4, 0} -#define NXT_LVLHSH_LARGE_MEMALIGN \ - NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ - { NXT_LVLHSH_MAX_MEMALIGN_SHIFT, 4, 4, 4, 4, 0, 0, 0 } +#define NXT_LVLHSH_LARGE_MEMALIGN \ + NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE), \ + {NXT_LVLHSH_MAX_MEMALIGN_SHIFT, 4, 4, 4, 4, 0, 0, 0} typedef struct { - uint32_t bucket_end; - uint32_t bucket_size; - uint32_t bucket_mask; - uint8_t shift[8]; - - nxt_lvlhsh_test_t test; - nxt_lvlhsh_alloc_t alloc; - nxt_lvlhsh_free_t free; + uint32_t bucket_end; + uint32_t bucket_size; + uint32_t bucket_mask; + uint8_t shift[8]; + + nxt_lvlhsh_test_t test; + nxt_lvlhsh_alloc_t alloc; + nxt_lvlhsh_free_t free; } nxt_lvlhsh_proto_t; - typedef struct { - void *slot; + void *slot; } nxt_lvlhsh_t; - struct nxt_lvlhsh_query_s { - uint32_t key_hash; - uint32_t replace; /* 1 bit */ - nxt_str_t key; - void *value; + uint32_t key_hash; + uint32_t replace; /* 1 bit */ + nxt_str_t key; + void *value; - const nxt_lvlhsh_proto_t *proto; - void *pool; + const nxt_lvlhsh_proto_t *proto; + void *pool; /* Opaque data passed for the test function. */ - void *data; + void *data; }; - typedef struct { - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; /* * Fields to store current bucket entry position. They cannot be * combined in a single bucket pointer with number of entries in low * bits, because entry positions are not aligned. A current level is * stored as key bit path from the root. */ - uint32_t *bucket; + uint32_t *bucket; uint32_t current; uint32_t entry; uint32_t entries; } nxt_lvlhsh_each_t; - -#define nxt_lvlhsh_is_empty(lh) \ - ((lh)->slot == NULL) +#define nxt_lvlhsh_is_empty(lh) ((lh)->slot == NULL) -#define nxt_lvlhsh_init(lh) \ - (lh)->slot = NULL +#define nxt_lvlhsh_init(lh) (lh)->slot = NULL /* * nxt_lvlhsh_find() finds a hash element. If the element has been @@ -128,7 +119,8 @@ typedef struct { * * The required nxt_lvlhsh_query_t fields: key_hash, key, proto. */ -NXT_EXPORT nxt_int_t nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq); +NXT_EXPORT nxt_int_t +nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq); /* * nxt_lvlhsh_insert() adds a hash element. If the element already @@ -144,8 +136,8 @@ NXT_EXPORT nxt_int_t nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq); * The required nxt_lvlhsh_query_t fields: key_hash, key, proto, replace, value. * The optional nxt_lvlhsh_query_t fields: pool. */ -NXT_EXPORT nxt_int_t nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, - nxt_lvlhsh_query_t *lhq); +NXT_EXPORT nxt_int_t +nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq); /* * nxt_lvlhsh_delete() deletes a hash element. If the element has been @@ -155,24 +147,25 @@ NXT_EXPORT nxt_int_t nxt_lvlhsh_insert(nxt_lvlhsh_t *lh, * The required nxt_lvlhsh_query_t fields: key_hash, key, proto. * The optional nxt_lvlhsh_query_t fields: pool. */ -NXT_EXPORT nxt_int_t nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, - nxt_lvlhsh_query_t *lhq); +NXT_EXPORT nxt_int_t +nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq); /* * nxt_lvlhsh_each_init() initializes iterator. * It must be called before the first nxt_lvlhsh_each() call. */ -#define nxt_lvlhsh_each_init(lhe, _proto) \ - do { \ - (lhe)->proto = _proto; \ - (lhe)->bucket = NULL; \ +#define nxt_lvlhsh_each_init(lhe, _proto) \ + do { \ + (lhe)->proto = _proto; \ + (lhe)->bucket = NULL; \ } while (0) /* * nxt_lvlhsh_each() iterates over a lvlhsh. * It returns NULL if there is no more elements. */ -NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe); +NXT_EXPORT void * +nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe); /* * nxt_lvlhsh_peek() is used to iterate over a lvlhsh during the lvlhsh @@ -180,8 +173,8 @@ NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe); * otherwise it will be returned again by the next nxt_lvlhsh_peek() call. * The function returns NULL if there is no more elements in the lvlhsh. */ -NXT_EXPORT void *nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, - const nxt_lvlhsh_proto_t *proto); +NXT_EXPORT void * +nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto); /* * nxt_lvlhsh_retrieve() is used to iterate over a lvlhsh during the lvlhsh @@ -189,12 +182,15 @@ NXT_EXPORT void *nxt_lvlhsh_peek(nxt_lvlhsh_t *lh, * is deleted from the lvlhsh. The function returns NULL if there is no * more elements in the lvlhsh. */ -NXT_EXPORT void *nxt_lvlhsh_retrieve(nxt_lvlhsh_t *lh, - const nxt_lvlhsh_proto_t *proto, void *pool); +NXT_EXPORT void * +nxt_lvlhsh_retrieve(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, + void *pool); -NXT_EXPORT void *nxt_lvlhsh_alloc(void *data, size_t size); -NXT_EXPORT void nxt_lvlhsh_free(void *data, void *p); +NXT_EXPORT void * +nxt_lvlhsh_alloc(void *data, size_t size); +NXT_EXPORT void +nxt_lvlhsh_free(void *data, void *p); #endif /* _NXT_LEVEL_HASH_H_INCLUDED_ */ diff --git a/src/nxt_macosx_sendfile.c b/src/nxt_macosx_sendfile.c index 2c6ea954c..e4a4f565b 100644 --- a/src/nxt_macosx_sendfile.c +++ b/src/nxt_macosx_sendfile.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,38 +10,38 @@ #ifdef NXT_TEST_BUILD_MACOSX_SENDFILE -ssize_t nxt_macosx_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_macosx_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static int nxt_sys_sendfile(int fd, int s, off_t offset, off_t *len, - struct sf_hdtr *hdtr, int flags) -{ +static int +nxt_sys_sendfile(int fd, int s, off_t offset, off_t *len, struct sf_hdtr *hdtr, + int flags) { return -1; } #else -#define nxt_sys_sendfile sendfile +#define nxt_sys_sendfile sendfile #endif ssize_t -nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) -{ - size_t hd_size, file_size; - ssize_t n; - nxt_buf_t *fb; - nxt_err_t err; - nxt_off_t sent; - nxt_uint_t nhd, ntr; - struct iovec hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX]; - struct sf_hdtr hdtr, *ht; - nxt_sendbuf_coalesce_t sb; - - sb.buf = b; +nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) { + size_t hd_size, file_size; + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_off_t sent; + nxt_uint_t nhd, ntr; + struct iovec hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX]; + struct sf_hdtr hdtr, *ht; + nxt_sendbuf_coalesce_t sb; + + sb.buf = b; sb.iobuf = hd; - sb.nmax = NXT_IOBUF_MAX; - sb.sync = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX; + sb.sync = 0; + sb.size = 0; sb.limit = limit; nhd = nxt_sendbuf_mem_coalesce(c->socket.task, &sb); @@ -56,7 +55,7 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) } hd_size = sb.size; - fb = sb.buf; + fb = sb.buf; file_size = nxt_sendbuf_file_coalesce(&sb); @@ -78,15 +77,15 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) ht = NULL; if (nhd != 0) { - ht = &hdtr; + ht = &hdtr; hdtr.headers = hd; hdtr.hdr_cnt = nhd; } if (ntr != 0) { - ht = &hdtr; + ht = &hdtr; hdtr.trailers = tr; - hdtr.trl_cnt = ntr; + hdtr.trl_cnt = ntr; } /* @@ -102,12 +101,11 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) sent = hd_size + file_size; nxt_log_debug(c->socket.log, - "sendfile(%FD, %d, @%O, %O) hd:%ui tr:%ui hs:%uz", - fb->file->fd, c->socket.fd, fb->file_pos, sent, - nhd, ntr, hd_size); + "sendfile(%FD, %d, @%O, %O) hd:%ui tr:%ui hs:%uz", fb->file->fd, + c->socket.fd, fb->file_pos, sent, nhd, ntr, hd_size); - n = nxt_sys_sendfile(fb->file->fd, c->socket.fd, - fb->file_pos, &sent, ht, 0); + n = nxt_sys_sendfile(fb->file->fd, c->socket.fd, fb->file_pos, &sent, ht, + 0); err = (n == -1) ? nxt_errno : 0; @@ -115,7 +113,6 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -126,9 +123,9 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "sendfile(%FD, %d, %O, %O) failed %E \"%FN\" hd:%ui tr:%ui", - fb->file->fd, c->socket.fd, fb->file_pos, sent, err, - fb->file->name, nhd, ntr); + "sendfile(%FD, %d, %O, %O) failed %E \"%FN\" hd:%ui tr:%ui", + fb->file->fd, c->socket.fd, fb->file_pos, sent, err, + fb->file->name, nhd, ntr); return NXT_ERROR; } @@ -140,7 +137,7 @@ nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit) if (sent == 0) { nxt_log(c->socket.task, NXT_LOG_ERR, - "file \"%FN\" was truncated while sendfile()", fb->file->name); + "file \"%FN\" was truncated while sendfile()", fb->file->name); return NXT_ERROR; } diff --git a/src/nxt_main.c b/src/nxt_main.c index 26bee8732..d9af92c91 100644 --- a/src/nxt_main.c +++ b/src/nxt_main.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,19 +7,17 @@ #include -extern char **environ; - +extern char **environ; int nxt_cdecl -main(int argc, char **argv) -{ - nxt_int_t ret; +main(int argc, char **argv) { + nxt_int_t ret; if (nxt_lib_start("unit", argv, &environ) != NXT_OK) { return 1; } -// nxt_main_log.level = NXT_LOG_INFO; + // nxt_main_log.level = NXT_LOG_INFO; nxt_main_log.handler = nxt_log_time_handler; diff --git a/src/nxt_main.h b/src/nxt_main.h index 7880e55f8..204a48977 100644 --- a/src/nxt_main.h +++ b/src/nxt_main.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,21 +10,21 @@ #include #include -#define NXT_NAME "Unit" -#define NXT_SERVER NXT_NAME "/" NXT_VERSION +#define NXT_NAME "Unit" +#define NXT_SERVER NXT_NAME "/" NXT_VERSION -typedef struct nxt_port_s nxt_port_t; -typedef struct nxt_task_s nxt_task_t; -typedef struct nxt_port_recv_msg_s nxt_port_recv_msg_t; +typedef struct nxt_port_s nxt_port_t; +typedef struct nxt_task_s nxt_task_t; +typedef struct nxt_port_recv_msg_s nxt_port_recv_msg_t; typedef void (*nxt_port_handler_t)(nxt_task_t *task, nxt_port_recv_msg_t *msg); -typedef struct nxt_port_handlers_s nxt_port_handlers_t; -typedef struct nxt_sig_event_s nxt_sig_event_t; -typedef struct nxt_runtime_s nxt_runtime_t; +typedef struct nxt_port_handlers_s nxt_port_handlers_t; +typedef struct nxt_sig_event_s nxt_sig_event_t; +typedef struct nxt_runtime_s nxt_runtime_t; -typedef struct nxt_thread_s nxt_thread_t; -typedef struct nxt_event_engine_s nxt_event_engine_t; -typedef struct nxt_log_s nxt_log_t; -typedef struct nxt_thread_pool_s nxt_thread_pool_t; +typedef struct nxt_thread_s nxt_thread_t; +typedef struct nxt_event_engine_s nxt_event_engine_t; +typedef struct nxt_log_s nxt_log_t; +typedef struct nxt_thread_pool_s nxt_thread_pool_t; typedef void (*nxt_work_handler_t)(nxt_task_t *task, void *obj, void *data); @@ -36,7 +35,7 @@ typedef void (*nxt_work_handler_t)(nxt_task_t *task, void *obj, void *data); #include #include -typedef uint16_t nxt_port_id_t; +typedef uint16_t nxt_port_id_t; #include @@ -69,15 +68,15 @@ typedef uint16_t nxt_port_id_t; #include #include -typedef struct nxt_tstr_state_s nxt_tstr_state_t; +typedef struct nxt_tstr_state_s nxt_tstr_state_t; #include #include /* TODO: remove unused */ -typedef struct nxt_fd_event_s nxt_fd_event_t; -typedef struct nxt_sockaddr_s nxt_sockaddr_t; +typedef struct nxt_fd_event_s nxt_fd_event_t; +typedef struct nxt_sockaddr_s nxt_sockaddr_t; #include @@ -87,14 +86,13 @@ typedef struct nxt_sockaddr_s nxt_sockaddr_t; typedef void *(*nxt_mem_proto_alloc_t)(void *pool, size_t size); -typedef void (*nxt_mem_proto_free_t)(void *pool, void *p); +typedef void (*nxt_mem_proto_free_t)(void *pool, void *p); typedef struct { - nxt_mem_proto_alloc_t alloc; - nxt_mem_proto_free_t free; + nxt_mem_proto_alloc_t alloc; + nxt_mem_proto_free_t free; } nxt_mem_proto_t; - #include #include #include @@ -108,12 +106,12 @@ typedef struct { #include -typedef struct nxt_buf_s nxt_buf_t; +typedef struct nxt_buf_s nxt_buf_t; #include #include #include -typedef struct nxt_conn_s nxt_conn_t; +typedef struct nxt_conn_s nxt_conn_t; #include #include @@ -123,8 +121,7 @@ typedef struct nxt_conn_s nxt_conn_t; #endif -#define nxt_thread() \ - (nxt_thread_t *) nxt_thread_get_data(nxt_thread_context) +#define nxt_thread() (nxt_thread_t *) nxt_thread_get_data(nxt_thread_context) nxt_thread_extern_data(nxt_thread_t, nxt_thread_context); @@ -159,14 +156,16 @@ typedef void (*nxt_event_conn_handler_t)(nxt_thread_t *thr, nxt_conn_t *c); * The envp argument must be &environ if application may * change its process title with nxt_process_title(). */ -NXT_EXPORT nxt_int_t nxt_lib_start(const char *app, char **argv, char ***envp); -NXT_EXPORT void nxt_lib_stop(void); +NXT_EXPORT nxt_int_t +nxt_lib_start(const char *app, char **argv, char ***envp); +NXT_EXPORT void +nxt_lib_stop(void); -NXT_EXPORT extern nxt_uint_t nxt_ncpu; -NXT_EXPORT extern nxt_uint_t nxt_pagesize; -NXT_EXPORT extern nxt_task_t nxt_main_task; -NXT_EXPORT extern nxt_atomic_t nxt_task_ident; +NXT_EXPORT extern nxt_uint_t nxt_ncpu; +NXT_EXPORT extern nxt_uint_t nxt_pagesize; +NXT_EXPORT extern nxt_task_t nxt_main_task; +NXT_EXPORT extern nxt_atomic_t nxt_task_ident; #endif /* _NXT_LIB_H_INCLUDED_ */ diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c index 00318226b..39f4d29e8 100644 --- a/src/nxt_main_process.c +++ b/src/nxt_main_process.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -20,56 +19,57 @@ #include - typedef struct { - nxt_socket_t socket; - nxt_socket_error_t error; - u_char *start; - u_char *end; + nxt_socket_t socket; + nxt_socket_error_t error; + u_char *start; + u_char *end; } nxt_listening_socket_t; - typedef struct { - nxt_uint_t size; - nxt_conf_map_t *map; + nxt_uint_t size; + nxt_conf_map_t *map; } nxt_conf_app_map_t; +static nxt_int_t +nxt_main_process_port_create(nxt_task_t *task, nxt_runtime_t *rt); +static void +nxt_main_process_title(nxt_task_t *task); +static void +nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_main_process_signal_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process); +static void +nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_main_port_socket_unlink_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static nxt_int_t +nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls); +static void +nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static int nxt_cdecl +nxt_app_lang_compare(const void *v1, const void *v2); +static void +nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static nxt_int_t +nxt_main_file_store(nxt_task_t *task, const char *tmp_name, const char *name, + u_char *buf, size_t size); +static void +nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -static nxt_int_t nxt_main_process_port_create(nxt_task_t *task, - nxt_runtime_t *rt); -static void nxt_main_process_title(nxt_task_t *task); -static void nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_main_process_signal_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process); -static void nxt_main_port_socket_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_main_port_socket_unlink_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static nxt_int_t nxt_main_listening_socket(nxt_sockaddr_t *sa, - nxt_listening_socket_t *ls); -static void nxt_main_port_modules_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static int nxt_cdecl nxt_app_lang_compare(const void *v1, const void *v2); -static void nxt_main_process_whoami_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_main_port_conf_store_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static nxt_int_t nxt_main_file_store(nxt_task_t *task, const char *tmp_name, - const char *name, u_char *buf, size_t size); -static void nxt_main_port_access_log_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); - -const nxt_sig_event_t nxt_main_process_signals[] = { - nxt_event_signal(SIGHUP, nxt_main_process_signal_handler), - nxt_event_signal(SIGINT, nxt_main_process_sigterm_handler), +const nxt_sig_event_t nxt_main_process_signals[] = { + nxt_event_signal(SIGHUP, nxt_main_process_signal_handler), + nxt_event_signal(SIGINT, nxt_main_process_sigterm_handler), nxt_event_signal(SIGQUIT, nxt_main_process_sigquit_handler), nxt_event_signal(SIGTERM, nxt_main_process_sigterm_handler), nxt_event_signal(SIGCHLD, nxt_main_process_sigchld_handler), @@ -78,15 +78,12 @@ const nxt_sig_event_t nxt_main_process_signals[] = { }; -nxt_uint_t nxt_conf_ver; - -static nxt_bool_t nxt_exiting; +nxt_uint_t nxt_conf_ver; +static nxt_bool_t nxt_exiting; nxt_int_t -nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task, - nxt_runtime_t *rt) -{ +nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task, nxt_runtime_t *rt) { rt->type = NXT_PROCESS_MAIN; if (nxt_main_process_port_create(task, rt) != NXT_OK) { @@ -103,8 +100,7 @@ nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task, return nxt_process_init_start(task, nxt_discovery_process); } - -static nxt_conf_map_t nxt_common_app_conf[] = { +static nxt_conf_map_t nxt_common_app_conf[] = { { nxt_string("type"), NXT_CONF_MAP_STR, @@ -162,7 +158,7 @@ static nxt_conf_map_t nxt_common_app_conf[] = { }; -static nxt_conf_map_t nxt_common_app_limits_conf[] = { +static nxt_conf_map_t nxt_common_app_limits_conf[] = { { nxt_string("shm"), NXT_CONF_MAP_SIZE, @@ -178,7 +174,7 @@ static nxt_conf_map_t nxt_common_app_limits_conf[] = { }; -static nxt_conf_map_t nxt_external_app_conf[] = { +static nxt_conf_map_t nxt_external_app_conf[] = { { nxt_string("executable"), NXT_CONF_MAP_CSTRZ, @@ -194,7 +190,7 @@ static nxt_conf_map_t nxt_external_app_conf[] = { }; -static nxt_conf_map_t nxt_python_app_conf[] = { +static nxt_conf_map_t nxt_python_app_conf[] = { { nxt_string("home"), NXT_CONF_MAP_CSTRZ, @@ -233,7 +229,7 @@ static nxt_conf_map_t nxt_python_app_conf[] = { }; -static nxt_conf_map_t nxt_php_app_conf[] = { +static nxt_conf_map_t nxt_php_app_conf[] = { { nxt_string("targets"), NXT_CONF_MAP_PTR, @@ -248,7 +244,7 @@ static nxt_conf_map_t nxt_php_app_conf[] = { }; -static nxt_conf_map_t nxt_perl_app_conf[] = { +static nxt_conf_map_t nxt_perl_app_conf[] = { { nxt_string("script"), NXT_CONF_MAP_CSTRZ, @@ -269,26 +265,25 @@ static nxt_conf_map_t nxt_perl_app_conf[] = { }; -static nxt_conf_map_t nxt_ruby_app_conf[] = { - { - nxt_string("script"), - NXT_CONF_MAP_STR, - offsetof(nxt_common_app_conf_t, u.ruby.script), - }, - { - nxt_string("threads"), - NXT_CONF_MAP_INT32, - offsetof(nxt_common_app_conf_t, u.ruby.threads), - }, - { - nxt_string("hooks"), - NXT_CONF_MAP_STR, - offsetof(nxt_common_app_conf_t, u.ruby.hooks), - } -}; +static nxt_conf_map_t nxt_ruby_app_conf[] + = {{ + nxt_string("script"), + NXT_CONF_MAP_STR, + offsetof(nxt_common_app_conf_t, u.ruby.script), + }, + { + nxt_string("threads"), + NXT_CONF_MAP_INT32, + offsetof(nxt_common_app_conf_t, u.ruby.threads), + }, + { + nxt_string("hooks"), + NXT_CONF_MAP_STR, + offsetof(nxt_common_app_conf_t, u.ruby.hooks), + }}; -static nxt_conf_map_t nxt_java_app_conf[] = { +static nxt_conf_map_t nxt_java_app_conf[] = { { nxt_string("classpath"), NXT_CONF_MAP_PTR, @@ -323,7 +318,7 @@ static nxt_conf_map_t nxt_java_app_conf[] = { }; -static nxt_conf_map_t nxt_wasm_app_conf[] = { +static nxt_conf_map_t nxt_wasm_app_conf[] = { { nxt_string("module"), NXT_CONF_MAP_CSTRZ, @@ -377,7 +372,7 @@ static nxt_conf_map_t nxt_wasm_app_conf[] = { }; -static nxt_conf_map_t nxt_wasm_wc_app_conf[] = { +static nxt_conf_map_t nxt_wasm_wc_app_conf[] = { { nxt_string("component"), NXT_CONF_MAP_CSTRZ, @@ -391,42 +386,35 @@ static nxt_conf_map_t nxt_wasm_wc_app_conf[] = { }; -static nxt_conf_app_map_t nxt_app_maps[] = { - { nxt_nitems(nxt_external_app_conf), nxt_external_app_conf }, - { nxt_nitems(nxt_python_app_conf), nxt_python_app_conf }, - { nxt_nitems(nxt_php_app_conf), nxt_php_app_conf }, - { nxt_nitems(nxt_perl_app_conf), nxt_perl_app_conf }, - { nxt_nitems(nxt_ruby_app_conf), nxt_ruby_app_conf }, - { nxt_nitems(nxt_java_app_conf), nxt_java_app_conf }, - { nxt_nitems(nxt_wasm_app_conf), nxt_wasm_app_conf }, - { nxt_nitems(nxt_wasm_wc_app_conf), nxt_wasm_wc_app_conf }, +static nxt_conf_app_map_t nxt_app_maps[] = { + {nxt_nitems(nxt_external_app_conf), nxt_external_app_conf}, + {nxt_nitems(nxt_python_app_conf), nxt_python_app_conf}, + {nxt_nitems(nxt_php_app_conf), nxt_php_app_conf}, + {nxt_nitems(nxt_perl_app_conf), nxt_perl_app_conf}, + {nxt_nitems(nxt_ruby_app_conf), nxt_ruby_app_conf}, + {nxt_nitems(nxt_java_app_conf), nxt_java_app_conf}, + {nxt_nitems(nxt_wasm_app_conf), nxt_wasm_app_conf}, + {nxt_nitems(nxt_wasm_wc_app_conf), nxt_wasm_wc_app_conf}, }; - static void -nxt_main_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_debug(task, "main data: %*s", - nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos); +nxt_main_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_debug(task, "main data: %*s", nxt_buf_mem_used_size(&msg->buf->mem), + msg->buf->mem.pos); } - static void -nxt_main_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - void *mem; - nxt_port_t *port; +nxt_main_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + void *mem; + nxt_port_t *port; nxt_port_new_port_handler(task, msg); port = msg->u.new_port; - if (port != NULL - && port->type == NXT_PROCESS_APP - && msg->fd[1] != -1) - { + if (port != NULL && port->type == NXT_PROCESS_APP && msg->fd[1] != -1) { mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, msg->fd[1], 0); + PROT_READ | PROT_WRITE, MAP_SHARED, msg->fd[1], 0); if (nxt_fast_path(mem != MAP_FAILED)) { port->queue = mem; } @@ -436,21 +424,19 @@ nxt_main_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static void -nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *start, *p, ch; +nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *start, *p, ch; size_t type_len; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_process_t *process; + nxt_buf_t *b; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_process_t *process; nxt_app_type_t idx; - nxt_conf_value_t *conf; - nxt_process_init_t *init; - nxt_common_app_conf_t *app_conf; + nxt_conf_value_t *conf; + nxt_process_init_t *init; + nxt_common_app_conf_t *app_conf; rt = task->thread->runtime; @@ -462,7 +448,7 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_slow_path(port->pid != nxt_recv_msg_cmsg_pid(msg))) { nxt_alert(task, "process %PI cannot start processes", - nxt_recv_msg_cmsg_pid(msg)); + nxt_recv_msg_cmsg_pid(msg)); goto close_fds; } @@ -490,37 +476,37 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } nxt_debug(task, "main start prototype: %*s", b->mem.free - b->mem.pos, - b->mem.pos); + b->mem.pos); app_conf = nxt_mp_zalloc(process->mem_pool, sizeof(nxt_common_app_conf_t)); if (nxt_slow_path(app_conf == NULL)) { goto failed; } - app_conf->shared_port_fd = msg->fd[0]; + app_conf->shared_port_fd = msg->fd[0]; app_conf->shared_queue_fd = msg->fd[1]; start = b->mem.pos; - app_conf->name.start = start; + app_conf->name.start = start; app_conf->name.length = nxt_strlen(start); init->name = (const char *) start; - process->name = nxt_mp_alloc(process->mem_pool, app_conf->name.length - + sizeof("\"\" prototype") + 1); + process->name = nxt_mp_alloc(process->mem_pool, + app_conf->name.length + sizeof("\"\" prototype") + 1); if (nxt_slow_path(process->name == NULL)) { goto failed; } - p = (u_char *) process->name; + p = (u_char *) process->name; *p++ = '"'; - p = nxt_cpymem(p, init->name, app_conf->name.length); - p = nxt_cpymem(p, "\" prototype", 11); - *p = '\0'; + p = nxt_cpymem(p, init->name, app_conf->name.length); + p = nxt_cpymem(p, "\" prototype", 11); + *p = '\0'; - app_conf->shm_limit = 100 * 1024 * 1024; + app_conf->shm_limit = 100 * 1024 * 1024; app_conf->request_limit = 0; start += app_conf->name.length + 1; @@ -534,11 +520,11 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) rt = task->thread->runtime; - app_conf->user.start = (u_char*)rt->user_cred.user; + app_conf->user.start = (u_char *) rt->user_cred.user; app_conf->user.length = nxt_strlen(rt->user_cred.user); ret = nxt_conf_map_object(process->mem_pool, conf, nxt_common_app_conf, - nxt_nitems(nxt_common_app_conf), app_conf); + nxt_nitems(nxt_common_app_conf), app_conf); if (ret != NXT_OK) { nxt_alert(task, "failed to map common app conf received from router"); @@ -561,7 +547,7 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } ret = nxt_conf_map_object(process->mem_pool, conf, nxt_app_maps[idx].map, - nxt_app_maps[idx].size, app_conf); + nxt_app_maps[idx].size, app_conf); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "failed to map app conf received from router"); @@ -570,9 +556,8 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (app_conf->limits != NULL) { ret = nxt_conf_map_object(process->mem_pool, app_conf->limits, - nxt_common_app_limits_conf, - nxt_nitems(nxt_common_app_limits_conf), - app_conf); + nxt_common_app_limits_conf, nxt_nitems(nxt_common_app_limits_conf), + app_conf); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "failed to map app limits received from router"); @@ -582,12 +567,11 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) app_conf->self = conf; - process->stream = msg->port_msg.stream; + process->stream = msg->port_msg.stream; process->data.app = app_conf; ret = nxt_process_start(task, process); if (nxt_fast_path(ret == NXT_OK || ret == NXT_AGAIN)) { - /* Close shared port fds only in main process. */ if (ret == NXT_OK) { nxt_fd_close(app_conf->shared_port_fd); @@ -606,11 +590,11 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_process_use(task, process, -1); port = nxt_runtime_port_find(rt, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_fast_path(port != NULL)) { - nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, - -1, msg->port_msg.stream, 0, NULL); + nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, + msg->port_msg.stream, 0, NULL); } close_fds: @@ -622,18 +606,16 @@ nxt_main_start_process_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) msg->fd[1] = -1; } - static void -nxt_main_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_port_t *port; - nxt_process_t *process; - nxt_runtime_t *rt; +nxt_main_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_port_t *port; + nxt_process_t *process; + nxt_runtime_t *rt; rt = task->thread->runtime; port = nxt_runtime_port_find(rt, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { return; } @@ -646,12 +628,10 @@ nxt_main_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) #if (NXT_HAVE_LINUX_NS && NXT_HAVE_CLONE_NEWUSER) if (nxt_is_clone_flag_set(process->isolation.clone.flags, NEWUSER)) { if (nxt_slow_path(nxt_clone_credential_map(task, process->pid, - process->user_cred, - &process->isolation.clone) - != NXT_OK)) - { - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, - -1, msg->port_msg.stream, 0, NULL); + process->user_cred, &process->isolation.clone) + != NXT_OK)) { + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, + msg->port_msg.stream, 0, NULL); return; } } @@ -660,51 +640,48 @@ nxt_main_process_created_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) process->state = NXT_PROCESS_STATE_CREATED; - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_READY_LAST, - -1, msg->port_msg.stream, 0, NULL); + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_READY_LAST, -1, + msg->port_msg.stream, 0, NULL); } - -static nxt_port_handlers_t nxt_main_process_port_handlers = { - .data = nxt_main_data_handler, - .new_port = nxt_main_new_port_handler, - .process_created = nxt_main_process_created_handler, - .process_ready = nxt_port_process_ready_handler, - .whoami = nxt_main_process_whoami_handler, - .remove_pid = nxt_port_remove_pid_handler, - .start_process = nxt_main_start_process_handler, - .socket = nxt_main_port_socket_handler, - .socket_unlink = nxt_main_port_socket_unlink_handler, - .modules = nxt_main_port_modules_handler, - .conf_store = nxt_main_port_conf_store_handler, +static nxt_port_handlers_t nxt_main_process_port_handlers = { + .data = nxt_main_data_handler, + .new_port = nxt_main_new_port_handler, + .process_created = nxt_main_process_created_handler, + .process_ready = nxt_port_process_ready_handler, + .whoami = nxt_main_process_whoami_handler, + .remove_pid = nxt_port_remove_pid_handler, + .start_process = nxt_main_start_process_handler, + .socket = nxt_main_port_socket_handler, + .socket_unlink = nxt_main_port_socket_unlink_handler, + .modules = nxt_main_port_modules_handler, + .conf_store = nxt_main_port_conf_store_handler, #if (NXT_TLS) - .cert_get = nxt_cert_store_get_handler, - .cert_delete = nxt_cert_store_delete_handler, + .cert_get = nxt_cert_store_get_handler, + .cert_delete = nxt_cert_store_delete_handler, #endif #if (NXT_HAVE_NJS) - .script_get = nxt_script_store_get_handler, - .script_delete = nxt_script_store_delete_handler, + .script_get = nxt_script_store_get_handler, + .script_delete = nxt_script_store_delete_handler, #endif - .access_log = nxt_main_port_access_log_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, + .access_log = nxt_main_port_access_log_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, }; static void -nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_buf_t *buf; +nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_buf_t *buf; nxt_pid_t pid, ppid; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_process_t *pprocess; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_process_t *pprocess; nxt_assert(msg->port_msg.reply_port == 0); if (nxt_slow_path(msg->buf == NULL - || nxt_buf_used_size(msg->buf) != sizeof(nxt_pid_t))) - { + || nxt_buf_used_size(msg->buf) != sizeof(nxt_pid_t))) { nxt_alert(task, "whoami: buffer is NULL or unexpected size"); goto fail; } @@ -722,11 +699,11 @@ nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) pid = nxt_recv_msg_cmsg_pid(msg); nxt_debug(task, "whoami: from %PI, parent %PI, fd %d", pid, ppid, - msg->fd[0]); + msg->fd[0]); if (msg->fd[0] != -1) { port = nxt_runtime_process_port_create(task, rt, pid, 0, - NXT_PROCESS_APP); + NXT_PROCESS_APP); if (nxt_slow_path(port == NULL)) { goto fail; } @@ -735,10 +712,10 @@ nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) port->pair[0] = -1; port->pair[1] = msg->fd[0]; - msg->fd[0] = -1; + msg->fd[0] = -1; - port->max_size = 16 * 1024; - port->max_share = 64 * 1024; + port->max_size = 16 * 1024; + port->max_share = 64 * 1024; port->socket.task = task; nxt_port_write_enable(task, port); @@ -754,8 +731,8 @@ nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_queue_insert_tail(&pprocess->children, &port->process->link); } - buf = nxt_buf_mem_alloc(task->thread->engine->mem_pool, - sizeof(nxt_pid_t), 0); + buf = nxt_buf_mem_alloc(task->thread->engine->mem_pool, sizeof(nxt_pid_t), + 0); if (nxt_slow_path(buf == NULL)) { goto fail; } @@ -763,7 +740,7 @@ nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) buf->mem.free = nxt_cpymem(buf->mem.free, &pid, sizeof(nxt_pid_t)); (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_READY_LAST, -1, - msg->port_msg.stream, 0, buf); + msg->port_msg.stream, 0, buf); fail: @@ -772,16 +749,14 @@ nxt_main_process_whoami_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static nxt_int_t -nxt_main_process_port_create(nxt_task_t *task, nxt_runtime_t *rt) -{ +nxt_main_process_port_create(nxt_task_t *task, nxt_runtime_t *rt) { nxt_int_t ret; - nxt_port_t *port; - nxt_process_t *process; + nxt_port_t *port; + nxt_process_t *process; port = nxt_runtime_process_port_create(task, rt, nxt_pid, 0, - NXT_PROCESS_MAIN); + NXT_PROCESS_MAIN); if (nxt_slow_path(port == NULL)) { return NXT_ERROR; } @@ -805,18 +780,16 @@ nxt_main_process_port_create(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static void -nxt_main_process_title(nxt_task_t *task) -{ - u_char *p, *end; - nxt_uint_t i; - u_char title[2048]; +nxt_main_process_title(nxt_task_t *task) { + u_char *p, *end; + nxt_uint_t i; + u_char title[2048]; end = title + sizeof(title) - 1; p = nxt_sprintf(title, end, "unit: main v" NXT_VERSION " [%s", - nxt_process_argv[0]); + nxt_process_argv[0]); for (i = 1; nxt_process_argv[i] != NULL; i++) { p = nxt_sprintf(p, end, " %s", nxt_process_argv[i]); @@ -831,12 +804,10 @@ nxt_main_process_title(nxt_task_t *task) nxt_process_title(task, "%s", title); } - static void -nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_debug(task, "sigterm handler signo:%d (%s)", - (int) (uintptr_t) obj, data); +nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, void *data) { + nxt_debug(task, "sigterm handler signo:%d (%s)", (int) (uintptr_t) obj, + data); /* TODO: fast exit. */ @@ -845,12 +816,10 @@ nxt_main_process_sigterm_handler(nxt_task_t *task, void *obj, void *data) nxt_runtime_quit(task, 0); } - static void -nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_debug(task, "sigquit handler signo:%d (%s)", - (int) (uintptr_t) obj, data); +nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, void *data) { + nxt_debug(task, "sigquit handler signo:%d (%s)", (int) (uintptr_t) obj, + data); /* TODO: graceful exit. */ @@ -859,28 +828,26 @@ nxt_main_process_sigquit_handler(nxt_task_t *task, void *obj, void *data) nxt_runtime_quit(task, 0); } - static void -nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_int_t ret; - nxt_uint_t n; - nxt_port_t *port; - nxt_file_t *file, *new_file; - nxt_array_t *new_files; - nxt_runtime_t *rt; +nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_int_t ret; + nxt_uint_t n; + nxt_port_t *port; + nxt_file_t *file, *new_file; + nxt_array_t *new_files; + nxt_runtime_t *rt; nxt_log(task, NXT_LOG_NOTICE, "signal %d (%s) received, %s", - (int) (uintptr_t) obj, data, "log files rotation"); + (int) (uintptr_t) obj, data, "log files rotation"); rt = task->thread->runtime; port = rt->port_by_type[NXT_PROCESS_ROUTER]; if (nxt_fast_path(port != NULL)) { - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_ACCESS_LOG, - -1, 0, 0, NULL); + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_ACCESS_LOG, -1, 0, + 0, NULL); } mp = nxt_mp_create(1024, 128, 256, 32); @@ -897,22 +864,21 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) } nxt_list_each(file, rt->log_files) { - /* This allocation cannot fail. */ new_file = nxt_array_add(new_files); - new_file->name = file->name; - new_file->fd = NXT_FILE_INVALID; + new_file->name = file->name; + new_file->fd = NXT_FILE_INVALID; new_file->log_level = NXT_LOG_ALERT; ret = nxt_file_open(task, new_file, O_WRONLY | O_APPEND, O_CREAT, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); if (ret != NXT_OK) { goto fail; } - - } nxt_list_loop; + } + nxt_list_loop; new_file = new_files->elts; @@ -922,7 +888,6 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) n = 0; nxt_list_each(file, rt->log_files) { - nxt_port_change_log_file(task, rt, n, new_file[n].fd); /* * The old log file descriptor must be closed at the moment @@ -933,8 +898,8 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) (void) nxt_file_redirect(file, new_file[n].fd); n++; - - } nxt_list_loop; + } + nxt_list_loop; nxt_mp_destroy(mp); return; @@ -943,7 +908,7 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) fail: new_file = new_files->elts; - n = new_files->nelts; + n = new_files->nelts; while (n != 0) { if (new_file->fd != NXT_FILE_INVALID) { @@ -957,32 +922,28 @@ nxt_main_process_sigusr1_handler(nxt_task_t *task, void *obj, void *data) nxt_mp_destroy(mp); } - static void -nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) -{ - int status; - nxt_int_t ret; - nxt_err_t err; - nxt_pid_t pid; - nxt_port_t *port; - nxt_queue_t children; - nxt_runtime_t *rt; - nxt_process_t *process, *child; - nxt_process_init_t init; - - nxt_debug(task, "sigchld handler signo:%d (%s)", - (int) (uintptr_t) obj, data); +nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) { + int status; + nxt_int_t ret; + nxt_err_t err; + nxt_pid_t pid; + nxt_port_t *port; + nxt_queue_t children; + nxt_runtime_t *rt; + nxt_process_t *process, *child; + nxt_process_init_t init; + + nxt_debug(task, "sigchld handler signo:%d (%s)", (int) (uintptr_t) obj, + data); rt = task->thread->runtime; - for ( ;; ) { + for (;;) { pid = waitpid(-1, &status, WNOHANG); if (pid == -1) { - switch (err = nxt_errno) { - case NXT_ECHILD: return; @@ -1003,17 +964,16 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) if (WTERMSIG(status)) { #ifdef WCOREDUMP - nxt_alert(task, "process %PI exited on signal %d%s", - pid, WTERMSIG(status), - WCOREDUMP(status) ? " (core dumped)" : ""); + nxt_alert(task, "process %PI exited on signal %d%s", pid, + WTERMSIG(status), WCOREDUMP(status) ? " (core dumped)" : ""); #else - nxt_alert(task, "process %PI exited on signal %d", - pid, WTERMSIG(status)); + nxt_alert(task, "process %PI exited on signal %d", pid, + WTERMSIG(status)); #endif } else { - nxt_trace(task, "process %PI exited with code %d", - pid, WEXITSTATUS(status)); + nxt_trace(task, "process %PI exited with code %d", pid, + WEXITSTATUS(status)); } process = nxt_runtime_process_find(rt, pid); @@ -1036,8 +996,9 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) port = nxt_process_port_first(child); (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, - -1, 0, 0, NULL); - } nxt_queue_loop; + -1, 0, 0, NULL); + } + nxt_queue_loop; } if (nxt_exiting) { @@ -1048,7 +1009,8 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) child->link.next = NULL; nxt_process_close_ports(task, child); - } nxt_queue_loop; + } + nxt_queue_loop; if (rt->nprocesses <= 1) { nxt_runtime_quit(task, 0); @@ -1068,7 +1030,8 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) child->link.next = NULL; nxt_process_close_ports(task, child); - } nxt_queue_loop; + } + nxt_queue_loop; init = *(nxt_process_init_t *) nxt_process_init(process); @@ -1084,18 +1047,14 @@ nxt_main_process_sigchld_handler(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_main_process_signal_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_main_process_signal_handler(nxt_task_t *task, void *obj, void *data) { nxt_trace(task, "signal signo:%d (%s) received, ignored", - (int) (uintptr_t) obj, data); + (int) (uintptr_t) obj, data); } - static void -nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process) -{ +nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process) { if (process->isolation.cleanup != NULL) { process->isolation.cleanup(task, process); } @@ -1105,50 +1064,48 @@ nxt_main_process_cleanup(nxt_task_t *task, nxt_process_t *process) } } - static void -nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - size_t size; - nxt_int_t ret; - nxt_buf_t *b, *out; - nxt_port_t *port; - nxt_sockaddr_t *sa; - nxt_port_msg_type_t type; - nxt_listening_socket_t ls; - u_char message[2048]; +nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + size_t size; + nxt_int_t ret; + nxt_buf_t *b, *out; + nxt_port_t *port; + nxt_sockaddr_t *sa; + nxt_port_msg_type_t type; + nxt_listening_socket_t ls; + u_char message[2048]; port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { return; } if (nxt_slow_path(port->type != NXT_PROCESS_ROUTER)) { nxt_alert(task, "process %PI cannot create listener sockets", - msg->port_msg.pid); + msg->port_msg.pid); return; } - b = msg->buf; + b = msg->buf; sa = (nxt_sockaddr_t *) b->mem.pos; /* TODO check b size and make plain */ ls.socket = -1; - ls.error = NXT_SOCKET_ERROR_SYSTEM; - ls.start = message; - ls.end = message + sizeof(message); + ls.error = NXT_SOCKET_ERROR_SYSTEM; + ls.start = message; + ls.end = message + sizeof(message); - nxt_debug(task, "listening socket \"%*s\"", - (size_t) sa->length, nxt_sockaddr_start(sa)); + nxt_debug(task, "listening socket \"%*s\"", (size_t) sa->length, + nxt_sockaddr_start(sa)); ret = nxt_main_listening_socket(sa, &ls); if (ret == NXT_OK) { - nxt_debug(task, "socket(\"%*s\"): %d", - (size_t) sa->length, nxt_sockaddr_start(sa), ls.socket); + nxt_debug(task, "socket(\"%*s\"): %d", (size_t) sa->length, + nxt_sockaddr_start(sa), ls.socket); out = NULL; @@ -1160,7 +1117,7 @@ nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_alert(task, "%*s", size, ls.start); out = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, - size + 1); + size + 1); if (nxt_fast_path(out != NULL)) { *out->mem.free++ = (uint8_t) ls.error; @@ -1170,19 +1127,17 @@ nxt_main_port_socket_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) type = NXT_PORT_MSG_RPC_ERROR; } - nxt_port_socket_write(task, port, type, ls.socket, msg->port_msg.stream, - 0, out); + nxt_port_socket_write(task, port, type, ls.socket, msg->port_msg.stream, 0, + out); } - static nxt_int_t -nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) -{ - nxt_err_t err; - nxt_socket_t s; +nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) { + nxt_err_t err; + nxt_socket_t s; - const socklen_t length = sizeof(int); - static const int enable = 1; + const socklen_t length = sizeof(int); + static const int enable = 1; s = socket(sa->u.sockaddr.sa_family, sa->type, 0); @@ -1197,30 +1152,27 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) #endif - ls->end = nxt_sprintf(ls->start, ls->end, - "socket(\\\"%*s\\\") failed %E", - (size_t) sa->length, nxt_sockaddr_start(sa), err); + ls->end + = nxt_sprintf(ls->start, ls->end, "socket(\\\"%*s\\\") failed %E", + (size_t) sa->length, nxt_sockaddr_start(sa), err); return NXT_ERROR; } if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, length) != 0) { ls->end = nxt_sprintf(ls->start, ls->end, - "setsockopt(\\\"%*s\\\", SO_REUSEADDR) failed %E", - (size_t) sa->length, nxt_sockaddr_start(sa), - nxt_errno); + "setsockopt(\\\"%*s\\\", SO_REUSEADDR) failed %E", + (size_t) sa->length, nxt_sockaddr_start(sa), nxt_errno); goto fail; } #if (NXT_INET6) if (sa->u.sockaddr.sa_family == AF_INET6) { - if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &enable, length) != 0) { ls->end = nxt_sprintf(ls->start, ls->end, - "setsockopt(\\\"%*s\\\", IPV6_V6ONLY) failed %E", - (size_t) sa->length, nxt_sockaddr_start(sa), - nxt_errno); + "setsockopt(\\\"%*s\\\", IPV6_V6ONLY) failed %E", + (size_t) sa->length, nxt_sockaddr_start(sa), nxt_errno); goto fail; } } @@ -1234,7 +1186,6 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) if (sa->u.sockaddr.sa_family == AF_UNIX) { switch (err) { - case EACCES: ls->error = NXT_SOCKET_ERROR_ACCESS; break; @@ -1249,7 +1200,6 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) #endif { switch (err) { - case EACCES: ls->error = NXT_SOCKET_ERROR_PORT; break; @@ -1265,24 +1215,22 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) } ls->end = nxt_sprintf(ls->start, ls->end, "bind(\\\"%*s\\\") failed %E", - (size_t) sa->length, nxt_sockaddr_start(sa), err); + (size_t) sa->length, nxt_sockaddr_start(sa), err); goto fail; } #if (NXT_HAVE_UNIX_DOMAIN) if (sa->u.sockaddr.sa_family == AF_UNIX - && sa->u.sockaddr_un.sun_path[0] != '\0') - { - char *filename; - nxt_thread_t *thr; + && sa->u.sockaddr_un.sun_path[0] != '\0') { + char *filename; + nxt_thread_t *thr; filename = sa->u.sockaddr_un.sun_path; if (chmod(filename, 0666) != 0) { ls->end = nxt_sprintf(ls->start, ls->end, - "chmod(\\\"%s\\\") failed %E", - filename, nxt_errno); + "chmod(\\\"%s\\\") failed %E", filename, nxt_errno); goto fail; } @@ -1303,19 +1251,18 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls) return NXT_ERROR; } - static void -nxt_main_port_socket_unlink_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_main_port_socket_unlink_handler(nxt_task_t *task, + nxt_port_recv_msg_t *msg) { #if (NXT_HAVE_UNIX_DOMAIN) size_t i; - nxt_buf_t *b; - const char *filename; - nxt_runtime_t *rt; - nxt_sockaddr_t *sa; - nxt_listen_socket_t *ls; + nxt_buf_t *b; + const char *filename; + nxt_runtime_t *rt; + nxt_sockaddr_t *sa; + nxt_listen_socket_t *ls; - b = msg->buf; + b = msg->buf; sa = (nxt_sockaddr_t *) b->mem.pos; filename = sa->u.sockaddr_un.sun_path; @@ -1324,14 +1271,13 @@ nxt_main_port_socket_unlink_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) rt = task->thread->runtime; for (i = 0; i < rt->listen_sockets->nelts; i++) { - const char *name; + const char *name; ls = (nxt_listen_socket_t *) rt->listen_sockets->elts + i; sa = ls->sockaddr; if (sa->u.sockaddr.sa_family != AF_UNIX - || sa->u.sockaddr_un.sun_path[0] == '\0') - { + || sa->u.sockaddr_un.sun_path[0] == '\0') { continue; } @@ -1346,8 +1292,7 @@ nxt_main_port_socket_unlink_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) #endif } - -static nxt_conf_map_t nxt_app_lang_module_map[] = { +static nxt_conf_map_t nxt_app_lang_module_map[] = { { nxt_string("type"), NXT_CONF_MAP_INT, @@ -1374,7 +1319,7 @@ static nxt_conf_map_t nxt_app_lang_module_map[] = { }; -static nxt_conf_map_t nxt_app_lang_mounts_map[] = { +static nxt_conf_map_t nxt_app_lang_mounts_map[] = { { nxt_string("src"), NXT_CONF_MAP_CSTRZ, @@ -1407,21 +1352,19 @@ static nxt_conf_map_t nxt_app_lang_mounts_map[] = { }, }; - static void -nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { uint32_t index, jindex, nmounts; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_fs_mount_t *mnt; - nxt_conf_value_t *conf, *root, *value, *mounts; - nxt_app_lang_module_t *lang; - - static nxt_str_t root_path = nxt_string("/"); + nxt_buf_t *b; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_fs_mount_t *mnt; + nxt_conf_value_t *conf, *root, *value, *mounts; + nxt_app_lang_module_t *lang; + + static nxt_str_t root_path = nxt_string("/"); static nxt_str_t mounts_name = nxt_string("mounts"); rt = task->thread->runtime; @@ -1437,11 +1380,11 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_fast_path(port != NULL)) { (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, - msg->port_msg.stream, 0, NULL); + msg->port_msg.stream, 0, NULL); } b = msg->buf; @@ -1461,8 +1404,8 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) return; } - nxt_debug(task, "application languages: \"%*s\"", - b->mem.free - b->mem.pos, b->mem.pos); + nxt_debug(task, "application languages: \"%*s\"", b->mem.free - b->mem.pos, + b->mem.pos); conf = nxt_conf_json_parse(mp, b->mem.pos, b->mem.free, NULL); if (conf == NULL) { @@ -1474,7 +1417,7 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto fail; } - for (index = 0; /* void */ ; index++) { + for (index = 0; /* void */; index++) { value = nxt_conf_get_array_element(root, index); if (value == NULL) { break; @@ -1488,7 +1431,7 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) lang->module = NULL; ret = nxt_conf_map_object(rt->mem_pool, value, nxt_app_lang_module_map, - nxt_nitems(nxt_app_lang_module_map), lang); + nxt_nitems(nxt_app_lang_module_map), lang); if (ret != NXT_OK) { goto fail; @@ -1507,8 +1450,8 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nmounts = nxt_conf_array_elements_count(mounts); - lang->mounts = nxt_array_create(rt->mem_pool, nmounts, - sizeof(nxt_fs_mount_t)); + lang->mounts + = nxt_array_create(rt->mem_pool, nmounts, sizeof(nxt_fs_mount_t)); if (lang->mounts == NULL) { goto fail; @@ -1526,23 +1469,23 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } mnt->builtin = 1; - mnt->deps = 1; + mnt->deps = 1; ret = nxt_conf_map_object(rt->mem_pool, value, - nxt_app_lang_mounts_map, - nxt_nitems(nxt_app_lang_mounts_map), mnt); + nxt_app_lang_mounts_map, nxt_nitems(nxt_app_lang_mounts_map), + mnt); if (ret != NXT_OK) { goto fail; } } - nxt_debug(task, "lang %d %s \"%s\" (%d mounts)", - lang->type, lang->version, lang->file, lang->mounts->nelts); + nxt_debug(task, "lang %d %s \"%s\" (%d mounts)", lang->type, + lang->version, lang->file, lang->mounts->nelts); } qsort(rt->languages->elts, rt->languages->nelts, - sizeof(nxt_app_lang_module_t), nxt_app_lang_compare); + sizeof(nxt_app_lang_module_t), nxt_app_lang_compare); fail: @@ -1560,12 +1503,10 @@ nxt_main_port_modules_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static int nxt_cdecl -nxt_app_lang_compare(const void *v1, const void *v2) -{ +nxt_app_lang_compare(const void *v1, const void *v2) { int n; - const nxt_app_lang_module_t *lang1, *lang2; + const nxt_app_lang_module_t *lang1, *lang2; lang1 = v1; lang2 = v2; @@ -1583,15 +1524,13 @@ nxt_app_lang_compare(const void *v1, const void *v2) return -n; } - static void -nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - void *p; +nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + void *p; size_t n, size; nxt_int_t ret; - nxt_port_t *ctl_port; - nxt_runtime_t *rt; + nxt_port_t *ctl_port; + nxt_runtime_t *rt; u_char ver[NXT_INT_T_LEN]; rt = task->thread->runtime; @@ -1618,7 +1557,7 @@ nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) { nxt_alert(task, "conf_store_handler: unexpected buffer size (%d)", - (int) nxt_buf_mem_used_size(&msg->buf->mem)); + (int) nxt_buf_mem_used_size(&msg->buf->mem)); goto error; } @@ -1668,21 +1607,19 @@ nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static nxt_int_t nxt_main_file_store(nxt_task_t *task, const char *tmp_name, const char *name, - u_char *buf, size_t size) -{ - ssize_t n; - nxt_int_t ret; - nxt_file_t file; + u_char *buf, size_t size) { + ssize_t n; + nxt_int_t ret; + nxt_file_t file; nxt_memzero(&file, sizeof(nxt_file_t)); file.name = (nxt_file_name_t *) name; ret = nxt_file_open(task, &file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -1699,15 +1636,13 @@ nxt_main_file_store(nxt_task_t *task, const char *tmp_name, const char *name, return nxt_file_rename(file.name, (nxt_file_name_t *) name); } - static void -nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *path; - nxt_int_t ret; - nxt_file_t file; - nxt_port_t *port; - nxt_port_msg_type_t type; +nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *path; + nxt_int_t ret; + nxt_file_t file; + nxt_port_t *port; + nxt_port_msg_type_t type; nxt_debug(task, "opening access log file"); @@ -1715,20 +1650,20 @@ nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_memzero(&file, sizeof(nxt_file_t)); - file.name = (nxt_file_name_t *) path; + file.name = (nxt_file_name_t *) path; file.log_level = NXT_LOG_ERR; ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); type = (ret == NXT_OK) ? NXT_PORT_MSG_RPC_READY_LAST | NXT_PORT_MSG_CLOSE_FD : NXT_PORT_MSG_RPC_ERROR; port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_fast_path(port != NULL)) { (void) nxt_port_socket_write(task, port, type, file.fd, - msg->port_msg.stream, 0, NULL); + msg->port_msg.stream, 0, NULL); } } diff --git a/src/nxt_main_process.h b/src/nxt_main_process.h index ef083d630..ca2b8feb4 100644 --- a/src/nxt_main_process.h +++ b/src/nxt_main_process.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_MAIN_PROCESS_H_INCLUDED_ #define _NXT_MAIN_PROCESS_H_INCLUDED_ - typedef enum { NXT_SOCKET_ERROR_SYSTEM = 0, NXT_SOCKET_ERROR_NOINET6, @@ -18,20 +16,20 @@ typedef enum { NXT_SOCKET_ERROR_PATH, } nxt_socket_error_t; - -nxt_int_t nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task, +nxt_int_t +nxt_main_process_start(nxt_thread_t *thr, nxt_task_t *task, nxt_runtime_t *runtime); -NXT_EXPORT extern nxt_uint_t nxt_conf_ver; -NXT_EXPORT extern const nxt_process_init_t nxt_discovery_process; -NXT_EXPORT extern const nxt_process_init_t nxt_controller_process; -NXT_EXPORT extern const nxt_process_init_t nxt_router_process; -NXT_EXPORT extern const nxt_process_init_t nxt_proto_process; -NXT_EXPORT extern const nxt_process_init_t nxt_app_process; +NXT_EXPORT extern nxt_uint_t nxt_conf_ver; +NXT_EXPORT extern const nxt_process_init_t nxt_discovery_process; +NXT_EXPORT extern const nxt_process_init_t nxt_controller_process; +NXT_EXPORT extern const nxt_process_init_t nxt_router_process; +NXT_EXPORT extern const nxt_process_init_t nxt_proto_process; +NXT_EXPORT extern const nxt_process_init_t nxt_app_process; -extern const nxt_sig_event_t nxt_main_process_signals[]; -extern const nxt_sig_event_t nxt_process_signals[]; +extern const nxt_sig_event_t nxt_main_process_signals[]; +extern const nxt_sig_event_t nxt_process_signals[]; #endif /* _NXT_MAIN_PROCESS_H_INCLUDED_ */ diff --git a/src/nxt_malloc.c b/src/nxt_malloc.c index 5ea7322f6..4cfbf960d 100644 --- a/src/nxt_malloc.c +++ b/src/nxt_malloc.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,15 +6,12 @@ #include -static nxt_log_moderation_t nxt_malloc_log_moderation = { - NXT_LOG_ALERT, 2, "memory allocation failed", NXT_LOG_MODERATION -}; - +static nxt_log_moderation_t nxt_malloc_log_moderation + = {NXT_LOG_ALERT, 2, "memory allocation failed", NXT_LOG_MODERATION}; static nxt_log_t * -nxt_malloc_log(void) -{ - nxt_thread_t *thr; +nxt_malloc_log(void) { + nxt_thread_t *thr; thr = nxt_thread(); @@ -26,11 +22,9 @@ nxt_malloc_log(void) return &nxt_main_log; } - void * -nxt_malloc(size_t size) -{ - void *p; +nxt_malloc(size_t size) { + void *p; p = malloc(size); @@ -39,17 +33,15 @@ nxt_malloc(size_t size) } else { nxt_log_alert_moderate(&nxt_malloc_log_moderation, nxt_malloc_log(), - "malloc(%uz) failed %E", size, nxt_errno); + "malloc(%uz) failed %E", size, nxt_errno); } return p; } - void * -nxt_zalloc(size_t size) -{ - void *p; +nxt_zalloc(size_t size) { + void *p; p = nxt_malloc(size); @@ -60,12 +52,10 @@ nxt_zalloc(size_t size) return p; } - void * -nxt_realloc(void *p, size_t size) -{ - void *n; - uintptr_t ptr; +nxt_realloc(void *p, size_t size) { + void *n; + uintptr_t ptr; /* * Workaround for a warning on GCC 12 about using "p" pointer in debug log @@ -80,26 +70,21 @@ nxt_realloc(void *p, size_t size) } else { nxt_log_alert_moderate(&nxt_malloc_log_moderation, nxt_malloc_log(), - "realloc(%p, %uz) failed %E", - ptr, size, nxt_errno); + "realloc(%p, %uz) failed %E", ptr, size, nxt_errno); } return n; } - /* nxt_lvlhsh_* functions moved here to avoid references from nxt_lvlhsh.c. */ void * -nxt_lvlhsh_alloc(void *data, size_t size) -{ +nxt_lvlhsh_alloc(void *data, size_t size) { return nxt_memalign(size, size); } - void -nxt_lvlhsh_free(void *data, void *p) -{ +nxt_lvlhsh_free(void *data, void *p) { nxt_free(p); } @@ -107,8 +92,7 @@ nxt_lvlhsh_free(void *data, void *p) #if (NXT_DEBUG) void -nxt_free(void *p) -{ +nxt_free(void *p) { nxt_log_debug(nxt_malloc_log(), "free(%p)", p); free(p); @@ -126,22 +110,20 @@ nxt_free(void *p) */ void * -nxt_memalign(size_t alignment, size_t size) -{ - void *p; - nxt_err_t err; +nxt_memalign(size_t alignment, size_t size) { + void *p; + nxt_err_t err; err = posix_memalign(&p, alignment, size); if (nxt_fast_path(err == 0)) { - nxt_thread_log_debug("posix_memalign(%uz, %uz): %p", - alignment, size, p); + nxt_thread_log_debug("posix_memalign(%uz, %uz): %p", alignment, size, + p); return p; } nxt_log_alert_moderate(&nxt_malloc_log_moderation, nxt_malloc_log(), - "posix_memalign(%uz, %uz) failed %E", - alignment, size, err); + "posix_memalign(%uz, %uz) failed %E", alignment, size, err); return NULL; } @@ -150,21 +132,18 @@ nxt_memalign(size_t alignment, size_t size) /* memalign() presents in Solaris, HP-UX. */ void * -nxt_memalign(size_t alignment, size_t size) -{ - void *p; +nxt_memalign(size_t alignment, size_t size) { + void *p; p = memalign(alignment, size); if (nxt_fast_path(p != NULL)) { - nxt_thread_log_debug("memalign(%uz, %uz): %p", - alignment, size, p); + nxt_thread_log_debug("memalign(%uz, %uz): %p", alignment, size, p); return p; } nxt_log_alert_moderate(&nxt_malloc_log_moderation, nxt_malloc_log(), - "memalign(%uz, %uz) failed %E", - alignment, size, nxt_errno); + "memalign(%uz, %uz) failed %E", alignment, size, nxt_errno); return NULL; } @@ -178,11 +157,10 @@ nxt_memalign(size_t alignment, size_t size) */ void * -nxt_memalign(size_t alignment, size_t size) -{ - size_t aligned_size; - u_char *p; - nxt_err_t err; +nxt_memalign(size_t alignment, size_t size) { + size_t aligned_size; + u_char *p; + nxt_err_t err; if (nxt_slow_path((alignment - 1) & alignment) != 0) { /* Alignment must be a power of 2. */ @@ -210,15 +188,15 @@ nxt_memalign(size_t alignment, size_t size) } else { nxt_log_alert_moderate(&nxt_malloc_log_moderation, nxt_malloc_log(), - "malloc(%uz) failed %E", size, nxt_errno); + "malloc(%uz) failed %E", size, nxt_errno); } return p; fail: - nxt_thread_log_alert("nxt_memalign(%uz, %uz) failed %E", - alignment, size, err); + nxt_thread_log_alert("nxt_memalign(%uz, %uz) failed %E", alignment, size, + err); return NULL; } diff --git a/src/nxt_malloc.h b/src/nxt_malloc.h index fd5493a57..98c061876 100644 --- a/src/nxt_malloc.h +++ b/src/nxt_malloc.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,24 +7,24 @@ #define _NXT_UNIX_MALLOC_H_INCLUDED_ -NXT_EXPORT void *nxt_malloc(size_t size) - NXT_MALLOC_LIKE; -NXT_EXPORT void *nxt_zalloc(size_t size) - NXT_MALLOC_LIKE; -NXT_EXPORT void *nxt_realloc(void *p, size_t size) - NXT_MALLOC_LIKE; -NXT_EXPORT void *nxt_memalign(size_t alignment, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_malloc(size_t size) NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_zalloc(size_t size) NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_realloc(void *p, size_t size) NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_memalign(size_t alignment, size_t size) NXT_MALLOC_LIKE; #if (NXT_DEBUG) -NXT_EXPORT void nxt_free(void *p); +NXT_EXPORT void +nxt_free(void *p); #else -#define nxt_free(p) \ - free(p) +#define nxt_free(p) free(p) #endif @@ -53,10 +52,9 @@ NXT_EXPORT void nxt_free(void *p); * Glibc malloc_usable_size() is fast operation. */ -#define nxt_malloc_usable_size(p, size) \ - size = malloc_usable_size(p) +#define nxt_malloc_usable_size(p, size) size = malloc_usable_size(p) -#define nxt_malloc_cutback(cutback, size) \ +#define nxt_malloc_cutback(cutback, size) \ size = ((cutback) && size > 127 * 1024) ? size - 32 : size #elif (NXT_FREEBSD) @@ -78,8 +76,7 @@ NXT_EXPORT void nxt_free(void *p); * are lesser than 1M. Larger allocations require mutex acquiring. */ -#define nxt_malloc_usable_size(p, size) \ - size = malloc_usable_size(p) +#define nxt_malloc_usable_size(p, size) size = malloc_usable_size(p) #define nxt_malloc_cutback(cutback, size) @@ -98,8 +95,7 @@ NXT_EXPORT void nxt_free(void *p); * malloc_good_size() is faster than malloc_size() */ -#define nxt_malloc_usable_size(p, size) \ - size = malloc_good_size(size) +#define nxt_malloc_usable_size(p, size) size = malloc_good_size(size) #define nxt_malloc_cutback(cutback, size) @@ -113,13 +109,13 @@ NXT_EXPORT void nxt_free(void *p); #if (NXT_HAVE_POSIX_MEMALIGN || NXT_HAVE_MEMALIGN) -#define NXT_MAX_MEMALIGN_SHIFT 32 +#define NXT_MAX_MEMALIGN_SHIFT 32 #elif (NXT_FREEBSD) -#define NXT_MAX_MEMALIGN_SHIFT 12 +#define NXT_MAX_MEMALIGN_SHIFT 12 #else -#define NXT_MAX_MEMALIGN_SHIFT 3 +#define NXT_MAX_MEMALIGN_SHIFT 3 #endif diff --git a/src/nxt_mem_map.c b/src/nxt_mem_map.c index f9caf54f1..38d23841e 100644 --- a/src/nxt_mem_map.c +++ b/src/nxt_mem_map.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,31 +5,27 @@ #include - void * nxt_mem_mmap(void *addr, size_t len, nxt_uint_t protection, nxt_uint_t flags, - nxt_fd_t fd, nxt_off_t offset) -{ - void *p; + nxt_fd_t fd, nxt_off_t offset) { + void *p; p = mmap(addr, len, protection, flags, fd, offset); if (nxt_fast_path(p != MAP_FAILED)) { - nxt_thread_log_debug("mmap(%p, %uz, %uxi, %uxi, %FD, %O): %p", - addr, len, protection, flags, fd, offset, p); + nxt_thread_log_debug("mmap(%p, %uz, %uxi, %uxi, %FD, %O): %p", addr, + len, protection, flags, fd, offset, p); } else { - nxt_thread_log_alert("mmap(%p, %uz, %ui, %ui, %FD, %O) failed %E", - addr, len, protection, flags, fd, offset, nxt_errno); + nxt_thread_log_alert("mmap(%p, %uz, %ui, %ui, %FD, %O) failed %E", addr, + len, protection, flags, fd, offset, nxt_errno); } return p; } - void -nxt_mem_munmap(void *addr, size_t len) -{ +nxt_mem_munmap(void *addr, size_t len) { if (nxt_fast_path(munmap(addr, len) == 0)) { nxt_thread_log_debug("munmap(%p, %uz)", addr, len); diff --git a/src/nxt_mem_map.h b/src/nxt_mem_map.h index e529aff8d..1cce2d8b8 100644 --- a/src/nxt_mem_map.h +++ b/src/nxt_mem_map.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,20 +7,20 @@ #define _NXT_UNIX_MEM_MAP_H_INCLUDED_ -#define NXT_MEM_MAP_FAILED MAP_FAILED +#define NXT_MEM_MAP_FAILED MAP_FAILED -#define NXT_MEM_MAP_READ PROT_READ -#define NXT_MEM_MAP_WRITE PROT_WRITE +#define NXT_MEM_MAP_READ PROT_READ +#define NXT_MEM_MAP_WRITE PROT_WRITE #if (NXT_HAVE_MAP_ANONYMOUS) -#define NXT_MEM_MAP_ANON MAP_ANONYMOUS +#define NXT_MEM_MAP_ANON MAP_ANONYMOUS #else -#define NXT_MEM_MAP_ANON MAP_ANON +#define NXT_MEM_MAP_ANON MAP_ANON #endif -#define NXT_MEM_MAP_SHARED (MAP_SHARED | NXT_MEM_MAP_ANON) +#define NXT_MEM_MAP_SHARED (MAP_SHARED | NXT_MEM_MAP_ANON) #if (NXT_HAVE_MAP_POPULATE) @@ -30,33 +29,34 @@ * (MAP_POPULATE | MAP_NONBLOCK) wires only resident pages * without read ahead but it does not work since Linux 2.6.23. */ -#define NXT_MEM_MAP_PREFAULT MAP_POPULATE +#define NXT_MEM_MAP_PREFAULT MAP_POPULATE #elif (NXT_HAVE_MAP_PREFAULT_READ) /* FreeBSD MAP_PREFAULT_READ wires resident pages without read ahead. */ -#define NXT_MEM_MAP_PREFAULT MAP_PREFAULT_READ +#define NXT_MEM_MAP_PREFAULT MAP_PREFAULT_READ #else -#define NXT_MEM_MAP_PREFAULT 0 +#define NXT_MEM_MAP_PREFAULT 0 #endif -#define NXT_MEM_MAP_FILE (MAP_SHARED | NXT_MEM_MAP_PREFAULT) +#define NXT_MEM_MAP_FILE (MAP_SHARED | NXT_MEM_MAP_PREFAULT) -#define nxt_mem_map_file_ctx_t(ctx) +#define nxt_mem_map_file_ctx_t(ctx) -#define nxt_mem_map(addr, ctx, len, protection, flags, fd, offset) \ +#define nxt_mem_map(addr, ctx, len, protection, flags, fd, offset) \ nxt_mem_mmap(addr, len, protection, flags, fd, offset) -#define nxt_mem_unmap(addr, ctx, len) \ - nxt_mem_munmap(addr, len) +#define nxt_mem_unmap(addr, ctx, len) nxt_mem_munmap(addr, len) -NXT_EXPORT void *nxt_mem_mmap(void *addr, size_t len, nxt_uint_t protection, - nxt_uint_t flags, nxt_fd_t fd, nxt_off_t offset); -NXT_EXPORT void nxt_mem_munmap(void *addr, size_t len); +NXT_EXPORT void * +nxt_mem_mmap(void *addr, size_t len, nxt_uint_t protection, nxt_uint_t flags, + nxt_fd_t fd, nxt_off_t offset); +NXT_EXPORT void +nxt_mem_munmap(void *addr, size_t len); #endif /* _NXT_UNIX_MEM_MAP_H_INCLUDED_ */ diff --git a/src/nxt_mem_zone.c b/src/nxt_mem_zone.c index a3ba37007..da0e5feba 100644 --- a/src/nxt_mem_zone.c +++ b/src/nxt_mem_zone.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,18 +6,18 @@ #include -#define NXT_MEM_ZONE_PAGE_FREE 0 +#define NXT_MEM_ZONE_PAGE_FREE 0 /* * A page was never allocated before so it should be filled with * junk on the first time allocation if memory debugging is enabled. */ -#define NXT_MEM_ZONE_PAGE_FRESH 1 +#define NXT_MEM_ZONE_PAGE_FRESH 1 /* An entire page is currently used, no chunks inside the page. */ -#define NXT_MEM_ZONE_PAGE_USED 2 +#define NXT_MEM_ZONE_PAGE_USED 2 -typedef struct nxt_mem_zone_page_s nxt_mem_zone_page_t; +typedef struct nxt_mem_zone_page_s nxt_mem_zone_page_t; struct nxt_mem_zone_page_s { /* @@ -26,142 +25,134 @@ struct nxt_mem_zone_page_s { * Otherwise it is used to mark page state: NXT_MEM_ZONE_PAGE_FREE, * NXT_MEM_ZONE_PAGE_FRESH, and NXT_MEM_ZONE_PAGE_USED. */ - uint16_t size; + uint16_t size; /* A number of free chunks of a chunked page. */ - uint16_t chunks; + uint16_t chunks; union { /* A chunk bitmap if a number of chunks is lesser than 32. */ - uint8_t map[4]; + uint8_t map[4]; /* * The count is a number of successive occupied pages in the first * page. In the next occupied pages and in all free pages the count * is zero, because a number of successive free pages is stored in * free block size resided in beginning of the first free page. */ - uint32_t count; + uint32_t count; } u; /* Used for slot list of pages with free chunks. */ - nxt_mem_zone_page_t *next; + nxt_mem_zone_page_t *next; /* * Used to link of all pages including free, chunked and occupied * pages to coalesce free pages. */ - nxt_queue_link_t link; + nxt_queue_link_t link; }; - typedef struct { - uint32_t size; - uint32_t chunks; - uint32_t start; - uint32_t map_size; - nxt_mem_zone_page_t *pages; + uint32_t size; + uint32_t chunks; + uint32_t start; + uint32_t map_size; + nxt_mem_zone_page_t *pages; } nxt_mem_zone_slot_t; - typedef struct { - NXT_RBTREE_NODE (node); - uint32_t size; + NXT_RBTREE_NODE(node); + uint32_t size; } nxt_mem_zone_free_block_t; - struct nxt_mem_zone_s { - nxt_thread_spinlock_t lock; - nxt_mem_zone_page_t *pages; - nxt_mem_zone_page_t sentinel_page; - nxt_rbtree_t free_pages; + nxt_thread_spinlock_t lock; + nxt_mem_zone_page_t *pages; + nxt_mem_zone_page_t sentinel_page; + nxt_rbtree_t free_pages; - uint32_t page_size_shift; - uint32_t page_size_mask; - uint32_t max_chunk_size; - uint32_t small_bitmap_min_size; + uint32_t page_size_shift; + uint32_t page_size_mask; + uint32_t max_chunk_size; + uint32_t small_bitmap_min_size; - u_char *start; - u_char *end; + u_char *start; + u_char *end; - nxt_mem_zone_slot_t slots[]; + nxt_mem_zone_slot_t slots[]; }; - -#define nxt_mem_zone_page_addr(zone, page) \ +#define nxt_mem_zone_page_addr(zone, page) \ (void *) (zone->start + ((page - zone->pages) << zone->page_size_shift)) -#define nxt_mem_zone_addr_page(zone, addr) \ +#define nxt_mem_zone_addr_page(zone, addr) \ &zone->pages[((u_char *) addr - zone->start) >> zone->page_size_shift] -#define nxt_mem_zone_page_is_free(page) \ - (page->size < NXT_MEM_ZONE_PAGE_USED) +#define nxt_mem_zone_page_is_free(page) (page->size < NXT_MEM_ZONE_PAGE_USED) -#define nxt_mem_zone_page_is_chunked(page) \ - (page->size >= 16) +#define nxt_mem_zone_page_is_chunked(page) (page->size >= 16) -#define nxt_mem_zone_page_bitmap(zone, slot) \ +#define nxt_mem_zone_page_bitmap(zone, slot) \ (slot->size < zone->small_bitmap_min_size) -#define nxt_mem_zone_set_chunk_free(map, chunk) \ +#define nxt_mem_zone_set_chunk_free(map, chunk) \ map[chunk / 8] &= ~(0x80 >> (chunk & 7)) -#define nxt_mem_zone_chunk_is_free(map, chunk) \ +#define nxt_mem_zone_chunk_is_free(map, chunk) \ ((map[chunk / 8] & (0x80 >> (chunk & 7))) == 0) -#define nxt_mem_zone_fresh_junk(p, size) \ - nxt_memset((p), 0xA5, size) +#define nxt_mem_zone_fresh_junk(p, size) nxt_memset((p), 0xA5, size) -#define nxt_mem_zone_free_junk(p, size) \ - nxt_memset((p), 0x5A, size) +#define nxt_mem_zone_free_junk(p, size) nxt_memset((p), 0x5A, size) -static uint32_t nxt_mem_zone_pages(u_char *start, size_t zone_size, - nxt_uint_t page_size); -static void *nxt_mem_zone_slots_init(nxt_mem_zone_t *zone, - nxt_uint_t page_size); -static void nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, - nxt_uint_t page_size); -static intptr_t nxt_mem_zone_rbtree_compare(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2); -static void *nxt_mem_zone_alloc_small(nxt_mem_zone_t *zone, - nxt_mem_zone_slot_t *slot, size_t size); -static nxt_uint_t nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, - nxt_uint_t size); -static void *nxt_mem_zone_alloc_large(nxt_mem_zone_t *zone, size_t alignment, +static uint32_t +nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size); +static void * +nxt_mem_zone_slots_init(nxt_mem_zone_t *zone, nxt_uint_t page_size); +static void +nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, nxt_uint_t page_size); +static intptr_t +nxt_mem_zone_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2); +static void * +nxt_mem_zone_alloc_small(nxt_mem_zone_t *zone, nxt_mem_zone_slot_t *slot, size_t size); -static nxt_mem_zone_page_t *nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, - size_t alignment, uint32_t pages); +static nxt_uint_t +nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, nxt_uint_t size); +static void * +nxt_mem_zone_alloc_large(nxt_mem_zone_t *zone, size_t alignment, size_t size); +static nxt_mem_zone_page_t * +nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, + uint32_t pages); static nxt_mem_zone_free_block_t * - nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, +nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, uint32_t alignment, uint32_t pages); -static const char *nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, - nxt_mem_zone_page_t *page, void *p); -static void nxt_mem_zone_free_pages(nxt_mem_zone_t *zone, - nxt_mem_zone_page_t *page, nxt_uint_t count); - +static const char * +nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, + void *p); +static void +nxt_mem_zone_free_pages(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, + nxt_uint_t count); -static nxt_log_moderation_t nxt_mem_zone_log_moderation = { - NXT_LOG_ALERT, 2, "mem_zone_alloc() failed, not enough memory", - NXT_LOG_MODERATION -}; +static nxt_log_moderation_t nxt_mem_zone_log_moderation = {NXT_LOG_ALERT, 2, + "mem_zone_alloc() failed, not enough memory", NXT_LOG_MODERATION}; nxt_mem_zone_t * -nxt_mem_zone_init(u_char *start, size_t zone_size, nxt_uint_t page_size) -{ +nxt_mem_zone_init(u_char *start, size_t zone_size, nxt_uint_t page_size) { uint32_t pages; nxt_uint_t n; - nxt_mem_zone_t *zone; - nxt_mem_zone_page_t *page; - nxt_mem_zone_free_block_t *block; + nxt_mem_zone_t *zone; + nxt_mem_zone_page_t *page; + nxt_mem_zone_free_block_t *block; if (nxt_slow_path((page_size & (page_size - 1)) != 0)) { nxt_thread_log_alert("mem zone page size must be a power of 2"); @@ -197,7 +188,7 @@ nxt_mem_zone_init(u_char *start, size_t zone_size, nxt_uint_t page_size) nxt_rbtree_init(&zone->free_pages, nxt_mem_zone_rbtree_compare); - block = (nxt_mem_zone_free_block_t *) zone->start; + block = (nxt_mem_zone_free_block_t *) zone->start; block->size = pages; nxt_rbtree_insert(&zone->free_pages, &block->node); @@ -205,21 +196,19 @@ nxt_mem_zone_init(u_char *start, size_t zone_size, nxt_uint_t page_size) return zone; } - static uint32_t -nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size) -{ - u_char *end; +nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size) { + u_char *end; size_t reserved; nxt_uint_t n, pages, size, chunks, last; - nxt_mem_zone_t *zone; + nxt_mem_zone_t *zone; /* * Find all maximum chunk sizes which zone page can be split on * with minimum 16-byte step. */ last = page_size / 16; - n = 0; + n = 0; size = 32; do { @@ -240,7 +229,7 @@ nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size) */ reserved = sizeof(nxt_mem_zone_t) + (n * sizeof(nxt_mem_zone_slot_t)); - end = nxt_trunc_ptr(start + zone_size, page_size); + end = nxt_trunc_ptr(start + zone_size, page_size); zone_size = end - start; pages = (zone_size - reserved) / (page_size + sizeof(nxt_mem_zone_page_t)); @@ -256,10 +245,10 @@ nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size) zone = (nxt_mem_zone_t *) start; zone->start = nxt_align_ptr(start + reserved, page_size); - zone->end = end; + zone->end = end; nxt_thread_log_debug("mem zone pages: %uD, unused:%z", pages, - end - (zone->start + pages * page_size)); + end - (zone->start + pages * page_size)); /* * If a chunk size is lesser than zone->small_bitmap_min_size @@ -281,32 +270,28 @@ nxt_mem_zone_pages(u_char *start, size_t zone_size, nxt_uint_t page_size) return (uint32_t) pages; } - static void * -nxt_mem_zone_slots_init(nxt_mem_zone_t *zone, nxt_uint_t page_size) -{ +nxt_mem_zone_slots_init(nxt_mem_zone_t *zone, nxt_uint_t page_size) { nxt_uint_t n, size, chunks; - nxt_mem_zone_slot_t *slot; + nxt_mem_zone_slot_t *slot; slot = zone->slots; slot[0].chunks = page_size / 16; - slot[0].size = 16; + slot[0].size = 16; - n = 0; + n = 0; size = 32; - for ( ;; ) { + for (;;) { chunks = page_size / size; if (slot[n].chunks != chunks) { - nxt_mem_zone_slot_init(&slot[n], page_size); nxt_thread_log_debug( - "mem zone size:%uD chunks:%uD start:%uD map:%uD", - slot[n].size, slot[n].chunks + 1, - slot[n].start, slot[n].map_size); + "mem zone size:%uD chunks:%uD start:%uD map:%uD", slot[n].size, + slot[n].chunks + 1, slot[n].start, slot[n].map_size); n++; @@ -315,16 +300,14 @@ nxt_mem_zone_slots_init(nxt_mem_zone_t *zone, nxt_uint_t page_size) } } - slot[n].chunks = chunks; - slot[n].size = size; - size += 16; + slot[n].chunks = chunks; + slot[n].size = size; + size += 16; } } - static void -nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, nxt_uint_t page_size) -{ +nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, nxt_uint_t page_size) { /* * Calculate number of bytes required to store a chunk bitmap * and align it to 4 bytes. @@ -350,7 +333,7 @@ nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, nxt_uint_t page_size) if (slot->size < slot->map_size) { /* The first chunks are occupied by bitmap. */ slot->chunks -= slot->map_size / slot->size; - slot->start = nxt_align_size(slot->map_size, 16); + slot->start = nxt_align_size(slot->map_size, 16); } else { /* The first chunk is occupied by bitmap. */ @@ -361,15 +344,13 @@ nxt_mem_zone_slot_init(nxt_mem_zone_slot_t *slot, nxt_uint_t page_size) } } - /* * Round up to the next highest power of 2. The algorithm is * described in "Bit Twiddling Hacks" by Sean Eron Anderson. */ nxt_inline uint32_t -nxt_next_highest_power_of_two(uint32_t n) -{ +nxt_next_highest_power_of_two(uint32_t n) { n--; n |= n >> 1; n |= n >> 2; @@ -381,13 +362,12 @@ nxt_next_highest_power_of_two(uint32_t n) return n; } - static intptr_t -nxt_mem_zone_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) -{ - u_char *start1, *end1, *start2, *end2; +nxt_mem_zone_rbtree_compare(nxt_rbtree_node_t *node1, + nxt_rbtree_node_t *node2) { + u_char *start1, *end1, *start2, *end2; uint32_t n, size, size1, size2; - nxt_mem_zone_free_block_t *block1, *block2; + nxt_mem_zone_free_block_t *block1, *block2; block1 = (nxt_mem_zone_free_block_t *) node1; block2 = (nxt_mem_zone_free_block_t *) node2; @@ -416,19 +396,17 @@ nxt_mem_zone_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) /* Align the blocks' start and end to the rounded size. */ start1 = nxt_align_ptr(block1, size); - end1 = nxt_trunc_ptr((u_char *) block1 + size1, size); + end1 = nxt_trunc_ptr((u_char *) block1 + size1, size); start2 = nxt_align_ptr(block2, size); - end2 = nxt_trunc_ptr((u_char *) block2 + size2, size); + end2 = nxt_trunc_ptr((u_char *) block2 + size2, size); return (end1 - start1) - (end2 - start2); } - void * -nxt_mem_zone_zalloc(nxt_mem_zone_t *zone, size_t size) -{ - void *p; +nxt_mem_zone_zalloc(nxt_mem_zone_t *zone, size_t size) { + void *p; p = nxt_mem_zone_align(zone, 1, size); @@ -439,12 +417,10 @@ nxt_mem_zone_zalloc(nxt_mem_zone_t *zone, size_t size) return p; } - void * -nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, size_t size) -{ - void *p; - nxt_mem_zone_slot_t *slot; +nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, size_t size) { + void *p; + nxt_mem_zone_slot_t *slot; if (nxt_slow_path((alignment - 1) & alignment) != 0) { /* Alignment must be a power of 2. */ @@ -470,17 +446,17 @@ nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, size_t size) * Find a zone slot with appropriate chunk size. * This operation can be performed without holding lock. */ - for (slot = zone->slots; slot->size < size; slot++) { /* void */ } + for (slot = zone->slots; slot->size < size; slot++) { /* void */ + } - nxt_thread_log_debug("mem zone alloc: @%uz:%uz chunk:%uD", - alignment, size, slot->size); + nxt_thread_log_debug("mem zone alloc: @%uz:%uz chunk:%uD", alignment, + size, slot->size); nxt_thread_spin_lock(&zone->lock); p = nxt_mem_zone_alloc_small(zone, slot, size); } else { - nxt_thread_log_debug("mem zone alloc: @%uz:%uz", alignment, size); nxt_thread_spin_lock(&zone->lock); @@ -495,26 +471,23 @@ nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, size_t size) } else { nxt_log_alert_moderate(&nxt_mem_zone_log_moderation, nxt_thread_log(), - "nxt_mem_zone_alloc(%uz, %uz) failed, not enough memory", - alignment, size); + "nxt_mem_zone_alloc(%uz, %uz) failed, not enough memory", alignment, + size); } return p; } - static void * nxt_mem_zone_alloc_small(nxt_mem_zone_t *zone, nxt_mem_zone_slot_t *slot, - size_t size) -{ - u_char *p; - uint8_t *map; - nxt_mem_zone_page_t *page; + size_t size) { + u_char *p; + uint8_t *map; + nxt_mem_zone_page_t *page; page = slot->pages; if (nxt_fast_path(page != NULL)) { - p = nxt_mem_zone_page_addr(zone, page); if (nxt_mem_zone_page_bitmap(zone, slot)) { @@ -546,14 +519,13 @@ nxt_mem_zone_alloc_small(nxt_mem_zone_t *zone, nxt_mem_zone_slot_t *slot, page = nxt_mem_zone_alloc_pages(zone, 1, 1); if (nxt_fast_path(page != NULL)) { - slot->pages = page; - page->size = slot->size; + page->size = slot->size; /* slot->chunks are already one less. */ - page->chunks = slot->chunks; + page->chunks = slot->chunks; page->u.count = 0; - page->next = NULL; + page->next = NULL; p = nxt_mem_zone_page_addr(zone, page); @@ -575,25 +547,21 @@ nxt_mem_zone_alloc_small(nxt_mem_zone_t *zone, nxt_mem_zone_slot_t *slot, return NULL; } - static nxt_uint_t -nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, nxt_uint_t size) -{ - uint8_t mask; - nxt_uint_t n; +nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, nxt_uint_t size) { + uint8_t mask; + nxt_uint_t n; n = 0; /* The page must have at least one free chunk. */ - for ( ;; ) { + for (;;) { /* The bitmap is always aligned to uint32_t. */ if (*(uint32_t *) &map[n] != 0xFFFFFFFF) { - do { if (map[n] != 0xFF) { - mask = 0x80; do { @@ -603,8 +571,8 @@ nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, nxt_uint_t size) return offset; } - offset += size; - mask >>= 1; + offset += size; + mask >>= 1; } while (mask != 0); @@ -620,17 +588,15 @@ nxt_mem_zone_alloc_chunk(uint8_t *map, nxt_uint_t offset, nxt_uint_t size) } else { /* Fast-forward: all 32 chunks are occupied. */ offset += size * 32; - n += 4; + n += 4; } } } - static void * -nxt_mem_zone_alloc_large(nxt_mem_zone_t *zone, size_t alignment, size_t size) -{ +nxt_mem_zone_alloc_large(nxt_mem_zone_t *zone, size_t alignment, size_t size) { uint32_t pages; - nxt_mem_zone_page_t *page; + nxt_mem_zone_page_t *page; pages = (size + zone->page_size_mask) >> zone->page_size_shift; @@ -643,20 +609,18 @@ nxt_mem_zone_alloc_large(nxt_mem_zone_t *zone, size_t alignment, size_t size) return NULL; } - static nxt_mem_zone_page_t * -nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) -{ - u_char *p; +nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, + uint32_t pages) { + u_char *p; size_t prev_size; uint32_t prev_pages, node_pages, next_pages; nxt_uint_t n; - nxt_mem_zone_page_t *prev_page, *page, *next_page; - nxt_mem_zone_free_block_t *block, *next_block; + nxt_mem_zone_page_t *prev_page, *page, *next_page; + nxt_mem_zone_free_block_t *block, *next_block; block = nxt_mem_zone_find_free_block(zone, - nxt_rbtree_root(&zone->free_pages), - alignment, pages); + nxt_rbtree_root(&zone->free_pages), alignment, pages); if (nxt_slow_path(block == NULL)) { return NULL; @@ -666,13 +630,13 @@ nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) nxt_rbtree_delete(&zone->free_pages, &block->node); - p = nxt_align_ptr(block, alignment); + p = nxt_align_ptr(block, alignment); page = nxt_mem_zone_addr_page(zone, p); prev_size = p - (u_char *) block; if (prev_size != 0) { - prev_pages = prev_size >> zone->page_size_shift; + prev_pages = prev_size >> zone->page_size_shift; node_pages -= prev_pages; block->size = prev_pages; @@ -685,8 +649,8 @@ nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) next_pages = node_pages - pages; if (next_pages != 0) { - next_page = &page[pages]; - next_block = nxt_mem_zone_page_addr(zone, next_page); + next_page = &page[pages]; + next_block = nxt_mem_zone_page_addr(zone, next_page); next_block->size = next_pages; nxt_rbtree_insert(&zone->free_pages, &next_block->node); @@ -698,10 +662,9 @@ nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) page[0].u.count = pages; for (n = 0; n < pages; n++) { - if (page[n].size == NXT_MEM_ZONE_PAGE_FRESH) { nxt_mem_zone_fresh_junk(nxt_mem_zone_page_addr(zone, &page[n]), - zone->page_size_mask + 1); + zone->page_size_mask + 1); } page[n].size = NXT_MEM_ZONE_PAGE_USED; @@ -710,7 +673,6 @@ nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) return page; } - /* * Free blocks are sorted by size and then if the sizes are equal * by aligned allocation capabilty. The former criterion is just @@ -725,10 +687,9 @@ nxt_mem_zone_alloc_pages(nxt_mem_zone_t *zone, size_t alignment, uint32_t pages) static nxt_mem_zone_free_block_t * nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, - uint32_t alignment, uint32_t pages) -{ - u_char *aligned, *end; - nxt_mem_zone_free_block_t *block, *free_block; + uint32_t alignment, uint32_t pages) { + u_char *aligned, *end; + nxt_mem_zone_free_block_t *block, *free_block; if (node == nxt_rbtree_sentinel(&zone->free_pages)) { return NULL; @@ -737,9 +698,8 @@ nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, block = (nxt_mem_zone_free_block_t *) node; if (pages <= block->size) { - free_block = nxt_mem_zone_find_free_block(zone, block->node.left, - alignment, pages); + alignment, pages); if (free_block != NULL) { return free_block; } @@ -752,7 +712,7 @@ nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, return block; } - } else { /* pages < block->size */ + } else { /* pages < block->size */ aligned += pages << zone->page_size_shift; end = nxt_pointer_to(block, block->size << zone->page_size_shift); @@ -762,23 +722,20 @@ nxt_mem_zone_find_free_block(nxt_mem_zone_t *zone, nxt_rbtree_node_t *node, } } - return nxt_mem_zone_find_free_block(zone, block->node.right, - alignment, pages); + return nxt_mem_zone_find_free_block(zone, block->node.right, alignment, + pages); } - void -nxt_mem_zone_free(nxt_mem_zone_t *zone, void *p) -{ +nxt_mem_zone_free(nxt_mem_zone_t *zone, void *p) { nxt_uint_t count; - const char *err; - nxt_mem_zone_page_t *page; + const char *err; + nxt_mem_zone_page_t *page; nxt_thread_log_debug("mem zone free: %p", p); - if (nxt_fast_path(zone->start <= (u_char *) p - && (u_char *) p < zone->end)) - { + if (nxt_fast_path( + zone->start <= (u_char *) p && (u_char *) p < zone->end)) { page = nxt_mem_zone_addr_page(zone, p); nxt_thread_spin_lock(&zone->lock); @@ -817,22 +774,21 @@ nxt_mem_zone_free(nxt_mem_zone_t *zone, void *p) } } - static const char * nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, - void *p) -{ - u_char *map; + void *p) { + u_char *map; uint32_t size, offset, chunk; - nxt_mem_zone_page_t *pg, **ppg; - nxt_mem_zone_slot_t *slot; + nxt_mem_zone_page_t *pg, **ppg; + nxt_mem_zone_slot_t *slot; size = page->size; /* Find a zone slot with appropriate chunk size. */ - for (slot = zone->slots; slot->size < size; slot++) { /* void */ } + for (slot = zone->slots; slot->size < size; slot++) { /* void */ + } - offset = (uintptr_t) p & zone->page_size_mask; + offset = (uintptr_t) p & zone->page_size_mask; offset -= slot->start; chunk = offset / size; @@ -861,14 +817,13 @@ nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, page->chunks = 1; /* Add the page to the head of slot list of pages with free chunks. */ - page->next = slot->pages; + page->next = slot->pages; slot->pages = page; } else if (page->chunks != slot->chunks) { page->chunks++; } else { - if (map != page->u.map) { nxt_mem_zone_free_junk(map, slot->map_size); } @@ -880,7 +835,6 @@ nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, ppg = &slot->pages; for (pg = slot->pages; pg != NULL; pg = pg->next) { - if (pg == page) { *ppg = page->next; break; @@ -895,45 +849,41 @@ nxt_mem_zone_free_chunk(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, return NULL; } - static void nxt_mem_zone_free_pages(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, - nxt_uint_t count) -{ - nxt_mem_zone_page_t *prev_page, *next_page; - nxt_mem_zone_free_block_t *block, *prev_block, *next_block; + nxt_uint_t count) { + nxt_mem_zone_page_t *prev_page, *next_page; + nxt_mem_zone_free_block_t *block, *prev_block, *next_block; - page->size = NXT_MEM_ZONE_PAGE_FREE; - page->chunks = 0; + page->size = NXT_MEM_ZONE_PAGE_FREE; + page->chunks = 0; page->u.count = 0; - page->next = NULL; + page->next = NULL; nxt_memzero(&page[1], (count - 1) * sizeof(nxt_mem_zone_page_t)); next_page = nxt_queue_link_data(page->link.next, nxt_mem_zone_page_t, link); if (nxt_mem_zone_page_is_free(next_page)) { - /* Coalesce with the next free pages. */ nxt_queue_remove(&next_page->link); nxt_memzero(next_page, sizeof(nxt_mem_zone_page_t)); - next_block = nxt_mem_zone_page_addr(zone, next_page); - count += next_block->size; + next_block = nxt_mem_zone_page_addr(zone, next_page); + count += next_block->size; nxt_rbtree_delete(&zone->free_pages, &next_block->node); } prev_page = nxt_queue_link_data(page->link.prev, nxt_mem_zone_page_t, link); if (nxt_mem_zone_page_is_free(prev_page)) { - /* Coalesce with the previous free pages. */ nxt_queue_remove(&page->link); - prev_block = nxt_mem_zone_page_addr(zone, prev_page); - count += prev_block->size; + prev_block = nxt_mem_zone_page_addr(zone, prev_page); + count += prev_block->size; nxt_rbtree_delete(&zone->free_pages, &prev_block->node); prev_block->size = count; @@ -942,7 +892,7 @@ nxt_mem_zone_free_pages(nxt_mem_zone_t *zone, nxt_mem_zone_page_t *page, return; } - block = nxt_mem_zone_page_addr(zone, page); + block = nxt_mem_zone_page_addr(zone, page); block->size = count; nxt_rbtree_insert(&zone->free_pages, &block->node); } diff --git a/src/nxt_mem_zone.h b/src/nxt_mem_zone.h index 89d73ca22..482de29e5 100644 --- a/src/nxt_mem_zone.h +++ b/src/nxt_mem_zone.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,21 +7,21 @@ #define _NXT_MEM_ZONE_H_INCLUDED_ -typedef struct nxt_mem_zone_s nxt_mem_zone_t; +typedef struct nxt_mem_zone_s nxt_mem_zone_t; -NXT_EXPORT nxt_mem_zone_t *nxt_mem_zone_init(u_char *start, size_t zone_size, - nxt_uint_t page_size); +NXT_EXPORT nxt_mem_zone_t * +nxt_mem_zone_init(u_char *start, size_t zone_size, nxt_uint_t page_size); -#define nxt_mem_zone_alloc(zone, size) \ - nxt_mem_zone_align((zone), 1, (size)) +#define nxt_mem_zone_alloc(zone, size) nxt_mem_zone_align((zone), 1, (size)) -NXT_EXPORT void *nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, - size_t size) - NXT_MALLOC_LIKE; -NXT_EXPORT void *nxt_mem_zone_zalloc(nxt_mem_zone_t *zone, size_t size) - NXT_MALLOC_LIKE; -NXT_EXPORT void nxt_mem_zone_free(nxt_mem_zone_t *zone, void *p); +NXT_EXPORT void * +nxt_mem_zone_align(nxt_mem_zone_t *zone, size_t alignment, + size_t size) NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mem_zone_zalloc(nxt_mem_zone_t *zone, size_t size) NXT_MALLOC_LIKE; +NXT_EXPORT void +nxt_mem_zone_free(nxt_mem_zone_t *zone, void *p); #endif /* _NXT_MEM_ZONE_H_INCLUDED_ */ diff --git a/src/nxt_mp.c b/src/nxt_mp.c index 2bd8cdee4..990e8f017 100644 --- a/src/nxt_mp.c +++ b/src/nxt_mp.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,7 +5,6 @@ #include - /* * A memory pool allocates memory in clusters of specified size and aligned * to page_alignment. A cluster is divided on pages of specified size. Page @@ -29,39 +27,38 @@ typedef struct { * *) pages with free space for non-freeable allocations, * *) free pages in clusters. */ - nxt_queue_link_t link; + nxt_queue_link_t link; union { /* Chunk bitmap. There can be no more than 32 chunks in a page. */ - uint32_t map; + uint32_t map; /* Size of taken non-freeable space. */ - uint32_t taken; + uint32_t taken; } u; /* * Size of chunks or page shifted by pool->chunk_size_shift. Zero means * that page is free, 0xFF means page with non-freeable allocations. */ - uint8_t size; + uint8_t size; /* Number of free chunks of a chunked page. */ - uint8_t chunks; + uint8_t chunks; /* * Number of allocation fails due to free space insufficiency * in non-freeable page. */ - uint8_t fails; + uint8_t fails; /* * Page number in page cluster. * There can be no more than 256 pages in a cluster. */ - uint8_t number; + uint8_t number; } nxt_mp_page_t; - /* * Some malloc implementations (e.g. jemalloc) allocates large enough * blocks (e.g. greater than 4K) with 4K alignment. So if a block @@ -85,111 +82,101 @@ typedef enum { NXT_MP_EMBEDDED_BLOCK, } nxt_mp_block_type_t; - typedef struct { - NXT_RBTREE_NODE (node); - nxt_mp_block_type_t type:8; - uint8_t freeable; + NXT_RBTREE_NODE(node); + nxt_mp_block_type_t type:8; + uint8_t freeable; /* Block size must be less than 4G. */ - uint32_t size; + uint32_t size; - u_char *start; - nxt_mp_page_t pages[]; + u_char *start; + nxt_mp_page_t pages[]; } nxt_mp_block_t; - struct nxt_mp_s { /* rbtree of nxt_mp_block_t. */ - nxt_rbtree_t blocks; + nxt_rbtree_t blocks; - uint8_t chunk_size_shift; - uint8_t page_size_shift; - uint32_t page_size; - uint32_t page_alignment; - uint32_t cluster_size; - uint32_t retain; + uint8_t chunk_size_shift; + uint8_t page_size_shift; + uint32_t page_size; + uint32_t page_alignment; + uint32_t cluster_size; + uint32_t retain; #if (NXT_DEBUG) - nxt_pid_t pid; - nxt_tid_t tid; + nxt_pid_t pid; + nxt_tid_t tid; #endif - nxt_work_t *cleanup; + nxt_work_t *cleanup; /* Lists of nxt_mp_page_t. */ - nxt_queue_t free_pages; - nxt_queue_t nget_pages; - nxt_queue_t get_pages; - nxt_queue_t chunk_pages[]; + nxt_queue_t free_pages; + nxt_queue_t nget_pages; + nxt_queue_t get_pages; + nxt_queue_t chunk_pages[]; }; - -#define nxt_mp_chunk_get_free(map) \ - (__builtin_ffs(map) - 1) +#define nxt_mp_chunk_get_free(map) (__builtin_ffs(map) - 1) -#define nxt_mp_chunk_is_free(map, chunk) \ - ((map & (1 << chunk)) != 0) +#define nxt_mp_chunk_is_free(map, chunk) ((map & (1 << chunk)) != 0) -#define nxt_mp_chunk_set_busy(map, chunk) \ - map &= ~(1 << chunk) +#define nxt_mp_chunk_set_busy(map, chunk) map &= ~(1 << chunk) -#define nxt_mp_chunk_set_free(map, chunk) \ - map |= (1 << chunk) +#define nxt_mp_chunk_set_free(map, chunk) map |= (1 << chunk) -#define nxt_mp_free_junk(p, size) \ - memset((p), 0x5A, size) +#define nxt_mp_free_junk(p, size) memset((p), 0x5A, size) #if !(NXT_DEBUG_MEMORY) -static void *nxt_mp_alloc_small(nxt_mp_t *mp, size_t size); -static void *nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size); -static nxt_mp_page_t *nxt_mp_alloc_page(nxt_mp_t *mp); -static nxt_mp_block_t *nxt_mp_alloc_cluster(nxt_mp_t *mp); +static void * +nxt_mp_alloc_small(nxt_mp_t *mp, size_t size); +static void * +nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size); +static nxt_mp_page_t * +nxt_mp_alloc_page(nxt_mp_t *mp); +static nxt_mp_block_t * +nxt_mp_alloc_cluster(nxt_mp_t *mp); #endif -static void *nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size, +static void * +nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size, nxt_bool_t freeable); -static intptr_t nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2); -static nxt_mp_block_t *nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p); -static const char *nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, - u_char *p); +static intptr_t +nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2); +static nxt_mp_block_t * +nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p); +static const char * +nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p); #if (NXT_HAVE_BUILTIN_CLZ) -#define nxt_lg2(value) \ - (31 - __builtin_clz(value)) +#define nxt_lg2(value) (31 - __builtin_clz(value)) #else -static const int nxt_lg2_tab64[64] = { - 63, 0, 58, 1, 59, 47, 53, 2, - 60, 39, 48, 27, 54, 33, 42, 3, - 61, 51, 37, 40, 49, 18, 28, 20, - 55, 30, 34, 11, 43, 14, 22, 4, - 62, 57, 46, 52, 38, 26, 32, 41, - 50, 36, 17, 19, 29, 10, 13, 21, - 56, 45, 25, 31, 35, 16, 9, 12, - 44, 24, 15, 8, 23, 7, 6, 5 -}; +static const int nxt_lg2_tab64[64] = {63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, + 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, + 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, + 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5}; static const uint64_t nxt_lg2_magic = 0x07EDD5E59A4E28C2ULL; static int -nxt_lg2(uint64_t v) -{ +nxt_lg2(uint64_t v) { v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v |= v >> 32; - return nxt_lg2_tab64[ ((v - (v >> 1)) * nxt_lg2_magic) >> 58 ]; + return nxt_lg2_tab64[((v - (v >> 1)) * nxt_lg2_magic) >> 58]; } #endif @@ -198,13 +185,12 @@ nxt_lg2(uint64_t v) #if (NXT_DEBUG) nxt_inline void -nxt_mp_thread_assert(nxt_mp_t *mp) -{ +nxt_mp_thread_assert(nxt_mp_t *mp) { nxt_tid_t tid; - nxt_thread_t *thread; + nxt_thread_t *thread; thread = nxt_thread(); - tid = nxt_thread_tid(thread); + tid = nxt_thread_tid(thread); if (nxt_fast_path(mp->tid == tid)) { return; @@ -229,37 +215,34 @@ nxt_mp_thread_assert(nxt_mp_t *mp) void -nxt_mp_thread_adopt(nxt_mp_t *mp) -{ +nxt_mp_thread_adopt(nxt_mp_t *mp) { #if (NXT_DEBUG) mp->pid = nxt_pid; mp->tid = nxt_thread_tid(nxt_thread()); #endif } - nxt_mp_t * nxt_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size, - size_t min_chunk_size) -{ - nxt_mp_t *mp; + size_t min_chunk_size) { + nxt_mp_t *mp; uint32_t pages, chunk_size_shift, page_size_shift; - nxt_queue_t *chunk_pages; + nxt_queue_t *chunk_pages; chunk_size_shift = nxt_lg2(min_chunk_size); - page_size_shift = nxt_lg2(page_size); + page_size_shift = nxt_lg2(page_size); pages = page_size_shift - chunk_size_shift; mp = nxt_zalloc(sizeof(nxt_mp_t) + pages * sizeof(nxt_queue_t)); if (nxt_fast_path(mp != NULL)) { - mp->retain = 1; + mp->retain = 1; mp->chunk_size_shift = chunk_size_shift; - mp->page_size_shift = page_size_shift; - mp->page_size = page_size; - mp->page_alignment = nxt_max(page_alignment, NXT_MAX_ALIGNMENT); - mp->cluster_size = cluster_size; + mp->page_size_shift = page_size_shift; + mp->page_size = page_size; + mp->page_alignment = nxt_max(page_alignment, NXT_MAX_ALIGNMENT); + mp->cluster_size = cluster_size; chunk_pages = mp->chunk_pages; @@ -277,24 +260,20 @@ nxt_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size, } nxt_debug_alloc("mp %p create(%uz, %uz, %uz, %uz)", mp, cluster_size, - page_alignment, page_size, min_chunk_size); + page_alignment, page_size, min_chunk_size); return mp; } - void -nxt_mp_retain(nxt_mp_t *mp) -{ +nxt_mp_retain(nxt_mp_t *mp) { mp->retain++; nxt_thread_log_debug("mp %p retain: %uD", mp, mp->retain); } - void -nxt_mp_release(nxt_mp_t *mp) -{ +nxt_mp_release(nxt_mp_t *mp) { mp->retain--; nxt_thread_log_debug("mp %p release: %uD", mp, mp->retain); @@ -304,21 +283,19 @@ nxt_mp_release(nxt_mp_t *mp) } } - void -nxt_mp_destroy(nxt_mp_t *mp) -{ - void *p; - nxt_work_t *work, *next_work; - nxt_mp_block_t *block; - nxt_rbtree_node_t *node, *next; +nxt_mp_destroy(nxt_mp_t *mp) { + void *p; + nxt_work_t *work, *next_work; + nxt_mp_block_t *block; + nxt_rbtree_node_t *node, *next; nxt_debug_alloc("mp %p destroy", mp); nxt_mp_thread_assert(mp); while (mp->cleanup != NULL) { - work = mp->cleanup; + work = mp->cleanup; next_work = work->next; work->handler(work->task, work->obj, work->data); @@ -329,8 +306,7 @@ nxt_mp_destroy(nxt_mp_t *mp) next = nxt_rbtree_root(&mp->blocks); while (next != nxt_rbtree_sentinel(&mp->blocks)) { - - node = nxt_rbtree_destroy_next(&mp->blocks, &next); + node = nxt_rbtree_destroy_next(&mp->blocks, &next); block = (nxt_mp_block_t *) node; p = block->start; @@ -345,31 +321,27 @@ nxt_mp_destroy(nxt_mp_t *mp) nxt_free(mp); } - nxt_bool_t nxt_mp_test_sizes(size_t cluster_size, size_t page_alignment, size_t page_size, - size_t min_chunk_size) -{ - nxt_bool_t valid; + size_t min_chunk_size) { + nxt_bool_t valid; /* Alignment and sizes must be a power of 2. */ - valid = nxt_expect(1, (nxt_is_power_of_two(page_alignment) - && nxt_is_power_of_two(page_size) - && nxt_is_power_of_two(min_chunk_size))); + valid = nxt_expect(1, + (nxt_is_power_of_two(page_alignment) && nxt_is_power_of_two(page_size) + && nxt_is_power_of_two(min_chunk_size))); if (!valid) { return 0; } page_alignment = nxt_max(page_alignment, NXT_MAX_ALIGNMENT); - valid = nxt_expect(1, (page_size >= 64 - && page_size >= page_alignment - && page_size >= min_chunk_size - && min_chunk_size * 32 >= page_size - && cluster_size >= page_size - && cluster_size / page_size <= 256 - && cluster_size % page_size == 0)); + valid = nxt_expect(1, + (page_size >= 64 && page_size >= page_alignment + && page_size >= min_chunk_size && min_chunk_size * 32 >= page_size + && cluster_size >= page_size && cluster_size / page_size <= 256 + && cluster_size % page_size == 0)); if (!valid) { return 0; } @@ -377,19 +349,15 @@ nxt_mp_test_sizes(size_t cluster_size, size_t page_alignment, size_t page_size, return 1; } - nxt_bool_t -nxt_mp_is_empty(nxt_mp_t *mp) -{ +nxt_mp_is_empty(nxt_mp_t *mp) { return (nxt_rbtree_is_empty(&mp->blocks) && nxt_queue_is_empty(&mp->free_pages)); } - void * -nxt_mp_alloc(nxt_mp_t *mp, size_t size) -{ - void *p; +nxt_mp_alloc(nxt_mp_t *mp, size_t size) { + void *p; #if !(NXT_DEBUG_MEMORY) @@ -411,11 +379,9 @@ nxt_mp_alloc(nxt_mp_t *mp, size_t size) return p; } - void * -nxt_mp_zalloc(nxt_mp_t *mp, size_t size) -{ - void *p; +nxt_mp_zalloc(nxt_mp_t *mp, size_t size) { + void *p; p = nxt_mp_alloc(mp, size); @@ -426,19 +392,16 @@ nxt_mp_zalloc(nxt_mp_t *mp, size_t size) return p; } - void * -nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) -{ - void *p; +nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) { + void *p; /* Alignment must be a power of 2. */ if (nxt_fast_path(nxt_is_power_of_two(alignment))) { - #if !(NXT_DEBUG_MEMORY) - size_t aligned_size; + size_t aligned_size; aligned_size = nxt_max(size, alignment); @@ -464,11 +427,9 @@ nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) return p; } - void * -nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) -{ - void *p; +nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) { + void *p; p = nxt_mp_align(mp, alignment, size); @@ -479,11 +440,9 @@ nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) return p; } - nxt_inline nxt_uint_t -nxt_mp_chunk_pages_index(nxt_mp_t *mp, size_t size) -{ - nxt_int_t n, index; +nxt_mp_chunk_pages_index(nxt_mp_t *mp, size_t size) { + nxt_int_t n, index; index = 0; @@ -502,10 +461,9 @@ nxt_mp_chunk_pages_index(nxt_mp_t *mp, size_t size) #if !(NXT_DEBUG_MEMORY) nxt_inline u_char * -nxt_mp_page_addr(nxt_mp_t *mp, nxt_mp_page_t *page) -{ +nxt_mp_page_addr(nxt_mp_t *mp, nxt_mp_page_t *page) { size_t page_offset; - nxt_mp_block_t *block; + nxt_mp_block_t *block; page_offset = page->number * sizeof(nxt_mp_page_t) + offsetof(nxt_mp_block_t, pages); @@ -515,27 +473,23 @@ nxt_mp_page_addr(nxt_mp_t *mp, nxt_mp_page_t *page) return block->start + (page->number << mp->page_size_shift); } - static void * -nxt_mp_alloc_small(nxt_mp_t *mp, size_t size) -{ - u_char *p; +nxt_mp_alloc_small(nxt_mp_t *mp, size_t size) { + u_char *p; nxt_uint_t n, index; - nxt_queue_t *chunk_pages; - nxt_mp_page_t *page; - nxt_queue_link_t *link; + nxt_queue_t *chunk_pages; + nxt_mp_page_t *page; + nxt_queue_link_t *link; nxt_mp_thread_assert(mp); p = NULL; if (size <= mp->page_size / 2) { - - index = nxt_mp_chunk_pages_index(mp, size); + index = nxt_mp_chunk_pages_index(mp, size); chunk_pages = &mp->chunk_pages[index]; if (nxt_fast_path(!nxt_queue_is_empty(chunk_pages))) { - link = nxt_queue_first(chunk_pages); page = nxt_queue_link_data(link, nxt_mp_page_t, link); @@ -585,26 +539,22 @@ nxt_mp_alloc_small(nxt_mp_t *mp, size_t size) } nxt_debug_alloc("mp %p chunk:%uz alloc: %p", mp, - page->size << mp->chunk_size_shift, p); + page->size << mp->chunk_size_shift, p); return p; } - static void * -nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size) -{ - u_char *p; +nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size) { + u_char *p; uint32_t available; - nxt_mp_page_t *page; - nxt_queue_link_t *link, *next; + nxt_mp_page_t *page; + nxt_queue_link_t *link, *next; nxt_mp_thread_assert(mp); - for (link = nxt_queue_first(pages); - link != nxt_queue_tail(pages); - link = next) - { + for (link = nxt_queue_first(pages); link != nxt_queue_tail(pages); + link = next) { next = nxt_queue_next(link); page = nxt_queue_link_data(link, nxt_mp_page_t, link); @@ -627,26 +577,24 @@ nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size) nxt_queue_insert_head(pages, &page->link); - page->size = 0xFF; + page->size = 0xFF; page->u.taken = 0; found: p = nxt_mp_page_addr(mp, page); - p += page->u.taken; + p += page->u.taken; page->u.taken += size; return p; } - static nxt_mp_page_t * -nxt_mp_alloc_page(nxt_mp_t *mp) -{ - nxt_mp_page_t *page; - nxt_mp_block_t *cluster; - nxt_queue_link_t *link; +nxt_mp_alloc_page(nxt_mp_t *mp) { + nxt_mp_page_t *page; + nxt_mp_block_t *cluster; + nxt_queue_link_t *link; if (nxt_queue_is_empty(&mp->free_pages)) { cluster = nxt_mp_alloc_cluster(mp); @@ -663,12 +611,10 @@ nxt_mp_alloc_page(nxt_mp_t *mp) return page; } - static nxt_mp_block_t * -nxt_mp_alloc_cluster(nxt_mp_t *mp) -{ +nxt_mp_alloc_cluster(nxt_mp_t *mp) { nxt_uint_t n; - nxt_mp_block_t *cluster; + nxt_mp_block_t *cluster; n = mp->cluster_size >> mp->page_size_shift; @@ -696,7 +642,7 @@ nxt_mp_alloc_cluster(nxt_mp_t *mp) n--; cluster->pages[n].number = n; nxt_queue_insert_before(&cluster->pages[n + 1].link, - &cluster->pages[n].link); + &cluster->pages[n].link); } nxt_rbtree_insert(&mp->blocks, &cluster->node); @@ -709,12 +655,11 @@ nxt_mp_alloc_cluster(nxt_mp_t *mp) static void * nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size, - nxt_bool_t freeable) -{ - u_char *p; + nxt_bool_t freeable) { + u_char *p; size_t aligned_size; uint8_t type; - nxt_mp_block_t *block; + nxt_mp_block_t *block; nxt_mp_thread_assert(mp); @@ -746,24 +691,22 @@ nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size, } block = (nxt_mp_block_t *) (p + aligned_size); - type = NXT_MP_EMBEDDED_BLOCK; + type = NXT_MP_EMBEDDED_BLOCK; } - block->type = type; + block->type = type; block->freeable = freeable; - block->size = size; - block->start = p; + block->size = size; + block->start = p; nxt_rbtree_insert(&mp->blocks, &block->node); return p; } - static intptr_t -nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) -{ - nxt_mp_block_t *block1, *block2; +nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) { + nxt_mp_block_t *block1, *block2; block1 = (nxt_mp_block_t *) node1; block2 = (nxt_mp_block_t *) node2; @@ -779,12 +722,10 @@ nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) return ((uintptr_t) block1->start >> 1) - ((uintptr_t) block2->start >> 1); } - void -nxt_mp_free(nxt_mp_t *mp, void *p) -{ - const char *err; - nxt_mp_block_t *block; +nxt_mp_free(nxt_mp_t *mp, void *p) { + const char *err; + nxt_mp_block_t *block; nxt_mp_thread_assert(mp); @@ -793,7 +734,6 @@ nxt_mp_free(nxt_mp_t *mp, void *p) block = nxt_mp_find_block(&mp->blocks, p); if (nxt_fast_path(block != NULL)) { - if (block->type == NXT_MP_CLUSTER_BLOCK) { err = nxt_mp_chunk_free(mp, block, p); @@ -802,7 +742,6 @@ nxt_mp_free(nxt_mp_t *mp, void *p) } } else if (nxt_fast_path(p == block->start)) { - if (block->freeable) { nxt_rbtree_delete(&mp->blocks, &block->node); @@ -828,18 +767,15 @@ nxt_mp_free(nxt_mp_t *mp, void *p) nxt_thread_log_alert(err, p); } - static nxt_mp_block_t * -nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p) -{ - nxt_mp_block_t *block; - nxt_rbtree_node_t *node, *sentinel; +nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p) { + nxt_mp_block_t *block; + nxt_rbtree_node_t *node, *sentinel; - node = nxt_rbtree_root(tree); + node = nxt_rbtree_root(tree); sentinel = nxt_rbtree_sentinel(tree); while (node != sentinel) { - block = (nxt_mp_block_t *) node; if (p < block->start) { @@ -856,17 +792,15 @@ nxt_mp_find_block(nxt_rbtree_t *tree, const u_char *p) return NULL; } - static const char * -nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) -{ - u_char *start; +nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) { + u_char *start; uintptr_t offset; nxt_uint_t n, size, chunk; - nxt_queue_t *chunk_pages; - nxt_mp_page_t *page; + nxt_queue_t *chunk_pages; + nxt_mp_page_t *page; - n = (p - cluster->start) >> mp->page_size_shift; + n = (p - cluster->start) >> mp->page_size_shift; start = cluster->start + (n << mp->page_size_shift); page = &cluster->pages[n]; @@ -882,9 +816,8 @@ nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) size = page->size << mp->chunk_size_shift; if (size != mp->page_size) { - offset = (uintptr_t) (p - start) & (mp->page_size - 1); - chunk = offset / size; + chunk = offset / size; if (nxt_slow_path(offset != chunk * size)) { return "freed pointer points to wrong chunk: %p"; @@ -904,7 +837,7 @@ nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) * Add the page to the head of pool chunk pages list * of pages with free chunks. */ - n = nxt_mp_chunk_pages_index(mp, size); + n = nxt_mp_chunk_pages_index(mp, size); chunk_pages = &mp->chunk_pages[n]; nxt_queue_insert_head(chunk_pages, &page->link); @@ -935,7 +868,7 @@ nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) /* Test if all pages in the cluster are free. */ - n = mp->cluster_size >> mp->page_size_shift; + n = mp->cluster_size >> mp->page_size_shift; page = cluster->pages; do { @@ -949,7 +882,7 @@ nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) /* Free cluster. */ - n = mp->cluster_size >> mp->page_size_shift; + n = mp->cluster_size >> mp->page_size_shift; page = cluster->pages; do { @@ -968,11 +901,9 @@ nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p) return NULL; } - void * -nxt_mp_nget(nxt_mp_t *mp, size_t size) -{ - void *p; +nxt_mp_nget(nxt_mp_t *mp, size_t size) { + void *p; #if !(NXT_DEBUG_MEMORY) @@ -994,17 +925,15 @@ nxt_mp_nget(nxt_mp_t *mp, size_t size) return p; } - void * -nxt_mp_get(nxt_mp_t *mp, size_t size) -{ - void *p; +nxt_mp_get(nxt_mp_t *mp, size_t size) { + void *p; #if !(NXT_DEBUG_MEMORY) if (size <= mp->page_size) { size = nxt_max(size, NXT_MAX_ALIGNMENT); - p = nxt_mp_get_small(mp, &mp->get_pages, size); + p = nxt_mp_get_small(mp, &mp->get_pages, size); } else { p = nxt_mp_alloc_large(mp, NXT_MAX_ALIGNMENT, size, 0); @@ -1021,11 +950,9 @@ nxt_mp_get(nxt_mp_t *mp, size_t size) return p; } - void * -nxt_mp_zget(nxt_mp_t *mp, size_t size) -{ - void *p; +nxt_mp_zget(nxt_mp_t *mp, size_t size) { + void *p; p = nxt_mp_get(mp, size); @@ -1036,12 +963,10 @@ nxt_mp_zget(nxt_mp_t *mp, size_t size) return p; } - nxt_int_t -nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, - nxt_task_t *task, void *obj, void *data) -{ - nxt_work_t *work; +nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, nxt_task_t *task, + void *obj, void *data) { + nxt_work_t *work; work = nxt_mp_get(mp, sizeof(nxt_work_t)); @@ -1049,27 +974,23 @@ nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, return NXT_ERROR; } - work->next = mp->cleanup; + work->next = mp->cleanup; work->handler = handler; - work->task = task; - work->obj = obj; - work->data = data; + work->task = task; + work->obj = obj; + work->data = data; mp->cleanup = work; return NXT_OK; } - void * -nxt_mp_lvlhsh_alloc(void *pool, size_t size) -{ +nxt_mp_lvlhsh_alloc(void *pool, size_t size) { return nxt_mp_align(pool, size, size); } - void -nxt_mp_lvlhsh_free(void *pool, void *p) -{ +nxt_mp_lvlhsh_free(void *pool, void *p) { nxt_mp_free(pool, p); } diff --git a/src/nxt_mp.h b/src/nxt_mp.h index a5aaabd19..8b5c995df 100644 --- a/src/nxt_mp.h +++ b/src/nxt_mp.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -24,96 +23,106 @@ * may also improve data cache locality. */ -typedef struct nxt_mp_s nxt_mp_t; +typedef struct nxt_mp_s nxt_mp_t; /* * nxt_mp_create() creates a memory pool and sets the pool's retention * counter to 1. */ -NXT_EXPORT nxt_mp_t *nxt_mp_create(size_t cluster_size, size_t page_alignment, - size_t page_size, size_t min_chunk_size) - NXT_MALLOC_LIKE; +NXT_EXPORT nxt_mp_t * +nxt_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size, + size_t min_chunk_size) NXT_MALLOC_LIKE; /* * nxt_mp_destroy() destroys memory pool in spite of the pool's retention * counter. */ -NXT_EXPORT void nxt_mp_destroy(nxt_mp_t *mp); +NXT_EXPORT void +nxt_mp_destroy(nxt_mp_t *mp); /* * nxt_mp_retain() increases memory pool retention counter. */ -NXT_EXPORT void nxt_mp_retain(nxt_mp_t *mp); +NXT_EXPORT void +nxt_mp_retain(nxt_mp_t *mp); /* * nxt_mp_release() decreases memory pool retention counter. * If the counter becomes zero the pool is destroyed. */ -NXT_EXPORT void nxt_mp_release(nxt_mp_t *mp); +NXT_EXPORT void +nxt_mp_release(nxt_mp_t *mp); /* nxt_mp_test_sizes() tests validity of memory pool parameters. */ -NXT_EXPORT nxt_bool_t nxt_mp_test_sizes(size_t cluster_size, - size_t page_alignment, size_t page_size, size_t min_chunk_size); +NXT_EXPORT nxt_bool_t +nxt_mp_test_sizes(size_t cluster_size, size_t page_alignment, size_t page_size, + size_t min_chunk_size); /* nxt_mp_is_empty() tests that pool is empty. */ -NXT_EXPORT nxt_bool_t nxt_mp_is_empty(nxt_mp_t *mp); +NXT_EXPORT nxt_bool_t +nxt_mp_is_empty(nxt_mp_t *mp); /* * nxt_mp_alloc() returns aligned freeable memory. * The alignment is sutiable to allocate structures. */ -NXT_EXPORT void *nxt_mp_alloc(nxt_mp_t *mp, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_alloc(nxt_mp_t *mp, size_t size) NXT_MALLOC_LIKE; /* * nxt_mp_zalloc() returns zeroed aligned freeable memory. * The alignment is sutiable to allocate structures. */ -NXT_EXPORT void *nxt_mp_zalloc(nxt_mp_t *mp, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_zalloc(nxt_mp_t *mp, size_t size) NXT_MALLOC_LIKE; /* nxt_mp_align() returns aligned freeable memory. */ -NXT_EXPORT void *nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size) NXT_MALLOC_LIKE; /* nxt_mp_zalign() returns zeroed aligned freeable memory. */ -NXT_EXPORT void *nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size) NXT_MALLOC_LIKE; /* nxt_mp_free() frees freeable memory. */ -NXT_EXPORT void nxt_mp_free(nxt_mp_t *mp, void *p); +NXT_EXPORT void +nxt_mp_free(nxt_mp_t *mp, void *p); /* nxt_mp_nget() returns non-aligned non-freeable memory. */ -NXT_EXPORT void *nxt_mp_nget(nxt_mp_t *mp, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_nget(nxt_mp_t *mp, size_t size) NXT_MALLOC_LIKE; /* * nxt_mp_get() returns aligned non-freeable memory. * The alignment is sutiable to allocate structures. */ -NXT_EXPORT void *nxt_mp_get(nxt_mp_t *mp, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_get(nxt_mp_t *mp, size_t size) NXT_MALLOC_LIKE; /* * nxt_mp_zget() returns zeroed aligned non-freeable memory. * The alignment is sutiable to allocate structures. */ -NXT_EXPORT void *nxt_mp_zget(nxt_mp_t *mp, size_t size) - NXT_MALLOC_LIKE; +NXT_EXPORT void * +nxt_mp_zget(nxt_mp_t *mp, size_t size) NXT_MALLOC_LIKE; -NXT_EXPORT nxt_int_t nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, - nxt_task_t *task, void *obj, void *data); +NXT_EXPORT nxt_int_t +nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler, nxt_task_t *task, + void *obj, void *data); -NXT_EXPORT void nxt_mp_thread_adopt(nxt_mp_t *mp); +NXT_EXPORT void +nxt_mp_thread_adopt(nxt_mp_t *mp); -NXT_EXPORT void *nxt_mp_lvlhsh_alloc(void *pool, size_t size); -NXT_EXPORT void nxt_mp_lvlhsh_free(void *pool, void *p); +NXT_EXPORT void * +nxt_mp_lvlhsh_alloc(void *pool, size_t size); +NXT_EXPORT void +nxt_mp_lvlhsh_free(void *pool, void *p); #endif /* _NXT_MP_H_INCLUDED_ */ diff --git a/src/nxt_murmur_hash.c b/src/nxt_murmur_hash.c index 56bf2d4b6..dfbd4c9d1 100644 --- a/src/nxt_murmur_hash.c +++ b/src/nxt_murmur_hash.c @@ -1,4 +1,3 @@ - /* * The code is based on the code by Austin Appleby, * released to the public domain. @@ -6,13 +5,11 @@ #include - uint32_t -nxt_murmur_hash2(const void *data, size_t len) -{ - uint32_t h, k; - const u_char *p; - const uint32_t m = 0x5BD1E995; +nxt_murmur_hash2(const void *data, size_t len) { + uint32_t h, k; + const u_char *p; + const uint32_t m = 0x5BD1E995; p = data; h = 0 ^ (uint32_t) len; @@ -30,7 +27,7 @@ nxt_murmur_hash2(const void *data, size_t len) h *= m; h ^= k; - p += 4; + p += 4; len -= 4; } @@ -53,15 +50,13 @@ nxt_murmur_hash2(const void *data, size_t len) return h; } - /* The MurmurHash2 for fixed 4 byte length. */ uint32_t -nxt_murmur_hash2_uint32(const void *data) -{ - uint32_t h, k; - const u_char *p; - const uint32_t m = 0x5BD1E995; +nxt_murmur_hash2_uint32(const void *data) { + uint32_t h, k; + const u_char *p; + const uint32_t m = 0x5BD1E995; p = data; @@ -74,7 +69,7 @@ nxt_murmur_hash2_uint32(const void *data) k ^= k >> 24; k *= m; - h = 0 ^ 4; + h = 0 ^ 4; h *= m; h ^= k; diff --git a/src/nxt_murmur_hash.h b/src/nxt_murmur_hash.h index 289dc5b01..a7d5f4e54 100644 --- a/src/nxt_murmur_hash.h +++ b/src/nxt_murmur_hash.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,8 +7,10 @@ #define _NXT_MURMUR_HASH_H_INCLUDED_ -NXT_EXPORT uint32_t nxt_murmur_hash2(const void *data, size_t len); -NXT_EXPORT uint32_t nxt_murmur_hash2_uint32(const void *data); +NXT_EXPORT uint32_t +nxt_murmur_hash2(const void *data, size_t len); +NXT_EXPORT uint32_t +nxt_murmur_hash2_uint32(const void *data); #endif /* _NXT_MURMUR_HASH_H_INCLUDED_ */ diff --git a/src/nxt_nncq.h b/src/nxt_nncq.h index 6c9ab3262..75103f2b1 100644 --- a/src/nxt_nncq.h +++ b/src/nxt_nncq.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,98 +8,76 @@ /* Numeric Naive Circular Queue */ -#define NXT_NNCQ_SIZE 16384 +#define NXT_NNCQ_SIZE 16384 typedef uint32_t nxt_nncq_atomic_t; typedef uint16_t nxt_nncq_cycle_t; typedef struct { - nxt_nncq_atomic_t head; - nxt_nncq_atomic_t entries[NXT_NNCQ_SIZE]; - nxt_nncq_atomic_t tail; + nxt_nncq_atomic_t head; + nxt_nncq_atomic_t entries[NXT_NNCQ_SIZE]; + nxt_nncq_atomic_t tail; } nxt_nncq_t; - static inline nxt_nncq_atomic_t -nxt_nncq_head(nxt_nncq_t const volatile *q) -{ +nxt_nncq_head(nxt_nncq_t const volatile *q) { return q->head; } - static inline nxt_nncq_atomic_t -nxt_nncq_tail(nxt_nncq_t const volatile *q) -{ +nxt_nncq_tail(nxt_nncq_t const volatile *q) { return q->tail; } - static inline void -nxt_nncq_tail_cmp_inc(nxt_nncq_t volatile *q, nxt_nncq_atomic_t t) -{ +nxt_nncq_tail_cmp_inc(nxt_nncq_t volatile *q, nxt_nncq_atomic_t t) { nxt_atomic_cmp_set(&q->tail, t, t + 1); } - static inline nxt_nncq_atomic_t -nxt_nncq_index(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) -{ +nxt_nncq_index(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) { return i % NXT_NNCQ_SIZE; } - static inline nxt_nncq_atomic_t -nxt_nncq_map(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) -{ +nxt_nncq_map(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) { return i % NXT_NNCQ_SIZE; } - static inline nxt_nncq_cycle_t -nxt_nncq_cycle(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) -{ +nxt_nncq_cycle(nxt_nncq_t const volatile *q, nxt_nncq_atomic_t i) { return i / NXT_NNCQ_SIZE; } - static inline nxt_nncq_cycle_t -nxt_nncq_next_cycle(nxt_nncq_t const volatile *q, nxt_nncq_cycle_t i) -{ +nxt_nncq_next_cycle(nxt_nncq_t const volatile *q, nxt_nncq_cycle_t i) { return i + 1; } - static inline nxt_nncq_atomic_t nxt_nncq_new_entry(nxt_nncq_t const volatile *q, nxt_nncq_cycle_t cycle, - nxt_nncq_atomic_t i) -{ + nxt_nncq_atomic_t i) { return cycle * NXT_NNCQ_SIZE + (i % NXT_NNCQ_SIZE); } - static inline nxt_nncq_atomic_t -nxt_nncq_empty(nxt_nncq_t const volatile *q) -{ +nxt_nncq_empty(nxt_nncq_t const volatile *q) { return NXT_NNCQ_SIZE; } - static inline void -nxt_nncq_init(nxt_nncq_t volatile *q) -{ +nxt_nncq_init(nxt_nncq_t volatile *q) { q->head = NXT_NNCQ_SIZE; nxt_memzero((void *) q->entries, NXT_NNCQ_SIZE * sizeof(nxt_nncq_atomic_t)); q->tail = NXT_NNCQ_SIZE; } - static inline void -nxt_nncq_enqueue(nxt_nncq_t volatile *q, nxt_nncq_atomic_t val) -{ - nxt_nncq_cycle_t e_cycle, t_cycle; - nxt_nncq_atomic_t n, t, e, j; +nxt_nncq_enqueue(nxt_nncq_t volatile *q, nxt_nncq_atomic_t val) { + nxt_nncq_cycle_t e_cycle, t_cycle; + nxt_nncq_atomic_t n, t, e, j; - for ( ;; ) { + for (;;) { t = nxt_nncq_tail(q); j = nxt_nncq_map(q, t); e = q->entries[j]; @@ -127,14 +104,12 @@ nxt_nncq_enqueue(nxt_nncq_t volatile *q, nxt_nncq_atomic_t val) nxt_nncq_tail_cmp_inc(q, t); } - static inline nxt_nncq_atomic_t -nxt_nncq_dequeue(nxt_nncq_t volatile *q) -{ - nxt_nncq_cycle_t e_cycle, h_cycle; - nxt_nncq_atomic_t h, j, e; +nxt_nncq_dequeue(nxt_nncq_t volatile *q) { + nxt_nncq_cycle_t e_cycle, h_cycle; + nxt_nncq_atomic_t h, j, e; - for ( ;; ) { + for (;;) { h = nxt_nncq_head(q); j = nxt_nncq_map(q, h); e = q->entries[j]; diff --git a/src/nxt_nvbcq.h b/src/nxt_nvbcq.h index e164615b6..11050cda2 100644 --- a/src/nxt_nvbcq.h +++ b/src/nxt_nvbcq.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,65 +8,51 @@ /* Numeric VBart Circular Queue */ -#define NXT_NVBCQ_SIZE 16384 +#define NXT_NVBCQ_SIZE 16384 typedef uint32_t nxt_nvbcq_atomic_t; struct nxt_nvbcq_s { - nxt_nvbcq_atomic_t head; - nxt_nvbcq_atomic_t entries[NXT_NVBCQ_SIZE]; - nxt_nvbcq_atomic_t tail; + nxt_nvbcq_atomic_t head; + nxt_nvbcq_atomic_t entries[NXT_NVBCQ_SIZE]; + nxt_nvbcq_atomic_t tail; }; typedef struct nxt_nvbcq_s nxt_nvbcq_t; - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_head(nxt_nvbcq_t const volatile *q) -{ +nxt_nvbcq_head(nxt_nvbcq_t const volatile *q) { return q->head; } - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_tail(nxt_nvbcq_t const volatile *q) -{ +nxt_nvbcq_tail(nxt_nvbcq_t const volatile *q) { return q->tail; } - static inline void -nxt_nvbcq_tail_cmp_inc(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t t) -{ +nxt_nvbcq_tail_cmp_inc(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t t) { nxt_atomic_cmp_set(&q->tail, t, t + 1); } - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_index(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i) -{ +nxt_nvbcq_index(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i) { return i % NXT_NVBCQ_SIZE; } - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_map(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i) -{ +nxt_nvbcq_map(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i) { return i % NXT_NVBCQ_SIZE; } - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_empty(nxt_nvbcq_t const volatile *q) -{ +nxt_nvbcq_empty(nxt_nvbcq_t const volatile *q) { return NXT_NVBCQ_SIZE; } - static inline void -nxt_nvbcq_init(nxt_nvbcq_t volatile *q) -{ - nxt_nvbcq_atomic_t i; +nxt_nvbcq_init(nxt_nvbcq_t volatile *q) { + nxt_nvbcq_atomic_t i; q->head = 0; @@ -78,21 +63,18 @@ nxt_nvbcq_init(nxt_nvbcq_t volatile *q) q->tail = NXT_NVBCQ_SIZE; } - static inline void -nxt_nvbcq_enqueue(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t val) -{ - nxt_nvbcq_atomic_t t, h, i; +nxt_nvbcq_enqueue(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t val) { + nxt_nvbcq_atomic_t t, h, i; t = nxt_nvbcq_tail(q); h = t - NXT_NVBCQ_SIZE; - for ( ;; ) { + for (;;) { i = nxt_nvbcq_map(q, t); if (q->entries[i] == NXT_NVBCQ_SIZE - && nxt_atomic_cmp_set(&q->entries[i], NXT_NVBCQ_SIZE, val)) - { + && nxt_atomic_cmp_set(&q->entries[i], NXT_NVBCQ_SIZE, val)) { nxt_nvbcq_tail_cmp_inc(q, t); return; } @@ -109,22 +91,19 @@ nxt_nvbcq_enqueue(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t val) } } - static inline nxt_nvbcq_atomic_t -nxt_nvbcq_dequeue(nxt_nvbcq_t volatile *q) -{ - nxt_nvbcq_atomic_t h, t, i, e; +nxt_nvbcq_dequeue(nxt_nvbcq_t volatile *q) { + nxt_nvbcq_atomic_t h, t, i, e; h = nxt_nvbcq_head(q); t = h + NXT_NVBCQ_SIZE; - for ( ;; ) { + for (;;) { i = nxt_nvbcq_map(q, h); e = q->entries[i]; if (e < NXT_NVBCQ_SIZE - && nxt_atomic_cmp_set(&q->entries[i], e, NXT_NVBCQ_SIZE)) - { + && nxt_atomic_cmp_set(&q->entries[i], e, NXT_NVBCQ_SIZE)) { nxt_atomic_cmp_set(&q->head, h, h + 1); return e; diff --git a/src/nxt_openssl.c b/src/nxt_openssl.c index 8f66f45b3..bffd94e91 100644 --- a/src/nxt_openssl.c +++ b/src/nxt_openssl.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -17,34 +16,30 @@ #include #include - typedef struct { - SSL *session; - nxt_conn_t *conn; + SSL *session; + nxt_conn_t *conn; - int ssl_error; - uint8_t times; /* 2 bits */ - uint8_t handshake; /* 1 bit */ + int ssl_error; + uint8_t times; /* 2 bits */ + uint8_t handshake; /* 1 bit */ - nxt_tls_conf_t *conf; - nxt_buf_mem_t buffer; + nxt_tls_conf_t *conf; + nxt_buf_mem_t buffer; } nxt_openssl_conn_t; - struct nxt_tls_ticket_s { - u_char name[16]; - u_char hmac_key[32]; - u_char aes_key[32]; - uint8_t size; + u_char name[16]; + u_char hmac_key[32]; + u_char aes_key[32]; + uint8_t size; }; - struct nxt_tls_tickets_s { - nxt_uint_t count; - nxt_tls_ticket_t tickets[]; + nxt_uint_t count; + nxt_tls_ticket_t tickets[]; }; - typedef enum { NXT_OPENSSL_HANDSHAKE = 0, NXT_OPENSSL_READ, @@ -52,60 +47,80 @@ typedef enum { NXT_OPENSSL_SHUTDOWN, } nxt_openssl_io_t; - -static nxt_int_t nxt_openssl_library_init(nxt_task_t *task); -static void nxt_openssl_library_free(nxt_task_t *task); +static nxt_int_t +nxt_openssl_library_init(nxt_task_t *task); +static void +nxt_openssl_library_free(nxt_task_t *task); #if OPENSSL_VERSION_NUMBER < 0x10100004L -static nxt_int_t nxt_openssl_locks_init(void); -static void nxt_openssl_lock(int mode, int type, const char *file, int line); -static unsigned long nxt_openssl_thread_id(void); -static void nxt_openssl_locks_free(void); +static nxt_int_t +nxt_openssl_locks_init(void); +static void +nxt_openssl_lock(int mode, int type, const char *file, int line); +static unsigned long +nxt_openssl_thread_id(void); +static void +nxt_openssl_locks_free(void); #endif -static nxt_int_t nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, +static nxt_int_t +nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, nxt_tls_init_t *tls_init, nxt_bool_t last); -static nxt_int_t nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, - nxt_tls_conf_t *conf, nxt_mp_t *mp, nxt_bool_t single); +static nxt_int_t +nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf, + nxt_mp_t *mp, nxt_bool_t single); #if (NXT_HAVE_OPENSSL_CONF_CMD) -static nxt_int_t nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, - nxt_conf_value_t *value, nxt_mp_t *mp); +static nxt_int_t +nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *value, + nxt_mp_t *mp); #endif #if (NXT_HAVE_OPENSSL_TLSEXT) -static nxt_int_t nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, - nxt_tls_init_t *tls_init, nxt_mp_t *mp); -static int nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, - unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc); +static nxt_int_t +nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init, + nxt_mp_t *mp); +static int +nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, + EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc); #endif -static void nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, - time_t timeout); -static nxt_uint_t nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, - nxt_tls_conf_t *conf, nxt_mp_t *mp); -static nxt_int_t nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq, - void *data); -static nxt_int_t nxt_openssl_bundle_hash_insert(nxt_task_t *task, - nxt_lvlhsh_t *lvlhsh, nxt_tls_bundle_hash_item_t *item, nxt_mp_t * mp); -static nxt_int_t nxt_openssl_servername(SSL *s, int *ad, void *arg); -static nxt_tls_bundle_conf_t *nxt_openssl_find_ctx(nxt_tls_conf_t *conf, - nxt_str_t *sn); -static void nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf); -static void nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, - nxt_conn_t *c); -static void nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data); -static ssize_t nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); -static ssize_t nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb); -static ssize_t nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, - void *buf, size_t size); -static void nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, - void *data); -static nxt_int_t nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, - int ret, nxt_err_t sys_err, nxt_openssl_io_t io); -static void nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, - void *data); -static void nxt_cdecl nxt_openssl_conn_error(nxt_task_t *task, - nxt_err_t err, const char *fmt, ...); -static nxt_uint_t nxt_openssl_log_error_level(nxt_err_t err); - - -const nxt_tls_lib_t nxt_openssl_lib = { +static void +nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, time_t timeout); +static nxt_uint_t +nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, + nxt_mp_t *mp); +static nxt_int_t +nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_int_t +nxt_openssl_bundle_hash_insert(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, + nxt_tls_bundle_hash_item_t *item, nxt_mp_t *mp); +static nxt_int_t +nxt_openssl_servername(SSL *s, int *ad, void *arg); +static nxt_tls_bundle_conf_t * +nxt_openssl_find_ctx(nxt_tls_conf_t *conf, nxt_str_t *sn); +static void +nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf); +static void +nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c); +static void +nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data); +static ssize_t +nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); +static ssize_t +nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb); +static ssize_t +nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, + size_t size); +static void +nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret, + nxt_err_t sys_err, nxt_openssl_io_t io); +static void +nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, void *data); +static void nxt_cdecl +nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...); +static nxt_uint_t +nxt_openssl_log_error_level(nxt_err_t err); + + +const nxt_tls_lib_t nxt_openssl_lib = { .library_init = nxt_openssl_library_init, .library_free = nxt_openssl_library_free, @@ -114,25 +129,23 @@ const nxt_tls_lib_t nxt_openssl_lib = { }; -static nxt_conn_io_t nxt_openssl_conn_io = { - .read = nxt_conn_io_read, +static nxt_conn_io_t nxt_openssl_conn_io = { + .read = nxt_conn_io_read, .recvbuf = nxt_openssl_conn_io_recvbuf, - .write = nxt_conn_io_write, + .write = nxt_conn_io_write, .sendbuf = nxt_openssl_conn_io_sendbuf, .shutdown = nxt_openssl_conn_io_shutdown, }; -static long nxt_openssl_version; -static int nxt_openssl_connection_index; - +static long nxt_openssl_version; +static int nxt_openssl_connection_index; static nxt_int_t -nxt_openssl_library_init(nxt_task_t *task) -{ - int index; +nxt_openssl_library_init(nxt_task_t *task) { + int index; if (nxt_fast_path(nxt_openssl_version != 0)) { return NXT_OK; @@ -144,7 +157,7 @@ nxt_openssl_library_init(nxt_task_t *task) #else { - nxt_int_t ret; + nxt_int_t ret; SSL_load_error_strings(); @@ -168,8 +181,8 @@ nxt_openssl_library_init(nxt_task_t *task) nxt_openssl_version = SSLeay(); - nxt_log(task, NXT_LOG_INFO, "%s, %xl", - SSLeay_version(SSLEAY_VERSION), nxt_openssl_version); + nxt_log(task, NXT_LOG_INFO, "%s, %xl", SSLeay_version(SSLEAY_VERSION), + nxt_openssl_version); #ifndef SSL_OP_NO_COMPRESSION { @@ -177,8 +190,8 @@ nxt_openssl_library_init(nxt_task_t *task) * Disable gzip compression in OpenSSL prior to 1.0.0 * version, this saves about 522K per connection. */ - int n; - STACK_OF(SSL_COMP) *ssl_comp_methods; + int n; + STACK_OF(SSL_COMP) * ssl_comp_methods; ssl_comp_methods = SSL_COMP_get_compression_methods(); @@ -192,7 +205,7 @@ nxt_openssl_library_init(nxt_task_t *task) if (index == -1) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_get_ex_new_index() failed"); + "SSL_get_ex_new_index() failed"); return NXT_ERROR; } @@ -205,19 +218,17 @@ nxt_openssl_library_init(nxt_task_t *task) #if OPENSSL_VERSION_NUMBER >= 0x10100003L static void -nxt_openssl_library_free(nxt_task_t *task) -{ +nxt_openssl_library_free(nxt_task_t *task) { } #else -static nxt_thread_mutex_t *nxt_openssl_locks; +static nxt_thread_mutex_t *nxt_openssl_locks; static nxt_int_t -nxt_openssl_locks_init(void) -{ - int i, n; - nxt_int_t ret; +nxt_openssl_locks_init(void) { + int i, n; + nxt_int_t ret; n = CRYPTO_num_locks(); @@ -240,11 +251,9 @@ nxt_openssl_locks_init(void) return NXT_OK; } - static void -nxt_openssl_lock(int mode, int type, const char *file, int line) -{ - nxt_thread_mutex_t *lock; +nxt_openssl_lock(int mode, int type, const char *file, int line) { + nxt_thread_mutex_t *lock; lock = &nxt_openssl_locks[type]; @@ -256,25 +265,19 @@ nxt_openssl_lock(int mode, int type, const char *file, int line) } } - static u_long -nxt_openssl_thread_id(void) -{ +nxt_openssl_thread_id(void) { return (u_long) nxt_thread_handle(); } - static void -nxt_openssl_library_free(nxt_task_t *task) -{ +nxt_openssl_library_free(nxt_task_t *task) { nxt_openssl_locks_free(); } - static void -nxt_openssl_locks_free(void) -{ - int i, n; +nxt_openssl_locks_free(void) { + int i, n; n = CRYPTO_num_locks(); @@ -292,13 +295,12 @@ nxt_openssl_locks_free(void) static nxt_int_t nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, - nxt_tls_init_t *tls_init, nxt_bool_t last) -{ - SSL_CTX *ctx; - const char *ca_certificate; - nxt_tls_conf_t *conf; - STACK_OF(X509_NAME) *list; - nxt_tls_bundle_conf_t *bundle; + nxt_tls_init_t *tls_init, nxt_bool_t last) { + SSL_CTX *ctx; + const char *ca_certificate; + nxt_tls_conf_t *conf; + STACK_OF(X509_NAME) * list; + nxt_tls_bundle_conf_t *bundle; ctx = SSL_CTX_new(SSLv23_server_method()); if (ctx == NULL) { @@ -345,35 +347,33 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, #endif if (nxt_openssl_chain_file(task, ctx, conf, mp, - last && bundle->next == NULL) - != NXT_OK) - { + last && bundle->next == NULL) + != NXT_OK) { goto fail; } -/* - key = conf->certificate_key; + /* + key = conf->certificate_key; - if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) == 0) { - nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_CTX_use_PrivateKey_file(\"%s\") failed", - key); - goto fail; - } -*/ + if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) == 0) { + nxt_openssl_log_error(task, NXT_LOG_ALERT, + "SSL_CTX_use_PrivateKey_file(\"%s\") failed", + key); + goto fail; + } + */ - if (conf->ciphers) { /* else use system crypto policy */ + if (conf->ciphers) { /* else use system crypto policy */ if (SSL_CTX_set_cipher_list(ctx, conf->ciphers) == 0) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_CTX_set_cipher_list(\"%s\") failed", - conf->ciphers); + "SSL_CTX_set_cipher_list(\"%s\") failed", conf->ciphers); goto fail; } } #if (NXT_HAVE_OPENSSL_CONF_CMD) if (tls_init->conf_cmds != NULL - && nxt_ssl_conf_commands(task, ctx, tls_init->conf_cmds, mp) != NXT_OK) - { + && nxt_ssl_conf_commands(task, ctx, tls_init->conf_cmds, mp) + != NXT_OK) { goto fail; } #endif @@ -389,7 +389,6 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); if (conf->ca_certificate != NULL) { - /* TODO: verify callback */ SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); @@ -400,8 +399,7 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, if (SSL_CTX_load_verify_locations(ctx, ca_certificate, NULL) == 0) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_CTX_load_verify_locations(\"%s\") failed", - ca_certificate); + "SSL_CTX_load_verify_locations(\"%s\") failed", ca_certificate); goto fail; } @@ -409,8 +407,7 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, if (list == NULL) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_load_client_CA_file(\"%s\") failed", - ca_certificate); + "SSL_load_client_CA_file(\"%s\") failed", ca_certificate); goto fail; } @@ -437,7 +434,7 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, SSL_CTX_free(ctx); -#if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \ +#if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \ && OPENSSL_VERSION_NUMBER < 0x1010101fL) RAND_keep_random_devices_open(0); #endif @@ -445,19 +442,17 @@ nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp, return NXT_ERROR; } - static nxt_int_t nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf, - nxt_mp_t *mp, nxt_bool_t single) -{ - BIO *bio; - X509 *cert, *ca; + nxt_mp_t *mp, nxt_bool_t single) { + BIO *bio; + X509 *cert, *ca; long reason; - EVP_PKEY *key; + EVP_PKEY *key; nxt_int_t ret; - nxt_tls_bundle_conf_t *bundle; + nxt_tls_bundle_conf_t *bundle; - ret = NXT_ERROR; + ret = NXT_ERROR; cert = NULL; bio = BIO_new(BIO_s_fd()); @@ -482,7 +477,7 @@ nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf, goto clean; } - for ( ;; ) { + for (;;) { ca = PEM_read_bio_X509(bio, NULL, NULL, NULL); if (ca == NULL) { @@ -529,7 +524,7 @@ nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf, if (ret != NXT_OK) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "nxt_openssl_chain_file() failed"); + "nxt_openssl_chain_file() failed"); } clean: @@ -545,32 +540,29 @@ nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf, static nxt_int_t nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf, - nxt_mp_t *mp) -{ + nxt_mp_t *mp) { int ret; - char *zcmd, *zvalue; + char *zcmd, *zvalue; uint32_t index; nxt_str_t cmd, value; - SSL_CONF_CTX *cctx; - nxt_conf_value_t *member; + SSL_CONF_CTX *cctx; + nxt_conf_value_t *member; cctx = SSL_CONF_CTX_new(); if (nxt_slow_path(cctx == NULL)) { - nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_CONF_CTX_new() failed"); + nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_CONF_CTX_new() failed"); return NXT_ERROR; } - SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE - | SSL_CONF_FLAG_SERVER - | SSL_CONF_FLAG_CERTIFICATE - | SSL_CONF_FLAG_SHOW_ERRORS); + SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE | SSL_CONF_FLAG_SERVER + | SSL_CONF_FLAG_CERTIFICATE + | SSL_CONF_FLAG_SHOW_ERRORS); SSL_CONF_CTX_set_ssl_ctx(cctx, ctx); index = 0; - for ( ;; ) { + for (;;) { member = nxt_conf_next_object_member(conf, &cmd, &index); if (nxt_slow_path(member == NULL)) { break; @@ -578,7 +570,7 @@ nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf, nxt_conf_get_string(member, &value); - zcmd = nxt_str_cstrz(mp, &cmd); + zcmd = nxt_str_cstrz(mp, &cmd); zvalue = nxt_str_cstrz(mp, &value); if (nxt_slow_path(zcmd == NULL || zvalue == NULL)) { @@ -588,16 +580,17 @@ nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf, ret = SSL_CONF_cmd(cctx, zcmd, zvalue); if (ret == -2) { nxt_openssl_log_error(task, NXT_LOG_ERR, - "unknown command \"%s\" in " - "\"conf_commands\" option", zcmd); + "unknown command \"%s\" in " + "\"conf_commands\" option", + zcmd); goto fail; } if (ret <= 0) { nxt_openssl_log_error(task, NXT_LOG_ERR, - "invalid value \"%s\" for command \"%s\" " - "in \"conf_commands\" option", - zvalue, zcmd); + "invalid value \"%s\" for command \"%s\" " + "in \"conf_commands\" option", + zvalue, zcmd); goto fail; } @@ -605,8 +598,7 @@ nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf, } if (SSL_CONF_CTX_finish(cctx) != 1) { - nxt_openssl_log_error(task, NXT_LOG_ALERT, - "SSL_CONF_finish() failed"); + nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_CONF_finish() failed"); goto fail; } @@ -627,15 +619,14 @@ nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf, static nxt_int_t nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init, - nxt_mp_t *mp) -{ + nxt_mp_t *mp) { size_t len; uint32_t i; nxt_str_t value; nxt_uint_t count; - nxt_conf_value_t *member, *tickets_conf; - nxt_tls_ticket_t *ticket; - nxt_tls_tickets_t *tickets; + nxt_conf_value_t *member, *tickets_conf; + nxt_tls_ticket_t *ticket; + nxt_tls_tickets_t *tickets; u_char buf[80]; tickets_conf = tls_init->tickets_conf; @@ -660,15 +651,15 @@ nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init, #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB - tickets = nxt_mp_get(mp, sizeof(nxt_tls_tickets_t) - + count * sizeof(nxt_tls_ticket_t)); + tickets = nxt_mp_get(mp, + sizeof(nxt_tls_tickets_t) + count * sizeof(nxt_tls_ticket_t)); if (nxt_slow_path(tickets == NULL)) { return NXT_ERROR; } - tickets->count = count; + tickets->count = count; tls_init->conf->tickets = tickets; - i = 0; + i = 0; do { ticket = &tickets->tickets[i]; @@ -700,13 +691,12 @@ nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init, } while (i < count); if (SSL_CTX_set_tlsext_ticket_key_cb(ctx, nxt_tls_ticket_key_callback) - == 0) - { + == 0) { nxt_openssl_log_error(task, NXT_LOG_ALERT, - "Unit was built with Session Tickets support, however, " - "now it is linked dynamically to an OpenSSL library " - "which has no tlsext support, therefore Session Tickets " - "are not available"); + "Unit was built with Session Tickets support, however, " + "now it is linked dynamically to an OpenSSL library " + "which has no tlsext support, therefore Session Tickets " + "are not available"); return NXT_ERROR; } @@ -733,14 +723,13 @@ nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init, static int nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, - EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) -{ + EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) { nxt_uint_t i; - nxt_conn_t *c; - const EVP_MD *digest; - const EVP_CIPHER *cipher; - nxt_tls_ticket_t *ticket; - nxt_openssl_conn_t *tls; + nxt_conn_t *c; + const EVP_MD *digest; + const EVP_CIPHER *cipher; + nxt_tls_ticket_t *ticket; + nxt_openssl_conn_t *tls; c = SSL_get_ex_data(s, nxt_openssl_connection_index); @@ -749,7 +738,7 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, return -1; } - tls = c->u.tls; + tls = c->u.tls; ticket = tls->conf->tickets->tickets; i = 0; @@ -763,16 +752,16 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) != 1) { nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT, - "RAND_bytes() failed"); + "RAND_bytes() failed"); return -1; } nxt_memcpy(name, ticket[0].name, 16); - if (EVP_EncryptInit_ex(ectx, cipher, NULL, ticket[0].aes_key, iv) != 1) - { + if (EVP_EncryptInit_ex(ectx, cipher, NULL, ticket[0].aes_key, iv) + != 1) { nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT, - "EVP_EncryptInit_ex() failed"); + "EVP_EncryptInit_ex() failed"); return -1; } @@ -793,16 +782,16 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, found: nxt_debug(c->socket.task, - "TLS session ticket decrypt, key number: \"%d\"", i); + "TLS session ticket decrypt, key number: \"%d\"", i); enc = (i == 0) ? 1 : 2 /* renew */; cipher = (ticket[i].size == 16) ? EVP_aes_128_cbc() : EVP_aes_256_cbc(); - if (EVP_DecryptInit_ex(ectx, cipher, NULL, ticket[i].aes_key, iv) != 1) - { + if (EVP_DecryptInit_ex(ectx, cipher, NULL, ticket[i].aes_key, iv) + != 1) { nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT, - "EVP_DecryptInit_ex() failed"); + "EVP_DecryptInit_ex() failed"); return -1; } } @@ -814,10 +803,9 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, #endif if (HMAC_Init_ex(hctx, ticket[i].hmac_key, ticket[i].size, digest, NULL) - != 1) - { + != 1) { nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT, - "HMAC_Init_ex() failed"); + "HMAC_Init_ex() failed"); return -1; } @@ -830,8 +818,7 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv, static void -nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, time_t timeout) -{ +nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, time_t timeout) { if (cache_size == 0) { SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); return; @@ -844,19 +831,17 @@ nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, time_t timeout) SSL_CTX_set_timeout(ctx, (long) timeout); } - static nxt_uint_t nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, - nxt_mp_t *mp) -{ - int len; - nxt_str_t domain, str; - X509_NAME *x509_name; - nxt_uint_t i, n; - GENERAL_NAME *name; - nxt_tls_bundle_conf_t *bundle; - STACK_OF(GENERAL_NAME) *alt_names; - nxt_tls_bundle_hash_item_t *item; + nxt_mp_t *mp) { + int len; + nxt_str_t domain, str; + X509_NAME *x509_name; + nxt_uint_t i, n; + GENERAL_NAME *name; + nxt_tls_bundle_conf_t *bundle; + STACK_OF(GENERAL_NAME) * alt_names; + nxt_tls_bundle_hash_item_t *item; bundle = conf->bundle; @@ -892,13 +877,12 @@ nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, goto fail; } - item->name = domain; + item->name = domain; item->bundle = bundle; - if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash, - item, mp) - == NXT_ERROR) - { + if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash, item, + mp) + == NXT_ERROR) { goto fail; } } @@ -907,11 +891,12 @@ nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, } else { x509_name = X509_get_subject_name(cert); - len = X509_NAME_get_text_by_NID(x509_name, NID_commonName, - NULL, 0); + len = X509_NAME_get_text_by_NID(x509_name, NID_commonName, NULL, 0); if (len <= 0) { - nxt_log(task, NXT_LOG_WARN, "certificate \"%V\" has neither " - "Subject Alternative Name nor Common Name", &bundle->name); + nxt_log(task, NXT_LOG_WARN, + "certificate \"%V\" has neither " + "Subject Alternative Name nor Common Name", + &bundle->name); return NXT_OK; } @@ -921,8 +906,7 @@ nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, } domain.length = X509_NAME_get_text_by_NID(x509_name, NID_commonName, - (char *) domain.start, - len + 1); + (char *) domain.start, len + 1); nxt_memcpy_lowcase(domain.start, domain.start, domain.length); item = nxt_mp_get(mp, sizeof(nxt_tls_bundle_hash_item_t)); @@ -930,13 +914,11 @@ nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, return NXT_ERROR; } - item->name = domain; + item->name = domain; item->bundle = bundle; - if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash, item, - mp) - == NXT_ERROR) - { + if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash, item, mp) + == NXT_ERROR) { return NXT_ERROR; } } @@ -950,36 +932,30 @@ nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf, return NXT_ERROR; } - -static const nxt_lvlhsh_proto_t nxt_openssl_bundle_hash_proto - nxt_aligned(64) = -{ - NXT_LVLHSH_DEFAULT, - nxt_openssl_bundle_hash_test, - nxt_mp_lvlhsh_alloc, - nxt_mp_lvlhsh_free, -}; - +static const nxt_lvlhsh_proto_t nxt_openssl_bundle_hash_proto nxt_aligned(64) + = { + NXT_LVLHSH_DEFAULT, + nxt_openssl_bundle_hash_test, + nxt_mp_lvlhsh_alloc, + nxt_mp_lvlhsh_free, + }; static nxt_int_t -nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_tls_bundle_hash_item_t *item; +nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_tls_bundle_hash_item_t *item; item = data; return nxt_strstr_eq(&lhq->key, &item->name) ? NXT_OK : NXT_DECLINED; } - static nxt_int_t nxt_openssl_bundle_hash_insert(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, - nxt_tls_bundle_hash_item_t *item, nxt_mp_t *mp) -{ + nxt_tls_bundle_hash_item_t *item, nxt_mp_t *mp) { nxt_str_t str; nxt_int_t ret; nxt_lvlhsh_query_t lhq; - nxt_tls_bundle_hash_item_t *old; + nxt_tls_bundle_hash_item_t *old; str = item->name; @@ -988,33 +964,36 @@ nxt_openssl_bundle_hash_insert(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, item->name.length--; if (item->name.length == 0 || item->name.start[0] != '.') { - nxt_log(task, NXT_LOG_WARN, "ignored invalid name \"%V\" " - "in certificate \"%V\": missing \".\" " - "after wildcard symbol", &str, &item->bundle->name); + nxt_log(task, NXT_LOG_WARN, + "ignored invalid name \"%V\" " + "in certificate \"%V\": missing \".\" " + "after wildcard symbol", + &str, &item->bundle->name); return NXT_OK; } } - lhq.pool = mp; - lhq.key = item->name; - lhq.value = item; - lhq.proto = &nxt_openssl_bundle_hash_proto; - lhq.replace = 0; + lhq.pool = mp; + lhq.key = item->name; + lhq.value = item; + lhq.proto = &nxt_openssl_bundle_hash_proto; + lhq.replace = 0; lhq.key_hash = nxt_murmur_hash2(item->name.start, item->name.length); ret = nxt_lvlhsh_insert(lvlhsh, &lhq); if (nxt_fast_path(ret == NXT_OK)) { - nxt_debug(task, "name \"%V\" for certificate \"%V\" is inserted", - &str, &item->bundle->name); + nxt_debug(task, "name \"%V\" for certificate \"%V\" is inserted", &str, + &item->bundle->name); return NXT_OK; } if (nxt_fast_path(ret == NXT_DECLINED)) { old = lhq.value; if (old->bundle != item->bundle) { - nxt_log(task, NXT_LOG_WARN, "ignored duplicate name \"%V\" " - "in certificate \"%V\", identical name appears in \"%V\"", - &str, &old->bundle->name, &item->bundle->name); + nxt_log(task, NXT_LOG_WARN, + "ignored duplicate name \"%V\" " + "in certificate \"%V\", identical name appears in \"%V\"", + &str, &old->bundle->name, &item->bundle->name); old->bundle = item->bundle; } @@ -1025,17 +1004,15 @@ nxt_openssl_bundle_hash_insert(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh, return NXT_ERROR; } - static nxt_int_t -nxt_openssl_servername(SSL *s, int *ad, void *arg) -{ +nxt_openssl_servername(SSL *s, int *ad, void *arg) { nxt_str_t str; nxt_uint_t i; - nxt_conn_t *c; - const char *servername; - nxt_tls_conf_t *conf; - nxt_openssl_conn_t *tls; - nxt_tls_bundle_conf_t *bundle; + nxt_conn_t *c; + const char *servername; + nxt_tls_conf_t *conf; + nxt_openssl_conn_t *tls; + nxt_tls_bundle_conf_t *bundle; c = SSL_get_ex_data(s, nxt_openssl_connection_index); @@ -1058,8 +1035,10 @@ nxt_openssl_servername(SSL *s, int *ad, void *arg) } if (servername[0] == '.') { - nxt_debug(c->socket.task, "ignored the server name \"%s\": " - "leading \".\"", servername); + nxt_debug(c->socket.task, + "ignored the server name \"%s\": " + "leading \".\"", + servername); goto done; } @@ -1072,7 +1051,7 @@ nxt_openssl_servername(SSL *s, int *ad, void *arg) nxt_memcpy_lowcase(str.start, (const u_char *) servername, str.length); - tls = c->u.tls; + tls = c->u.tls; conf = tls->conf; bundle = nxt_openssl_find_ctx(conf, &str); @@ -1080,7 +1059,7 @@ nxt_openssl_servername(SSL *s, int *ad, void *arg) if (bundle == NULL) { for (i = 1; i < str.length; i++) { if (str.start[i] == '.') { - str.start += i; + str.start += i; str.length -= i; bundle = nxt_openssl_find_ctx(conf, &str); @@ -1090,14 +1069,15 @@ nxt_openssl_servername(SSL *s, int *ad, void *arg) } if (bundle != NULL) { - nxt_debug(c->socket.task, "new tls context found for \"%V\": \"%V\" " - "(old: \"%V\")", &str, &bundle->name, - &conf->bundle->name); + nxt_debug(c->socket.task, + "new tls context found for \"%V\": \"%V\" " + "(old: \"%V\")", + &str, &bundle->name, &conf->bundle->name); if (bundle != conf->bundle) { if (SSL_set_SSL_CTX(s, bundle->ctx) == NULL) { nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT, - "SSL_set_SSL_CTX() failed"); + "SSL_set_SSL_CTX() failed"); return SSL_TLSEXT_ERR_ALERT_FATAL; } @@ -1109,17 +1089,15 @@ nxt_openssl_servername(SSL *s, int *ad, void *arg) return SSL_TLSEXT_ERR_OK; } - static nxt_tls_bundle_conf_t * -nxt_openssl_find_ctx(nxt_tls_conf_t *conf, nxt_str_t *sn) -{ +nxt_openssl_find_ctx(nxt_tls_conf_t *conf, nxt_str_t *sn) { nxt_int_t ret; nxt_lvlhsh_query_t lhq; - nxt_tls_bundle_hash_item_t *item; + nxt_tls_bundle_hash_item_t *item; lhq.key_hash = nxt_murmur_hash2(sn->start, sn->length); - lhq.key = *sn; - lhq.proto = &nxt_openssl_bundle_hash_proto; + lhq.key = *sn; + lhq.proto = &nxt_openssl_bundle_hash_proto; ret = nxt_lvlhsh_find(&conf->bundle_hash, &lhq); if (ret != NXT_OK) { @@ -1131,11 +1109,9 @@ nxt_openssl_find_ctx(nxt_tls_conf_t *conf, nxt_str_t *sn) return item->bundle; } - static void -nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf) -{ - nxt_tls_bundle_conf_t *bundle; +nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf) { + nxt_tls_bundle_conf_t *bundle; bundle = conf->bundle; nxt_assert(bundle != NULL); @@ -1147,23 +1123,21 @@ nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf) if (conf->tickets) { nxt_memzero(conf->tickets->tickets, - conf->tickets->count * sizeof(nxt_tls_ticket_t)); + conf->tickets->count * sizeof(nxt_tls_ticket_t)); } -#if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \ +#if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \ && OPENSSL_VERSION_NUMBER < 0x1010101fL) RAND_keep_random_devices_open(0); #endif } - static void -nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) -{ +nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) { int ret; - SSL *s; - SSL_CTX *ctx; - nxt_openssl_conn_t *tls; + SSL *s; + SSL_CTX *ctx; + nxt_openssl_conn_t *tls; nxt_log_debug(c->socket.log, "openssl conn init"); @@ -1185,14 +1159,14 @@ nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) tls->session = s; /* To pass TLS config to the nxt_openssl_servername() callback. */ - tls->conf = conf; - tls->conn = c; + tls->conf = conf; + tls->conn = c; ret = SSL_set_fd(s, c->socket.fd); if (ret == 0) { nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_set_fd(%d) failed", - c->socket.fd); + c->socket.fd); goto fail; } @@ -1203,7 +1177,7 @@ nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) goto fail; } - c->io = &nxt_openssl_conn_io; + c->io = &nxt_openssl_conn_io; c->sendfile = NXT_CONN_SENDFILE_OFF; nxt_openssl_conn_handshake(task, c, c->socket.data); @@ -1212,15 +1186,13 @@ nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) fail: - nxt_work_queue_add(c->read_work_queue, c->read_state->error_handler, - task, c, c->socket.data); + nxt_work_queue_add(c->read_work_queue, c->read_state->error_handler, task, + c, c->socket.data); } - nxt_inline void -nxt_openssl_conn_free(nxt_task_t *task, nxt_conn_t *c) -{ - nxt_openssl_conn_t *tls; +nxt_openssl_conn_free(nxt_task_t *task, nxt_conn_t *c) { + nxt_openssl_conn_t *tls; nxt_debug(task, "openssl conn free"); @@ -1233,18 +1205,16 @@ nxt_openssl_conn_free(nxt_task_t *task, nxt_conn_t *c) } } - static void -nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) -{ +nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) { int ret; nxt_int_t n; nxt_err_t err; - nxt_conn_t *c; - nxt_work_queue_t *wq; + nxt_conn_t *c; + nxt_work_queue_t *wq; nxt_work_handler_t handler; - nxt_openssl_conn_t *tls; - const nxt_conn_state_t *state; + nxt_openssl_conn_t *tls; + const nxt_conn_state_t *state; c = obj; @@ -1294,13 +1264,12 @@ nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) handler = state->ready_handler; } else { - c->socket.read_handler = nxt_openssl_conn_handshake; + c->socket.read_handler = nxt_openssl_conn_handshake; c->socket.write_handler = nxt_openssl_conn_handshake; n = nxt_openssl_conn_test_error(task, c, ret, err, - NXT_OPENSSL_HANDSHAKE); + NXT_OPENSSL_HANDSHAKE); switch (n) { - case NXT_AGAIN: if (tls->ssl_error == SSL_ERROR_WANT_READ && tls->times < 2) { tls->times++; @@ -1315,7 +1284,7 @@ nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) default: case NXT_ERROR: nxt_openssl_conn_error(task, err, "SSL_do_handshake(%d) failed", - c->socket.fd); + c->socket.fd); handler = state->error_handler; break; @@ -1327,47 +1296,42 @@ nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(wq, handler, task, c, data); } - static ssize_t -nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) -{ +nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) { int ret; size_t size; nxt_int_t n; nxt_err_t err; - nxt_openssl_conn_t *tls; + nxt_openssl_conn_t *tls; - tls = c->u.tls; + tls = c->u.tls; size = b->mem.end - b->mem.free; ret = SSL_read(tls->session, b->mem.free, size); err = (ret <= 0) ? nxt_socket_errno : 0; - nxt_debug(c->socket.task, "SSL_read(%d, %p, %uz): %d err:%d", - c->socket.fd, b->mem.free, size, ret, err); + nxt_debug(c->socket.task, "SSL_read(%d, %p, %uz): %d err:%d", c->socket.fd, + b->mem.free, size, ret, err); if (ret > 0) { return ret; } n = nxt_openssl_conn_test_error(c->socket.task, c, ret, err, - NXT_OPENSSL_READ); + NXT_OPENSSL_READ); if (n == NXT_ERROR) { nxt_openssl_conn_error(c->socket.task, err, - "SSL_read(%d, %p, %uz) failed", - c->socket.fd, b->mem.free, size); + "SSL_read(%d, %p, %uz) failed", c->socket.fd, b->mem.free, size); } return n; } - static ssize_t -nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) -{ - nxt_uint_t niov; - struct iovec iov; +nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) { + nxt_uint_t niov; + struct iovec iov; niov = nxt_sendbuf_mem_coalesce0(task, sb, &iov, 1); @@ -1378,16 +1342,14 @@ nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) return nxt_openssl_conn_io_send(task, sb, iov.iov_base, iov.iov_len); } - static ssize_t nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, - size_t size) -{ + size_t size) { int ret; nxt_err_t err; nxt_int_t n; - nxt_conn_t *c; - nxt_openssl_conn_t *tls; + nxt_conn_t *c; + nxt_openssl_conn_t *tls; tls = sb->tls; @@ -1395,14 +1357,14 @@ nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, err = (ret <= 0) ? nxt_socket_errno : 0; - nxt_debug(task, "SSL_write(%d, %p, %uz): %d err:%d", - sb->socket, buf, size, ret, err); + nxt_debug(task, "SSL_write(%d, %p, %uz): %d err:%d", sb->socket, buf, size, + ret, err); if (ret > 0) { return ret; } - c = tls->conn; + c = tls->conn; c->socket.write_ready = sb->ready; n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_WRITE); @@ -1412,23 +1374,21 @@ nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, if (n == NXT_ERROR) { sb->error = c->socket.error; nxt_openssl_conn_error(task, err, "SSL_write(%d, %p, %uz) failed", - sb->socket, buf, size); + sb->socket, buf, size); } return n; } - static void -nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) -{ +nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) { int ret, mode; - SSL *s; + SSL *s; nxt_err_t err; nxt_int_t n; nxt_bool_t quiet, once; - nxt_conn_t *c; - nxt_openssl_conn_t *tls; + nxt_conn_t *c; + nxt_openssl_conn_t *tls; nxt_work_handler_t handler; c = obj; @@ -1436,7 +1396,7 @@ nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) nxt_debug(task, "openssl conn shutdown fd:%d", c->socket.fd); c->read_state = NULL; - tls = c->u.tls; + tls = c->u.tls; if (tls == NULL) { return; @@ -1469,15 +1429,15 @@ nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) once = 1; - for ( ;; ) { + for (;;) { SSL_set_shutdown(s, mode); ret = SSL_shutdown(s); err = (ret <= 0) ? nxt_socket_errno : 0; - nxt_debug(task, "SSL_shutdown(%d, %d, %b): %d err:%d", - c->socket.fd, mode, quiet, ret, err); + nxt_debug(task, "SSL_shutdown(%d, %d, %b): %d err:%d", c->socket.fd, + mode, quiet, ret, err); if (ret > 0) { /* ret == 1, the shutdown was successfully completed. */ @@ -1508,14 +1468,13 @@ nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) break; } - c->socket.read_handler = nxt_openssl_conn_io_shutdown; + c->socket.read_handler = nxt_openssl_conn_io_shutdown; c->socket.write_handler = nxt_openssl_conn_io_shutdown; c->socket.error_handler = c->write_state->error_handler; n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_SHUTDOWN); switch (n) { - case 0: handler = c->write_state->close_handler; break; @@ -1528,7 +1487,7 @@ nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) default: case NXT_ERROR: nxt_openssl_conn_error(task, err, "SSL_shutdown(%d) failed", - c->socket.fd); + c->socket.fd); handler = c->write_state->error_handler; } @@ -1539,13 +1498,11 @@ nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) nxt_work_queue_add(c->write_work_queue, handler, task, c, data); } - static nxt_int_t nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret, - nxt_err_t sys_err, nxt_openssl_io_t io) -{ + nxt_err_t sys_err, nxt_openssl_io_t io) { u_long lib_err; - nxt_openssl_conn_t *tls; + nxt_openssl_conn_t *tls; tls = c->u.tls; @@ -1554,7 +1511,6 @@ nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret, nxt_debug(task, "SSL_get_error(): %d", tls->ssl_error); switch (tls->ssl_error) { - case SSL_ERROR_WANT_READ: c->socket.read_ready = 0; @@ -1599,18 +1555,16 @@ nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret, /* A "close notify" alert. */ return 0; - default: /* SSL_ERROR_SSL, etc. */ - c->socket.error = 1000; /* Nonexistent errno code. */ + default: /* SSL_ERROR_SSL, etc. */ + c->socket.error = 1000; /* Nonexistent errno code. */ return NXT_ERROR; } } - static void -nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_conn_t *c; - nxt_timer_t *timer; +nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_conn_t *c; + nxt_timer_t *timer; timer = obj; @@ -1622,19 +1576,16 @@ nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, void *data) nxt_openssl_conn_io_shutdown(task, c, NULL); } - static void nxt_cdecl -nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...) -{ - u_char *p, *end; - va_list args; - nxt_uint_t level; - u_char msg[NXT_MAX_ERROR_STR]; +nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...) { + u_char *p, *end; + va_list args; + nxt_uint_t level; + u_char msg[NXT_MAX_ERROR_STR]; level = nxt_openssl_log_error_level(err); if (nxt_log_level_enough(task->log, level)) { - end = msg + sizeof(msg); va_start(args, fmt); @@ -1654,74 +1605,71 @@ nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...) } } - static nxt_uint_t -nxt_openssl_log_error_level(nxt_err_t err) -{ +nxt_openssl_log_error_level(nxt_err_t err) { switch (ERR_GET_REASON(ERR_peek_error())) { - case 0: return nxt_socket_error_level(err); - case SSL_R_BAD_CHANGE_CIPHER_SPEC: /* 103 */ - case SSL_R_BLOCK_CIPHER_PAD_IS_WRONG: /* 129 */ - case SSL_R_DIGEST_CHECK_FAILED: /* 149 */ - case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: /* 151 */ - case SSL_R_EXCESSIVE_MESSAGE_SIZE: /* 152 */ - case SSL_R_LENGTH_MISMATCH: /* 159 */ + case SSL_R_BAD_CHANGE_CIPHER_SPEC: /* 103 */ + case SSL_R_BLOCK_CIPHER_PAD_IS_WRONG: /* 129 */ + case SSL_R_DIGEST_CHECK_FAILED: /* 149 */ + case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: /* 151 */ + case SSL_R_EXCESSIVE_MESSAGE_SIZE: /* 152 */ + case SSL_R_LENGTH_MISMATCH: /* 159 */ #ifdef SSL_R_NO_CIPHERS_PASSED - case SSL_R_NO_CIPHERS_PASSED: /* 182 */ + case SSL_R_NO_CIPHERS_PASSED: /* 182 */ #endif - case SSL_R_NO_CIPHERS_SPECIFIED: /* 183 */ - case SSL_R_NO_COMPRESSION_SPECIFIED: /* 187 */ - case SSL_R_NO_SHARED_CIPHER: /* 193 */ - case SSL_R_RECORD_LENGTH_MISMATCH: /* 213 */ + case SSL_R_NO_CIPHERS_SPECIFIED: /* 183 */ + case SSL_R_NO_COMPRESSION_SPECIFIED: /* 187 */ + case SSL_R_NO_SHARED_CIPHER: /* 193 */ + case SSL_R_RECORD_LENGTH_MISMATCH: /* 213 */ #ifdef SSL_R_PARSE_TLSEXT - case SSL_R_PARSE_TLSEXT: /* 227 */ + case SSL_R_PARSE_TLSEXT: /* 227 */ #endif - case SSL_R_UNEXPECTED_MESSAGE: /* 244 */ - case SSL_R_UNEXPECTED_RECORD: /* 245 */ - case SSL_R_UNKNOWN_ALERT_TYPE: /* 246 */ - case SSL_R_UNKNOWN_PROTOCOL: /* 252 */ - case SSL_R_WRONG_VERSION_NUMBER: /* 267 */ - case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: /* 281 */ + case SSL_R_UNEXPECTED_MESSAGE: /* 244 */ + case SSL_R_UNEXPECTED_RECORD: /* 245 */ + case SSL_R_UNKNOWN_ALERT_TYPE: /* 246 */ + case SSL_R_UNKNOWN_PROTOCOL: /* 252 */ + case SSL_R_WRONG_VERSION_NUMBER: /* 267 */ + case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: /* 281 */ #ifdef SSL_R_RENEGOTIATE_EXT_TOO_LONG - case SSL_R_RENEGOTIATE_EXT_TOO_LONG: /* 335 */ - case SSL_R_RENEGOTIATION_ENCODING_ERR: /* 336 */ - case SSL_R_RENEGOTIATION_MISMATCH: /* 337 */ + case SSL_R_RENEGOTIATE_EXT_TOO_LONG: /* 335 */ + case SSL_R_RENEGOTIATION_ENCODING_ERR: /* 336 */ + case SSL_R_RENEGOTIATION_MISMATCH: /* 337 */ #endif #ifdef SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED - case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: /* 338 */ + case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: /* 338 */ #endif #ifdef SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING - case SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING: /* 345 */ + case SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING: /* 345 */ #endif - case 1000:/* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */ - case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: /* 1010 */ - case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: /* 1020 */ - case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: /* 1021 */ - case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: /* 1022 */ - case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: /* 1030 */ - case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: /* 1040 */ - case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: /* 1047 */ + case 1000: /* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */ + case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: /* 1010 */ + case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: /* 1020 */ + case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: /* 1021 */ + case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: /* 1022 */ + case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: /* 1030 */ + case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: /* 1040 */ + case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: /* 1047 */ break; - case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: /* 1041 */ - case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: /* 1042 */ - case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: /* 1043 */ - case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: /* 1044 */ - case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: /* 1045 */ - case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: /* 1046 */ - case SSL_R_TLSV1_ALERT_UNKNOWN_CA: /* 1048 */ - case SSL_R_TLSV1_ALERT_ACCESS_DENIED: /* 1049 */ - case SSL_R_TLSV1_ALERT_DECODE_ERROR: /* 1050 */ - case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: /* 1051 */ - case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: /* 1060 */ - case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: /* 1070 */ - case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: /* 1071 */ - case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: /* 1080 */ - case SSL_R_TLSV1_ALERT_USER_CANCELLED: /* 1090 */ - case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: /* 1100 */ + case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: /* 1041 */ + case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: /* 1042 */ + case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: /* 1043 */ + case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: /* 1044 */ + case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: /* 1045 */ + case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: /* 1046 */ + case SSL_R_TLSV1_ALERT_UNKNOWN_CA: /* 1048 */ + case SSL_R_TLSV1_ALERT_ACCESS_DENIED: /* 1049 */ + case SSL_R_TLSV1_ALERT_DECODE_ERROR: /* 1050 */ + case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: /* 1051 */ + case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: /* 1060 */ + case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: /* 1070 */ + case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: /* 1071 */ + case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: /* 1080 */ + case SSL_R_TLSV1_ALERT_USER_CANCELLED: /* 1090 */ + case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: /* 1100 */ return NXT_LOG_ERR; default: @@ -1731,13 +1679,12 @@ nxt_openssl_log_error_level(nxt_err_t err) return NXT_LOG_INFO; } - void nxt_cdecl -nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, ...) -{ - u_char *p, *end; - va_list args; - u_char msg[NXT_MAX_ERROR_STR]; +nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, + ...) { + u_char *p, *end; + va_list args; + u_char msg[NXT_MAX_ERROR_STR]; end = msg + sizeof(msg); @@ -1750,14 +1697,12 @@ nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, ...) nxt_log(task, level, "%*s", p - msg, msg); } - u_char * -nxt_openssl_copy_error(u_char *p, u_char *end) -{ +nxt_openssl_copy_error(u_char *p, u_char *end) { int flags; u_long err; nxt_bool_t clear; - const char *data, *delimiter; + const char *data, *delimiter; err = ERR_peek_error(); if (err == 0) { @@ -1774,9 +1719,9 @@ nxt_openssl_copy_error(u_char *p, u_char *end) * and drain the error queue. */ delimiter = ""; - clear = 0; + clear = 0; - for ( ;; ) { + for (;;) { #if OPENSSL_VERSION_NUMBER >= 0x30000000L err = ERR_get_error_all(NULL, NULL, NULL, &data, &flags); #else diff --git a/src/nxt_parse.c b/src/nxt_parse.c index e60bab4c4..82842b88b 100644 --- a/src/nxt_parse.c +++ b/src/nxt_parse.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,23 +5,20 @@ #include - /* * nxt_int_parse() returns size_t value >= 0 on success, * -1 on failure, and -2 on overflow. */ nxt_int_t -nxt_int_parse(const u_char *p, size_t length) -{ - u_char c; - nxt_uint_t val; +nxt_int_parse(const u_char *p, size_t length) { + u_char c; + nxt_uint_t val; static const nxt_uint_t cutoff = NXT_INT_T_MAX / 10; static const nxt_uint_t cutlim = NXT_INT_T_MAX % 10; if (nxt_fast_path(length != 0)) { - val = 0; do { @@ -52,23 +48,20 @@ nxt_int_parse(const u_char *p, size_t length) return -1; } - /* * nxt_size_t_parse() returns size_t value >= 0 on success, * -1 on failure, and -2 on overflow. */ ssize_t -nxt_size_t_parse(const u_char *p, size_t length) -{ - u_char c; - size_t val; +nxt_size_t_parse(const u_char *p, size_t length) { + u_char c; + size_t val; static const size_t cutoff = NXT_SIZE_T_MAX / 10; static const size_t cutlim = NXT_SIZE_T_MAX % 10; if (nxt_fast_path(length != 0)) { - val = 0; do { @@ -98,40 +91,36 @@ nxt_size_t_parse(const u_char *p, size_t length) return -1; } - /* * nxt_size_parse() parses size string with optional K or M units and * returns size_t value >= 0 on success, -1 on failure, and -2 on overflow. */ ssize_t -nxt_size_parse(const u_char *p, size_t length) -{ - u_char unit; - ssize_t val, max; - nxt_uint_t shift; +nxt_size_parse(const u_char *p, size_t length) { + u_char unit; + ssize_t val, max; + nxt_uint_t shift; if (nxt_fast_path(length != 0)) { - length--; /* Upper case. */ unit = p[length] & ~0x20; switch (unit) { - case 'G': - max = NXT_SIZE_T_MAX >> 30; + max = NXT_SIZE_T_MAX >> 30; shift = 30; break; case 'M': - max = NXT_SIZE_T_MAX >> 20; + max = NXT_SIZE_T_MAX >> 20; shift = 20; break; case 'K': - max = NXT_SIZE_T_MAX >> 10; + max = NXT_SIZE_T_MAX >> 10; shift = 10; break; @@ -142,7 +131,6 @@ nxt_size_parse(const u_char *p, size_t length) val = nxt_size_t_parse(p, length); if (nxt_fast_path(val >= 0)) { - if (nxt_slow_path(val > max)) { /* An overflow. */ return -2; @@ -157,23 +145,20 @@ nxt_size_parse(const u_char *p, size_t length) return -1; } - /* * nxt_off_t_parse() returns nxt_off_t value >= 0 on success, * -1 on failure, and -2 on overflow. */ nxt_off_t -nxt_off_t_parse(const u_char *p, size_t length) -{ - u_char c; - nxt_uoff_t val; +nxt_off_t_parse(const u_char *p, size_t length) { + u_char c; + nxt_uoff_t val; static const nxt_uoff_t cutoff = NXT_OFF_T_MAX / 10; static const nxt_uoff_t cutlim = NXT_OFF_T_MAX % 10; if (nxt_fast_path(length != 0)) { - val = 0; do { @@ -203,18 +188,16 @@ nxt_off_t_parse(const u_char *p, size_t length) return -1; } - /* * nxt_str_int_parse() returns nxt_int_t value >= 0 on success, * -1 on failure, and -2 on overflow and also updates the 's' argument. */ nxt_int_t -nxt_str_int_parse(nxt_str_t *s) -{ - u_char c, *p; - size_t length; - nxt_uint_t val; +nxt_str_int_parse(nxt_str_t *s) { + u_char c, *p; + size_t length; + nxt_uint_t val; static const nxt_uint_t cutoff = NXT_INT_T_MAX / 10; static const nxt_uint_t cutlim = NXT_INT_T_MAX % 10; @@ -225,7 +208,7 @@ nxt_str_int_parse(nxt_str_t *s) return -1; } - p = s->start; + p = s->start; val = 0; do { @@ -251,29 +234,27 @@ nxt_str_int_parse(nxt_str_t *s) } while (length != 0); s->length = length; - s->start = p; + s->start = p; return val; } - /* * nxt_number_parse() returns a double value >= 0 and updates the start * argument on success, or returns -1 on failure or -2 on overflow. */ double -nxt_number_parse(const u_char **start, const u_char *end) -{ +nxt_number_parse(const u_char **start, const u_char *end) { u_char c; nxt_bool_t overflow; nxt_uint_t integral, frac, power; - const u_char *p; + const u_char *p; static const nxt_uint_t cutoff = NXT_INT_T_MAX / 10; static const nxt_uint_t cutlim = NXT_INT_T_MAX % 10; - p = *start; + p = *start; integral = 0; while (p < end) { @@ -290,8 +271,8 @@ nxt_number_parse(const u_char **start, const u_char *end) break; } - overflow = nxt_expect(0, (integral >= cutoff - && (integral > cutoff || c > cutlim))); + overflow = nxt_expect(0, + (integral >= cutoff && (integral > cutoff || c > cutlim))); if (overflow) { return -2; @@ -317,7 +298,7 @@ nxt_number_parse(const u_char **start, const u_char *end) return -1; } - frac = 0; + frac = 0; power = 1; for (p++; p < end; p++) { @@ -330,15 +311,15 @@ nxt_number_parse(const u_char **start, const u_char *end) break; } - overflow = nxt_expect(0, (frac >= cutoff && (frac > cutoff - || c > cutlim)) - || power > cutoff); + overflow + = nxt_expect(0, (frac >= cutoff && (frac > cutoff || c > cutlim)) + || power > cutoff); if (overflow) { return -2; } - frac = frac * 10 + c; + frac = frac * 10 + c; power *= 10; } diff --git a/src/nxt_parse.h b/src/nxt_parse.h index 0643af1ed..498f96a9a 100644 --- a/src/nxt_parse.h +++ b/src/nxt_parse.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,18 +7,25 @@ #define _NXT_PARSE_H_INCLUDED_ -NXT_EXPORT nxt_int_t nxt_int_parse(const u_char *p, size_t len); -NXT_EXPORT ssize_t nxt_size_t_parse(const u_char *p, size_t len); -NXT_EXPORT ssize_t nxt_size_parse(const u_char *p, size_t len); -NXT_EXPORT nxt_off_t nxt_off_t_parse(const u_char *p, size_t len); +NXT_EXPORT nxt_int_t +nxt_int_parse(const u_char *p, size_t len); +NXT_EXPORT ssize_t +nxt_size_t_parse(const u_char *p, size_t len); +NXT_EXPORT ssize_t +nxt_size_parse(const u_char *p, size_t len); +NXT_EXPORT nxt_off_t +nxt_off_t_parse(const u_char *p, size_t len); -NXT_EXPORT nxt_int_t nxt_str_int_parse(nxt_str_t *s); +NXT_EXPORT nxt_int_t +nxt_str_int_parse(nxt_str_t *s); -NXT_EXPORT double nxt_number_parse(const u_char **start, const u_char *end); +NXT_EXPORT double +nxt_number_parse(const u_char **start, const u_char *end); -NXT_EXPORT nxt_time_t nxt_time_parse(const u_char *p, size_t len); -NXT_EXPORT nxt_int_t nxt_term_parse(const u_char *p, size_t len, - nxt_bool_t seconds); +NXT_EXPORT nxt_time_t +nxt_time_parse(const u_char *p, size_t len); +NXT_EXPORT nxt_int_t +nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds); #endif /* _NXT_PARSE_H_INCLUDED_ */ diff --git a/src/nxt_pcre.c b/src/nxt_pcre.c index 737fc7cf7..244a93218 100644 --- a/src/nxt_pcre.c +++ b/src/nxt_pcre.c @@ -7,32 +7,30 @@ #include #include - struct nxt_regex_s { - pcre *code; - pcre_extra *extra; + pcre *code; + pcre_extra *extra; nxt_str_t pattern; }; struct nxt_regex_match_s { - int ovecsize; - int ovec[]; + int ovecsize; + int ovec[]; }; +static void * +nxt_pcre_malloc(size_t size); +static void +nxt_pcre_free(void *p); -static void *nxt_pcre_malloc(size_t size); -static void nxt_pcre_free(void *p); - -static nxt_mp_t *nxt_pcre_mp; - +static nxt_mp_t *nxt_pcre_mp; nxt_regex_t * -nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) -{ +nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) { int erroffset; - char *pattern; - void *saved_malloc, *saved_free; - nxt_regex_t *re; + char *pattern; + void *saved_malloc, *saved_free; + nxt_regex_t *re; err->offset = source->length; @@ -48,13 +46,13 @@ nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) pattern[source->length] = '\0'; re->pattern.length = source->length; - re->pattern.start = (u_char *) pattern; + re->pattern.start = (u_char *) pattern; saved_malloc = pcre_malloc; - saved_free = pcre_free; + saved_free = pcre_free; pcre_malloc = nxt_pcre_malloc; - pcre_free = nxt_pcre_free; + pcre_free = nxt_pcre_free; nxt_pcre_mp = mp; re->code = pcre_compile(pattern, 0, &err->msg, &erroffset, NULL); @@ -70,22 +68,20 @@ nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) } else { err->offset = erroffset; - re = NULL; + re = NULL; } pcre_malloc = saved_malloc; - pcre_free = saved_free; + pcre_free = saved_free; return re; } - -static void* -nxt_pcre_malloc(size_t size) -{ +static void * +nxt_pcre_malloc(size_t size) { if (nxt_slow_path(nxt_pcre_mp == NULL)) { nxt_thread_log_alert("pcre_malloc(%uz) called without memory pool", - size); + size); return NULL; } @@ -94,17 +90,13 @@ nxt_pcre_malloc(size_t size) return nxt_mp_get(nxt_pcre_mp, size); } - static void -nxt_pcre_free(void *p) -{ +nxt_pcre_free(void *p) { } - nxt_regex_match_t * -nxt_regex_match_create(nxt_mp_t *mp, size_t size) -{ - nxt_regex_match_t *match; +nxt_regex_match_create(nxt_mp_t *mp, size_t size) { + nxt_regex_match_t *match; match = nxt_mp_get(mp, sizeof(nxt_regex_match_t) + sizeof(int) * size); if (nxt_fast_path(match != NULL)) { @@ -114,19 +106,17 @@ nxt_regex_match_create(nxt_mp_t *mp, size_t size) return match; } - nxt_int_t nxt_regex_match(nxt_regex_t *re, u_char *subject, size_t length, - nxt_regex_match_t *match) -{ - int ret; + nxt_regex_match_t *match) { + int ret; ret = pcre_exec(re->code, re->extra, (const char *) subject, length, 0, 0, - match->ovec, match->ovecsize); + match->ovec, match->ovecsize); if (nxt_slow_path(ret < PCRE_ERROR_NOMATCH)) { nxt_thread_log_error(NXT_LOG_ERR, - "pcre_exec() failed: %d on \"%*s\" using \"%V\"", - ret, length, subject, &re->pattern); + "pcre_exec() failed: %d on \"%*s\" using \"%V\"", ret, length, + subject, &re->pattern); return NXT_ERROR; } diff --git a/src/nxt_pcre2.c b/src/nxt_pcre2.c index cb51062c0..0ea7c51cc 100644 --- a/src/nxt_pcre2.c +++ b/src/nxt_pcre2.c @@ -10,30 +10,29 @@ #include -static void *nxt_pcre2_malloc(PCRE2_SIZE size, void *memory_data); -static void nxt_pcre2_free(void *p, void *memory_data); - +static void * +nxt_pcre2_malloc(PCRE2_SIZE size, void *memory_data); +static void +nxt_pcre2_free(void *p, void *memory_data); struct nxt_regex_s { - pcre2_code *code; + pcre2_code *code; nxt_str_t pattern; }; - nxt_regex_t * -nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) -{ +nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) { int errcode; nxt_int_t ret; PCRE2_SIZE erroffset; - nxt_regex_t *re; - pcre2_general_context *general_ctx; - pcre2_compile_context *compile_ctx; + nxt_regex_t *re; + pcre2_general_context *general_ctx; + pcre2_compile_context *compile_ctx; - static const u_char alloc_error[] = "memory allocation failed"; + static const u_char alloc_error[] = "memory allocation failed"; - general_ctx = pcre2_general_context_create(nxt_pcre2_malloc, - nxt_pcre2_free, mp); + general_ctx + = pcre2_general_context_create(nxt_pcre2_malloc, nxt_pcre2_free, mp); if (nxt_slow_path(general_ctx == NULL)) { goto alloc_fail; } @@ -53,16 +52,17 @@ nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) } re->code = pcre2_compile((PCRE2_SPTR) source->start, source->length, 0, - &errcode, &erroffset, compile_ctx); + &errcode, &erroffset, compile_ctx); if (nxt_slow_path(re->code == NULL)) { err->offset = erroffset; ret = pcre2_get_error_message(errcode, (PCRE2_UCHAR *) err->msg, - ERR_BUF_SIZE); + ERR_BUF_SIZE); if (ret < 0) { (void) nxt_sprintf(err->msg, err->msg + ERR_BUF_SIZE, - "compilation failed with unknown " - "error code: %d%Z", errcode); + "compilation failed with unknown " + "error code: %d%Z", + errcode); } return NULL; @@ -93,25 +93,19 @@ nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err) return NULL; } - static void * -nxt_pcre2_malloc(PCRE2_SIZE size, void *mp) -{ +nxt_pcre2_malloc(PCRE2_SIZE size, void *mp) { return nxt_mp_get(mp, size); } - static void -nxt_pcre2_free(void *p, void *mp) -{ +nxt_pcre2_free(void *p, void *mp) { } - nxt_regex_match_t * -nxt_regex_match_create(nxt_mp_t *mp, size_t size) -{ - nxt_regex_match_t *match; - pcre2_general_context *ctx; +nxt_regex_match_create(nxt_mp_t *mp, size_t size) { + nxt_regex_match_t *match; + pcre2_general_context *ctx; ctx = pcre2_general_context_create(nxt_pcre2_malloc, nxt_pcre2_free, mp); if (nxt_slow_path(ctx == NULL)) { @@ -128,30 +122,27 @@ nxt_regex_match_create(nxt_mp_t *mp, size_t size) return match; } - nxt_int_t nxt_regex_match(nxt_regex_t *re, u_char *subject, size_t length, - nxt_regex_match_t *match) -{ - nxt_int_t ret; - PCRE2_UCHAR errptr[ERR_BUF_SIZE]; + nxt_regex_match_t *match) { + nxt_int_t ret; + PCRE2_UCHAR errptr[ERR_BUF_SIZE]; ret = pcre2_match(re->code, (PCRE2_SPTR) subject, length, 0, 0, match, - NULL); + NULL); if (nxt_slow_path(ret < PCRE2_ERROR_NOMATCH)) { - if (pcre2_get_error_message(ret, errptr, ERR_BUF_SIZE) < 0) { nxt_thread_log_error(NXT_LOG_ERR, - "pcre2_match() failed: %d on \"%*s\" " - "using \"%V\"", ret, length, subject, - &re->pattern); + "pcre2_match() failed: %d on \"%*s\" " + "using \"%V\"", + ret, length, subject, &re->pattern); } else { nxt_thread_log_error(NXT_LOG_ERR, - "pcre2_match() failed: %s (%d) on \"%*s\" " - "using \"%V\"", errptr, ret, length, subject, - &re->pattern); + "pcre2_match() failed: %s (%d) on \"%*s\" " + "using \"%V\"", + errptr, ret, length, subject, &re->pattern); } return NXT_ERROR; diff --git a/src/nxt_php_sapi.c b/src/nxt_php_sapi.c index da667b660..c10c6a8db 100644 --- a/src/nxt_php_sapi.c +++ b/src/nxt_php_sapi.c @@ -38,31 +38,30 @@ #ifndef TSRMLS_CC #define TSRMLS_CC #define TSRMLS_DC -#define TSRMLS_D void +#define TSRMLS_D void #define TSRMLS_C #endif typedef struct { - nxt_str_t root; - nxt_str_t index; - nxt_str_t script_name; - nxt_str_t script_dirname; - nxt_str_t script_filename; + nxt_str_t root; + nxt_str_t index; + nxt_str_t script_name; + nxt_str_t script_dirname; + nxt_str_t script_filename; } nxt_php_target_t; - typedef struct { - char *cookie; - nxt_str_t *root; - nxt_str_t *index; + char *cookie; + nxt_str_t *root; + nxt_str_t *index; nxt_str_t path_info; nxt_str_t script_name; nxt_str_t script_filename; nxt_str_t script_dirname; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - uint8_t chdir; /* 1 bit */ + uint8_t chdir; /* 1 bit */ } nxt_php_run_ctx_t; @@ -79,80 +78,107 @@ typedef void (*zif_handler)(INTERNAL_FUNCTION_PARAMETERS); #endif -static nxt_int_t nxt_php_setup(nxt_task_t *task, nxt_process_t *process, +static nxt_int_t +nxt_php_setup(nxt_task_t *task, nxt_process_t *process, nxt_common_app_conf_t *conf); -static nxt_int_t nxt_php_start(nxt_task_t *task, nxt_process_data_t *data); -static nxt_int_t nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, +static nxt_int_t +nxt_php_start(nxt_task_t *task, nxt_process_data_t *data); +static nxt_int_t +nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, nxt_conf_value_t *conf); -static nxt_int_t nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *path, - char *workdir); -static void nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, - int type); -static nxt_int_t nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, - int type); +static nxt_int_t +nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *path, char *workdir); +static void +nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type); +static nxt_int_t +nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type); #ifdef NXT_PHP8 -static void nxt_php_disable_functions(nxt_str_t *str); +static void +nxt_php_disable_functions(nxt_str_t *str); #endif -static void nxt_php_disable(nxt_task_t *task, const char *type, - nxt_str_t *value, char **ptr, nxt_php_disable_t disable); +static void +nxt_php_disable(nxt_task_t *task, const char *type, nxt_str_t *value, + char **ptr, nxt_php_disable_t disable); -static nxt_int_t nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir); -static void nxt_php_str_trim_trail(nxt_str_t *str, u_char t); -static void nxt_php_str_trim_lead(nxt_str_t *str, u_char t); -nxt_inline u_char *nxt_realpath(const void *c); +static nxt_int_t +nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir); +static void +nxt_php_str_trim_trail(nxt_str_t *str, u_char t); +static void +nxt_php_str_trim_lead(nxt_str_t *str, u_char t); +nxt_inline u_char * +nxt_realpath(const void *c); -static nxt_int_t nxt_php_do_301(nxt_unit_request_info_t *req); -static nxt_int_t nxt_php_handle_fs_err(nxt_unit_request_info_t *req); +static nxt_int_t +nxt_php_do_301(nxt_unit_request_info_t *req); +static nxt_int_t +nxt_php_handle_fs_err(nxt_unit_request_info_t *req); -static void nxt_php_request_handler(nxt_unit_request_info_t *req); -static void nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, - nxt_unit_request_t *r); +static void +nxt_php_request_handler(nxt_unit_request_info_t *req); +static void +nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r); #if (PHP_VERSION_ID < 70400) -static void nxt_zend_stream_init_fp(zend_file_handle *handle, FILE *fp, +static void +nxt_zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename); #endif -static void nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r); -nxt_inline void nxt_php_vcwd_chdir(nxt_unit_request_info_t *req, u_char *dir); +static void +nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r); +nxt_inline void +nxt_php_vcwd_chdir(nxt_unit_request_info_t *req, u_char *dir); -static int nxt_php_startup(sapi_module_struct *sapi_module); -static int nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC); -static void *nxt_php_hash_str_find_ptr(const HashTable *ht, - const nxt_str_t *str); +static int +nxt_php_startup(sapi_module_struct *sapi_module); +static int +nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC); +static void * +nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str); static char *nxt_php_read_cookies(TSRMLS_D); -static void nxt_php_set_sptr(nxt_unit_request_info_t *req, const char *name, +static void +nxt_php_set_sptr(nxt_unit_request_info_t *req, const char *name, nxt_unit_sptr_t *v, uint32_t len, zval *track_vars_array TSRMLS_DC); -nxt_inline void nxt_php_set_str(nxt_unit_request_info_t *req, const char *name, - nxt_str_t *s, zval *track_vars_array TSRMLS_DC); -static void nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name, +nxt_inline void +nxt_php_set_str(nxt_unit_request_info_t *req, const char *name, nxt_str_t *s, + zval *track_vars_array TSRMLS_DC); +static void +nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name, const char *str, uint32_t len, zval *track_vars_array TSRMLS_DC); -static void nxt_php_register_variables(zval *track_vars_array TSRMLS_DC); +static void +nxt_php_register_variables(zval *track_vars_array TSRMLS_DC); #if NXT_PHP8 -static void nxt_php_log_message(const char *message, int syslog_type_int); +static void +nxt_php_log_message(const char *message, int syslog_type_int); #else #ifdef NXT_HAVE_PHP_LOG_MESSAGE_WITH_SYSLOG_TYPE -static void nxt_php_log_message(char *message, int syslog_type_int); +static void +nxt_php_log_message(char *message, int syslog_type_int); #else -static void nxt_php_log_message(char *message TSRMLS_DC); +static void +nxt_php_log_message(char *message TSRMLS_DC); #endif #endif #ifdef NXT_PHP7 -static size_t nxt_php_unbuffered_write(const char *str, - size_t str_length TSRMLS_DC); -static size_t nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC); +static size_t +nxt_php_unbuffered_write(const char *str, size_t str_length TSRMLS_DC); +static size_t +nxt_php_read_post(char *buffer, size_t count_bytes TSRMLS_DC); #else -static int nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC); -static int nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC); +static int +nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC); +static int +nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC); #endif #ifdef NXT_PHP7 #if (PHP_VERSION_ID < 70200) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_fastcgi_finish_request, 0, 0, - _IS_BOOL, NULL, 0) + _IS_BOOL, NULL, 0) #else ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_fastcgi_finish_request, 0, 0, - _IS_BOOL, 0) + _IS_BOOL, 0) #endif #else /* PHP5 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_fastcgi_finish_request, 0, 0, 0) @@ -165,51 +191,41 @@ PHP_MINIT_FUNCTION(nxt_php_ext); ZEND_NAMED_FUNCTION(nxt_php_chdir); -static const zend_function_entry nxt_php_ext_functions[] = { - ZEND_FE(fastcgi_finish_request, arginfo_fastcgi_finish_request) - ZEND_FE_END -}; +static const zend_function_entry nxt_php_ext_functions[] = {ZEND_FE( + fastcgi_finish_request, arginfo_fastcgi_finish_request) ZEND_FE_END}; zif_handler nxt_php_chdir_handler; -zend_auto_global *nxt_php_server_ag; - - -static zend_module_entry nxt_php_unit_module = { - STANDARD_MODULE_HEADER, - "unit", - nxt_php_ext_functions, /* function table */ - PHP_MINIT(nxt_php_ext), /* initialization */ - NULL, /* shutdown */ - NULL, /* request initialization */ - NULL, /* request shutdown */ - NULL, /* information */ - NXT_VERSION, - STANDARD_MODULE_PROPERTIES -}; +zend_auto_global *nxt_php_server_ag; -PHP_MINIT_FUNCTION(nxt_php_ext) -{ - zend_function *func; +static zend_module_entry nxt_php_unit_module = {STANDARD_MODULE_HEADER, "unit", + nxt_php_ext_functions, /* function table */ + PHP_MINIT(nxt_php_ext), /* initialization */ + NULL, /* shutdown */ + NULL, /* request initialization */ + NULL, /* request shutdown */ + NULL, /* information */ + NXT_VERSION, STANDARD_MODULE_PROPERTIES}; - static const nxt_str_t chdir = nxt_string("chdir"); +PHP_MINIT_FUNCTION(nxt_php_ext) { + zend_function *func; + + static const nxt_str_t chdir = nxt_string("chdir"); func = nxt_php_hash_str_find_ptr(CG(function_table), &chdir); if (nxt_slow_path(func == NULL)) { return FAILURE; } - nxt_php_chdir_handler = func->internal_function.handler; + nxt_php_chdir_handler = func->internal_function.handler; func->internal_function.handler = nxt_php_chdir; return SUCCESS; } - -ZEND_NAMED_FUNCTION(nxt_php_chdir) -{ - nxt_php_run_ctx_t *ctx; +ZEND_NAMED_FUNCTION(nxt_php_chdir) { + nxt_php_run_ctx_t *ctx; ctx = SG(server_context); @@ -220,11 +236,9 @@ ZEND_NAMED_FUNCTION(nxt_php_chdir) nxt_php_chdir_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); } - -PHP_FUNCTION(fastcgi_finish_request) -{ - zend_auto_global *ag; - nxt_php_run_ctx_t *ctx; +PHP_FUNCTION(fastcgi_finish_request) { + zend_auto_global *ag; + nxt_php_run_ctx_t *ctx; if (nxt_slow_path(zend_parse_parameters_none() == FAILURE)) { #ifdef NXT_PHP8 @@ -280,11 +294,8 @@ PHP_FUNCTION(fastcgi_finish_request) RETURN_TRUE; } - -static sapi_module_struct nxt_php_sapi_module = -{ - (char *) "cli-server", - (char *) "unit", +static sapi_module_struct nxt_php_sapi_module = { + (char *) "cli-server", (char *) "unit", nxt_php_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ @@ -342,12 +353,13 @@ static sapi_module_struct nxt_php_sapi_module = }; -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -NXT_EXPORT nxt_app_module_t nxt_app_module = { +NXT_EXPORT nxt_app_module_t nxt_app_module = { sizeof(compat), compat, nxt_string("php"), @@ -359,27 +371,26 @@ NXT_EXPORT nxt_app_module_t nxt_app_module = { }; -static nxt_php_target_t *nxt_php_targets; +static nxt_php_target_t *nxt_php_targets; static nxt_int_t nxt_php_last_target = -1; -static nxt_unit_ctx_t *nxt_php_unit_ctx; +static nxt_unit_ctx_t *nxt_php_unit_ctx; #if defined(ZTS) && (PHP_VERSION_ID < 70400) -static void ***tsrm_ls; +static void ***tsrm_ls; #endif static nxt_int_t nxt_php_setup(nxt_task_t *task, nxt_process_t *process, - nxt_common_app_conf_t *conf) -{ + nxt_common_app_conf_t *conf) { nxt_str_t ini_path; nxt_int_t ret; - nxt_conf_value_t *value; - nxt_php_app_conf_t *c; + nxt_conf_value_t *value; + nxt_php_app_conf_t *c; - static const nxt_str_t file_str = nxt_string("file"); - static const nxt_str_t user_str = nxt_string("user"); - static const nxt_str_t admin_str = nxt_string("admin"); + static const nxt_str_t file_str = nxt_string("file"); + static const nxt_str_t user_str = nxt_string("user"); + static const nxt_str_t admin_str = nxt_string("admin"); c = &conf->u.php; @@ -413,7 +424,7 @@ nxt_php_setup(nxt_task_t *task, nxt_process_t *process, nxt_conf_get_string(value, &ini_path); ret = nxt_php_set_ini_path(task, &ini_path, - conf->working_directory); + conf->working_directory); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -436,11 +447,11 @@ nxt_php_setup(nxt_task_t *task, nxt_process_t *process, #ifdef NXT_PHP7 nxt_php_server_ag = zend_hash_str_find_ptr(CG(auto_globals), "_SERVER", - nxt_length("_SERVER")); + nxt_length("_SERVER")); #else zend_hash_quick_find(CG(auto_globals), "_SERVER", sizeof("_SERVER"), - zend_hash_func("_SERVER", sizeof("_SERVER")), - (void **) &nxt_php_server_ag); + zend_hash_func("_SERVER", sizeof("_SERVER")), + (void **) &nxt_php_server_ag); #endif if (nxt_slow_path(nxt_php_server_ag == NULL)) { nxt_alert(task, "failed to find $_SERVER auto global"); @@ -450,22 +461,20 @@ nxt_php_setup(nxt_task_t *task, nxt_process_t *process, return NXT_OK; } - static nxt_int_t -nxt_php_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_php_start(nxt_task_t *task, nxt_process_data_t *data) { uint32_t next; nxt_int_t ret; nxt_str_t name; nxt_uint_t n; - nxt_unit_ctx_t *unit_ctx; + nxt_unit_ctx_t *unit_ctx; nxt_unit_init_t php_init; - nxt_conf_value_t *value; - nxt_php_app_conf_t *c; - nxt_common_app_conf_t *conf; + nxt_conf_value_t *value; + nxt_php_app_conf_t *c; + nxt_common_app_conf_t *conf; conf = data->app; - c = &conf->u.php; + c = &conf->u.php; n = (c->targets != NULL) ? nxt_conf_object_members_count(c->targets) : 1; @@ -519,19 +528,17 @@ nxt_php_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_OK; } - static nxt_int_t nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, - nxt_conf_value_t *conf) -{ - u_char *tmp, *p; + nxt_conf_value_t *conf) { + u_char *tmp, *p; nxt_str_t str; nxt_int_t ret; - nxt_conf_value_t *value; + nxt_conf_value_t *value; - static const nxt_str_t root_str = nxt_string("root"); - static const nxt_str_t script_str = nxt_string("script"); - static const nxt_str_t index_str = nxt_string("index"); + static const nxt_str_t root_str = nxt_string("root"); + static const nxt_str_t script_str = nxt_string("script"); + static const nxt_str_t index_str = nxt_string("index"); value = nxt_conf_get_object_member(conf, &root_str, NULL); @@ -544,7 +551,7 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, p = tmp; - p = nxt_cpymem(p, str.start, str.length); + p = nxt_cpymem(p, str.start, str.length); *p = '\0'; p = nxt_realpath(tmp); @@ -556,7 +563,7 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, nxt_free(tmp); target->root.length = nxt_strlen(p); - target->root.start = p; + target->root.start = p; nxt_php_str_trim_trail(&target->root, '/'); @@ -574,10 +581,10 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, p = tmp; - p = nxt_cpymem(p, target->root.start, target->root.length); + p = nxt_cpymem(p, target->root.start, target->root.length); *p++ = '/'; - p = nxt_cpymem(p, str.start, str.length); + p = nxt_cpymem(p, str.start, str.length); *p = '\0'; p = nxt_realpath(tmp); @@ -589,25 +596,24 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, nxt_free(tmp); target->script_filename.length = nxt_strlen(p); - target->script_filename.start = p; + target->script_filename.start = p; - if (!nxt_str_start(&target->script_filename, - target->root.start, target->root.length)) - { + if (!nxt_str_start(&target->script_filename, target->root.start, + target->root.length)) { nxt_alert(task, "script is not under php root"); return NXT_ERROR; } ret = nxt_php_dirname(&target->script_filename, - &target->script_dirname); + &target->script_dirname); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } - target->script_name.length = target->script_filename.length - - target->root.length; - target->script_name.start = target->script_filename.start - + target->root.length; + target->script_name.length + = target->script_filename.length - target->root.length; + target->script_name.start + = target->script_filename.start + target->root.length; } else { value = nxt_conf_get_object_member(conf, &index_str, NULL); @@ -623,7 +629,7 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, nxt_memcpy(tmp, str.start, str.length); target->index.length = str.length; - target->index.start = tmp; + target->index.start = tmp; } else { nxt_str_set(&target->index, "index.php"); @@ -633,12 +639,10 @@ nxt_php_set_target(nxt_task_t *task, nxt_php_target_t *target, return NXT_OK; } - static nxt_int_t -nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *ini_path, char *workdir) -{ +nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *ini_path, char *workdir) { size_t wdlen; - u_char *p, *start; + u_char *p, *start; if (ini_path->start[0] == '/' || workdir == NULL) { p = nxt_malloc(ini_path->length + 1); @@ -665,7 +669,7 @@ nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *ini_path, char *workdir) } } - p = nxt_cpymem(p, ini_path->start, ini_path->length); + p = nxt_cpymem(p, ini_path->start, ini_path->length); *p = '\0'; nxt_php_sapi_module.php_ini_path_override = (char *) start; @@ -673,18 +677,16 @@ nxt_php_set_ini_path(nxt_task_t *task, nxt_str_t *ini_path, char *workdir) return NXT_OK; } - static void -nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type) -{ +nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type) { uint32_t next; nxt_str_t name, value; - nxt_conf_value_t *value_obj; + nxt_conf_value_t *value_obj; if (options != NULL) { next = 0; - for ( ;; ) { + for (;;) { value_obj = nxt_conf_next_object_member(options, &name, &next); if (value_obj == NULL) { break; @@ -694,7 +696,7 @@ nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type) if (nxt_php_alter_option(&name, &value, type) != NXT_OK) { nxt_log(task, NXT_LOG_ERR, - "setting PHP option \"%V: %V\" failed", &name, &value); + "setting PHP option \"%V: %V\" failed", &name, &value); continue; } @@ -703,16 +705,14 @@ nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type) nxt_php_disable_functions(&value); #else nxt_php_disable(task, "function", &value, - &PG(disable_functions), - zend_disable_function); + &PG(disable_functions), zend_disable_function); #endif continue; } if (nxt_str_eq(&name, "disable_classes", 15)) { - nxt_php_disable(task, "class", &value, - &PG(disable_classes), - zend_disable_class); + nxt_php_disable(task, "class", &value, &PG(disable_classes), + zend_disable_class); continue; } } @@ -723,10 +723,9 @@ nxt_php_set_options(nxt_task_t *task, nxt_conf_value_t *options, int type) #ifdef NXT_PHP7 static nxt_int_t -nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) -{ - zend_string *zs; - zend_ini_entry *ini_entry; +nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) { + zend_string *zs; + zend_ini_entry *ini_entry; ini_entry = nxt_php_hash_str_find_ptr(EG(ini_directives), name); if (nxt_slow_path(ini_entry == NULL)) { @@ -738,27 +737,24 @@ nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) if (ini_entry->on_modify && ini_entry->on_modify(ini_entry, zs, ini_entry->mh_arg1, - ini_entry->mh_arg2, ini_entry->mh_arg3, - ZEND_INI_STAGE_ACTIVATE) - != SUCCESS) - { + ini_entry->mh_arg2, ini_entry->mh_arg3, ZEND_INI_STAGE_ACTIVATE) + != SUCCESS) { zend_string_release(zs); return NXT_ERROR; } - ini_entry->value = zs; + ini_entry->value = zs; ini_entry->modifiable = type; return NXT_OK; } -#else /* PHP 5. */ +#else /* PHP 5. */ static nxt_int_t -nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) -{ - char *cstr; - zend_ini_entry *ini_entry; +nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) { + char *cstr; + zend_ini_entry *ini_entry; ini_entry = nxt_php_hash_str_find_ptr(EG(ini_directives), name); if (nxt_slow_path(ini_entry == NULL)) { @@ -775,18 +771,16 @@ nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) if (ini_entry->on_modify && ini_entry->on_modify(ini_entry, cstr, value->length, - ini_entry->mh_arg1, ini_entry->mh_arg2, - ini_entry->mh_arg3, ZEND_INI_STAGE_ACTIVATE - TSRMLS_CC) - != SUCCESS) - { + ini_entry->mh_arg1, ini_entry->mh_arg2, ini_entry->mh_arg3, + ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) + != SUCCESS) { nxt_free(cstr); return NXT_ERROR; } - ini_entry->value = cstr; + ini_entry->value = cstr; ini_entry->value_length = value->length; - ini_entry->modifiable = type; + ini_entry->modifiable = type; return NXT_OK; } @@ -797,9 +791,8 @@ nxt_php_alter_option(nxt_str_t *name, nxt_str_t *value, int type) #ifdef NXT_PHP8 static void -nxt_php_disable_functions(nxt_str_t *str) -{ - char *p; +nxt_php_disable_functions(nxt_str_t *str) { + char *p; p = nxt_malloc(str->length + 1); if (nxt_slow_path(p == NULL)) { @@ -819,9 +812,8 @@ nxt_php_disable_functions(nxt_str_t *str) static void nxt_php_disable(nxt_task_t *task, const char *type, nxt_str_t *value, - char **ptr, nxt_php_disable_t disable) -{ - char c, *p, *start; + char **ptr, nxt_php_disable_t disable) { + char c, *p, *start; p = nxt_malloc(value->length + 1); if (nxt_slow_path(p == NULL)) { @@ -843,7 +835,6 @@ nxt_php_disable(nxt_task_t *task, const char *type, nxt_str_t *value, c = *p; if (c == ' ' || c == ',' || c == '\0') { - if (p != start) { *p = '\0'; @@ -852,11 +843,10 @@ nxt_php_disable(nxt_task_t *task, const char *type, nxt_str_t *value, #else if (disable(start, p - start TSRMLS_CC) #endif - != SUCCESS) - { + != SUCCESS) { nxt_log(task, NXT_LOG_ERR, - "PHP: failed to disable \"%s\": no such %s", - start, type); + "PHP: failed to disable \"%s\": no such %s", start, + type); } } @@ -868,15 +858,13 @@ nxt_php_disable(nxt_task_t *task, const char *type, nxt_str_t *value, } while (c != '\0'); } - static nxt_int_t -nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir) -{ - size_t length; +nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir) { + size_t length; if (file->length == 0 || file->start[0] != '/') { nxt_unit_alert(NULL, "php_dirname: invalid file name " - "(not starts from '/')"); + "(not starts from '/')"); return NXT_ERROR; } @@ -887,7 +875,7 @@ nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir) } dir->length = length; - dir->start = nxt_malloc(length + 1); + dir->start = nxt_malloc(length + 1); if (nxt_slow_path(dir->start == NULL)) { return NXT_ERROR; } @@ -899,10 +887,8 @@ nxt_php_dirname(const nxt_str_t *file, nxt_str_t *dir) return NXT_OK; } - static void -nxt_php_str_trim_trail(nxt_str_t *str, u_char t) -{ +nxt_php_str_trim_trail(nxt_str_t *str, u_char t) { while (str->length > 0 && str->start[str->length - 1] == t) { str->length--; } @@ -910,64 +896,53 @@ nxt_php_str_trim_trail(nxt_str_t *str, u_char t) str->start[str->length] = '\0'; } - static void -nxt_php_str_trim_lead(nxt_str_t *str, u_char t) -{ +nxt_php_str_trim_lead(nxt_str_t *str, u_char t) { while (str->length > 0 && str->start[0] == t) { str->length--; str->start++; } } - nxt_inline u_char * -nxt_realpath(const void *c) -{ +nxt_realpath(const void *c) { return (u_char *) realpath(c, NULL); } - static nxt_int_t -nxt_php_do_301(nxt_unit_request_info_t *req) -{ - char *p, *url, *port; +nxt_php_do_301(nxt_unit_request_info_t *req) { + char *p, *url, *port; uint32_t size; - const char *proto; - nxt_unit_request_t *r; + const char *proto; + nxt_unit_request_t *r; r = req->request; - url = nxt_malloc(sizeof("https://") - 1 - + r->server_name_length - + r->local_port_length + 1 - + r->path_length + 1 - + r->query_length + 1 - + 1); + url = nxt_malloc(sizeof("https://") - 1 + r->server_name_length + + r->local_port_length + 1 + r->path_length + 1 + + r->query_length + 1 + 1); if (nxt_slow_path(url == NULL)) { return NXT_UNIT_ERROR; } proto = r->tls ? "https://" : "http://"; - p = nxt_cpymem(url, proto, strlen(proto)); - p = nxt_cpymem(p, nxt_unit_sptr_get(&r->server_name), - r->server_name_length); + p = nxt_cpymem(url, proto, strlen(proto)); + p = nxt_cpymem(p, nxt_unit_sptr_get(&r->server_name), + r->server_name_length); port = nxt_unit_sptr_get(&r->local_port); - if (r->local_port_length > 0 - && !(r->tls && strcmp(port, "443") == 0) - && !(!r->tls && strcmp(port, "80") == 0)) - { + if (r->local_port_length > 0 && !(r->tls && strcmp(port, "443") == 0) + && !(!r->tls && strcmp(port, "80") == 0)) { *p++ = ':'; - p = nxt_cpymem(p, port, r->local_port_length); + p = nxt_cpymem(p, port, r->local_port_length); } - p = nxt_cpymem(p, nxt_unit_sptr_get(&r->path), r->path_length); + p = nxt_cpymem(p, nxt_unit_sptr_get(&r->path), r->path_length); *p++ = '/'; if (r->query_length > 0) { *p++ = '?'; - p = nxt_cpymem(p, nxt_unit_sptr_get(&r->query), r->query_length); + p = nxt_cpymem(p, nxt_unit_sptr_get(&r->query), r->query_length); } *p = '\0'; @@ -975,19 +950,17 @@ nxt_php_do_301(nxt_unit_request_info_t *req) size = p - url; nxt_unit_response_init(req, NXT_HTTP_MOVED_PERMANENTLY, 1, - nxt_length("Location") + size); - nxt_unit_response_add_field(req, "Location", nxt_length("Location"), - url, size); + nxt_length("Location") + size); + nxt_unit_response_add_field(req, "Location", nxt_length("Location"), url, + size); nxt_free(url); return NXT_UNIT_OK; } - static nxt_int_t -nxt_php_handle_fs_err(nxt_unit_request_info_t *req) -{ +nxt_php_handle_fs_err(nxt_unit_request_info_t *req) { switch (nxt_errno) { case ELOOP: case EACCES: @@ -1002,21 +975,19 @@ nxt_php_handle_fs_err(nxt_unit_request_info_t *req) return NXT_UNIT_ERROR; } - static void -nxt_php_request_handler(nxt_unit_request_info_t *req) -{ - nxt_php_target_t *target; +nxt_php_request_handler(nxt_unit_request_info_t *req) { + nxt_php_target_t *target; nxt_php_run_ctx_t ctx; - nxt_unit_request_t *r; + nxt_unit_request_t *r; - r = req->request; + r = req->request; target = &nxt_php_targets[r->app_target]; nxt_memzero(&ctx, sizeof(ctx)); - ctx.req = req; - ctx.root = &target->root; + ctx.req = req; + ctx.root = &target->root; ctx.index = &target->index; if (target->script_filename.length == 0) { @@ -1025,8 +996,8 @@ nxt_php_request_handler(nxt_unit_request_info_t *req) } ctx.script_filename = target->script_filename; - ctx.script_dirname = target->script_dirname; - ctx.script_name = target->script_name; + ctx.script_dirname = target->script_dirname; + ctx.script_name = target->script_name; ctx.chdir = (r->app_target != nxt_php_last_target); @@ -1035,24 +1006,22 @@ nxt_php_request_handler(nxt_unit_request_info_t *req) nxt_php_last_target = ctx.chdir ? -1 : r->app_target; } - static void -nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) -{ - u_char *p; - nxt_str_t path, script_name; - nxt_int_t ret; +nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) { + u_char *p; + nxt_str_t path, script_name; + nxt_int_t ret; path.length = r->path_length; - path.start = nxt_unit_sptr_get(&r->path); + path.start = nxt_unit_sptr_get(&r->path); nxt_str_null(&script_name); - ctx->path_info.start = memmem(path.start, path.length, ".php/", - strlen(".php/")); + ctx->path_info.start + = memmem(path.start, path.length, ".php/", strlen(".php/")); if (ctx->path_info.start != NULL) { ctx->path_info.start += 4; - path.length = ctx->path_info.start - path.start; + path.length = ctx->path_info.start - path.start; ctx->path_info.length = r->path_length - path.length; @@ -1060,11 +1029,10 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) script_name = *ctx->index; } else if (path.length < 4 - || memcmp(path.start + (path.length - 4), ".php", 4) != 0) - { - char tpath[PATH_MAX]; - nxt_int_t ec; - struct stat sb; + || memcmp(path.start + (path.length - 4), ".php", 4) != 0) { + char tpath[PATH_MAX]; + nxt_int_t ec; + struct stat sb; ec = NXT_UNIT_ERROR; @@ -1074,8 +1042,8 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) return; } - p = nxt_cpymem(tpath, ctx->root->start, ctx->root->length); - p = nxt_cpymem(p, path.start, path.length); + p = nxt_cpymem(tpath, ctx->root->start, ctx->root->length); + p = nxt_cpymem(p, path.start, path.length); *p = '\0'; ret = stat(tpath, &sb); @@ -1090,9 +1058,8 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) return; } - ctx->script_filename.length = ctx->root->length - + path.length - + script_name.length; + ctx->script_filename.length + = ctx->root->length + path.length + script_name.length; p = nxt_malloc(ctx->script_filename.length + 1); if (nxt_slow_path(p == NULL)) { @@ -1104,7 +1071,7 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) ctx->script_filename.start = p; ctx->script_name.length = path.length + script_name.length; - ctx->script_name.start = p + ctx->root->length; + ctx->script_name.start = p + ctx->root->length; p = nxt_cpymem(p, ctx->root->start, ctx->root->length); p = nxt_cpymem(p, path.start, path.length); @@ -1137,27 +1104,25 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) #if (PHP_VERSION_ID < 70400) static void nxt_zend_stream_init_fp(zend_file_handle *handle, FILE *fp, - const char *filename) -{ + const char *filename) { nxt_memzero(handle, sizeof(zend_file_handle)); - handle->type = ZEND_HANDLE_FP; + handle->type = ZEND_HANDLE_FP; handle->handle.fp = fp; - handle->filename = filename; + handle->filename = filename; } #else -#define nxt_zend_stream_init_fp zend_stream_init_fp +#define nxt_zend_stream_init_fp zend_stream_init_fp #endif static void -nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) -{ - FILE *fp; +nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) { + FILE *fp; #if (PHP_VERSION_ID < 50600) - void *read_post; + void *read_post; #endif - const char *filename; - nxt_unit_field_t *f; + const char *filename; + nxt_unit_field_t *f; zend_file_handle file_handle; filename = (const char *) ctx->script_filename.start; @@ -1166,7 +1131,7 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) fp = fopen(filename, "re"); if (fp == NULL) { - nxt_int_t ec; + nxt_int_t ec; nxt_unit_req_debug(ctx->req, "PHP fopen(\"%s\") failed", filename); @@ -1175,15 +1140,15 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) return; } - SG(server_context) = ctx; - SG(options) |= SAPI_OPTION_NO_CHDIR; - SG(request_info).request_uri = nxt_unit_sptr_get(&r->target); - SG(request_info).request_method = nxt_unit_sptr_get(&r->method); + SG(server_context) = ctx; + SG(options) |= SAPI_OPTION_NO_CHDIR; + SG(request_info).request_uri = nxt_unit_sptr_get(&r->target); + SG(request_info).request_method = nxt_unit_sptr_get(&r->method); SG(request_info).proto_num = 1001; - SG(request_info).query_string = r->query.offset - ? nxt_unit_sptr_get(&r->query) : NULL; + SG(request_info).query_string + = r->query.offset ? nxt_unit_sptr_get(&r->query) : NULL; SG(request_info).content_length = r->content_length; if (r->content_type_field != NXT_UNIT_NONE_FIELD) { @@ -1208,8 +1173,8 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) #endif } else { - SG(request_info).auth_digest = NULL; - SG(request_info).auth_user = NULL; + SG(request_info).auth_digest = NULL; + SG(request_info).auth_user = NULL; SG(request_info).auth_password = NULL; } @@ -1245,7 +1210,7 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) /* Prevention of consuming possible unread request body. */ #if (PHP_VERSION_ID < 50600) - read_post = sapi_module.read_post; + read_post = sapi_module.read_post; sapi_module.read_post = NULL; #else SG(post_read) = 1; @@ -1262,20 +1227,16 @@ nxt_php_execute(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r) #endif } - nxt_inline void -nxt_php_vcwd_chdir(nxt_unit_request_info_t *req, u_char *dir) -{ +nxt_php_vcwd_chdir(nxt_unit_request_info_t *req, u_char *dir) { if (nxt_slow_path(VCWD_CHDIR((char *) dir) != 0)) { - nxt_unit_req_alert(req, "VCWD_CHDIR(%s) failed (%d: %s)", - dir, errno, strerror(errno)); + nxt_unit_req_alert(req, "VCWD_CHDIR(%s) failed (%d: %s)", dir, errno, + strerror(errno)); } } - static int -nxt_php_startup(sapi_module_struct *sapi_module) -{ +nxt_php_startup(sapi_module_struct *sapi_module) { #if (PHP_VERSION_ID < 80200) return php_module_startup(sapi_module, &nxt_php_unit_module, 1); #else @@ -1293,7 +1254,7 @@ nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC) #endif { int rc; - nxt_php_run_ctx_t *ctx; + nxt_php_run_ctx_t *ctx; ctx = SG(server_context); @@ -1306,18 +1267,16 @@ nxt_php_unbuffered_write(const char *str, uint str_length TSRMLS_DC) return 0; } - static int -nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) -{ +nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) { int rc, fields_count; - char *colon, *value; + char *colon, *value; uint16_t status; uint32_t resp_size; - nxt_php_run_ctx_t *ctx; - sapi_header_struct *h; + nxt_php_run_ctx_t *ctx; + sapi_header_struct *h; zend_llist_position zpos; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; ctx = SG(server_context); req = ctx->req; @@ -1333,13 +1292,11 @@ nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) return SAPI_HEADER_SENT_SUCCESSFULLY; } - resp_size = 0; + resp_size = 0; fields_count = zend_llist_count(&sapi_headers->headers); - for (h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos); - h; - h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos)) - { + for (h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos); h; + h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos)) { resp_size += h->header_len; } @@ -1350,25 +1307,22 @@ nxt_php_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) return SAPI_HEADER_SEND_FAILED; } - for (h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos); - h; - h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos)) - { + for (h = zend_llist_get_first_ex(&sapi_headers->headers, &zpos); h; + h = zend_llist_get_next_ex(&sapi_headers->headers, &zpos)) { colon = memchr(h->header, ':', h->header_len); if (nxt_slow_path(colon == NULL)) { nxt_unit_req_warn(req, "colon not found in header '%.*s'", - (int) h->header_len, h->header); + (int) h->header_len, h->header); continue; } value = colon + 1; - while(isspace(*value)) { + while (isspace(*value)) { value++; } - nxt_unit_response_add_field(req, h->header, colon - h->header, - value, - h->header_len - (value - h->header)); + nxt_unit_response_add_field(req, h->header, colon - h->header, value, + h->header_len - (value - h->header)); } rc = nxt_unit_response_send(req); @@ -1390,7 +1344,7 @@ static int nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC) #endif { - nxt_php_run_ctx_t *ctx; + nxt_php_run_ctx_t *ctx; ctx = SG(server_context); @@ -1399,11 +1353,9 @@ nxt_php_read_post(char *buffer, uint count_bytes TSRMLS_DC) return nxt_unit_request_read(ctx->req, buffer, count_bytes); } - static char * -nxt_php_read_cookies(TSRMLS_D) -{ - nxt_php_run_ctx_t *ctx; +nxt_php_read_cookies(TSRMLS_D) { + nxt_php_run_ctx_t *ctx; ctx = SG(server_context); @@ -1412,29 +1364,27 @@ nxt_php_read_cookies(TSRMLS_D) return ctx->cookie; } - static void -nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) -{ - const char *name; - nxt_unit_field_t *f, *f_end; - nxt_php_run_ctx_t *ctx; - nxt_unit_request_t *r; - nxt_unit_request_info_t *req; +nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) { + const char *name; + nxt_unit_field_t *f, *f_end; + nxt_php_run_ctx_t *ctx; + nxt_unit_request_t *r; + nxt_unit_request_info_t *req; ctx = SG(server_context); req = ctx->req; - r = req->request; + r = req->request; nxt_unit_req_debug(req, "nxt_php_register_variables"); php_register_variable_safe((char *) "SERVER_SOFTWARE", - (char *) nxt_server.start, - nxt_server.length, track_vars_array TSRMLS_CC); + (char *) nxt_server.start, nxt_server.length, + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "SERVER_PROTOCOL", &r->version, r->version_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); /* * 'PHP_SELF' @@ -1449,14 +1399,14 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) if (ctx->path_info.length != 0) { nxt_php_set_sptr(req, "PHP_SELF", &r->path, r->path_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_str(req, "PATH_INFO", &ctx->path_info, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } else { nxt_php_set_str(req, "PHP_SELF", &ctx->script_name, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } /* @@ -1467,7 +1417,7 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) */ nxt_php_set_str(req, "SCRIPT_NAME", &ctx->script_name, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); /* * 'SCRIPT_FILENAME' @@ -1475,7 +1425,7 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) */ nxt_php_set_str(req, "SCRIPT_FILENAME", &ctx->script_filename, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); /* * 'DOCUMENT_ROOT' @@ -1484,22 +1434,22 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) */ nxt_php_set_str(req, "DOCUMENT_ROOT", ctx->root, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "REQUEST_METHOD", &r->method, r->method_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "REQUEST_URI", &r->target, r->target_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "QUERY_STRING", &r->query, r->query_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "REMOTE_ADDR", &r->remote, r->remote_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "SERVER_ADDR", &r->local_addr, r->local_addr_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_sptr(req, "SERVER_NAME", &r->server_name, r->server_name_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); nxt_php_set_cstr(req, "SERVER_PORT", "80", 2, track_vars_array TSRMLS_CC); if (r->tls) { @@ -1511,34 +1461,32 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC) name = nxt_unit_sptr_get(&f->name); nxt_php_set_sptr(req, name, &f->value, f->value_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } if (r->content_length_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_length_field; nxt_php_set_sptr(req, "CONTENT_LENGTH", &f->value, f->value_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } if (r->content_type_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_type_field; nxt_php_set_sptr(req, "CONTENT_TYPE", &f->value, f->value_length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } } - static void nxt_php_set_sptr(nxt_unit_request_info_t *req, const char *name, - nxt_unit_sptr_t *v, uint32_t len, zval *track_vars_array TSRMLS_DC) -{ - char *str; + nxt_unit_sptr_t *v, uint32_t len, zval *track_vars_array TSRMLS_DC) { + char *str; #if NXT_PHP7 - size_t new_len; + size_t new_len; #else - unsigned int new_len; + unsigned int new_len; #endif str = nxt_unit_sptr_get(v); @@ -1546,38 +1494,33 @@ nxt_php_set_sptr(nxt_unit_request_info_t *req, const char *name, nxt_unit_req_debug(req, "php: register %s='%.*s'", name, (int) len, str); if (sapi_module.input_filter(PARSE_SERVER, (char *) name, &str, len, - &new_len TSRMLS_CC)) - { + &new_len TSRMLS_CC)) { php_register_variable_safe((char *) name, str, new_len, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } } - nxt_inline void -nxt_php_set_str(nxt_unit_request_info_t *req, const char *name, - nxt_str_t *s, zval *track_vars_array TSRMLS_DC) -{ +nxt_php_set_str(nxt_unit_request_info_t *req, const char *name, nxt_str_t *s, + zval *track_vars_array TSRMLS_DC) { nxt_php_set_cstr(req, name, (char *) s->start, s->length, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } #ifdef NXT_PHP7 static void * -nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str) -{ +nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str) { return zend_hash_str_find_ptr(ht, (const char *) str->start, str->length); } #else static void * -nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str) -{ +nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str) { int ret; - void *entry; + void *entry; char buf[256]; if (nxt_slow_path(str->length >= (sizeof(buf) - 1))) { @@ -1600,8 +1543,7 @@ nxt_php_hash_str_find_ptr(const HashTable *ht, const nxt_str_t *str) static void nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name, - const char *cstr, uint32_t len, zval *track_vars_array TSRMLS_DC) -{ + const char *cstr, uint32_t len, zval *track_vars_array TSRMLS_DC) { if (nxt_slow_path(cstr == NULL)) { return; } @@ -1609,7 +1551,7 @@ nxt_php_set_cstr(nxt_unit_request_info_t *req, const char *name, nxt_unit_req_debug(req, "php: register %s='%.*s'", name, (int) len, cstr); php_register_variable_safe((char *) name, (char *) cstr, len, - track_vars_array TSRMLS_CC); + track_vars_array TSRMLS_CC); } @@ -1626,16 +1568,16 @@ nxt_php_log_message(char *message TSRMLS_DC) #endif #endif { - nxt_php_run_ctx_t *ctx; + nxt_php_run_ctx_t *ctx; ctx = SG(server_context); if (ctx != NULL) { - nxt_unit_req_log(ctx->req, NXT_UNIT_LOG_NOTICE, - "php message: %s", message); + nxt_unit_req_log(ctx->req, NXT_UNIT_LOG_NOTICE, "php message: %s", + message); } else { - nxt_unit_log(nxt_php_unit_ctx, NXT_UNIT_LOG_NOTICE, - "php message: %s", message); + nxt_unit_log(nxt_php_unit_ctx, NXT_UNIT_LOG_NOTICE, "php message: %s", + message); } } diff --git a/src/nxt_polarssl.c b/src/nxt_polarssl.c index 4a8144731..a74b425a8 100644 --- a/src/nxt_polarssl.c +++ b/src/nxt_polarssl.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Igor Sysoev @@ -10,33 +9,32 @@ #include #include - typedef struct { - ssl_context ssl; - x509_cert certificate; - rsa_context key; + ssl_context ssl; + x509_cert certificate; + rsa_context key; } nxt_polarssl_ctx_t; - -static nxt_int_t nxt_polarssl_server_init(nxt_ssltls_conf_t *conf); -static void nxt_polarssl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, +static nxt_int_t +nxt_polarssl_server_init(nxt_ssltls_conf_t *conf); +static void +nxt_polarssl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, nxt_event_conn_t *c); -static void nxt_polarssl_log_error(nxt_uint_t level, nxt_log_t *log, int err, +static void +nxt_polarssl_log_error(nxt_uint_t level, nxt_log_t *log, int err, const char *fmt, ...); -nxt_ssltls_lib_t nxt_polarssl_lib = { +nxt_ssltls_lib_t nxt_polarssl_lib = { nxt_polarssl_server_init, NULL, }; - static nxt_int_t -nxt_polarssl_server_init(nxt_ssltls_conf_t *conf) -{ +nxt_polarssl_server_init(nxt_ssltls_conf_t *conf) { int n; - nxt_thread_t *thr; - nxt_polarssl_ctx_t *ctx; + nxt_thread_t *thr; + nxt_polarssl_ctx_t *ctx; thr = nxt_thread(); @@ -53,16 +51,15 @@ nxt_polarssl_server_init(nxt_ssltls_conf_t *conf) return NXT_ERROR; } - ssl_set_endpoint(&ctx->ssl, SSL_IS_SERVER ); + ssl_set_endpoint(&ctx->ssl, SSL_IS_SERVER); - conf->ctx = ctx; + conf->ctx = ctx; conf->conn_init = nxt_polarssl_conn_init; n = x509parse_crtfile(&ctx->certificate, conf->certificate); if (n != 0) { nxt_polarssl_log_error(NXT_LOG_ALERT, thr->log, n, - "x509parse_crt(\"%V\") failed", - &conf->certificate); + "x509parse_crt(\"%V\") failed", &conf->certificate); goto fail; } @@ -71,8 +68,7 @@ nxt_polarssl_server_init(nxt_ssltls_conf_t *conf) n = x509parse_keyfile(&ctx->key, conf->certificate_key, NULL); if (n != 0) { nxt_polarssl_log_error(NXT_LOG_ALERT, thr->log, n, - "x509parse_key(\"%V\") failed", - &conf->certificate_key); + "x509parse_key(\"%V\") failed", &conf->certificate_key); goto fail; } @@ -89,20 +85,16 @@ nxt_polarssl_server_init(nxt_ssltls_conf_t *conf) return NXT_ERROR; } - static void nxt_polarssl_conn_init(nxt_thread_t *thr, nxt_ssltls_conf_t *conf, - nxt_event_conn_t *c) -{ + nxt_event_conn_t *c) { } - static void nxt_polarssl_log_error(nxt_uint_t level, nxt_log_t *log, int err, - const char *fmt, ...) -{ - va_list args; - u_char *p, *end, msg[NXT_MAX_ERROR_STR]; + const char *fmt, ...) { + va_list args; + u_char *p, *end, msg[NXT_MAX_ERROR_STR]; end = msg + NXT_MAX_ERROR_STR; diff --git a/src/nxt_poll_engine.c b/src/nxt_poll_engine.c index f514e0a9a..8573122fb 100644 --- a/src/nxt_poll_engine.c +++ b/src/nxt_poll_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,10 +6,9 @@ #include -#define NXT_POLL_ADD 0 -#define NXT_POLL_CHANGE 1 -#define NXT_POLL_DELETE 2 - +#define NXT_POLL_ADD 0 +#define NXT_POLL_CHANGE 1 +#define NXT_POLL_DELETE 2 typedef struct { /* @@ -19,53 +17,61 @@ typedef struct { * nxt_fd_event_t which may be invalid if the file descriptor has * been already closed and the nxt_fd_event_t's memory has been freed. */ - nxt_socket_t fd; + nxt_socket_t fd; - uint32_t index; - void *event; + uint32_t index; + void *event; } nxt_poll_hash_entry_t; +static nxt_int_t +nxt_poll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_poll_free(nxt_event_engine_t *engine); +static void +nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_poll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op, + nxt_uint_t events); +static nxt_int_t +nxt_poll_commit_changes(nxt_event_engine_t *engine); +static nxt_int_t +nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events); +static nxt_int_t +nxt_poll_set_change(nxt_event_engine_t *engine, nxt_fd_t fd, int events); +static nxt_int_t +nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd); +static void +nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); +static nxt_poll_hash_entry_t * +nxt_poll_fd_hash_get(nxt_event_engine_t *engine, nxt_fd_t fd); +static nxt_int_t +nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static void +nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine, nxt_lvlhsh_t *lh); + -static nxt_int_t nxt_poll_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_poll_free(nxt_event_engine_t *engine); -static void nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_poll_disable(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static nxt_bool_t nxt_poll_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_poll_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - nxt_uint_t op, nxt_uint_t events); -static nxt_int_t nxt_poll_commit_changes(nxt_event_engine_t *engine); -static nxt_int_t nxt_poll_set_add(nxt_event_engine_t *engine, - nxt_fd_event_t *ev, int events); -static nxt_int_t nxt_poll_set_change(nxt_event_engine_t *engine, - nxt_fd_t fd, int events); -static nxt_int_t nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd); -static void nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); -static nxt_poll_hash_entry_t *nxt_poll_fd_hash_get(nxt_event_engine_t *engine, - nxt_fd_t fd); -static nxt_int_t nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data); -static void nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine, - nxt_lvlhsh_t *lh); - - -const nxt_event_interface_t nxt_poll_engine = { +const nxt_event_interface_t nxt_poll_engine = { "poll", nxt_poll_create, nxt_poll_free, @@ -95,19 +101,16 @@ const nxt_event_interface_t nxt_poll_engine = { }; -static const nxt_lvlhsh_proto_t nxt_poll_fd_hash_proto nxt_aligned(64) = -{ +static const nxt_lvlhsh_proto_t nxt_poll_fd_hash_proto nxt_aligned(64) = { NXT_LVLHSH_LARGE_MEMALIGN, nxt_poll_fd_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - static nxt_int_t nxt_poll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ + nxt_uint_t mevents) { engine->u.poll.mchanges = mchanges; engine->u.poll.changes = nxt_malloc(sizeof(nxt_poll_change_t) * mchanges); @@ -119,10 +122,8 @@ nxt_poll_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_poll_free(nxt_event_engine_t *engine) -{ +nxt_poll_free(nxt_event_engine_t *engine) { nxt_debug(&engine->task, "poll free"); nxt_free(engine->u.poll.set); @@ -132,166 +133,145 @@ nxt_poll_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.poll, sizeof(nxt_poll_engine_t)); } - static void -nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_poll_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->write = NXT_EVENT_ACTIVE; nxt_poll_change(engine, ev, NXT_POLL_ADD, POLLIN | POLLOUT); } - static void -nxt_poll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_poll_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE && ev->write != NXT_EVENT_INACTIVE) { - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_poll_change(engine, ev, NXT_POLL_DELETE, 0); } } - static nxt_bool_t -nxt_poll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_poll_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_poll_disable(engine, ev); return ev->changing; } - static void -nxt_poll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_poll_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->read = NXT_EVENT_ACTIVE; if (ev->write == NXT_EVENT_INACTIVE) { - op = NXT_POLL_ADD; + op = NXT_POLL_ADD; events = POLLIN; } else { - op = NXT_POLL_CHANGE; + op = NXT_POLL_CHANGE; events = POLLIN | POLLOUT; } nxt_poll_change(engine, ev, op, events); } - static void -nxt_poll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_poll_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->write = NXT_EVENT_ACTIVE; if (ev->read == NXT_EVENT_INACTIVE) { - op = NXT_POLL_ADD; + op = NXT_POLL_ADD; events = POLLOUT; } else { - op = NXT_POLL_CHANGE; + op = NXT_POLL_CHANGE; events = POLLIN | POLLOUT; } nxt_poll_change(engine, ev, op, events); } - static void -nxt_poll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_poll_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->read = NXT_EVENT_INACTIVE; if (ev->write == NXT_EVENT_INACTIVE) { - op = NXT_POLL_DELETE; + op = NXT_POLL_DELETE; events = 0; } else { - op = NXT_POLL_CHANGE; + op = NXT_POLL_CHANGE; events = POLLOUT; } nxt_poll_change(engine, ev, op, events); } - static void -nxt_poll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_poll_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->write = NXT_EVENT_INACTIVE; if (ev->read == NXT_EVENT_INACTIVE) { - op = NXT_POLL_DELETE; + op = NXT_POLL_DELETE; events = 0; } else { - op = NXT_POLL_CHANGE; + op = NXT_POLL_CHANGE; events = POLLIN; } nxt_poll_change(engine, ev, op, events); } - static void -nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_poll_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { nxt_poll_disable_read(engine, ev); } } - static void -nxt_poll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_poll_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { nxt_poll_disable_write(engine, ev); } } - static void -nxt_poll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op; +nxt_poll_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op; - op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ? - NXT_POLL_ADD : NXT_POLL_CHANGE; + op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) + ? NXT_POLL_ADD + : NXT_POLL_CHANGE; - ev->read = NXT_EVENT_ONESHOT; + ev->read = NXT_EVENT_ONESHOT; ev->write = NXT_EVENT_INACTIVE; nxt_poll_change(engine, ev, op, POLLIN); } - static void -nxt_poll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op; +nxt_poll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op; - op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) ? - NXT_POLL_ADD : NXT_POLL_CHANGE; + op = (ev->read == NXT_EVENT_INACTIVE && ev->write == NXT_EVENT_INACTIVE) + ? NXT_POLL_ADD + : NXT_POLL_CHANGE; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_ONESHOT; nxt_poll_change(engine, ev, op, POLLOUT); } - /* * poll changes are batched to improve instruction and data cache * locality of several lvlhsh operations followed by poll() call. @@ -299,9 +279,8 @@ nxt_poll_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) static void nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op, - nxt_uint_t events) -{ - nxt_poll_change_t *change; + nxt_uint_t events) { + nxt_poll_change_t *change; nxt_debug(ev->task, "poll change: fd:%d op:%d ev:%XD", ev->fd, op, events); @@ -311,32 +290,29 @@ nxt_poll_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op, ev->changing = 1; - change = &engine->u.poll.changes[engine->u.poll.nchanges++]; - change->op = op; + change = &engine->u.poll.changes[engine->u.poll.nchanges++]; + change->op = op; change->events = events; - change->event = ev; + change->event = ev; } - static nxt_int_t -nxt_poll_commit_changes(nxt_event_engine_t *engine) -{ +nxt_poll_commit_changes(nxt_event_engine_t *engine) { nxt_int_t ret, retval; - nxt_fd_event_t *ev; - nxt_poll_change_t *change, *end; + nxt_fd_event_t *ev; + nxt_poll_change_t *change, *end; nxt_debug(&engine->task, "poll changes:%ui", engine->u.poll.nchanges); retval = NXT_OK; change = engine->u.poll.changes; - end = change + engine->u.poll.nchanges; + end = change + engine->u.poll.nchanges; do { - ev = change->event; + ev = change->event; ev->changing = 0; switch (change->op) { - case NXT_POLL_ADD: ret = nxt_poll_set_add(engine, ev, change->events); @@ -366,7 +342,7 @@ nxt_poll_commit_changes(nxt_event_engine_t *engine) } nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); retval = NXT_ERROR; @@ -381,15 +357,13 @@ nxt_poll_commit_changes(nxt_event_engine_t *engine) return retval; } - static nxt_int_t -nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events) -{ +nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events) { nxt_int_t ret; nxt_uint_t max_nfds; - struct pollfd *pfd; + struct pollfd *pfd; nxt_lvlhsh_query_t lhq; - nxt_poll_hash_entry_t *phe; + nxt_poll_hash_entry_t *phe; nxt_debug(&engine->task, "poll add event: fd:%d ev:%04Xi", ev->fd, events); @@ -401,7 +375,7 @@ nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events) return NXT_ERROR; } - engine->u.poll.set = pfd; + engine->u.poll.set = pfd; engine->u.poll.max_nfds = max_nfds; } @@ -410,20 +384,20 @@ nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events) return NXT_ERROR; } - phe->fd = ev->fd; + phe->fd = ev->fd; phe->index = engine->u.poll.nfds; phe->event = ev; - pfd = &engine->u.poll.set[engine->u.poll.nfds++]; - pfd->fd = ev->fd; - pfd->events = events; + pfd = &engine->u.poll.set[engine->u.poll.nfds++]; + pfd->fd = ev->fd; + pfd->events = events; pfd->revents = 0; lhq.key_hash = nxt_murmur_hash2(&ev->fd, sizeof(nxt_fd_t)); - lhq.replace = 0; - lhq.value = phe; - lhq.proto = &nxt_poll_fd_hash_proto; - lhq.data = engine; + lhq.replace = 0; + lhq.value = phe; + lhq.proto = &nxt_poll_fd_hash_proto; + lhq.data = engine; ret = nxt_lvlhsh_insert(&engine->u.poll.fd_hash, &lhq); @@ -436,14 +410,11 @@ nxt_poll_set_add(nxt_event_engine_t *engine, nxt_fd_event_t *ev, int events) return NXT_ERROR; } - static nxt_int_t -nxt_poll_set_change(nxt_event_engine_t *engine, nxt_fd_t fd, int events) -{ - nxt_poll_hash_entry_t *phe; +nxt_poll_set_change(nxt_event_engine_t *engine, nxt_fd_t fd, int events) { + nxt_poll_hash_entry_t *phe; - nxt_debug(&engine->task, "poll change event: fd:%d ev:%04Xi", - fd, events); + nxt_debug(&engine->task, "poll change event: fd:%d ev:%04Xi", fd, events); phe = nxt_poll_fd_hash_get(engine, fd); @@ -455,20 +426,18 @@ nxt_poll_set_change(nxt_event_engine_t *engine, nxt_fd_t fd, int events) return NXT_ERROR; } - static nxt_int_t -nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd) -{ +nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd) { nxt_int_t ret; nxt_uint_t index, nfds; nxt_lvlhsh_query_t lhq; - nxt_poll_hash_entry_t *phe; + nxt_poll_hash_entry_t *phe; nxt_debug(&engine->task, "poll delete event: fd:%d", fd); lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t)); - lhq.proto = &nxt_poll_fd_hash_proto; - lhq.data = engine; + lhq.proto = &nxt_poll_fd_hash_proto; + lhq.data = engine; ret = nxt_lvlhsh_delete(&engine->u.poll.fd_hash, &lhq); @@ -495,18 +464,16 @@ nxt_poll_set_delete(nxt_event_engine_t *engine, nxt_fd_t fd) return NXT_OK; } - static void -nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ +nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { int nevents; nxt_fd_t fd; nxt_err_t err; nxt_bool_t error; nxt_uint_t i, events, level; - struct pollfd *pfd; - nxt_fd_event_t *ev; - nxt_poll_hash_entry_t *phe; + struct pollfd *pfd; + nxt_fd_event_t *ev; + nxt_poll_hash_entry_t *phe; if (engine->u.poll.nchanges != 0) { if (nxt_poll_commit_changes(engine) != NXT_OK) { @@ -516,7 +483,7 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_debug(&engine->task, "poll() events:%ui timeout:%M", - engine->u.poll.nfds, timeout); + engine->u.poll.nfds, timeout); nevents = poll(engine->u.poll.set, engine->u.poll.nfds, timeout); @@ -533,8 +500,7 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } for (i = 0; i < engine->u.poll.nfds && nevents != 0; i++) { - - pfd = &engine->u.poll.set[i]; + pfd = &engine->u.poll.set[i]; events = pfd->revents; if (events == 0) { @@ -547,8 +513,8 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (nxt_slow_path(phe == NULL)) { nxt_alert(&engine->task, - "poll() returned invalid fd:%d ev:%04Xd rev:%04uXi", - fd, pfd->events, events); + "poll() returned invalid fd:%d ev:%04Xd rev:%04uXi", fd, + pfd->events, events); /* Mark the poll entry to ignore it by the kernel. */ pfd->fd = -1; @@ -557,18 +523,18 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) ev = phe->event; - nxt_debug(ev->task, "poll: fd:%d ev:%04uXi rd:%d wr:%d", - fd, events, ev->read, ev->write); + nxt_debug(ev->task, "poll: fd:%d ev:%04uXi rd:%d wr:%d", fd, events, + ev->read, ev->write); if (nxt_slow_path((events & POLLNVAL) != 0)) { - nxt_alert(ev->task, "poll() error fd:%d ev:%04Xd rev:%04uXi", - fd, pfd->events, events); + nxt_alert(ev->task, "poll() error fd:%d ev:%04Xd rev:%04uXi", fd, + pfd->events, events); /* Mark the poll entry to ignore it by the kernel. */ pfd->fd = -1; - nxt_work_queue_add(&engine->fast_work_queue, - ev->error_handler, ev->task, ev, ev->data); + nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, + ev->task, ev, ev->data); goto next; } @@ -602,7 +568,7 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) && ((events & (POLLIN | POLLOUT)) == 0)); if ((events & POLLIN) || (error && ev->read_handler != NULL)) { - error = 0; + error = 0; ev->read_ready = 1; if (ev->read == NXT_EVENT_ONESHOT) { @@ -610,8 +576,8 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) nxt_poll_change(engine, ev, NXT_POLL_DELETE, 0); } - nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); + nxt_work_queue_add(ev->read_work_queue, ev->read_handler, ev->task, + ev, ev->data); } if ((events & POLLOUT) || (error && ev->write_handler != NULL)) { @@ -623,7 +589,7 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } next: @@ -632,16 +598,14 @@ nxt_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } } - static nxt_poll_hash_entry_t * -nxt_poll_fd_hash_get(nxt_event_engine_t *engine, nxt_fd_t fd) -{ +nxt_poll_fd_hash_get(nxt_event_engine_t *engine, nxt_fd_t fd) { nxt_lvlhsh_query_t lhq; - nxt_poll_hash_entry_t *phe; + nxt_poll_hash_entry_t *phe; lhq.key_hash = nxt_murmur_hash2(&fd, sizeof(nxt_fd_t)); - lhq.proto = &nxt_poll_fd_hash_proto; - lhq.data = engine; + lhq.proto = &nxt_poll_fd_hash_proto; + lhq.data = engine; if (nxt_lvlhsh_find(&engine->u.poll.fd_hash, &lhq) == NXT_OK) { phe = lhq.value; @@ -653,12 +617,10 @@ nxt_poll_fd_hash_get(nxt_event_engine_t *engine, nxt_fd_t fd) return NULL; } - static nxt_int_t -nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_event_engine_t *engine; - nxt_poll_hash_entry_t *phe; +nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_event_engine_t *engine; + nxt_poll_hash_entry_t *phe; phe = data; @@ -671,18 +633,16 @@ nxt_poll_fd_hash_test(nxt_lvlhsh_query_t *lhq, void *data) } nxt_alert(&engine->task, "fd %d in hash mismatches fd %d in poll set", - phe->fd, engine->u.poll.set[phe->index].fd); + phe->fd, engine->u.poll.set[phe->index].fd); return NXT_DECLINED; } - static void -nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine, nxt_lvlhsh_t *lh) -{ - nxt_poll_hash_entry_t *phe; +nxt_poll_fd_hash_destroy(nxt_event_engine_t *engine, nxt_lvlhsh_t *lh) { + nxt_poll_hash_entry_t *phe; - for ( ;; ) { + for (;;) { phe = nxt_lvlhsh_retrieve(lh, &nxt_poll_fd_hash_proto, NULL); if (phe == NULL) { diff --git a/src/nxt_pollset_engine.c b/src/nxt_pollset_engine.c index e2c1a2d01..c2c31116f 100644 --- a/src/nxt_pollset_engine.c +++ b/src/nxt_pollset_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -16,47 +15,55 @@ */ -#define NXT_POLLSET_ADD 0 -#define NXT_POLLSET_UPDATE 1 -#define NXT_POLLSET_CHANGE 2 -#define NXT_POLLSET_DELETE 3 - - -static nxt_int_t nxt_pollset_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_pollset_free(nxt_event_engine_t *engine); -static void nxt_pollset_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_pollset_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static nxt_bool_t nxt_pollset_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, +#define NXT_POLLSET_ADD 0 +#define NXT_POLLSET_UPDATE 1 +#define NXT_POLLSET_CHANGE 2 +#define NXT_POLLSET_DELETE 3 + + +static nxt_int_t +nxt_pollset_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_pollset_free(nxt_event_engine_t *engine); +static void +nxt_pollset_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_pollset_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, nxt_uint_t op, nxt_uint_t events); -static nxt_int_t nxt_pollset_commit_changes(nxt_event_engine_t *engine); -static void nxt_pollset_change_error(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_pollset_remove(nxt_event_engine_t *engine, nxt_fd_t fd); -static nxt_int_t nxt_pollset_write(nxt_event_engine_t *engine, - struct poll_ctl *ctl, int n); -static void nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); +static nxt_int_t +nxt_pollset_commit_changes(nxt_event_engine_t *engine); +static void +nxt_pollset_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_pollset_remove(nxt_event_engine_t *engine, nxt_fd_t fd); +static nxt_int_t +nxt_pollset_write(nxt_event_engine_t *engine, struct poll_ctl *ctl, int n); +static void +nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); -const nxt_event_interface_t nxt_pollset_engine = { +const nxt_event_interface_t nxt_pollset_engine = { "pollset", nxt_pollset_create, nxt_pollset_free, @@ -85,16 +92,14 @@ const nxt_event_interface_t nxt_pollset_engine = { NXT_NO_SIGNAL_EVENTS, }; - static nxt_int_t nxt_pollset_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - void *changes; + nxt_uint_t mevents) { + void *changes; - engine->u.pollset.ps = -1; + engine->u.pollset.ps = -1; engine->u.pollset.mchanges = mchanges; - engine->u.pollset.mevents = mevents; + engine->u.pollset.mevents = mevents; changes = nxt_malloc(sizeof(nxt_pollset_change_t) * mchanges); if (changes == NULL) { @@ -137,19 +142,17 @@ nxt_pollset_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_pollset_free(nxt_event_engine_t *engine) -{ - pollset_t ps; +nxt_pollset_free(nxt_event_engine_t *engine) { + pollset_t ps; ps = engine->u.pollset.ps; nxt_debug(&engine->task, "pollset %d free", ps); if (ps != -1 && pollset_destroy(ps) != 0) { - nxt_alert(&engine->task, "pollset_destroy(%d) failed %E", - ps, nxt_errno); + nxt_alert(&engine->task, "pollset_destroy(%d) failed %E", ps, + nxt_errno); } nxt_free(engine->u.pollset.events); @@ -160,30 +163,24 @@ nxt_pollset_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.pollset, sizeof(nxt_pollset_engine_t)); } - static void -nxt_pollset_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_ACTIVE; +nxt_pollset_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_ACTIVE; ev->write = NXT_EVENT_ACTIVE; nxt_pollset_change(engine, ev, NXT_POLLSET_ADD, POLLIN | POLLOUT); } - static void -nxt_pollset_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE || ev->write != NXT_EVENT_INACTIVE) { - - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; nxt_pollset_change(engine, ev, NXT_POLLSET_DELETE, 0); } } - /* * A closed descriptor must be deleted from a pollset, otherwise next * pollset_poll() will return POLLNVAL on it. However, pollset_ctl() @@ -200,21 +197,17 @@ nxt_pollset_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) */ static nxt_bool_t -nxt_pollset_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_pollset_disable(engine, ev); return ev->changing; } - static void -nxt_pollset_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_pollset_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; if (ev->read != NXT_EVENT_BLOCKED) { - events = POLLIN; if (ev->write == NXT_EVENT_INACTIVE) { @@ -222,10 +215,10 @@ nxt_pollset_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } else if (ev->write == NXT_EVENT_BLOCKED) { ev->write = NXT_EVENT_INACTIVE; - op = NXT_POLLSET_CHANGE; + op = NXT_POLLSET_CHANGE; } else { - op = NXT_POLLSET_UPDATE; + op = NXT_POLLSET_UPDATE; events = POLLIN | POLLOUT; } @@ -235,14 +228,11 @@ nxt_pollset_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->read = NXT_EVENT_ACTIVE; } - static void -nxt_pollset_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_pollset_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; if (ev->write != NXT_EVENT_BLOCKED) { - events = POLLOUT; if (ev->read == NXT_EVENT_INACTIVE) { @@ -250,10 +240,10 @@ nxt_pollset_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } else if (ev->read == NXT_EVENT_BLOCKED) { ev->read = NXT_EVENT_INACTIVE; - op = NXT_POLLSET_CHANGE; + op = NXT_POLLSET_CHANGE; } else { - op = NXT_POLLSET_UPDATE; + op = NXT_POLLSET_UPDATE; events = POLLIN | POLLOUT; } @@ -263,85 +253,72 @@ nxt_pollset_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->write = NXT_EVENT_ACTIVE; } - static void -nxt_pollset_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_pollset_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->read = NXT_EVENT_INACTIVE; if (ev->write <= NXT_EVENT_BLOCKED) { ev->write = NXT_EVENT_INACTIVE; - op = NXT_POLLSET_DELETE; - events = POLLREMOVE; + op = NXT_POLLSET_DELETE; + events = POLLREMOVE; } else { - op = NXT_POLLSET_CHANGE; + op = NXT_POLLSET_CHANGE; events = POLLOUT; } nxt_pollset_change(engine, ev, op, events); } - static void -nxt_pollset_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_uint_t op, events; +nxt_pollset_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_uint_t op, events; ev->write = NXT_EVENT_INACTIVE; if (ev->read <= NXT_EVENT_BLOCKED) { ev->read = NXT_EVENT_INACTIVE; - op = NXT_POLLSET_DELETE; - events = POLLREMOVE; + op = NXT_POLLSET_DELETE; + events = POLLREMOVE; } else { - op = NXT_POLLSET_CHANGE; + op = NXT_POLLSET_CHANGE; events = POLLIN; } nxt_pollset_change(engine, ev, op, events); } - static void -nxt_pollset_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { ev->read = NXT_EVENT_BLOCKED; } } - static void -nxt_pollset_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { ev->write = NXT_EVENT_BLOCKED; } } - static void -nxt_pollset_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_pollset_enable_read(engine, ev); ev->read = NXT_EVENT_ONESHOT; } - static void -nxt_pollset_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_pollset_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_pollset_enable_write(engine, ev); ev->write = NXT_EVENT_ONESHOT; } - /* * PS_ADD adds only a new file descriptor to a pollset. * PS_DELETE removes a file descriptor from a pollset. @@ -355,12 +332,11 @@ nxt_pollset_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) static void nxt_pollset_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, - nxt_uint_t op, nxt_uint_t events) -{ - nxt_pollset_change_t *change; + nxt_uint_t op, nxt_uint_t events) { + nxt_pollset_change_t *change; nxt_debug(ev->task, "pollset %d change fd:%d op:%ui ev:%04Xi", - engine->u.pollset.ps, ev->fd, op, events); + engine->u.pollset.ps, ev->fd, op, events); if (engine->u.pollset.nchanges >= engine->u.pollset.mchanges) { (void) nxt_pollset_commit_changes(engine); @@ -368,62 +344,59 @@ nxt_pollset_change(nxt_event_engine_t *engine, nxt_fd_event_t *ev, ev->changing = 1; - change = &engine->u.pollset.changes[engine->u.pollset.nchanges++]; - change->op = op; - change->cmd = (op == NXT_POLLSET_DELETE) ? PS_DELETE : PS_MOD; + change = &engine->u.pollset.changes[engine->u.pollset.nchanges++]; + change->op = op; + change->cmd = (op == NXT_POLLSET_DELETE) ? PS_DELETE : PS_MOD; change->events = events; - change->event = ev; + change->event = ev; } - static nxt_int_t -nxt_pollset_commit_changes(nxt_event_engine_t *engine) -{ +nxt_pollset_commit_changes(nxt_event_engine_t *engine) { size_t n; nxt_int_t ret, retval; - nxt_fd_event_t *ev; - struct poll_ctl *ctl, *write_changes; - nxt_pollset_change_t *change, *end; + nxt_fd_event_t *ev; + struct poll_ctl *ctl, *write_changes; + nxt_pollset_change_t *change, *end; - nxt_debug(&engine->task, "pollset %d changes:%ui", - engine->u.pollset.ps, engine->u.pollset.nchanges); + nxt_debug(&engine->task, "pollset %d changes:%ui", engine->u.pollset.ps, + engine->u.pollset.nchanges); - retval = NXT_OK; - n = 0; + retval = NXT_OK; + n = 0; write_changes = engine->u.pollset.write_changes; - change = engine->u.pollset.changes; - end = change + engine->u.pollset.nchanges; + change = engine->u.pollset.changes; + end = change + engine->u.pollset.nchanges; do { - ev = change->event; + ev = change->event; ev->changing = 0; - nxt_debug(&engine->task, "pollset fd:%d op:%d ev:%04Xd", - ev->fd, change->op, change->events); + nxt_debug(&engine->task, "pollset fd:%d op:%d ev:%04Xd", ev->fd, + change->op, change->events); if (change->op == NXT_POLLSET_CHANGE) { - ctl = &write_changes[n++]; - ctl->cmd = PS_DELETE; + ctl = &write_changes[n++]; + ctl->cmd = PS_DELETE; ctl->events = 0; - ctl->fd = ev->fd; + ctl->fd = ev->fd; } - ctl = &write_changes[n++]; - ctl->cmd = change->cmd; + ctl = &write_changes[n++]; + ctl->cmd = change->cmd; ctl->events = change->events; - ctl->fd = ev->fd; + ctl->fd = ev->fd; change++; } while (change < end); change = engine->u.pollset.changes; - end = change + engine->u.pollset.nchanges; + end = change + engine->u.pollset.nchanges; ret = nxt_pollset_write(engine, write_changes, n); if (nxt_slow_path(ret != NXT_OK)) { - do { nxt_pollset_change_error(engine, change->event); change++; @@ -447,7 +420,7 @@ nxt_pollset_commit_changes(nxt_event_engine_t *engine) } else if (change->op == NXT_POLLSET_DELETE) { nxt_fd_event_hash_delete(&engine->task, &engine->u.pollset.fd_hash, - ev->fd, 0); + ev->fd, 0); } /* Nothing to do for NXT_POLLSET_UPDATE and NXT_POLLSET_CHANGE. */ @@ -461,38 +434,34 @@ nxt_pollset_commit_changes(nxt_event_engine_t *engine) return retval; } - static void -nxt_pollset_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - ev->read = NXT_EVENT_INACTIVE; +nxt_pollset_change_error(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; - nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, - ev->task, ev, ev->data); + nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, ev->task, + ev, ev->data); - nxt_fd_event_hash_delete(&engine->task, &engine->u.pollset.fd_hash, - ev->fd, 1); + nxt_fd_event_hash_delete(&engine->task, &engine->u.pollset.fd_hash, ev->fd, + 1); nxt_pollset_remove(engine, ev->fd); } - static void -nxt_pollset_remove(nxt_event_engine_t *engine, nxt_fd_t fd) -{ - int n; - struct pollfd pfd; - struct poll_ctl ctl; - - pfd.fd = fd; - pfd.events = 0; +nxt_pollset_remove(nxt_event_engine_t *engine, nxt_fd_t fd) { + int n; + struct pollfd pfd; + struct poll_ctl ctl; + + pfd.fd = fd; + pfd.events = 0; pfd.revents = 0; n = pollset_query(engine->u.pollset.ps, &pfd); - nxt_debug(&engine->task, "pollset_query(%d, %d): %d", - engine->u.pollset.ps, fd, n); + nxt_debug(&engine->task, "pollset_query(%d, %d): %d", engine->u.pollset.ps, + fd, n); if (n == 0) { /* The file descriptor is not in the pollset. */ @@ -501,27 +470,25 @@ nxt_pollset_remove(nxt_event_engine_t *engine, nxt_fd_t fd) if (n == -1) { nxt_alert(&engine->task, "pollset_query(%d, %d) failed %E", - engine->u.pollset.ps, fd, nxt_errno); + engine->u.pollset.ps, fd, nxt_errno); /* Fall through. */ } /* n == 1: The file descriptor is in the pollset. */ - nxt_debug(&engine->task, "pollset %d remove fd:%d", - engine->u.pollset.ps, fd); + nxt_debug(&engine->task, "pollset %d remove fd:%d", engine->u.pollset.ps, + fd); - ctl.cmd = PS_DELETE; + ctl.cmd = PS_DELETE; ctl.events = 0; - ctl.fd = fd; + ctl.fd = fd; nxt_pollset_write(engine, &ctl, 1); } - static nxt_int_t -nxt_pollset_write(nxt_event_engine_t *engine, struct poll_ctl *ctl, int n) -{ - pollset_t ps; +nxt_pollset_write(nxt_event_engine_t *engine, struct poll_ctl *ctl, int n) { + pollset_t ps; ps = engine->u.pollset.ps; @@ -540,17 +507,15 @@ nxt_pollset_write(nxt_event_engine_t *engine, struct poll_ctl *ctl, int n) return NXT_ERROR; } - static void -nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ +nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { int nevents; nxt_fd_t fd; nxt_int_t i; nxt_err_t err; nxt_uint_t events, level; - struct pollfd *pfd; - nxt_fd_event_t *ev; + struct pollfd *pfd; + nxt_fd_event_t *ev; if (engine->u.pollset.nchanges != 0) { if (nxt_pollset_commit_changes(engine) != NXT_OK) { @@ -560,41 +525,40 @@ nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_debug(&engine->task, "pollset_poll(%d) timeout:%M", - engine->u.pollset.ps, timeout); + engine->u.pollset.ps, timeout); nevents = pollset_poll(engine->u.pollset.ps, engine->u.pollset.events, - engine->u.pollset.mevents, timeout); + engine->u.pollset.mevents, timeout); err = (nevents == -1) ? nxt_errno : 0; nxt_thread_time_update(engine->task.thread); - nxt_debug(&engine->task, "pollset_poll(%d): %d", - engine->u.pollset.ps, nevents); + nxt_debug(&engine->task, "pollset_poll(%d): %d", engine->u.pollset.ps, + nevents); if (nevents == -1) { level = (err == NXT_EINTR) ? NXT_LOG_INFO : NXT_LOG_ALERT; nxt_log(&engine->task, level, "pollset_poll(%d) failed %E", - engine->u.pollset.ps, err); + engine->u.pollset.ps, err); return; } for (i = 0; i < nevents; i++) { - - pfd = &engine->u.pollset.events[i]; - fd = pfd->fd; + pfd = &engine->u.pollset.events[i]; + fd = pfd->fd; events = pfd->revents; ev = nxt_fd_event_hash_get(&engine->task, &engine->u.pollset.fd_hash, - fd); + fd); if (nxt_slow_path(ev == NULL)) { nxt_alert(&engine->task, - "pollset_poll(%d) returned invalid " - "fd:%d ev:%04Xd rev:%04uXi", - engine->u.pollset.ps, fd, pfd->events, events); + "pollset_poll(%d) returned invalid " + "fd:%d ev:%04Xd rev:%04uXi", + engine->u.pollset.ps, fd, pfd->events, events); nxt_pollset_remove(engine, fd); continue; @@ -604,11 +568,11 @@ nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (nxt_slow_path(events & (POLLERR | POLLHUP | POLLNVAL)) != 0) { nxt_alert(ev->task, - "pollset_poll(%d) error fd:%d ev:%04Xd rev:%04uXi", - engine->u.pollset.ps, fd, pfd->events, events); + "pollset_poll(%d) error fd:%d ev:%04Xd rev:%04uXi", + engine->u.pollset.ps, fd, pfd->events, events); nxt_work_queue_add(&engine->fast_work_queue, ev->error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); continue; } @@ -617,12 +581,11 @@ nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->read != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } if (ev->read == NXT_EVENT_BLOCKED - || ev->read == NXT_EVENT_ONESHOT) - { + || ev->read == NXT_EVENT_ONESHOT) { nxt_pollset_disable_read(engine, ev); } } @@ -632,12 +595,11 @@ nxt_pollset_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) if (ev->write != NXT_EVENT_BLOCKED) { nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); } if (ev->write == NXT_EVENT_BLOCKED - || ev->write == NXT_EVENT_ONESHOT) - { + || ev->write == NXT_EVENT_ONESHOT) { nxt_pollset_disable_write(engine, ev); } } diff --git a/src/nxt_port.c b/src/nxt_port.c index b9964df46..aeb7713d0 100644 --- a/src/nxt_port.c +++ b/src/nxt_port.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -12,21 +11,20 @@ #include -static void nxt_port_remove_pid(nxt_task_t *task, nxt_port_recv_msg_t *msg, - nxt_pid_t pid); -static void nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_port_remove_pid(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_pid_t pid); +static void +nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); static nxt_atomic_uint_t nxt_port_last_id = 1; - static void -nxt_port_mp_cleanup(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_port_t *port; +nxt_port_mp_cleanup(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_port_t *port; port = obj; - mp = data; + mp = data; nxt_assert(port->pair[0] == -1); nxt_assert(port->pair[1] == -1); @@ -44,13 +42,11 @@ nxt_port_mp_cleanup(nxt_task_t *task, void *obj, void *data) nxt_mp_free(mp, port); } - nxt_port_t * nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, - nxt_process_type_t type) -{ - nxt_mp_t *mp; - nxt_port_t *port; + nxt_process_type_t type) { + nxt_mp_t *mp; + nxt_port_t *port; mp = nxt_mp_create(1024, 128, 256, 32); @@ -61,10 +57,10 @@ nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, port = nxt_mp_zalloc(mp, sizeof(nxt_port_t)); if (nxt_fast_path(port != NULL)) { - port->id = id; - port->pid = pid; - port->type = type; - port->mem_pool = mp; + port->id = id; + port->pid = pid; + port->type = type; + port->mem_pool = mp; port->use_count = 1; nxt_mp_cleanup(mp, nxt_port_mp_cleanup, task, port, mp); @@ -83,14 +79,12 @@ nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, return port; } - void -nxt_port_close(nxt_task_t *task, nxt_port_t *port) -{ - size_t size; +nxt_port_close(nxt_task_t *task, nxt_port_t *port) { + size_t size; - nxt_debug(task, "port %p %d:%d close, type %d", port, port->pid, - port->id, port->type); + nxt_debug(task, "port %p %d:%d close, type %d", port, port->pid, port->id, + port->type); if (port->pair[0] != -1) { nxt_port_rpc_close(task, port); @@ -122,12 +116,10 @@ nxt_port_close(nxt_task_t *task, nxt_port_t *port) } } - static void -nxt_port_release(nxt_task_t *task, nxt_port_t *port) -{ - nxt_debug(task, "port %p %d:%d release, type %d", port, port->pid, - port->id, port->type); +nxt_port_release(nxt_task_t *task, nxt_port_t *port) { + nxt_debug(task, "port %p %d:%d release, type %d", port, port->pid, port->id, + port->type); port->app = NULL; @@ -142,43 +134,33 @@ nxt_port_release(nxt_task_t *task, nxt_port_t *port) nxt_mp_release(port->mem_pool); } - nxt_port_id_t -nxt_port_get_next_id(void) -{ +nxt_port_get_next_id(void) { return nxt_atomic_fetch_add(&nxt_port_last_id, 1); } - void -nxt_port_reset_next_id(void) -{ +nxt_port_reset_next_id(void) { nxt_port_last_id = 1; } - void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, - const nxt_port_handlers_t *handlers) -{ - port->pid = nxt_pid; + const nxt_port_handlers_t *handlers) { + port->pid = nxt_pid; port->handler = nxt_port_handler; - port->data = (nxt_port_handler_t *) (handlers); + port->data = (nxt_port_handler_t *) (handlers); nxt_port_read_enable(task, port); } - static void -nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_port_handler_t *handlers; +nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_port_handler_t *handlers; if (nxt_fast_path(msg->port_msg.type < NXT_PORT_MSG_MAX)) { - nxt_debug(task, "port %d: message type:%uD fds:%d,%d", - msg->port->socket.fd, msg->port_msg.type, - msg->fd[0], msg->fd[1]); + msg->port->socket.fd, msg->port_msg.type, msg->fd[0], msg->fd[1]); handlers = msg->port->data; handlers[msg->port_msg.type](task, msg); @@ -186,31 +168,26 @@ nxt_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) return; } - nxt_alert(task, "port %d: unknown message type:%uD", - msg->port->socket.fd, msg->port_msg.type); + nxt_alert(task, "port %d: unknown message type:%uD", msg->port->socket.fd, + msg->port_msg.type); } - void -nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_runtime_quit(task, 0); } - /* TODO join with process_ready and move to nxt_main_process.c */ nxt_inline void nxt_port_send_new_port(nxt_task_t *task, nxt_runtime_t *rt, - nxt_port_t *new_port, uint32_t stream) -{ - nxt_port_t *port; - nxt_process_t *process; + nxt_port_t *new_port, uint32_t stream) { + nxt_port_t *port; + nxt_process_t *process; - nxt_debug(task, "new port %d for process %PI", - new_port->pair[1], new_port->pid); + nxt_debug(task, "new port %d for process %PI", new_port->pair[1], + new_port->pid); nxt_runtime_process_each(rt, process) { - if (process->pid == new_port->pid || process->pid == nxt_pid) { continue; } @@ -220,48 +197,43 @@ nxt_port_send_new_port(nxt_task_t *task, nxt_runtime_t *rt, if (nxt_proc_send_matrix[port->type][new_port->type]) { (void) nxt_port_send_port(task, port, new_port, stream); } - - } nxt_runtime_process_loop; + } + nxt_runtime_process_loop; } - nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, nxt_port_t *new_port, - uint32_t stream) -{ - nxt_buf_t *b; - nxt_port_msg_new_port_t *msg; + uint32_t stream) { + nxt_buf_t *b; + nxt_port_msg_new_port_t *msg; b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, - sizeof(nxt_port_data_t)); + sizeof(nxt_port_data_t)); if (nxt_slow_path(b == NULL)) { return NXT_ERROR; } - nxt_debug(task, "send port %FD to process %PI", - new_port->pair[1], port->pid); + nxt_debug(task, "send port %FD to process %PI", new_port->pair[1], + port->pid); b->mem.free += sizeof(nxt_port_msg_new_port_t); - msg = (nxt_port_msg_new_port_t *) b->mem.pos; + msg = (nxt_port_msg_new_port_t *) b->mem.pos; - msg->id = new_port->id; - msg->pid = new_port->pid; - msg->max_size = port->max_size; + msg->id = new_port->id; + msg->pid = new_port->pid; + msg->max_size = port->max_size; msg->max_share = port->max_share; - msg->type = new_port->type; + msg->type = new_port->type; return nxt_port_socket_write2(task, port, NXT_PORT_MSG_NEW_PORT, - new_port->pair[1], new_port->queue_fd, - stream, 0, b); + new_port->pair[1], new_port->queue_fd, stream, 0, b); } - void -nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_port_msg_new_port_t *new_port_msg; +nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_port_msg_new_port_t *new_port_msg; rt = task->thread->runtime; @@ -269,13 +241,13 @@ nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) /* TODO check b size and make plain */ - nxt_debug(task, "new port %d received for process %PI:%d", - msg->fd[0], new_port_msg->pid, new_port_msg->id); + nxt_debug(task, "new port %d received for process %PI:%d", msg->fd[0], + new_port_msg->pid, new_port_msg->id); port = nxt_runtime_port_find(rt, new_port_msg->pid, new_port_msg->id); if (port != NULL) { nxt_debug(task, "port %PI:%d already exists", new_port_msg->pid, - new_port_msg->id); + new_port_msg->id); msg->u.new_port = port; @@ -285,17 +257,16 @@ nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } port = nxt_runtime_process_port_create(task, rt, new_port_msg->pid, - new_port_msg->id, - new_port_msg->type); + new_port_msg->id, new_port_msg->type); if (nxt_slow_path(port == NULL)) { return; } nxt_fd_nonblocking(task, msg->fd[0]); - port->pair[0] = -1; - port->pair[1] = msg->fd[0]; - port->max_size = new_port_msg->max_size; + port->pair[0] = -1; + port->pair[1] = msg->fd[0]; + port->max_size = new_port_msg->max_size; port->max_share = new_port_msg->max_share; port->socket.task = task; @@ -307,11 +278,10 @@ nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) /* TODO move to nxt_main_process.c */ void -nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_port_t *port; - nxt_process_t *process; - nxt_runtime_t *rt; +nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_port_t *port; + nxt_process_t *process; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -332,20 +302,17 @@ nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (msg->fd[0] != -1) { port->queue_fd = msg->fd[0]; - port->queue = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, - msg->fd[0], 0); + port->queue = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), + PROT_READ | PROT_WRITE, MAP_SHARED, msg->fd[0], 0); } nxt_port_send_new_port(task, rt, port, msg->port_msg.stream); } - void -nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_runtime_t *rt; - nxt_process_t *process; +nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_runtime_t *rt; + nxt_process_t *process; rt = task->thread->runtime; @@ -358,7 +325,7 @@ nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) process = nxt_runtime_process_find(rt, msg->port_msg.pid); if (nxt_slow_path(process == NULL)) { nxt_log(task, NXT_LOG_WARN, "failed to get process #%PI", - msg->port_msg.pid); + msg->port_msg.pid); goto fail_close; } @@ -370,19 +337,16 @@ nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_fd_close(msg->fd[0]); } - void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, nxt_uint_t slot, - nxt_fd_t fd) -{ - nxt_buf_t *b; - nxt_port_t *port; - nxt_process_t *process; + nxt_fd_t fd) { + nxt_buf_t *b; + nxt_port_t *port; + nxt_process_t *process; nxt_debug(task, "change log file #%ui fd:%FD", slot, fd); nxt_runtime_process_each(rt, process) { - if (nxt_pid == process->pid) { continue; } @@ -390,31 +354,29 @@ nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, nxt_uint_t slot, port = nxt_process_port_first(process); b = nxt_buf_mem_alloc(task->thread->engine->mem_pool, - sizeof(nxt_uint_t), 0); + sizeof(nxt_uint_t), 0); if (nxt_slow_path(b == NULL)) { continue; } b->mem.free = nxt_cpymem(b->mem.free, &slot, sizeof(nxt_uint_t)); - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_CHANGE_FILE, - fd, 0, 0, b); - - } nxt_runtime_process_loop; + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_CHANGE_FILE, fd, + 0, 0, b); + } + nxt_runtime_process_loop; } - void -nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_buf_t *b; +nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_buf_t *b; nxt_uint_t slot; - nxt_file_t *log_file; - nxt_runtime_t *rt; + nxt_file_t *log_file; + nxt_runtime_t *rt; rt = task->thread->runtime; - b = msg->buf; + b = msg->buf; slot = *(nxt_uint_t *) b->mem.pos; log_file = nxt_list_elt(rt->log_files, slot); @@ -433,14 +395,12 @@ nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - void -nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { size_t dump_size; - nxt_buf_t *b; + nxt_buf_t *b; - b = msg->buf; + b = msg->buf; dump_size = b->mem.free - b->mem.pos; if (dump_size > 300) { @@ -450,16 +410,14 @@ nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_debug(task, "data: %*s", dump_size, b->mem.pos); } - void -nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process) -{ - nxt_pid_t pid; - nxt_buf_t *buf; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_process_t *p; - nxt_process_type_t ptype; +nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process) { + nxt_pid_t pid; + nxt_buf_t *buf; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_process_t *p; + nxt_process_type_t ptype; pid = process->pid; @@ -468,11 +426,8 @@ nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process) rt = task->thread->runtime; nxt_runtime_process_each(rt, p) { - - if (p->pid == nxt_pid - || p->pid == pid - || nxt_queue_is_empty(&p->ports)) - { + if (p->pid == nxt_pid || p->pid == pid + || nxt_queue_is_empty(&p->ports)) { continue; } @@ -483,7 +438,7 @@ nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process) } buf = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, - sizeof(pid)); + sizeof(pid)); if (nxt_slow_path(buf == NULL)) { continue; @@ -492,17 +447,15 @@ nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process) buf->mem.free = nxt_cpymem(buf->mem.free, &pid, sizeof(pid)); nxt_port_socket_write(task, port, NXT_PORT_MSG_REMOVE_PID, -1, - process->stream, 0, buf); - - } nxt_runtime_process_loop; + process->stream, 0, buf); + } + nxt_runtime_process_loop; } - void -nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_pid_t pid; - nxt_buf_t *buf; + nxt_buf_t *buf; buf = msg->buf; @@ -513,13 +466,10 @@ nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_remove_pid(task, msg, pid); } - static void -nxt_port_remove_pid(nxt_task_t *task, nxt_port_recv_msg_t *msg, - nxt_pid_t pid) -{ - nxt_runtime_t *rt; - nxt_process_t *process; +nxt_port_remove_pid(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_pid_t pid) { + nxt_runtime_t *rt; + nxt_process_t *process; msg->u.removed_pid = pid; @@ -536,30 +486,25 @@ nxt_port_remove_pid(nxt_task_t *task, nxt_port_recv_msg_t *msg, } } - void -nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_debug(task, "port empty handler"); } - typedef struct { - nxt_work_t work; - nxt_port_t *port; - nxt_port_post_handler_t handler; + nxt_work_t work; + nxt_port_t *port; + nxt_port_post_handler_t handler; } nxt_port_work_t; - static void -nxt_port_post_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_port_t *port; - nxt_port_work_t *pw; - nxt_port_post_handler_t handler; - - pw = obj; - port = pw->port; +nxt_port_post_handler(nxt_task_t *task, void *obj, void *data) { + nxt_port_t *port; + nxt_port_work_t *pw; + nxt_port_post_handler_t handler; + + pw = obj; + port = pw->port; handler = pw->handler; nxt_free(pw); @@ -569,12 +514,10 @@ nxt_port_post_handler(nxt_task_t *task, void *obj, void *data) nxt_port_use(task, port, -1); } - nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, - nxt_port_post_handler_t handler, void *data) -{ - nxt_port_work_t *pw; + nxt_port_post_handler_t handler, void *data) { + nxt_port_work_t *pw; if (task->thread->engine == port->engine) { handler(task, port, data); @@ -591,11 +534,11 @@ nxt_port_post(nxt_task_t *task, nxt_port_t *port, nxt_atomic_fetch_add(&port->use_count, 1); pw->work.handler = nxt_port_post_handler; - pw->work.task = &port->engine->task; - pw->work.obj = pw; - pw->work.data = data; + pw->work.task = &port->engine->task; + pw->work.obj = pw; + pw->work.data = data; - pw->port = port; + pw->port = port; pw->handler = handler; nxt_event_engine_post(port->engine, &pw->work); @@ -603,23 +546,18 @@ nxt_port_post(nxt_task_t *task, nxt_port_t *port, return NXT_OK; } - static void -nxt_port_release_handler(nxt_task_t *task, nxt_port_t *port, void *data) -{ +nxt_port_release_handler(nxt_task_t *task, nxt_port_t *port, void *data) { /* no op */ } - void -nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i) -{ - int c; +nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i) { + int c; c = nxt_atomic_fetch_add(&port->use_count, i); if (i < 0 && c == -i) { - if (port->engine == NULL || task->thread->engine == port->engine) { nxt_port_release(task, port); diff --git a/src/nxt_port.h b/src/nxt_port.h index 772fb41ae..3e6e56fcd 100644 --- a/src/nxt_port.h +++ b/src/nxt_port.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,98 +6,95 @@ #ifndef _NXT_PORT_H_INCLUDED_ #define _NXT_PORT_H_INCLUDED_ - struct nxt_port_handlers_s { /* RPC responses. */ - nxt_port_handler_t rpc_ready; - nxt_port_handler_t rpc_error; + nxt_port_handler_t rpc_ready; + nxt_port_handler_t rpc_error; /* Main process RPC requests. */ - nxt_port_handler_t start_process; - nxt_port_handler_t socket; - nxt_port_handler_t socket_unlink; - nxt_port_handler_t modules; - nxt_port_handler_t conf_store; - nxt_port_handler_t cert_get; - nxt_port_handler_t cert_delete; - nxt_port_handler_t script_get; - nxt_port_handler_t script_delete; - nxt_port_handler_t access_log; + nxt_port_handler_t start_process; + nxt_port_handler_t socket; + nxt_port_handler_t socket_unlink; + nxt_port_handler_t modules; + nxt_port_handler_t conf_store; + nxt_port_handler_t cert_get; + nxt_port_handler_t cert_delete; + nxt_port_handler_t script_get; + nxt_port_handler_t script_delete; + nxt_port_handler_t access_log; /* File descriptor exchange. */ - nxt_port_handler_t change_file; - nxt_port_handler_t new_port; - nxt_port_handler_t get_port; - nxt_port_handler_t port_ack; - nxt_port_handler_t mmap; - nxt_port_handler_t get_mmap; + nxt_port_handler_t change_file; + nxt_port_handler_t new_port; + nxt_port_handler_t get_port; + nxt_port_handler_t port_ack; + nxt_port_handler_t mmap; + nxt_port_handler_t get_mmap; /* New process */ - nxt_port_handler_t process_created; - nxt_port_handler_t process_ready; - nxt_port_handler_t whoami; + nxt_port_handler_t process_created; + nxt_port_handler_t process_ready; + nxt_port_handler_t whoami; /* Process exit/crash notification. */ - nxt_port_handler_t remove_pid; + nxt_port_handler_t remove_pid; /* Stop process command. */ - nxt_port_handler_t quit; + nxt_port_handler_t quit; /* Request headers. */ - nxt_port_handler_t req_headers; - nxt_port_handler_t req_headers_ack; - nxt_port_handler_t req_body; + nxt_port_handler_t req_headers; + nxt_port_handler_t req_headers_ack; + nxt_port_handler_t req_body; /* Websocket frame. */ - nxt_port_handler_t websocket_frame; + nxt_port_handler_t websocket_frame; /* Various data. */ - nxt_port_handler_t data; - nxt_port_handler_t app_restart; + nxt_port_handler_t data; + nxt_port_handler_t app_restart; /* Status report. */ - nxt_port_handler_t status; + nxt_port_handler_t status; - nxt_port_handler_t oosm; - nxt_port_handler_t shm_ack; - nxt_port_handler_t read_queue; - nxt_port_handler_t read_socket; + nxt_port_handler_t oosm; + nxt_port_handler_t shm_ack; + nxt_port_handler_t read_queue; + nxt_port_handler_t read_socket; }; +#define nxt_port_handler_idx(name) \ + (offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t)) -#define nxt_port_handler_idx(name) \ - ( offsetof(nxt_port_handlers_t, name) / sizeof(nxt_port_handler_t) ) - -#define nxt_msg_last(handler) \ - (handler | NXT_PORT_MSG_LAST) +#define nxt_msg_last(handler) (handler | NXT_PORT_MSG_LAST) typedef enum { - NXT_PORT_MSG_LAST = 0x100, - NXT_PORT_MSG_CLOSE_FD = 0x200, - NXT_PORT_MSG_SYNC = 0x400, - - NXT_PORT_MSG_MASK = 0xFF, - - _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), - _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), - - _NXT_PORT_MSG_START_PROCESS = nxt_port_handler_idx(start_process), - _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), - _NXT_PORT_MSG_SOCKET_UNLINK = nxt_port_handler_idx(socket_unlink), - _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), - _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), - _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), - _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), - _NXT_PORT_MSG_SCRIPT_GET = nxt_port_handler_idx(script_get), - _NXT_PORT_MSG_SCRIPT_DELETE = nxt_port_handler_idx(script_delete), - _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), - - _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), - _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), - _NXT_PORT_MSG_GET_PORT = nxt_port_handler_idx(get_port), - _NXT_PORT_MSG_PORT_ACK = nxt_port_handler_idx(port_ack), - _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), - _NXT_PORT_MSG_GET_MMAP = nxt_port_handler_idx(get_mmap), + NXT_PORT_MSG_LAST = 0x100, + NXT_PORT_MSG_CLOSE_FD = 0x200, + NXT_PORT_MSG_SYNC = 0x400, + + NXT_PORT_MSG_MASK = 0xFF, + + _NXT_PORT_MSG_RPC_READY = nxt_port_handler_idx(rpc_ready), + _NXT_PORT_MSG_RPC_ERROR = nxt_port_handler_idx(rpc_error), + + _NXT_PORT_MSG_START_PROCESS = nxt_port_handler_idx(start_process), + _NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket), + _NXT_PORT_MSG_SOCKET_UNLINK = nxt_port_handler_idx(socket_unlink), + _NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules), + _NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store), + _NXT_PORT_MSG_CERT_GET = nxt_port_handler_idx(cert_get), + _NXT_PORT_MSG_CERT_DELETE = nxt_port_handler_idx(cert_delete), + _NXT_PORT_MSG_SCRIPT_GET = nxt_port_handler_idx(script_get), + _NXT_PORT_MSG_SCRIPT_DELETE = nxt_port_handler_idx(script_delete), + _NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log), + + _NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file), + _NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port), + _NXT_PORT_MSG_GET_PORT = nxt_port_handler_idx(get_port), + _NXT_PORT_MSG_PORT_ACK = nxt_port_handler_idx(port_ack), + _NXT_PORT_MSG_MMAP = nxt_port_handler_idx(mmap), + _NXT_PORT_MSG_GET_MMAP = nxt_port_handler_idx(get_mmap), _NXT_PORT_MSG_PROCESS_CREATED = nxt_port_handler_idx(process_created), _NXT_PORT_MSG_PROCESS_READY = nxt_port_handler_idx(process_ready), @@ -111,265 +107,278 @@ typedef enum { _NXT_PORT_MSG_REQ_BODY = nxt_port_handler_idx(req_body), _NXT_PORT_MSG_WEBSOCKET = nxt_port_handler_idx(websocket_frame), - _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), - _NXT_PORT_MSG_APP_RESTART = nxt_port_handler_idx(app_restart), - _NXT_PORT_MSG_STATUS = nxt_port_handler_idx(status), - - _NXT_PORT_MSG_OOSM = nxt_port_handler_idx(oosm), - _NXT_PORT_MSG_SHM_ACK = nxt_port_handler_idx(shm_ack), - _NXT_PORT_MSG_READ_QUEUE = nxt_port_handler_idx(read_queue), - _NXT_PORT_MSG_READ_SOCKET = nxt_port_handler_idx(read_socket), - - NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) - / sizeof(nxt_port_handler_t), - - NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, - NXT_PORT_MSG_RPC_READY_LAST = nxt_msg_last(_NXT_PORT_MSG_RPC_READY), - NXT_PORT_MSG_RPC_ERROR = nxt_msg_last(_NXT_PORT_MSG_RPC_ERROR), - NXT_PORT_MSG_START_PROCESS = nxt_msg_last(_NXT_PORT_MSG_START_PROCESS), - NXT_PORT_MSG_SOCKET = nxt_msg_last(_NXT_PORT_MSG_SOCKET), - NXT_PORT_MSG_SOCKET_UNLINK = nxt_msg_last(_NXT_PORT_MSG_SOCKET_UNLINK), - NXT_PORT_MSG_MODULES = nxt_msg_last(_NXT_PORT_MSG_MODULES), - NXT_PORT_MSG_CONF_STORE = nxt_msg_last(_NXT_PORT_MSG_CONF_STORE), - NXT_PORT_MSG_CERT_GET = nxt_msg_last(_NXT_PORT_MSG_CERT_GET), - NXT_PORT_MSG_CERT_DELETE = nxt_msg_last(_NXT_PORT_MSG_CERT_DELETE), - NXT_PORT_MSG_SCRIPT_GET = nxt_msg_last(_NXT_PORT_MSG_SCRIPT_GET), - NXT_PORT_MSG_SCRIPT_DELETE = nxt_msg_last(_NXT_PORT_MSG_SCRIPT_DELETE), - NXT_PORT_MSG_ACCESS_LOG = nxt_msg_last(_NXT_PORT_MSG_ACCESS_LOG), - NXT_PORT_MSG_CHANGE_FILE = nxt_msg_last(_NXT_PORT_MSG_CHANGE_FILE), - NXT_PORT_MSG_NEW_PORT = nxt_msg_last(_NXT_PORT_MSG_NEW_PORT), - NXT_PORT_MSG_GET_PORT = nxt_msg_last(_NXT_PORT_MSG_GET_PORT), - NXT_PORT_MSG_PORT_ACK = nxt_msg_last(_NXT_PORT_MSG_PORT_ACK), - NXT_PORT_MSG_MMAP = nxt_msg_last(_NXT_PORT_MSG_MMAP) - | NXT_PORT_MSG_SYNC, - NXT_PORT_MSG_GET_MMAP = nxt_msg_last(_NXT_PORT_MSG_GET_MMAP), - - NXT_PORT_MSG_PROCESS_CREATED = nxt_msg_last(_NXT_PORT_MSG_PROCESS_CREATED), - NXT_PORT_MSG_PROCESS_READY = nxt_msg_last(_NXT_PORT_MSG_PROCESS_READY), - NXT_PORT_MSG_WHOAMI = nxt_msg_last(_NXT_PORT_MSG_WHOAMI), - NXT_PORT_MSG_QUIT = nxt_msg_last(_NXT_PORT_MSG_QUIT), - NXT_PORT_MSG_REMOVE_PID = nxt_msg_last(_NXT_PORT_MSG_REMOVE_PID), - - NXT_PORT_MSG_REQ_HEADERS = _NXT_PORT_MSG_REQ_HEADERS, - NXT_PORT_MSG_REQ_BODY = _NXT_PORT_MSG_REQ_BODY, - NXT_PORT_MSG_WEBSOCKET = _NXT_PORT_MSG_WEBSOCKET, - NXT_PORT_MSG_WEBSOCKET_LAST = nxt_msg_last(_NXT_PORT_MSG_WEBSOCKET), - - NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, - NXT_PORT_MSG_DATA_LAST = nxt_msg_last(_NXT_PORT_MSG_DATA), - NXT_PORT_MSG_APP_RESTART = nxt_msg_last(_NXT_PORT_MSG_APP_RESTART), - NXT_PORT_MSG_STATUS = nxt_msg_last(_NXT_PORT_MSG_STATUS), - - NXT_PORT_MSG_OOSM = nxt_msg_last(_NXT_PORT_MSG_OOSM), - NXT_PORT_MSG_SHM_ACK = nxt_msg_last(_NXT_PORT_MSG_SHM_ACK), - NXT_PORT_MSG_READ_QUEUE = _NXT_PORT_MSG_READ_QUEUE, - NXT_PORT_MSG_READ_SOCKET = _NXT_PORT_MSG_READ_SOCKET, + _NXT_PORT_MSG_DATA = nxt_port_handler_idx(data), + _NXT_PORT_MSG_APP_RESTART = nxt_port_handler_idx(app_restart), + _NXT_PORT_MSG_STATUS = nxt_port_handler_idx(status), + + _NXT_PORT_MSG_OOSM = nxt_port_handler_idx(oosm), + _NXT_PORT_MSG_SHM_ACK = nxt_port_handler_idx(shm_ack), + _NXT_PORT_MSG_READ_QUEUE = nxt_port_handler_idx(read_queue), + _NXT_PORT_MSG_READ_SOCKET = nxt_port_handler_idx(read_socket), + + NXT_PORT_MSG_MAX = sizeof(nxt_port_handlers_t) / sizeof(nxt_port_handler_t), + + NXT_PORT_MSG_RPC_READY = _NXT_PORT_MSG_RPC_READY, + NXT_PORT_MSG_RPC_READY_LAST = nxt_msg_last(_NXT_PORT_MSG_RPC_READY), + NXT_PORT_MSG_RPC_ERROR = nxt_msg_last(_NXT_PORT_MSG_RPC_ERROR), + NXT_PORT_MSG_START_PROCESS = nxt_msg_last(_NXT_PORT_MSG_START_PROCESS), + NXT_PORT_MSG_SOCKET = nxt_msg_last(_NXT_PORT_MSG_SOCKET), + NXT_PORT_MSG_SOCKET_UNLINK = nxt_msg_last(_NXT_PORT_MSG_SOCKET_UNLINK), + NXT_PORT_MSG_MODULES = nxt_msg_last(_NXT_PORT_MSG_MODULES), + NXT_PORT_MSG_CONF_STORE = nxt_msg_last(_NXT_PORT_MSG_CONF_STORE), + NXT_PORT_MSG_CERT_GET = nxt_msg_last(_NXT_PORT_MSG_CERT_GET), + NXT_PORT_MSG_CERT_DELETE = nxt_msg_last(_NXT_PORT_MSG_CERT_DELETE), + NXT_PORT_MSG_SCRIPT_GET = nxt_msg_last(_NXT_PORT_MSG_SCRIPT_GET), + NXT_PORT_MSG_SCRIPT_DELETE = nxt_msg_last(_NXT_PORT_MSG_SCRIPT_DELETE), + NXT_PORT_MSG_ACCESS_LOG = nxt_msg_last(_NXT_PORT_MSG_ACCESS_LOG), + NXT_PORT_MSG_CHANGE_FILE = nxt_msg_last(_NXT_PORT_MSG_CHANGE_FILE), + NXT_PORT_MSG_NEW_PORT = nxt_msg_last(_NXT_PORT_MSG_NEW_PORT), + NXT_PORT_MSG_GET_PORT = nxt_msg_last(_NXT_PORT_MSG_GET_PORT), + NXT_PORT_MSG_PORT_ACK = nxt_msg_last(_NXT_PORT_MSG_PORT_ACK), + NXT_PORT_MSG_MMAP = nxt_msg_last(_NXT_PORT_MSG_MMAP) | NXT_PORT_MSG_SYNC, + NXT_PORT_MSG_GET_MMAP = nxt_msg_last(_NXT_PORT_MSG_GET_MMAP), + + NXT_PORT_MSG_PROCESS_CREATED = nxt_msg_last(_NXT_PORT_MSG_PROCESS_CREATED), + NXT_PORT_MSG_PROCESS_READY = nxt_msg_last(_NXT_PORT_MSG_PROCESS_READY), + NXT_PORT_MSG_WHOAMI = nxt_msg_last(_NXT_PORT_MSG_WHOAMI), + NXT_PORT_MSG_QUIT = nxt_msg_last(_NXT_PORT_MSG_QUIT), + NXT_PORT_MSG_REMOVE_PID = nxt_msg_last(_NXT_PORT_MSG_REMOVE_PID), + + NXT_PORT_MSG_REQ_HEADERS = _NXT_PORT_MSG_REQ_HEADERS, + NXT_PORT_MSG_REQ_BODY = _NXT_PORT_MSG_REQ_BODY, + NXT_PORT_MSG_WEBSOCKET = _NXT_PORT_MSG_WEBSOCKET, + NXT_PORT_MSG_WEBSOCKET_LAST = nxt_msg_last(_NXT_PORT_MSG_WEBSOCKET), + + NXT_PORT_MSG_DATA = _NXT_PORT_MSG_DATA, + NXT_PORT_MSG_DATA_LAST = nxt_msg_last(_NXT_PORT_MSG_DATA), + NXT_PORT_MSG_APP_RESTART = nxt_msg_last(_NXT_PORT_MSG_APP_RESTART), + NXT_PORT_MSG_STATUS = nxt_msg_last(_NXT_PORT_MSG_STATUS), + + NXT_PORT_MSG_OOSM = nxt_msg_last(_NXT_PORT_MSG_OOSM), + NXT_PORT_MSG_SHM_ACK = nxt_msg_last(_NXT_PORT_MSG_SHM_ACK), + NXT_PORT_MSG_READ_QUEUE = _NXT_PORT_MSG_READ_QUEUE, + NXT_PORT_MSG_READ_SOCKET = _NXT_PORT_MSG_READ_SOCKET, } nxt_port_msg_type_t; - /* Passed as a first iov chunk. */ typedef struct { - uint32_t stream; + uint32_t stream; - nxt_pid_t pid; /* not used on Linux and FreeBSD */ + nxt_pid_t pid; /* not used on Linux and FreeBSD */ - nxt_port_id_t reply_port; + nxt_port_id_t reply_port; - uint8_t type; + uint8_t type; /* Last message for this stream. */ - uint8_t last; /* 1 bit */ + uint8_t last; /* 1 bit */ /* Message data send using mmap, next chunk is a nxt_port_mmap_msg_t. */ - uint8_t mmap; /* 1 bit */ + uint8_t mmap; /* 1 bit */ /* Non-First fragment in fragmented message sequence. */ - uint8_t nf; /* 1 bit */ + uint8_t nf; /* 1 bit */ /* More Fragments followed. */ - uint8_t mf; /* 1 bit */ + uint8_t mf; /* 1 bit */ } nxt_port_msg_t; - typedef struct { - nxt_queue_link_t link; - nxt_buf_t *buf; - size_t share; - nxt_fd_t fd[2]; - nxt_port_msg_t port_msg; - uint8_t close_fd; /* 1 bit */ - uint8_t allocated; /* 1 bit */ + nxt_queue_link_t link; + nxt_buf_t *buf; + size_t share; + nxt_fd_t fd[2]; + nxt_port_msg_t port_msg; + uint8_t close_fd; /* 1 bit */ + uint8_t allocated; /* 1 bit */ } nxt_port_send_msg_t; #if (NXT_HAVE_UCRED) || (NXT_HAVE_MSGHDR_CMSGCRED) -#define NXT_USE_CMSG_PID 1 +#define NXT_USE_CMSG_PID 1 #endif struct nxt_port_recv_msg_s { - nxt_fd_t fd[2]; - nxt_buf_t *buf; - nxt_port_t *port; - nxt_port_msg_t port_msg; - size_t size; + nxt_fd_t fd[2]; + nxt_buf_t *buf; + nxt_port_t *port; + nxt_port_msg_t port_msg; + size_t size; #if (NXT_USE_CMSG_PID) - nxt_pid_t cmsg_pid; + nxt_pid_t cmsg_pid; #endif - nxt_bool_t cancelled; + nxt_bool_t cancelled; + union { - nxt_port_t *new_port; - nxt_pid_t removed_pid; - void *data; + nxt_port_t *new_port; + nxt_pid_t removed_pid; + void *data; } u; }; #if (NXT_USE_CMSG_PID) -#define nxt_recv_msg_cmsg_pid(msg) ((msg)->cmsg_pid) -#define nxt_recv_msg_cmsg_pid_ref(msg) (&(msg)->cmsg_pid) +#define nxt_recv_msg_cmsg_pid(msg) ((msg)->cmsg_pid) +#define nxt_recv_msg_cmsg_pid_ref(msg) (&(msg)->cmsg_pid) #else -#define nxt_recv_msg_cmsg_pid(msg) ((msg)->port_msg.pid) -#define nxt_recv_msg_cmsg_pid_ref(msg) (NULL) +#define nxt_recv_msg_cmsg_pid(msg) ((msg)->port_msg.pid) +#define nxt_recv_msg_cmsg_pid_ref(msg) (NULL) #endif -typedef struct nxt_app_s nxt_app_t; +typedef struct nxt_app_s nxt_app_t; struct nxt_port_s { - nxt_fd_event_t socket; + nxt_fd_event_t socket; - nxt_queue_link_t link; /* for nxt_process_t.ports */ - nxt_process_t *process; + nxt_queue_link_t link; /* for nxt_process_t.ports */ + nxt_process_t *process; - nxt_queue_link_t app_link; /* for nxt_app_t.ports */ - nxt_app_t *app; - nxt_port_t *main_app_port; + nxt_queue_link_t app_link; /* for nxt_app_t.ports */ + nxt_app_t *app; + nxt_port_t *main_app_port; - nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ - nxt_msec_t idle_start; + nxt_queue_link_t idle_link; /* for nxt_app_t.idle_ports */ + nxt_msec_t idle_start; - nxt_queue_t messages; /* of nxt_port_send_msg_t */ - nxt_thread_mutex_t write_mutex; + nxt_queue_t messages; /* of nxt_port_send_msg_t */ + nxt_thread_mutex_t write_mutex; /* Maximum size of message part. */ - uint32_t max_size; + uint32_t max_size; /* Maximum interleave of message parts. */ - uint32_t max_share; + uint32_t max_share; - uint32_t active_websockets; - uint32_t active_requests; + uint32_t active_websockets; + uint32_t active_requests; nxt_port_handler_t handler; - nxt_port_handler_t *data; + nxt_port_handler_t *data; - nxt_mp_t *mem_pool; - nxt_event_engine_t *engine; + nxt_mp_t *mem_pool; + nxt_event_engine_t *engine; - nxt_buf_t *free_bufs; - nxt_socket_t pair[2]; + nxt_buf_t *free_bufs; + nxt_socket_t pair[2]; - nxt_port_id_t id; - nxt_pid_t pid; + nxt_port_id_t id; + nxt_pid_t pid; - nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ - nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ + nxt_lvlhsh_t rpc_streams; /* stream to nxt_port_rpc_reg_t */ + nxt_lvlhsh_t rpc_peers; /* peer to queue of nxt_port_rpc_reg_t */ - nxt_lvlhsh_t frags; + nxt_lvlhsh_t frags; - nxt_atomic_t use_count; + nxt_atomic_t use_count; - nxt_process_type_t type; + nxt_process_type_t type; - nxt_fd_t queue_fd; - void *queue; + nxt_fd_t queue_fd; + void *queue; - void *socket_msg; - int from_socket; + void *socket_msg; + int from_socket; }; - typedef struct { - nxt_port_id_t id; - nxt_pid_t pid; - size_t max_size; - size_t max_share; - nxt_process_type_t type:8; + nxt_port_id_t id; + nxt_pid_t pid; + size_t max_size; + size_t max_share; + nxt_process_type_t type:8; } nxt_port_msg_new_port_t; - typedef struct { - nxt_port_id_t id; - nxt_pid_t pid; + nxt_port_id_t id; + nxt_pid_t pid; } nxt_port_msg_get_port_t; - typedef struct { - uint32_t id; + uint32_t id; } nxt_port_msg_get_mmap_t; - /* * nxt_port_data_t size is allocation size * which enables effective reuse of memory pool cache. */ typedef union { - nxt_buf_t buf; - nxt_port_msg_new_port_t new_port; + nxt_buf_t buf; + nxt_port_msg_new_port_t new_port; } nxt_port_data_t; +typedef void ( + *nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, void *data); -typedef void (*nxt_port_post_handler_t)(nxt_task_t *task, nxt_port_t *port, - void *data); - -nxt_port_t *nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, +nxt_port_t * +nxt_port_new(nxt_task_t *task, nxt_port_id_t id, nxt_pid_t pid, nxt_process_type_t type); -nxt_port_id_t nxt_port_get_next_id(void); -void nxt_port_reset_next_id(void); - -nxt_int_t nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, - size_t max_size); -void nxt_port_destroy(nxt_port_t *port); -void nxt_port_close(nxt_task_t *task, nxt_port_t *port); -void nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); -void nxt_port_write_close(nxt_port_t *port); -void nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); -void nxt_port_read_close(nxt_port_t *port); -nxt_int_t nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, - nxt_uint_t type, nxt_fd_t fd, nxt_fd_t fd2, uint32_t stream, - nxt_port_id_t reply_port, nxt_buf_t *b); +nxt_port_id_t +nxt_port_get_next_id(void); +void +nxt_port_reset_next_id(void); + +nxt_int_t +nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size); +void +nxt_port_destroy(nxt_port_t *port); +void +nxt_port_close(nxt_task_t *task, nxt_port_t *port); +void +nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port); +void +nxt_port_write_close(nxt_port_t *port); +void +nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port); +void +nxt_port_read_close(nxt_port_t *port); +nxt_int_t +nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, + nxt_fd_t fd, nxt_fd_t fd2, uint32_t stream, nxt_port_id_t reply_port, + nxt_buf_t *b); nxt_inline nxt_int_t -nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, - nxt_uint_t type, nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, - nxt_buf_t *b) -{ +nxt_port_socket_write(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, + nxt_fd_t fd, uint32_t stream, nxt_port_id_t reply_port, nxt_buf_t *b) { return nxt_port_socket_write2(task, port, type, fd, -1, stream, reply_port, - b); + b); } -void nxt_port_enable(nxt_task_t *task, nxt_port_t *port, +void +nxt_port_enable(nxt_task_t *task, nxt_port_t *port, const nxt_port_handlers_t *handlers); -nxt_int_t nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, - nxt_port_t *new_port, uint32_t stream); -void nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, - nxt_uint_t slot, nxt_fd_t fd); -void nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process); - -void nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_change_log_file_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -void nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); - -nxt_int_t nxt_port_post(nxt_task_t *task, nxt_port_t *port, +nxt_int_t +nxt_port_send_port(nxt_task_t *task, nxt_port_t *port, nxt_port_t *new_port, + uint32_t stream); +void +nxt_port_change_log_file(nxt_task_t *task, nxt_runtime_t *rt, nxt_uint_t slot, + nxt_fd_t fd); +void +nxt_port_remove_notify_others(nxt_task_t *task, nxt_process_t *process); + +void +nxt_port_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_process_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_change_log_file_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_empty_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); + +nxt_int_t +nxt_port_post(nxt_task_t *task, nxt_port_t *port, nxt_port_post_handler_t handler, void *data); -void nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); +void +nxt_port_use(nxt_task_t *task, nxt_port_t *port, int i); -nxt_inline void nxt_port_inc_use(nxt_port_t *port) -{ +nxt_inline void +nxt_port_inc_use(nxt_port_t *port) { nxt_atomic_fetch_add(&port->use_count, 1); } diff --git a/src/nxt_port_hash.c b/src/nxt_port_hash.c index b23acfb28..8c21d3c48 100644 --- a/src/nxt_port_hash.c +++ b/src/nxt_port_hash.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -6,98 +5,84 @@ #include - // Explicitly using 32 bit types to avoid possible alignment. typedef struct { - int32_t pid; - uint32_t port_id; + int32_t pid; + uint32_t port_id; } nxt_pid_port_id_t; - static nxt_int_t -nxt_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_port_t *port; - nxt_pid_port_id_t *pid_port_id; +nxt_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_port_t *port; + nxt_pid_port_id_t *pid_port_id; - port = data; + port = data; pid_port_id = (nxt_pid_port_id_t *) lhq->key.start; if (lhq->key.length == sizeof(nxt_pid_port_id_t) - && pid_port_id->pid == port->pid - && pid_port_id->port_id == port->id) - { + && pid_port_id->pid == port->pid && pid_port_id->port_id == port->id) { return NXT_OK; } return NXT_DECLINED; } -static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_port_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - nxt_port_t * -nxt_port_hash_retrieve(nxt_lvlhsh_t *port_hash) -{ +nxt_port_hash_retrieve(nxt_lvlhsh_t *port_hash) { return nxt_lvlhsh_retrieve(port_hash, &lvlhsh_ports_proto, NULL); } - nxt_inline void -nxt_port_hash_lhq(nxt_lvlhsh_query_t *lhq, nxt_pid_port_id_t *pid_port) -{ - lhq->key_hash = nxt_murmur_hash2(pid_port, sizeof(nxt_pid_port_id_t)); +nxt_port_hash_lhq(nxt_lvlhsh_query_t *lhq, nxt_pid_port_id_t *pid_port) { + lhq->key_hash = nxt_murmur_hash2(pid_port, sizeof(nxt_pid_port_id_t)); lhq->key.length = sizeof(nxt_pid_port_id_t); - lhq->key.start = (u_char *) pid_port; - lhq->proto = &lvlhsh_ports_proto; - lhq->pool = NULL; + lhq->key.start = (u_char *) pid_port; + lhq->proto = &lvlhsh_ports_proto; + lhq->pool = NULL; } - nxt_int_t -nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_port_t *port) -{ - nxt_int_t res; - nxt_pid_port_id_t pid_port; - nxt_lvlhsh_query_t lhq; +nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_port_t *port) { + nxt_int_t res; + nxt_pid_port_id_t pid_port; + nxt_lvlhsh_query_t lhq; - pid_port.pid = port->pid; + pid_port.pid = port->pid; pid_port.port_id = port->id; nxt_port_hash_lhq(&lhq, &pid_port); lhq.replace = 0; - lhq.value = port; + lhq.value = port; res = nxt_lvlhsh_insert(port_hash, &lhq); switch (res) { - case NXT_OK: break; default: nxt_thread_log_error(NXT_LOG_WARN, "port #%d for pid %PI add failed", - port->id, port->pid); + port->id, port->pid); break; } return res; } - nxt_int_t -nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_port_t *port) -{ - nxt_int_t res; - nxt_pid_port_id_t pid_port; - nxt_lvlhsh_query_t lhq; +nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_port_t *port) { + nxt_int_t res; + nxt_pid_port_id_t pid_port; + nxt_lvlhsh_query_t lhq; - pid_port.pid = port->pid; + pid_port.pid = port->pid; pid_port.port_id = port->id; nxt_port_hash_lhq(&lhq, &pid_port); @@ -105,28 +90,25 @@ nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_port_t *port) res = nxt_lvlhsh_delete(port_hash, &lhq); switch (res) { - case NXT_OK: break; default: nxt_thread_log_error(NXT_LOG_WARN, "port #%d for pid %PI remove failed", - port->id, port->pid); + port->id, port->pid); break; } return res; } - nxt_port_t * nxt_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_pid_t pid, - nxt_port_id_t port_id) -{ - nxt_pid_port_id_t pid_port; - nxt_lvlhsh_query_t lhq; + nxt_port_id_t port_id) { + nxt_pid_port_id_t pid_port; + nxt_lvlhsh_query_t lhq; - pid_port.pid = pid; + pid_port.pid = pid; pid_port.port_id = port_id; nxt_port_hash_lhq(&lhq, &pid_port); diff --git a/src/nxt_port_hash.h b/src/nxt_port_hash.h index c975cab71..bcb2c34c0 100644 --- a/src/nxt_port_hash.h +++ b/src/nxt_port_hash.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -11,14 +10,18 @@ #include -nxt_int_t nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_port_t *port); +nxt_int_t +nxt_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_port_t *port); -nxt_int_t nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_port_t *port); +nxt_int_t +nxt_port_hash_remove(nxt_lvlhsh_t *port_hash, nxt_port_t *port); -nxt_port_t *nxt_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_pid_t pid, +nxt_port_t * +nxt_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_pid_t pid, nxt_port_id_t port_id); -nxt_port_t *nxt_port_hash_retrieve(nxt_lvlhsh_t *port_hash); +nxt_port_t * +nxt_port_hash_retrieve(nxt_lvlhsh_t *port_hash); #endif /* _NXT_PORT_HASH_H_INCLIDED_ */ diff --git a/src/nxt_port_memory.c b/src/nxt_port_memory.c index ad6e97ade..1c7f6557c 100644 --- a/src/nxt_port_memory.c +++ b/src/nxt_port_memory.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -17,14 +16,12 @@ #include -static void nxt_port_broadcast_shm_ack(nxt_task_t *task, nxt_port_t *port, - void *data); - +static void +nxt_port_broadcast_shm_ack(nxt_task_t *task, nxt_port_t *port, void *data); nxt_inline void -nxt_port_mmap_handler_use(nxt_port_mmap_handler_t *mmap_handler, int i) -{ - int c; +nxt_port_mmap_handler_use(nxt_port_mmap_handler_t *mmap_handler, int i) { + int c; c = nxt_atomic_fetch_add(&mmap_handler->use_count, i); @@ -42,11 +39,9 @@ nxt_port_mmap_handler_use(nxt_port_mmap_handler_t *mmap_handler, int i) } } - static nxt_port_mmap_t * -nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i) -{ - uint32_t cap; +nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i) { + uint32_t cap; cap = port_mmaps->cap; @@ -55,7 +50,6 @@ nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i) } while (i + 1 > cap) { - if (cap < 16) { cap = cap * 2; @@ -65,15 +59,14 @@ nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i) } if (cap != port_mmaps->cap) { - - port_mmaps->elts = nxt_realloc(port_mmaps->elts, - cap * sizeof(nxt_port_mmap_t)); + port_mmaps->elts + = nxt_realloc(port_mmaps->elts, cap * sizeof(nxt_port_mmap_t)); if (nxt_slow_path(port_mmaps->elts == NULL)) { return NULL; } nxt_memzero(port_mmaps->elts + port_mmaps->cap, - sizeof(nxt_port_mmap_t) * (cap - port_mmaps->cap)); + sizeof(nxt_port_mmap_t) * (cap - port_mmaps->cap)); port_mmaps->cap = cap; } @@ -85,12 +78,10 @@ nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i) return port_mmaps->elts + i; } - void -nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts) -{ +nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts) { uint32_t i; - nxt_port_mmap_t *port_mmap; + nxt_port_mmap_t *port_mmap; if (port_mmaps == NULL) { return; @@ -109,21 +100,17 @@ nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts) } } - -#define nxt_port_mmap_free_junk(p, size) \ - memset((p), 0xA5, size) - +#define nxt_port_mmap_free_junk(p, size) memset((p), 0xA5, size) static void -nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - u_char *p; - nxt_mp_t *mp; - nxt_buf_t *b, *next; - nxt_process_t *process; +nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) { + u_char *p; + nxt_mp_t *mp; + nxt_buf_t *b, *next; + nxt_process_t *process; nxt_chunk_id_t c; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; if (nxt_buf_ts_handle(task, obj, data)) { return; @@ -140,8 +127,10 @@ nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) hdr = mmap_handler->hdr; if (nxt_slow_path(hdr->src_pid != nxt_pid && hdr->dst_pid != nxt_pid)) { - nxt_debug(task, "mmap buf completion: mmap for other process pair " - "%PI->%PI", hdr->src_pid, hdr->dst_pid); + nxt_debug(task, + "mmap buf completion: mmap for other process pair " + "%PI->%PI", + hdr->src_pid, hdr->dst_pid); goto release_buf; } @@ -162,9 +151,11 @@ nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_port_mmap_free_junk(p, b->mem.end - p); - nxt_debug(task, "mmap buf completion: %p [%p,%uz] (sent=%d), " - "%PI->%PI,%d,%d", b, b->mem.start, b->mem.end - b->mem.start, - b->is_port_mmap_sent, hdr->src_pid, hdr->dst_pid, hdr->id, c); + nxt_debug(task, + "mmap buf completion: %p [%p,%uz] (sent=%d), " + "%PI->%PI,%d,%d", + b, b->mem.start, b->mem.end - b->mem.start, b->is_port_mmap_sent, + hdr->src_pid, hdr->dst_pid, hdr->id, c); while (p < b->mem.end) { nxt_port_mmap_set_chunk_free(hdr->free_map, c); @@ -173,9 +164,7 @@ nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) c++; } - if (hdr->dst_pid == nxt_pid - && nxt_atomic_cmp_set(&hdr->oosm, 1, 0)) - { + if (hdr->dst_pid == nxt_pid && nxt_atomic_cmp_set(&hdr->oosm, 1, 0)) { process = nxt_runtime_process_find(task->thread->runtime, hdr->src_pid); nxt_process_broadcast_shm_ack(task, process); @@ -186,32 +175,29 @@ nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_port_mmap_handler_use(mmap_handler, -1); next = b->next; - mp = b->data; + mp = b->data; nxt_mp_free(mp, b); nxt_mp_release(mp); if (next != NULL) { - b = next; + b = next; mmap_handler = b->parent; goto complete_buf; } } - nxt_port_mmap_handler_t * nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, - nxt_fd_t fd) -{ - void *mem; + nxt_fd_t fd) { + void *mem; struct stat mmap_stat; - nxt_port_mmap_t *port_mmap; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_t *port_mmap; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; - nxt_debug(task, "got new mmap fd #%FD from process %PI", - fd, process->pid); + nxt_debug(task, "got new mmap fd #%FD from process %PI", fd, process->pid); port_mmap = NULL; @@ -221,8 +207,8 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, return NULL; } - mem = nxt_mem_mmap(NULL, mmap_stat.st_size, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + mem = nxt_mem_mmap(NULL, mmap_stat.st_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_log(task, NXT_LOG_WARN, "mmap() failed %E", nxt_errno); @@ -232,12 +218,12 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, hdr = mem; - if (nxt_slow_path(hdr->src_pid != process->pid - || hdr->dst_pid != nxt_pid)) - { - nxt_log(task, NXT_LOG_WARN, "unexpected pid in mmap header detected: " - "%PI != %PI or %PI != %PI", hdr->src_pid, process->pid, - hdr->dst_pid, nxt_pid); + if (nxt_slow_path( + hdr->src_pid != process->pid || hdr->dst_pid != nxt_pid)) { + nxt_log(task, NXT_LOG_WARN, + "unexpected pid in mmap header detected: " + "%PI != %PI or %PI != %PI", + hdr->src_pid, process->pid, hdr->dst_pid, nxt_pid); nxt_mem_munmap(mem, PORT_MMAP_SIZE); @@ -254,7 +240,7 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, } mmap_handler->hdr = hdr; - mmap_handler->fd = -1; + mmap_handler->fd = -1; nxt_thread_mutex_lock(&process->incoming.mutex); @@ -282,18 +268,16 @@ nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, return mmap_handler; } - static nxt_port_mmap_handler_t * nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps, - nxt_bool_t tracking, nxt_int_t n) -{ - void *mem; + nxt_bool_t tracking, nxt_int_t n) { + void *mem; nxt_fd_t fd; nxt_int_t i; - nxt_free_map_t *free_map; - nxt_port_mmap_t *port_mmap; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_free_map_t *free_map; + nxt_port_mmap_t *port_mmap; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t)); if (nxt_slow_path(mmap_handler == NULL)) { @@ -315,16 +299,16 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps, goto remove_fail; } - mem = nxt_mem_mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); + mem = nxt_mem_mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, + fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_fd_close(fd); goto remove_fail; } - mmap_handler->hdr = mem; - mmap_handler->fd = fd; + mmap_handler->hdr = mem; + mmap_handler->fd = fd; port_mmap->mmap_handler = mmap_handler; nxt_port_mmap_handler_use(mmap_handler, 1); @@ -334,8 +318,8 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_memset(hdr->free_map, 0xFFU, sizeof(hdr->free_map)); nxt_memset(hdr->free_tracking_map, 0xFFU, sizeof(hdr->free_tracking_map)); - hdr->id = mmaps->size - 1; - hdr->src_pid = nxt_pid; + hdr->id = mmaps->size - 1; + hdr->src_pid = nxt_pid; hdr->sent_over = 0xFFFFu; /* Mark first chunk as busy */ @@ -349,8 +333,8 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_port_mmap_set_chunk_busy(hdr->free_map, PORT_MMAP_CHUNK_COUNT); nxt_port_mmap_set_chunk_busy(hdr->free_tracking_map, PORT_MMAP_CHUNK_COUNT); - nxt_log(task, NXT_LOG_DEBUG, "new mmap #%D created for %PI -> ...", - hdr->id, nxt_pid); + nxt_log(task, NXT_LOG_DEBUG, "new mmap #%D created for %PI -> ...", hdr->id, + nxt_pid); return mmap_handler; @@ -363,18 +347,16 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps, return NULL; } - nxt_int_t -nxt_shm_open(nxt_task_t *task, size_t size) -{ - nxt_fd_t fd; +nxt_shm_open(nxt_task_t *task, size_t size) { + nxt_fd_t fd; #if (NXT_HAVE_MEMFD_CREATE || NXT_HAVE_SHM_OPEN) - u_char *p, name[64]; + u_char *p, name[64]; - p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD", - nxt_pid, nxt_random(&task->thread->random)); + p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD", + nxt_pid, nxt_random(&task->thread->random)); *p = '\0'; #endif @@ -418,7 +400,7 @@ nxt_shm_open(nxt_task_t *task, size_t size) if (nxt_slow_path(shm_unlink((char *) name) == -1)) { nxt_log(task, NXT_LOG_WARN, "shm_unlink(%s) failed %E", name, - nxt_errno); + nxt_errno); } #else @@ -438,17 +420,15 @@ nxt_shm_open(nxt_task_t *task, size_t size) return fd; } - static nxt_port_mmap_handler_t * nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c, - nxt_int_t n, nxt_bool_t tracking) -{ + nxt_int_t n, nxt_bool_t tracking) { nxt_int_t i, res, nchunks; - nxt_free_map_t *free_map; - nxt_port_mmap_t *port_mmap; - nxt_port_mmap_t *end_port_mmap; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_free_map_t *free_map; + nxt_port_mmap_t *port_mmap; + nxt_port_mmap_t *end_port_mmap; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; nxt_thread_mutex_lock(&mmaps->mutex); @@ -458,12 +438,9 @@ nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c, end_port_mmap = mmaps->elts + mmaps->size; - for (port_mmap = mmaps->elts; - port_mmap < end_port_mmap; - port_mmap++) - { + for (port_mmap = mmaps->elts; port_mmap < end_port_mmap; port_mmap++) { mmap_handler = port_mmap->mmap_handler; - hdr = mmap_handler->hdr; + hdr = mmap_handler->hdr; if (hdr->sent_over != 0xFFFFu) { continue; @@ -484,8 +461,8 @@ nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c, nxt_port_mmap_set_chunk_free(free_map, *c + i); } - *c += nchunks + 1; - nchunks = 0; + *c += nchunks + 1; + nchunks = 0; break; } @@ -504,7 +481,7 @@ nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c, end: - *c = 0; + *c = 0; mmap_handler = nxt_port_new_port_mmap(task, mmaps, tracking, n); unlock_return: @@ -514,12 +491,10 @@ nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c, return mmap_handler; } - static nxt_port_mmap_handler_t * -nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id) -{ - nxt_process_t *process; - nxt_port_mmap_handler_t *mmap_handler; +nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id) { + nxt_process_t *process; + nxt_port_mmap_handler_t *mmap_handler; process = nxt_runtime_process_find(task->thread->runtime, spid); if (nxt_slow_path(process == NULL)) { @@ -542,16 +517,14 @@ nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id) return mmap_handler; } - nxt_buf_t * -nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_mmaps_t *mmaps, size_t size) -{ - nxt_mp_t *mp; - nxt_buf_t *b; +nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_mmaps_t *mmaps, size_t size) { + nxt_mp_t *mp; + nxt_buf_t *b; nxt_int_t nchunks; nxt_chunk_id_t c; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; nxt_debug(task, "request %z bytes shm buffer", size); @@ -586,32 +559,29 @@ nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_mmaps_t *mmaps, size_t size) hdr = mmap_handler->hdr; b->mem.start = nxt_port_mmap_chunk_start(hdr, c); - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start; - b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start; + b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE; nxt_debug(task, "outgoing mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d", - b, b->mem.start, b->mem.end - b->mem.start, - hdr->src_pid, hdr->dst_pid, hdr->id, c); + b, b->mem.start, b->mem.end - b->mem.start, hdr->src_pid, hdr->dst_pid, + hdr->id, c); return b; } - nxt_int_t nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, - size_t min_size) -{ + size_t min_size) { size_t nchunks, free_size; nxt_chunk_id_t c, start; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; nxt_debug(task, "request increase %z bytes shm buffer", size); if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) { - nxt_log(task, NXT_LOG_WARN, - "failed to increase, not a mmap buffer"); + nxt_log(task, NXT_LOG_WARN, "failed to increase, not a mmap buffer"); return NXT_ERROR; } @@ -622,7 +592,7 @@ nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, } mmap_handler = b->parent; - hdr = mmap_handler->hdr; + hdr = mmap_handler->hdr; start = nxt_port_mmap_chunk_id(hdr, b->mem.end); @@ -634,7 +604,6 @@ nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, /* Try to acquire as much chunks as required. */ while (nchunks > 0) { - if (nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, c) == 0) { break; } @@ -644,8 +613,7 @@ nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, } if (nchunks != 0 - && min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start)) - { + && min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start)) { c--; while (c >= start) { nxt_port_mmap_set_chunk_free(hdr->free_map, c); @@ -663,18 +631,16 @@ nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, } } - static nxt_buf_t * nxt_port_mmap_get_incoming_buf(nxt_task_t *task, nxt_port_t *port, - nxt_pid_t spid, nxt_port_mmap_msg_t *mmap_msg) -{ + nxt_pid_t spid, nxt_port_mmap_msg_t *mmap_msg) { size_t nchunks; - nxt_buf_t *b; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_buf_t *b; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; - mmap_handler = nxt_port_get_port_incoming_mmap(task, spid, - mmap_msg->mmap_id); + mmap_handler + = nxt_port_get_port_incoming_mmap(task, spid, mmap_msg->mmap_id); if (nxt_slow_path(mmap_handler == NULL)) { return NULL; } @@ -696,42 +662,41 @@ nxt_port_mmap_get_incoming_buf(nxt_task_t *task, nxt_port_t *port, hdr = mmap_handler->hdr; b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id); - b->mem.pos = b->mem.start; - b->mem.free = b->mem.start + mmap_msg->size; - b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE; + b->mem.pos = b->mem.start; + b->mem.free = b->mem.start + mmap_msg->size; + b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE; b->parent = mmap_handler; nxt_port_mmap_handler_use(mmap_handler, 1); nxt_debug(task, "incoming mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d", - b, b->mem.start, b->mem.end - b->mem.start, - hdr->src_pid, hdr->dst_pid, hdr->id, mmap_msg->chunk_id); + b, b->mem.start, b->mem.end - b->mem.start, hdr->src_pid, hdr->dst_pid, + hdr->id, mmap_msg->chunk_id); return b; } - void nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port, - nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb, void *mmsg_buf) -{ + nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb, void *mmsg_buf) { size_t bsize; - nxt_buf_t *bmem; + nxt_buf_t *bmem; nxt_uint_t i; - nxt_port_mmap_msg_t *mmap_msg; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_msg_t *mmap_msg; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; - nxt_debug(task, "prepare %z bytes message for transfer to process %PI " - "via shared memory", sb->size, port->pid); + nxt_debug(task, + "prepare %z bytes message for transfer to process %PI " + "via shared memory", + sb->size, port->pid); - bsize = sb->niov * sizeof(nxt_port_mmap_msg_t); + bsize = sb->niov * sizeof(nxt_port_mmap_msg_t); mmap_msg = mmsg_buf; bmem = msg->buf; for (i = 0; i < sb->niov; i++, mmap_msg++) { - /* Lookup buffer which starts current iov_base. */ while (bmem && sb->iobuf[i].iov_base != bmem->mem.pos) { bmem = bmem->next; @@ -739,62 +704,57 @@ nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port, if (nxt_slow_path(bmem == NULL)) { nxt_log_error(NXT_LOG_ERR, task->log, - "failed to find buf for iobuf[%d]", i); + "failed to find buf for iobuf[%d]", i); return; /* TODO clear b and exit */ } mmap_handler = bmem->parent; - hdr = mmap_handler->hdr; + hdr = mmap_handler->hdr; - mmap_msg->mmap_id = hdr->id; + mmap_msg->mmap_id = hdr->id; mmap_msg->chunk_id = nxt_port_mmap_chunk_id(hdr, bmem->mem.pos); - mmap_msg->size = sb->iobuf[i].iov_len; + mmap_msg->size = sb->iobuf[i].iov_len; - nxt_debug(task, "mmap_msg={%D, %D, %D} to %PI", - mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size, - port->pid); + nxt_debug(task, "mmap_msg={%D, %D, %D} to %PI", mmap_msg->mmap_id, + mmap_msg->chunk_id, mmap_msg->size, port->pid); } sb->iobuf[0].iov_base = mmsg_buf; - sb->iobuf[0].iov_len = bsize; - sb->niov = 1; - sb->size = bsize; + sb->iobuf[0].iov_len = bsize; + sb->niov = 1; + sb->size = bsize; msg->port_msg.mmap = 1; } - void -nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_buf_t *b, **pb; - nxt_port_mmap_msg_t *end, *mmap_msg; +nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_buf_t *b, **pb; + nxt_port_mmap_msg_t *end, *mmap_msg; - pb = &msg->buf; + pb = &msg->buf; msg->size = 0; for (b = msg->buf; b != NULL; b = b->next) { - mmap_msg = (nxt_port_mmap_msg_t *) b->mem.pos; - end = (nxt_port_mmap_msg_t *) b->mem.free; + end = (nxt_port_mmap_msg_t *) b->mem.free; while (mmap_msg < end) { - nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI", - mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size, - msg->port_msg.pid); + nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI", mmap_msg->mmap_id, + mmap_msg->chunk_id, mmap_msg->size, msg->port_msg.pid); *pb = nxt_port_mmap_get_incoming_buf(task, msg->port, - msg->port_msg.pid, mmap_msg); + msg->port_msg.pid, mmap_msg); if (nxt_slow_path(*pb == NULL)) { nxt_log_error(NXT_LOG_ERR, task->log, - "failed to get mmap buffer"); + "failed to get mmap buffer"); break; } msg->size += mmap_msg->size; - pb = &(*pb)->next; + pb = &(*pb)->next; mmap_msg++; /* Mark original buf as complete. */ @@ -803,11 +763,9 @@ nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - nxt_port_method_t -nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b) -{ - nxt_port_method_t m; +nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b) { + nxt_port_method_t m; m = NXT_PORT_METHOD_ANY; @@ -820,8 +778,8 @@ nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b) if (nxt_buf_is_port_mmap(b)) { if (m == NXT_PORT_METHOD_PLAIN) { nxt_log_error(NXT_LOG_ERR, task->log, - "mixing plain and mmap buffers, " - "using plain mode"); + "mixing plain and mmap buffers, " + "using plain mode"); break; } @@ -834,8 +792,8 @@ nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b) } else { if (m == NXT_PORT_METHOD_MMAP) { nxt_log_error(NXT_LOG_ERR, task->log, - "mixing mmap and plain buffers, " - "switching to plain mode"); + "mixing mmap and plain buffers, " + "switching to plain mode"); m = NXT_PORT_METHOD_PLAIN; @@ -853,14 +811,11 @@ nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b) return m; } - void -nxt_process_broadcast_shm_ack(nxt_task_t *task, nxt_process_t *process) -{ - nxt_port_t *port; +nxt_process_broadcast_shm_ack(nxt_task_t *task, nxt_process_t *process) { + nxt_port_t *port; - if (nxt_slow_path(process == NULL || nxt_queue_is_empty(&process->ports))) - { + if (nxt_slow_path(process == NULL || nxt_queue_is_empty(&process->ports))) { return; } @@ -871,16 +826,15 @@ nxt_process_broadcast_shm_ack(nxt_task_t *task, nxt_process_t *process) } } - static void -nxt_port_broadcast_shm_ack(nxt_task_t *task, nxt_port_t *port, void *data) -{ - nxt_process_t *process; +nxt_port_broadcast_shm_ack(nxt_task_t *task, nxt_port_t *port, void *data) { + nxt_process_t *process; process = data; nxt_queue_each(port, &process->ports, nxt_port_t, link) { - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_SHM_ACK, - -1, 0, 0, NULL); - } nxt_queue_loop; + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_SHM_ACK, -1, 0, 0, + NULL); + } + nxt_queue_loop; } diff --git a/src/nxt_port_memory.h b/src/nxt_port_memory.h index f1e709646..da0b9934c 100644 --- a/src/nxt_port_memory.h +++ b/src/nxt_port_memory.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -10,10 +9,11 @@ #define PORT_MMAP_MIN_SIZE (3 * sizeof(uint32_t)) -typedef struct nxt_port_mmap_header_s nxt_port_mmap_header_t; +typedef struct nxt_port_mmap_header_s nxt_port_mmap_header_t; typedef struct nxt_port_mmap_handler_s nxt_port_mmap_handler_t; -void nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts); +void +nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts); /* * Allocates nxt_but_t structure from task's thread engine mem_pool, assigns @@ -23,8 +23,9 @@ void nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts); nxt_buf_t * nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_mmaps_t *mmaps, size_t size); -nxt_int_t nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, - size_t size, size_t min_size); +nxt_int_t +nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size, + size_t min_size); nxt_port_mmap_handler_t * nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process, @@ -48,8 +49,10 @@ typedef enum nxt_port_method_e nxt_port_method_t; nxt_port_method_t nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b); -nxt_int_t nxt_shm_open(nxt_task_t *task, size_t size); +nxt_int_t +nxt_shm_open(nxt_task_t *task, size_t size); -void nxt_process_broadcast_shm_ack(nxt_task_t *task, nxt_process_t *process); +void +nxt_process_broadcast_shm_ack(nxt_task_t *task, nxt_process_t *process); #endif /* _NXT_PORT_MEMORY_H_INCLUDED_ */ diff --git a/src/nxt_port_memory_int.h b/src/nxt_port_memory_int.h index 21a05b104..4b612876c 100644 --- a/src/nxt_port_memory_int.h +++ b/src/nxt_port_memory_int.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -14,54 +13,51 @@ #ifdef NXT_MMAP_TINY_CHUNK -#define PORT_MMAP_CHUNK_SIZE 16 -#define PORT_MMAP_HEADER_SIZE 1024 -#define PORT_MMAP_DATA_SIZE 1024 +#define PORT_MMAP_CHUNK_SIZE 16 +#define PORT_MMAP_HEADER_SIZE 1024 +#define PORT_MMAP_DATA_SIZE 1024 #else -#define PORT_MMAP_CHUNK_SIZE (1024 * 16) -#define PORT_MMAP_HEADER_SIZE (1024 * 4) -#define PORT_MMAP_DATA_SIZE (1024 * 1024 * 10) +#define PORT_MMAP_CHUNK_SIZE (1024 * 16) +#define PORT_MMAP_HEADER_SIZE (1024 * 4) +#define PORT_MMAP_DATA_SIZE (1024 * 1024 * 10) #endif -#define PORT_MMAP_SIZE (PORT_MMAP_HEADER_SIZE + PORT_MMAP_DATA_SIZE) -#define PORT_MMAP_CHUNK_COUNT (PORT_MMAP_DATA_SIZE / PORT_MMAP_CHUNK_SIZE) +#define PORT_MMAP_SIZE (PORT_MMAP_HEADER_SIZE + PORT_MMAP_DATA_SIZE) +#define PORT_MMAP_CHUNK_COUNT (PORT_MMAP_DATA_SIZE / PORT_MMAP_CHUNK_SIZE) -typedef uint32_t nxt_chunk_id_t; +typedef uint32_t nxt_chunk_id_t; -typedef nxt_atomic_uint_t nxt_free_map_t; +typedef nxt_atomic_uint_t nxt_free_map_t; #define FREE_BITS (sizeof(nxt_free_map_t) * 8) #define FREE_IDX(nchunk) ((nchunk) / FREE_BITS) -#define FREE_MASK(nchunk) \ - ( 1ULL << ( (nchunk) % FREE_BITS ) ) +#define FREE_MASK(nchunk) (1ULL << ((nchunk) % FREE_BITS)) #define MAX_FREE_IDX FREE_IDX(PORT_MMAP_CHUNK_COUNT) - /* Mapped at the start of shared memory segment. */ struct nxt_port_mmap_header_s { - uint32_t id; - nxt_pid_t src_pid; /* For sanity check. */ - nxt_pid_t dst_pid; /* For sanity check. */ - nxt_port_id_t sent_over; - nxt_atomic_t oosm; - nxt_free_map_t free_map[MAX_FREE_IDX]; - nxt_free_map_t free_map_padding; - nxt_free_map_t free_tracking_map[MAX_FREE_IDX]; - nxt_free_map_t free_tracking_map_padding; - nxt_atomic_t tracking[PORT_MMAP_CHUNK_COUNT]; + uint32_t id; + nxt_pid_t src_pid; /* For sanity check. */ + nxt_pid_t dst_pid; /* For sanity check. */ + nxt_port_id_t sent_over; + nxt_atomic_t oosm; + nxt_free_map_t free_map[MAX_FREE_IDX]; + nxt_free_map_t free_map_padding; + nxt_free_map_t free_tracking_map[MAX_FREE_IDX]; + nxt_free_map_t free_tracking_map_padding; + nxt_atomic_t tracking[PORT_MMAP_CHUNK_COUNT]; }; - struct nxt_port_mmap_handler_s { - nxt_port_mmap_header_t *hdr; + nxt_port_mmap_header_t *hdr; nxt_atomic_t use_count; nxt_fd_t fd; }; @@ -71,23 +67,22 @@ struct nxt_port_mmap_handler_s { * descriptor. */ struct nxt_port_mmap_s { - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_handler_t *mmap_handler; }; typedef struct nxt_port_mmap_msg_s nxt_port_mmap_msg_t; /* Passed as a second iov chunk when 'mmap' bit in nxt_port_msg_t is 1. */ struct nxt_port_mmap_msg_s { - uint32_t mmap_id; /* Mmap index in nxt_process_t.outgoing. */ - nxt_chunk_id_t chunk_id; /* Mmap chunk index. */ - uint32_t size; /* Payload data size. */ + uint32_t mmap_id; /* Mmap index in nxt_process_t.outgoing. */ + nxt_chunk_id_t chunk_id; /* Mmap chunk index. */ + uint32_t size; /* Payload data size. */ }; - nxt_inline nxt_bool_t nxt_port_mmap_get_free_chunk(nxt_free_map_t *m, nxt_chunk_id_t *c); -#define nxt_port_mmap_get_chunk_busy(m, c) \ +#define nxt_port_mmap_get_chunk_busy(m, c) \ ((m[FREE_IDX(c)] & FREE_MASK(c)) == 0) nxt_inline void @@ -100,39 +95,34 @@ nxt_inline void nxt_port_mmap_set_chunk_free(nxt_free_map_t *m, nxt_chunk_id_t c); nxt_inline nxt_chunk_id_t -nxt_port_mmap_chunk_id(nxt_port_mmap_header_t *hdr, const u_char *p) -{ - u_char *mm_start; +nxt_port_mmap_chunk_id(nxt_port_mmap_header_t *hdr, const u_char *p) { + u_char *mm_start; mm_start = (u_char *) hdr; return ((p - mm_start) - PORT_MMAP_HEADER_SIZE) / PORT_MMAP_CHUNK_SIZE; } - nxt_inline u_char * -nxt_port_mmap_chunk_start(nxt_port_mmap_header_t *hdr, nxt_chunk_id_t c) -{ - u_char *mm_start; +nxt_port_mmap_chunk_start(nxt_port_mmap_header_t *hdr, nxt_chunk_id_t c) { + u_char *mm_start; mm_start = (u_char *) hdr; return mm_start + PORT_MMAP_HEADER_SIZE + c * PORT_MMAP_CHUNK_SIZE; } - nxt_inline nxt_bool_t -nxt_port_mmap_get_free_chunk(nxt_free_map_t *m, nxt_chunk_id_t *c) -{ - const nxt_free_map_t default_mask = (nxt_free_map_t) -1; +nxt_port_mmap_get_free_chunk(nxt_free_map_t *m, nxt_chunk_id_t *c) { + const nxt_free_map_t default_mask = (nxt_free_map_t) -1; - int ffs; - size_t i, start; - nxt_chunk_id_t chunk; - nxt_free_map_t bits, mask; + int ffs; + size_t i, start; + nxt_chunk_id_t chunk; + nxt_free_map_t bits, mask; start = FREE_IDX(*c); - mask = default_mask << ((*c) % FREE_BITS); + mask = default_mask << ((*c) % FREE_BITS); for (i = start; i < MAX_FREE_IDX; i++) { bits = m[i] & mask; @@ -156,24 +146,19 @@ nxt_port_mmap_get_free_chunk(nxt_free_map_t *m, nxt_chunk_id_t *c) return 0; } - nxt_inline void -nxt_port_mmap_set_chunk_busy(nxt_free_map_t *m, nxt_chunk_id_t c) -{ +nxt_port_mmap_set_chunk_busy(nxt_free_map_t *m, nxt_chunk_id_t c) { nxt_atomic_and_fetch(m + FREE_IDX(c), ~FREE_MASK(c)); } - nxt_inline nxt_bool_t -nxt_port_mmap_chk_set_chunk_busy(nxt_free_map_t *m, nxt_chunk_id_t c) -{ - nxt_free_map_t *f; +nxt_port_mmap_chk_set_chunk_busy(nxt_free_map_t *m, nxt_chunk_id_t c) { + nxt_free_map_t *f; nxt_free_map_t free_val, busy_val; f = m + FREE_IDX(c); - while ( (*f & FREE_MASK(c)) != 0 ) { - + while ((*f & FREE_MASK(c)) != 0) { free_val = *f | FREE_MASK(c); busy_val = free_val & ~FREE_MASK(c); @@ -185,10 +170,8 @@ nxt_port_mmap_chk_set_chunk_busy(nxt_free_map_t *m, nxt_chunk_id_t c) return 0; } - nxt_inline void -nxt_port_mmap_set_chunk_free(nxt_free_map_t *m, nxt_chunk_id_t c) -{ +nxt_port_mmap_set_chunk_free(nxt_free_map_t *m, nxt_chunk_id_t c) { nxt_atomic_or_fetch(m + FREE_IDX(c), FREE_MASK(c)); } diff --git a/src/nxt_port_queue.h b/src/nxt_port_queue.h index d2b2326bf..482c6ecf6 100644 --- a/src/nxt_port_queue.h +++ b/src/nxt_port_queue.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,28 +11,24 @@ /* Using Numeric Naive Circular Queue as a backend. */ -#define NXT_PORT_QUEUE_SIZE NXT_NNCQ_SIZE -#define NXT_PORT_QUEUE_MSG_SIZE 31 - +#define NXT_PORT_QUEUE_SIZE NXT_NNCQ_SIZE +#define NXT_PORT_QUEUE_MSG_SIZE 31 typedef struct { - uint8_t size; - uint8_t data[NXT_PORT_QUEUE_MSG_SIZE]; + uint8_t size; + uint8_t data[NXT_PORT_QUEUE_MSG_SIZE]; } nxt_port_queue_item_t; - typedef struct { - nxt_nncq_atomic_t nitems; - nxt_nncq_t free_items; - nxt_nncq_t queue; - nxt_port_queue_item_t items[NXT_PORT_QUEUE_SIZE]; + nxt_nncq_atomic_t nitems; + nxt_nncq_t free_items; + nxt_nncq_t queue; + nxt_port_queue_item_t items[NXT_PORT_QUEUE_SIZE]; } nxt_port_queue_t; - nxt_inline void -nxt_port_queue_init(nxt_port_queue_t volatile *q) -{ - nxt_nncq_atomic_t i; +nxt_port_queue_init(nxt_port_queue_t volatile *q) { + nxt_nncq_atomic_t i; nxt_nncq_init(&q->free_items); nxt_nncq_init(&q->queue); @@ -45,13 +40,11 @@ nxt_port_queue_init(nxt_port_queue_t volatile *q) q->nitems = 0; } - nxt_inline nxt_int_t nxt_port_queue_send(nxt_port_queue_t volatile *q, const void *p, uint8_t size, - int *notify) -{ + int *notify) { nxt_nncq_atomic_t i; - nxt_port_queue_item_t *qi; + nxt_port_queue_item_t *qi; i = nxt_nncq_dequeue(&q->free_items); if (i == nxt_nncq_empty(&q->free_items)) { @@ -73,13 +66,11 @@ nxt_port_queue_send(nxt_port_queue_t volatile *q, const void *p, uint8_t size, return NXT_OK; } - nxt_inline ssize_t -nxt_port_queue_recv(nxt_port_queue_t volatile *q, void *p) -{ +nxt_port_queue_recv(nxt_port_queue_t volatile *q, void *p) { ssize_t res; nxt_nncq_atomic_t i; - nxt_port_queue_item_t *qi; + nxt_port_queue_item_t *qi; i = nxt_nncq_dequeue(&q->queue); if (i == nxt_nncq_empty(&q->queue)) { diff --git a/src/nxt_port_rpc.c b/src/nxt_port_rpc.c index 285909335..92bee4293 100644 --- a/src/nxt_port_rpc.c +++ b/src/nxt_port_rpc.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -8,96 +7,84 @@ #include -static volatile uint32_t *nxt_stream_ident; +static volatile uint32_t *nxt_stream_ident; typedef struct nxt_port_rpc_reg_s nxt_port_rpc_reg_t; struct nxt_port_rpc_reg_s { - uint32_t stream; + uint32_t stream; - nxt_pid_t peer; - nxt_queue_link_t link; - nxt_bool_t link_first; + nxt_pid_t peer; + nxt_queue_link_t link; + nxt_bool_t link_first; - nxt_port_rpc_handler_t ready_handler; - nxt_port_rpc_handler_t error_handler; - void *data; + nxt_port_rpc_handler_t ready_handler; + nxt_port_rpc_handler_t error_handler; + void *data; }; - static void nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_reg_t *reg); - nxt_int_t -nxt_port_rpc_init(void) -{ - void *p; +nxt_port_rpc_init(void) { + void *p; if (nxt_stream_ident != NULL) { return NXT_OK; } p = nxt_mem_mmap(NULL, sizeof(*nxt_stream_ident), PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, -1, 0); + MAP_ANON | MAP_SHARED, -1, 0); if (nxt_slow_path(p == MAP_FAILED)) { return NXT_ERROR; } - nxt_stream_ident = p; + nxt_stream_ident = p; *nxt_stream_ident = 1; return NXT_OK; } - static nxt_int_t -nxt_rpc_reg_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_rpc_reg_test(nxt_lvlhsh_query_t *lhq, void *data) { return NXT_OK; } - -static const nxt_lvlhsh_proto_t lvlhsh_rpc_reg_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_rpc_reg_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_rpc_reg_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - nxt_inline void -nxt_port_rpc_lhq_stream(nxt_lvlhsh_query_t *lhq, uint32_t *stream) -{ - lhq->key_hash = nxt_murmur_hash2(stream, sizeof(*stream)); +nxt_port_rpc_lhq_stream(nxt_lvlhsh_query_t *lhq, uint32_t *stream) { + lhq->key_hash = nxt_murmur_hash2(stream, sizeof(*stream)); lhq->key.length = sizeof(*stream); - lhq->key.start = (u_char *) stream; - lhq->proto = &lvlhsh_rpc_reg_proto; + lhq->key.start = (u_char *) stream; + lhq->proto = &lvlhsh_rpc_reg_proto; } - nxt_inline void -nxt_port_rpc_lhq_peer(nxt_lvlhsh_query_t *lhq, nxt_pid_t *peer) -{ - lhq->key_hash = nxt_murmur_hash2(peer, sizeof(*peer)); +nxt_port_rpc_lhq_peer(nxt_lvlhsh_query_t *lhq, nxt_pid_t *peer) { + lhq->key_hash = nxt_murmur_hash2(peer, sizeof(*peer)); lhq->key.length = sizeof(*peer); - lhq->key.start = (u_char *) peer; - lhq->proto = &lvlhsh_rpc_reg_proto; + lhq->key.start = (u_char *) peer; + lhq->proto = &lvlhsh_rpc_reg_proto; } - uint32_t nxt_port_rpc_register_handler(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler, - nxt_pid_t peer, void *data) -{ - void *ex; - nxt_port_rpc_reg_t *reg; + nxt_pid_t peer, void *data) { + void *ex; + nxt_port_rpc_reg_t *reg; ex = nxt_port_rpc_register_handler_ex(task, port, ready_handler, - error_handler, 0); + error_handler, 0); if (ex == NULL) { return 0; @@ -116,14 +103,12 @@ nxt_port_rpc_register_handler(nxt_task_t *task, nxt_port_t *port, return reg->stream; } - void * nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler, - size_t ex_size) -{ + size_t ex_size) { uint32_t stream; - nxt_port_rpc_reg_t *reg; + nxt_port_rpc_reg_t *reg; nxt_lvlhsh_query_t lhq; nxt_assert(port->pair[0] != -1); @@ -138,25 +123,26 @@ nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port, return NULL; } - reg->stream = stream; - reg->peer = -1; + reg->stream = stream; + reg->peer = -1; reg->ready_handler = ready_handler; reg->error_handler = error_handler; - reg->data = reg + 1; + reg->data = reg + 1; nxt_port_rpc_lhq_stream(&lhq, &stream); lhq.replace = 0; - lhq.value = reg; - lhq.pool = port->mem_pool; + lhq.value = reg; + lhq.pool = port->mem_pool; switch (nxt_lvlhsh_insert(&port->rpc_streams, &lhq)) { - case NXT_OK: break; default: - nxt_log_error(NXT_LOG_ERR, task->log, "rpc: stream #%uD failed to add " - "reg ", stream); + nxt_log_error(NXT_LOG_ERR, task->log, + "rpc: stream #%uD failed to add " + "reg ", + stream); nxt_mp_free(port->mem_pool, reg); @@ -170,11 +156,9 @@ nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port, return reg->data; } - uint32_t -nxt_port_rpc_ex_stream(void *ex) -{ - nxt_port_rpc_reg_t *reg; +nxt_port_rpc_ex_stream(void *ex) { + nxt_port_rpc_reg_t *reg; reg = nxt_pointer_to(ex, -sizeof(nxt_port_rpc_reg_t)); @@ -183,14 +167,12 @@ nxt_port_rpc_ex_stream(void *ex) return reg->stream; } - void -nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port, - void *ex, nxt_pid_t peer) -{ +nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port, void *ex, + nxt_pid_t peer) { nxt_int_t ret; - nxt_queue_link_t *peer_link; - nxt_port_rpc_reg_t *reg; + nxt_queue_link_t *peer_link; + nxt_port_rpc_reg_t *reg; nxt_lvlhsh_query_t lhq; reg = nxt_pointer_to(ex, -sizeof(nxt_port_rpc_reg_t)); @@ -215,49 +197,47 @@ nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_lhq_peer(&lhq, &peer); lhq.replace = 0; - lhq.value = ®->link; - lhq.pool = port->mem_pool; + lhq.value = ®->link; + lhq.pool = port->mem_pool; ret = nxt_lvlhsh_insert(&port->rpc_peers, &lhq); switch (ret) { - case NXT_OK: reg->link_first = 1; nxt_queue_self(®->link); nxt_debug(task, "rpc: stream #%uD assigned uniq pid %PI (%p)", - reg->stream, reg->peer, reg->link.next); + reg->stream, reg->peer, reg->link.next); break; case NXT_DECLINED: reg->link_first = 0; - peer_link = lhq.value; + peer_link = lhq.value; nxt_queue_insert_after(peer_link, ®->link); nxt_debug(task, "rpc: stream #%uD assigned duplicate pid %PI (%p)", - reg->stream, reg->peer, reg->link.next); + reg->stream, reg->peer, reg->link.next); break; default: - nxt_log_error(NXT_LOG_ERR, task->log, "rpc: failed to add " - "peer for stream #%uD (%d)", reg->stream, ret); + nxt_log_error(NXT_LOG_ERR, task->log, + "rpc: failed to add " + "peer for stream #%uD (%d)", + reg->stream, ret); reg->peer = -1; break; } - } - static void nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port, - nxt_port_rpc_reg_t *reg) -{ + nxt_port_rpc_reg_t *reg) { uint32_t stream; nxt_int_t ret; nxt_lvlhsh_query_t lhq; - nxt_port_rpc_reg_t *r; + nxt_port_rpc_reg_t *r; stream = reg->stream; @@ -268,17 +248,21 @@ nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port, if (reg->link.next == ®->link) { nxt_assert(reg->link.prev == ®->link); - nxt_debug(task, "rpc: stream #%uD remove first and last pid %PI " - "registration (%p)", stream, reg->peer, reg->link.next); + nxt_debug(task, + "rpc: stream #%uD remove first and last pid %PI " + "registration (%p)", + stream, reg->peer, reg->link.next); ret = nxt_lvlhsh_delete(&port->rpc_peers, &lhq); } else { - nxt_debug(task, "rpc: stream #%uD remove first pid %PI " - "registration (%p)", stream, reg->peer, reg->link.next); + nxt_debug(task, + "rpc: stream #%uD remove first pid %PI " + "registration (%p)", + stream, reg->peer, reg->link.next); lhq.replace = 1; - lhq.value = reg->link.next; + lhq.value = reg->link.next; r = nxt_queue_link_data(reg->link.next, nxt_port_rpc_reg_t, link); r->link_first = 1; @@ -289,35 +273,37 @@ nxt_port_rpc_remove_from_peers(nxt_task_t *task, nxt_port_t *port, } } else { - nxt_debug(task, "rpc: stream #%uD remove pid %PI " - "registration (%p)", stream, reg->peer, reg->link.next); + nxt_debug(task, + "rpc: stream #%uD remove pid %PI " + "registration (%p)", + stream, reg->peer, reg->link.next); nxt_queue_remove(®->link); ret = NXT_OK; } if (nxt_slow_path(ret != NXT_OK)) { - nxt_log_error(NXT_LOG_ERR, task->log, "rpc: stream #%uD failed" - " to delete peer %PI (%d)", stream, reg->peer, ret); + nxt_log_error(NXT_LOG_ERR, task->log, + "rpc: stream #%uD failed" + " to delete peer %PI (%d)", + stream, reg->peer, ret); } } - void -nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - uint8_t last; - uint32_t stream; - nxt_int_t ret; - nxt_port_t *port; - nxt_port_rpc_reg_t *reg; - nxt_lvlhsh_query_t lhq; - nxt_port_msg_type_t type; +nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + uint8_t last; + uint32_t stream; + nxt_int_t ret; + nxt_port_t *port; + nxt_port_rpc_reg_t *reg; + nxt_lvlhsh_query_t lhq; + nxt_port_msg_type_t type; stream = msg->port_msg.stream; - port = msg->port; - last = msg->port_msg.last; - type = msg->port_msg.type; + port = msg->port; + last = msg->port_msg.last; + type = msg->port_msg.type; nxt_port_rpc_lhq_stream(&lhq, &stream); lhq.pool = port->mem_pool; @@ -336,7 +322,7 @@ nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } nxt_debug(task, "rpc: stream #%uD %shandler, type %d", stream, - (last ? "last " : ""), type); + (last ? "last " : ""), type); reg = lhq.value; @@ -362,18 +348,16 @@ nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_use(task, port, -1); } - void -nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) -{ - uint8_t last; - uint32_t stream; - nxt_int_t ret; - nxt_buf_t buf; - nxt_queue_link_t *peer_link, *next_link; - nxt_port_rpc_reg_t *reg; - nxt_lvlhsh_query_t lhq; - nxt_port_recv_msg_t msg; +nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) { + uint8_t last; + uint32_t stream; + nxt_int_t ret; + nxt_buf_t buf; + nxt_queue_link_t *peer_link, *next_link; + nxt_port_rpc_reg_t *reg; + nxt_lvlhsh_query_t lhq; + nxt_port_recv_msg_t msg; nxt_port_rpc_lhq_peer(&lhq, &peer); lhq.pool = port->mem_pool; @@ -389,19 +373,18 @@ nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) nxt_memzero(&msg, sizeof(msg)); nxt_memzero(&buf, sizeof(buf)); - msg.fd[0] = -1; - msg.fd[1] = -1; - msg.buf = &buf; - msg.port = port; + msg.fd[0] = -1; + msg.fd[1] = -1; + msg.buf = &buf; + msg.port = port; msg.u.removed_pid = peer; - msg.port_msg.pid = nxt_pid; + msg.port_msg.pid = nxt_pid; msg.port_msg.type = _NXT_PORT_MSG_REMOVE_PID; peer_link = lhq.value; - last = 0; + last = 0; while (last == 0) { - reg = nxt_queue_link_data(peer_link, nxt_port_rpc_reg_t, link); nxt_assert(reg->peer == peer); @@ -411,7 +394,7 @@ nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) nxt_debug(task, "rpc: stream #%uD trigger error", stream); msg.port_msg.stream = stream; - msg.port_msg.last = 1; + msg.port_msg.last = 1; if (peer_link == peer_link->next) { nxt_assert(peer_link->prev == peer_link); @@ -444,7 +427,7 @@ nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) if (nxt_slow_path(ret != NXT_OK)) { nxt_log_error(NXT_LOG_ERR, task->log, - "rpc: stream #%uD failed to delete handler", stream); + "rpc: stream #%uD failed to delete handler", stream); return; } @@ -455,12 +438,10 @@ nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer) } } - void -nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream) -{ +nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream) { nxt_int_t ret; - nxt_port_rpc_reg_t *reg; + nxt_port_rpc_reg_t *reg; nxt_lvlhsh_query_t lhq; nxt_port_rpc_lhq_stream(&lhq, &stream); @@ -487,34 +468,33 @@ nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream) nxt_port_use(task, port, -1); } -static nxt_buf_t nxt_port_close_dummy_buf; +static nxt_buf_t nxt_port_close_dummy_buf; void -nxt_port_rpc_close(nxt_task_t *task, nxt_port_t *port) -{ - nxt_port_rpc_reg_t *reg; - nxt_port_recv_msg_t msg; +nxt_port_rpc_close(nxt_task_t *task, nxt_port_t *port) { + nxt_port_rpc_reg_t *reg; + nxt_port_recv_msg_t msg; - for ( ;; ) { + for (;;) { reg = nxt_lvlhsh_peek(&port->rpc_streams, &lvlhsh_rpc_reg_proto); if (reg == NULL) { return; } - msg.fd[0] = -1; - msg.fd[1] = -1; - msg.buf = &nxt_port_close_dummy_buf; - msg.port = port; + msg.fd[0] = -1; + msg.fd[1] = -1; + msg.buf = &nxt_port_close_dummy_buf; + msg.port = port; msg.port_msg.stream = reg->stream; - msg.port_msg.pid = nxt_pid; - msg.port_msg.type = _NXT_PORT_MSG_RPC_ERROR; - msg.port_msg.last = 1; - msg.port_msg.mmap = 0; - msg.port_msg.nf = 0; - msg.port_msg.mf = 0; - msg.size = 0; - msg.cancelled = 0; - msg.u.data = NULL; + msg.port_msg.pid = nxt_pid; + msg.port_msg.type = _NXT_PORT_MSG_RPC_ERROR; + msg.port_msg.last = 1; + msg.port_msg.mmap = 0; + msg.port_msg.nf = 0; + msg.port_msg.mf = 0; + msg.size = 0; + msg.cancelled = 0; + msg.u.data = NULL; nxt_port_rpc_handler(task, &msg); } diff --git a/src/nxt_port_rpc.h b/src/nxt_port_rpc.h index c07683fb0..c5b4e4c20 100644 --- a/src/nxt_port_rpc.h +++ b/src/nxt_port_rpc.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) NGINX, Inc. @@ -11,25 +10,32 @@ typedef void (*nxt_port_rpc_handler_t)(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -nxt_int_t nxt_port_rpc_init(void); +nxt_int_t +nxt_port_rpc_init(void); -uint32_t nxt_port_rpc_register_handler(nxt_task_t *task, nxt_port_t *port, +uint32_t +nxt_port_rpc_register_handler(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler, nxt_pid_t peer, void *data); -void *nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port, +void * +nxt_port_rpc_register_handler_ex(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_handler_t ready_handler, nxt_port_rpc_handler_t error_handler, size_t ex_size); -uint32_t nxt_port_rpc_ex_stream(void *ex); -void nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port, - void *ex, nxt_pid_t peer); - -void nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, +uint32_t +nxt_port_rpc_ex_stream(void *ex); +void +nxt_port_rpc_ex_set_peer(nxt_task_t *task, nxt_port_t *port, void *ex, nxt_pid_t peer); -void nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream); -void nxt_port_rpc_close(nxt_task_t *task, nxt_port_t *port); +void +nxt_port_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_port_rpc_remove_peer(nxt_task_t *task, nxt_port_t *port, nxt_pid_t peer); +void +nxt_port_rpc_cancel(nxt_task_t *task, nxt_port_t *port, uint32_t stream); +void +nxt_port_rpc_close(nxt_task_t *task, nxt_port_t *port); -#endif /* _NXT_PORT_RPC_H_INCLUDED_ */ +#endif /* _NXT_PORT_RPC_H_INCLUDED_ */ diff --git a/src/nxt_port_socket.c b/src/nxt_port_socket.c index 5752d5ab0..d168a0a22 100644 --- a/src/nxt_port_socket.c +++ b/src/nxt_port_socket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -10,39 +9,51 @@ #include -#define NXT_PORT_MAX_ENQUEUE_BUF_SIZE \ - (int) (NXT_PORT_QUEUE_MSG_SIZE - sizeof(nxt_port_msg_t)) +#define NXT_PORT_MAX_ENQUEUE_BUF_SIZE \ + (int) (NXT_PORT_QUEUE_MSG_SIZE - sizeof(nxt_port_msg_t)) -static nxt_bool_t nxt_port_can_enqueue_buf(nxt_buf_t *b); -static uint8_t nxt_port_enqueue_buf(nxt_task_t *task, nxt_port_msg_t *pm, - void *qbuf, nxt_buf_t *b); -static nxt_int_t nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port, - nxt_port_send_msg_t *msg); -static nxt_port_send_msg_t *nxt_port_msg_alloc(const nxt_port_send_msg_t *m); -static void nxt_port_write_handler(nxt_task_t *task, void *obj, void *data); -static nxt_port_send_msg_t *nxt_port_msg_first(nxt_port_t *port); -nxt_inline void nxt_port_msg_close_fd(nxt_port_send_msg_t *msg); -nxt_inline void nxt_port_close_fds(nxt_fd_t *fd); -static nxt_buf_t *nxt_port_buf_completion(nxt_task_t *task, - nxt_work_queue_t *wq, nxt_buf_t *b, size_t sent, nxt_bool_t mmap_mode); -static nxt_port_send_msg_t *nxt_port_msg_insert_tail(nxt_port_t *port, +static nxt_bool_t +nxt_port_can_enqueue_buf(nxt_buf_t *b); +static uint8_t +nxt_port_enqueue_buf(nxt_task_t *task, nxt_port_msg_t *pm, void *qbuf, + nxt_buf_t *b); +static nxt_int_t +nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port, nxt_port_send_msg_t *msg); -static void nxt_port_read_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_port_queue_read_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, +static nxt_port_send_msg_t * +nxt_port_msg_alloc(const nxt_port_send_msg_t *m); +static void +nxt_port_write_handler(nxt_task_t *task, void *obj, void *data); +static nxt_port_send_msg_t * +nxt_port_msg_first(nxt_port_t *port); +nxt_inline void +nxt_port_msg_close_fd(nxt_port_send_msg_t *msg); +nxt_inline void +nxt_port_close_fds(nxt_fd_t *fd); +static nxt_buf_t * +nxt_port_buf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b, + size_t sent, nxt_bool_t mmap_mode); +static nxt_port_send_msg_t * +nxt_port_msg_insert_tail(nxt_port_t *port, nxt_port_send_msg_t *msg); +static void +nxt_port_read_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, nxt_port_recv_msg_t *msg); -static nxt_buf_t *nxt_port_buf_alloc(nxt_port_t *port); -static void nxt_port_buf_free(nxt_port_t *port, nxt_buf_t *b); -static void nxt_port_error_handler(nxt_task_t *task, void *obj, void *data); - +static nxt_buf_t * +nxt_port_buf_alloc(nxt_port_t *port); +static void +nxt_port_buf_free(nxt_port_t *port, nxt_buf_t *b); +static void +nxt_port_error_handler(nxt_task_t *task, void *obj, void *data); nxt_int_t -nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) -{ - nxt_int_t sndbuf, rcvbuf, size; - nxt_socket_t snd, rcv; +nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) { + nxt_int_t sndbuf, rcvbuf, size; + nxt_socket_t snd, rcv; port->socket.task = task; @@ -80,7 +91,7 @@ nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) * Solaris uses 16K on send direction and 5K on receive direction. */ (void) nxt_socket_setsockopt(task, snd, SOL_SOCKET, SO_SNDBUF, - max_size); + max_size); sndbuf = nxt_socket_getsockopt(task, snd, SOL_SOCKET, SO_SNDBUF); if (nxt_slow_path(sndbuf < 0)) { @@ -91,7 +102,7 @@ nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) if (rcvbuf < size) { (void) nxt_socket_setsockopt(task, rcv, SOL_SOCKET, SO_RCVBUF, - size); + size); rcvbuf = nxt_socket_getsockopt(task, rcv, SOL_SOCKET, SO_RCVBUF); if (nxt_slow_path(rcvbuf < 0)) { @@ -100,7 +111,7 @@ nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) } } - port->max_size = nxt_min(max_size, (size_t) sndbuf); + port->max_size = nxt_min(max_size, (size_t) sndbuf); port->max_share = (64 * 1024); return NXT_OK; @@ -115,82 +126,72 @@ nxt_port_socket_init(nxt_task_t *task, nxt_port_t *port, size_t max_size) return NXT_ERROR; } - void -nxt_port_destroy(nxt_port_t *port) -{ +nxt_port_destroy(nxt_port_t *port) { nxt_socket_close(port->socket.task, port->socket.fd); nxt_mp_destroy(port->mem_pool); } - void -nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port) -{ - port->socket.fd = port->pair[1]; - port->socket.log = &nxt_main_log; +nxt_port_write_enable(nxt_task_t *task, nxt_port_t *port) { + port->socket.fd = port->pair[1]; + port->socket.log = &nxt_main_log; port->socket.write_ready = 1; port->engine = task->thread->engine; port->socket.write_work_queue = &port->engine->fast_work_queue; - port->socket.write_handler = nxt_port_write_handler; - port->socket.error_handler = nxt_port_error_handler; + port->socket.write_handler = nxt_port_write_handler; + port->socket.error_handler = nxt_port_error_handler; } - void -nxt_port_write_close(nxt_port_t *port) -{ +nxt_port_write_close(nxt_port_t *port) { nxt_socket_close(port->socket.task, port->pair[1]); port->pair[1] = -1; } - static void -nxt_port_release_send_msg(nxt_port_send_msg_t *msg) -{ +nxt_port_release_send_msg(nxt_port_send_msg_t *msg) { if (msg->allocated) { nxt_free(msg); } } - nxt_int_t nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, nxt_fd_t fd, nxt_fd_t fd2, uint32_t stream, nxt_port_id_t reply_port, - nxt_buf_t *b) -{ - int notify; - uint8_t qmsg_size; - nxt_int_t res; - nxt_port_send_msg_t msg; + nxt_buf_t *b) { + int notify; + uint8_t qmsg_size; + nxt_int_t res; + nxt_port_send_msg_t msg; + struct { - nxt_port_msg_t pm; - uint8_t buf[NXT_PORT_MAX_ENQUEUE_BUF_SIZE]; + nxt_port_msg_t pm; + uint8_t buf[NXT_PORT_MAX_ENQUEUE_BUF_SIZE]; } qmsg; msg.link.next = NULL; msg.link.prev = NULL; - msg.buf = b; - msg.share = 0; - msg.fd[0] = fd; - msg.fd[1] = fd2; - msg.close_fd = (type & NXT_PORT_MSG_CLOSE_FD) != 0; + msg.buf = b; + msg.share = 0; + msg.fd[0] = fd; + msg.fd[1] = fd2; + msg.close_fd = (type & NXT_PORT_MSG_CLOSE_FD) != 0; msg.allocated = 0; - msg.port_msg.stream = stream; - msg.port_msg.pid = nxt_pid; + msg.port_msg.stream = stream; + msg.port_msg.pid = nxt_pid; msg.port_msg.reply_port = reply_port; - msg.port_msg.type = type & NXT_PORT_MSG_MASK; - msg.port_msg.last = (type & NXT_PORT_MSG_LAST) != 0; - msg.port_msg.mmap = 0; - msg.port_msg.nf = 0; - msg.port_msg.mf = 0; + msg.port_msg.type = type & NXT_PORT_MSG_MASK; + msg.port_msg.last = (type & NXT_PORT_MSG_LAST) != 0; + msg.port_msg.mmap = 0; + msg.port_msg.nf = 0; + msg.port_msg.mf = 0; if (port->queue != NULL && type != _NXT_PORT_MSG_READ_QUEUE) { - if (fd == -1 && nxt_port_can_enqueue_buf(b)) { qmsg.pm = msg.port_msg; @@ -203,8 +204,8 @@ nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, res = nxt_port_queue_send(port->queue, &qmsg, qmsg_size, ¬ify); nxt_debug(task, "port{%d,%d} %d: enqueue %d notify %d, %d", - (int) port->pid, (int) port->id, port->socket.fd, - (int) qmsg_size, notify, res); + (int) port->pid, (int) port->id, port->socket.fd, + (int) qmsg_size, notify, res); if (b != NULL && nxt_fast_path(res == NXT_OK)) { if (qmsg.pm.mmap) { @@ -214,7 +215,7 @@ nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, b->mem.pos = b->mem.free; nxt_work_queue_add(&task->thread->engine->fast_work_queue, - b->completion_handler, task, b, b->parent); + b->completion_handler, task, b, b->parent); } if (notify == 0) { @@ -222,7 +223,7 @@ nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, } msg.port_msg.type = _NXT_PORT_MSG_READ_QUEUE; - msg.buf = NULL; + msg.buf = NULL; } else { qmsg.buf[0] = _NXT_PORT_MSG_READ_SOCKET; @@ -230,8 +231,7 @@ nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, res = nxt_port_queue_send(port->queue, qmsg.buf, 1, ¬ify); nxt_debug(task, "port{%d,%d} %d: enqueue 1 notify %d, %d", - (int) port->pid, (int) port->id, port->socket.fd, - notify, res); + (int) port->pid, (int) port->id, port->socket.fd, notify, res); if (nxt_slow_path(res == NXT_AGAIN)) { return NXT_AGAIN; @@ -248,10 +248,8 @@ nxt_port_socket_write2(nxt_task_t *task, nxt_port_t *port, nxt_uint_t type, return res; } - static nxt_bool_t -nxt_port_can_enqueue_buf(nxt_buf_t *b) -{ +nxt_port_can_enqueue_buf(nxt_buf_t *b) { if (b == NULL) { return 1; } @@ -264,15 +262,13 @@ nxt_port_can_enqueue_buf(nxt_buf_t *b) || nxt_buf_is_port_mmap(b)); } - static uint8_t nxt_port_enqueue_buf(nxt_task_t *task, nxt_port_msg_t *pm, void *qbuf, - nxt_buf_t *b) -{ + nxt_buf_t *b) { ssize_t size; - nxt_port_mmap_msg_t *mm; - nxt_port_mmap_header_t *hdr; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_mmap_msg_t *mm; + nxt_port_mmap_header_t *hdr; + nxt_port_mmap_handler_t *mmap_handler; size = nxt_buf_mem_used_size(&b->mem); @@ -283,33 +279,30 @@ nxt_port_enqueue_buf(nxt_task_t *task, nxt_port_msg_t *pm, void *qbuf, } mmap_handler = b->parent; - hdr = mmap_handler->hdr; - mm = qbuf; + hdr = mmap_handler->hdr; + mm = qbuf; - mm->mmap_id = hdr->id; + mm->mmap_id = hdr->id; mm->chunk_id = nxt_port_mmap_chunk_id(hdr, b->mem.pos); - mm->size = nxt_buf_mem_used_size(&b->mem); + mm->size = nxt_buf_mem_used_size(&b->mem); pm->mmap = 1; nxt_debug(task, "mmap_msg={%D, %D, %D}", mm->mmap_id, mm->chunk_id, - mm->size); + mm->size); return sizeof(nxt_port_mmap_msg_t); } - static nxt_int_t nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port, - nxt_port_send_msg_t *msg) -{ - nxt_int_t res; + nxt_port_send_msg_t *msg) { + nxt_int_t res; nxt_thread_mutex_lock(&port->write_mutex); - if (nxt_fast_path(port->socket.write_ready - && nxt_queue_is_empty(&port->messages))) - { + if (nxt_fast_path( + port->socket.write_ready && nxt_queue_is_empty(&port->messages))) { res = NXT_DECLINED; } else { @@ -330,11 +323,9 @@ nxt_port_msg_chk_insert(nxt_task_t *task, nxt_port_t *port, return res; } - static nxt_port_send_msg_t * -nxt_port_msg_alloc(const nxt_port_send_msg_t *m) -{ - nxt_port_send_msg_t *msg; +nxt_port_msg_alloc(const nxt_port_send_msg_t *m) { + nxt_port_send_msg_t *msg; msg = nxt_malloc(sizeof(nxt_port_send_msg_t)); if (nxt_slow_path(msg == NULL)) { @@ -348,41 +339,35 @@ nxt_port_msg_alloc(const nxt_port_send_msg_t *m) return msg; } - static void -nxt_port_fd_block_write(nxt_task_t *task, nxt_port_t *port, void *data) -{ +nxt_port_fd_block_write(nxt_task_t *task, nxt_port_t *port, void *data) { nxt_fd_event_block_write(task->thread->engine, &port->socket); } - static void -nxt_port_fd_enable_write(nxt_task_t *task, nxt_port_t *port, void *data) -{ +nxt_port_fd_enable_write(nxt_task_t *task, nxt_port_t *port, void *data) { nxt_fd_event_enable_write(task->thread->engine, &port->socket); } - static void -nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) -{ - int use_delta; - size_t plain_size; - ssize_t n; - uint32_t mmsg_buf[3 * NXT_IOBUF_MAX * 10]; - nxt_bool_t block_write, enable_write; - nxt_port_t *port; - struct iovec iov[NXT_IOBUF_MAX * 10]; - nxt_work_queue_t *wq; - nxt_port_method_t m; - nxt_port_send_msg_t *msg; - nxt_sendbuf_coalesce_t sb; +nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) { + int use_delta; + size_t plain_size; + ssize_t n; + uint32_t mmsg_buf[3 * NXT_IOBUF_MAX * 10]; + nxt_bool_t block_write, enable_write; + nxt_port_t *port; + struct iovec iov[NXT_IOBUF_MAX * 10]; + nxt_work_queue_t *wq; + nxt_port_method_t m; + nxt_port_send_msg_t *msg; + nxt_sendbuf_coalesce_t sb; port = nxt_container_of(obj, nxt_port_t, socket); - block_write = 0; + block_write = 0; enable_write = 0; - use_delta = 0; + use_delta = 0; wq = &task->thread->engine->fast_work_queue; @@ -399,28 +384,28 @@ nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) } } -next_fragment: + next_fragment: iov[0].iov_base = &msg->port_msg; - iov[0].iov_len = sizeof(nxt_port_msg_t); + iov[0].iov_len = sizeof(nxt_port_msg_t); - sb.buf = msg->buf; + sb.buf = msg->buf; sb.iobuf = &iov[1]; - sb.nmax = NXT_IOBUF_MAX - 1; - sb.sync = 0; - sb.last = 0; - sb.size = 0; + sb.nmax = NXT_IOBUF_MAX - 1; + sb.sync = 0; + sb.last = 0; + sb.size = 0; sb.limit = port->max_size; sb.limit_reached = 0; - sb.nmax_reached = 0; + sb.nmax_reached = 0; m = nxt_port_mmap_get_method(task, port, msg->buf); if (m == NXT_PORT_METHOD_MMAP) { sb.limit = (1ULL << 31) - 1; - sb.nmax = nxt_min(NXT_IOBUF_MAX * 10 - 1, - port->max_size / PORT_MMAP_MIN_SIZE); + sb.nmax = nxt_min(NXT_IOBUF_MAX * 10 - 1, + port->max_size / PORT_MMAP_MIN_SIZE); } sb.limit -= iov[0].iov_len; @@ -441,34 +426,34 @@ nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) } msg->port_msg.last |= sb.last; - msg->port_msg.mf = sb.limit_reached || sb.nmax_reached; + msg->port_msg.mf = sb.limit_reached || sb.nmax_reached; n = nxt_socketpair_send(&port->socket, msg->fd, iov, sb.niov + 1); if (n > 0) { if (nxt_slow_path((size_t) n != sb.size + iov[0].iov_len)) { nxt_alert(task, "port %d: short write: %z instead of %uz", - port->socket.fd, n, sb.size + iov[0].iov_len); + port->socket.fd, n, sb.size + iov[0].iov_len); goto fail; } nxt_port_msg_close_fd(msg); msg->buf = nxt_port_buf_completion(task, wq, msg->buf, plain_size, - m == NXT_PORT_METHOD_MMAP); + m == NXT_PORT_METHOD_MMAP); if (msg->buf != NULL) { nxt_debug(task, "port %d: frag stream #%uD", port->socket.fd, - msg->port_msg.stream); + msg->port_msg.stream); /* * A file descriptor is sent only * in the first message of a stream. */ - msg->fd[0] = -1; - msg->fd[1] = -1; - msg->share += n; - msg->port_msg.nf = 1; + msg->fd[0] = -1; + msg->fd[1] = -1; + msg->share += n; + msg->port_msg.nf = 1; if (msg->share >= port->max_share) { msg->share = 0; @@ -547,7 +532,7 @@ nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) use_delta++; nxt_work_queue_add(wq, nxt_port_error_handler, task, &port->socket, - &port->socket); + &port->socket); cleanup: @@ -564,12 +549,10 @@ nxt_port_write_handler(nxt_task_t *task, void *obj, void *data) } } - static nxt_port_send_msg_t * -nxt_port_msg_first(nxt_port_t *port) -{ - nxt_queue_link_t *lnk; - nxt_port_send_msg_t *msg; +nxt_port_msg_first(nxt_port_t *port) { + nxt_queue_link_t *lnk; + nxt_port_send_msg_t *msg; nxt_thread_mutex_lock(&port->write_mutex); @@ -587,10 +570,8 @@ nxt_port_msg_first(nxt_port_t *port) return msg; } - nxt_inline void -nxt_port_msg_close_fd(nxt_port_send_msg_t *msg) -{ +nxt_port_msg_close_fd(nxt_port_send_msg_t *msg) { if (!msg->close_fd) { return; } @@ -598,10 +579,8 @@ nxt_port_msg_close_fd(nxt_port_send_msg_t *msg) nxt_port_close_fds(msg->fd); } - nxt_inline void -nxt_port_close_fds(nxt_fd_t *fd) -{ +nxt_port_close_fds(nxt_fd_t *fd) { if (fd[0] != -1) { nxt_fd_close(fd[0]); fd[0] = -1; @@ -613,24 +592,19 @@ nxt_port_close_fds(nxt_fd_t *fd) } } - static nxt_buf_t * nxt_port_buf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b, - size_t sent, nxt_bool_t mmap_mode) -{ + size_t sent, nxt_bool_t mmap_mode) { size_t size; - nxt_buf_t *next; + nxt_buf_t *next; while (b != NULL) { - nxt_prefetch(b->next); if (!nxt_buf_is_sync(b)) { - size = nxt_buf_used_size(b); if (size != 0) { - if (sent == 0) { break; } @@ -644,7 +618,6 @@ nxt_port_buf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b, } if (sent < size) { - if (nxt_buf_is_mem(b)) { b->mem.pos += sent; } @@ -669,18 +642,16 @@ nxt_port_buf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b, nxt_work_queue_add(wq, b->completion_handler, task, b, b->parent); - next = b->next; + next = b->next; b->next = NULL; - b = next; + b = next; } return b; } - static nxt_port_send_msg_t * -nxt_port_msg_insert_tail(nxt_port_t *port, nxt_port_send_msg_t *msg) -{ +nxt_port_msg_insert_tail(nxt_port_t *port, nxt_port_send_msg_t *msg) { if (msg->allocated == 0) { msg = nxt_port_msg_alloc(msg); @@ -698,51 +669,45 @@ nxt_port_msg_insert_tail(nxt_port_t *port, nxt_port_send_msg_t *msg) return msg; } - void -nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port) -{ - port->socket.fd = port->pair[0]; +nxt_port_read_enable(nxt_task_t *task, nxt_port_t *port) { + port->socket.fd = port->pair[0]; port->socket.log = &nxt_main_log; port->engine = task->thread->engine; port->socket.read_work_queue = &port->engine->fast_work_queue; - port->socket.read_handler = port->queue != NULL - ? nxt_port_queue_read_handler - : nxt_port_read_handler; - port->socket.error_handler = nxt_port_error_handler; + port->socket.read_handler = port->queue != NULL + ? nxt_port_queue_read_handler + : nxt_port_read_handler; + port->socket.error_handler = nxt_port_error_handler; nxt_fd_event_enable_read(port->engine, &port->socket); } - void -nxt_port_read_close(nxt_port_t *port) -{ +nxt_port_read_close(nxt_port_t *port) { port->socket.read_ready = 0; - port->socket.read = NXT_EVENT_INACTIVE; + port->socket.read = NXT_EVENT_INACTIVE; nxt_socket_close(port->socket.task, port->pair[0]); port->pair[0] = -1; } - static void -nxt_port_read_handler(nxt_task_t *task, void *obj, void *data) -{ - ssize_t n; - nxt_buf_t *b; - nxt_int_t ret; - nxt_port_t *port; - nxt_recv_oob_t oob; - nxt_port_recv_msg_t msg; - struct iovec iov[2]; +nxt_port_read_handler(nxt_task_t *task, void *obj, void *data) { + ssize_t n; + nxt_buf_t *b; + nxt_int_t ret; + nxt_port_t *port; + nxt_recv_oob_t oob; + nxt_port_recv_msg_t msg; + struct iovec iov[2]; port = msg.port = nxt_container_of(obj, nxt_port_t, socket); nxt_assert(port->engine == task->thread->engine); - for ( ;; ) { + for (;;) { b = nxt_port_buf_alloc(port); if (nxt_slow_path(b == NULL)) { @@ -750,10 +715,10 @@ nxt_port_read_handler(nxt_task_t *task, void *obj, void *data) } iov[0].iov_base = &msg.port_msg; - iov[0].iov_len = sizeof(nxt_port_msg_t); + iov[0].iov_len = sizeof(nxt_port_msg_t); iov[1].iov_base = b->mem.pos; - iov[1].iov_len = port->max_size; + iov[1].iov_len = port->max_size; n = nxt_socketpair_recv(&port->socket, iov, 2, &oob); @@ -762,17 +727,17 @@ nxt_port_read_handler(nxt_task_t *task, void *obj, void *data) msg.fd[1] = -1; ret = nxt_socket_msg_oob_get(&oob, msg.fd, - nxt_recv_msg_cmsg_pid_ref(&msg)); + nxt_recv_msg_cmsg_pid_ref(&msg)); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "failed to get oob data from %d", - port->socket.fd); + port->socket.fd); nxt_port_close_fds(msg.fd); goto fail; } - msg.buf = b; + msg.buf = b; msg.size = n; nxt_port_read_msg_process(task, port, &msg); @@ -799,29 +764,27 @@ nxt_port_read_handler(nxt_task_t *task, void *obj, void *data) return; } -fail: + fail: /* n == 0 || error */ nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_port_error_handler, task, &port->socket, NULL); + nxt_port_error_handler, task, &port->socket, NULL); return; } } - static void -nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) -{ - ssize_t n; - nxt_buf_t *b; - nxt_int_t ret; - nxt_port_t *port; - struct iovec iov[2]; - nxt_recv_oob_t oob; - nxt_port_queue_t *queue; - nxt_port_recv_msg_t msg, *smsg; - uint8_t qmsg[NXT_PORT_QUEUE_MSG_SIZE]; - - port = nxt_container_of(obj, nxt_port_t, socket); +nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) { + ssize_t n; + nxt_buf_t *b; + nxt_int_t ret; + nxt_port_t *port; + struct iovec iov[2]; + nxt_recv_oob_t oob; + nxt_port_queue_t *queue; + nxt_port_recv_msg_t msg, *smsg; + uint8_t qmsg[NXT_PORT_QUEUE_MSG_SIZE]; + + port = nxt_container_of(obj, nxt_port_t, socket); msg.port = port; nxt_assert(port->engine == task->thread->engine); @@ -829,8 +792,7 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) queue = port->queue; nxt_atomic_fetch_add(&queue->nitems, 1); - for ( ;; ) { - + for (;;) { if (port->from_socket == 0) { n = nxt_port_queue_recv(queue, qmsg); @@ -849,31 +811,29 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) port->from_socket++; nxt_debug(task, "port{%d,%d} %d: dequeue 1 read_socket %d", - (int) port->pid, (int) port->id, port->socket.fd, - port->from_socket); + (int) port->pid, (int) port->id, port->socket.fd, + port->from_socket); continue; } - nxt_debug(task, "port{%d,%d} %d: dequeue %d", - (int) port->pid, (int) port->id, port->socket.fd, - (int) n); + nxt_debug(task, "port{%d,%d} %d: dequeue %d", (int) port->pid, + (int) port->id, port->socket.fd, (int) n); } else { if ((smsg = port->socket_msg) != NULL && smsg->size != 0) { msg.port_msg = smsg->port_msg; - b = smsg->buf; - n = smsg->size; - msg.fd[0] = smsg->fd[0]; - msg.fd[1] = smsg->fd[1]; + b = smsg->buf; + n = smsg->size; + msg.fd[0] = smsg->fd[0]; + msg.fd[1] = smsg->fd[1]; smsg->size = 0; port->from_socket--; nxt_debug(task, "port{%d,%d} %d: use suspended message %d", - (int) port->pid, (int) port->id, port->socket.fd, - (int) n); + (int) port->pid, (int) port->id, port->socket.fd, (int) n); goto process; } @@ -897,15 +857,15 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) if (n > (ssize_t) sizeof(nxt_port_msg_t)) { nxt_memcpy(b->mem.pos, qmsg + sizeof(nxt_port_msg_t), - n - sizeof(nxt_port_msg_t)); + n - sizeof(nxt_port_msg_t)); } } else { iov[0].iov_base = &msg.port_msg; - iov[0].iov_len = sizeof(nxt_port_msg_t); + iov[0].iov_len = sizeof(nxt_port_msg_t); iov[1].iov_base = b->mem.pos; - iov[1].iov_len = port->max_size; + iov[1].iov_len = port->max_size; n = nxt_socketpair_recv(&port->socket, iov, 2, &oob); @@ -914,10 +874,10 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) msg.fd[1] = -1; ret = nxt_socket_msg_oob_get(&oob, msg.fd, - nxt_recv_msg_cmsg_pid_ref(&msg)); + nxt_recv_msg_cmsg_pid_ref(&msg)); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "failed to get oob data from %d", - port->socket.fd); + port->socket.fd); nxt_port_close_fds(msg.fd); @@ -926,38 +886,36 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) } if (n == (ssize_t) sizeof(nxt_port_msg_t) - && msg.port_msg.type == _NXT_PORT_MSG_READ_QUEUE) - { + && msg.port_msg.type == _NXT_PORT_MSG_READ_QUEUE) { nxt_port_buf_free(port, b); nxt_debug(task, "port{%d,%d} %d: recv %d read_queue", - (int) port->pid, (int) port->id, port->socket.fd, - (int) n); + (int) port->pid, (int) port->id, port->socket.fd, (int) n); continue; } - nxt_debug(task, "port{%d,%d} %d: recvmsg %d", - (int) port->pid, (int) port->id, port->socket.fd, - (int) n); + nxt_debug(task, "port{%d,%d} %d: recvmsg %d", (int) port->pid, + (int) port->id, port->socket.fd, (int) n); if (n > 0) { if (port->from_socket == 0) { nxt_debug(task, "port{%d,%d} %d: suspend message %d", - (int) port->pid, (int) port->id, port->socket.fd, - (int) n); + (int) port->pid, (int) port->id, port->socket.fd, + (int) n); smsg = port->socket_msg; if (nxt_slow_path(smsg == NULL)) { smsg = nxt_mp_alloc(port->mem_pool, - sizeof(nxt_port_recv_msg_t)); + sizeof(nxt_port_recv_msg_t)); if (nxt_slow_path(smsg == NULL)) { - nxt_alert(task, "port{%d,%d} %d: suspend message " - "failed", - (int) port->pid, (int) port->id, - port->socket.fd); + nxt_alert(task, + "port{%d,%d} %d: suspend message " + "failed", + (int) port->pid, (int) port->id, + port->socket.fd); return; } @@ -966,20 +924,21 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) } else { if (nxt_slow_path(smsg->size != 0)) { - nxt_alert(task, "port{%d,%d} %d: too many suspend " - "messages", - (int) port->pid, (int) port->id, - port->socket.fd); + nxt_alert(task, + "port{%d,%d} %d: too many suspend " + "messages", + (int) port->pid, (int) port->id, + port->socket.fd); return; } } smsg->port_msg = msg.port_msg; - smsg->buf = b; - smsg->size = n; - smsg->fd[0] = msg.fd[0]; - smsg->fd[1] = msg.fd[1]; + smsg->buf = b; + smsg->size = n; + smsg->fd[0] = msg.fd[0]; + smsg->fd[1] = msg.fd[1]; continue; } @@ -991,7 +950,7 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) process: if (n > 0) { - msg.buf = b; + msg.buf = b; msg.size = n; nxt_port_read_msg_process(task, port, &msg); @@ -1018,67 +977,56 @@ nxt_port_queue_read_handler(nxt_task_t *task, void *obj, void *data) /* n == 0 || n == NXT_ERROR */ nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_port_error_handler, task, &port->socket, NULL); + nxt_port_error_handler, task, &port->socket, NULL); return; } } - typedef struct { - uint32_t stream; - uint32_t pid; + uint32_t stream; + uint32_t pid; } nxt_port_frag_key_t; - static nxt_int_t -nxt_port_lvlhsh_frag_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_port_recv_msg_t *fmsg; - nxt_port_frag_key_t *frag_key; +nxt_port_lvlhsh_frag_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_port_recv_msg_t *fmsg; + nxt_port_frag_key_t *frag_key; - fmsg = data; + fmsg = data; frag_key = (nxt_port_frag_key_t *) lhq->key.start; if (lhq->key.length == sizeof(nxt_port_frag_key_t) && frag_key->stream == fmsg->port_msg.stream - && frag_key->pid == (uint32_t) fmsg->port_msg.pid) - { + && frag_key->pid == (uint32_t) fmsg->port_msg.pid) { return NXT_OK; } return NXT_DECLINED; } - static void * -nxt_port_lvlhsh_frag_alloc(void *ctx, size_t size) -{ +nxt_port_lvlhsh_frag_alloc(void *ctx, size_t size) { return nxt_mp_align(ctx, size, size); } - static void -nxt_port_lvlhsh_frag_free(void *ctx, void *p) -{ +nxt_port_lvlhsh_frag_free(void *ctx, void *p) { nxt_mp_free(ctx, p); } - -static const nxt_lvlhsh_proto_t lvlhsh_frag_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_frag_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_port_lvlhsh_frag_test, nxt_port_lvlhsh_frag_alloc, nxt_port_lvlhsh_frag_free, }; - static nxt_port_recv_msg_t * nxt_port_frag_start(nxt_task_t *task, nxt_port_t *port, - nxt_port_recv_msg_t *msg) -{ + nxt_port_recv_msg_t *msg) { nxt_int_t res; nxt_lvlhsh_query_t lhq; - nxt_port_recv_msg_t *fmsg; + nxt_port_recv_msg_t *fmsg; nxt_port_frag_key_t frag_key; nxt_debug(task, "start frag stream #%uD", msg->port_msg.stream); @@ -1092,91 +1040,85 @@ nxt_port_frag_start(nxt_task_t *task, nxt_port_t *port, *fmsg = *msg; frag_key.stream = fmsg->port_msg.stream; - frag_key.pid = fmsg->port_msg.pid; + frag_key.pid = fmsg->port_msg.pid; - lhq.key_hash = nxt_murmur_hash2(&frag_key, sizeof(nxt_port_frag_key_t)); + lhq.key_hash = nxt_murmur_hash2(&frag_key, sizeof(nxt_port_frag_key_t)); lhq.key.length = sizeof(nxt_port_frag_key_t); - lhq.key.start = (u_char *) &frag_key; - lhq.proto = &lvlhsh_frag_proto; - lhq.replace = 0; - lhq.value = fmsg; - lhq.pool = port->mem_pool; + lhq.key.start = (u_char *) &frag_key; + lhq.proto = &lvlhsh_frag_proto; + lhq.replace = 0; + lhq.value = fmsg; + lhq.pool = port->mem_pool; res = nxt_lvlhsh_insert(&port->frags, &lhq); switch (res) { - case NXT_OK: return fmsg; case NXT_DECLINED: nxt_log(task, NXT_LOG_WARN, "duplicate frag stream #%uD", - fmsg->port_msg.stream); + fmsg->port_msg.stream); nxt_mp_free(port->mem_pool, fmsg); return NULL; default: nxt_log(task, NXT_LOG_WARN, "failed to add frag stream #%uD", - fmsg->port_msg.stream); + fmsg->port_msg.stream); nxt_mp_free(port->mem_pool, fmsg); return NULL; - } } - static nxt_port_recv_msg_t * -nxt_port_frag_find(nxt_task_t *task, nxt_port_t *port, nxt_port_recv_msg_t *msg) -{ - nxt_int_t res; - nxt_bool_t last; - nxt_lvlhsh_query_t lhq; - nxt_port_frag_key_t frag_key; +nxt_port_frag_find(nxt_task_t *task, nxt_port_t *port, + nxt_port_recv_msg_t *msg) { + nxt_int_t res; + nxt_bool_t last; + nxt_lvlhsh_query_t lhq; + nxt_port_frag_key_t frag_key; last = msg->port_msg.mf == 0; nxt_debug(task, "%s frag stream #%uD", last ? "last" : "next", - msg->port_msg.stream); + msg->port_msg.stream); frag_key.stream = msg->port_msg.stream; - frag_key.pid = msg->port_msg.pid; + frag_key.pid = msg->port_msg.pid; - lhq.key_hash = nxt_murmur_hash2(&frag_key, sizeof(nxt_port_frag_key_t)); + lhq.key_hash = nxt_murmur_hash2(&frag_key, sizeof(nxt_port_frag_key_t)); lhq.key.length = sizeof(nxt_port_frag_key_t); - lhq.key.start = (u_char *) &frag_key; - lhq.proto = &lvlhsh_frag_proto; - lhq.pool = port->mem_pool; + lhq.key.start = (u_char *) &frag_key; + lhq.proto = &lvlhsh_frag_proto; + lhq.pool = port->mem_pool; - res = last != 0 ? nxt_lvlhsh_delete(&port->frags, &lhq) : - nxt_lvlhsh_find(&port->frags, &lhq); + res = last != 0 ? nxt_lvlhsh_delete(&port->frags, &lhq) + : nxt_lvlhsh_find(&port->frags, &lhq); switch (res) { - case NXT_OK: return lhq.value; default: nxt_log(task, NXT_LOG_INFO, "frag stream #%uD not found", - frag_key.stream); + frag_key.stream); return NULL; } } - static void nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, - nxt_port_recv_msg_t *msg) -{ - nxt_buf_t *b, *orig_b, *next; - nxt_port_recv_msg_t *fmsg; + nxt_port_recv_msg_t *msg) { + nxt_buf_t *b, *orig_b, *next; + nxt_port_recv_msg_t *fmsg; if (nxt_slow_path(msg->size < sizeof(nxt_port_msg_t))) { - nxt_alert(task, "port %d: too small message:%uz", - port->socket.fd, msg->size); + nxt_alert(task, "port %d: too small message:%uz", port->socket.fd, + msg->size); nxt_port_close_fds(msg->fd); @@ -1186,13 +1128,12 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, /* adjust size to actual buffer used size */ msg->size -= sizeof(nxt_port_msg_t); - b = orig_b = msg->buf; + b = orig_b = msg->buf; b->mem.free += msg->size; msg->cancelled = 0; if (nxt_slow_path(msg->port_msg.nf != 0)) { - fmsg = nxt_port_frag_find(task, port, msg); if (nxt_slow_path(fmsg == NULL)) { @@ -1200,7 +1141,6 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, } if (nxt_fast_path(fmsg->cancelled == 0)) { - if (msg->port_msg.mmap) { nxt_port_mmap_read(task, msg); } @@ -1208,16 +1148,15 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, nxt_buf_chain_add(&fmsg->buf, msg->buf); fmsg->size += msg->size; - msg->buf = NULL; - b = NULL; + msg->buf = NULL; + b = NULL; if (nxt_fast_path(msg->port_msg.mf == 0)) { - b = fmsg->buf; port->handler(task, fmsg); - msg->buf = fmsg->buf; + msg->buf = fmsg->buf; msg->fd[0] = fmsg->fd[0]; msg->fd[1] = fmsg->fd[1]; @@ -1236,7 +1175,6 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, } } else { if (nxt_slow_path(msg->port_msg.mf != 0)) { - if (msg->port_msg.mmap && msg->cancelled == 0) { nxt_port_mmap_read(task, msg); b = msg->buf; @@ -1252,17 +1190,16 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, fmsg->port_msg.mf = 0; if (nxt_fast_path(msg->cancelled == 0)) { - msg->buf = NULL; + msg->buf = NULL; msg->fd[0] = -1; msg->fd[1] = -1; - b = NULL; + b = NULL; } else { nxt_port_close_fds(msg->fd); } } else { if (nxt_fast_path(msg->cancelled == 0)) { - if (msg->port_msg.mmap) { nxt_port_mmap_read(task, msg); b = msg->buf; @@ -1276,7 +1213,6 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, fmsg_failed: if (msg->port_msg.mmap && orig_b != b) { - /* * To disable instant buffer completion, * handler should reset 'msg->buf'. @@ -1289,9 +1225,9 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, nxt_work_queue_add(port->socket.read_work_queue, b->completion_handler, task, b, b->parent); - next = b->next; + next = b->next; b->next = NULL; - b = next; + b = next; } } @@ -1300,19 +1236,17 @@ nxt_port_read_msg_process(nxt_task_t *task, nxt_port_t *port, } } - static nxt_buf_t * -nxt_port_buf_alloc(nxt_port_t *port) -{ - nxt_buf_t *b; +nxt_port_buf_alloc(nxt_port_t *port) { + nxt_buf_t *b; if (port->free_bufs != NULL) { - b = port->free_bufs; + b = port->free_bufs; port->free_bufs = b->next; - b->mem.pos = b->mem.start; + b->mem.pos = b->mem.start; b->mem.free = b->mem.start; - b->next = NULL; + b->next = NULL; } else { b = nxt_buf_mem_alloc(port->mem_pool, port->max_size, 0); if (nxt_slow_path(b == NULL)) { @@ -1323,23 +1257,19 @@ nxt_port_buf_alloc(nxt_port_t *port) return b; } - static void -nxt_port_buf_free(nxt_port_t *port, nxt_buf_t *b) -{ +nxt_port_buf_free(nxt_port_t *port, nxt_buf_t *b) { nxt_buf_chain_add(&b, port->free_bufs); port->free_bufs = b; } - static void -nxt_port_error_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_port_error_handler(nxt_task_t *task, void *obj, void *data) { int use_delta; - nxt_buf_t *b, *next; - nxt_port_t *port; - nxt_work_queue_t *wq; - nxt_port_send_msg_t *msg; + nxt_buf_t *b, *next; + nxt_port_t *port; + nxt_work_queue_t *wq; + nxt_port_send_msg_t *msg; nxt_debug(task, "port error handler %p", obj); /* TODO */ @@ -1357,11 +1287,10 @@ nxt_port_error_handler(nxt_task_t *task, void *obj, void *data) nxt_thread_mutex_lock(&port->write_mutex); nxt_queue_each(msg, &port->messages, nxt_port_send_msg_t, link) { - nxt_port_msg_close_fd(msg); for (b = msg->buf; b != NULL; b = next) { - next = b->next; + next = b->next; b->next = NULL; if (nxt_buf_is_sync(b)) { @@ -1375,8 +1304,8 @@ nxt_port_error_handler(nxt_task_t *task, void *obj, void *data) use_delta--; nxt_port_release_send_msg(msg); - - } nxt_queue_loop; + } + nxt_queue_loop; nxt_thread_mutex_unlock(&port->write_mutex); diff --git a/src/nxt_process.c b/src/nxt_process.c index f445f0f5b..a4fd1c3ae 100644 --- a/src/nxt_process.c +++ b/src/nxt_process.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -21,101 +20,108 @@ #if (NXT_HAVE_LINUX_NS) && (NXT_HAVE_CLONE_NEWPID) -#define nxt_is_pid_isolated(process) \ +#define nxt_is_pid_isolated(process) \ nxt_is_clone_flag_set(process->isolation.clone.flags, NEWPID) #else -#define nxt_is_pid_isolated(process) \ - (0) +#define nxt_is_pid_isolated(process) (0) #endif #if (NXT_HAVE_LINUX_NS) -static nxt_int_t nxt_process_pipe_timer(nxt_fd_t fd, short event); -static nxt_int_t nxt_process_check_pid_status(const nxt_fd_t *gc_pipe); -static nxt_pid_t nxt_process_recv_pid(const nxt_fd_t *pid_pipe, - const nxt_fd_t *gc_pipe); -static void nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid); -static nxt_int_t nxt_process_unshare(nxt_task_t *task, nxt_process_t *process, +static nxt_int_t +nxt_process_pipe_timer(nxt_fd_t fd, short event); +static nxt_int_t +nxt_process_check_pid_status(const nxt_fd_t *gc_pipe); +static nxt_pid_t +nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe); +static void +nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid); +static nxt_int_t +nxt_process_unshare(nxt_task_t *task, nxt_process_t *process, nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, nxt_bool_t use_pidns); -static nxt_int_t nxt_process_init_pidns(nxt_task_t *task, - const nxt_process_t *process, nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, - nxt_bool_t *use_pidns); +static nxt_int_t +nxt_process_init_pidns(nxt_task_t *task, const nxt_process_t *process, + nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, nxt_bool_t *use_pidns); #endif -static nxt_pid_t nxt_process_create(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_process_do_start(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_process_whoami(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_process_setup(nxt_task_t *task, nxt_process_t *process); -static nxt_int_t nxt_process_child_fixup(nxt_task_t *task, - nxt_process_t *process); -static void nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data); -static void nxt_process_whoami_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, +static nxt_pid_t +nxt_process_create(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_process_do_start(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_process_whoami(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_process_setup(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process); +static void +nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); +static void +nxt_process_whoami_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static nxt_int_t nxt_process_send_created(nxt_task_t *task, - nxt_process_t *process); -static nxt_int_t nxt_process_send_ready(nxt_task_t *task, - nxt_process_t *process); -static void nxt_process_created_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, +static nxt_int_t +nxt_process_send_created(nxt_task_t *task, nxt_process_t *process); +static nxt_int_t +nxt_process_send_ready(nxt_task_t *task, nxt_process_t *process); +static void +nxt_process_created_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); +static void +nxt_process_created_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static void nxt_process_created_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); /* A cached process pid. */ -nxt_pid_t nxt_pid; +nxt_pid_t nxt_pid; /* An original parent process pid. */ -nxt_pid_t nxt_ppid; +nxt_pid_t nxt_ppid; /* A cached process effective uid */ -nxt_uid_t nxt_euid; +nxt_uid_t nxt_euid; /* A cached process effective gid */ -nxt_gid_t nxt_egid; - -uint8_t nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { - { 1, 1, 1, 1, 1, 1 }, - { 1, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 1, 0, 0 }, - { 1, 0, 1, 1, 1, 1 }, - { 1, 0, 0, 1, 0, 0 }, - { 1, 0, 0, 1, 0, 0 }, +nxt_gid_t nxt_egid; + +uint8_t nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { + {1, 1, 1, 1, 1, 1}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 1, 0, 0}, + {1, 0, 1, 1, 1, 1}, + {1, 0, 0, 1, 0, 0}, + {1, 0, 0, 1, 0, 0}, }; -uint8_t nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { - { 1, 1, 1, 1, 1, 1 }, - { 1, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 1, 0, 0 }, - { 1, 0, 1, 1, 1, 1 }, - { 1, 0, 0, 0, 0, 0 }, - { 1, 0, 0, 0, 0, 0 }, +uint8_t nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { + {1, 1, 1, 1, 1, 1}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 1, 0, 0}, + {1, 0, 1, 1, 1, 1}, + {1, 0, 0, 0, 0, 0}, + {1, 0, 0, 0, 0, 0}, }; -uint8_t nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { - { 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0 }, - { 0, 0, 0, 1, 0, 0 }, - { 0, 0, 1, 0, 1, 1 }, - { 0, 0, 0, 1, 0, 0 }, - { 1, 0, 0, 1, 0, 0 }, +uint8_t nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX] = { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0}, + {0, 0, 1, 0, 1, 1}, + {0, 0, 0, 1, 0, 0}, + {1, 0, 0, 1, 0, 0}, }; -static const nxt_port_handlers_t nxt_process_whoami_port_handlers = { - .quit = nxt_signal_quit_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, +static const nxt_port_handlers_t nxt_process_whoami_port_handlers = { + .quit = nxt_signal_quit_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, }; - nxt_process_t * -nxt_process_new(nxt_runtime_t *rt) -{ - nxt_process_t *process; +nxt_process_new(nxt_runtime_t *rt) { + nxt_process_t *process; - process = nxt_mp_zalloc(rt->mem_pool, sizeof(nxt_process_t) - + sizeof(nxt_process_init_t)); + process = nxt_mp_zalloc(rt->mem_pool, + sizeof(nxt_process_t) + sizeof(nxt_process_init_t)); if (nxt_slow_path(process == NULL)) { return NULL; @@ -132,10 +138,8 @@ nxt_process_new(nxt_runtime_t *rt) return process; } - void -nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i) -{ +nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i) { process->use_count += i; if (process->use_count == 0) { @@ -143,14 +147,12 @@ nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i) } } - nxt_int_t -nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init) -{ +nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init) { nxt_int_t ret; - nxt_runtime_t *rt; - nxt_process_t *process; - nxt_process_init_t *pinit; + nxt_runtime_t *rt; + nxt_process_t *process; + nxt_process_init_t *pinit; rt = task->thread->runtime; @@ -161,10 +163,10 @@ nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init) process->parent_port = rt->port_by_type[rt->type]; - process->name = init.name; + process->name = init.name; process->user_cred = &rt->user_cred; - pinit = nxt_process_init(process); + pinit = nxt_process_init(process); *pinit = init; ret = nxt_process_start(task, process); @@ -175,15 +177,13 @@ nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init) return ret; } - nxt_int_t -nxt_process_start(nxt_task_t *task, nxt_process_t *process) -{ - nxt_mp_t *tmp_mp; +nxt_process_start(nxt_task_t *task, nxt_process_t *process) { + nxt_mp_t *tmp_mp; nxt_int_t ret; nxt_pid_t pid; - nxt_port_t *port; - nxt_process_init_t *init; + nxt_port_t *port; + nxt_process_init_t *init; init = nxt_process_init(process); @@ -216,7 +216,6 @@ nxt_process_start(nxt_task_t *task, nxt_process_t *process) pid = nxt_process_create(task, process); switch (pid) { - case -1: ret = NXT_ERROR; break; @@ -258,13 +257,11 @@ nxt_process_start(nxt_task_t *task, nxt_process_t *process) return ret; } - static nxt_int_t -nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) -{ - nxt_process_t *p; - nxt_runtime_t *rt; - nxt_process_init_t *init; +nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) { + nxt_process_t *p; + nxt_runtime_t *rt; + nxt_process_init_t *init; nxt_process_type_t ptype; init = nxt_process_init(process); @@ -273,7 +270,7 @@ nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) nxt_pid = getpid(); - process->pid = nxt_pid; + process->pid = nxt_pid; process->isolated_pid = nxt_pid; /* Clean inherited cached thread tid. */ @@ -289,7 +286,6 @@ nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) /* Remove not ready processes. */ nxt_runtime_process_each(rt, p) { - if (nxt_proc_keep_matrix[ptype][nxt_process_type(p)] == 0 && p->pid != nxt_ppid) /* Always keep parent's port. */ { @@ -309,17 +305,16 @@ nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) } nxt_port_mmaps_destroy(&p->incoming, 0); - - } nxt_runtime_process_loop; + } + nxt_runtime_process_loop; if (init->siblings != NULL) { nxt_queue_each(p, init->siblings, nxt_process_t, link) { - nxt_debug(task, "remove sibling process %PI", p->pid); nxt_process_close_ports(task, p); - - } nxt_queue_loop; + } + nxt_queue_loop; } return NXT_OK; @@ -329,13 +324,12 @@ nxt_process_child_fixup(nxt_task_t *task, nxt_process_t *process) #if (NXT_HAVE_LINUX_NS) static nxt_int_t -nxt_process_pipe_timer(nxt_fd_t fd, short event) -{ - int ret; - sigset_t mask; - struct pollfd pfd; +nxt_process_pipe_timer(nxt_fd_t fd, short event) { + int ret; + sigset_t mask; + struct pollfd pfd; - static const struct timespec ts = { .tv_sec = 5 }; + static const struct timespec ts = {.tv_sec = 5}; /* * Temporarily block the signals we are handling, (except @@ -355,7 +349,7 @@ nxt_process_pipe_timer(nxt_fd_t fd, short event) sigdelset(&mask, SIGINT); sigdelset(&mask, SIGTERM); - pfd.fd = fd; + pfd.fd = fd; pfd.events = event; ret = ppoll(&pfd, 1, &ts, &mask); @@ -366,12 +360,10 @@ nxt_process_pipe_timer(nxt_fd_t fd, short event) return NXT_OK; } - static nxt_int_t -nxt_process_check_pid_status(const nxt_fd_t *gc_pipe) -{ - int8_t status = -1; - ssize_t ret; +nxt_process_check_pid_status(const nxt_fd_t *gc_pipe) { + int8_t status = -1; + ssize_t ret; close(gc_pipe[1]); @@ -385,13 +377,11 @@ nxt_process_check_pid_status(const nxt_fd_t *gc_pipe) return status; } - static nxt_pid_t -nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe) -{ - int8_t status; - ssize_t ret; - nxt_pid_t pid; +nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe) { + int8_t status; + ssize_t ret; + nxt_pid_t pid; close(pid_pipe[1]); close(gc_pipe[0]); @@ -405,7 +395,7 @@ nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe) if (ret <= 0) { status = -1; - pid = -1; + pid = -1; } write(gc_pipe[1], &status, sizeof(int8_t)); @@ -416,11 +406,9 @@ nxt_process_recv_pid(const nxt_fd_t *pid_pipe, const nxt_fd_t *gc_pipe) return pid; } - static void -nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid) -{ - nxt_int_t ret; +nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid) { + nxt_int_t ret; close(pid_pipe[0]); @@ -432,14 +420,11 @@ nxt_process_send_pid(const nxt_fd_t *pid_pipe, nxt_pid_t pid) close(pid_pipe[1]); } - static nxt_int_t nxt_process_unshare(nxt_task_t *task, nxt_process_t *process, - nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, - nxt_bool_t use_pidns) -{ - int ret; - nxt_pid_t pid; + nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, nxt_bool_t use_pidns) { + int ret; + nxt_pid_t pid; if (process->isolation.clone.flags == 0) { return NXT_OK; @@ -447,8 +432,7 @@ nxt_process_unshare(nxt_task_t *task, nxt_process_t *process, ret = unshare(process->isolation.clone.flags); if (nxt_slow_path(ret == -1)) { - nxt_alert(task, "unshare() failed for %s %E", process->name, - nxt_errno); + nxt_alert(task, "unshare() failed for %s %E", process->name, nxt_errno); if (use_pidns) { nxt_pipe_close(task, gc_pipe); @@ -491,12 +475,9 @@ nxt_process_unshare(nxt_task_t *task, nxt_process_t *process, return NXT_OK; } - static nxt_int_t nxt_process_init_pidns(nxt_task_t *task, const nxt_process_t *process, - nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, - nxt_bool_t *use_pidns) -{ + nxt_fd_t *pid_pipe, nxt_fd_t *gc_pipe, nxt_bool_t *use_pidns) { int ret; *use_pidns = 0; @@ -523,7 +504,7 @@ nxt_process_init_pidns(nxt_task_t *task, const nxt_process_t *process, ret = prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); if (nxt_slow_path(ret == -1)) { nxt_alert(task, "prctl(PR_SET_CHILD_SUBREAPER) failed for %s %E", - process->name, nxt_errno); + process->name, nxt_errno); } #endif @@ -534,15 +515,14 @@ nxt_process_init_pidns(nxt_task_t *task, const nxt_process_t *process, static nxt_pid_t -nxt_process_create(nxt_task_t *task, nxt_process_t *process) -{ +nxt_process_create(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret; nxt_pid_t pid; - nxt_runtime_t *rt; + nxt_runtime_t *rt; #if (NXT_HAVE_LINUX_NS) - nxt_fd_t pid_pipe[2], gc_pipe[2]; - nxt_bool_t use_pidns; + nxt_fd_t pid_pipe[2], gc_pipe[2]; + nxt_bool_t use_pidns; ret = nxt_process_init_pidns(task, process, pid_pipe, gc_pipe, &use_pidns); if (ret == NXT_ERROR) { @@ -597,7 +577,7 @@ nxt_process_create(nxt_task_t *task, nxt_process_t *process) } #endif - process->pid = pid; + process->pid = pid; process->isolated_pid = pid; rt = task->thread->runtime; @@ -619,7 +599,7 @@ nxt_process_create(nxt_task_t *task, nxt_process_t *process) ret = nxt_cgroup_proc_add(task, process); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "cgroup: failed to add process %s to %s %E", - process->name, process->isolation.cgroup.path, nxt_errno); + process->name, process->isolation.cgroup.path, nxt_errno); nxt_cgroup_cleanup(task, process); kill(pid, SIGTERM); return -1; @@ -629,16 +609,14 @@ nxt_process_create(nxt_task_t *task, nxt_process_t *process) return pid; } - static nxt_int_t -nxt_process_setup(nxt_task_t *task, nxt_process_t *process) -{ +nxt_process_setup(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret; - nxt_thread_t *thread; - nxt_runtime_t *rt; - nxt_process_init_t *init; - nxt_event_engine_t *engine; - const nxt_event_interface_t *interface; + nxt_thread_t *thread; + nxt_runtime_t *rt; + nxt_process_init_t *init; + nxt_event_engine_t *engine; + const nxt_event_interface_t *interface; init = nxt_process_init(process); @@ -672,7 +650,7 @@ nxt_process_setup(nxt_task_t *task, nxt_process_t *process) } ret = nxt_runtime_thread_pool_create(thread, rt, rt->auxiliary_threads, - 60000 * 1000000LL); + 60000 * 1000000LL); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -689,8 +667,7 @@ nxt_process_setup(nxt_task_t *task, nxt_process_t *process) } if (rt->is_pid_isolated - || process->parent_port != rt->port_by_type[NXT_PROCESS_MAIN]) - { + || process->parent_port != rt->port_by_type[NXT_PROCESS_MAIN]) { ret = nxt_process_whoami(task, process); } else { @@ -700,13 +677,11 @@ nxt_process_setup(nxt_task_t *task, nxt_process_t *process) return ret; } - static nxt_int_t -nxt_process_do_start(nxt_task_t *task, nxt_process_t *process) -{ +nxt_process_do_start(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret; - nxt_port_t *port; - nxt_process_init_t *init; + nxt_port_t *port; + nxt_process_init_t *init; nxt_runtime_process_add(task, process); @@ -721,7 +696,6 @@ nxt_process_do_start(nxt_task_t *task, nxt_process_t *process) } switch (process->state) { - case NXT_PROCESS_STATE_CREATED: ret = nxt_process_send_created(task, process); break; @@ -750,20 +724,18 @@ nxt_process_do_start(nxt_task_t *task, nxt_process_t *process) return ret; } - static nxt_int_t -nxt_process_whoami(nxt_task_t *task, nxt_process_t *process) -{ +nxt_process_whoami(nxt_task_t *task, nxt_process_t *process) { uint32_t stream; nxt_fd_t fd; - nxt_buf_t *buf; + nxt_buf_t *buf; nxt_int_t ret; - nxt_port_t *my_port, *main_port; - nxt_runtime_t *rt; + nxt_port_t *my_port, *main_port; + nxt_runtime_t *rt; rt = task->thread->runtime; - my_port = nxt_process_port_first(process); + my_port = nxt_process_port_first(process); main_port = rt->port_by_type[NXT_PROCESS_MAIN]; nxt_assert(my_port != NULL && main_port != NULL); @@ -777,10 +749,8 @@ nxt_process_whoami(nxt_task_t *task, nxt_process_t *process) buf->mem.free = nxt_cpymem(buf->mem.free, &nxt_ppid, sizeof(nxt_pid_t)); - stream = nxt_port_rpc_register_handler(task, my_port, - nxt_process_whoami_ok, - nxt_process_whoami_error, - main_port->pid, process); + stream = nxt_port_rpc_register_handler(task, my_port, nxt_process_whoami_ok, + nxt_process_whoami_error, main_port->pid, process); if (nxt_slow_path(stream == 0)) { nxt_mp_free(main_port->mem_pool, buf); @@ -789,8 +759,8 @@ nxt_process_whoami(nxt_task_t *task, nxt_process_t *process) fd = (process->parent_port != main_port) ? my_port->pair[1] : -1; - ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_WHOAMI, - fd, stream, my_port->id, buf); + ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_WHOAMI, fd, + stream, my_port->id, buf); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "%s failed to send WHOAMI message", process->name); @@ -803,15 +773,13 @@ nxt_process_whoami(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - static void -nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) -{ +nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) { nxt_pid_t pid, isolated_pid; - nxt_buf_t *buf; - nxt_port_t *port; - nxt_process_t *process; - nxt_runtime_t *rt; + nxt_buf_t *buf; + nxt_port_t *port; + nxt_process_t *process; + nxt_runtime_t *rt; process = data; @@ -824,12 +792,13 @@ nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) isolated_pid = nxt_pid; if (isolated_pid != pid) { - nxt_pid = pid; + nxt_pid = pid; process->pid = pid; nxt_process_port_each(process, port) { port->pid = pid; - } nxt_process_port_loop; + } + nxt_process_port_loop; } rt = task->thread->runtime; @@ -838,7 +807,7 @@ nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) port = process->parent_port; (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_PROCESS_CREATED, - -1, 0, 0, NULL); + -1, 0, 0, NULL); nxt_log(task, NXT_LOG_INFO, "%s started", process->name); } @@ -848,44 +817,40 @@ nxt_process_whoami_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) } } - static void -nxt_process_whoami_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) -{ +nxt_process_whoami_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data) { nxt_alert(task, "WHOAMI error"); nxt_process_quit(task, 1); } - static nxt_int_t -nxt_process_send_created(nxt_task_t *task, nxt_process_t *process) -{ - uint32_t stream; - nxt_int_t ret; - nxt_port_t *my_port, *main_port; - nxt_runtime_t *rt; +nxt_process_send_created(nxt_task_t *task, nxt_process_t *process) { + uint32_t stream; + nxt_int_t ret; + nxt_port_t *my_port, *main_port; + nxt_runtime_t *rt; nxt_assert(process->state == NXT_PROCESS_STATE_CREATED); rt = task->thread->runtime; - my_port = nxt_process_port_first(process); + my_port = nxt_process_port_first(process); main_port = rt->port_by_type[NXT_PROCESS_MAIN]; nxt_assert(my_port != NULL && main_port != NULL); - stream = nxt_port_rpc_register_handler(task, my_port, - nxt_process_created_ok, - nxt_process_created_error, - main_port->pid, process); + stream + = nxt_port_rpc_register_handler(task, my_port, nxt_process_created_ok, + nxt_process_created_error, main_port->pid, process); if (nxt_slow_path(stream == 0)) { return NXT_ERROR; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_PROCESS_CREATED, - -1, stream, my_port->id, NULL); + -1, stream, my_port->id, NULL); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "%s failed to send CREATED message", process->name); @@ -898,13 +863,11 @@ nxt_process_send_created(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - static void -nxt_process_created_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) -{ +nxt_process_created_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) { nxt_int_t ret; - nxt_process_t *process; - nxt_process_init_t *init; + nxt_process_t *process; + nxt_process_init_t *init; process = data; @@ -938,27 +901,23 @@ nxt_process_created_ok(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data) nxt_process_quit(task, 1); } - static void nxt_process_created_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_process_t *process; - nxt_process_init_t *init; + void *data) { + nxt_process_t *process; + nxt_process_init_t *init; process = data; - init = nxt_process_init(process); + init = nxt_process_init(process); nxt_alert(task, "%s failed to start", init->name); nxt_process_quit(task, 1); } - nxt_int_t -nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process) -{ - nxt_int_t ret; +nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process) { + nxt_int_t ret; ret = nxt_process_apply_creds(task, process); if (nxt_slow_path(ret != NXT_OK)) { @@ -970,15 +929,13 @@ nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process, nxt_str_t *user, - nxt_str_t *group) -{ - char *str; + nxt_str_t *group) { + char *str; - process->user_cred = nxt_mp_zalloc(process->mem_pool, - sizeof(nxt_credential_t)); + process->user_cred + = nxt_mp_zalloc(process->mem_pool, sizeof(nxt_credential_t)); if (nxt_slow_path(process->user_cred == NULL)) { return NXT_ERROR; @@ -1010,12 +967,10 @@ nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process, nxt_str_t *user, return nxt_credential_get(task, process->mem_pool, process->user_cred, str); } - nxt_int_t -nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process) -{ +nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process) { nxt_int_t ret, cap_setid; - nxt_runtime_t *rt; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -1023,8 +978,7 @@ nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process) #if (NXT_HAVE_LINUX_NS && NXT_HAVE_CLONE_NEWUSER) if (!cap_setid - && nxt_is_clone_flag_set(process->isolation.clone.flags, NEWUSER)) - { + && nxt_is_clone_flag_set(process->isolation.clone.flags, NEWUSER)) { cap_setid = 1; } #endif @@ -1043,8 +997,7 @@ nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process) #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) if (nxt_slow_path(process->isolation.new_privs == 0 - && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0)) - { + && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0)) { nxt_alert(task, "failed to set no_new_privs %E", nxt_errno); return NXT_ERROR; } @@ -1053,15 +1006,12 @@ nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - static nxt_int_t -nxt_process_send_ready(nxt_task_t *task, nxt_process_t *process) -{ - nxt_int_t ret; +nxt_process_send_ready(nxt_task_t *task, nxt_process_t *process) { + nxt_int_t ret; ret = nxt_port_socket_write(task, process->parent_port, - NXT_PORT_MSG_PROCESS_READY, - -1, process->stream, 0, NULL); + NXT_PORT_MSG_PROCESS_READY, -1, process->stream, 0, NULL); if (nxt_slow_path(ret != NXT_OK)) { nxt_alert(task, "%s failed to send READY message", process->name); @@ -1073,7 +1023,6 @@ nxt_process_send_ready(nxt_task_t *task, nxt_process_t *process) return NXT_OK; } - /* * Linux glibc 2.2 posix_spawn() is implemented via fork()/execve(). * Linux glibc 2.4 posix_spawn() without file actions and spawn @@ -1094,9 +1043,8 @@ nxt_process_send_ready(nxt_task_t *task, nxt_process_t *process) */ nxt_pid_t -nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp) -{ - nxt_pid_t pid; +nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp) { + nxt_pid_t pid; nxt_debug(task, "posix_spawn(\"%s\")", name); @@ -1108,13 +1056,11 @@ nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp) return pid; } - nxt_int_t -nxt_process_daemon(nxt_task_t *task) -{ - nxt_fd_t fd; - nxt_pid_t pid; - const char *msg; +nxt_process_daemon(nxt_task_t *task) { + nxt_fd_t fd; + nxt_pid_t pid; + const char *msg; fd = -1; @@ -1127,7 +1073,6 @@ nxt_process_daemon(nxt_task_t *task) pid = fork(); switch (pid) { - case -1: msg = "fork() failed %E"; goto fail; @@ -1198,22 +1143,19 @@ nxt_process_daemon(nxt_task_t *task) return NXT_ERROR; } - void -nxt_nanosleep(nxt_nsec_t ns) -{ - struct timespec ts; +nxt_nanosleep(nxt_nsec_t ns) { + struct timespec ts; - ts.tv_sec = ns / 1000000000; + ts.tv_sec = ns / 1000000000; ts.tv_nsec = ns % 1000000000; (void) nanosleep(&ts, NULL); } - void -nxt_process_port_add(nxt_task_t *task, nxt_process_t *process, nxt_port_t *port) -{ +nxt_process_port_add(nxt_task_t *task, nxt_process_t *process, + nxt_port_t *port) { nxt_assert(port->process == NULL); port->process = process; @@ -1222,51 +1164,43 @@ nxt_process_port_add(nxt_task_t *task, nxt_process_t *process, nxt_port_t *port) nxt_process_use(task, process, 1); } - nxt_process_type_t -nxt_process_type(nxt_process_t *process) -{ - return nxt_queue_is_empty(&process->ports) ? 0 : - (nxt_process_port_first(process))->type; +nxt_process_type(nxt_process_t *process) { + return nxt_queue_is_empty(&process->ports) + ? 0 + : (nxt_process_port_first(process))->type; } - void -nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process) -{ - nxt_port_t *port; +nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process) { + nxt_port_t *port; nxt_process_use(task, process, 1); nxt_process_port_each(process, port) { - nxt_port_close(task, port); nxt_runtime_port_remove(task, port); - - } nxt_process_port_loop; + } + nxt_process_port_loop; nxt_process_use(task, process, -1); } - void -nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status) -{ - nxt_queue_t *listen; - nxt_queue_link_t *link, *next; - nxt_listen_event_t *lev; +nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status) { + nxt_queue_t *listen; + nxt_queue_link_t *link, *next; + nxt_listen_event_t *lev; nxt_debug(task, "close listen connections"); listen = &task->thread->engine->listen_connections; - for (link = nxt_queue_first(listen); - link != nxt_queue_tail(listen); - link = next) - { + for (link = nxt_queue_first(listen); link != nxt_queue_tail(listen); + link = next) { next = nxt_queue_next(link); - lev = nxt_queue_link_data(link, nxt_listen_event_t, link); + lev = nxt_queue_link_data(link, nxt_listen_event_t, link); nxt_queue_remove(link); nxt_fd_event_close(task->thread->engine, &lev->socket); diff --git a/src/nxt_process.h b/src/nxt_process.h index 42fd1bed2..0feedfedc 100644 --- a/src/nxt_process.h +++ b/src/nxt_process.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -13,36 +12,32 @@ #endif -typedef pid_t nxt_pid_t; +typedef pid_t nxt_pid_t; typedef struct nxt_common_app_conf_s nxt_common_app_conf_t; - typedef struct { - nxt_runtime_t *rt; + nxt_runtime_t *rt; } nxt_discovery_init_t; - typedef struct { - nxt_str_t conf; + nxt_str_t conf; #if (NXT_TLS) - nxt_array_t *certs; + nxt_array_t *certs; #endif #if (NXT_HAVE_NJS) - nxt_array_t *scripts; + nxt_array_t *scripts; #endif } nxt_controller_init_t; - typedef union { - void *discovery; - nxt_controller_init_t controller; - void *router; - nxt_common_app_conf_t *app; + void *discovery; + nxt_controller_init_t controller; + void *router; + nxt_common_app_conf_t *app; } nxt_process_data_t; - typedef enum { NXT_PROCESS_STATE_CREATING = 0, NXT_PROCESS_STATE_CREATED, @@ -50,192 +45,195 @@ typedef enum { } nxt_process_state_t; -typedef struct nxt_port_mmap_s nxt_port_mmap_t; -typedef struct nxt_process_s nxt_process_t; -typedef struct nxt_cgroup_s nxt_cgroup_t; -typedef void (*nxt_isolation_cleanup_t)(nxt_task_t *task, - nxt_process_t *process); -typedef void (*nxt_cgroup_cleanup_t)(nxt_task_t *task, - const nxt_process_t *process); - +typedef struct nxt_port_mmap_s nxt_port_mmap_t; +typedef struct nxt_process_s nxt_process_t; +typedef struct nxt_cgroup_s nxt_cgroup_t; +typedef void ( + *nxt_isolation_cleanup_t)(nxt_task_t *task, nxt_process_t *process); +typedef void ( + *nxt_cgroup_cleanup_t)(nxt_task_t *task, const nxt_process_t *process); typedef struct { - nxt_thread_mutex_t mutex; - uint32_t size; - uint32_t cap; - nxt_port_mmap_t *elts; + nxt_thread_mutex_t mutex; + uint32_t size; + uint32_t cap; + nxt_port_mmap_t *elts; } nxt_port_mmaps_t; - typedef struct { - uint8_t language_deps; /* 1-bit */ - uint8_t tmpfs; /* 1-bit */ - uint8_t procfs; /* 1-bit */ + uint8_t language_deps; /* 1-bit */ + uint8_t tmpfs; /* 1-bit */ + uint8_t procfs; /* 1-bit */ } nxt_process_automount_t; - struct nxt_cgroup_s { - char *path; + char *path; }; - typedef struct { - u_char *rootfs; - nxt_process_automount_t automount; - nxt_array_t *mounts; /* of nxt_mount_t */ + u_char *rootfs; + nxt_process_automount_t automount; + nxt_array_t *mounts; /* of nxt_mount_t */ - nxt_isolation_cleanup_t cleanup; + nxt_isolation_cleanup_t cleanup; - nxt_cgroup_cleanup_t cgroup_cleanup; + nxt_cgroup_cleanup_t cgroup_cleanup; #if (NXT_HAVE_CGROUP) - nxt_cgroup_t cgroup; + nxt_cgroup_t cgroup; #endif #if (NXT_HAVE_LINUX_NS) - nxt_clone_t clone; + nxt_clone_t clone; #endif #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS) - uint8_t new_privs; /* 1 bit */ + uint8_t new_privs; /* 1 bit */ #endif } nxt_process_isolation_t; - struct nxt_process_s { - nxt_pid_t pid; - nxt_queue_t ports; /* of nxt_port_t.link */ - nxt_process_state_t state; - nxt_bool_t registered; - nxt_int_t use_count; + nxt_pid_t pid; + nxt_queue_t ports; /* of nxt_port_t.link */ + nxt_process_state_t state; + nxt_bool_t registered; + nxt_int_t use_count; - nxt_port_mmaps_t incoming; + nxt_port_mmaps_t incoming; - nxt_pid_t isolated_pid; - const char *name; - nxt_port_t *parent_port; + nxt_pid_t isolated_pid; + const char *name; + nxt_port_t *parent_port; - uint32_t stream; + uint32_t stream; - nxt_mp_t *mem_pool; - nxt_credential_t *user_cred; + nxt_mp_t *mem_pool; + nxt_credential_t *user_cred; - nxt_queue_t children; /* of nxt_process_t.link */ - nxt_queue_link_t link; /* for nxt_process_t.children */ + nxt_queue_t children; /* of nxt_process_t.link */ + nxt_queue_link_t link; /* for nxt_process_t.children */ - nxt_process_data_t data; + nxt_process_data_t data; - nxt_process_isolation_t isolation; + nxt_process_isolation_t isolation; }; - typedef nxt_int_t (*nxt_process_prefork_t)(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp); typedef nxt_int_t (*nxt_process_postfork_t)(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp); -typedef nxt_int_t (*nxt_process_setup_t)(nxt_task_t *task, - nxt_process_t *process); -typedef nxt_int_t (*nxt_process_start_t)(nxt_task_t *task, - nxt_process_data_t *data); - +typedef nxt_int_t ( + *nxt_process_setup_t)(nxt_task_t *task, nxt_process_t *process); +typedef nxt_int_t ( + *nxt_process_start_t)(nxt_task_t *task, nxt_process_data_t *data); typedef struct { - const char *name; - nxt_process_type_t type; + const char *name; + nxt_process_type_t type; - nxt_process_prefork_t prefork; + nxt_process_prefork_t prefork; - nxt_process_setup_t setup; - nxt_process_start_t start; + nxt_process_setup_t setup; + nxt_process_start_t start; - uint8_t restart; /* 1-bit */ + uint8_t restart; /* 1-bit */ - const nxt_port_handlers_t *port_handlers; - const nxt_sig_event_t *signals; + const nxt_port_handlers_t *port_handlers; + const nxt_sig_event_t *signals; - nxt_queue_t *siblings; + nxt_queue_t *siblings; } nxt_process_init_t; +extern uint8_t nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; +extern uint8_t nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; +extern uint8_t nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; -extern uint8_t nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; -extern uint8_t nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; -extern uint8_t nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX]; +NXT_EXPORT nxt_pid_t +nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp); +NXT_EXPORT nxt_int_t +nxt_process_daemon(nxt_task_t *task); +NXT_EXPORT void +nxt_nanosleep(nxt_nsec_t ns); -NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name, - char **argv, char **envp); -NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task); -NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns); +NXT_EXPORT void +nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp); -NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv, - char ***orig_envp); - -#define nxt_process_init(process) \ +#define nxt_process_init(process) \ (nxt_pointer_to(process, sizeof(nxt_process_t))) -#define nxt_process_port_remove(port) \ - nxt_queue_remove(&port->link) +#define nxt_process_port_remove(port) nxt_queue_remove(&port->link) -#define nxt_process_port_first(process) \ +#define nxt_process_port_first(process) \ nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link) -NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process, +NXT_EXPORT void +nxt_process_port_add(nxt_task_t *task, nxt_process_t *process, nxt_port_t *port); #define nxt_process_port_each(process, port) \ nxt_queue_each(port, &process->ports, nxt_port_t, link) -#define nxt_process_port_loop \ - nxt_queue_loop - -nxt_process_t *nxt_process_new(nxt_runtime_t *rt); -void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i); -nxt_int_t nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init); -nxt_int_t nxt_process_start(nxt_task_t *task, nxt_process_t *process); -nxt_process_type_t nxt_process_type(nxt_process_t *process); - -void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i); - -void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process); - -void nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status); -void nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); - -nxt_int_t nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process); -nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process, - nxt_str_t *user, nxt_str_t *group); -nxt_int_t nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process); +#define nxt_process_port_loop nxt_queue_loop + +nxt_process_t * +nxt_process_new(nxt_runtime_t *rt); +void +nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i); +nxt_int_t +nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init); +nxt_int_t +nxt_process_start(nxt_task_t *task, nxt_process_t *process); +nxt_process_type_t +nxt_process_type(nxt_process_t *process); + +void +nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i); + +void +nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process); + +void +nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status); +void +nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); + +nxt_int_t +nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process); +nxt_int_t +nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process, nxt_str_t *user, + nxt_str_t *group); +nxt_int_t +nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process); #if (NXT_HAVE_SETPROCTITLE) -#define nxt_process_title(task, fmt, ...) \ - setproctitle(fmt, __VA_ARGS__) +#define nxt_process_title(task, fmt, ...) setproctitle(fmt, __VA_ARGS__) #elif (NXT_LINUX || NXT_SOLARIS || NXT_MACOSX) -#define NXT_SETPROCTITLE_ARGV 1 -NXT_EXPORT void nxt_process_title(nxt_task_t *task, const char *fmt, ...); +#define NXT_SETPROCTITLE_ARGV 1 +NXT_EXPORT void +nxt_process_title(nxt_task_t *task, const char *fmt, ...); #endif -#define nxt_sched_yield() \ - sched_yield() +#define nxt_sched_yield() sched_yield() /* * Solaris declares abort() as __NORETURN, * raise(SIGABRT) is mostly the same. */ -#define nxt_abort() \ - (void) raise(SIGABRT) +#define nxt_abort() (void) raise(SIGABRT) -NXT_EXPORT extern nxt_pid_t nxt_pid; -NXT_EXPORT extern nxt_pid_t nxt_ppid; -NXT_EXPORT extern nxt_uid_t nxt_euid; -NXT_EXPORT extern nxt_gid_t nxt_egid; -NXT_EXPORT extern char **nxt_process_argv; -NXT_EXPORT extern char ***nxt_process_environ; +NXT_EXPORT extern nxt_pid_t nxt_pid; +NXT_EXPORT extern nxt_pid_t nxt_ppid; +NXT_EXPORT extern nxt_uid_t nxt_euid; +NXT_EXPORT extern nxt_gid_t nxt_egid; +NXT_EXPORT extern char **nxt_process_argv; +NXT_EXPORT extern char ***nxt_process_environ; #endif /* _NXT_PROCESS_H_INCLUDED_ */ diff --git a/src/nxt_process_title.c b/src/nxt_process_title.c index 3c20ac594..42fa3f86c 100644 --- a/src/nxt_process_title.c +++ b/src/nxt_process_title.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,7 +7,7 @@ /* The arguments passed to main(). */ -char **nxt_process_argv; +char **nxt_process_argv; /* * MacOSX environ(7): @@ -20,7 +19,7 @@ char **nxt_process_argv; * So nxt_process_environ contains an address of environ to allow * change environ[] placement. */ -char ***nxt_process_environ; +char ***nxt_process_environ; #if (NXT_SETPROCTITLE_ARGV) @@ -38,18 +37,16 @@ char ***nxt_process_environ; */ -static u_char *nxt_process_title_start; -static u_char *nxt_process_title_end; - +static u_char *nxt_process_title_start; +static u_char *nxt_process_title_end; void -nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) -{ - u_char *p, *end, *argv_end, **argv, **env; - size_t size, argv_size, environ_size, strings_size; - nxt_uint_t i; +nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) { + u_char *p, *end, *argv_end, **argv, **env; + size_t size, argv_size, environ_size, strings_size; + nxt_uint_t i; - nxt_process_argv = orig_argv; + nxt_process_argv = orig_argv; nxt_process_environ = orig_envp; if (orig_envp == NULL) { @@ -60,22 +57,22 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) * Set a conservative title space for a case if program argument * strings and environment strings are not contiguous. */ - argv = (u_char **) orig_argv; + argv = (u_char **) orig_argv; nxt_process_title_start = argv[0]; - nxt_process_title_end = argv[0] + nxt_strlen(argv[0]); + nxt_process_title_end = argv[0] + nxt_strlen(argv[0]); - end = argv[0]; + end = argv[0]; strings_size = 0; - argv_size = sizeof(void *); + argv_size = sizeof(void *); for (i = 0; argv[i] != NULL; i++) { argv_size += sizeof(void *); if (argv[i] == end) { /* Argument strings are contiguous. */ - size = nxt_strlen(argv[i]) + 1; + size = nxt_strlen(argv[i]) + 1; strings_size += size; - end = argv[i] + size; + end = argv[i] + size; } } @@ -101,8 +98,8 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) nxt_process_argv = (char **) argv; - argv_end = end; - env = (u_char **) *orig_envp; + argv_end = end; + env = (u_char **) *orig_envp; environ_size = sizeof(void *); for (i = 0; env[i] != NULL; i++) { @@ -110,9 +107,9 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) if (env[i] == end) { /* Environment strings are contiguous. */ - size = nxt_strlen(env[i]) + 1; + size = nxt_strlen(env[i]) + 1; strings_size += size; - end = env[i] + size; + end = env[i] + size; } } @@ -133,7 +130,6 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) end = argv[0]; for (i = 0; argv[i] != NULL; i++) { - if (argv[i] != end) { /* Argument strings are not contiguous. */ goto done; @@ -142,9 +138,9 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) size = nxt_strlen(argv[i]) + 1; nxt_memcpy(p, argv[i], size); - end = argv[i] + size; - argv[i] = p; - p += size; + end = argv[i] + size; + argv[i] = p; + p += size; } env = nxt_malloc(environ_size); @@ -163,7 +159,6 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) *orig_envp = (char **) env; for (i = 0; env[i] != NULL; i++) { - if (env[i] != end) { /* Environment strings are not contiguous. */ goto done; @@ -172,9 +167,9 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) size = nxt_strlen(env[i]) + 1; nxt_memcpy(p, env[i], size); - end = env[i] + size; - env[i] = p; - p += size; + end = env[i] + size; + env[i] = p; + p += size; } done: @@ -185,12 +180,10 @@ nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) nxt_process_title_end = end; } - void -nxt_process_title(nxt_task_t *task, const char *fmt, ...) -{ - u_char *p, *start, *end; - va_list args; +nxt_process_title(nxt_task_t *task, const char *fmt, ...) { + u_char *p, *start, *end; + va_list args; start = nxt_process_title_start; @@ -211,8 +204,8 @@ nxt_process_title(nxt_task_t *task, const char *fmt, ...) * to append the original command line in parenthesis to the title. */ { - size_t size; - nxt_uint_t i; + size_t size; + nxt_uint_t i; size = 0; @@ -221,7 +214,6 @@ nxt_process_title(nxt_task_t *task, const char *fmt, ...) } if (size > (size_t) (p - start)) { - p = nxt_sprintf(p, end, " ("); for (i = 0; nxt_process_argv[i] != NULL; i++) { @@ -247,9 +239,8 @@ nxt_process_title(nxt_task_t *task, const char *fmt, ...) #else /* !(NXT_SETPROCTITLE_ARGV) */ void -nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) -{ - nxt_process_argv = orig_argv; +nxt_process_arguments(nxt_task_t *task, char **orig_argv, char ***orig_envp) { + nxt_process_argv = orig_argv; nxt_process_environ = orig_envp; } diff --git a/src/nxt_process_type.h b/src/nxt_process_type.h index d0093431e..383e8e714 100644 --- a/src/nxt_process_type.h +++ b/src/nxt_process_type.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_PROCESS_TYPE_H_INCLUDED_ #define _NXT_PROCESS_TYPE_H_INCLUDED_ - typedef enum { NXT_PROCESS_MAIN = 0, NXT_PROCESS_DISCOVERY, diff --git a/src/nxt_queue.c b/src/nxt_queue.c index c81356dbd..a0b53d6a3 100644 --- a/src/nxt_queue.c +++ b/src/nxt_queue.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,16 +6,14 @@ #include - /* * Find the middle queue element if the queue has odd number of elements, * or the first element of the queue's second part otherwise. */ nxt_queue_link_t * -nxt_queue_middle(nxt_queue_t *queue) -{ - nxt_queue_link_t *middle, *next; +nxt_queue_middle(nxt_queue_t *queue) { + nxt_queue_link_t *middle, *next; middle = nxt_queue_first(queue); @@ -26,7 +23,7 @@ nxt_queue_middle(nxt_queue_t *queue) next = middle; - for ( ;; ) { + for (;;) { middle = nxt_queue_next(middle); next = nxt_queue_next(next); @@ -43,7 +40,6 @@ nxt_queue_middle(nxt_queue_t *queue) } } - /* * nxt_queue_sort() provides a stable sort because it uses the insertion * sort algorithm. Its worst and average computational complexity is O^2. @@ -51,10 +47,10 @@ nxt_queue_middle(nxt_queue_t *queue) void nxt_queue_sort(nxt_queue_t *queue, - nxt_int_t (*cmp)(const void *data, const nxt_queue_link_t *, - const nxt_queue_link_t *), const void *data) -{ - nxt_queue_link_t *link, *prev, *next; + nxt_int_t (*cmp)(const void *data, const nxt_queue_link_t *, + const nxt_queue_link_t *), + const void *data) { + nxt_queue_link_t *link, *prev, *next; link = nxt_queue_first(queue); @@ -62,10 +58,8 @@ nxt_queue_sort(nxt_queue_t *queue, return; } - for (link = nxt_queue_next(link); - link != nxt_queue_tail(queue); - link = next) - { + for (link = nxt_queue_next(link); link != nxt_queue_tail(queue); + link = next) { prev = nxt_queue_prev(link); next = nxt_queue_next(link); diff --git a/src/nxt_queue.h b/src/nxt_queue.h index 6b7f5d577..910fb4304 100644 --- a/src/nxt_queue.h +++ b/src/nxt_queue.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,30 +7,28 @@ #define _NXT_QUEUE_H_INCLUDED_ -typedef struct nxt_queue_link_s nxt_queue_link_t; +typedef struct nxt_queue_link_s nxt_queue_link_t; struct nxt_queue_link_s { - nxt_queue_link_t *prev; - nxt_queue_link_t *next; + nxt_queue_link_t *prev; + nxt_queue_link_t *next; }; - typedef struct { - nxt_queue_link_t head; + nxt_queue_link_t head; } nxt_queue_t; - -#define nxt_queue_init(queue) \ - do { \ - (queue)->head.prev = &(queue)->head; \ - (queue)->head.next = &(queue)->head; \ +#define nxt_queue_init(queue) \ + do { \ + (queue)->head.prev = &(queue)->head; \ + (queue)->head.next = &(queue)->head; \ } while (0) -#define nxt_queue_sentinel(link) \ - do { \ - (link)->prev = (link); \ - (link)->next = (link); \ +#define nxt_queue_sentinel(link) \ + do { \ + (link)->prev = (link); \ + (link)->next = (link); \ } while (0) @@ -40,12 +37,10 @@ typedef struct { * using nxt_queue_remove(). */ -#define nxt_queue_self(link) \ - nxt_queue_sentinel(link) +#define nxt_queue_self(link) nxt_queue_sentinel(link) -#define nxt_queue_is_empty(queue) \ - (&(queue)->head == (queue)->head.prev) +#define nxt_queue_is_empty(queue) (&(queue)->head == (queue)->head.prev) /* * A loop to iterate all queue links starting from head: @@ -69,82 +64,76 @@ typedef struct { * tp = nxt_queue_link_data(lnk, nxt_type_t, link); */ -#define nxt_queue_first(queue) \ - (queue)->head.next +#define nxt_queue_first(queue) (queue)->head.next -#define nxt_queue_last(queue) \ - (queue)->head.prev +#define nxt_queue_last(queue) (queue)->head.prev -#define nxt_queue_head(queue) \ - (&(queue)->head) +#define nxt_queue_head(queue) (&(queue)->head) -#define nxt_queue_tail(queue) \ - (&(queue)->head) +#define nxt_queue_tail(queue) (&(queue)->head) -#define nxt_queue_next(link) \ - (link)->next +#define nxt_queue_next(link) (link)->next -#define nxt_queue_prev(link) \ - (link)->prev +#define nxt_queue_prev(link) (link)->prev -#define nxt_queue_insert_head(queue, link) \ - do { \ - (link)->next = (queue)->head.next; \ - (link)->next->prev = (link); \ - (link)->prev = &(queue)->head; \ - (queue)->head.next = (link); \ +#define nxt_queue_insert_head(queue, link) \ + do { \ + (link)->next = (queue)->head.next; \ + (link)->next->prev = (link); \ + (link)->prev = &(queue)->head; \ + (queue)->head.next = (link); \ } while (0) -#define nxt_queue_insert_tail(queue, link) \ - do { \ - (link)->prev = (queue)->head.prev; \ - (link)->prev->next = (link); \ - (link)->next = &(queue)->head; \ - (queue)->head.prev = (link); \ +#define nxt_queue_insert_tail(queue, link) \ + do { \ + (link)->prev = (queue)->head.prev; \ + (link)->prev->next = (link); \ + (link)->next = &(queue)->head; \ + (queue)->head.prev = (link); \ } while (0) -#define nxt_queue_insert_after(target, link) \ - do { \ - (link)->next = (target)->next; \ - (link)->next->prev = (link); \ - (link)->prev = (target); \ - (target)->next = (link); \ +#define nxt_queue_insert_after(target, link) \ + do { \ + (link)->next = (target)->next; \ + (link)->next->prev = (link); \ + (link)->prev = (target); \ + (target)->next = (link); \ } while (0) -#define nxt_queue_insert_before(target, link) \ - do { \ - (link)->next = (target); \ - (link)->prev = (target)->prev; \ - (target)->prev = (link); \ - (link)->prev->next = (link); \ +#define nxt_queue_insert_before(target, link) \ + do { \ + (link)->next = (target); \ + (link)->prev = (target)->prev; \ + (target)->prev = (link); \ + (link)->prev->next = (link); \ } while (0) #if (NXT_DEBUG) -#define nxt_queue_remove(link) \ - do { \ - (link)->next->prev = (link)->prev; \ - (link)->prev->next = (link)->next; \ - (link)->prev = NULL; \ - (link)->next = NULL; \ +#define nxt_queue_remove(link) \ + do { \ + (link)->next->prev = (link)->prev; \ + (link)->prev->next = (link)->next; \ + (link)->prev = NULL; \ + (link)->next = NULL; \ } while (0) #else -#define nxt_queue_remove(link) \ - do { \ - (link)->next->prev = (link)->prev; \ - (link)->prev->next = (link)->next; \ +#define nxt_queue_remove(link) \ + do { \ + (link)->next->prev = (link)->prev; \ + (link)->prev->next = (link)->next; \ } while (0) #endif @@ -155,23 +144,23 @@ typedef struct { * the "tail" is the new tail queue. */ -#define nxt_queue_split(queue, link, tail) \ - do { \ - (tail)->head.prev = (queue)->head.prev; \ - (tail)->head.prev->next = &(tail)->head; \ - (tail)->head.next = (link); \ - (queue)->head.prev = (link)->prev; \ - (queue)->head.prev->next = &(queue)->head; \ - (link)->prev = &(tail)->head; \ +#define nxt_queue_split(queue, link, tail) \ + do { \ + (tail)->head.prev = (queue)->head.prev; \ + (tail)->head.prev->next = &(tail)->head; \ + (tail)->head.next = (link); \ + (queue)->head.prev = (link)->prev; \ + (queue)->head.prev->next = &(queue)->head; \ + (link)->prev = &(tail)->head; \ } while (0) /* Truncate the queue "queue" starting at element "link". */ -#define nxt_queue_truncate(queue, link) \ - do { \ - (queue)->head.prev = (link)->prev; \ - (queue)->head.prev->next = &(queue)->head; \ +#define nxt_queue_truncate(queue, link) \ + do { \ + (queue)->head.prev = (link)->prev; \ + (queue)->head.prev->next = &(queue)->head; \ } while (0) @@ -181,39 +170,40 @@ typedef struct { * it must be initiated with nxt_queue_init(tail). */ -#define nxt_queue_add(queue, tail) \ - do { \ - (queue)->head.prev->next = (tail)->head.next; \ - (tail)->head.next->prev = (queue)->head.prev; \ - (queue)->head.prev = (tail)->head.prev; \ - (queue)->head.prev->next = &(queue)->head; \ +#define nxt_queue_add(queue, tail) \ + do { \ + (queue)->head.prev->next = (tail)->head.next; \ + (tail)->head.next->prev = (queue)->head.prev; \ + (queue)->head.prev = (tail)->head.prev; \ + (queue)->head.prev->next = &(queue)->head; \ } while (0) -#define nxt_queue_link_data(lnk, type, link) \ - nxt_container_of(lnk, type, link) +#define nxt_queue_link_data(lnk, type, link) nxt_container_of(lnk, type, link) -NXT_EXPORT nxt_queue_link_t *nxt_queue_middle(nxt_queue_t *queue); -NXT_EXPORT void nxt_queue_sort(nxt_queue_t *queue, - nxt_int_t (*cmp)(const void *, const nxt_queue_link_t *, - const nxt_queue_link_t *), const void *data); +NXT_EXPORT nxt_queue_link_t * +nxt_queue_middle(nxt_queue_t *queue); +NXT_EXPORT void +nxt_queue_sort(nxt_queue_t *queue, + nxt_int_t ( + *cmp)(const void *, const nxt_queue_link_t *, const nxt_queue_link_t *), + const void *data); -#define nxt_queue_each(elt, queue, type, link) \ - do { \ - nxt_queue_link_t *_lnk, *_nxt; \ - \ - for (_lnk = nxt_queue_first(queue); \ - _lnk != nxt_queue_tail(queue); \ - _lnk = _nxt) { \ - \ - _nxt = nxt_queue_next(_lnk); \ - elt = nxt_queue_link_data(_lnk, type, link); \ +#define nxt_queue_each(elt, queue, type, link) \ + do { \ + nxt_queue_link_t *_lnk, *_nxt; \ + \ + for (_lnk = nxt_queue_first(queue); _lnk != nxt_queue_tail(queue); \ + _lnk = _nxt) { \ + _nxt = nxt_queue_next(_lnk); \ + elt = nxt_queue_link_data(_lnk, type, link); -#define nxt_queue_loop \ - } \ - } while(0) +#define nxt_queue_loop \ + } \ + } \ + while (0) #endif /* _NXT_QUEUE_H_INCLUDED_ */ diff --git a/src/nxt_random.c b/src/nxt_random.c index 8290488c8..ba12527e7 100644 --- a/src/nxt_random.c +++ b/src/nxt_random.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -15,28 +14,28 @@ */ -#define NXT_RANDOM_KEY_SIZE 128 - +#define NXT_RANDOM_KEY_SIZE 128 -nxt_inline void nxt_random_start_schedule(nxt_random_t *r); -static void nxt_random_stir(nxt_random_t *r); -static void nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len); -nxt_inline uint8_t nxt_random_byte(nxt_random_t *r); +nxt_inline void +nxt_random_start_schedule(nxt_random_t *r); +static void +nxt_random_stir(nxt_random_t *r); +static void +nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len); +nxt_inline uint8_t +nxt_random_byte(nxt_random_t *r); void -nxt_random_init(nxt_random_t *r) -{ +nxt_random_init(nxt_random_t *r) { nxt_random_start_schedule(r); nxt_random_stir(r); } - nxt_inline void -nxt_random_start_schedule(nxt_random_t *r) -{ - nxt_uint_t i; +nxt_random_start_schedule(nxt_random_t *r) { + nxt_uint_t i; r->i = 0; r->j = 0; @@ -46,16 +45,15 @@ nxt_random_start_schedule(nxt_random_t *r) } } - static void -nxt_random_stir(nxt_random_t *r) -{ - int fd; - ssize_t n; - struct timeval tv; +nxt_random_stir(nxt_random_t *r) { + int fd; + ssize_t n; + struct timeval tv; + union { - uint32_t value[4]; - u_char bytes[NXT_RANDOM_KEY_SIZE]; + uint32_t value[4]; + u_char bytes[NXT_RANDOM_KEY_SIZE]; } key; #if (NXT_HAVE_GETRANDOM) @@ -113,15 +111,13 @@ nxt_random_stir(nxt_random_t *r) r->count = 400000; } - static void -nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len) -{ - uint8_t val; - uint32_t n; +nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len) { + uint8_t val; + uint32_t n; for (n = 0; n < 256; n++) { - val = r->s[r->i]; + val = r->s[r->i]; r->j += val + key[n % len]; r->s[r->i] = r->s[r->j]; @@ -136,11 +132,9 @@ nxt_random_add(nxt_random_t *r, const u_char *key, uint32_t len) r->j = r->i; } - uint32_t -nxt_random(nxt_random_t *r) -{ - uint32_t val; +nxt_random(nxt_random_t *r) { + uint32_t val; r->count--; @@ -156,17 +150,15 @@ nxt_random(nxt_random_t *r) return val; } - nxt_inline uint8_t -nxt_random_byte(nxt_random_t *r) -{ - uint8_t si, sj; +nxt_random_byte(nxt_random_t *r) { + uint8_t si, sj; r->i++; - si = r->s[r->i]; + si = r->s[r->i]; r->j += si; - sj = r->s[r->j]; + sj = r->s[r->j]; r->s[r->i] = sj; r->s[r->j] = si; @@ -179,10 +171,9 @@ nxt_random_byte(nxt_random_t *r) #if (NXT_TESTS) nxt_int_t -nxt_random_test(nxt_thread_t *thr) -{ - nxt_uint_t n; - nxt_random_t r; +nxt_random_test(nxt_thread_t *thr) { + nxt_uint_t n; + nxt_random_t r; nxt_random_start_schedule(&r); @@ -196,7 +187,6 @@ nxt_random_test(nxt_thread_t *thr) */ if (nxt_random(&r) == 0xD6270B27) { - for (n = 100000; n != 0; n--) { (void) nxt_random(&r); } diff --git a/src/nxt_random.h b/src/nxt_random.h index 962fdf8fa..e389cc5c5 100644 --- a/src/nxt_random.h +++ b/src/nxt_random.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,20 +6,21 @@ #ifndef _NXT_RANDOM_H_INCLUDED_ #define _NXT_RANDOM_H_INCLUDED_ - typedef struct { - uint8_t i; - uint8_t j; - uint8_t s[256]; - int32_t count; + uint8_t i; + uint8_t j; + uint8_t s[256]; + int32_t count; } nxt_random_t; - -void nxt_random_init(nxt_random_t *r); -uint32_t nxt_random(nxt_random_t *r); +void +nxt_random_init(nxt_random_t *r); +uint32_t +nxt_random(nxt_random_t *r); #if (NXT_TESTS) -nxt_int_t nxt_random_test(nxt_thread_t *thr); +nxt_int_t +nxt_random_test(nxt_thread_t *thr); #endif diff --git a/src/nxt_rbtree.c b/src/nxt_rbtree.c index 4a0f9c22a..17dbf27d2 100644 --- a/src/nxt_rbtree.c +++ b/src/nxt_rbtree.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -13,26 +12,27 @@ */ -static void nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node); -static void nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, - nxt_rbtree_node_t *node); -nxt_inline void nxt_rbtree_left_rotate(nxt_rbtree_node_t *node); -nxt_inline void nxt_rbtree_right_rotate(nxt_rbtree_node_t *node); -nxt_inline void nxt_rbtree_parent_relink(nxt_rbtree_node_t *subst, - nxt_rbtree_node_t *node); +static void +nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node); +static void +nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node); +nxt_inline void +nxt_rbtree_left_rotate(nxt_rbtree_node_t *node); +nxt_inline void +nxt_rbtree_right_rotate(nxt_rbtree_node_t *node); +nxt_inline void +nxt_rbtree_parent_relink(nxt_rbtree_node_t *subst, nxt_rbtree_node_t *node); -#define NXT_RBTREE_BLACK 0 -#define NXT_RBTREE_RED 1 +#define NXT_RBTREE_BLACK 0 +#define NXT_RBTREE_RED 1 -#define nxt_rbtree_comparison_callback(tree) \ +#define nxt_rbtree_comparison_callback(tree) \ ((nxt_rbtree_compare_t) (tree)->sentinel.right) - void -nxt_rbtree_init(nxt_rbtree_t *tree, nxt_rbtree_compare_t compare) -{ +nxt_rbtree_init(nxt_rbtree_t *tree, nxt_rbtree_compare_t compare) { /* * The sentinel is used as a leaf node sentinel and as a tree root * sentinel: it is a parent of a root node and the root node is @@ -55,24 +55,22 @@ nxt_rbtree_init(nxt_rbtree_t *tree, nxt_rbtree_compare_t compare) tree->sentinel.color = NXT_RBTREE_BLACK; } - void -nxt_rbtree_insert(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) -{ - nxt_rbtree_node_t *node, *new_node, *sentinel, **child; - nxt_rbtree_compare_t compare; +nxt_rbtree_insert(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) { + nxt_rbtree_node_t *node, *new_node, *sentinel, **child; + nxt_rbtree_compare_t compare; new_node = (nxt_rbtree_node_t *) part; - node = nxt_rbtree_root(tree); + node = nxt_rbtree_root(tree); sentinel = nxt_rbtree_sentinel(tree); - new_node->left = sentinel; + new_node->left = sentinel; new_node->right = sentinel; new_node->color = NXT_RBTREE_RED; compare = (nxt_rbtree_compare_t) tree->sentinel.right; - child = &nxt_rbtree_root(tree); + child = &nxt_rbtree_root(tree); while (*child != sentinel) { node = *child; @@ -83,27 +81,25 @@ nxt_rbtree_insert(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) child = (compare(new_node, node) < 0) ? &node->left : &node->right; } - *child = new_node; + *child = new_node; new_node->parent = node; nxt_rbtree_insert_fixup(new_node); - node = nxt_rbtree_root(tree); + node = nxt_rbtree_root(tree); node->color = NXT_RBTREE_BLACK; } - static void -nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *parent, *grandparent, *uncle; +nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *parent, *grandparent, *uncle; /* * Prefetching parent nodes does not help here because they are * already traversed during insertion. */ - for ( ;; ) { + for (;;) { parent = node->parent; /* @@ -120,7 +116,6 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) uncle = grandparent->right; if (uncle->color == NXT_RBTREE_BLACK) { - if (node == parent->right) { node = parent; nxt_rbtree_left_rotate(node); @@ -132,7 +127,7 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) */ parent = node->parent; - parent->color = NXT_RBTREE_BLACK; + parent->color = NXT_RBTREE_BLACK; grandparent->color = NXT_RBTREE_RED; nxt_rbtree_right_rotate(grandparent); @@ -148,7 +143,6 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) uncle = grandparent->left; if (uncle->color == NXT_RBTREE_BLACK) { - if (node == parent->left) { node = parent; nxt_rbtree_right_rotate(node); @@ -157,7 +151,7 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) /* See the comment in the symmetric branch above. */ parent = node->parent; - parent->color = NXT_RBTREE_BLACK; + parent->color = NXT_RBTREE_BLACK; grandparent->color = NXT_RBTREE_RED; nxt_rbtree_left_rotate(grandparent); @@ -167,27 +161,25 @@ nxt_rbtree_insert_fixup(nxt_rbtree_node_t *node) } } - uncle->color = NXT_RBTREE_BLACK; - parent->color = NXT_RBTREE_BLACK; + uncle->color = NXT_RBTREE_BLACK; + parent->color = NXT_RBTREE_BLACK; grandparent->color = NXT_RBTREE_RED; node = grandparent; } } - nxt_rbtree_node_t * -nxt_rbtree_find(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) -{ - intptr_t n; - nxt_rbtree_node_t *node, *next, *sentinel; - nxt_rbtree_compare_t compare; +nxt_rbtree_find(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) { + intptr_t n; + nxt_rbtree_node_t *node, *next, *sentinel; + nxt_rbtree_compare_t compare; node = (nxt_rbtree_node_t *) part; - next = nxt_rbtree_root(tree); + next = nxt_rbtree_root(tree); sentinel = nxt_rbtree_sentinel(tree); - compare = nxt_rbtree_comparison_callback(tree); + compare = nxt_rbtree_comparison_callback(tree); while (next != sentinel) { nxt_prefetch(next->left); @@ -209,20 +201,18 @@ nxt_rbtree_find(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) return NULL; } - nxt_rbtree_node_t * -nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) -{ - intptr_t n; - nxt_rbtree_node_t *node, *retval, *next, *sentinel; - nxt_rbtree_compare_t compare; +nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) { + intptr_t n; + nxt_rbtree_node_t *node, *retval, *next, *sentinel; + nxt_rbtree_compare_t compare; node = (nxt_rbtree_node_t *) part; - retval = NULL; - next = nxt_rbtree_root(tree); + retval = NULL; + next = nxt_rbtree_root(tree); sentinel = nxt_rbtree_sentinel(tree); - compare = nxt_rbtree_comparison_callback(tree); + compare = nxt_rbtree_comparison_callback(tree); while (next != sentinel) { nxt_prefetch(next->left); @@ -235,7 +225,7 @@ nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) } else if (n > 0) { retval = next; - next = next->right; + next = next->right; } else { /* Exact match. */ @@ -246,20 +236,18 @@ nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) return retval; } - nxt_rbtree_node_t * -nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) -{ - intptr_t n; - nxt_rbtree_node_t *node, *retval, *next, *sentinel; - nxt_rbtree_compare_t compare; +nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) { + intptr_t n; + nxt_rbtree_node_t *node, *retval, *next, *sentinel; + nxt_rbtree_compare_t compare; node = (nxt_rbtree_node_t *) part; - retval = NULL; - next = nxt_rbtree_root(tree); + retval = NULL; + next = nxt_rbtree_root(tree); sentinel = nxt_rbtree_sentinel(tree); - compare = nxt_rbtree_comparison_callback(tree); + compare = nxt_rbtree_comparison_callback(tree); while (next != sentinel) { nxt_prefetch(next->left); @@ -269,7 +257,7 @@ nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) if (n < 0) { retval = next; - next = next->left; + next = next->left; } else if (n > 0) { next = next->right; @@ -283,16 +271,14 @@ nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) return retval; } - void -nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) -{ +nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) { uint8_t color; - nxt_rbtree_node_t *node, *sentinel, *subst, *child; + nxt_rbtree_node_t *node, *sentinel, *subst, *child; node = (nxt_rbtree_node_t *) part; - subst = node; + subst = node; sentinel = nxt_rbtree_sentinel(tree); if (node->left == sentinel) { @@ -315,18 +301,18 @@ nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) subst->color = node->color; - subst->left = node->left; + subst->left = node->left; subst->left->parent = subst; - subst->right = node->right; + subst->right = node->right; subst->right->parent = subst; nxt_rbtree_parent_relink(subst, node); } #if (NXT_DEBUG) - node->left = NULL; - node->right = NULL; + node->left = NULL; + node->right = NULL; node->parent = NULL; #endif @@ -335,11 +321,9 @@ nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *part) } } - static void -nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *parent, *sibling; +nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *parent, *sibling; while (node != nxt_rbtree_root(tree) && node->color == NXT_RBTREE_BLACK) { /* @@ -353,9 +337,8 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) sibling = parent->right; if (sibling->color != NXT_RBTREE_BLACK) { - sibling->color = NXT_RBTREE_BLACK; - parent->color = NXT_RBTREE_RED; + parent->color = NXT_RBTREE_RED; nxt_rbtree_left_rotate(parent); @@ -363,7 +346,6 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) } if (sibling->right->color == NXT_RBTREE_BLACK) { - sibling->color = NXT_RBTREE_RED; if (sibling->left->color == NXT_RBTREE_BLACK) { @@ -393,8 +375,8 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) sibling = parent->right; } - sibling->color = parent->color; - parent->color = NXT_RBTREE_BLACK; + sibling->color = parent->color; + parent->color = NXT_RBTREE_BLACK; sibling->right->color = NXT_RBTREE_BLACK; nxt_rbtree_left_rotate(parent); @@ -405,9 +387,8 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) sibling = parent->left; if (sibling->color != NXT_RBTREE_BLACK) { - sibling->color = NXT_RBTREE_BLACK; - parent->color = NXT_RBTREE_RED; + parent->color = NXT_RBTREE_RED; nxt_rbtree_right_rotate(parent); @@ -415,7 +396,6 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) } if (sibling->left->color == NXT_RBTREE_BLACK) { - sibling->color = NXT_RBTREE_RED; if (sibling->right->color == NXT_RBTREE_BLACK) { @@ -431,8 +411,8 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) sibling = parent->left; } - sibling->color = parent->color; - parent->color = NXT_RBTREE_BLACK; + sibling->color = parent->color; + parent->color = NXT_RBTREE_BLACK; sibling->left->color = NXT_RBTREE_BLACK; nxt_rbtree_right_rotate(parent); @@ -444,47 +424,41 @@ nxt_rbtree_delete_fixup(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) node->color = NXT_RBTREE_BLACK; } - nxt_inline void -nxt_rbtree_left_rotate(nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *child; +nxt_rbtree_left_rotate(nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *child; - child = node->right; - node->right = child->left; + child = node->right; + node->right = child->left; child->left->parent = node; - child->left = node; + child->left = node; nxt_rbtree_parent_relink(child, node); node->parent = child; } - nxt_inline void -nxt_rbtree_right_rotate(nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *child; +nxt_rbtree_right_rotate(nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *child; - child = node->left; - node->left = child->right; + child = node->left; + node->left = child->right; child->right->parent = node; - child->right = node; + child->right = node; nxt_rbtree_parent_relink(child, node); node->parent = child; } - /* Relink a parent from the node to the subst node. */ nxt_inline void -nxt_rbtree_parent_relink(nxt_rbtree_node_t *subst, nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *parent, **link; +nxt_rbtree_parent_relink(nxt_rbtree_node_t *subst, nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *parent, **link; - parent = node->parent; + parent = node->parent; /* * The leaf sentinel's parent can be safely changed here. * See the comment in nxt_rbtree_delete_fixup() for details. @@ -494,26 +468,25 @@ nxt_rbtree_parent_relink(nxt_rbtree_node_t *subst, nxt_rbtree_node_t *node) * If the node's parent is the root sentinel it is safely changed * because the root sentinel's left child is the tree root. */ - link = (node == parent->left) ? &parent->left : &parent->right; - *link = subst; + link = (node == parent->left) ? &parent->left : &parent->right; + *link = subst; } - nxt_rbtree_node_t * -nxt_rbtree_destroy_next(nxt_rbtree_t *tree, nxt_rbtree_node_t **next) -{ - nxt_rbtree_node_t *node, *subst, *parent, *sentinel; +nxt_rbtree_destroy_next(nxt_rbtree_t *tree, nxt_rbtree_node_t **next) { + nxt_rbtree_node_t *node, *subst, *parent, *sentinel; sentinel = nxt_rbtree_sentinel(tree); /* Find the leftmost node. */ - for (node = *next; node->left != sentinel; node = node->left); + for (node = *next; node->left != sentinel; node = node->left) + ; /* Replace the leftmost node with its right child. */ - subst = node->right; + subst = node->right; parent = node->parent; - parent->left = subst; + parent->left = subst; subst->parent = parent; /* diff --git a/src/nxt_rbtree.h b/src/nxt_rbtree.h index 4bf479ea2..2789390f2 100644 --- a/src/nxt_rbtree.h +++ b/src/nxt_rbtree.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,65 +7,56 @@ #define _NXT_RBTREE_H_INCLUDED_ -typedef struct nxt_rbtree_node_s nxt_rbtree_node_t; +typedef struct nxt_rbtree_node_s nxt_rbtree_node_t; struct nxt_rbtree_node_s { - nxt_rbtree_node_t *left; - nxt_rbtree_node_t *right; - nxt_rbtree_node_t *parent; + nxt_rbtree_node_t *left; + nxt_rbtree_node_t *right; + nxt_rbtree_node_t *parent; - uint8_t color; + uint8_t color; }; - typedef struct { - nxt_rbtree_node_t *left; - nxt_rbtree_node_t *right; - nxt_rbtree_node_t *parent; + nxt_rbtree_node_t *left; + nxt_rbtree_node_t *right; + nxt_rbtree_node_t *parent; } nxt_rbtree_part_t; - -#define NXT_RBTREE_NODE(node) \ - nxt_rbtree_part_t node; \ - uint8_t node##_color +#define NXT_RBTREE_NODE(node) \ + nxt_rbtree_part_t node; \ + uint8_t node##_color -#define NXT_RBTREE_NODE_INIT { NULL, NULL, NULL }, 0 - +#define NXT_RBTREE_NODE_INIT {NULL, NULL, NULL}, 0 typedef struct { - nxt_rbtree_node_t sentinel; + nxt_rbtree_node_t sentinel; } nxt_rbtree_t; - /* * A comparison function should return intptr_t result because * this eliminates overhead required to implement correct addresses * comparison without result truncation. */ -typedef intptr_t (*nxt_rbtree_compare_t)(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2); +typedef intptr_t ( + *nxt_rbtree_compare_t)(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2); -#define nxt_rbtree_root(tree) \ - ((tree)->sentinel.left) +#define nxt_rbtree_root(tree) ((tree)->sentinel.left) -#define nxt_rbtree_sentinel(tree) \ - (&(tree)->sentinel) +#define nxt_rbtree_sentinel(tree) (&(tree)->sentinel) -#define nxt_rbtree_is_empty(tree) \ +#define nxt_rbtree_is_empty(tree) \ (nxt_rbtree_root(tree) == nxt_rbtree_sentinel(tree)) -#define nxt_rbtree_min(tree) \ - nxt_rbtree_branch_min(tree, &(tree)->sentinel) - +#define nxt_rbtree_min(tree) nxt_rbtree_branch_min(tree, &(tree)->sentinel) nxt_inline nxt_rbtree_node_t * -nxt_rbtree_branch_min(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) -{ +nxt_rbtree_branch_min(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) { while (node->left != nxt_rbtree_sentinel(tree)) { node = node->left; } @@ -74,21 +64,18 @@ nxt_rbtree_branch_min(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) return node; } - -#define nxt_rbtree_is_there_successor(tree, node) \ +#define nxt_rbtree_is_there_successor(tree, node) \ ((node) != nxt_rbtree_sentinel(tree)) - nxt_inline nxt_rbtree_node_t * -nxt_rbtree_node_successor(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) -{ - nxt_rbtree_node_t *parent; +nxt_rbtree_node_successor(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) { + nxt_rbtree_node_t *parent; if (node->right != nxt_rbtree_sentinel(tree)) { return nxt_rbtree_branch_min(tree, node->right); } - for ( ;; ) { + for (;;) { parent = node->parent; /* @@ -103,18 +90,18 @@ nxt_rbtree_node_successor(nxt_rbtree_t *tree, nxt_rbtree_node_t *node) } } - -NXT_EXPORT void nxt_rbtree_init(nxt_rbtree_t *tree, - nxt_rbtree_compare_t compare); -NXT_EXPORT void nxt_rbtree_insert(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); -NXT_EXPORT nxt_rbtree_node_t *nxt_rbtree_find(nxt_rbtree_t *tree, - nxt_rbtree_part_t *node); -NXT_EXPORT nxt_rbtree_node_t *nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, - nxt_rbtree_part_t *node); -NXT_EXPORT nxt_rbtree_node_t - *nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, - nxt_rbtree_part_t *node); -NXT_EXPORT void nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); +NXT_EXPORT void +nxt_rbtree_init(nxt_rbtree_t *tree, nxt_rbtree_compare_t compare); +NXT_EXPORT void +nxt_rbtree_insert(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); +NXT_EXPORT nxt_rbtree_node_t * +nxt_rbtree_find(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); +NXT_EXPORT nxt_rbtree_node_t * +nxt_rbtree_find_less_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); +NXT_EXPORT nxt_rbtree_node_t * +nxt_rbtree_find_greater_or_equal(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); +NXT_EXPORT void +nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); /* * nxt_rbtree_destroy_next() is iterator to use only while rbtree destruction. @@ -124,8 +111,8 @@ NXT_EXPORT void nxt_rbtree_delete(nxt_rbtree_t *tree, nxt_rbtree_part_t *node); * the "next" parameter will be equal to the rbtree sentinel. No other * operations must be performed on the rbtree while destruction. */ -NXT_EXPORT nxt_rbtree_node_t *nxt_rbtree_destroy_next(nxt_rbtree_t *tree, - nxt_rbtree_node_t **next); +NXT_EXPORT nxt_rbtree_node_t * +nxt_rbtree_destroy_next(nxt_rbtree_t *tree, nxt_rbtree_node_t **next); #endif /* _NXT_RBTREE_H_INCLUDED_ */ diff --git a/src/nxt_recvbuf.c b/src/nxt_recvbuf.c index 29a2b65d9..5ba3c385c 100644 --- a/src/nxt_recvbuf.c +++ b/src/nxt_recvbuf.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,27 +5,23 @@ #include - nxt_uint_t -nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb) -{ - u_char *last; +nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb) { + u_char *last; size_t size, total; nxt_int_t n; - nxt_buf_t *b; + nxt_buf_t *b; total = 0; - last = NULL; - n = -1; + last = NULL; + n = -1; for (b = rb->buf; b != NULL; b = b->next) { - nxt_prefetch(b->next); size = b->mem.end - b->mem.free; if (b->mem.free != last) { - if (++n >= rb->nmax) { goto done; } @@ -38,11 +33,10 @@ nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb) } nxt_thread_log_debug("recvbuf: %ui, %p, %uz", n, - nxt_iobuf_data(&rb->iobuf[n]), - nxt_iobuf_size(&rb->iobuf[n])); + nxt_iobuf_data(&rb->iobuf[n]), nxt_iobuf_size(&rb->iobuf[n])); total += size; - last = b->mem.end; + last = b->mem.end; } n++; @@ -54,18 +48,14 @@ nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb) return n; } - void -nxt_recvbuf_update(nxt_buf_t *b, size_t sent) -{ - size_t size; +nxt_recvbuf_update(nxt_buf_t *b, size_t sent) { + size_t size; while (b != NULL && sent != 0) { - nxt_prefetch(b->next); if (!nxt_buf_is_sync(b)) { - size = b->mem.end - b->mem.free; if (sent < size) { @@ -73,8 +63,8 @@ nxt_recvbuf_update(nxt_buf_t *b, size_t sent) return; } - b->mem.free = b->mem.end; - sent -= size; + b->mem.free = b->mem.end; + sent -= size; } b = b->next; diff --git a/src/nxt_recvbuf.h b/src/nxt_recvbuf.h index 69b514987..eeab0642e 100644 --- a/src/nxt_recvbuf.h +++ b/src/nxt_recvbuf.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,18 +6,18 @@ #ifndef _NXT_RECVBUF_H_INCLUDED_ #define _NXT_RECVBUF_H_INCLUDED_ - typedef struct { - nxt_buf_t *buf; - nxt_iobuf_t *iobuf; + nxt_buf_t *buf; + nxt_iobuf_t *iobuf; - int32_t nmax; - size_t size; + int32_t nmax; + size_t size; } nxt_recvbuf_coalesce_t; - -nxt_uint_t nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb); -void nxt_recvbuf_update(nxt_buf_t *b, size_t sent); +nxt_uint_t +nxt_recvbuf_mem_coalesce(nxt_recvbuf_coalesce_t *rb); +void +nxt_recvbuf_update(nxt_buf_t *b, size_t sent); #endif /* _NXT_RECVBUF_H_INCLUDED_ */ diff --git a/src/nxt_regex.h b/src/nxt_regex.h index a24c50f80..5a8b70baa 100644 --- a/src/nxt_regex.h +++ b/src/nxt_regex.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Axel Duch * Copyright (C) NGINX, Inc. @@ -9,32 +8,34 @@ #if (NXT_HAVE_REGEX) -typedef struct nxt_regex_s nxt_regex_t; +typedef struct nxt_regex_s nxt_regex_t; - #if (NXT_HAVE_PCRE2) -typedef void nxt_regex_match_t; +#if (NXT_HAVE_PCRE2) +typedef void nxt_regex_match_t; #else -typedef struct nxt_regex_match_s nxt_regex_match_t; +typedef struct nxt_regex_match_s nxt_regex_match_t; #endif typedef struct { - size_t offset; + size_t offset; #if (NXT_HAVE_PCRE2) -#define ERR_BUF_SIZE 256 - u_char msg[ERR_BUF_SIZE]; +#define ERR_BUF_SIZE 256 + u_char msg[ERR_BUF_SIZE]; #else - const char *msg; + const char *msg; #endif } nxt_regex_err_t; - -NXT_EXPORT void nxt_regex_init(void); -NXT_EXPORT nxt_regex_t *nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, - nxt_regex_err_t *err); -NXT_EXPORT nxt_regex_match_t *nxt_regex_match_create(nxt_mp_t *mp, size_t size); -NXT_EXPORT nxt_int_t nxt_regex_match(nxt_regex_t *re, u_char *subject, - size_t length, nxt_regex_match_t *match); +NXT_EXPORT void +nxt_regex_init(void); +NXT_EXPORT nxt_regex_t * +nxt_regex_compile(nxt_mp_t *mp, nxt_str_t *source, nxt_regex_err_t *err); +NXT_EXPORT nxt_regex_match_t * +nxt_regex_match_create(nxt_mp_t *mp, size_t size); +NXT_EXPORT nxt_int_t +nxt_regex_match(nxt_regex_t *re, u_char *subject, size_t length, + nxt_regex_match_t *match); #endif /* NXT_HAVE_REGEX */ diff --git a/src/nxt_router.c b/src/nxt_router.c index 076cd134b..c11127f38 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -22,7 +21,7 @@ #include #include -#define NXT_SHARED_PORT_ID 0xFFFFu +#define NXT_SHARED_PORT_ID 0xFFFFu typedef struct { nxt_str_t type; @@ -31,16 +30,15 @@ typedef struct { uint32_t spare_processes; nxt_msec_t timeout; nxt_msec_t idle_timeout; - nxt_conf_value_t *limits_value; - nxt_conf_value_t *processes_value; - nxt_conf_value_t *targets_value; + nxt_conf_value_t *limits_value; + nxt_conf_value_t *processes_value; + nxt_conf_value_t *targets_value; } nxt_router_app_conf_t; - typedef struct { - nxt_str_t pass; - nxt_str_t application; - int backlog; + nxt_str_t pass; + nxt_str_t application; + int backlog; } nxt_router_listener_conf_t; @@ -48,12 +46,12 @@ typedef struct { typedef struct { nxt_str_t name; - nxt_socket_conf_t *socket_conf; - nxt_router_temp_conf_t *temp_conf; - nxt_tls_init_t *tls_init; + nxt_socket_conf_t *socket_conf; + nxt_router_temp_conf_t *temp_conf; + nxt_tls_init_t *tls_init; nxt_bool_t last; - nxt_queue_link_t link; /* for nxt_socket_conf_t.tls */ + nxt_queue_link_t link; /* for nxt_socket_conf_t.tls */ } nxt_router_tlssock_t; #endif @@ -63,7 +61,7 @@ typedef struct { typedef struct { nxt_str_t name; - nxt_router_temp_conf_t *temp_conf; + nxt_router_temp_conf_t *temp_conf; nxt_queue_link_t link; } nxt_router_js_module_t; @@ -71,275 +69,312 @@ typedef struct { typedef struct { - nxt_str_t *name; - nxt_socket_conf_t *socket_conf; - nxt_router_temp_conf_t *temp_conf; + nxt_str_t *name; + nxt_socket_conf_t *socket_conf; + nxt_router_temp_conf_t *temp_conf; nxt_bool_t last; } nxt_socket_rpc_t; - typedef struct { - nxt_app_t *app; - nxt_router_temp_conf_t *temp_conf; - uint8_t proto; /* 1 bit */ + nxt_app_t *app; + nxt_router_temp_conf_t *temp_conf; + uint8_t proto; /* 1 bit */ } nxt_app_rpc_t; - typedef struct { - nxt_app_joint_t *app_joint; - uint32_t generation; - uint8_t proto; /* 1 bit */ + nxt_app_joint_t *app_joint; + uint32_t generation; + uint8_t proto; /* 1 bit */ } nxt_app_joint_rpc_t; +static nxt_int_t +nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp); +static nxt_int_t +nxt_router_start(nxt_task_t *task, nxt_process_data_t *data); +static void +nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port); -static nxt_int_t nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, - nxt_mp_t *mp); -static nxt_int_t nxt_router_start(nxt_task_t *task, nxt_process_data_t *data); -static void nxt_router_greet_controller(nxt_task_t *task, - nxt_port_t *controller_port); +static nxt_int_t +nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app); -static nxt_int_t nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app); +static void +nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -static void nxt_router_new_port_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_router_conf_data_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_router_app_restart_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_router_status_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_router_remove_pid_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); +static nxt_router_temp_conf_t * +nxt_router_temp_conf(nxt_task_t *task); +static void +nxt_router_conf_ready(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); +static void +nxt_router_conf_send(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_port_msg_type_t type); -static nxt_router_temp_conf_t *nxt_router_temp_conf(nxt_task_t *task); -static void nxt_router_conf_ready(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf); -static void nxt_router_conf_send(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_port_msg_type_t type); - -static nxt_int_t nxt_router_conf_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end); -static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task, - nxt_router_conf_t *rtcf, nxt_conf_value_t *conf); -static nxt_http_forward_t *nxt_router_conf_forward(nxt_task_t *task, - nxt_mp_t *mp, nxt_conf_value_t *conf); -static nxt_int_t nxt_router_conf_forward_header(nxt_mp_t *mp, - nxt_conf_value_t *conf, nxt_http_forward_header_t *fh); - -static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name); -static nxt_int_t nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data); -static nxt_int_t nxt_router_apps_hash_add(nxt_router_conf_t *rtcf, - nxt_app_t *app); -static nxt_app_t *nxt_router_apps_hash_get(nxt_router_conf_t *rtcf, - nxt_str_t *name); -static void nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf, - int i); - -static nxt_int_t nxt_router_app_queue_init(nxt_task_t *task, - nxt_port_t *port); -static nxt_int_t nxt_router_port_queue_init(nxt_task_t *task, - nxt_port_t *port); -static nxt_int_t nxt_router_port_queue_map(nxt_task_t *task, - nxt_port_t *port, nxt_fd_t fd); -static void nxt_router_listen_socket_rpc_create(nxt_task_t *task, +static nxt_int_t +nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + u_char *start, u_char *end); +static nxt_int_t +nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_conf_value_t *conf); +static nxt_http_forward_t * +nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf); +static nxt_int_t +nxt_router_conf_forward_header(nxt_mp_t *mp, nxt_conf_value_t *conf, + nxt_http_forward_header_t *fh); + +static nxt_app_t * +nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name); +static nxt_int_t +nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_int_t +nxt_router_apps_hash_add(nxt_router_conf_t *rtcf, nxt_app_t *app); +static nxt_app_t * +nxt_router_apps_hash_get(nxt_router_conf_t *rtcf, nxt_str_t *name); +static void +nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf, int i); + +static nxt_int_t +nxt_router_app_queue_init(nxt_task_t *task, nxt_port_t *port); +static nxt_int_t +nxt_router_port_queue_init(nxt_task_t *task, nxt_port_t *port); +static nxt_int_t +nxt_router_port_queue_map(nxt_task_t *task, nxt_port_t *port, nxt_fd_t fd); +static void +nxt_router_listen_socket_rpc_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf); -static void nxt_router_listen_socket_ready(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_listen_socket_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); +static void +nxt_router_listen_socket_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_router_listen_socket_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); #if (NXT_TLS) -static void nxt_router_tls_rpc_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_int_t nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf, +static void +nxt_router_tls_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static nxt_int_t +nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *value, nxt_socket_conf_t *skcf, nxt_tls_init_t *tls_init, nxt_bool_t last); #endif #if (NXT_HAVE_NJS) -static void nxt_router_js_module_rpc_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_int_t nxt_router_js_module_insert(nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *value); +static void +nxt_router_js_module_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static nxt_int_t +nxt_router_js_module_insert(nxt_router_temp_conf_t *tmcf, + nxt_conf_value_t *value); #endif -static void nxt_router_app_rpc_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_app_t *app); -static void nxt_router_app_prefork_ready(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_app_prefork_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_str_t *name, int backlog); -static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf, +static void +nxt_router_app_rpc_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_app_t *app); +static void +nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_router_app_prefork_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static nxt_socket_conf_t * +nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_str_t *name, int backlog); +static nxt_int_t +nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa); -static nxt_int_t nxt_router_engines_create(nxt_task_t *task, - nxt_router_t *router, nxt_router_temp_conf_t *tmcf, - const nxt_event_interface_t *interface); -static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf); -static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf); -static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf); -static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf, +static nxt_int_t +nxt_router_engines_create(nxt_task_t *task, nxt_router_t *router, + nxt_router_temp_conf_t *tmcf, const nxt_event_interface_t *interface); +static nxt_int_t +nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf, + nxt_router_engine_conf_t *recf); +static nxt_int_t +nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf, + nxt_router_engine_conf_t *recf); +static nxt_int_t +nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf, + nxt_router_engine_conf_t *recf); +static nxt_int_t +nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf, nxt_router_engine_conf_t *recf, nxt_queue_t *sockets, nxt_work_handler_t handler); -static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf); -static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf, +static nxt_int_t +nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf, + nxt_router_engine_conf_t *recf); +static nxt_int_t +nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf, nxt_router_engine_conf_t *recf, nxt_queue_t *sockets); -static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt, +static nxt_int_t +nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt, nxt_router_temp_conf_t *tmcf); -static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt, +static nxt_int_t +nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt, nxt_event_engine_t *engine); -static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router, +static void +nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router, nxt_router_temp_conf_t *tmcf); -static void nxt_router_engines_post(nxt_router_t *router, - nxt_router_temp_conf_t *tmcf); -static void nxt_router_engine_post(nxt_event_engine_t *engine, - nxt_work_t *jobs); +static void +nxt_router_engines_post(nxt_router_t *router, nxt_router_temp_conf_t *tmcf); +static void +nxt_router_engine_post(nxt_event_engine_t *engine, nxt_work_t *jobs); -static void nxt_router_thread_start(void *data); -static void nxt_router_rt_add_port(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_req_headers_ack_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data); -static void nxt_router_listen_socket_release(nxt_task_t *task, - nxt_socket_conf_t *skcf); - -static void nxt_router_app_port_ready(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_app_port_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); - -static void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i); -static void nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app); - -static void nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, - nxt_port_t *port, nxt_apr_action_t action); -static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app, +static void +nxt_router_thread_start(void *data); +static void +nxt_router_rt_add_port(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_listen_socket_create(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_listen_socket_update(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_listen_socket_delete(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_worker_thread_quit(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_listen_socket_close(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_req_headers_ack_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data); -static void nxt_router_http_request_error(nxt_task_t *task, void *obj, +static void +nxt_router_listen_socket_release(nxt_task_t *task, nxt_socket_conf_t *skcf); + +static void +nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_http_request_done(nxt_task_t *task, void *obj, +static void +nxt_router_app_port_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_app_prepare_request(nxt_task_t *task, +static void +nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i); +static void +nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app); + +static void +nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, + nxt_apr_action_t action); +static void +nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app, nxt_request_rpc_data_t *req_rpc_data); -static nxt_buf_t *nxt_router_prepare_msg(nxt_task_t *task, - nxt_http_request_t *r, nxt_app_t *app, const nxt_str_t *prefix); +static void +nxt_router_http_request_error(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_http_request_done(nxt_task_t *task, void *obj, void *data); -static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data); -static void nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_app_idle_timeout(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_free_app(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_app_prepare_request(nxt_task_t *task, + nxt_request_rpc_data_t *req_rpc_data); +static nxt_buf_t * +nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, nxt_app_t *app, + const nxt_str_t *prefix); -static const nxt_http_request_state_t nxt_http_request_send_state; -static void nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_app_idle_timeout(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_free_app(nxt_task_t *task, void *obj, void *data); + +static const nxt_http_request_state_t nxt_http_request_send_state; +static void +nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data); -static void nxt_router_app_joint_use(nxt_task_t *task, - nxt_app_joint_t *app_joint, int i); +static void +nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i); -static void nxt_router_http_request_release_post(nxt_task_t *task, - nxt_http_request_t *r); -static void nxt_router_http_request_release(nxt_task_t *task, void *obj, - void *data); -static void nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -static void nxt_router_get_port_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); -static void nxt_router_get_mmap_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); +static void +nxt_router_http_request_release_post(nxt_task_t *task, nxt_http_request_t *r); +static void +nxt_router_http_request_release(nxt_task_t *task, void *obj, void *data); +static void +nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_get_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +static void +nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -extern const nxt_http_request_state_t nxt_http_websocket; +extern const nxt_http_request_state_t nxt_http_websocket; -nxt_router_t *nxt_router; +nxt_router_t *nxt_router; -static const nxt_str_t http_prefix = nxt_string("HTTP_"); +static const nxt_str_t http_prefix = nxt_string("HTTP_"); static const nxt_str_t empty_prefix = nxt_string(""); -static const nxt_str_t *nxt_app_msg_prefix[] = { - [NXT_APP_EXTERNAL] = &empty_prefix, - [NXT_APP_PYTHON] = &empty_prefix, - [NXT_APP_PHP] = &http_prefix, - [NXT_APP_PERL] = &http_prefix, - [NXT_APP_RUBY] = &http_prefix, - [NXT_APP_JAVA] = &empty_prefix, - [NXT_APP_WASM] = &empty_prefix, - [NXT_APP_WASM_WC] = &empty_prefix, +static const nxt_str_t *nxt_app_msg_prefix[] = { + [NXT_APP_EXTERNAL] = &empty_prefix, + [NXT_APP_PYTHON] = &empty_prefix, + [NXT_APP_PHP] = &http_prefix, + [NXT_APP_PERL] = &http_prefix, + [NXT_APP_RUBY] = &http_prefix, + [NXT_APP_JAVA] = &empty_prefix, + [NXT_APP_WASM] = &empty_prefix, + [NXT_APP_WASM_WC] = &empty_prefix, }; -static const nxt_port_handlers_t nxt_router_process_port_handlers = { - .quit = nxt_signal_quit_handler, - .new_port = nxt_router_new_port_handler, - .get_port = nxt_router_get_port_handler, - .change_file = nxt_port_change_log_file_handler, - .mmap = nxt_port_mmap_handler, - .get_mmap = nxt_router_get_mmap_handler, - .data = nxt_router_conf_data_handler, - .app_restart = nxt_router_app_restart_handler, - .status = nxt_router_status_handler, - .remove_pid = nxt_router_remove_pid_handler, - .access_log = nxt_router_access_log_reopen_handler, - .rpc_ready = nxt_port_rpc_handler, - .rpc_error = nxt_port_rpc_handler, - .oosm = nxt_router_oosm_handler, +static const nxt_port_handlers_t nxt_router_process_port_handlers = { + .quit = nxt_signal_quit_handler, + .new_port = nxt_router_new_port_handler, + .get_port = nxt_router_get_port_handler, + .change_file = nxt_port_change_log_file_handler, + .mmap = nxt_port_mmap_handler, + .get_mmap = nxt_router_get_mmap_handler, + .data = nxt_router_conf_data_handler, + .app_restart = nxt_router_app_restart_handler, + .status = nxt_router_status_handler, + .remove_pid = nxt_router_remove_pid_handler, + .access_log = nxt_router_access_log_reopen_handler, + .rpc_ready = nxt_port_rpc_handler, + .rpc_error = nxt_port_rpc_handler, + .oosm = nxt_router_oosm_handler, }; -const nxt_process_init_t nxt_router_process = { - .name = "router", - .type = NXT_PROCESS_ROUTER, - .prefork = nxt_router_prefork, - .restart = 1, - .setup = nxt_process_core_setup, - .start = nxt_router_start, - .port_handlers = &nxt_router_process_port_handlers, - .signals = nxt_process_signals, +const nxt_process_init_t nxt_router_process = { + .name = "router", + .type = NXT_PROCESS_ROUTER, + .prefork = nxt_router_prefork, + .restart = 1, + .setup = nxt_process_core_setup, + .start = nxt_router_start, + .port_handlers = &nxt_router_process_port_handlers, + .signals = nxt_process_signals, }; /* Queues of nxt_socket_conf_t */ -nxt_queue_t creating_sockets; -nxt_queue_t pending_sockets; -nxt_queue_t updating_sockets; -nxt_queue_t keeping_sockets; -nxt_queue_t deleting_sockets; - +nxt_queue_t creating_sockets; +nxt_queue_t pending_sockets; +nxt_queue_t updating_sockets; +nxt_queue_t keeping_sockets; +nxt_queue_t deleting_sockets; static nxt_int_t -nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) -{ +nxt_router_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp) { nxt_runtime_stop_app_processes(task, task->thread->runtime); return NXT_OK; } - static nxt_int_t -nxt_router_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_router_start(nxt_task_t *task, nxt_process_data_t *data) { nxt_int_t ret; - nxt_port_t *controller_port; - nxt_router_t *router; - nxt_runtime_t *rt; + nxt_port_t *controller_port; + nxt_router_t *router; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -381,28 +416,24 @@ nxt_router_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_OK; } - static void -nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port) -{ - nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY, - -1, 0, 0, NULL); +nxt_router_greet_controller(nxt_task_t *task, nxt_port_t *controller_port) { + nxt_port_socket_write(task, controller_port, NXT_PORT_MSG_PROCESS_READY, -1, + 0, 0, NULL); } - static void nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, - void *data) -{ + void *data) { size_t size; uint32_t stream; nxt_fd_t port_fd, queue_fd; nxt_int_t ret; - nxt_app_t *app; - nxt_buf_t *b; - nxt_port_t *dport; - nxt_runtime_t *rt; - nxt_app_joint_rpc_t *app_joint_rpc; + nxt_app_t *app; + nxt_buf_t *b; + nxt_port_t *dport; + nxt_runtime_t *rt; + nxt_app_joint_rpc_t *app_joint_rpc; app = data; @@ -415,14 +446,14 @@ nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, if (dport != NULL) { nxt_debug(task, "app '%V' %p start process", &app->name, app); - b = NULL; - port_fd = -1; + b = NULL; + port_fd = -1; queue_fd = -1; } else { if (app->proto_port_requests > 0) { nxt_debug(task, "app '%V' %p wait for prototype process", - &app->name, app); + &app->name, app); app->proto_port_requests++; @@ -431,7 +462,7 @@ nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, nxt_debug(task, "app '%V' %p start prototype process", &app->name, app); - rt = task->thread->runtime; + rt = task->thread->runtime; dport = rt->port_by_type[NXT_PROCESS_MAIN]; size = app->name.length + 1 + app->conf.length; @@ -445,14 +476,13 @@ nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, *b->mem.free++ = '\0'; nxt_buf_cpystr(b, &app->conf); - port_fd = app->shared_port->pair[0]; + port_fd = app->shared_port->pair[0]; queue_fd = app->shared_port->queue_fd; } app_joint_rpc = nxt_port_rpc_register_handler_ex(task, port, - nxt_router_app_port_ready, - nxt_router_app_port_error, - sizeof(nxt_app_joint_rpc_t)); + nxt_router_app_port_ready, nxt_router_app_port_error, + sizeof(nxt_app_joint_rpc_t)); if (nxt_slow_path(app_joint_rpc == NULL)) { goto failed; } @@ -460,16 +490,16 @@ nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, stream = nxt_port_rpc_ex_stream(app_joint_rpc); ret = nxt_port_socket_write2(task, dport, NXT_PORT_MSG_START_PROCESS, - port_fd, queue_fd, stream, port->id, b); + port_fd, queue_fd, stream, port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, port, stream); goto failed; } - app_joint_rpc->app_joint = app->joint; + app_joint_rpc->app_joint = app->joint; app_joint_rpc->generation = app->generation; - app_joint_rpc->proto = (b != NULL); + app_joint_rpc->proto = (b != NULL); if (b != NULL) { app->proto_port_requests++; @@ -490,10 +520,8 @@ nxt_router_start_app_process_handler(nxt_task_t *task, nxt_port_t *port, nxt_router_app_use(task, app, -1); } - static void -nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i) -{ +nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i) { app_joint->use_count += i; if (app_joint->use_count == 0) { @@ -503,23 +531,21 @@ nxt_router_app_joint_use(nxt_task_t *task, nxt_app_joint_t *app_joint, int i) } } - static nxt_int_t -nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app) -{ +nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app) { nxt_int_t res; - nxt_port_t *router_port; - nxt_runtime_t *rt; + nxt_port_t *router_port; + nxt_runtime_t *rt; nxt_debug(task, "app '%V' start process", &app->name); - rt = task->thread->runtime; + rt = task->thread->runtime; router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; nxt_router_app_use(task, app, 1); res = nxt_port_post(task, router_port, nxt_router_start_app_process_handler, - app); + app); if (res == NXT_OK) { return res; @@ -536,14 +562,12 @@ nxt_router_start_app_process(nxt_task_t *task, nxt_app_t *app) return NXT_ERROR; } - nxt_inline nxt_bool_t -nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data) -{ - nxt_buf_t *b, *next; +nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data) { + nxt_buf_t *b, *next; nxt_bool_t cancelled; - nxt_port_t *app_port; - nxt_msg_info_t *msg_info; + nxt_port_t *app_port; + nxt_msg_info_t *msg_info; msg_info = &req_rpc_data->msg_info; @@ -555,12 +579,11 @@ nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data) if (app_port != NULL && app_port->id == NXT_SHARED_PORT_ID) { cancelled = nxt_app_queue_cancel(app_port->queue, - msg_info->tracking_cookie, - req_rpc_data->stream); + msg_info->tracking_cookie, req_rpc_data->stream); if (cancelled) { nxt_debug(task, "stream #%uD: cancelled by router", - req_rpc_data->stream); + req_rpc_data->stream); } } else { @@ -568,7 +591,7 @@ nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data) } for (b = msg_info->buf; b != NULL; b = next) { - next = b->next; + next = b->next; b->next = NULL; if (b->is_port_mmap_sent) { @@ -583,10 +606,8 @@ nxt_router_msg_cancel(nxt_task_t *task, nxt_request_rpc_data_t *req_rpc_data) return cancelled; } - nxt_inline nxt_bool_t -nxt_queue_chk_remove(nxt_queue_link_t *lnk) -{ +nxt_queue_chk_remove(nxt_queue_link_t *lnk) { if (lnk->next != NULL) { nxt_queue_remove(lnk); @@ -598,14 +619,12 @@ nxt_queue_chk_remove(nxt_queue_link_t *lnk) return 0; } - nxt_inline void nxt_request_rpc_data_unlink(nxt_task_t *task, - nxt_request_rpc_data_t *req_rpc_data) -{ - nxt_app_t *app; + nxt_request_rpc_data_t *req_rpc_data) { + nxt_app_t *app; nxt_bool_t unlinked; - nxt_http_request_t *r; + nxt_http_request_t *r; nxt_router_msg_cancel(task, req_rpc_data); @@ -613,7 +632,7 @@ nxt_request_rpc_data_unlink(nxt_task_t *task, if (req_rpc_data->app_port != NULL) { nxt_router_app_port_release(task, app, req_rpc_data->app_port, - req_rpc_data->apr_action); + req_rpc_data->apr_action); req_rpc_data->app_port = NULL; } @@ -625,7 +644,7 @@ nxt_request_rpc_data_unlink(nxt_task_t *task, nxt_router_http_request_release_post(task, r); - r->req_rpc_data = NULL; + r->req_rpc_data = NULL; req_rpc_data->request = NULL; if (app != NULL) { @@ -664,18 +683,16 @@ nxt_request_rpc_data_unlink(nxt_task_t *task, req_rpc_data->rpc_cancel = 0; nxt_port_rpc_cancel(task, task->thread->engine->port, - req_rpc_data->stream); + req_rpc_data->stream); } } - static void -nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_int_t res; - nxt_app_t *app; - nxt_port_t *port, *main_app_port; - nxt_runtime_t *rt; + nxt_app_t *app; + nxt_port_t *port, *main_app_port; + nxt_runtime_t *rt; nxt_port_new_port_handler(task, msg); @@ -685,14 +702,13 @@ nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_router_greet_controller(task, msg->u.new_port); } - if (port != NULL && port->type == NXT_PROCESS_PROTOTYPE) { + if (port != NULL && port->type == NXT_PROCESS_PROTOTYPE) { nxt_port_rpc_handler(task, msg); return; } if (port == NULL || port->type != NXT_PROCESS_APP) { - if (msg->port_msg.stream == 0) { return; } @@ -754,19 +770,16 @@ nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_socket_write(task, port, NXT_PORT_MSG_PORT_ACK, -1, 0, 0, NULL); } - static void -nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - void *p; +nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + void *p; size_t size; nxt_int_t ret; - nxt_port_t *port; - nxt_router_temp_conf_t *tmcf; + nxt_port_t *port; + nxt_router_temp_conf_t *tmcf; - port = nxt_runtime_port_find(task->thread->runtime, - msg->port_msg.pid, - msg->port_msg.reply_port); + port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "conf_data_handler: reply port not found"); return; @@ -792,7 +805,7 @@ nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_buf_mem_used_size(&msg->buf->mem) != sizeof(size_t)) { nxt_alert(task, "conf_data_handler: unexpected buffer size (%d)", - (int) nxt_buf_mem_used_size(&msg->buf->mem)); + (int) nxt_buf_mem_used_size(&msg->buf->mem)); goto fail; } @@ -810,8 +823,8 @@ nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_debug(task, "conf_data_handler(%uz): %*s", size, size, p); tmcf->router_conf->router = nxt_router; - tmcf->stream = msg->port_msg.stream; - tmcf->port = port; + tmcf->stream = msg->port_msg.stream; + tmcf->port = port; nxt_port_use(task, tmcf->port, 1); @@ -829,7 +842,7 @@ nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) fail: nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, - msg->port_msg.stream, 0, NULL); + msg->port_msg.stream, 0, NULL); if (tmcf != NULL) { nxt_mp_release(tmcf->mem_pool); @@ -847,35 +860,32 @@ nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static void -nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_app_t *app; - nxt_int_t ret; - nxt_str_t app_name; - nxt_port_t *reply_port, *shared_port, *old_shared_port; - nxt_port_t *proto_port; - nxt_port_msg_type_t reply; - - reply_port = nxt_runtime_port_find(task->thread->runtime, - msg->port_msg.pid, - msg->port_msg.reply_port); +nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_app_t *app; + nxt_int_t ret; + nxt_str_t app_name; + nxt_port_t *reply_port, *shared_port, *old_shared_port; + nxt_port_t *proto_port; + nxt_port_msg_type_t reply; + + reply_port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, + msg->port_msg.reply_port); if (nxt_slow_path(reply_port == NULL)) { nxt_alert(task, "app_restart_handler: reply port not found"); return; } app_name.length = nxt_buf_mem_used_size(&msg->buf->mem); - app_name.start = msg->buf->mem.pos; + app_name.start = msg->buf->mem.pos; nxt_debug(task, "app_restart_handler: %V", &app_name); app = nxt_router_app_find(&nxt_router->apps, &app_name); if (nxt_fast_path(app != NULL)) { - shared_port = nxt_port_new(task, NXT_SHARED_PORT_ID, nxt_pid, - NXT_PROCESS_APP); + shared_port + = nxt_port_new(task, NXT_SHARED_PORT_ID, nxt_pid, NXT_PROCESS_APP); if (nxt_slow_path(shared_port == NULL)) { goto fail; } @@ -902,7 +912,7 @@ nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (proto_port != NULL) { nxt_debug(task, "send QUIT to prototype '%V' pid %PI", &app->name, - proto_port->pid); + proto_port->pid); app->proto_port = NULL; proto_port->app = NULL; @@ -912,7 +922,7 @@ nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) shared_port->app = app; - old_shared_port = app->shared_port; + old_shared_port = app->shared_port; old_shared_port->app = NULL; app->shared_port = shared_port; @@ -924,7 +934,7 @@ nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (proto_port != NULL) { (void) nxt_port_socket_write(task, proto_port, NXT_PORT_MSG_QUIT, - -1, 0, 0, NULL); + -1, 0, 0, NULL); nxt_port_close(task, proto_port); @@ -934,33 +944,29 @@ nxt_router_app_restart_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) reply = NXT_PORT_MSG_RPC_READY_LAST; } else { - -fail: + fail: reply = NXT_PORT_MSG_RPC_ERROR; } - nxt_port_socket_write(task, reply_port, reply, -1, msg->port_msg.stream, - 0, NULL); + nxt_port_socket_write(task, reply_port, reply, -1, msg->port_msg.stream, 0, + NULL); } - static void -nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; +nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; size_t alloc; - nxt_app_t *app; - nxt_buf_t *b; + nxt_app_t *app; + nxt_buf_t *b; nxt_uint_t type; - nxt_port_t *port; - nxt_status_app_t *app_stat; - nxt_event_engine_t *engine; - nxt_status_report_t *report; - - port = nxt_runtime_port_find(task->thread->runtime, - msg->port_msg.pid, - msg->port_msg.reply_port); + nxt_port_t *port; + nxt_status_app_t *app_stat; + nxt_event_engine_t *engine; + nxt_status_report_t *report; + + port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "nxt_router_status_handler(): reply port not found"); return; @@ -969,10 +975,9 @@ nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) alloc = sizeof(nxt_status_report_t); nxt_queue_each(app, &nxt_router->apps, nxt_app_t, link) { - alloc += sizeof(nxt_status_app_t) + app->name.length; - - } nxt_queue_loop; + } + nxt_queue_loop; b = nxt_buf_mem_alloc(port->mem_pool, alloc, 0); if (nxt_slow_path(b == NULL)) { @@ -980,23 +985,22 @@ nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto fail; } - report = (nxt_status_report_t *) b->mem.free; + report = (nxt_status_report_t *) b->mem.free; b->mem.free = b->mem.end; nxt_memzero(report, sizeof(nxt_status_report_t)); nxt_queue_each(engine, &nxt_router->engines, nxt_event_engine_t, link0) { - report->accepted_conns += engine->accepted_conns_cnt; - report->idle_conns += engine->idle_conns_cnt; - report->closed_conns += engine->closed_conns_cnt; - report->requests += engine->requests_cnt; - - } nxt_queue_loop; + report->idle_conns += engine->idle_conns_cnt; + report->closed_conns += engine->closed_conns_cnt; + report->requests += engine->requests_cnt; + } + nxt_queue_loop; report->apps_count = 0; - app_stat = report->apps; - p = b->mem.end; + app_stat = report->apps; + p = b->mem.end; nxt_queue_each(app, &nxt_router->apps, nxt_app_t, link) { p -= app->name.length; @@ -1004,16 +1008,17 @@ nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_memcpy(p, app->name.start, app->name.length); app_stat->name.length = app->name.length; - app_stat->name.start = (u_char *) (p - b->mem.pos); + app_stat->name.start = (u_char *) (p - b->mem.pos); - app_stat->active_requests = app->active_requests; + app_stat->active_requests = app->active_requests; app_stat->pending_processes = app->pending_processes; - app_stat->processes = app->processes; - app_stat->idle_processes = app->idle_processes; + app_stat->processes = app->processes; + app_stat->idle_processes = app->idle_processes; report->apps_count++; app_stat++; - } nxt_queue_loop; + } + nxt_queue_loop; type = NXT_PORT_MSG_RPC_READY_LAST; @@ -1022,14 +1027,12 @@ nxt_router_status_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_socket_write(task, port, type, -1, msg->port_msg.stream, 0, b); } - static void nxt_router_app_process_remove_pid(nxt_task_t *task, nxt_port_t *port, - void *data) -{ + void *data) { union { - nxt_pid_t removed_pid; - void *data; + nxt_pid_t removed_pid; + void *data; } u; u.data = data; @@ -1037,19 +1040,16 @@ nxt_router_app_process_remove_pid(nxt_task_t *task, nxt_port_t *port, nxt_port_rpc_remove_peer(task, port, u.removed_pid); } - static void -nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_event_engine_t *engine; +nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_event_engine_t *engine; nxt_port_remove_pid_handler(task, msg); - nxt_queue_each(engine, &nxt_router->engines, nxt_event_engine_t, link0) - { + nxt_queue_each(engine, &nxt_router->engines, nxt_event_engine_t, link0) { if (nxt_fast_path(engine->port != NULL)) { nxt_port_post(task, engine->port, nxt_router_app_process_remove_pid, - msg->u.data); + msg->u.data); } } nxt_queue_loop; @@ -1063,13 +1063,11 @@ nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_port_rpc_handler(task, msg); } - static nxt_router_temp_conf_t * -nxt_router_temp_conf(nxt_task_t *task) -{ - nxt_mp_t *mp, *tmp; - nxt_router_conf_t *rtcf; - nxt_router_temp_conf_t *tmcf; +nxt_router_temp_conf(nxt_task_t *task) { + nxt_mp_t *mp, *tmp; + nxt_router_conf_t *rtcf; + nxt_router_temp_conf_t *tmcf; mp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp == NULL)) { @@ -1102,13 +1100,13 @@ nxt_router_temp_conf(nxt_task_t *task) goto out_free; } - tmcf->mem_pool = tmp; + tmcf->mem_pool = tmp; tmcf->router_conf = rtcf; - tmcf->count = 1; - tmcf->engine = task->thread->engine; + tmcf->count = 1; + tmcf->engine = task->thread->engine; - tmcf->engines = nxt_array_create(tmcf->mem_pool, 4, - sizeof(nxt_router_engine_conf_t)); + tmcf->engines + = nxt_array_create(tmcf->mem_pool, 4, sizeof(nxt_router_engine_conf_t)); if (nxt_slow_path(tmcf->engines == NULL)) { goto out_free; } @@ -1149,42 +1147,36 @@ nxt_router_temp_conf(nxt_task_t *task) return NULL; } - nxt_inline nxt_bool_t -nxt_router_app_can_start(nxt_app_t *app) -{ +nxt_router_app_can_start(nxt_app_t *app) { return app->processes + app->pending_processes < app->max_processes - && app->pending_processes < app->max_pending_processes; + && app->pending_processes < app->max_pending_processes; } - nxt_inline nxt_bool_t -nxt_router_app_need_start(nxt_app_t *app) -{ +nxt_router_app_need_start(nxt_app_t *app) { return (app->active_requests - > app->port_hash_count + app->pending_processes) + > app->port_hash_count + app->pending_processes) || (app->spare_processes - > app->idle_processes + app->pending_processes); + > app->idle_processes + app->pending_processes); } - void -nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) -{ +nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) { nxt_int_t ret; - nxt_app_t *app; - nxt_router_t *router; - nxt_runtime_t *rt; - nxt_queue_link_t *qlk; - nxt_socket_conf_t *skcf; - nxt_router_conf_t *rtcf; - nxt_router_temp_conf_t *tmcf; - const nxt_event_interface_t *interface; + nxt_app_t *app; + nxt_router_t *router; + nxt_runtime_t *rt; + nxt_queue_link_t *qlk; + nxt_socket_conf_t *skcf; + nxt_router_conf_t *rtcf; + nxt_router_temp_conf_t *tmcf; + const nxt_event_interface_t *interface; #if (NXT_TLS) - nxt_router_tlssock_t *tls; + nxt_router_tlssock_t *tls; #endif #if (NXT_HAVE_NJS) - nxt_router_js_module_t *js_module; + nxt_router_js_module_t *js_module; #endif tmcf = obj; @@ -1211,7 +1203,7 @@ nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) tls = nxt_queue_link_data(qlk, nxt_router_tlssock_t, link); nxt_cert_store_get(task, &tls->name, tmcf->mem_pool, - nxt_router_tls_rpc_handler, tls); + nxt_router_tls_rpc_handler, tls); return; } #endif @@ -1225,7 +1217,7 @@ nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) js_module = nxt_queue_link_data(qlk, nxt_router_js_module_t, link); nxt_script_store_get(task, &js_module->name, tmcf->mem_pool, - nxt_router_js_module_rpc_handler, js_module); + nxt_router_js_module_rpc_handler, js_module); return; } #endif @@ -1238,13 +1230,12 @@ nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) } nxt_queue_each(app, &tmcf->apps, nxt_app_t, link) { - if (nxt_router_app_need_start(app)) { nxt_router_app_rpc_create(task, tmcf, app); return; } - - } nxt_queue_loop; + } + nxt_queue_loop; if (rtcf->access_log != NULL && rtcf->access_log->fd == -1) { nxt_router_access_log_open(task, tmcf); @@ -1295,24 +1286,20 @@ nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data) return; } - static void -nxt_router_conf_wait(nxt_task_t *task, void *obj, void *data) -{ - nxt_joint_job_t *job; +nxt_router_conf_wait(nxt_task_t *task, void *obj, void *data) { + nxt_joint_job_t *job; job = obj; nxt_router_conf_ready(task, job->tmcf); } - static void -nxt_router_conf_ready(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) -{ +nxt_router_conf_ready(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) { uint32_t count; - nxt_router_conf_t *rtcf; - nxt_thread_spinlock_t *lock; + nxt_router_conf_t *rtcf; + nxt_thread_spinlock_t *lock; nxt_debug(task, "temp conf %p count: %D", tmcf, tmcf->count); @@ -1345,25 +1332,21 @@ nxt_router_conf_ready(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) nxt_mp_release(tmcf->mem_pool); } - void -nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) -{ - nxt_app_t *app; +nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) { + nxt_app_t *app; nxt_socket_t s; - nxt_router_t *router; - nxt_queue_link_t *qlk; - nxt_socket_conf_t *skcf; - nxt_router_conf_t *rtcf; + nxt_router_t *router; + nxt_queue_link_t *qlk; + nxt_socket_conf_t *skcf; + nxt_router_conf_t *rtcf; nxt_alert(task, "failed to apply new conf"); for (qlk = nxt_queue_first(&creating_sockets); - qlk != nxt_queue_tail(&creating_sockets); - qlk = nxt_queue_next(qlk)) - { + qlk != nxt_queue_tail(&creating_sockets); qlk = nxt_queue_next(qlk)) { skcf = nxt_queue_link_data(qlk, nxt_socket_conf_t, link); - s = skcf->listen->socket; + s = skcf->listen->socket; if (s != -1) { nxt_socket_close(task, s); @@ -1375,10 +1358,9 @@ nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) rtcf = tmcf->router_conf; nxt_queue_each(app, &tmcf->apps, nxt_app_t, link) { - nxt_router_app_unlink(task, app); - - } nxt_queue_loop; + } + nxt_queue_loop; router = rtcf->router; @@ -1398,11 +1380,9 @@ nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) nxt_mp_release(tmcf->mem_pool); } - static void nxt_router_conf_send(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_port_msg_type_t type) -{ + nxt_port_msg_type_t type) { nxt_port_socket_write(task, tmcf->port, type, -1, tmcf->stream, 0, NULL); nxt_port_use(task, tmcf->port, -1); @@ -1410,8 +1390,7 @@ nxt_router_conf_send(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, tmcf->port = NULL; } - -static nxt_conf_map_t nxt_router_conf[] = { +static nxt_conf_map_t nxt_router_conf[] = { { nxt_string("listen_threads"), NXT_CONF_MAP_INT32, @@ -1420,7 +1399,7 @@ static nxt_conf_map_t nxt_router_conf[] = { }; -static nxt_conf_map_t nxt_router_app_conf[] = { +static nxt_conf_map_t nxt_router_app_conf[] = { { nxt_string("type"), NXT_CONF_MAP_STR, @@ -1453,7 +1432,7 @@ static nxt_conf_map_t nxt_router_app_conf[] = { }; -static nxt_conf_map_t nxt_router_app_limits_conf[] = { +static nxt_conf_map_t nxt_router_app_limits_conf[] = { { nxt_string("timeout"), NXT_CONF_MAP_MSEC, @@ -1462,7 +1441,7 @@ static nxt_conf_map_t nxt_router_app_limits_conf[] = { }; -static nxt_conf_map_t nxt_router_app_processes_conf[] = { +static nxt_conf_map_t nxt_router_app_processes_conf[] = { { nxt_string("spare"), NXT_CONF_MAP_INT32, @@ -1483,7 +1462,7 @@ static nxt_conf_map_t nxt_router_app_processes_conf[] = { }; -static nxt_conf_map_t nxt_router_listener_conf[] = { +static nxt_conf_map_t nxt_router_listener_conf[] = { { nxt_string("pass"), NXT_CONF_MAP_STR_COPY, @@ -1504,7 +1483,7 @@ static nxt_conf_map_t nxt_router_listener_conf[] = { }; -static nxt_conf_map_t nxt_router_http_conf[] = { +static nxt_conf_map_t nxt_router_http_conf[] = { { nxt_string("header_buffer_size"), NXT_CONF_MAP_SIZE, @@ -1591,7 +1570,7 @@ static nxt_conf_map_t nxt_router_http_conf[] = { }; -static nxt_conf_map_t nxt_router_websocket_conf[] = { +static nxt_conf_map_t nxt_router_websocket_conf[] = { { nxt_string("max_frame_size"), NXT_CONF_MAP_SIZE, @@ -1612,65 +1591,63 @@ static nxt_conf_map_t nxt_router_websocket_conf[] = { }; - static nxt_int_t nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - u_char *start, u_char *end) -{ - u_char *p; - size_t size; - nxt_mp_t *mp, *app_mp; - uint32_t next, next_target; - nxt_int_t ret; - nxt_str_t name, target; - nxt_app_t *app, *prev; - nxt_str_t *t, *s, *targets; - nxt_uint_t n, i; - nxt_port_t *port; - nxt_router_t *router; - nxt_app_joint_t *app_joint; + u_char *start, u_char *end) { + u_char *p; + size_t size; + nxt_mp_t *mp, *app_mp; + uint32_t next, next_target; + nxt_int_t ret; + nxt_str_t name, target; + nxt_app_t *app, *prev; + nxt_str_t *t, *s, *targets; + nxt_uint_t n, i; + nxt_port_t *port; + nxt_router_t *router; + nxt_app_joint_t *app_joint; #if (NXT_TLS) - nxt_tls_init_t *tls_init; - nxt_conf_value_t *certificate; + nxt_tls_init_t *tls_init; + nxt_conf_value_t *certificate; #endif #if (NXT_HAVE_NJS) - nxt_conf_value_t *js_module; + nxt_conf_value_t *js_module; #endif - nxt_conf_value_t *root, *conf, *http, *value, *websocket; - nxt_conf_value_t *applications, *application, *settings; - nxt_conf_value_t *listeners, *listener; - nxt_socket_conf_t *skcf; - nxt_router_conf_t *rtcf; - nxt_http_routes_t *routes; - nxt_event_engine_t *engine; - nxt_app_lang_module_t *lang; - nxt_router_app_conf_t apcf; - nxt_router_listener_conf_t lscf; - - static const nxt_str_t settings_path = nxt_string("/settings"); - static const nxt_str_t http_path = nxt_string("/settings/http"); - static const nxt_str_t applications_path = nxt_string("/applications"); - static const nxt_str_t listeners_path = nxt_string("/listeners"); - static const nxt_str_t routes_path = nxt_string("/routes"); - static const nxt_str_t access_log_path = nxt_string("/access_log"); + nxt_conf_value_t *root, *conf, *http, *value, *websocket; + nxt_conf_value_t *applications, *application, *settings; + nxt_conf_value_t *listeners, *listener; + nxt_socket_conf_t *skcf; + nxt_router_conf_t *rtcf; + nxt_http_routes_t *routes; + nxt_event_engine_t *engine; + nxt_app_lang_module_t *lang; + nxt_router_app_conf_t apcf; + nxt_router_listener_conf_t lscf; + + static const nxt_str_t settings_path = nxt_string("/settings"); + static const nxt_str_t http_path = nxt_string("/settings/http"); + static const nxt_str_t applications_path = nxt_string("/applications"); + static const nxt_str_t listeners_path = nxt_string("/listeners"); + static const nxt_str_t routes_path = nxt_string("/routes"); + static const nxt_str_t access_log_path = nxt_string("/access_log"); #if (NXT_TLS) - static const nxt_str_t certificate_path = nxt_string("/tls/certificate"); - static const nxt_str_t conf_commands_path = - nxt_string("/tls/conf_commands"); - static const nxt_str_t conf_cache_path = - nxt_string("/tls/session/cache_size"); - static const nxt_str_t conf_timeout_path = - nxt_string("/tls/session/timeout"); - static const nxt_str_t conf_tickets = nxt_string("/tls/session/tickets"); + static const nxt_str_t certificate_path = nxt_string("/tls/certificate"); + static const nxt_str_t conf_commands_path + = nxt_string("/tls/conf_commands"); + static const nxt_str_t conf_cache_path + = nxt_string("/tls/session/cache_size"); + static const nxt_str_t conf_timeout_path + = nxt_string("/tls/session/timeout"); + static const nxt_str_t conf_tickets = nxt_string("/tls/session/tickets"); #endif #if (NXT_HAVE_NJS) - static const nxt_str_t js_module_path = nxt_string("/settings/js_module"); + static const nxt_str_t js_module_path = nxt_string("/settings/js_module"); #endif - static const nxt_str_t static_path = nxt_string("/settings/http/static"); - static const nxt_str_t websocket_path = - nxt_string("/settings/http/websocket"); - static const nxt_str_t forwarded_path = nxt_string("/forwarded"); - static const nxt_str_t client_ip_path = nxt_string("/client_ip"); + static const nxt_str_t static_path = nxt_string("/settings/http/static"); + static const nxt_str_t websocket_path + = nxt_string("/settings/http/websocket"); + static const nxt_str_t forwarded_path = nxt_string("/forwarded"); + static const nxt_str_t client_ip_path = nxt_string("/client_ip"); root = nxt_conf_json_parse(tmcf->mem_pool, start, end, NULL); if (root == NULL) { @@ -1679,12 +1656,12 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } rtcf = tmcf->router_conf; - mp = rtcf->mem_pool; + mp = rtcf->mem_pool; settings = nxt_conf_get_path(root, &settings_path); if (settings != NULL) { ret = nxt_conf_map_object(mp, settings, nxt_router_conf, - nxt_nitems(nxt_router_conf), rtcf); + nxt_nitems(nxt_router_conf), rtcf); if (ret != NXT_OK) { nxt_alert(task, "router_conf map error"); return NXT_ERROR; @@ -1709,9 +1686,9 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (applications != NULL) { next = 0; - for ( ;; ) { - application = nxt_conf_next_object_member(applications, - &name, &next); + for (;;) { + application + = nxt_conf_next_object_member(applications, &name, &next); if (application == NULL) { break; } @@ -1735,8 +1712,8 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, app->mem_pool = app_mp; app->name.start = nxt_pointer_to(app, sizeof(nxt_app_t)); - app->conf.start = nxt_pointer_to(app, sizeof(nxt_app_t) - + name.length); + app->conf.start + = nxt_pointer_to(app, sizeof(nxt_app_t) + name.length); p = nxt_conf_json_print(app->conf.start, application, NULL); app->conf.length = p - app->conf.start; @@ -1761,14 +1738,14 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, continue; } - apcf.processes = 1; - apcf.max_processes = 1; + apcf.processes = 1; + apcf.max_processes = 1; apcf.spare_processes = 0; - apcf.timeout = 0; - apcf.idle_timeout = 15000; - apcf.limits_value = NULL; + apcf.timeout = 0; + apcf.idle_timeout = 15000; + apcf.limits_value = NULL; apcf.processes_value = NULL; - apcf.targets_value = NULL; + apcf.targets_value = NULL; app_joint = nxt_malloc(sizeof(nxt_app_joint_t)); if (nxt_slow_path(app_joint == NULL)) { @@ -1778,23 +1755,21 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_memzero(app_joint, sizeof(nxt_app_joint_t)); ret = nxt_conf_map_object(mp, application, nxt_router_app_conf, - nxt_nitems(nxt_router_app_conf), &apcf); + nxt_nitems(nxt_router_app_conf), &apcf); if (ret != NXT_OK) { nxt_alert(task, "application map error"); goto app_fail; } if (apcf.limits_value != NULL) { - if (nxt_conf_type(apcf.limits_value) != NXT_CONF_OBJECT) { nxt_alert(task, "application limits is not object"); goto app_fail; } ret = nxt_conf_map_object(mp, apcf.limits_value, - nxt_router_app_limits_conf, - nxt_nitems(nxt_router_app_limits_conf), - &apcf); + nxt_router_app_limits_conf, + nxt_nitems(nxt_router_app_limits_conf), &apcf); if (ret != NXT_OK) { nxt_alert(task, "application limits map error"); goto app_fail; @@ -1802,19 +1777,17 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } if (apcf.processes_value != NULL - && nxt_conf_type(apcf.processes_value) == NXT_CONF_OBJECT) - { + && nxt_conf_type(apcf.processes_value) == NXT_CONF_OBJECT) { ret = nxt_conf_map_object(mp, apcf.processes_value, - nxt_router_app_processes_conf, - nxt_nitems(nxt_router_app_processes_conf), - &apcf); + nxt_router_app_processes_conf, + nxt_nitems(nxt_router_app_processes_conf), &apcf); if (ret != NXT_OK) { nxt_alert(task, "application processes map error"); goto app_fail; } } else { - apcf.max_processes = apcf.processes; + apcf.max_processes = apcf.processes; apcf.spare_processes = apcf.processes; } @@ -1830,7 +1803,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, for (i = 0; i < n; i++) { (void) nxt_conf_next_object_member(apcf.targets_value, - &target, &next_target); + &target, &next_target); s = nxt_str_dup(app_mp, &targets[i], &target); if (nxt_slow_path(s == NULL)) { @@ -1868,12 +1841,12 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, app->name.length = name.length; nxt_memcpy(app->name.start, name.start, name.length); - app->type = lang->type; - app->max_processes = apcf.max_processes; + app->type = lang->type; + app->max_processes = apcf.max_processes; app->spare_processes = apcf.spare_processes; - app->max_pending_processes = apcf.spare_processes - ? apcf.spare_processes : 1; - app->timeout = apcf.timeout; + app->max_pending_processes + = apcf.spare_processes ? apcf.spare_processes : 1; + app->timeout = apcf.timeout; app->idle_timeout = apcf.idle_timeout; app->targets = targets; @@ -1883,8 +1856,8 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, app->engine = engine; app->adjust_idle_work.handler = nxt_router_adjust_idle_timer; - app->adjust_idle_work.task = &engine->task; - app->adjust_idle_work.obj = app; + app->adjust_idle_work.task = &engine->task; + app->adjust_idle_work.obj = app; nxt_queue_insert_tail(&tmcf->apps, &app->link); @@ -1898,20 +1871,20 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, app->joint = app_joint; app_joint->use_count = 1; - app_joint->app = app; + app_joint->app = app; - app_joint->idle_timer.bias = NXT_TIMER_DEFAULT_BIAS; + app_joint->idle_timer.bias = NXT_TIMER_DEFAULT_BIAS; app_joint->idle_timer.work_queue = &engine->fast_work_queue; - app_joint->idle_timer.handler = nxt_router_app_idle_timeout; - app_joint->idle_timer.task = &engine->task; - app_joint->idle_timer.log = app_joint->idle_timer.task->log; + app_joint->idle_timer.handler = nxt_router_app_idle_timeout; + app_joint->idle_timer.task = &engine->task; + app_joint->idle_timer.log = app_joint->idle_timer.task->log; app_joint->free_app_work.handler = nxt_router_free_app; - app_joint->free_app_work.task = &engine->task; - app_joint->free_app_work.obj = app_joint; + app_joint->free_app_work.task = &engine->task; + app_joint->free_app_work.obj = app_joint; port = nxt_port_new(task, NXT_SHARED_PORT_ID, nxt_pid, - NXT_PROCESS_APP); + NXT_PROCESS_APP); if (nxt_slow_path(port == NULL)) { return NXT_ERROR; } @@ -1969,7 +1942,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (listeners != NULL) { next = 0; - for ( ;; ) { + for (;;) { listener = nxt_conf_next_object_member(listeners, &name, &next); if (listener == NULL) { break; @@ -1980,8 +1953,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, lscf.backlog = -1; ret = nxt_conf_map_object(mp, listener, nxt_router_listener_conf, - nxt_nitems(nxt_router_listener_conf), - &lscf); + nxt_nitems(nxt_router_listener_conf), &lscf); if (ret != NXT_OK) { nxt_alert(task, "listener map error"); goto fail; @@ -1995,36 +1967,35 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } // STUB, default values if http block is not defined. - skcf->header_buffer_size = 2048; + skcf->header_buffer_size = 2048; skcf->large_header_buffer_size = 8192; - skcf->large_header_buffers = 4; - skcf->discard_unsafe_fields = 1; - skcf->body_buffer_size = 16 * 1024; - skcf->max_body_size = 8 * 1024 * 1024; + skcf->large_header_buffers = 4; + skcf->discard_unsafe_fields = 1; + skcf->body_buffer_size = 16 * 1024; + skcf->max_body_size = 8 * 1024 * 1024; skcf->proxy_header_buffer_size = 64 * 1024; - skcf->proxy_buffer_size = 4096; - skcf->proxy_buffers = 256; - skcf->idle_timeout = 180 * 1000; - skcf->header_read_timeout = 30 * 1000; - skcf->body_read_timeout = 30 * 1000; - skcf->send_timeout = 30 * 1000; - skcf->proxy_timeout = 60 * 1000; - skcf->proxy_send_timeout = 30 * 1000; - skcf->proxy_read_timeout = 30 * 1000; - - skcf->server_version = 1; + skcf->proxy_buffer_size = 4096; + skcf->proxy_buffers = 256; + skcf->idle_timeout = 180 * 1000; + skcf->header_read_timeout = 30 * 1000; + skcf->body_read_timeout = 30 * 1000; + skcf->send_timeout = 30 * 1000; + skcf->proxy_timeout = 60 * 1000; + skcf->proxy_send_timeout = 30 * 1000; + skcf->proxy_read_timeout = 30 * 1000; + + skcf->server_version = 1; skcf->chunked_transform = 0; - skcf->websocket_conf.max_frame_size = 1024 * 1024; - skcf->websocket_conf.read_timeout = 60 * 1000; + skcf->websocket_conf.max_frame_size = 1024 * 1024; + skcf->websocket_conf.read_timeout = 60 * 1000; skcf->websocket_conf.keepalive_interval = 30 * 1000; nxt_str_null(&skcf->body_temp_path); if (http != NULL) { ret = nxt_conf_map_object(mp, http, nxt_router_http_conf, - nxt_nitems(nxt_router_http_conf), - skcf); + nxt_nitems(nxt_router_http_conf), skcf); if (ret != NXT_OK) { nxt_alert(task, "http map error"); goto fail; @@ -2033,9 +2004,9 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, if (websocket != NULL) { ret = nxt_conf_map_object(mp, websocket, - nxt_router_websocket_conf, - nxt_nitems(nxt_router_websocket_conf), - &skcf->websocket_conf); + nxt_router_websocket_conf, + nxt_nitems(nxt_router_websocket_conf), + &skcf->websocket_conf); if (ret != NXT_OK) { nxt_alert(task, "websocket map error"); goto fail; @@ -2045,7 +2016,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, t = &skcf->body_temp_path; if (t->length == 0) { - t->start = (u_char *) task->thread->runtime->tmp; + t->start = (u_char *) task->thread->runtime->tmp; t->length = nxt_strlen(t->start); } @@ -2077,7 +2048,7 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } tls_init->cache_size = 0; - tls_init->timeout = 300; + tls_init->timeout = 300; value = nxt_conf_get_path(listener, &conf_cache_path); if (value != NULL) { @@ -2089,21 +2060,21 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, tls_init->timeout = nxt_conf_get_number(value); } - tls_init->conf_cmds = nxt_conf_get_path(listener, - &conf_commands_path); + tls_init->conf_cmds + = nxt_conf_get_path(listener, &conf_commands_path); - tls_init->tickets_conf = nxt_conf_get_path(listener, - &conf_tickets); + tls_init->tickets_conf + = nxt_conf_get_path(listener, &conf_tickets); n = nxt_conf_array_elements_count_or_1(certificate); for (i = 0; i < n; i++) { - value = nxt_conf_get_array_element_or_itself(certificate, - i); + value + = nxt_conf_get_array_element_or_itself(certificate, i); nxt_assert(value != NULL); ret = nxt_router_conf_tls_insert(tmcf, value, skcf, - tls_init, i == 0); + tls_init, i == 0); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } @@ -2112,16 +2083,16 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, #endif skcf->listen->handler = nxt_http_conn_init; - skcf->router_conf = rtcf; + skcf->router_conf = rtcf; skcf->router_conf->count++; if (lscf.pass.length != 0) { skcf->action = nxt_http_action_create(task, tmcf, &lscf.pass); - /* COMPATIBILITY: listener application. */ + /* COMPATIBILITY: listener application. */ } else if (lscf.application.length > 0) { - skcf->action = nxt_http_pass_application(task, rtcf, - &lscf.application); + skcf->action + = nxt_http_pass_application(task, rtcf, &lscf.application); } if (nxt_slow_path(skcf->action == NULL)) { @@ -2184,12 +2155,11 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, fail: nxt_queue_each(app, &tmcf->apps, nxt_app_t, link) { - nxt_queue_remove(&app->link); nxt_thread_mutex_destroy(&app->mutex); nxt_mp_destroy(app->mem_pool); - - } nxt_queue_loop; + } + nxt_queue_loop; return NXT_ERROR; } @@ -2199,20 +2169,19 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, static nxt_int_t nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *value, nxt_socket_conf_t *skcf, - nxt_tls_init_t *tls_init, nxt_bool_t last) -{ - nxt_router_tlssock_t *tls; + nxt_conf_value_t *value, nxt_socket_conf_t *skcf, nxt_tls_init_t *tls_init, + nxt_bool_t last) { + nxt_router_tlssock_t *tls; tls = nxt_mp_get(tmcf->mem_pool, sizeof(nxt_router_tlssock_t)); if (nxt_slow_path(tls == NULL)) { return NXT_ERROR; } - tls->tls_init = tls_init; + tls->tls_init = tls_init; tls->socket_conf = skcf; - tls->temp_conf = tmcf; - tls->last = last; + tls->temp_conf = tmcf; + tls->last = last; nxt_conf_get_string(value, &tls->name); nxt_queue_insert_tail(&tmcf->tls, &tls->link); @@ -2227,18 +2196,17 @@ nxt_router_conf_tls_insert(nxt_router_temp_conf_t *tmcf, static void nxt_router_js_module_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ + void *data) { nxt_int_t ret; nxt_str_t text; - nxt_router_conf_t *rtcf; - nxt_router_temp_conf_t *tmcf; - nxt_router_js_module_t *js_module; + nxt_router_conf_t *rtcf; + nxt_router_temp_conf_t *tmcf; + nxt_router_js_module_t *js_module; nxt_debug(task, "auto module rpc handler"); js_module = data; - tmcf = js_module->temp_conf; + tmcf = js_module->temp_conf; if (msg == NULL || msg->port_msg.type == _NXT_PORT_MSG_RPC_ERROR) { goto fail; @@ -2265,7 +2233,7 @@ nxt_router_js_module_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_router_conf_apply, task, tmcf, NULL); + nxt_router_conf_apply, task, tmcf, NULL); return; fail: @@ -2273,12 +2241,10 @@ nxt_router_js_module_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_router_conf_error(task, tmcf); } - static nxt_int_t nxt_router_js_module_insert(nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *value) -{ - nxt_router_js_module_t *js_module; + nxt_conf_value_t *value) { + nxt_router_js_module_t *js_module; js_module = nxt_mp_get(tmcf->mem_pool, sizeof(nxt_router_js_module_t)); if (nxt_slow_path(js_module == NULL)) { @@ -2298,16 +2264,15 @@ nxt_router_js_module_insert(nxt_router_temp_conf_t *tmcf, static nxt_int_t nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, - nxt_conf_value_t *conf) -{ + nxt_conf_value_t *conf) { uint32_t next, i; - nxt_mp_t *mp; - nxt_str_t *type, exten, str, *s; + nxt_mp_t *mp; + nxt_str_t *type, exten, str, *s; nxt_int_t ret; nxt_uint_t exts; - nxt_conf_value_t *mtypes_conf, *ext_conf, *value; + nxt_conf_value_t *mtypes_conf, *ext_conf, *value; - static const nxt_str_t mtypes_path = nxt_string("/mime_types"); + static const nxt_str_t mtypes_path = nxt_string("/mime_types"); mp = rtcf->mem_pool; @@ -2325,7 +2290,7 @@ nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, if (mtypes_conf != NULL) { next = 0; - for ( ;; ) { + for (;;) { ext_conf = nxt_conf_next_object_member(mtypes_conf, &str, &next); if (ext_conf == NULL) { @@ -2344,7 +2309,7 @@ nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, } ret = nxt_http_static_mtypes_hash_add(mp, &rtcf->mtypes_hash, - &exten, type); + &exten, type); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -2363,7 +2328,7 @@ nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, } ret = nxt_http_static_mtypes_hash_add(mp, &rtcf->mtypes_hash, - &exten, type); + &exten, type); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -2374,39 +2339,37 @@ nxt_router_conf_process_static(nxt_task_t *task, nxt_router_conf_t *rtcf, return NXT_OK; } - static nxt_http_forward_t * -nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf) -{ +nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, + nxt_conf_value_t *conf) { nxt_int_t ret; - nxt_conf_value_t *header_conf, *client_ip_conf, *protocol_conf; - nxt_conf_value_t *source_conf, *recursive_conf; - nxt_http_forward_t *forward; - nxt_http_route_addr_rule_t *source; + nxt_conf_value_t *header_conf, *client_ip_conf, *protocol_conf; + nxt_conf_value_t *source_conf, *recursive_conf; + nxt_http_forward_t *forward; + nxt_http_route_addr_rule_t *source; - static const nxt_str_t header_path = nxt_string("/header"); - static const nxt_str_t client_ip_path = nxt_string("/client_ip"); - static const nxt_str_t protocol_path = nxt_string("/protocol"); - static const nxt_str_t source_path = nxt_string("/source"); - static const nxt_str_t recursive_path = nxt_string("/recursive"); + static const nxt_str_t header_path = nxt_string("/header"); + static const nxt_str_t client_ip_path = nxt_string("/client_ip"); + static const nxt_str_t protocol_path = nxt_string("/protocol"); + static const nxt_str_t source_path = nxt_string("/source"); + static const nxt_str_t recursive_path = nxt_string("/recursive"); header_conf = nxt_conf_get_path(conf, &header_path); if (header_conf != NULL) { client_ip_conf = nxt_conf_get_path(conf, &header_path); - protocol_conf = NULL; + protocol_conf = NULL; } else { client_ip_conf = nxt_conf_get_path(conf, &client_ip_path); - protocol_conf = nxt_conf_get_path(conf, &protocol_path); + protocol_conf = nxt_conf_get_path(conf, &protocol_path); } - source_conf = nxt_conf_get_path(conf, &source_path); + source_conf = nxt_conf_get_path(conf, &source_path); recursive_conf = nxt_conf_get_path(conf, &recursive_path); if (source_conf == NULL - || (protocol_conf == NULL && client_ip_conf == NULL)) - { + || (protocol_conf == NULL && client_ip_conf == NULL)) { return NULL; } @@ -2428,7 +2391,7 @@ nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf) if (client_ip_conf != NULL) { ret = nxt_router_conf_forward_header(mp, client_ip_conf, - &forward->client_ip); + &forward->client_ip); if (nxt_slow_path(ret != NXT_OK)) { return NULL; } @@ -2436,7 +2399,7 @@ nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf) if (protocol_conf != NULL) { ret = nxt_router_conf_forward_header(mp, protocol_conf, - &forward->protocol); + &forward->protocol); if (nxt_slow_path(ret != NXT_OK)) { return NULL; } @@ -2445,14 +2408,12 @@ nxt_router_conf_forward(nxt_task_t *task, nxt_mp_t *mp, nxt_conf_value_t *conf) return forward; } - static nxt_int_t nxt_router_conf_forward_header(nxt_mp_t *mp, nxt_conf_value_t *conf, - nxt_http_forward_header_t *fh) -{ - char c; - size_t i; - uint32_t hash; + nxt_http_forward_header_t *fh) { + char c; + size_t i; + uint32_t hash; fh->header = nxt_conf_get_string_dup(conf, mp, NULL); if (nxt_slow_path(fh->header == NULL)) { @@ -2462,7 +2423,7 @@ nxt_router_conf_forward_header(nxt_mp_t *mp, nxt_conf_value_t *conf, hash = NXT_HTTP_FIELD_HASH_INIT; for (i = 0; i < fh->header->length; i++) { - c = fh->header->start[i]; + c = fh->header->start[i]; hash = nxt_http_field_hash_char(hash, nxt_lowcase(c)); } @@ -2473,37 +2434,32 @@ nxt_router_conf_forward_header(nxt_mp_t *mp, nxt_conf_value_t *conf, return NXT_OK; } - static nxt_app_t * -nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name) -{ - nxt_app_t *app; +nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name) { + nxt_app_t *app; nxt_queue_each(app, queue, nxt_app_t, link) { - if (nxt_strstr_eq(name, &app->name)) { return app; } - - } nxt_queue_loop; + } + nxt_queue_loop; return NULL; } - static nxt_int_t -nxt_router_app_queue_init(nxt_task_t *task, nxt_port_t *port) -{ - void *mem; - nxt_int_t fd; +nxt_router_app_queue_init(nxt_task_t *task, nxt_port_t *port) { + void *mem; + nxt_int_t fd; fd = nxt_shm_open(task, sizeof(nxt_app_queue_t)); if (nxt_slow_path(fd == -1)) { return NXT_ERROR; } - mem = nxt_mem_mmap(NULL, sizeof(nxt_app_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + mem = nxt_mem_mmap(NULL, sizeof(nxt_app_queue_t), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_fd_close(fd); @@ -2513,25 +2469,23 @@ nxt_router_app_queue_init(nxt_task_t *task, nxt_port_t *port) nxt_app_queue_init(mem); port->queue_fd = fd; - port->queue = mem; + port->queue = mem; return NXT_OK; } - static nxt_int_t -nxt_router_port_queue_init(nxt_task_t *task, nxt_port_t *port) -{ - void *mem; - nxt_int_t fd; +nxt_router_port_queue_init(nxt_task_t *task, nxt_port_t *port) { + void *mem; + nxt_int_t fd; fd = nxt_shm_open(task, sizeof(nxt_port_queue_t)); if (nxt_slow_path(fd == -1)) { return NXT_ERROR; } - mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_fd_close(fd); @@ -2541,23 +2495,20 @@ nxt_router_port_queue_init(nxt_task_t *task, nxt_port_t *port) nxt_port_queue_init(mem); port->queue_fd = fd; - port->queue = mem; + port->queue = mem; return NXT_OK; } - static nxt_int_t -nxt_router_port_queue_map(nxt_task_t *task, nxt_port_t *port, nxt_fd_t fd) -{ - void *mem; +nxt_router_port_queue_map(nxt_task_t *task, nxt_port_t *port, nxt_fd_t fd) { + void *mem; nxt_assert(fd != -1); - mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + mem = nxt_mem_mmap(NULL, sizeof(nxt_port_queue_t), PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { - return NXT_ERROR; } @@ -2566,61 +2517,54 @@ nxt_router_port_queue_map(nxt_task_t *task, nxt_port_t *port, nxt_fd_t fd) return NXT_OK; } - -static const nxt_lvlhsh_proto_t nxt_router_apps_hash_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t nxt_router_apps_hash_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_router_apps_hash_test, nxt_mp_lvlhsh_alloc, nxt_mp_lvlhsh_free, }; - static nxt_int_t -nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_app_t *app; +nxt_router_apps_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_app_t *app; app = data; return nxt_strstr_eq(&lhq->key, &app->name) ? NXT_OK : NXT_DECLINED; } - static nxt_int_t -nxt_router_apps_hash_add(nxt_router_conf_t *rtcf, nxt_app_t *app) -{ - nxt_lvlhsh_query_t lhq; +nxt_router_apps_hash_add(nxt_router_conf_t *rtcf, nxt_app_t *app) { + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(app->name.start, app->name.length); - lhq.replace = 0; - lhq.key = app->name; - lhq.value = app; - lhq.proto = &nxt_router_apps_hash_proto; - lhq.pool = rtcf->mem_pool; + lhq.replace = 0; + lhq.key = app->name; + lhq.value = app; + lhq.proto = &nxt_router_apps_hash_proto; + lhq.pool = rtcf->mem_pool; switch (nxt_lvlhsh_insert(&rtcf->apps_hash, &lhq)) { - case NXT_OK: return NXT_OK; case NXT_DECLINED: nxt_thread_log_alert("router app hash adding failed: " - "\"%V\" is already in hash", &lhq.key); + "\"%V\" is already in hash", + &lhq.key); /* Fall through. */ default: return NXT_ERROR; } } - static nxt_app_t * -nxt_router_apps_hash_get(nxt_router_conf_t *rtcf, nxt_str_t *name) -{ - nxt_lvlhsh_query_t lhq; +nxt_router_apps_hash_get(nxt_router_conf_t *rtcf, nxt_str_t *name) { + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_router_apps_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_router_apps_hash_proto; if (nxt_lvlhsh_find(&rtcf->apps_hash, &lhq) != NXT_OK) { return NULL; @@ -2629,16 +2573,14 @@ nxt_router_apps_hash_get(nxt_router_conf_t *rtcf, nxt_str_t *name) return lhq.value; } - static void -nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf, int i) -{ - nxt_app_t *app; - nxt_lvlhsh_each_t lhe; +nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf, int i) { + nxt_app_t *app; + nxt_lvlhsh_each_t lhe; nxt_lvlhsh_each_init(&lhe, &nxt_router_apps_hash_proto); - for ( ;; ) { + for (;;) { app = nxt_lvlhsh_each(&rtcf->apps_hash, &lhe); if (app == NULL) { @@ -2649,21 +2591,18 @@ nxt_router_apps_hash_use(nxt_task_t *task, nxt_router_conf_t *rtcf, int i) } } - typedef struct { - nxt_app_t *app; + nxt_app_t *app; nxt_int_t target; } nxt_http_app_conf_t; - nxt_int_t nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name, - nxt_str_t *target, nxt_http_action_t *action) -{ - nxt_app_t *app; - nxt_str_t *targets; + nxt_str_t *target, nxt_http_action_t *action) { + nxt_app_t *app; + nxt_str_t *targets; nxt_uint_t i; - nxt_http_app_conf_t *conf; + nxt_http_app_conf_t *conf; app = nxt_router_apps_hash_get(rtcf, name); if (app == NULL) { @@ -2676,14 +2615,15 @@ nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name, } action->handler = nxt_http_application_handler; - action->u.conf = conf; + action->u.conf = conf; conf->app = app; if (target != NULL && target->length != 0) { targets = app->targets; - for (i = 0; !nxt_strstr_eq(target, &targets[i]); i++); + for (i = 0; !nxt_strstr_eq(target, &targets[i]); i++) + ; conf->target = i; @@ -2694,17 +2634,15 @@ nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name, return NXT_OK; } - static nxt_socket_conf_t * nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_str_t *name, int backlog) -{ + nxt_str_t *name, int backlog) { size_t size; nxt_int_t ret; nxt_bool_t wildcard; - nxt_sockaddr_t *sa; - nxt_socket_conf_t *skcf; - nxt_listen_socket_t *ls; + nxt_sockaddr_t *sa; + nxt_socket_conf_t *skcf; + nxt_listen_socket_t *ls; sa = nxt_sockaddr_parse(tmcf->mem_pool, name); if (nxt_slow_path(sa == NULL)) { @@ -2714,8 +2652,8 @@ nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, sa->type = SOCK_STREAM; - nxt_debug(task, "router listener: \"%*s\"", - (size_t) sa->length, nxt_sockaddr_start(sa)); + nxt_debug(task, "router listener: \"%*s\"", (size_t) sa->length, + nxt_sockaddr_start(sa)); skcf = nxt_mp_zget(tmcf->router_conf->mem_pool, sizeof(nxt_socket_conf_t)); if (nxt_slow_path(skcf == NULL)) { @@ -2727,7 +2665,6 @@ nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, ret = nxt_router_listen_socket_find(tmcf, skcf, sa); if (ret != NXT_OK) { - ls = nxt_zalloc(sizeof(nxt_listen_socket_t) + size); if (nxt_slow_path(ls == NULL)) { return NULL; @@ -2740,9 +2677,9 @@ nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, nxt_listen_socket_remote_size(ls); - ls->socket = -1; - ls->backlog = backlog > -1 ? backlog : NXT_LISTEN_BACKLOG; - ls->flags = NXT_NONBLOCK; + ls->socket = -1; + ls->backlog = backlog > -1 ? backlog : NXT_LISTEN_BACKLOG; + ls->flags = NXT_NONBLOCK; ls->read_after_accept = 1; } @@ -2775,21 +2712,17 @@ nxt_router_socket_conf(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return skcf; } - static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf, - nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa) -{ - nxt_router_t *router; - nxt_queue_link_t *qlk; - nxt_socket_conf_t *skcf; + nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa) { + nxt_router_t *router; + nxt_queue_link_t *qlk; + nxt_socket_conf_t *skcf; router = tmcf->router_conf->router; for (qlk = nxt_queue_first(&router->sockets); - qlk != nxt_queue_tail(&router->sockets); - qlk = nxt_queue_next(qlk)) - { + qlk != nxt_queue_tail(&router->sockets); qlk = nxt_queue_next(qlk)) { skcf = nxt_queue_link_data(qlk, nxt_socket_conf_t, link); if (nxt_sockaddr_cmp(skcf->listen->sockaddr, sa)) { @@ -2809,18 +2742,16 @@ nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf, return NXT_DECLINED; } - static void nxt_router_listen_socket_rpc_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf) -{ + nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf) { size_t size; uint32_t stream; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *main_port, *router_port; - nxt_runtime_t *rt; - nxt_socket_rpc_t *rpc; + nxt_buf_t *b; + nxt_port_t *main_port, *router_port; + nxt_runtime_t *rt; + nxt_socket_rpc_t *rpc; rpc = nxt_mp_alloc(tmcf->mem_pool, sizeof(nxt_socket_rpc_t)); if (rpc == NULL) { @@ -2828,7 +2759,7 @@ nxt_router_listen_socket_rpc_create(nxt_task_t *task, } rpc->socket_conf = skcf; - rpc->temp_conf = tmcf; + rpc->temp_conf = tmcf; size = nxt_sockaddr_size(skcf->listen->sockaddr); @@ -2841,20 +2772,19 @@ nxt_router_listen_socket_rpc_create(nxt_task_t *task, b->mem.free = nxt_cpymem(b->mem.free, skcf->listen->sockaddr, size); - rt = task->thread->runtime; - main_port = rt->port_by_type[NXT_PROCESS_MAIN]; + rt = task->thread->runtime; + main_port = rt->port_by_type[NXT_PROCESS_MAIN]; router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; stream = nxt_port_rpc_register_handler(task, router_port, - nxt_router_listen_socket_ready, - nxt_router_listen_socket_error, - main_port->pid, rpc); + nxt_router_listen_socket_ready, nxt_router_listen_socket_error, + main_port->pid, rpc); if (nxt_slow_path(stream == 0)) { goto fail; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_SOCKET, -1, - stream, router_port->id, b); + stream, router_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, router_port, stream); @@ -2868,14 +2798,12 @@ nxt_router_listen_socket_rpc_create(nxt_task_t *task, nxt_router_conf_error(task, tmcf); } - static void nxt_router_listen_socket_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ + void *data) { nxt_int_t ret; nxt_socket_t s; - nxt_socket_rpc_t *rpc; + nxt_socket_rpc_t *rpc; rpc = data; @@ -2896,7 +2824,7 @@ nxt_router_listen_socket_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, rpc->socket_conf->listen->socket = s; nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_router_conf_apply, task, rpc->temp_conf, NULL); + nxt_router_conf_apply, task, rpc->temp_conf, NULL); return; @@ -2907,15 +2835,13 @@ nxt_router_listen_socket_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_router_conf_error(task, rpc->temp_conf); } - static void nxt_router_listen_socket_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_socket_rpc_t *rpc; - nxt_router_temp_conf_t *tmcf; + void *data) { + nxt_socket_rpc_t *rpc; + nxt_router_temp_conf_t *tmcf; - rpc = data; + rpc = data; tmcf = rpc->temp_conf; #if 0 @@ -2973,18 +2899,17 @@ nxt_router_listen_socket_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, static void nxt_router_tls_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_mp_t *mp; + void *data) { + nxt_mp_t *mp; nxt_int_t ret; - nxt_tls_conf_t *tlscf; - nxt_router_tlssock_t *tls; - nxt_tls_bundle_conf_t *bundle; - nxt_router_temp_conf_t *tmcf; + nxt_tls_conf_t *tlscf; + nxt_router_tlssock_t *tls; + nxt_tls_bundle_conf_t *bundle; + nxt_router_temp_conf_t *tmcf; nxt_debug(task, "tls rpc handler"); - tls = data; + tls = data; tmcf = tls->temp_conf; if (msg == NULL || msg->port_msg.type == _NXT_PORT_MSG_RPC_ERROR) { @@ -3000,7 +2925,7 @@ nxt_router_tls_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } tlscf->no_wait_shutdown = 1; - tls->socket_conf->tls = tlscf; + tls->socket_conf->tls = tlscf; } else { tlscf = tls->socket_conf->tls; @@ -3018,17 +2943,17 @@ nxt_router_tls_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } bundle->chain_file = msg->fd[0]; - bundle->next = tlscf->bundle; - tlscf->bundle = bundle; + bundle->next = tlscf->bundle; + tlscf->bundle = bundle; ret = task->thread->runtime->tls->server_init(task, mp, tls->tls_init, - tls->last); + tls->last); if (nxt_slow_path(ret != NXT_OK)) { goto fail; } nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_router_conf_apply, task, tmcf, NULL); + nxt_router_conf_apply, task, tmcf, NULL); return; fail: @@ -3040,17 +2965,16 @@ nxt_router_tls_rpc_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, static void -nxt_router_app_rpc_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_app_t *app) -{ +nxt_router_app_rpc_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_app_t *app) { size_t size; uint32_t stream; nxt_fd_t port_fd, queue_fd; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *router_port, *dport; - nxt_runtime_t *rt; - nxt_app_rpc_t *rpc; + nxt_buf_t *b; + nxt_port_t *router_port, *dport; + nxt_runtime_t *rt; + nxt_app_rpc_t *rpc; rt = task->thread->runtime; @@ -3074,35 +2998,34 @@ nxt_router_app_rpc_create(nxt_task_t *task, dport = rt->port_by_type[NXT_PROCESS_MAIN]; - port_fd = app->shared_port->pair[0]; + port_fd = app->shared_port->pair[0]; queue_fd = app->shared_port->queue_fd; } else { nxt_debug(task, "app '%V' prefork", &app->name); - b = NULL; - port_fd = -1; + b = NULL; + port_fd = -1; queue_fd = -1; } router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; rpc = nxt_port_rpc_register_handler_ex(task, router_port, - nxt_router_app_prefork_ready, - nxt_router_app_prefork_error, - sizeof(nxt_app_rpc_t)); + nxt_router_app_prefork_ready, nxt_router_app_prefork_error, + sizeof(nxt_app_rpc_t)); if (nxt_slow_path(rpc == NULL)) { goto fail; } - rpc->app = app; + rpc->app = app; rpc->temp_conf = tmcf; - rpc->proto = (b != NULL); + rpc->proto = (b != NULL); stream = nxt_port_rpc_ex_stream(rpc); ret = nxt_port_socket_write2(task, dport, NXT_PORT_MSG_START_PROCESS, - port_fd, queue_fd, stream, router_port->id, b); + port_fd, queue_fd, stream, router_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, router_port, stream); goto fail; @@ -3121,15 +3044,13 @@ nxt_router_app_rpc_create(nxt_task_t *task, nxt_router_conf_error(task, tmcf); } - static void nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_app_t *app; - nxt_port_t *port; - nxt_app_rpc_t *rpc; - nxt_event_engine_t *engine; + void *data) { + nxt_app_t *app; + nxt_port_t *port; + nxt_app_rpc_t *rpc; + nxt_event_engine_t *engine; rpc = data; app = rpc->app; @@ -3146,7 +3067,7 @@ nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_port_inc_use(port); app->proto_port = port; - port->app = app; + port->app = app; nxt_router_app_rpc_create(task, rpc->temp_conf, app); @@ -3155,7 +3076,7 @@ nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_assert(port->type == NXT_PROCESS_APP); - port->app = app; + port->app = app; port->main_app_port = port; app->pending_processes--; @@ -3167,8 +3088,8 @@ nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_queue_insert_tail(&app->ports, &port->app_link); nxt_queue_insert_tail(&app->spare_ports, &port->idle_link); - nxt_debug(task, "app '%V' move new port %PI:%d to spare_ports", - &app->name, port->pid, port->id); + nxt_debug(task, "app '%V' move new port %PI:%d to spare_ports", &app->name, + port->pid, port->id); nxt_port_hash_add(&app->port_hash, port); app->port_hash_count++; @@ -3179,30 +3100,28 @@ nxt_router_app_prefork_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_port_socket_write(task, port, NXT_PORT_MSG_PORT_ACK, -1, 0, 0, NULL); - nxt_work_queue_add(&engine->fast_work_queue, - nxt_router_conf_apply, task, rpc->temp_conf, NULL); + nxt_work_queue_add(&engine->fast_work_queue, nxt_router_conf_apply, task, + rpc->temp_conf, NULL); } - static void nxt_router_app_prefork_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_app_t *app; - nxt_app_rpc_t *rpc; - nxt_router_temp_conf_t *tmcf; + void *data) { + nxt_app_t *app; + nxt_app_rpc_t *rpc; + nxt_router_temp_conf_t *tmcf; - rpc = data; - app = rpc->app; + rpc = data; + app = rpc->app; tmcf = rpc->temp_conf; if (rpc->proto) { nxt_log(task, NXT_LOG_WARN, "failed to start prototype \"%V\"", - &app->name); + &app->name); } else { nxt_log(task, NXT_LOG_WARN, "failed to start application \"%V\"", - &app->name); + &app->name); app->pending_processes--; } @@ -3210,20 +3129,18 @@ nxt_router_app_prefork_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_router_conf_error(task, tmcf); } - static nxt_int_t nxt_router_engines_create(nxt_task_t *task, nxt_router_t *router, - nxt_router_temp_conf_t *tmcf, const nxt_event_interface_t *interface) -{ + nxt_router_temp_conf_t *tmcf, const nxt_event_interface_t *interface) { nxt_int_t ret; nxt_uint_t n, threads; - nxt_queue_link_t *qlk; - nxt_router_engine_conf_t *recf; + nxt_queue_link_t *qlk; + nxt_router_engine_conf_t *recf; threads = tmcf->router_conf->threads; tmcf->engines = nxt_array_create(tmcf->mem_pool, threads, - sizeof(nxt_router_engine_conf_t)); + sizeof(nxt_router_engine_conf_t)); if (nxt_slow_path(tmcf->engines == NULL)) { return NXT_ERROR; } @@ -3231,9 +3148,7 @@ nxt_router_engines_create(nxt_task_t *task, nxt_router_t *router, n = 0; for (qlk = nxt_queue_first(&router->engines); - qlk != nxt_queue_tail(&router->engines); - qlk = nxt_queue_next(qlk)) - { + qlk != nxt_queue_tail(&router->engines); qlk = nxt_queue_next(qlk)) { recf = nxt_array_zero_add(tmcf->engines); if (nxt_slow_path(recf == NULL)) { return NXT_ERROR; @@ -3243,11 +3158,11 @@ nxt_router_engines_create(nxt_task_t *task, nxt_router_t *router, if (n < threads) { recf->action = NXT_ROUTER_ENGINE_KEEP; - ret = nxt_router_engine_conf_update(tmcf, recf); + ret = nxt_router_engine_conf_update(tmcf, recf); } else { recf->action = NXT_ROUTER_ENGINE_DELETE; - ret = nxt_router_engine_conf_delete(tmcf, recf); + ret = nxt_router_engine_conf_delete(tmcf, recf); } if (nxt_slow_path(ret != NXT_OK)) { @@ -3283,21 +3198,19 @@ nxt_router_engines_create(nxt_task_t *task, nxt_router_t *router, return NXT_OK; } - static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf) -{ - nxt_int_t ret; + nxt_router_engine_conf_t *recf) { + nxt_int_t ret; ret = nxt_router_engine_joints_create(tmcf, recf, &creating_sockets, - nxt_router_listen_socket_create); + nxt_router_listen_socket_create); if (nxt_slow_path(ret != NXT_OK)) { return ret; } ret = nxt_router_engine_joints_create(tmcf, recf, &updating_sockets, - nxt_router_listen_socket_create); + nxt_router_listen_socket_create); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -3305,21 +3218,19 @@ nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf, return ret; } - static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf) -{ - nxt_int_t ret; + nxt_router_engine_conf_t *recf) { + nxt_int_t ret; ret = nxt_router_engine_joints_create(tmcf, recf, &creating_sockets, - nxt_router_listen_socket_create); + nxt_router_listen_socket_create); if (nxt_slow_path(ret != NXT_OK)) { return ret; } ret = nxt_router_engine_joints_create(tmcf, recf, &updating_sockets, - nxt_router_listen_socket_update); + nxt_router_listen_socket_update); if (nxt_slow_path(ret != NXT_OK)) { return ret; } @@ -3332,12 +3243,10 @@ nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf, return ret; } - static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf) -{ - nxt_int_t ret; + nxt_router_engine_conf_t *recf) { + nxt_int_t ret; ret = nxt_router_engine_quit(tmcf, recf); if (nxt_slow_path(ret != NXT_OK)) { @@ -3352,40 +3261,36 @@ nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf, return nxt_router_engine_joints_delete(tmcf, recf, &deleting_sockets); } - static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf, nxt_router_engine_conf_t *recf, nxt_queue_t *sockets, - nxt_work_handler_t handler) -{ + nxt_work_handler_t handler) { nxt_int_t ret; - nxt_joint_job_t *job; - nxt_queue_link_t *qlk; - nxt_socket_conf_t *skcf; - nxt_socket_conf_joint_t *joint; + nxt_joint_job_t *job; + nxt_queue_link_t *qlk; + nxt_socket_conf_t *skcf; + nxt_socket_conf_joint_t *joint; - for (qlk = nxt_queue_first(sockets); - qlk != nxt_queue_tail(sockets); - qlk = nxt_queue_next(qlk)) - { + for (qlk = nxt_queue_first(sockets); qlk != nxt_queue_tail(sockets); + qlk = nxt_queue_next(qlk)) { job = nxt_mp_get(tmcf->mem_pool, sizeof(nxt_joint_job_t)); if (nxt_slow_path(job == NULL)) { return NXT_ERROR; } job->work.next = recf->jobs; - recf->jobs = &job->work; + recf->jobs = &job->work; - job->task = tmcf->engine->task; + job->task = tmcf->engine->task; job->work.handler = handler; - job->work.task = &job->task; - job->work.obj = job; - job->tmcf = tmcf; + job->work.task = &job->task; + job->work.obj = job; + job->tmcf = tmcf; tmcf->count++; joint = nxt_mp_alloc(tmcf->router_conf->mem_pool, - sizeof(nxt_socket_conf_joint_t)); + sizeof(nxt_socket_conf_joint_t)); if (nxt_slow_path(joint == NULL)) { return NXT_ERROR; } @@ -3409,12 +3314,10 @@ nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf) -{ - nxt_joint_job_t *job; + nxt_router_engine_conf_t *recf) { + nxt_joint_job_t *job; job = nxt_mp_get(tmcf->mem_pool, sizeof(nxt_joint_job_t)); if (nxt_slow_path(job == NULL)) { @@ -3422,44 +3325,40 @@ nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf, } job->work.next = recf->jobs; - recf->jobs = &job->work; + recf->jobs = &job->work; - job->task = tmcf->engine->task; + job->task = tmcf->engine->task; job->work.handler = nxt_router_worker_thread_quit; - job->work.task = &job->task; - job->work.obj = NULL; - job->work.data = NULL; - job->tmcf = NULL; + job->work.task = &job->task; + job->work.obj = NULL; + job->work.data = NULL; + job->tmcf = NULL; return NXT_OK; } - static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf, - nxt_router_engine_conf_t *recf, nxt_queue_t *sockets) -{ - nxt_joint_job_t *job; - nxt_queue_link_t *qlk; + nxt_router_engine_conf_t *recf, nxt_queue_t *sockets) { + nxt_joint_job_t *job; + nxt_queue_link_t *qlk; - for (qlk = nxt_queue_first(sockets); - qlk != nxt_queue_tail(sockets); - qlk = nxt_queue_next(qlk)) - { + for (qlk = nxt_queue_first(sockets); qlk != nxt_queue_tail(sockets); + qlk = nxt_queue_next(qlk)) { job = nxt_mp_get(tmcf->mem_pool, sizeof(nxt_joint_job_t)); if (nxt_slow_path(job == NULL)) { return NXT_ERROR; } job->work.next = recf->jobs; - recf->jobs = &job->work; + recf->jobs = &job->work; - job->task = tmcf->engine->task; + job->task = tmcf->engine->task; job->work.handler = nxt_router_listen_socket_delete; - job->work.task = &job->task; - job->work.obj = job; - job->work.data = nxt_queue_link_data(qlk, nxt_socket_conf_t, link); - job->tmcf = tmcf; + job->work.task = &job->task; + job->work.obj = job; + job->work.data = nxt_queue_link_data(qlk, nxt_socket_conf_t, link); + job->tmcf = tmcf; tmcf->count++; } @@ -3467,16 +3366,14 @@ nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt, - nxt_router_temp_conf_t *tmcf) -{ + nxt_router_temp_conf_t *tmcf) { nxt_int_t ret; nxt_uint_t i, threads; - nxt_router_engine_conf_t *recf; + nxt_router_engine_conf_t *recf; - recf = tmcf->engines->elts; + recf = tmcf->engines->elts; threads = tmcf->router_conf->threads; for (i = tmcf->new_threads; i < threads; i++) { @@ -3489,14 +3386,12 @@ nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt, return NXT_OK; } - static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt, - nxt_event_engine_t *engine) -{ - nxt_int_t ret; - nxt_thread_link_t *link; - nxt_thread_handle_t handle; + nxt_event_engine_t *engine) { + nxt_int_t ret; + nxt_thread_link_t *link; + nxt_thread_handle_t handle; link = nxt_zalloc(sizeof(nxt_thread_link_t)); @@ -3504,11 +3399,11 @@ nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt, return NXT_ERROR; } - link->start = nxt_router_thread_start; - link->engine = engine; + link->start = nxt_router_thread_start; + link->engine = engine; link->work.handler = nxt_router_thread_exit_handler; - link->work.task = task; - link->work.data = link; + link->work.task = task; + link->work.data = link; nxt_queue_insert_tail(&rt->engines, &engine->link); @@ -3521,30 +3416,25 @@ nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt, return ret; } - static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router, - nxt_router_temp_conf_t *tmcf) -{ - nxt_app_t *app; + nxt_router_temp_conf_t *tmcf) { + nxt_app_t *app; nxt_queue_each(app, &router->apps, nxt_app_t, link) { - nxt_router_app_unlink(task, app); - - } nxt_queue_loop; + } + nxt_queue_loop; nxt_queue_add(&router->apps, &tmcf->previous); nxt_queue_add(&router->apps, &tmcf->apps); } - static void -nxt_router_engines_post(nxt_router_t *router, nxt_router_temp_conf_t *tmcf) -{ +nxt_router_engines_post(nxt_router_t *router, nxt_router_temp_conf_t *tmcf) { nxt_uint_t n; - nxt_event_engine_t *engine; - nxt_router_engine_conf_t *recf; + nxt_event_engine_t *engine; + nxt_router_engine_conf_t *recf; recf = tmcf->engines->elts; @@ -3552,7 +3442,6 @@ nxt_router_engines_post(nxt_router_t *router, nxt_router_temp_conf_t *tmcf) engine = recf->engine; switch (recf->action) { - case NXT_ROUTER_ENGINE_KEEP: break; @@ -3571,22 +3460,19 @@ nxt_router_engines_post(nxt_router_t *router, nxt_router_temp_conf_t *tmcf) } } - static void -nxt_router_engine_post(nxt_event_engine_t *engine, nxt_work_t *jobs) -{ - nxt_work_t *work, *next; +nxt_router_engine_post(nxt_event_engine_t *engine, nxt_work_t *jobs) { + nxt_work_t *work, *next; for (work = jobs; work != NULL; work = next) { - next = work->next; + next = work->next; work->next = NULL; nxt_event_engine_post(engine, work); } } - -static nxt_port_handlers_t nxt_router_app_port_handlers = { +static nxt_port_handlers_t nxt_router_app_port_handlers = { .rpc_error = nxt_port_rpc_handler, .mmap = nxt_port_mmap_handler, .data = nxt_port_rpc_handler, @@ -3594,21 +3480,19 @@ static nxt_port_handlers_t nxt_router_app_port_handlers = { .req_headers_ack = nxt_port_rpc_handler, }; - static void -nxt_router_thread_start(void *data) -{ +nxt_router_thread_start(void *data) { nxt_int_t ret; - nxt_port_t *port; - nxt_task_t *task; - nxt_work_t *work; - nxt_thread_t *thread; - nxt_thread_link_t *link; - nxt_event_engine_t *engine; - - link = data; + nxt_port_t *port; + nxt_task_t *task; + nxt_work_t *work; + nxt_thread_t *thread; + nxt_thread_link_t *link; + nxt_event_engine_t *engine; + + link = data; engine = link->engine; - task = &engine->task; + task = &engine->task; thread = nxt_thread(); @@ -3618,9 +3502,9 @@ nxt_router_thread_start(void *data) thread->runtime = engine->task.thread->runtime; engine->task.thread = thread; - engine->task.log = thread->log; - thread->engine = engine; - thread->task = &engine->task; + engine->task.log = thread->log; + thread->engine = engine; + thread->task = &engine->task; #if 0 thread->fiber = &engine->fibers->fiber; #endif @@ -3631,7 +3515,7 @@ nxt_router_thread_start(void *data) } port = nxt_port_new(task, nxt_port_get_next_id(), nxt_pid, - NXT_PROCESS_ROUTER); + NXT_PROCESS_ROUTER); if (nxt_slow_path(port == NULL)) { return; } @@ -3658,24 +3542,22 @@ nxt_router_thread_start(void *data) } work->handler = nxt_router_rt_add_port; - work->task = link->work.task; - work->obj = work; - work->data = port; + work->task = link->work.task; + work->obj = work; + work->data = port; nxt_event_engine_post(link->work.task->thread->engine, work); nxt_event_engine_start(engine); } - static void -nxt_router_rt_add_port(nxt_task_t *task, void *obj, void *data) -{ +nxt_router_rt_add_port(nxt_task_t *task, void *obj, void *data) { nxt_int_t res; - nxt_port_t *port; - nxt_runtime_t *rt; + nxt_port_t *port; + nxt_runtime_t *rt; - rt = task->thread->runtime; + rt = task->thread->runtime; port = data; nxt_free(obj); @@ -3687,24 +3569,22 @@ nxt_router_rt_add_port(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_router_listen_socket_create(nxt_task_t *task, void *obj, void *data) -{ - nxt_joint_job_t *job; - nxt_socket_conf_t *skcf; - nxt_listen_event_t *lev; - nxt_listen_socket_t *ls; - nxt_thread_spinlock_t *lock; - nxt_socket_conf_joint_t *joint; - - job = obj; +nxt_router_listen_socket_create(nxt_task_t *task, void *obj, void *data) { + nxt_joint_job_t *job; + nxt_socket_conf_t *skcf; + nxt_listen_event_t *lev; + nxt_listen_socket_t *ls; + nxt_thread_spinlock_t *lock; + nxt_socket_conf_joint_t *joint; + + job = obj; joint = data; nxt_queue_insert_tail(&task->thread->engine->joints, &joint->link); skcf = joint->socket_conf; - ls = skcf->listen; + ls = skcf->listen; lev = nxt_listen_event(task, ls); if (nxt_slow_path(lev == NULL)) { @@ -3720,27 +3600,23 @@ nxt_router_listen_socket_create(nxt_task_t *task, void *obj, void *data) ls->count++; nxt_thread_spin_unlock(lock); - job->work.next = NULL; + job->work.next = NULL; job->work.handler = nxt_router_conf_wait; nxt_event_engine_post(job->tmcf->engine, &job->work); } - nxt_inline nxt_listen_event_t * nxt_router_listen_event(nxt_queue_t *listen_connections, - nxt_socket_conf_t *skcf) -{ + nxt_socket_conf_t *skcf) { nxt_socket_t fd; - nxt_queue_link_t *qlk; - nxt_listen_event_t *lev; + nxt_queue_link_t *qlk; + nxt_listen_event_t *lev; fd = skcf->listen->socket; for (qlk = nxt_queue_first(listen_connections); - qlk != nxt_queue_tail(listen_connections); - qlk = nxt_queue_next(qlk)) - { + qlk != nxt_queue_tail(listen_connections); qlk = nxt_queue_next(qlk)) { lev = nxt_queue_link_data(qlk, nxt_listen_event_t, link); if (fd == lev->socket.fd) { @@ -3751,16 +3627,14 @@ nxt_router_listen_event(nxt_queue_t *listen_connections, return NULL; } - static void -nxt_router_listen_socket_update(nxt_task_t *task, void *obj, void *data) -{ - nxt_joint_job_t *job; - nxt_event_engine_t *engine; - nxt_listen_event_t *lev; - nxt_socket_conf_joint_t *joint, *old; +nxt_router_listen_socket_update(nxt_task_t *task, void *obj, void *data) { + nxt_joint_job_t *job; + nxt_event_engine_t *engine; + nxt_listen_event_t *lev; + nxt_socket_conf_joint_t *joint, *old; - job = obj; + job = obj; joint = data; engine = task->thread->engine; @@ -3768,13 +3642,13 @@ nxt_router_listen_socket_update(nxt_task_t *task, void *obj, void *data) nxt_queue_insert_tail(&engine->joints, &joint->link); lev = nxt_router_listen_event(&engine->listen_connections, - joint->socket_conf); + joint->socket_conf); - old = lev->socket.data; + old = lev->socket.data; lev->socket.data = joint; - lev->listen = joint->socket_conf->listen; + lev->listen = joint->socket_conf->listen; - job->work.next = NULL; + job->work.next = NULL; job->work.handler = nxt_router_conf_wait; nxt_event_engine_post(job->tmcf->engine, &job->work); @@ -3787,14 +3661,12 @@ nxt_router_listen_socket_update(nxt_task_t *task, void *obj, void *data) nxt_router_conf_release(&engine->task, old); } - static void -nxt_router_listen_socket_delete(nxt_task_t *task, void *obj, void *data) -{ - nxt_socket_conf_t *skcf; - nxt_listen_event_t *lev; - nxt_event_engine_t *engine; - nxt_socket_conf_joint_t *joint; +nxt_router_listen_socket_delete(nxt_task_t *task, void *obj, void *data) { + nxt_socket_conf_t *skcf; + nxt_listen_event_t *lev; + nxt_event_engine_t *engine; + nxt_socket_conf_joint_t *joint; skcf = data; @@ -3805,22 +3677,20 @@ nxt_router_listen_socket_delete(nxt_task_t *task, void *obj, void *data) nxt_fd_event_delete(engine, &lev->socket); nxt_debug(task, "engine %p: listen socket delete: %d", engine, - lev->socket.fd); + lev->socket.fd); - joint = lev->socket.data; + joint = lev->socket.data; joint->close_job = obj; - lev->timer.handler = nxt_router_listen_socket_close; + lev->timer.handler = nxt_router_listen_socket_close; lev->timer.work_queue = &engine->fast_work_queue; nxt_timer_add(engine, &lev->timer, 0); } - static void -nxt_router_worker_thread_quit(nxt_task_t *task, void *obj, void *data) -{ - nxt_event_engine_t *engine; +nxt_router_worker_thread_quit(nxt_task_t *task, void *obj, void *data) { + nxt_event_engine_t *engine; nxt_debug(task, "router worker thread quit"); @@ -3833,24 +3703,22 @@ nxt_router_worker_thread_quit(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_router_listen_socket_close(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_joint_job_t *job; - nxt_listen_event_t *lev; - nxt_socket_conf_joint_t *joint; +nxt_router_listen_socket_close(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_joint_job_t *job; + nxt_listen_event_t *lev; + nxt_socket_conf_joint_t *joint; timer = obj; - lev = nxt_timer_data(timer, nxt_listen_event_t, timer); + lev = nxt_timer_data(timer, nxt_listen_event_t, timer); nxt_debug(task, "engine %p: listen socket close: %d", task->thread->engine, - lev->socket.fd); + lev->socket.fd); nxt_queue_remove(&lev->link); - joint = lev->socket.data; + joint = lev->socket.data; lev->socket.data = NULL; /* 'task' refers to lev->task and we cannot use after nxt_free() */ @@ -3858,8 +3726,8 @@ nxt_router_listen_socket_close(nxt_task_t *task, void *obj, void *data) nxt_router_listen_socket_release(task, joint->socket_conf); - job = joint->close_job; - job->work.next = NULL; + job = joint->close_job; + job->work.next = NULL; job->work.handler = nxt_router_conf_wait; nxt_event_engine_post(job->tmcf->engine, &job->work); @@ -3867,27 +3735,25 @@ nxt_router_listen_socket_close(nxt_task_t *task, void *obj, void *data) nxt_router_listen_event_release(task, lev, joint); } - static void -nxt_router_listen_socket_release(nxt_task_t *task, nxt_socket_conf_t *skcf) -{ +nxt_router_listen_socket_release(nxt_task_t *task, nxt_socket_conf_t *skcf) { #if (NXT_HAVE_UNIX_DOMAIN) - size_t size; - nxt_buf_t *b; - nxt_port_t *main_port; - nxt_runtime_t *rt; - nxt_sockaddr_t *sa; + size_t size; + nxt_buf_t *b; + nxt_port_t *main_port; + nxt_runtime_t *rt; + nxt_sockaddr_t *sa; #endif - nxt_listen_socket_t *ls; - nxt_thread_spinlock_t *lock; + nxt_listen_socket_t *ls; + nxt_thread_spinlock_t *lock; - ls = skcf->listen; + ls = skcf->listen; lock = &skcf->router_conf->router->lock; nxt_thread_spin_lock(lock); nxt_debug(task, "engine %p: listen socket release: ls->count %D", - task->thread->engine, ls->count); + task->thread->engine, ls->count); if (--ls->count != 0) { ls = NULL; @@ -3904,8 +3770,7 @@ nxt_router_listen_socket_release(nxt_task_t *task, nxt_socket_conf_t *skcf) #if (NXT_HAVE_UNIX_DOMAIN) sa = ls->sockaddr; if (sa->u.sockaddr.sa_family != AF_UNIX - || sa->u.sockaddr_un.sun_path[0] == '\0') - { + || sa->u.sockaddr_un.sun_path[0] == '\0') { goto out_free_ls; } @@ -3918,23 +3783,21 @@ nxt_router_listen_socket_release(nxt_task_t *task, nxt_socket_conf_t *skcf) b->mem.free = nxt_cpymem(b->mem.free, ls->sockaddr, size); - rt = task->thread->runtime; + rt = task->thread->runtime; main_port = rt->port_by_type[NXT_PROCESS_MAIN]; (void) nxt_port_socket_write(task, main_port, NXT_PORT_MSG_SOCKET_UNLINK, - -1, 0, 0, b); + -1, 0, 0, b); out_free_ls: #endif nxt_free(ls); } - void nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev, - nxt_socket_conf_joint_t *joint) -{ - nxt_event_engine_t *engine; + nxt_socket_conf_joint_t *joint) { + nxt_event_engine_t *engine; nxt_debug(task, "listen event count: %D", lev->count); @@ -3959,13 +3822,11 @@ nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev, } } - void -nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint) -{ - nxt_socket_conf_t *skcf; - nxt_router_conf_t *rtcf; - nxt_thread_spinlock_t *lock; +nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint) { + nxt_socket_conf_t *skcf; + nxt_router_conf_t *rtcf; + nxt_thread_spinlock_t *lock; nxt_debug(task, "conf joint %p count: %D", joint, joint->count); @@ -3986,8 +3847,8 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint) nxt_thread_spin_lock(lock); - nxt_debug(task, "conf skcf %p: %D, rtcf %p: %D", skcf, skcf->count, - rtcf, rtcf->count); + nxt_debug(task, "conf skcf %p: %D, rtcf %p: %D", skcf, skcf->count, rtcf, + rtcf->count); if (--skcf->count != 0) { skcf = NULL; @@ -4026,17 +3887,15 @@ nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint) } } - static void -nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_port_t *port; - nxt_thread_link_t *link; - nxt_event_engine_t *engine; - nxt_thread_handle_t handle; +nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, void *data) { + nxt_port_t *port; + nxt_thread_link_t *link; + nxt_event_engine_t *engine; + nxt_thread_handle_t handle; handle = (nxt_thread_handle_t) (uintptr_t) obj; - link = data; + link = data; nxt_thread_wait(handle); @@ -4060,21 +3919,19 @@ nxt_router_thread_exit_handler(nxt_task_t *task, void *obj, void *data) nxt_free(link); } - static void nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ + void *data) { size_t b_size, count; nxt_int_t ret; - nxt_app_t *app; - nxt_buf_t *b, *next; - nxt_port_t *app_port; - nxt_unit_field_t *f; - nxt_http_field_t *field; - nxt_http_request_t *r; - nxt_unit_response_t *resp; - nxt_request_rpc_data_t *req_rpc_data; + nxt_app_t *app; + nxt_buf_t *b, *next; + nxt_port_t *app_port; + nxt_unit_field_t *f; + nxt_http_field_t *field; + nxt_http_request_t *r; + nxt_unit_response_t *resp; + nxt_request_rpc_data_t *req_rpc_data; req_rpc_data = data; @@ -4115,7 +3972,7 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } else { if (app->timeout != 0) { r->timer.handler = nxt_router_app_timeout; - r->timer_data = req_rpc_data; + r->timer_data = req_rpc_data; nxt_timer_add(task->thread->engine, &r->timer, app->timeout); } } @@ -4142,12 +3999,12 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } resp = (void *) b->mem.pos; - count = (b_size - sizeof(nxt_unit_response_t)) - / sizeof(nxt_unit_field_t); + count + = (b_size - sizeof(nxt_unit_response_t)) / sizeof(nxt_unit_field_t); if (nxt_slow_path(count < resp->fields_count)) { nxt_alert(task, "response buffer too small for fields count: %D", - resp->fields_count); + resp->fields_count); goto fail; } @@ -4164,14 +4021,14 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, goto fail; } - field->hash = f->hash; - field->skip = 0; + field->hash = f->hash; + field->skip = 0; field->hopbyhop = 0; - field->name_length = f->name_length; + field->name_length = f->name_length; field->value_length = f->value_length; - field->name = nxt_unit_sptr_get(&f->name); - field->value = nxt_unit_sptr_get(&f->value); + field->name = nxt_unit_sptr_get(&f->name); + field->value = nxt_unit_sptr_get(&f->value); ret = nxt_http_field_process(field, &nxt_response_fields_hash, r); if (nxt_slow_path(ret != NXT_OK)) { @@ -4179,9 +4036,8 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } nxt_debug(task, "header%s: %*s: %*s", - (field->skip ? " skipped" : ""), - (size_t) field->name_length, field->name, - (size_t) field->value_length, field->value); + (field->skip ? " skipped" : ""), (size_t) field->name_length, + field->name, (size_t) field->value_length, field->value); if (field->skip) { r->resp.fields->last->nelts--; @@ -4191,7 +4047,7 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, r->status = resp->status; if (resp->piggyback_content_length != 0) { - b->mem.pos = nxt_unit_sptr_get(&resp->piggyback_content); + b->mem.pos = nxt_unit_sptr_get(&resp->piggyback_content); b->mem.free = b->mem.pos + resp->piggyback_content_length; } else { @@ -4199,11 +4055,11 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, } if (nxt_buf_mem_used_size(&b->mem) == 0) { - next = b->next; + next = b->next; b->next = NULL; nxt_work_queue_add(&task->thread->engine->fast_work_queue, - b->completion_handler, task, b, b->parent); + b->completion_handler, task, b, b->parent); b = next; } @@ -4215,8 +4071,7 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_http_request_header_send(task, r, nxt_http_request_send_body, NULL); if (r->websocket_handshake - && r->status == NXT_HTTP_SWITCHING_PROTOCOLS) - { + && r->status == NXT_HTTP_SWITCHING_PROTOCOLS) { app_port = req_rpc_data->app_port; if (nxt_slow_path(app_port == NULL)) { goto fail; @@ -4249,31 +4104,27 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_request_rpc_data_unlink(task, req_rpc_data); } - static void -nxt_router_req_headers_ack_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg, nxt_request_rpc_data_t *req_rpc_data) -{ +nxt_router_req_headers_ack_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, + nxt_request_rpc_data_t *req_rpc_data) { int res; - nxt_app_t *app; - nxt_buf_t *b; + nxt_app_t *app; + nxt_buf_t *b; nxt_bool_t start_process, unlinked; - nxt_port_t *app_port, *main_app_port, *idle_port; - nxt_queue_link_t *idle_lnk; - nxt_http_request_t *r; + nxt_port_t *app_port, *main_app_port, *idle_port; + nxt_queue_link_t *idle_lnk; + nxt_http_request_t *r; - nxt_debug(task, "stream #%uD: got ack from %PI:%d", - req_rpc_data->stream, - msg->port_msg.pid, msg->port_msg.reply_port); + nxt_debug(task, "stream #%uD: got ack from %PI:%d", req_rpc_data->stream, + msg->port_msg.pid, msg->port_msg.reply_port); - nxt_port_rpc_ex_set_peer(task, msg->port, req_rpc_data, - msg->port_msg.pid); + nxt_port_rpc_ex_set_peer(task, msg->port, req_rpc_data, msg->port_msg.pid); app = req_rpc_data->app; - r = req_rpc_data->request; + r = req_rpc_data->request; start_process = 0; - unlinked = 0; + unlinked = 0; nxt_thread_mutex_lock(&app->mutex); @@ -4285,7 +4136,7 @@ nxt_router_req_headers_ack_handler(nxt_task_t *task, } app_port = nxt_port_hash_find(&app->port_hash, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(app_port == NULL)) { nxt_thread_mutex_unlock(&app->mutex); @@ -4303,21 +4154,20 @@ nxt_router_req_headers_ack_handler(nxt_task_t *task, if (nxt_queue_chk_remove(&main_app_port->idle_link)) { app->idle_processes--; - nxt_debug(task, "app '%V' move port %PI:%d out of %s (ack)", - &app->name, main_app_port->pid, main_app_port->id, - (main_app_port->idle_start ? "idle_ports" : "spare_ports")); + nxt_debug(task, "app '%V' move port %PI:%d out of %s (ack)", &app->name, + main_app_port->pid, main_app_port->id, + (main_app_port->idle_start ? "idle_ports" : "spare_ports")); /* Check port was in 'spare_ports' using idle_start field. */ if (main_app_port->idle_start == 0 - && app->idle_processes >= app->spare_processes) - { + && app->idle_processes >= app->spare_processes) { /* * If there is a vacant space in spare ports, * move the last idle to spare_ports. */ nxt_assert(!nxt_queue_is_empty(&app->idle_ports)); - idle_lnk = nxt_queue_last(&app->idle_ports); + idle_lnk = nxt_queue_last(&app->idle_ports); idle_port = nxt_queue_link_data(idle_lnk, nxt_port_t, idle_link); nxt_queue_remove(idle_lnk); @@ -4325,9 +4175,10 @@ nxt_router_req_headers_ack_handler(nxt_task_t *task, idle_port->idle_start = 0; - nxt_debug(task, "app '%V' move port %PI:%d from idle_ports " - "to spare_ports", - &app->name, idle_port->pid, idle_port->id); + nxt_debug(task, + "app '%V' move port %PI:%d from idle_ports " + "to spare_ports", + &app->name, idle_port->pid, idle_port->id); } if (nxt_router_app_can_start(app) && nxt_router_app_need_start(app)) { @@ -4365,16 +4216,15 @@ nxt_router_req_headers_ack_handler(nxt_task_t *task, if (req_rpc_data->msg_info.body_fd != -1 || b != NULL) { nxt_debug(task, "stream #%uD: send body fd %d", req_rpc_data->stream, - req_rpc_data->msg_info.body_fd); + req_rpc_data->msg_info.body_fd); if (req_rpc_data->msg_info.body_fd != -1) { lseek(req_rpc_data->msg_info.body_fd, 0, SEEK_SET); } res = nxt_port_socket_write(task, app_port, NXT_PORT_MSG_REQ_BODY, - req_rpc_data->msg_info.body_fd, - req_rpc_data->stream, - task->thread->engine->port->id, b); + req_rpc_data->msg_info.body_fd, req_rpc_data->stream, + task->thread->engine->port->id, b); if (nxt_slow_path(res != NXT_OK)) { nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); @@ -4383,24 +4233,21 @@ nxt_router_req_headers_ack_handler(nxt_task_t *task, if (app->timeout != 0) { r->timer.handler = nxt_router_app_timeout; - r->timer_data = req_rpc_data; + r->timer_data = req_rpc_data; nxt_timer_add(task->thread->engine, &r->timer, app->timeout); } } - -static const nxt_http_request_state_t nxt_http_request_send_state - nxt_aligned(64) = -{ - .error_handler = nxt_http_request_error_handler, +static const nxt_http_request_state_t nxt_http_request_send_state nxt_aligned( + 64) + = { + .error_handler = nxt_http_request_error_handler, }; - static void -nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data) -{ - nxt_buf_t *out; - nxt_http_request_t *r; +nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data) { + nxt_buf_t *out; + nxt_http_request_t *r; r = obj; @@ -4412,45 +4259,42 @@ nxt_http_request_send_body(nxt_task_t *task, void *obj, void *data) } } - static void nxt_router_response_error_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_request_rpc_data_t *req_rpc_data; + void *data) { + nxt_request_rpc_data_t *req_rpc_data; req_rpc_data = data; req_rpc_data->rpc_cancel = 0; /* TODO cancel message and return if cancelled. */ - // nxt_router_msg_cancel(task, &req_rpc_data->msg_info, req_rpc_data->stream); + // nxt_router_msg_cancel(task, &req_rpc_data->msg_info, + // req_rpc_data->stream); if (req_rpc_data->request != NULL) { nxt_http_request_error(task, req_rpc_data->request, - NXT_HTTP_SERVICE_UNAVAILABLE); + NXT_HTTP_SERVICE_UNAVAILABLE); } nxt_request_rpc_data_unlink(task, req_rpc_data); } - static void nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ + void *data) { uint32_t n; - nxt_app_t *app; + nxt_app_t *app; nxt_bool_t start_process, restarted; - nxt_port_t *port; - nxt_app_joint_t *app_joint; - nxt_app_joint_rpc_t *app_joint_rpc; + nxt_port_t *port; + nxt_app_joint_t *app_joint; + nxt_app_joint_rpc_t *app_joint_rpc; nxt_assert(data != NULL); app_joint_rpc = data; - app_joint = app_joint_rpc->app_joint; - port = msg->u.new_port; + app_joint = app_joint_rpc->app_joint; + port = msg->u.new_port; nxt_assert(app_joint != NULL); nxt_assert(port != NULL); @@ -4476,7 +4320,7 @@ nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_assert(app->proto_port == NULL); nxt_assert(port->type == NXT_PROCESS_PROTOTYPE); - n = app->proto_port_requests; + n = app->proto_port_requests; app->proto_port_requests = 0; if (nxt_slow_path(restarted)) { @@ -4485,10 +4329,10 @@ nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_debug(task, "proto port ready for restarted app, send QUIT"); nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, -1, 0, 0, - NULL); + NULL); } else { - port->app = app; + port->app = app; app->proto_port = port; nxt_thread_mutex_unlock(&app->mutex); @@ -4536,7 +4380,7 @@ nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, return; } - port->app = app; + port->app = app; port->main_app_port = port; app->processes++; @@ -4545,29 +4389,27 @@ nxt_router_app_port_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_thread_mutex_unlock(&app->mutex); - nxt_debug(task, "app '%V' new port ready, pid %PI, %d/%d", - &app->name, port->pid, app->processes, app->pending_processes); + nxt_debug(task, "app '%V' new port ready, pid %PI, %d/%d", &app->name, + port->pid, app->processes, app->pending_processes); nxt_port_socket_write(task, port, NXT_PORT_MSG_PORT_ACK, -1, 0, 0, NULL); nxt_router_app_port_release(task, app, port, NXT_APR_NEW_PORT); } - static void nxt_router_app_port_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_app_t *app; - nxt_app_joint_t *app_joint; - nxt_queue_link_t *link; - nxt_http_request_t *r; - nxt_app_joint_rpc_t *app_joint_rpc; + void *data) { + nxt_app_t *app; + nxt_app_joint_t *app_joint; + nxt_queue_link_t *link; + nxt_http_request_t *r; + nxt_app_joint_rpc_t *app_joint_rpc; nxt_assert(data != NULL); app_joint_rpc = data; - app_joint = app_joint_rpc->app_joint; + app_joint = app_joint_rpc->app_joint; nxt_assert(app_joint != NULL); @@ -4610,8 +4452,7 @@ nxt_router_app_port_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_thread_mutex_lock(&app->mutex); if (app->processes == 0 && app->pending_processes == 0 - && !nxt_queue_is_empty(&app->ack_waiting_req)) - { + && !nxt_queue_is_empty(&app->ack_waiting_req)) { link = nxt_queue_first(&app->ack_waiting_req); nxt_queue_remove(link); @@ -4622,18 +4463,15 @@ nxt_router_app_port_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, } } - nxt_inline nxt_port_t * -nxt_router_app_get_port_for_quit(nxt_task_t *task, nxt_app_t *app) -{ - nxt_port_t *port; +nxt_router_app_get_port_for_quit(nxt_task_t *task, nxt_app_t *app) { + nxt_port_t *port; port = NULL; nxt_thread_mutex_lock(&app->mutex); nxt_queue_each(port, &app->ports, nxt_port_t, app_link) { - /* Caller is responsible to decrease port use count. */ nxt_queue_chk_remove(&port->app_link); @@ -4641,8 +4479,8 @@ nxt_router_app_get_port_for_quit(nxt_task_t *task, nxt_app_t *app) app->idle_processes--; nxt_debug(task, "app '%V' move port %PI:%d out of %s for quit", - &app->name, port->pid, port->id, - (port->idle_start ? "idle_ports" : "spare_ports")); + &app->name, port->pid, port->id, + (port->idle_start ? "idle_ports" : "spare_ports")); } nxt_port_hash_remove(&app->port_hash, port); @@ -4652,24 +4490,21 @@ nxt_router_app_get_port_for_quit(nxt_task_t *task, nxt_app_t *app) app->processes--; break; - - } nxt_queue_loop; + } + nxt_queue_loop; nxt_thread_mutex_unlock(&app->mutex); return port; } - static void -nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i) -{ - int c; +nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i) { + int c; c = nxt_atomic_fetch_add(&app->use_count, i); if (i < 0 && c == -i) { - if (task->thread->engine != app->engine) { nxt_event_engine_post(app->engine, &app->joint->free_app_work); @@ -4679,10 +4514,8 @@ nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i) } } - static void -nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app) -{ +nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app) { nxt_debug(task, "app '%V' %p unlink", &app->name, app); nxt_queue_remove(&app->link); @@ -4690,19 +4523,17 @@ nxt_router_app_unlink(nxt_task_t *task, nxt_app_t *app) nxt_router_app_use(task, app, -1); } - static void nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, - nxt_apr_action_t action) -{ + nxt_apr_action_t action) { int inc_use; uint32_t got_response, dec_requests; nxt_bool_t adjust_idle_timer; - nxt_port_t *main_app_port; + nxt_port_t *main_app_port; nxt_assert(port != NULL); - inc_use = 0; + inc_use = 0; got_response = 0; dec_requests = 0; @@ -4711,11 +4542,11 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, break; case NXT_APR_REQUEST_FAILED: dec_requests = 1; - inc_use = -1; + inc_use = -1; break; case NXT_APR_GOT_RESPONSE: got_response = 1; - inc_use = -1; + inc_use = -1; break; case NXT_APR_UPGRADE: got_response = 1; @@ -4726,8 +4557,7 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, } nxt_debug(task, "app '%V' release port %PI:%d: %d %d", &app->name, - port->pid, port->id, - (int) inc_use, (int) got_response); + port->pid, port->id, (int) inc_use, (int) got_response); if (port->id == NXT_SHARED_PORT_ID) { nxt_thread_mutex_lock(&app->mutex); @@ -4744,7 +4574,7 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, nxt_thread_mutex_lock(&app->mutex); main_app_port->active_requests -= got_response + dec_requests; - app->active_requests -= got_response + dec_requests; + app->active_requests -= got_response + dec_requests; if (main_app_port->pair[1] != -1 && main_app_port->app_link.next == NULL) { nxt_queue_insert_tail(&app->ports, &main_app_port->app_link); @@ -4754,15 +4584,12 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, adjust_idle_timer = 0; - if (main_app_port->pair[1] != -1 - && main_app_port->active_requests == 0 + if (main_app_port->pair[1] != -1 && main_app_port->active_requests == 0 && main_app_port->active_websockets == 0 - && main_app_port->idle_link.next == NULL) - { + && main_app_port->idle_link.next == NULL) { if (app->idle_processes == app->spare_processes - && app->adjust_idle_work.data == NULL) - { - adjust_idle_timer = 1; + && app->adjust_idle_work.data == NULL) { + adjust_idle_timer = 1; app->adjust_idle_work.data = app; app->adjust_idle_work.next = NULL; } @@ -4771,14 +4598,14 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, nxt_queue_insert_tail(&app->spare_ports, &main_app_port->idle_link); nxt_debug(task, "app '%V' move port %PI:%d to spare_ports", - &app->name, main_app_port->pid, main_app_port->id); + &app->name, main_app_port->pid, main_app_port->id); } else { nxt_queue_insert_tail(&app->idle_ports, &main_app_port->idle_link); main_app_port->idle_start = task->thread->engine->timers.now; nxt_debug(task, "app '%V' move port %PI:%d to idle_ports", - &app->name, main_app_port->pid, main_app_port->id); + &app->name, main_app_port->pid, main_app_port->id); } app->idle_processes++; @@ -4794,27 +4621,25 @@ nxt_router_app_port_release(nxt_task_t *task, nxt_app_t *app, nxt_port_t *port, /* ? */ if (main_app_port->pair[1] == -1) { nxt_debug(task, "app '%V' %p port %p already closed (pid %PI dead?)", - &app->name, app, main_app_port, main_app_port->pid); + &app->name, app, main_app_port, main_app_port->pid); goto adjust_use; } nxt_debug(task, "app '%V' %p requests queue is empty, keep the port", - &app->name, app); + &app->name, app); adjust_use: nxt_port_use(task, port, inc_use); } - void -nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) -{ - nxt_app_t *app; +nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) { + nxt_app_t *app; nxt_bool_t unchain, start_process; - nxt_port_t *idle_port; - nxt_queue_link_t *idle_lnk; + nxt_port_t *idle_port; + nxt_queue_link_t *idle_lnk; app = port->app; @@ -4824,12 +4649,12 @@ nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) if (port == app->proto_port) { app->proto_port = NULL; - port->app = NULL; + port->app = NULL; nxt_thread_mutex_unlock(&app->mutex); nxt_debug(task, "app '%V' prototype pid %PI closed", &app->name, - port->pid); + port->pid); nxt_port_use(task, port, -1); @@ -4842,8 +4667,8 @@ nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) if (port->id != 0) { nxt_thread_mutex_unlock(&app->mutex); - nxt_debug(task, "app '%V' port (%PI, %d) closed", &app->name, - port->pid, port->id); + nxt_debug(task, "app '%V' port (%PI, %d) closed", &app->name, port->pid, + port->id); return; } @@ -4854,15 +4679,14 @@ nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) app->idle_processes--; nxt_debug(task, "app '%V' move port %PI:%d out of %s before close", - &app->name, port->pid, port->id, - (port->idle_start ? "idle_ports" : "spare_ports")); + &app->name, port->pid, port->id, + (port->idle_start ? "idle_ports" : "spare_ports")); if (port->idle_start == 0 - && app->idle_processes >= app->spare_processes) - { + && app->idle_processes >= app->spare_processes) { nxt_assert(!nxt_queue_is_empty(&app->idle_ports)); - idle_lnk = nxt_queue_last(&app->idle_ports); + idle_lnk = nxt_queue_last(&app->idle_ports); idle_port = nxt_queue_link_data(idle_lnk, nxt_port_t, idle_link); nxt_queue_remove(idle_lnk); @@ -4870,9 +4694,10 @@ nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) idle_port->idle_start = 0; - nxt_debug(task, "app '%V' move port %PI:%d from idle_ports " - "to spare_ports", - &app->name, idle_port->pid, idle_port->id); + nxt_debug(task, + "app '%V' move port %PI:%d from idle_ports " + "to spare_ports", + &app->name, idle_port->pid, idle_port->id); } } @@ -4899,29 +4724,27 @@ nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port) } } - static void -nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) -{ - nxt_app_t *app; +nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) { + nxt_app_t *app; nxt_bool_t queued; - nxt_port_t *port; + nxt_port_t *port; nxt_msec_t timeout, threshold; - nxt_queue_link_t *lnk; - nxt_event_engine_t *engine; + nxt_queue_link_t *lnk; + nxt_event_engine_t *engine; - app = obj; + app = obj; queued = (data == app); nxt_debug(task, "nxt_router_adjust_idle_timer: app \"%V\", queued %b", - &app->name, queued); + &app->name, queued); engine = task->thread->engine; nxt_assert(app->engine == engine); threshold = engine->timers.now + app->joint->idle_timer.bias; - timeout = 0; + timeout = 0; nxt_thread_mutex_lock(&app->mutex); @@ -4930,21 +4753,18 @@ nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) } nxt_debug(task, "app '%V' idle_processes %d, spare_processes %d", - &app->name, - (int) app->idle_processes, (int) app->spare_processes); + &app->name, (int) app->idle_processes, (int) app->spare_processes); while (app->idle_processes > app->spare_processes) { - nxt_assert(!nxt_queue_is_empty(&app->idle_ports)); - lnk = nxt_queue_first(&app->idle_ports); + lnk = nxt_queue_first(&app->idle_ports); port = nxt_queue_link_data(lnk, nxt_port_t, idle_link); timeout = port->idle_start + app->idle_timeout; nxt_debug(task, "app '%V' pid %PI, start %M, timeout %M, threshold %M", - &app->name, port->pid, - port->idle_start, timeout, threshold); + &app->name, port->pid, port->idle_start, timeout, threshold); if (timeout > threshold) { break; @@ -4954,7 +4774,7 @@ nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) lnk->next = NULL; nxt_debug(task, "app '%V' move port %PI:%d out of idle_ports (timeout)", - &app->name, port->pid, port->id); + &app->name, port->pid, port->id); nxt_queue_chk_remove(&port->app_link); @@ -4967,8 +4787,8 @@ nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) nxt_thread_mutex_unlock(&app->mutex); - nxt_debug(task, "app '%V' send QUIT to idle port %PI", - &app->name, port->pid); + nxt_debug(task, "app '%V' send QUIT to idle port %PI", &app->name, + port->pid); nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, -1, 0, 0, NULL); @@ -4991,14 +4811,12 @@ nxt_router_adjust_idle_timer(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_router_app_idle_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_app_joint_t *app_joint; +nxt_router_app_idle_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_app_joint_t *app_joint; - timer = obj; + timer = obj; app_joint = nxt_container_of(timer, nxt_app_joint_t, idle_timer); if (nxt_fast_path(app_joint->app != NULL)) { @@ -5006,31 +4824,27 @@ nxt_router_app_idle_timeout(nxt_task_t *task, void *obj, void *data) } } - static void -nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_app_joint_t *app_joint; +nxt_router_app_joint_release_handler(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_app_joint_t *app_joint; - timer = obj; + timer = obj; app_joint = nxt_container_of(timer, nxt_app_joint_t, idle_timer); nxt_router_app_joint_use(task, app_joint, -1); } - static void -nxt_router_free_app(nxt_task_t *task, void *obj, void *data) -{ - nxt_app_t *app; - nxt_port_t *port, *proto_port; - nxt_app_joint_t *app_joint; +nxt_router_free_app(nxt_task_t *task, void *obj, void *data) { + nxt_app_t *app; + nxt_port_t *port, *proto_port; + nxt_app_joint_t *app_joint; app_joint = obj; - app = app_joint->app; + app = app_joint->app; - for ( ;; ) { + for (;;) { port = nxt_router_app_get_port_for_quit(task, app); if (port == NULL) { break; @@ -5041,7 +4855,7 @@ nxt_router_free_app(nxt_task_t *task, void *obj, void *data) nxt_thread_mutex_lock(&app->mutex); - for ( ;; ) { + for (;;) { port = nxt_port_hash_retrieve(&app->port_hash); if (port == NULL) { break; @@ -5060,7 +4874,7 @@ nxt_router_free_app(nxt_task_t *task, void *obj, void *data) if (proto_port != NULL) { nxt_debug(task, "send QUIT to prototype '%V' pid %PI", &app->name, - proto_port->pid); + proto_port->pid); app->proto_port = NULL; proto_port->app = NULL; @@ -5069,8 +4883,8 @@ nxt_router_free_app(nxt_task_t *task, void *obj, void *data) nxt_thread_mutex_unlock(&app->mutex); if (proto_port != NULL) { - nxt_port_socket_write(task, proto_port, NXT_PORT_MSG_QUIT, - -1, 0, 0, NULL); + nxt_port_socket_write(task, proto_port, NXT_PORT_MSG_QUIT, -1, 0, 0, + NULL); nxt_port_close(task, proto_port); @@ -5112,14 +4926,12 @@ nxt_router_free_app(nxt_task_t *task, void *obj, void *data) } } - static void nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app, - nxt_request_rpc_data_t *req_rpc_data) -{ + nxt_request_rpc_data_t *req_rpc_data) { nxt_bool_t start_process; - nxt_port_t *port; - nxt_http_request_t *r; + nxt_port_t *port; + nxt_http_request_t *r; start_process = 0; @@ -5151,7 +4963,7 @@ nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app, */ nxt_mp_retain(r->mem_pool); - req_rpc_data->app_port = port; + req_rpc_data->app_port = port; req_rpc_data->apr_action = NXT_APR_REQUEST_FAILED; if (start_process) { @@ -5159,24 +4971,21 @@ nxt_router_app_port_get(nxt_task_t *task, nxt_app_t *app, } } - void nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ - nxt_event_engine_t *engine; - nxt_http_app_conf_t *conf; - nxt_request_rpc_data_t *req_rpc_data; + nxt_http_action_t *action) { + nxt_event_engine_t *engine; + nxt_http_app_conf_t *conf; + nxt_request_rpc_data_t *req_rpc_data; - conf = action->u.conf; + conf = action->u.conf; engine = task->thread->engine; r->app_target = conf->target; req_rpc_data = nxt_port_rpc_register_handler_ex(task, engine->port, - nxt_router_response_ready_handler, - nxt_router_response_error_handler, - sizeof(nxt_request_rpc_data_t)); + nxt_router_response_ready_handler, nxt_router_response_error_handler, + sizeof(nxt_request_rpc_data_t)); if (nxt_slow_path(req_rpc_data == NULL)) { nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); return; @@ -5192,25 +5001,25 @@ nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r, */ nxt_mp_retain(r->mem_pool); - r->timer.task = &engine->task; + r->timer.task = &engine->task; r->timer.work_queue = &engine->fast_work_queue; - r->timer.log = engine->task.log; - r->timer.bias = NXT_TIMER_DEFAULT_BIAS; + r->timer.log = engine->task.log; + r->timer.bias = NXT_TIMER_DEFAULT_BIAS; - r->engine = engine; + r->engine = engine; r->err_work.handler = nxt_router_http_request_error; - r->err_work.task = task; - r->err_work.obj = r; + r->err_work.task = task; + r->err_work.obj = r; - req_rpc_data->stream = nxt_port_rpc_ex_stream(req_rpc_data); - req_rpc_data->app = conf->app; + req_rpc_data->stream = nxt_port_rpc_ex_stream(req_rpc_data); + req_rpc_data->app = conf->app; req_rpc_data->msg_info.body_fd = -1; - req_rpc_data->rpc_cancel = 1; + req_rpc_data->rpc_cancel = 1; nxt_router_app_use(task, conf->app, 1); req_rpc_data->request = r; - r->req_rpc_data = req_rpc_data; + r->req_rpc_data = req_rpc_data; if (r->last != NULL) { r->last->completion_handler = nxt_router_http_request_done; @@ -5220,11 +5029,9 @@ nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r, nxt_router_app_prepare_request(task, req_rpc_data); } - static void -nxt_router_http_request_error(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; +nxt_router_http_request_error(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; r = obj; @@ -5239,11 +5046,9 @@ nxt_router_http_request_error(nxt_task_t *task, void *obj, void *data) nxt_mp_release(r->mem_pool); } - static void -nxt_router_http_request_done(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; +nxt_router_http_request_done(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; r = data; @@ -5256,23 +5061,21 @@ nxt_router_http_request_done(nxt_task_t *task, void *obj, void *data) nxt_http_request_close_handler(task, r, r->proto.any); } - static void nxt_router_app_prepare_request(nxt_task_t *task, - nxt_request_rpc_data_t *req_rpc_data) -{ - nxt_app_t *app; - nxt_buf_t *buf, *body; - nxt_int_t res; - nxt_port_t *port, *reply_port; + nxt_request_rpc_data_t *req_rpc_data) { + nxt_app_t *app; + nxt_buf_t *buf, *body; + nxt_int_t res; + nxt_port_t *port, *reply_port; + + int notify; - int notify; struct { - nxt_port_msg_t pm; - nxt_port_mmap_msg_t mm; + nxt_port_msg_t pm; + nxt_port_mmap_msg_t mm; } msg; - app = req_rpc_data->app; nxt_assert(app != NULL); @@ -5285,20 +5088,19 @@ nxt_router_app_prepare_request(nxt_task_t *task, reply_port = task->thread->engine->port; buf = nxt_router_prepare_msg(task, req_rpc_data->request, app, - nxt_app_msg_prefix[app->type]); + nxt_app_msg_prefix[app->type]); if (nxt_slow_path(buf == NULL)) { nxt_alert(task, "stream #%uD, app '%V': failed to prepare app message", - req_rpc_data->stream, &app->name); + req_rpc_data->stream, &app->name); nxt_http_request_error(task, req_rpc_data->request, - NXT_HTTP_INTERNAL_SERVER_ERROR); + NXT_HTTP_INTERNAL_SERVER_ERROR); return; } nxt_debug(task, "about to send %O bytes buffer to app process port %d", - nxt_buf_used_size(buf), - port->socket.fd); + nxt_buf_used_size(buf), port->socket.fd); req_rpc_data->msg_info.buf = buf; @@ -5313,60 +5115,54 @@ nxt_router_app_prepare_request(nxt_task_t *task, req_rpc_data->msg_info.body_fd = -1; } - msg.pm.stream = req_rpc_data->stream; - msg.pm.pid = reply_port->pid; + msg.pm.stream = req_rpc_data->stream; + msg.pm.pid = reply_port->pid; msg.pm.reply_port = reply_port->id; - msg.pm.type = NXT_PORT_MSG_REQ_HEADERS; - msg.pm.last = 0; - msg.pm.mmap = 1; - msg.pm.nf = 0; - msg.pm.mf = 0; + msg.pm.type = NXT_PORT_MSG_REQ_HEADERS; + msg.pm.last = 0; + msg.pm.mmap = 1; + msg.pm.nf = 0; + msg.pm.mf = 0; nxt_port_mmap_handler_t *mmap_handler = buf->parent; - nxt_port_mmap_header_t *hdr = mmap_handler->hdr; + nxt_port_mmap_header_t *hdr = mmap_handler->hdr; - msg.mm.mmap_id = hdr->id; + msg.mm.mmap_id = hdr->id; msg.mm.chunk_id = nxt_port_mmap_chunk_id(hdr, buf->mem.pos); - msg.mm.size = nxt_buf_used_size(buf); + msg.mm.size = nxt_buf_used_size(buf); res = nxt_app_queue_send(port->queue, &msg, sizeof(msg), - req_rpc_data->stream, ¬ify, - &req_rpc_data->msg_info.tracking_cookie); + req_rpc_data->stream, ¬ify, &req_rpc_data->msg_info.tracking_cookie); if (nxt_fast_path(res == NXT_OK)) { if (notify != 0) { - (void) nxt_port_socket_write(task, port, - NXT_PORT_MSG_READ_QUEUE, - -1, req_rpc_data->stream, - reply_port->id, NULL); + (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_READ_QUEUE, + -1, req_rpc_data->stream, reply_port->id, NULL); } else { nxt_debug(task, "queue is not empty"); } buf->is_port_mmap_sent = 1; - buf->mem.pos = buf->mem.free; + buf->mem.pos = buf->mem.free; } else { nxt_alert(task, "stream #%uD, app '%V': failed to send app message", - req_rpc_data->stream, &app->name); + req_rpc_data->stream, &app->name); nxt_http_request_error(task, req_rpc_data->request, - NXT_HTTP_INTERNAL_SERVER_ERROR); + NXT_HTTP_INTERNAL_SERVER_ERROR); } } - struct nxt_fields_iter_s { - nxt_list_part_t *part; - nxt_http_field_t *field; + nxt_list_part_t *part; + nxt_http_field_t *field; }; -typedef struct nxt_fields_iter_s nxt_fields_iter_t; - +typedef struct nxt_fields_iter_s nxt_fields_iter_t; static nxt_http_field_t * -nxt_fields_part_first(nxt_list_part_t *part, nxt_fields_iter_t *i) -{ +nxt_fields_part_first(nxt_list_part_t *part, nxt_fields_iter_t *i) { if (part == NULL) { return NULL; } @@ -5378,24 +5174,20 @@ nxt_fields_part_first(nxt_list_part_t *part, nxt_fields_iter_t *i) } } - i->part = part; + i->part = part; i->field = nxt_list_data(i->part); return i->field; } - static nxt_http_field_t * -nxt_fields_first(nxt_list_t *fields, nxt_fields_iter_t *i) -{ +nxt_fields_first(nxt_list_t *fields, nxt_fields_iter_t *i) { return nxt_fields_part_first(nxt_list_part(fields), i); } - static nxt_http_field_t * -nxt_fields_next(nxt_fields_iter_t *i) -{ - nxt_http_field_t *end = nxt_list_data(i->part); +nxt_fields_next(nxt_fields_iter_t *i) { + nxt_http_field_t *end = nxt_list_data(i->part); end += i->part->nelts; i->field++; @@ -5407,58 +5199,54 @@ nxt_fields_next(nxt_fields_iter_t *i) return nxt_fields_part_first(i->part->next, i); } - static nxt_buf_t * -nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, - nxt_app_t *app, const nxt_str_t *prefix) -{ - void *target_pos, *query_pos; - u_char *pos, *end, *p, c; +nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, nxt_app_t *app, + const nxt_str_t *prefix) { + void *target_pos, *query_pos; + u_char *pos, *end, *p, c; size_t fields_count, req_size, size, free_size; size_t copy_size; nxt_off_t content_length; - nxt_buf_t *b, *buf, *out, **tail; - nxt_http_field_t *field, *dup; - nxt_unit_field_t *dst_field; + nxt_buf_t *b, *buf, *out, **tail; + nxt_http_field_t *field, *dup; + nxt_unit_field_t *dst_field; nxt_fields_iter_t iter, dup_iter; - nxt_unit_request_t *req; + nxt_unit_request_t *req; - req_size = sizeof(nxt_unit_request_t) - + r->method->length + 1 - + r->version.length + 1 - + r->remote->address_length + 1 + req_size = sizeof(nxt_unit_request_t) + r->method->length + 1 + + r->version.length + 1 + r->remote->address_length + 1 + r->local->address_length + 1 - + nxt_sockaddr_port_length(r->local) + 1 - + r->server_name.length + 1 - + r->target.length + 1 + + nxt_sockaddr_port_length(r->local) + 1 + r->server_name.length + + 1 + r->target.length + 1 + (r->path->start != r->target.start ? r->path->length + 1 : 0); content_length = r->content_length_n < 0 ? 0 : r->content_length_n; - fields_count = 0; + fields_count = 0; nxt_list_each(field, r->fields) { fields_count++; req_size += field->name_length + prefix->length + 1 + field->value_length + 1; - } nxt_list_loop; + } + nxt_list_loop; req_size += fields_count * sizeof(nxt_unit_field_t); if (nxt_slow_path(req_size > PORT_MMAP_DATA_SIZE)) { nxt_alert(task, "headers to big to fit in shared memory (%d)", - (int) req_size); + (int) req_size); return NULL; } out = nxt_port_mmap_get_buf(task, &app->outgoing, - nxt_min(req_size + content_length, PORT_MMAP_DATA_SIZE)); + nxt_min(req_size + content_length, PORT_MMAP_DATA_SIZE)); if (nxt_slow_path(out == NULL)) { return NULL; } - req = (nxt_unit_request_t *) out->mem.free; + req = (nxt_unit_request_t *) out->mem.free; out->mem.free += req_size; req->app_target = r->app_target; @@ -5471,18 +5259,18 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, req->method_length = r->method->length; nxt_unit_sptr_set(&req->method, p); - p = nxt_cpymem(p, r->method->start, r->method->length); + p = nxt_cpymem(p, r->method->start, r->method->length); *p++ = '\0'; req->version_length = r->version.length; nxt_unit_sptr_set(&req->version, p); - p = nxt_cpymem(p, r->version.start, r->version.length); + p = nxt_cpymem(p, r->version.start, r->version.length); *p++ = '\0'; req->remote_length = r->remote->address_length; nxt_unit_sptr_set(&req->remote, p); - p = nxt_cpymem(p, nxt_sockaddr_address(r->remote), - r->remote->address_length); + p = nxt_cpymem(p, nxt_sockaddr_address(r->remote), + r->remote->address_length); *p++ = '\0'; req->local_addr_length = r->local->address_length; @@ -5492,22 +5280,22 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, req->local_port_length = nxt_sockaddr_port_length(r->local); nxt_unit_sptr_set(&req->local_port, p); - p = nxt_cpymem(p, nxt_sockaddr_port(r->local), - nxt_sockaddr_port_length(r->local)); + p = nxt_cpymem(p, nxt_sockaddr_port(r->local), + nxt_sockaddr_port_length(r->local)); *p++ = '\0'; - req->tls = r->tls; + req->tls = r->tls; req->websocket_handshake = r->websocket_handshake; req->server_name_length = r->server_name.length; nxt_unit_sptr_set(&req->server_name, p); - p = nxt_cpymem(p, r->server_name.start, r->server_name.length); + p = nxt_cpymem(p, r->server_name.start, r->server_name.length); *p++ = '\0'; - target_pos = p; + target_pos = p; req->target_length = (uint32_t) r->target.length; nxt_unit_sptr_set(&req->target, p); - p = nxt_cpymem(p, r->target.start, r->target.length); + p = nxt_cpymem(p, r->target.start, r->target.length); *p++ = '\0'; req->path_length = (uint32_t) r->path->length; @@ -5516,14 +5304,14 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, } else { nxt_unit_sptr_set(&req->path, p); - p = nxt_cpymem(p, r->path->start, r->path->length); + p = nxt_cpymem(p, r->path->start, r->path->length); *p++ = '\0'; } req->query_length = (uint32_t) r->args->length; if (r->args->start != NULL) { - query_pos = nxt_pointer_to(target_pos, - r->args->start - r->target.start); + query_pos + = nxt_pointer_to(target_pos, r->args->start - r->target.start); nxt_unit_sptr_set(&req->query, query_pos); @@ -5538,17 +5326,15 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, dst_field = req->fields; - for (field = nxt_fields_first(r->fields, &iter); - field != NULL; - field = nxt_fields_next(&iter)) - { + for (field = nxt_fields_first(r->fields, &iter); field != NULL; + field = nxt_fields_next(&iter)) { if (field->skip) { continue; } - dst_field->hash = field->hash; - dst_field->skip = 0; - dst_field->name_length = field->name_length + prefix->length; + dst_field->hash = field->hash; + dst_field->skip = 0; + dst_field->name_length = field->name_length + prefix->length; dst_field->value_length = field->value_length; if (field == r->content_length) { @@ -5565,9 +5351,8 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, } nxt_debug(task, "add field 0x%04Xd, %d, %d, %p : %d %p", - (int) field->hash, (int) field->skip, - (int) field->name_length, field->name, - (int) field->value_length, field->value); + (int) field->hash, (int) field->skip, (int) field->name_length, + field->name, (int) field->value_length, field->value); if (prefix->length != 0) { nxt_unit_sptr_set(&dst_field->name, p); @@ -5603,15 +5388,12 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, if (prefix->length != 0) { dup_iter = iter; - for (dup = nxt_fields_next(&dup_iter); - dup != NULL; - dup = nxt_fields_next(&dup_iter)) - { - if (dup->name_length != field->name_length - || dup->skip + for (dup = nxt_fields_next(&dup_iter); dup != NULL; + dup = nxt_fields_next(&dup_iter)) { + if (dup->name_length != field->name_length || dup->skip || dup->hash != field->hash - || nxt_memcasecmp(dup->name, field->name, dup->name_length)) - { + || nxt_memcasecmp(dup->name, field->name, + dup->name_length)) { continue; } @@ -5633,12 +5415,12 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, nxt_unit_sptr_set(&req->preread_content, out->mem.free); - buf = out; + buf = out; tail = &buf->next; for (b = r->body; b != NULL; b = b->next) { size = nxt_buf_mem_used_size(&b->mem); - pos = b->mem.pos; + pos = b->mem.pos; while (size > 0) { if (buf == NULL) { @@ -5647,7 +5429,7 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, buf = nxt_port_mmap_get_buf(task, &app->outgoing, free_size); if (nxt_slow_path(buf == NULL)) { while (out != NULL) { - buf = out->next; + buf = out->next; out->next = NULL; out->completion_handler(task, out, out->parent); out = buf; @@ -5656,14 +5438,13 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, } *tail = buf; - tail = &buf->next; + tail = &buf->next; } else { free_size = nxt_buf_mem_free_size(&buf->mem); if (free_size < size && nxt_port_mmap_increase_buf(task, buf, size, 1) - == NXT_OK) - { + == NXT_OK) { free_size = nxt_buf_mem_free_size(&buf->mem); } } @@ -5674,7 +5455,7 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, buf->mem.free = nxt_cpymem(buf->mem.free, pos, copy_size); size -= copy_size; - pos += copy_size; + pos += copy_size; if (size == 0) { break; @@ -5688,19 +5469,17 @@ nxt_router_prepare_msg(nxt_task_t *task, nxt_http_request_t *r, return out; } - static void -nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; - nxt_http_request_t *r; - nxt_request_rpc_data_t *req_rpc_data; +nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; + nxt_http_request_t *r; + nxt_request_rpc_data_t *req_rpc_data; timer = obj; nxt_debug(task, "router app timeout"); - r = nxt_timer_data(timer, nxt_http_request_t, timer); + r = nxt_timer_data(timer, nxt_http_request_t, timer); req_rpc_data = r->timer_data; nxt_http_request_error(task, r, NXT_HTTP_SERVICE_UNAVAILABLE); @@ -5708,19 +5487,15 @@ nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data) nxt_request_rpc_data_unlink(task, req_rpc_data); } - static void -nxt_router_http_request_release_post(nxt_task_t *task, nxt_http_request_t *r) -{ +nxt_router_http_request_release_post(nxt_task_t *task, nxt_http_request_t *r) { r->timer.handler = nxt_router_http_request_release; nxt_timer_add(task->thread->engine, &r->timer, 0); } - static void -nxt_router_http_request_release(nxt_task_t *task, void *obj, void *data) -{ - nxt_http_request_t *r; +nxt_router_http_request_release(nxt_task_t *task, void *obj, void *data) { + nxt_http_request_t *r; nxt_debug(task, "http request pool release"); @@ -5729,21 +5504,19 @@ nxt_router_http_request_release(nxt_task_t *task, void *obj, void *data) nxt_mp_release(r->mem_pool); } - static void -nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { size_t mi; uint32_t i; nxt_bool_t ack; - nxt_process_t *process; - nxt_free_map_t *m; - nxt_port_mmap_handler_t *mmap_handler; + nxt_process_t *process; + nxt_free_map_t *m; + nxt_port_mmap_handler_t *mmap_handler; nxt_debug(task, "oosm in %PI", msg->port_msg.pid); - process = nxt_runtime_process_find(task->thread->runtime, - msg->port_msg.pid); + process + = nxt_runtime_process_find(task->thread->runtime, msg->port_msg.pid); if (nxt_slow_path(process == NULL)) { return; } @@ -5771,8 +5544,8 @@ nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (m[mi] != 0) { ack = 1; - nxt_debug(task, "oosm: already free #%uD %uz = 0x%08xA", - i, mi, m[mi]); + nxt_debug(task, "oosm: already free #%uD %uz = 0x%08xA", i, mi, + m[mi]); break; } @@ -5786,33 +5559,30 @@ nxt_router_oosm_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - static void -nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_fd_t fd; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_port_mmaps_t *mmaps; - nxt_port_msg_get_mmap_t *get_mmap_msg; - nxt_port_mmap_handler_t *mmap_handler; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_port_mmaps_t *mmaps; + nxt_port_msg_get_mmap_t *get_mmap_msg; + nxt_port_mmap_handler_t *mmap_handler; rt = task->thread->runtime; port = nxt_runtime_port_find(rt, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "get_mmap_handler: reply_port %PI:%d not found", - msg->port_msg.pid, msg->port_msg.reply_port); + msg->port_msg.pid, msg->port_msg.reply_port); return; } if (nxt_slow_path(nxt_buf_used_size(msg->buf) - < (int) sizeof(nxt_port_msg_get_mmap_t))) - { + < (int) sizeof(nxt_port_msg_get_mmap_t))) { nxt_alert(task, "get_mmap_handler: message buffer too small (%d)", - (int) nxt_buf_used_size(msg->buf)); + (int) nxt_buf_used_size(msg->buf)); return; } @@ -5823,11 +5593,11 @@ nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_slow_path(port->app == NULL)) { nxt_alert(task, "get_mmap_handler: app == NULL for reply port %PI:%d", - port->pid, port->id); + port->pid, port->id); // FIXME - nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, - -1, msg->port_msg.stream, 0, NULL); + nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, + msg->port_msg.stream, 0, NULL); return; } @@ -5839,11 +5609,11 @@ nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_thread_mutex_unlock(&mmaps->mutex); nxt_alert(task, "get_mmap_handler: mmap id is too big (%d)", - (int) get_mmap_msg->id); + (int) get_mmap_msg->id); // FIXME - nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, - -1, msg->port_msg.stream, 0, NULL); + nxt_port_socket_write(task, port, NXT_PORT_MSG_RPC_ERROR, -1, + msg->port_msg.stream, 0, NULL); return; } @@ -5853,36 +5623,33 @@ nxt_router_get_mmap_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_thread_mutex_unlock(&mmaps->mutex); - nxt_debug(task, "get mmap %PI:%d found", - msg->port_msg.pid, (int) get_mmap_msg->id); + nxt_debug(task, "get mmap %PI:%d found", msg->port_msg.pid, + (int) get_mmap_msg->id); (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_MMAP, fd, 0, 0, NULL); } - static void -nxt_router_get_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_port_t *port, *reply_port; - nxt_runtime_t *rt; - nxt_port_msg_get_port_t *get_port_msg; +nxt_router_get_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + nxt_port_t *port, *reply_port; + nxt_runtime_t *rt; + nxt_port_msg_get_port_t *get_port_msg; rt = task->thread->runtime; reply_port = nxt_runtime_port_find(rt, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(reply_port == NULL)) { nxt_alert(task, "get_port_handler: reply_port %PI:%d not found", - msg->port_msg.pid, msg->port_msg.reply_port); + msg->port_msg.pid, msg->port_msg.reply_port); return; } if (nxt_slow_path(nxt_buf_used_size(msg->buf) - < (int) sizeof(nxt_port_msg_get_port_t))) - { + < (int) sizeof(nxt_port_msg_get_port_t))) { nxt_alert(task, "get_port_handler: message buffer too small (%d)", - (int) nxt_buf_used_size(msg->buf)); + (int) nxt_buf_used_size(msg->buf)); return; } @@ -5892,13 +5659,13 @@ nxt_router_get_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) port = nxt_runtime_port_find(rt, get_port_msg->pid, get_port_msg->id); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "get_port_handler: port %PI:%d not found", - get_port_msg->pid, get_port_msg->id); + get_port_msg->pid, get_port_msg->id); return; } nxt_debug(task, "get port %PI:%d found", get_port_msg->pid, - get_port_msg->id); + get_port_msg->id); (void) nxt_port_send_port(task, reply_port, port, msg->port_msg.stream); } diff --git a/src/nxt_router.h b/src/nxt_router.h index 06c6bb32a..31609230f 100644 --- a/src/nxt_router.h +++ b/src/nxt_router.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -12,260 +11,259 @@ #include #include -typedef struct nxt_http_request_s nxt_http_request_t; +typedef struct nxt_http_request_s nxt_http_request_t; #include -typedef struct nxt_http_action_s nxt_http_action_t; -typedef struct nxt_http_routes_s nxt_http_routes_t; -typedef struct nxt_http_forward_s nxt_http_forward_t; -typedef struct nxt_upstream_s nxt_upstream_t; -typedef struct nxt_upstreams_s nxt_upstreams_t; -typedef struct nxt_router_access_log_s nxt_router_access_log_t; - +typedef struct nxt_http_action_s nxt_http_action_t; +typedef struct nxt_http_routes_s nxt_http_routes_t; +typedef struct nxt_http_forward_s nxt_http_forward_t; +typedef struct nxt_upstream_s nxt_upstream_t; +typedef struct nxt_upstreams_s nxt_upstreams_t; +typedef struct nxt_router_access_log_s nxt_router_access_log_t; -#define NXT_HTTP_ACTION_ERROR ((nxt_http_action_t *) -1) +#define NXT_HTTP_ACTION_ERROR ((nxt_http_action_t *) -1) typedef struct { - nxt_thread_spinlock_t lock; - nxt_queue_t engines; + nxt_thread_spinlock_t lock; + nxt_queue_t engines; - nxt_queue_t sockets; /* of nxt_socket_conf_t */ - nxt_queue_t apps; /* of nxt_app_t */ + nxt_queue_t sockets; /* of nxt_socket_conf_t */ + nxt_queue_t apps; /* of nxt_app_t */ - nxt_router_access_log_t *access_log; + nxt_router_access_log_t *access_log; } nxt_router_t; - typedef struct { - uint32_t count; - uint32_t threads; + uint32_t count; + uint32_t threads; - nxt_mp_t *mem_pool; - nxt_tstr_state_t *tstr_state; + nxt_mp_t *mem_pool; + nxt_tstr_state_t *tstr_state; - nxt_router_t *router; - nxt_http_routes_t *routes; - nxt_upstreams_t *upstreams; + nxt_router_t *router; + nxt_http_routes_t *routes; + nxt_upstreams_t *upstreams; - nxt_lvlhsh_t mtypes_hash; - nxt_lvlhsh_t apps_hash; + nxt_lvlhsh_t mtypes_hash; + nxt_lvlhsh_t apps_hash; - nxt_router_access_log_t *access_log; - nxt_tstr_t *log_format; + nxt_router_access_log_t *access_log; + nxt_tstr_t *log_format; nxt_tstr_cond_t log_cond; } nxt_router_conf_t; - typedef struct { - nxt_event_engine_t *engine; - nxt_work_t *jobs; + nxt_event_engine_t *engine; + nxt_work_t *jobs; enum { NXT_ROUTER_ENGINE_KEEP = 0, NXT_ROUTER_ENGINE_ADD, NXT_ROUTER_ENGINE_DELETE, - } action; + } action; } nxt_router_engine_conf_t; - typedef struct { #if (NXT_TLS) - nxt_queue_t tls; /* of nxt_router_tlssock_t */ + nxt_queue_t tls; /* of nxt_router_tlssock_t */ #endif #if (NXT_HAVE_NJS) - nxt_queue_t js_modules; + nxt_queue_t js_modules; #endif - nxt_queue_t apps; /* of nxt_app_t */ - nxt_queue_t previous; /* of nxt_app_t */ + nxt_queue_t apps; /* of nxt_app_t */ + nxt_queue_t previous; /* of nxt_app_t */ - uint32_t new_threads; - uint32_t stream; - uint32_t count; + uint32_t new_threads; + uint32_t stream; + uint32_t count; - nxt_event_engine_t *engine; - nxt_port_t *port; - nxt_array_t *engines; - nxt_router_conf_t *router_conf; - nxt_mp_t *mem_pool; + nxt_event_engine_t *engine; + nxt_port_t *port; + nxt_array_t *engines; + nxt_router_conf_t *router_conf; + nxt_mp_t *mem_pool; } nxt_router_temp_conf_t; - typedef struct { nxt_task_t task; nxt_work_t work; - nxt_router_temp_conf_t *tmcf; + nxt_router_temp_conf_t *tmcf; } nxt_joint_job_t; - typedef struct { - uint32_t use_count; - nxt_app_t *app; - nxt_timer_t idle_timer; - nxt_work_t free_app_work; + uint32_t use_count; + nxt_app_t *app; + nxt_timer_t idle_timer; + nxt_work_t free_app_work; } nxt_app_joint_t; - struct nxt_app_s { - nxt_thread_mutex_t mutex; /* Protects ports queue. */ - nxt_queue_t ports; /* of nxt_port_t.app_link */ - nxt_lvlhsh_t port_hash; /* of nxt_port_t */ + nxt_thread_mutex_t mutex; /* Protects ports queue. */ + nxt_queue_t ports; /* of nxt_port_t.app_link */ + nxt_lvlhsh_t port_hash; /* of nxt_port_t */ - nxt_queue_t spare_ports; /* of nxt_port_t.idle_link */ - nxt_queue_t idle_ports; /* of nxt_port_t.idle_link */ - nxt_work_t adjust_idle_work; - nxt_event_engine_t *engine; + nxt_queue_t spare_ports; /* of nxt_port_t.idle_link */ + nxt_queue_t idle_ports; /* of nxt_port_t.idle_link */ + nxt_work_t adjust_idle_work; + nxt_event_engine_t *engine; - nxt_str_t name; + nxt_str_t name; - uint32_t port_hash_count; + uint32_t port_hash_count; - uint32_t active_requests; - uint32_t pending_processes; - uint32_t processes; - uint32_t idle_processes; + uint32_t active_requests; + uint32_t pending_processes; + uint32_t processes; + uint32_t idle_processes; - uint32_t max_processes; - uint32_t spare_processes; - uint32_t max_pending_processes; + uint32_t max_processes; + uint32_t spare_processes; + uint32_t max_pending_processes; - uint32_t generation; - uint32_t proto_port_requests; + uint32_t generation; + uint32_t proto_port_requests; - nxt_msec_t timeout; - nxt_msec_t idle_timeout; + nxt_msec_t timeout; + nxt_msec_t idle_timeout; - nxt_str_t *targets; + nxt_str_t *targets; - nxt_app_type_t type:8; + nxt_app_type_t type:8; - nxt_mp_t *mem_pool; - nxt_queue_link_t link; + nxt_mp_t *mem_pool; + nxt_queue_link_t link; - nxt_str_t conf; + nxt_str_t conf; - nxt_atomic_t use_count; - nxt_queue_t ack_waiting_req; /* of nxt_http_request_t.app_link */ + nxt_atomic_t use_count; + nxt_queue_t ack_waiting_req; /* of nxt_http_request_t.app_link */ - nxt_app_joint_t *joint; - nxt_port_t *shared_port; - nxt_port_t *proto_port; + nxt_app_joint_t *joint; + nxt_port_t *shared_port; + nxt_port_t *proto_port; - nxt_port_mmaps_t outgoing; + nxt_port_mmaps_t outgoing; }; - typedef struct { - size_t max_frame_size; - nxt_msec_t read_timeout; - nxt_msec_t keepalive_interval; + size_t max_frame_size; + nxt_msec_t read_timeout; + nxt_msec_t keepalive_interval; } nxt_websocket_conf_t; - typedef struct { - uint32_t count; - nxt_queue_link_t link; - nxt_router_conf_t *router_conf; + uint32_t count; + nxt_queue_link_t link; + nxt_router_conf_t *router_conf; - nxt_http_action_t *action; + nxt_http_action_t *action; /* * A listen socket time can be shorter than socket configuration life * time, so a copy of the non-wildcard socket sockaddr is stored here * to be used as a local sockaddr in connections. */ - nxt_sockaddr_t *sockaddr; + nxt_sockaddr_t *sockaddr; - nxt_listen_socket_t *listen; + nxt_listen_socket_t *listen; - size_t header_buffer_size; - size_t large_header_buffer_size; - size_t large_header_buffers; - size_t body_buffer_size; - size_t max_body_size; - size_t proxy_header_buffer_size; - size_t proxy_buffer_size; - size_t proxy_buffers; + size_t header_buffer_size; + size_t large_header_buffer_size; + size_t large_header_buffers; + size_t body_buffer_size; + size_t max_body_size; + size_t proxy_header_buffer_size; + size_t proxy_buffer_size; + size_t proxy_buffers; - nxt_msec_t idle_timeout; - nxt_msec_t header_read_timeout; - nxt_msec_t body_read_timeout; - nxt_msec_t send_timeout; - nxt_msec_t proxy_timeout; - nxt_msec_t proxy_send_timeout; - nxt_msec_t proxy_read_timeout; + nxt_msec_t idle_timeout; + nxt_msec_t header_read_timeout; + nxt_msec_t body_read_timeout; + nxt_msec_t send_timeout; + nxt_msec_t proxy_timeout; + nxt_msec_t proxy_send_timeout; + nxt_msec_t proxy_read_timeout; - nxt_websocket_conf_t websocket_conf; + nxt_websocket_conf_t websocket_conf; - nxt_str_t body_temp_path; + nxt_str_t body_temp_path; - uint8_t log_route; /* 1 bit */ + uint8_t log_route; /* 1 bit */ - uint8_t discard_unsafe_fields; /* 1 bit */ + uint8_t discard_unsafe_fields; /* 1 bit */ - uint8_t server_version; /* 1 bit */ - uint8_t chunked_transform; /* 1 bit */ + uint8_t server_version; /* 1 bit */ + uint8_t chunked_transform; /* 1 bit */ - nxt_http_forward_t *forwarded; - nxt_http_forward_t *client_ip; + nxt_http_forward_t *forwarded; + nxt_http_forward_t *client_ip; #if (NXT_TLS) - nxt_tls_conf_t *tls; + nxt_tls_conf_t *tls; #endif } nxt_socket_conf_t; - typedef struct { - uint32_t count; - nxt_queue_link_t link; - nxt_event_engine_t *engine; - nxt_socket_conf_t *socket_conf; + uint32_t count; + nxt_queue_link_t link; + nxt_event_engine_t *engine; + nxt_socket_conf_t *socket_conf; - nxt_joint_job_t *close_job; + nxt_joint_job_t *close_job; - nxt_upstream_t **upstreams; + nxt_upstream_t **upstreams; /* Modules configuraitons. */ } nxt_socket_conf_joint_t; - struct nxt_router_access_log_s { - void (*handler)(nxt_task_t *task, nxt_http_request_t *r, - nxt_router_access_log_t *access_log, - nxt_tstr_t *format); - nxt_fd_t fd; - nxt_str_t path; - uint32_t count; + void (*handler)(nxt_task_t *task, nxt_http_request_t *r, + nxt_router_access_log_t *access_log, nxt_tstr_t *format); + nxt_fd_t fd; + nxt_str_t path; + uint32_t count; }; - -void nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r, +void +nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r, nxt_http_action_t *action); -void nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port); -nxt_int_t nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name, +void +nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port); +nxt_int_t +nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name, nxt_str_t *target, nxt_http_action_t *action); -void nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev, +void +nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev, nxt_socket_conf_joint_t *joint); -void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data); -void nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); -void nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint); - -nxt_int_t nxt_router_access_log_create(nxt_task_t *task, - nxt_router_conf_t *rtcf, nxt_conf_value_t *value); -void nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); -void nxt_router_access_log_use(nxt_thread_spinlock_t *lock, +void +nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data); +void +nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); +void +nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint); + +nxt_int_t +nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, + nxt_conf_value_t *value); +void +nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf); +void +nxt_router_access_log_use(nxt_thread_spinlock_t *lock, + nxt_router_access_log_t *access_log); +void +nxt_router_access_log_release(nxt_task_t *task, nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log); -void nxt_router_access_log_release(nxt_task_t *task, - nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log); -void nxt_router_access_log_reopen_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); +void +nxt_router_access_log_reopen_handler(nxt_task_t *task, + nxt_port_recv_msg_t *msg); -extern nxt_router_t *nxt_router; +extern nxt_router_t *nxt_router; -#endif /* _NXT_ROUTER_H_INCLUDED_ */ +#endif /* _NXT_ROUTER_H_INCLUDED_ */ diff --git a/src/nxt_router_access_log.c b/src/nxt_router_access_log.c index 024bff395..9357644b6 100644 --- a/src/nxt_router_access_log.c +++ b/src/nxt_router_access_log.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -9,38 +8,41 @@ #include #include - typedef struct { - nxt_str_t path; - nxt_str_t format; - nxt_conf_value_t *expr; + nxt_str_t path; + nxt_str_t format; + nxt_conf_value_t *expr; } nxt_router_access_log_conf_t; - typedef struct { - nxt_str_t text; - nxt_router_access_log_t *access_log; + nxt_str_t text; + nxt_router_access_log_t *access_log; } nxt_router_access_log_ctx_t; - -static void nxt_router_access_log_writer(nxt_task_t *task, - nxt_http_request_t *r, nxt_router_access_log_t *access_log, - nxt_tstr_t *format); -static void nxt_router_access_log_write(nxt_task_t *task, nxt_http_request_t *r, +static void +nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, + nxt_router_access_log_t *access_log, nxt_tstr_t *format); +static void +nxt_router_access_log_write(nxt_task_t *task, nxt_http_request_t *r, nxt_router_access_log_ctx_t *ctx); -static void nxt_router_access_log_ready(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_access_log_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj, +static void +nxt_router_access_log_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_router_access_log_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj, + void *data); +static void +nxt_router_access_log_reopen_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, + void *data); +static void +nxt_router_access_log_reopen_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_access_log_reopen_ready(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static void nxt_router_access_log_reopen_error(nxt_task_t *task, - nxt_port_recv_msg_t *msg, void *data); -static nxt_conf_map_t nxt_router_access_log_conf[] = { +static nxt_conf_map_t nxt_router_access_log_conf[] = { { nxt_string("path"), NXT_CONF_MAP_STR, @@ -60,21 +62,20 @@ static nxt_conf_map_t nxt_router_access_log_conf[] = { }, }; - nxt_int_t nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, - nxt_conf_value_t *value) -{ - nxt_int_t ret; - nxt_str_t str; - nxt_tstr_t *format; - nxt_router_t *router; - nxt_router_access_log_t *access_log; - nxt_router_access_log_conf_t alcf; - - static const nxt_str_t log_format_str = nxt_string("$remote_addr - - " - "[$time_local] \"$request_line\" $status $body_bytes_sent " - "\"$header_referer\" \"$header_user_agent\""); + nxt_conf_value_t *value) { + nxt_int_t ret; + nxt_str_t str; + nxt_tstr_t *format; + nxt_router_t *router; + nxt_router_access_log_t *access_log; + nxt_router_access_log_conf_t alcf; + + static const nxt_str_t log_format_str + = nxt_string("$remote_addr - - " + "[$time_local] \"$request_line\" $status $body_bytes_sent " + "\"$header_referer\" \"$header_user_agent\""); nxt_memzero(&alcf, sizeof(nxt_router_access_log_conf_t)); @@ -85,9 +86,8 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, } else { ret = nxt_conf_map_object(rtcf->mem_pool, value, - nxt_router_access_log_conf, - nxt_nitems(nxt_router_access_log_conf), - &alcf); + nxt_router_access_log_conf, nxt_nitems(nxt_router_access_log_conf), + &alcf); if (ret != NXT_OK) { nxt_alert(task, "access log map error"); return NXT_ERROR; @@ -102,26 +102,26 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, nxt_router_access_log_use(&router->lock, access_log); } else { - access_log = nxt_malloc(sizeof(nxt_router_access_log_t) - + alcf.path.length); + access_log + = nxt_malloc(sizeof(nxt_router_access_log_t) + alcf.path.length); if (access_log == NULL) { nxt_alert(task, "failed to allocate access log structure"); return NXT_ERROR; } - access_log->fd = -1; + access_log->fd = -1; access_log->handler = &nxt_router_access_log_writer; - access_log->count = 1; + access_log->count = 1; access_log->path.length = alcf.path.length; - access_log->path.start = (u_char *) access_log - + sizeof(nxt_router_access_log_t); + access_log->path.start + = (u_char *) access_log + sizeof(nxt_router_access_log_t); nxt_memcpy(access_log->path.start, alcf.path.start, alcf.path.length); } format = nxt_tstr_compile(rtcf->tstr_state, &alcf.format, - NXT_TSTR_LOGGING | NXT_TSTR_NEWLINE); + NXT_TSTR_LOGGING | NXT_TSTR_NEWLINE); if (nxt_slow_path(format == NULL)) { return NXT_ERROR; } @@ -141,14 +141,12 @@ nxt_router_access_log_create(nxt_task_t *task, nxt_router_conf_t *rtcf, return NXT_OK; } - static void nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, - nxt_router_access_log_t *access_log, nxt_tstr_t *format) -{ + nxt_router_access_log_t *access_log, nxt_tstr_t *format) { nxt_int_t ret; - nxt_router_conf_t *rtcf; - nxt_router_access_log_ctx_t *ctx; + nxt_router_conf_t *rtcf; + nxt_router_access_log_ctx_t *ctx; ctx = nxt_mp_get(r->mem_pool, sizeof(nxt_router_access_log_ctx_t)); if (nxt_slow_path(ctx == NULL)) { @@ -164,7 +162,7 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, rtcf = r->conf->socket_conf->router_conf; ret = nxt_tstr_query_init(&r->tstr_query, rtcf->tstr_state, - &r->tstr_cache, r, r->mem_pool); + &r->tstr_cache, r, r->mem_pool); if (nxt_slow_path(ret != NXT_OK)) { return; } @@ -178,26 +176,22 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, nxt_router_access_log_write(task, r, ctx); } - static void nxt_router_access_log_write(nxt_task_t *task, nxt_http_request_t *r, - nxt_router_access_log_ctx_t *ctx) -{ + nxt_router_access_log_ctx_t *ctx) { nxt_fd_write(ctx->access_log->fd, ctx->text.start, ctx->text.length); nxt_http_request_close_handler(task, r, r->proto.any); } - void -nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) -{ +nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) { uint32_t stream; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *main_port, *router_port; - nxt_runtime_t *rt; - nxt_router_access_log_t *access_log; + nxt_buf_t *b; + nxt_port_t *main_port, *router_port; + nxt_runtime_t *rt; + nxt_router_access_log_t *access_log; access_log = tmcf->router_conf->access_log; @@ -211,20 +205,18 @@ nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) nxt_buf_cpystr(b, &access_log->path); *b->mem.free++ = '\0'; - rt = task->thread->runtime; - main_port = rt->port_by_type[NXT_PROCESS_MAIN]; + rt = task->thread->runtime; + main_port = rt->port_by_type[NXT_PROCESS_MAIN]; router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; stream = nxt_port_rpc_register_handler(task, router_port, - nxt_router_access_log_ready, - nxt_router_access_log_error, - -1, tmcf); + nxt_router_access_log_ready, nxt_router_access_log_error, -1, tmcf); if (nxt_slow_path(stream == 0)) { goto fail; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_ACCESS_LOG, -1, - stream, router_port->id, b); + stream, router_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, router_port, stream); @@ -238,13 +230,11 @@ nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf) nxt_router_conf_error(task, tmcf); } - static void nxt_router_access_log_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_router_temp_conf_t *tmcf; - nxt_router_access_log_t *access_log; + void *data) { + nxt_router_temp_conf_t *tmcf; + nxt_router_access_log_t *access_log; tmcf = data; @@ -253,26 +243,22 @@ nxt_router_access_log_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, access_log->fd = msg->fd[0]; nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_router_conf_apply, task, tmcf, NULL); + nxt_router_conf_apply, task, tmcf, NULL); } - static void nxt_router_access_log_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_router_temp_conf_t *tmcf; + void *data) { + nxt_router_temp_conf_t *tmcf; tmcf = data; nxt_router_conf_error(task, tmcf); } - void nxt_router_access_log_use(nxt_thread_spinlock_t *lock, - nxt_router_access_log_t *access_log) -{ + nxt_router_access_log_t *access_log) { if (access_log == NULL) { return; } @@ -284,11 +270,9 @@ nxt_router_access_log_use(nxt_thread_spinlock_t *lock, nxt_thread_spin_unlock(lock); } - void nxt_router_access_log_release(nxt_task_t *task, nxt_thread_spinlock_t *lock, - nxt_router_access_log_t *access_log) -{ + nxt_router_access_log_t *access_log) { if (access_log == NULL) { return; } @@ -302,7 +286,6 @@ nxt_router_access_log_release(nxt_task_t *task, nxt_thread_spinlock_t *lock, nxt_thread_spin_unlock(lock); if (access_log != NULL) { - if (access_log->fd != -1) { nxt_fd_close(access_log->fd); } @@ -311,24 +294,22 @@ nxt_router_access_log_release(nxt_task_t *task, nxt_thread_spinlock_t *lock, } } - typedef struct { - nxt_mp_t *mem_pool; - nxt_router_access_log_t *access_log; + nxt_mp_t *mem_pool; + nxt_router_access_log_t *access_log; } nxt_router_access_log_reopen_t; - void -nxt_router_access_log_reopen_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - nxt_mp_t *mp; +nxt_router_access_log_reopen_handler(nxt_task_t *task, + nxt_port_recv_msg_t *msg) { + nxt_mp_t *mp; uint32_t stream; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *main_port, *router_port; - nxt_runtime_t *rt; - nxt_router_access_log_t *access_log; - nxt_router_access_log_reopen_t *reopen; + nxt_buf_t *b; + nxt_port_t *main_port, *router_port; + nxt_runtime_t *rt; + nxt_router_access_log_t *access_log; + nxt_router_access_log_reopen_t *reopen; access_log = nxt_router->access_log; @@ -346,7 +327,7 @@ nxt_router_access_log_reopen_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto fail; } - reopen->mem_pool = mp; + reopen->mem_pool = mp; reopen->access_log = access_log; b = nxt_buf_mem_alloc(mp, access_log->path.length + 1, 0); @@ -359,20 +340,19 @@ nxt_router_access_log_reopen_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_buf_cpystr(b, &access_log->path); *b->mem.free++ = '\0'; - rt = task->thread->runtime; - main_port = rt->port_by_type[NXT_PROCESS_MAIN]; + rt = task->thread->runtime; + main_port = rt->port_by_type[NXT_PROCESS_MAIN]; router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; stream = nxt_port_rpc_register_handler(task, router_port, - nxt_router_access_log_reopen_ready, - nxt_router_access_log_reopen_error, - -1, reopen); + nxt_router_access_log_reopen_ready, nxt_router_access_log_reopen_error, + -1, reopen); if (nxt_slow_path(stream == 0)) { goto fail; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_ACCESS_LOG, -1, - stream, router_port->id, b); + stream, router_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, router_port, stream); @@ -388,36 +368,32 @@ nxt_router_access_log_reopen_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) nxt_mp_destroy(mp); } - static void -nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b; +nxt_router_access_log_reopen_completion(nxt_task_t *task, void *obj, + void *data) { + nxt_mp_t *mp; + nxt_buf_t *b; - b = obj; + b = obj; mp = b->data; nxt_mp_release(mp); } - static void nxt_router_access_log_reopen_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_router_access_log_t *access_log; - nxt_router_access_log_reopen_t *reopen; + void *data) { + nxt_router_access_log_t *access_log; + nxt_router_access_log_reopen_t *reopen; reopen = data; access_log = reopen->access_log; if (access_log == nxt_router->access_log) { - if (nxt_slow_path(dup2(msg->fd[0], access_log->fd) == -1)) { - nxt_alert(task, "dup2(%FD, %FD) failed %E", - msg->fd[0], access_log->fd, nxt_errno); + nxt_alert(task, "dup2(%FD, %FD) failed %E", msg->fd[0], + access_log->fd, nxt_errno); } } @@ -425,12 +401,10 @@ nxt_router_access_log_reopen_ready(nxt_task_t *task, nxt_port_recv_msg_t *msg, nxt_mp_release(reopen->mem_pool); } - static void nxt_router_access_log_reopen_error(nxt_task_t *task, nxt_port_recv_msg_t *msg, - void *data) -{ - nxt_router_access_log_reopen_t *reopen; + void *data) { + nxt_router_access_log_reopen_t *reopen; reopen = data; diff --git a/src/nxt_router_request.h b/src/nxt_router_request.h index 6e226bae8..535f62da5 100644 --- a/src/nxt_router_request.h +++ b/src/nxt_router_request.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,14 +5,12 @@ #ifndef _NXT_ROUTER_REQUEST_H_INCLUDED_ #define _NXT_ROUTER_REQUEST_H_INCLUDED_ - typedef struct { - nxt_buf_t *buf; - nxt_fd_t body_fd; - uint32_t tracking_cookie; + nxt_buf_t *buf; + nxt_fd_t body_fd; + uint32_t tracking_cookie; } nxt_msg_info_t; - typedef enum { NXT_APR_NEW_PORT, NXT_APR_REQUEST_FAILED, @@ -22,18 +19,17 @@ typedef enum { NXT_APR_CLOSE, } nxt_apr_action_t; - typedef struct { - uint32_t stream; - nxt_app_t *app; + uint32_t stream; + nxt_app_t *app; - nxt_port_t *app_port; - nxt_apr_action_t apr_action; + nxt_port_t *app_port; + nxt_apr_action_t apr_action; - nxt_http_request_t *request; - nxt_msg_info_t msg_info; + nxt_http_request_t *request; + nxt_msg_info_t msg_info; - nxt_bool_t rpc_cancel; + nxt_bool_t rpc_cancel; } nxt_request_rpc_data_t; diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c index de76f19e0..c8a0d8c89 100644 --- a/src/nxt_runtime.c +++ b/src/nxt_runtime.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -13,44 +12,57 @@ #include -static nxt_int_t nxt_runtime_inherited_listen_sockets(nxt_task_t *task, - nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_systemd_listen_sockets(nxt_task_t *task, - nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt); -static void nxt_runtime_start(nxt_task_t *task, void *obj, void *data); -static void nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status); -static void nxt_runtime_close_idle_connections(nxt_event_engine_t *engine); -static void nxt_runtime_stop_all_processes(nxt_task_t *task, nxt_runtime_t *rt); -static void nxt_runtime_exit(nxt_task_t *task, void *obj, void *data); -static nxt_int_t nxt_runtime_event_engine_change(nxt_task_t *task, - nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_log_files_init(nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_log_files_create(nxt_task_t *task, - nxt_runtime_t *rt); -static nxt_int_t nxt_runtime_pid_file_create(nxt_task_t *task, - nxt_file_name_t *pid_file); -static void nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, +static nxt_int_t +nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt); +static void +nxt_runtime_start(nxt_task_t *task, void *obj, void *data); +static void +nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status); +static void +nxt_runtime_close_idle_connections(nxt_event_engine_t *engine); +static void +nxt_runtime_stop_all_processes(nxt_task_t *task, nxt_runtime_t *rt); +static void +nxt_runtime_exit(nxt_task_t *task, void *obj, void *data); +static nxt_int_t +nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_log_files_init(nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_log_files_create(nxt_task_t *task, nxt_runtime_t *rt); +static nxt_int_t +nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file); +static void +nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, nxt_runtime_cont_t cont); -static void nxt_runtime_thread_pool_init(void); -static void nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, - void *data); -static nxt_process_t *nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid); -static void nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port); - +static void +nxt_runtime_thread_pool_init(void); +static void +nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data); +static nxt_process_t * +nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid); +static void +nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port); nxt_int_t -nxt_runtime_create(nxt_task_t *task) -{ - nxt_mp_t *mp; +nxt_runtime_create(nxt_task_t *task) { + nxt_mp_t *mp; nxt_int_t ret; - nxt_array_t *listen_sockets; - nxt_runtime_t *rt; - nxt_app_lang_module_t *lang; + nxt_array_t *listen_sockets; + nxt_runtime_t *rt; + nxt_app_lang_module_t *lang; mp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp == NULL)) { @@ -63,7 +75,7 @@ nxt_runtime_create(nxt_task_t *task) } task->thread->runtime = rt; - rt->mem_pool = mp; + rt->mem_pool = mp; nxt_thread_mutex_create(&rt->processes_mutex); @@ -78,11 +90,11 @@ nxt_runtime_create(nxt_task_t *task) } /* Should not fail. */ - lang = nxt_array_add(rt->languages); - lang->type = NXT_APP_EXTERNAL; + lang = nxt_array_add(rt->languages); + lang->type = NXT_APP_EXTERNAL; lang->version = (u_char *) ""; - lang->file = NULL; - lang->module = &nxt_external_module; + lang->file = NULL; + lang->module = &nxt_external_module; lang->mounts = nxt_array_create(mp, 1, sizeof(nxt_fs_mount_t)); if (nxt_slow_path(lang->mounts == NULL)) { @@ -136,7 +148,7 @@ nxt_runtime_create(nxt_task_t *task) } nxt_work_queue_add(&task->thread->engine->fast_work_queue, - nxt_runtime_start, task, rt, NULL); + nxt_runtime_start, task, rt, NULL); return NXT_OK; @@ -147,15 +159,13 @@ nxt_runtime_create(nxt_task_t *task) return NXT_ERROR; } - static nxt_int_t -nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) -{ - u_char *v, *p; +nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) { + u_char *v, *p; nxt_int_t type; - nxt_array_t *inherited_sockets; + nxt_array_t *inherited_sockets; nxt_socket_t s; - nxt_listen_socket_t *ls; + nxt_listen_socket_t *ls; v = (u_char *) getenv("NGINX"); @@ -165,8 +175,8 @@ nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) nxt_alert(task, "using inherited listen sockets: %s", v); - inherited_sockets = nxt_array_create(rt->mem_pool, - 1, sizeof(nxt_listen_socket_t)); + inherited_sockets + = nxt_array_create(rt->mem_pool, 1, sizeof(nxt_listen_socket_t)); if (inherited_sockets == NULL) { return NXT_ERROR; } @@ -174,14 +184,15 @@ nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) rt->inherited_sockets = inherited_sockets; for (p = v; *p != '\0'; p++) { - if (*p == ';') { s = nxt_int_parse(v, p - v); if (nxt_slow_path(s < 0)) { - nxt_alert(task, "invalid socket number \"%s\" " - "in NGINX environment variable, " - "ignoring the rest of the variable", v); + nxt_alert(task, + "invalid socket number \"%s\" " + "in NGINX environment variable, " + "ignoring the rest of the variable", + v); return NXT_ERROR; } @@ -211,15 +222,13 @@ nxt_runtime_inherited_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static nxt_int_t -nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) -{ - u_char *nfd, *pid; +nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) { + u_char *nfd, *pid; nxt_int_t n; - nxt_array_t *inherited_sockets; + nxt_array_t *inherited_sockets; nxt_socket_t s; - nxt_listen_socket_t *ls; + nxt_listen_socket_t *ls; /* * Number of listening sockets passed. The socket @@ -247,8 +256,8 @@ nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) nxt_log(task, NXT_LOG_INFO, "using %i systemd listen sockets", n); - inherited_sockets = nxt_array_create(rt->mem_pool, - n, sizeof(nxt_listen_socket_t)); + inherited_sockets + = nxt_array_create(rt->mem_pool, n, sizeof(nxt_listen_socket_t)); if (inherited_sockets == NULL) { return NXT_ERROR; } @@ -274,13 +283,11 @@ nxt_runtime_systemd_listen_sockets(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static nxt_int_t -nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt) -{ - nxt_thread_t *thread; - nxt_event_engine_t *engine; - const nxt_event_interface_t *interface; +nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt) { + nxt_thread_t *thread; + nxt_event_engine_t *engine; + const nxt_event_interface_t *interface; interface = nxt_service_get(rt->services, "engine", NULL); @@ -289,20 +296,20 @@ nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt) return NXT_ERROR; } - engine = nxt_event_engine_create(task, interface, - nxt_main_process_signals, 0, 0); + engine = nxt_event_engine_create(task, interface, nxt_main_process_signals, + 0, 0); if (nxt_slow_path(engine == NULL)) { return NXT_ERROR; } - thread = task->thread; + thread = task->thread; thread->engine = engine; #if 0 thread->fiber = &engine->fibers->fiber; #endif - engine->id = rt->last_engine_id++; + engine->id = rt->last_engine_id++; engine->mem_pool = nxt_mp_create(1024, 128, 256, 32); nxt_queue_init(&rt->engines); @@ -311,15 +318,13 @@ nxt_runtime_event_engines(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static nxt_int_t -nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt) -{ +nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt) { nxt_int_t ret; - nxt_array_t *thread_pools; + nxt_array_t *thread_pools; - thread_pools = nxt_array_create(rt->mem_pool, 1, - sizeof(nxt_thread_pool_t *)); + thread_pools + = nxt_array_create(rt->mem_pool, 1, sizeof(nxt_thread_pool_t *)); if (nxt_slow_path(thread_pools == NULL)) { return NXT_ERROR; @@ -335,18 +340,16 @@ nxt_runtime_thread_pools(nxt_thread_t *thr, nxt_runtime_t *rt) return NXT_OK; } - static void -nxt_runtime_start(nxt_task_t *task, void *obj, void *data) -{ - nxt_runtime_t *rt; +nxt_runtime_start(nxt_task_t *task, void *obj, void *data) { + nxt_runtime_t *rt; rt = obj; nxt_debug(task, "rt conf done"); task->thread->log->ctx_handler = NULL; - task->thread->log->ctx = NULL; + task->thread->log->ctx = NULL; if (nxt_runtime_log_files_create(task, rt) != NXT_OK) { goto fail; @@ -370,20 +373,17 @@ nxt_runtime_start(nxt_task_t *task, void *obj, void *data) nxt_runtime_quit(task, 1); } - static void -nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status) -{ +nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status) { nxt_int_t ret; - nxt_thread_t *thr; - nxt_runtime_t *rt; - const nxt_event_interface_t *interface; + nxt_thread_t *thr; + nxt_runtime_t *rt; + const nxt_event_interface_t *interface; thr = task->thread; - rt = thr->runtime; + rt = thr->runtime; if (rt->inherited_sockets == NULL && rt->daemon) { - if (nxt_process_daemon(task) != NXT_OK) { goto fail; } @@ -400,7 +400,7 @@ nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status) } ret = nxt_event_engine_change(task->thread->engine, interface, - rt->batch); + rt->batch); if (ret != NXT_OK) { goto fail; } @@ -426,17 +426,15 @@ nxt_runtime_initial_start(nxt_task_t *task, nxt_uint_t status) nxt_runtime_quit(task, 1); } - void -nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status) -{ +nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status) { nxt_bool_t done; - nxt_runtime_t *rt; - nxt_event_engine_t *engine; + nxt_runtime_t *rt; + nxt_event_engine_t *engine; - rt = task->thread->runtime; + rt = task->thread->runtime; rt->status |= status; - engine = task->thread->engine; + engine = task->thread->engine; nxt_debug(task, "exiting"); @@ -459,29 +457,25 @@ nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status) nxt_runtime_close_idle_connections(engine); if (done) { - nxt_work_queue_add(&engine->fast_work_queue, nxt_runtime_exit, - task, rt, engine); + nxt_work_queue_add(&engine->fast_work_queue, nxt_runtime_exit, task, rt, + engine); } } - static void -nxt_runtime_close_idle_connections(nxt_event_engine_t *engine) -{ - nxt_conn_t *c; - nxt_queue_t *idle; - nxt_queue_link_t *link, *next; +nxt_runtime_close_idle_connections(nxt_event_engine_t *engine) { + nxt_conn_t *c; + nxt_queue_t *idle; + nxt_queue_link_t *link, *next; nxt_debug(&engine->task, "close idle connections"); idle = &engine->idle_connections; - for (link = nxt_queue_first(idle); - link != nxt_queue_tail(idle); - link = next) - { + for (link = nxt_queue_first(idle); link != nxt_queue_tail(idle); + link = next) { next = nxt_queue_next(link); - c = nxt_queue_link_data(link, nxt_conn_t, link); + c = nxt_queue_link_data(link, nxt_conn_t, link); if (!c->socket.read_ready) { nxt_queue_remove(link); @@ -490,64 +484,52 @@ nxt_runtime_close_idle_connections(nxt_event_engine_t *engine) } } - void -nxt_runtime_stop_app_processes(nxt_task_t *task, nxt_runtime_t *rt) -{ - nxt_port_t *port; - nxt_process_t *process; - nxt_process_init_t *init; +nxt_runtime_stop_app_processes(nxt_task_t *task, nxt_runtime_t *rt) { + nxt_port_t *port; + nxt_process_t *process; + nxt_process_init_t *init; nxt_runtime_process_each(rt, process) { - init = nxt_process_init(process); if (init->type == NXT_PROCESS_APP - || init->type == NXT_PROCESS_PROTOTYPE) - { - + || init->type == NXT_PROCESS_PROTOTYPE) { nxt_process_port_each(process, port) { - (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, -1, - 0, 0, NULL); - - } nxt_process_port_loop; + 0, 0, NULL); + } + nxt_process_port_loop; } - - } nxt_runtime_process_loop; + } + nxt_runtime_process_loop; } - static void -nxt_runtime_stop_all_processes(nxt_task_t *task, nxt_runtime_t *rt) -{ - nxt_port_t *port; - nxt_process_t *process; +nxt_runtime_stop_all_processes(nxt_task_t *task, nxt_runtime_t *rt) { + nxt_port_t *port; + nxt_process_t *process; nxt_runtime_process_each(rt, process) { - nxt_process_port_each(process, port) { - nxt_debug(task, "%d sending quit to %PI", rt->type, port->pid); (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_QUIT, -1, 0, - 0, NULL); - - } nxt_process_port_loop; - - } nxt_runtime_process_loop; + 0, NULL); + } + nxt_process_port_loop; + } + nxt_runtime_process_loop; } - static void -nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) -{ +nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) { int status, engine_count; - nxt_runtime_t *rt; - nxt_process_t *process; - nxt_event_engine_t *engine; + nxt_runtime_t *rt; + nxt_process_t *process; + nxt_event_engine_t *engine; - rt = obj; + rt = obj; engine = data; nxt_debug(task, "thread pools: %d", rt->thread_pools->nelts); @@ -564,8 +546,8 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) #if (NXT_HAVE_UNIX_DOMAIN) { size_t i; - nxt_sockaddr_t *sa; - nxt_file_name_t *name; + nxt_sockaddr_t *sa; + nxt_file_name_t *name; sa = rt->controller_listen; @@ -575,14 +557,13 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) } for (i = 0; i < rt->listen_sockets->nelts; i++) { - nxt_listen_socket_t *ls; + nxt_listen_socket_t *ls; ls = (nxt_listen_socket_t *) rt->listen_sockets->elts + i; sa = ls->sockaddr; if (sa->u.sockaddr.sa_family != AF_UNIX - || sa->u.sockaddr_un.sun_path[0] == '\0') - { + || sa->u.sockaddr_un.sun_path[0] == '\0') { continue; } @@ -598,21 +579,19 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) } nxt_runtime_process_each(rt, process) { - nxt_runtime_process_remove(rt, process); nxt_process_close_ports(task, process); - - } nxt_runtime_process_loop; + } + nxt_runtime_process_loop; status = rt->status; engine_count = 0; nxt_queue_each(engine, &rt->engines, nxt_event_engine_t, link) { - engine_count++; - - } nxt_queue_loop; + } + nxt_queue_loop; if (engine_count <= 1) { if (rt->port_by_type[rt->type] != NULL) { @@ -630,18 +609,15 @@ nxt_runtime_exit(nxt_task_t *task, void *obj, void *data) nxt_unreachable(); } - static nxt_int_t -nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt) -{ - nxt_event_engine_t *engine; - const nxt_event_interface_t *interface; +nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt) { + nxt_event_engine_t *engine; + const nxt_event_interface_t *interface; engine = task->thread->engine; if (engine->batch == rt->batch - && nxt_strcmp(engine->event.name, rt->engine) == 0) - { + && nxt_strcmp(engine->event.name, rt->engine) == 0) { return NXT_OK; } @@ -654,12 +630,10 @@ nxt_runtime_event_engine_change(nxt_task_t *task, nxt_runtime_t *rt) return NXT_ERROR; } - void -nxt_runtime_event_engine_free(nxt_runtime_t *rt) -{ - nxt_queue_link_t *link; - nxt_event_engine_t *engine; +nxt_runtime_event_engine_free(nxt_runtime_t *rt) { + nxt_queue_link_t *link; + nxt_event_engine_t *engine; link = nxt_queue_first(&rt->engines); nxt_queue_remove(link); @@ -668,12 +642,10 @@ nxt_runtime_event_engine_free(nxt_runtime_t *rt) nxt_event_engine_free(engine); } - nxt_int_t nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, - nxt_uint_t max_threads, nxt_nsec_t timeout) -{ - nxt_thread_pool_t *thread_pool, **tp; + nxt_uint_t max_threads, nxt_nsec_t timeout) { + nxt_thread_pool_t *thread_pool, **tp; tp = nxt_array_add(rt->thread_pools); if (tp == NULL) { @@ -681,9 +653,8 @@ nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, } thread_pool = nxt_thread_pool_create(max_threads, timeout, - nxt_runtime_thread_pool_init, - thr->engine, - nxt_runtime_thread_pool_exit); + nxt_runtime_thread_pool_init, thr->engine, + nxt_runtime_thread_pool_exit); if (nxt_fast_path(thread_pool != NULL)) { *tp = thread_pool; @@ -692,13 +663,11 @@ nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, return NXT_OK; } - static void nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, - nxt_runtime_cont_t cont) -{ - nxt_uint_t n; - nxt_thread_pool_t **tp; + nxt_runtime_cont_t cont) { + nxt_uint_t n; + nxt_thread_pool_t **tp; rt->continuation = cont; @@ -719,20 +688,16 @@ nxt_runtime_thread_pool_destroy(nxt_task_t *task, nxt_runtime_t *rt, } while (n != 0); } - static void -nxt_runtime_thread_pool_init(void) -{ +nxt_runtime_thread_pool_init(void) { } - static void -nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) -{ - nxt_uint_t i, n; - nxt_runtime_t *rt; - nxt_thread_pool_t *tp, **thread_pools; - nxt_thread_handle_t handle; +nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) { + nxt_uint_t i, n; + nxt_runtime_t *rt; + nxt_thread_pool_t *tp, **thread_pools; + nxt_thread_handle_t handle; tp = obj; @@ -744,12 +709,11 @@ nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) rt = task->thread->runtime; thread_pools = rt->thread_pools->elts; - n = rt->thread_pools->nelts; + n = rt->thread_pools->nelts; nxt_debug(task, "thread pools: %ui", n); for (i = 0; i < n; i++) { - if (tp == thread_pools[i]) { nxt_array_remove(rt->thread_pools, &thread_pools[i]); @@ -765,30 +729,28 @@ nxt_runtime_thread_pool_exit(nxt_task_t *task, void *obj, void *data) } } - static nxt_int_t -nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) -{ +nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) { nxt_int_t ret; nxt_str_t control; nxt_uint_t n; - nxt_file_t *file; - const char *slash; - nxt_sockaddr_t *sa; + nxt_file_t *file; + const char *slash; + nxt_sockaddr_t *sa; nxt_file_name_str_t file_name; - const nxt_event_interface_t *interface; + const nxt_event_interface_t *interface; - rt->daemon = 1; + rt->daemon = 1; rt->engine_connections = 256; - rt->auxiliary_threads = 2; - rt->user_cred.user = NXT_USER; - rt->group = NXT_GROUP; - rt->pid = NXT_PID; - rt->log = NXT_LOG; - rt->modules = NXT_MODULESDIR; - rt->state = NXT_STATEDIR; - rt->control = NXT_CONTROL_SOCK; - rt->tmp = NXT_TMPDIR; + rt->auxiliary_threads = 2; + rt->user_cred.user = NXT_USER; + rt->group = NXT_GROUP; + rt->pid = NXT_PID; + rt->log = NXT_LOG; + rt->modules = NXT_MODULESDIR; + rt->state = NXT_STATEDIR; + rt->control = NXT_CONTROL_SOCK; + rt->tmp = NXT_TMPDIR; nxt_memzero(&rt->capabilities, sizeof(nxt_capabilities_t)); @@ -801,16 +763,16 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) } if (rt->capabilities.setid) { - ret = nxt_credential_get(task, rt->mem_pool, &rt->user_cred, - rt->group); + ret = nxt_credential_get(task, rt->mem_pool, &rt->user_cred, rt->group); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } } else { - nxt_log(task, NXT_LOG_WARN, "Unit is running unprivileged, then it " - "cannot use arbitrary user and group."); + nxt_log(task, NXT_LOG_WARN, + "Unit is running unprivileged, then it " + "cannot use arbitrary user and group."); } /* An engine's parameters. */ @@ -834,18 +796,18 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) return NXT_ERROR; } - file = nxt_list_first(rt->log_files); + file = nxt_list_first(rt->log_files); file->name = file_name.start; slash = ""; - n = nxt_strlen(rt->modules); + n = nxt_strlen(rt->modules); if (n > 1 && rt->modules[n - 1] != '/') { slash = "/"; } ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%s*.unit.so%Z", - rt->modules, slash); + rt->modules, slash); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -853,14 +815,14 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) rt->modules = (char *) file_name.start; slash = ""; - n = nxt_strlen(rt->state); + n = nxt_strlen(rt->state); if (n > 1 && rt->state[n - 1] != '/') { slash = "/"; } ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%sversion%Z", - rt->state, slash); + rt->state, slash); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -875,7 +837,7 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) rt->ver_tmp = (char *) file_name.start; ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%sconf.json%Z", - rt->state, slash); + rt->state, slash); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -890,7 +852,7 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) rt->conf_tmp = (char *) file_name.start; ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%scerts/%Z", - rt->state, slash); + rt->state, slash); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -898,15 +860,17 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) ret = mkdir((char *) file_name.start, 0700); if (nxt_fast_path(ret == 0 || nxt_errno == EEXIST)) { rt->certs.length = file_name.len; - rt->certs.start = file_name.start; + rt->certs.start = file_name.start; } else { - nxt_alert(task, "Unable to create certificates storage directory: " - "mkdir(%s) failed %E", file_name.start, nxt_errno); + nxt_alert(task, + "Unable to create certificates storage directory: " + "mkdir(%s) failed %E", + file_name.start, nxt_errno); } ret = nxt_file_name_create(rt->mem_pool, &file_name, "%s%sscripts/%Z", - rt->state, slash); + rt->state, slash); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -914,15 +878,17 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) ret = mkdir((char *) file_name.start, 0700); if (nxt_fast_path(ret == 0 || nxt_errno == EEXIST)) { rt->scripts.length = file_name.len; - rt->scripts.start = file_name.start; + rt->scripts.start = file_name.start; } else { - nxt_alert(task, "Unable to create scripts storage directory: " - "mkdir(%s) failed %E", file_name.start, nxt_errno); + nxt_alert(task, + "Unable to create scripts storage directory: " + "mkdir(%s) failed %E", + file_name.start, nxt_errno); } control.length = nxt_strlen(rt->control); - control.start = (u_char *) rt->control; + control.start = (u_char *) rt->control; sa = nxt_sockaddr_parse(rt->mem_pool, &control); if (nxt_slow_path(sa == NULL)) { @@ -940,90 +906,87 @@ nxt_runtime_conf_init(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - static nxt_int_t -nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) -{ - char *p, **argv; - u_char *end; +nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) { + char *p, **argv; + u_char *end; u_char buf[1024]; - static const char version[] = - "unit version: " NXT_VERSION "\n" - "configured as ./configure" NXT_CONFIGURE_OPTIONS "\n"; - - static const char no_control[] = - "option \"--control\" requires socket address\n"; - static const char no_control_mode[] = - "option \"--control-mode\" requires a mode\n"; - static const char no_control_user[] = - "option \"--control-user\" requires a username\n"; - static const char no_control_group[] = - "option \"--control-group\" requires a group name\n"; - static const char no_user[] = "option \"--user\" requires username\n"; - static const char no_group[] = "option \"--group\" requires group name\n"; - static const char no_pid[] = "option \"--pid\" requires filename\n"; - static const char no_log[] = "option \"--log\" requires filename\n"; - static const char no_modules[] = - "option \"--modulesdir\" requires directory\n"; - static const char no_state[] = - "option \"--statedir\" requires directory\n"; - static const char no_tmp[] = "option \"--tmpdir\" requires directory\n"; - - static const char modules_deprecated[] = - "option \"--modules\" is deprecated; use \"--modulesdir\" instead\n"; - static const char state_deprecated[] = - "option \"--state\" is deprecated; use \"--statedir\" instead\n"; - static const char tmp_deprecated[] = - "option \"--tmp\" is deprecated; use \"--tmpdir\" instead\n"; - - static const char help[] = - "\n" - "unit options:\n" - "\n" - " --version print unit version and configure options\n" - "\n" - " --no-daemon run unit in non-daemon mode\n" - "\n" - " --control ADDRESS set address of control API socket\n" - " default: \"" NXT_CONTROL_SOCK "\"\n" - "\n" - " --control-mode MODE set mode of the control API socket\n" - " default: 0600\n" - "\n" - " --control-user USER set the owner of the control API socket\n" - "\n" - " --control-group GROUP set the group of the control API socket\n" - "\n" - " --pid FILE set pid filename\n" - " default: \"" NXT_PID "\"\n" - "\n" - " --log FILE set log filename\n" - " default: \"" NXT_LOG "\"\n" - "\n" - " --modulesdir DIR set modules directory name\n" - " default: \"" NXT_MODULESDIR "\"\n" - "\n" - " --statedir DIR set state directory name\n" - " default: \"" NXT_STATEDIR "\"\n" - "\n" - " --tmpdir DIR set tmp directory name\n" - " default: \"" NXT_TMPDIR "\"\n" - "\n" - " --modules DIR [deprecated] synonym for --modulesdir\n" - " --state DIR [deprecated] synonym for --statedir\n" - " --tmp DIR [deprecated] synonym for --tmpdir\n" - "\n" - " --user USER set non-privileged processes to run" - " as specified user\n" - " default: \"" NXT_USER "\"\n" - "\n" - " --group GROUP set non-privileged processes to run" - " as specified group\n" - " default: "; - - static const char group[] = "\"" NXT_GROUP "\"\n\n"; - static const char primary[] = "user's primary group\n\n"; + static const char version[] + = "unit version: " NXT_VERSION "\n" + "configured as ./configure" NXT_CONFIGURE_OPTIONS "\n"; + + static const char no_control[] + = "option \"--control\" requires socket address\n"; + static const char no_control_mode[] + = "option \"--control-mode\" requires a mode\n"; + static const char no_control_user[] + = "option \"--control-user\" requires a username\n"; + static const char no_control_group[] + = "option \"--control-group\" requires a group name\n"; + static const char no_user[] = "option \"--user\" requires username\n"; + static const char no_group[] = "option \"--group\" requires group name\n"; + static const char no_pid[] = "option \"--pid\" requires filename\n"; + static const char no_log[] = "option \"--log\" requires filename\n"; + static const char no_modules[] + = "option \"--modulesdir\" requires directory\n"; + static const char no_state[] = "option \"--statedir\" requires directory\n"; + static const char no_tmp[] = "option \"--tmpdir\" requires directory\n"; + + static const char modules_deprecated[] + = "option \"--modules\" is deprecated; use \"--modulesdir\" instead\n"; + static const char state_deprecated[] + = "option \"--state\" is deprecated; use \"--statedir\" instead\n"; + static const char tmp_deprecated[] + = "option \"--tmp\" is deprecated; use \"--tmpdir\" instead\n"; + + static const char help[] + = "\n" + "unit options:\n" + "\n" + " --version print unit version and configure options\n" + "\n" + " --no-daemon run unit in non-daemon mode\n" + "\n" + " --control ADDRESS set address of control API socket\n" + " default: \"" NXT_CONTROL_SOCK "\"\n" + "\n" + " --control-mode MODE set mode of the control API socket\n" + " default: 0600\n" + "\n" + " --control-user USER set the owner of the control API socket\n" + "\n" + " --control-group GROUP set the group of the control API socket\n" + "\n" + " --pid FILE set pid filename\n" + " default: \"" NXT_PID "\"\n" + "\n" + " --log FILE set log filename\n" + " default: \"" NXT_LOG "\"\n" + "\n" + " --modulesdir DIR set modules directory name\n" + " default: \"" NXT_MODULESDIR "\"\n" + "\n" + " --statedir DIR set state directory name\n" + " default: \"" NXT_STATEDIR "\"\n" + "\n" + " --tmpdir DIR set tmp directory name\n" + " default: \"" NXT_TMPDIR "\"\n" + "\n" + " --modules DIR [deprecated] synonym for --modulesdir\n" + " --state DIR [deprecated] synonym for --statedir\n" + " --tmp DIR [deprecated] synonym for --tmpdir\n" + "\n" + " --user USER set non-privileged processes to run" + " as specified user\n" + " default: \"" NXT_USER "\"\n" + "\n" + " --group GROUP set non-privileged processes to run" + " as specified group\n" + " default: "; + + static const char group[] = "\"" NXT_GROUP "\"\n\n"; + static const char primary[] = "user's primary group\n\n"; argv = &nxt_process_argv[1]; @@ -1046,7 +1009,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) if (nxt_strcmp(p, "--control-mode") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_control_mode, - nxt_length(no_control_mode)); + nxt_length(no_control_mode)); return NXT_ERROR; } @@ -1060,7 +1023,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) if (nxt_strcmp(p, "--control-user") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_control_user, - nxt_length(no_control_user)); + nxt_length(no_control_user)); return NXT_ERROR; } @@ -1074,7 +1037,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) if (nxt_strcmp(p, "--control-group") == 0) { if (*argv == NULL) { write(STDERR_FILENO, no_control_group, - nxt_length(no_control_group)); + nxt_length(no_control_group)); return NXT_ERROR; } @@ -1139,12 +1102,12 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) if (nxt_strcmp(p, "--modules") == 0) { write(STDERR_FILENO, modules_deprecated, - nxt_length(modules_deprecated)); + nxt_length(modules_deprecated)); goto modulesdir; } if (nxt_strcmp(p, "--modulesdir") == 0) { -modulesdir: + modulesdir: if (*argv == NULL) { write(STDERR_FILENO, no_modules, nxt_length(no_modules)); return NXT_ERROR; @@ -1159,12 +1122,12 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) if (nxt_strcmp(p, "--state") == 0) { write(STDERR_FILENO, state_deprecated, - nxt_length(state_deprecated)); + nxt_length(state_deprecated)); goto statedir; } if (nxt_strcmp(p, "--statedir") == 0) { -statedir: + statedir: if (*argv == NULL) { write(STDERR_FILENO, no_state, nxt_length(no_state)); return NXT_ERROR; @@ -1183,7 +1146,7 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) } if (nxt_strcmp(p, "--tmpdir") == 0) { -tmpdir: + tmpdir: if (*argv == NULL) { write(STDERR_FILENO, no_tmp, nxt_length(no_tmp)); return NXT_ERROR; @@ -1219,9 +1182,10 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) exit(0); } - end = nxt_sprintf(buf, buf + sizeof(buf), "unknown option \"%s\", " - "try \"%s -h\" for available options\n", - p, nxt_process_argv[0]); + end = nxt_sprintf(buf, buf + sizeof(buf), + "unknown option \"%s\", " + "try \"%s -h\" for available options\n", + p, nxt_process_argv[0]); write(STDERR_FILENO, buf, end - buf); @@ -1231,12 +1195,10 @@ nxt_runtime_conf_read_cmd(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - nxt_listen_socket_t * -nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) -{ - nxt_mp_t *mp; - nxt_listen_socket_t *ls; +nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) { + nxt_mp_t *mp; + nxt_listen_socket_t *ls; ls = nxt_array_zero_add(rt->listen_sockets); if (ls == NULL) { @@ -1245,8 +1207,8 @@ nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) mp = rt->mem_pool; - ls->sockaddr = nxt_sockaddr_create(mp, &sa->u.sockaddr, sa->socklen, - sa->length); + ls->sockaddr + = nxt_sockaddr_create(mp, &sa->u.sockaddr, sa->socklen, sa->length); if (ls->sockaddr == NULL) { return NULL; } @@ -1255,18 +1217,16 @@ nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa) nxt_sockaddr_text(ls->sockaddr); - ls->socket = -1; + ls->socket = -1; ls->backlog = NXT_LISTEN_BACKLOG; return ls; } - static nxt_int_t -nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) -{ - size_t length; - char hostname[NXT_MAXHOSTNAMELEN + 1]; +nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) { + size_t length; + char hostname[NXT_MAXHOSTNAMELEN + 1]; if (gethostname(hostname, NXT_MAXHOSTNAMELEN) != 0) { nxt_alert(task, "gethostname() failed %E", nxt_errno); @@ -1283,7 +1243,7 @@ nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) */ hostname[NXT_MAXHOSTNAMELEN] = '\0'; - length = nxt_strlen(hostname); + length = nxt_strlen(hostname); rt->hostname.length = length; rt->hostname.start = nxt_mp_nget(rt->mem_pool, length); @@ -1296,12 +1256,10 @@ nxt_runtime_hostname(nxt_task_t *task, nxt_runtime_t *rt) return NXT_ERROR; } - static nxt_int_t -nxt_runtime_log_files_init(nxt_runtime_t *rt) -{ - nxt_file_t *file; - nxt_list_t *log_files; +nxt_runtime_log_files_init(nxt_runtime_t *rt) { + nxt_file_t *file; + nxt_list_t *log_files; log_files = nxt_list_create(rt->mem_pool, 1, sizeof(nxt_file_t)); @@ -1311,7 +1269,7 @@ nxt_runtime_log_files_init(nxt_runtime_t *rt) /* Preallocate the main log. This allocation cannot fail. */ file = nxt_list_zero_add(log_files); - file->fd = NXT_FILE_INVALID; + file->fd = NXT_FILE_INVALID; file->log_level = NXT_LOG_ALERT; return NXT_OK; @@ -1320,13 +1278,11 @@ nxt_runtime_log_files_init(nxt_runtime_t *rt) return NXT_ERROR; } - nxt_file_t * -nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) -{ - nxt_int_t ret; - nxt_file_t *file; - nxt_file_name_str_t file_name; +nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) { + nxt_int_t ret; + nxt_file_t *file; + nxt_file_name_str_t file_name; ret = nxt_file_name_create(rt->mem_pool, &file_name, "V%Z", name); @@ -1335,16 +1291,14 @@ nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) } nxt_list_each(file, rt->log_files) { - /* STUB: hardecoded case sensitive/insensitive. */ if (file->name != NULL - && nxt_file_name_eq(file->name, file_name.start)) - { + && nxt_file_name_eq(file->name, file_name.start)) { return file; } - - } nxt_list_loop; + } + nxt_list_loop; file = nxt_list_zero_add(rt->log_files); @@ -1352,62 +1306,54 @@ nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name) return NULL; } - file->fd = NXT_FILE_INVALID; + file->fd = NXT_FILE_INVALID; file->log_level = NXT_LOG_ALERT; - file->name = file_name.start; + file->name = file_name.start; return file; } - static nxt_int_t -nxt_runtime_log_files_create(nxt_task_t *task, nxt_runtime_t *rt) -{ +nxt_runtime_log_files_create(nxt_task_t *task, nxt_runtime_t *rt) { nxt_int_t ret; - nxt_file_t *file; + nxt_file_t *file; nxt_list_each(file, rt->log_files) { - ret = nxt_file_open(task, file, O_WRONLY | O_APPEND, O_CREAT, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); if (ret != NXT_OK) { return NXT_ERROR; } - - } nxt_list_loop; + } + nxt_list_loop; file = nxt_list_first(rt->log_files); return nxt_file_stderr(file); } - nxt_int_t -nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt) -{ +nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt) { nxt_int_t ret; nxt_uint_t c, p, ncurr, nprev; - nxt_listen_socket_t *curr, *prev; + nxt_listen_socket_t *curr, *prev; - curr = rt->listen_sockets->elts; + curr = rt->listen_sockets->elts; ncurr = rt->listen_sockets->nelts; if (rt->inherited_sockets != NULL) { - prev = rt->inherited_sockets->elts; + prev = rt->inherited_sockets->elts; nprev = rt->inherited_sockets->nelts; } else { - prev = NULL; + prev = NULL; nprev = 0; } for (c = 0; c < ncurr; c++) { - for (p = 0; p < nprev; p++) { - if (nxt_sockaddr_cmp(curr[c].sockaddr, prev[p].sockaddr)) { - ret = nxt_listen_socket_update(task, &curr[c], &prev[p]); if (ret != NXT_OK) { return NXT_ERROR; @@ -1429,15 +1375,13 @@ nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - nxt_int_t -nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt) -{ +nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt) { nxt_uint_t i, n; - nxt_listen_socket_t *ls; + nxt_listen_socket_t *ls; ls = rt->listen_sockets->elts; - n = rt->listen_sockets->nelts; + n = rt->listen_sockets->nelts; for (i = 0; i < n; i++) { if (ls[i].flags == NXT_NONBLOCK) { @@ -1450,13 +1394,11 @@ nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt) return NXT_OK; } - nxt_str_t * -nxt_current_directory(nxt_mp_t *mp) -{ +nxt_current_directory(nxt_mp_t *mp) { size_t length; - u_char *p; - nxt_str_t *name; + u_char *p; + nxt_str_t *name; char buf[NXT_MAX_PATH_LEN]; length = nxt_dir_current(buf, NXT_MAX_PATH_LEN); @@ -1465,7 +1407,7 @@ nxt_current_directory(nxt_mp_t *mp) name = nxt_str_alloc(mp, length + 1); if (nxt_fast_path(name != NULL)) { - p = nxt_cpymem(name->start, buf, length); + p = nxt_cpymem(name->start, buf, length); *p = '/'; return name; @@ -1475,14 +1417,12 @@ nxt_current_directory(nxt_mp_t *mp) return NULL; } - static nxt_int_t -nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) -{ - ssize_t length; - nxt_int_t n; - nxt_file_t file; - u_char pid[NXT_INT64_T_LEN + nxt_length("\n")]; +nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) { + ssize_t length; + nxt_int_t n; + nxt_file_t file; + u_char pid[NXT_INT64_T_LEN + nxt_length("\n")]; nxt_memzero(&file, sizeof(nxt_file_t)); @@ -1491,7 +1431,7 @@ nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) nxt_fs_mkdir_p_dirname(pid_file, 0755); n = nxt_file_open(task, &file, O_WRONLY, O_CREAT | O_TRUNC, - NXT_FILE_DEFAULT_ACCESS); + NXT_FILE_DEFAULT_ACCESS); if (n != NXT_OK) { return NXT_ERROR; @@ -1508,11 +1448,9 @@ nxt_runtime_pid_file_create(nxt_task_t *task, nxt_file_name_t *pid_file) return NXT_OK; } - void -nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process) -{ - nxt_process_t *child; +nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process) { + nxt_process_t *child; if (process->registered == 1) { nxt_runtime_process_remove(rt, process); @@ -1525,7 +1463,8 @@ nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process) nxt_queue_each(child, &process->children, nxt_process_t, link) { nxt_queue_remove(&child->link); child->link.next = NULL; - } nxt_queue_loop; + } + nxt_queue_loop; nxt_assert(process->use_count == 0); nxt_assert(process->registered == 0); @@ -1543,46 +1482,39 @@ nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process) nxt_mp_free(rt->mem_pool, process); } - static nxt_int_t -nxt_runtime_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_process_t *process; +nxt_runtime_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_process_t *process; process = data; if (lhq->key.length == sizeof(nxt_pid_t) - && *(nxt_pid_t *) lhq->key.start == process->pid) - { + && *(nxt_pid_t *) lhq->key.start == process->pid) { return NXT_OK; } return NXT_DECLINED; } -static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_runtime_lvlhsh_pid_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - nxt_inline void -nxt_runtime_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) -{ - lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); +nxt_runtime_process_lhq_pid(nxt_lvlhsh_query_t *lhq, nxt_pid_t *pid) { + lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); lhq->key.length = sizeof(*pid); - lhq->key.start = (u_char *) pid; - lhq->proto = &lvlhsh_processes_proto; + lhq->key.start = (u_char *) pid; + lhq->proto = &lvlhsh_processes_proto; } - nxt_process_t * -nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) -{ - nxt_process_t *process; - nxt_lvlhsh_query_t lhq; +nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) { + nxt_process_t *process; + nxt_lvlhsh_query_t lhq; process = NULL; @@ -1602,12 +1534,10 @@ nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid) return process; } - static nxt_process_t * -nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) -{ - nxt_process_t *process; - nxt_lvlhsh_query_t lhq; +nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) { + nxt_process_t *process; + nxt_lvlhsh_query_t lhq; nxt_runtime_process_lhq_pid(&lhq, &pid); @@ -1626,7 +1556,6 @@ nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) process = nxt_process_new(rt); if (nxt_slow_path(process == NULL)) { - nxt_thread_mutex_unlock(&rt->processes_mutex); return NULL; @@ -1635,11 +1564,10 @@ nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) process->pid = pid; lhq.replace = 0; - lhq.value = process; - lhq.pool = rt->mem_pool; + lhq.value = process; + lhq.pool = rt->mem_pool; switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { - case NXT_OK: if (rt->nprocesses == 0) { rt->mprocess = process; @@ -1662,13 +1590,11 @@ nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid) return process; } - void -nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) -{ - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_lvlhsh_query_t lhq; +nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) { + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_lvlhsh_query_t lhq; nxt_assert(process->registered == 0); @@ -1677,13 +1603,12 @@ nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) nxt_runtime_process_lhq_pid(&lhq, &process->pid); lhq.replace = 0; - lhq.value = process; - lhq.pool = rt->mem_pool; + lhq.value = process; + lhq.pool = rt->mem_pool; nxt_thread_mutex_lock(&rt->processes_mutex); switch (nxt_lvlhsh_insert(&rt->processes, &lhq)) { - case NXT_OK: if (rt->nprocesses == 0) { rt->mprocess = process; @@ -1692,12 +1617,11 @@ nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) rt->nprocesses++; nxt_process_port_each(process, port) { - port->pid = process->pid; nxt_runtime_port_add(task, port); - - } nxt_process_port_loop; + } + nxt_process_port_loop; process->registered = 1; @@ -1712,12 +1636,10 @@ nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process) nxt_thread_mutex_unlock(&rt->processes_mutex); } - void -nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process) -{ - nxt_pid_t pid; - nxt_lvlhsh_query_t lhq; +nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process) { + nxt_pid_t pid; + nxt_lvlhsh_query_t lhq; nxt_assert(process->registered != 0); @@ -1730,7 +1652,6 @@ nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process) nxt_thread_mutex_lock(&rt->processes_mutex); switch (nxt_lvlhsh_delete(&rt->processes, &lhq)) { - case NXT_OK: nxt_assert(lhq.value == process); @@ -1749,22 +1670,18 @@ nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process) nxt_thread_mutex_unlock(&rt->processes_mutex); } - nxt_process_t * -nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) -{ +nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) { nxt_lvlhsh_each_init(lhe, &lvlhsh_processes_proto); return nxt_runtime_process_next(rt, lhe); } - nxt_port_t * nxt_runtime_process_port_create(nxt_task_t *task, nxt_runtime_t *rt, - nxt_pid_t pid, nxt_port_id_t id, nxt_process_type_t type) -{ - nxt_port_t *port; - nxt_process_t *process; + nxt_pid_t pid, nxt_port_id_t id, nxt_process_type_t type) { + nxt_port_t *port; + nxt_process_t *process; process = nxt_runtime_process_get(rt, pid); if (nxt_slow_path(process == NULL)) { @@ -1788,12 +1705,10 @@ nxt_runtime_process_port_create(nxt_task_t *task, nxt_runtime_t *rt, return port; } - static void -nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port) -{ +nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port) { nxt_int_t res; - nxt_runtime_t *rt; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -1808,12 +1723,10 @@ nxt_runtime_port_add(nxt_task_t *task, nxt_port_t *port) nxt_port_use(task, port, 1); } - void -nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port) -{ +nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port) { nxt_int_t res; - nxt_runtime_t *rt; + nxt_runtime_t *rt; rt = task->thread->runtime; @@ -1830,10 +1743,7 @@ nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port) nxt_port_use(task, port, -1); } - nxt_port_t * -nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, - nxt_port_id_t port_id) -{ +nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, nxt_port_id_t port_id) { return nxt_port_hash_find(&rt->ports, pid, port_id); } diff --git a/src/nxt_runtime.h b/src/nxt_runtime.h index 7bd490d70..e5c9806f7 100644 --- a/src/nxt_runtime.h +++ b/src/nxt_runtime.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) Valentin V. Bartenev @@ -11,161 +10,173 @@ typedef void (*nxt_runtime_cont_t)(nxt_task_t *task, nxt_uint_t status); - struct nxt_runtime_s { - nxt_mp_t *mem_pool; + nxt_mp_t *mem_pool; - nxt_array_t *inherited_sockets; /* of nxt_listen_socket_t */ - nxt_array_t *listen_sockets; /* of nxt_listen_socket_t */ + nxt_array_t *inherited_sockets; /* of nxt_listen_socket_t */ + nxt_array_t *listen_sockets; /* of nxt_listen_socket_t */ - nxt_array_t *services; /* of nxt_service_t */ - nxt_array_t *languages; /* of nxt_app_lang_module_t */ - void *data; + nxt_array_t *services; /* of nxt_service_t */ + nxt_array_t *languages; /* of nxt_app_lang_module_t */ + void *data; - nxt_runtime_cont_t start; + nxt_runtime_cont_t start; - nxt_str_t hostname; + nxt_str_t hostname; - nxt_file_name_t *pid_file; + nxt_file_name_t *pid_file; #if (NXT_TLS) - const nxt_tls_lib_t *tls; + const nxt_tls_lib_t *tls; #endif - nxt_array_t *thread_pools; /* of nxt_thread_pool_t */ - nxt_runtime_cont_t continuation; + nxt_array_t *thread_pools; /* of nxt_thread_pool_t */ + nxt_runtime_cont_t continuation; - nxt_process_t *mprocess; - size_t nprocesses; - nxt_thread_mutex_t processes_mutex; - nxt_lvlhsh_t processes; /* of nxt_process_t */ + nxt_process_t *mprocess; + size_t nprocesses; + nxt_thread_mutex_t processes_mutex; + nxt_lvlhsh_t processes; /* of nxt_process_t */ - nxt_port_t *port_by_type[NXT_PROCESS_MAX]; - nxt_lvlhsh_t ports; /* of nxt_port_t */ + nxt_port_t *port_by_type[NXT_PROCESS_MAX]; + nxt_lvlhsh_t ports; /* of nxt_port_t */ - nxt_list_t *log_files; /* of nxt_file_t */ + nxt_list_t *log_files; /* of nxt_file_t */ - uint32_t last_engine_id; + uint32_t last_engine_id; - nxt_process_type_t type; + nxt_process_type_t type; - nxt_timer_t timer; + nxt_timer_t timer; - uint8_t daemon; - uint8_t batch; - uint8_t status; - uint8_t is_pid_isolated; + uint8_t daemon; + uint8_t batch; + uint8_t status; + uint8_t is_pid_isolated; - const char *engine; - uint32_t engine_connections; - uint32_t auxiliary_threads; - nxt_credential_t user_cred; - nxt_capabilities_t capabilities; - const char *group; - const char *pid; - const char *log; - const char *modules; - const char *state; - const char *ver; - const char *ver_tmp; - const char *conf; - const char *conf_tmp; - const char *tmp; - const char *control; + const char *engine; + uint32_t engine_connections; + uint32_t auxiliary_threads; + nxt_credential_t user_cred; + nxt_capabilities_t capabilities; + const char *group; + const char *pid; + const char *log; + const char *modules; + const char *state; + const char *ver; + const char *ver_tmp; + const char *conf; + const char *conf_tmp; + const char *tmp; + const char *control; - mode_t control_mode; - const char *control_user; - const char *control_group; + mode_t control_mode; + const char *control_user; + const char *control_group; - nxt_str_t certs; - nxt_str_t scripts; + nxt_str_t certs; + nxt_str_t scripts; - nxt_queue_t engines; /* of nxt_event_engine_t */ + nxt_queue_t engines; /* of nxt_event_engine_t */ - nxt_sockaddr_t *controller_listen; - nxt_listen_socket_t *controller_socket; + nxt_sockaddr_t *controller_listen; + nxt_listen_socket_t *controller_socket; }; - - typedef nxt_int_t (*nxt_module_init_t)(nxt_thread_t *thr, nxt_runtime_t *rt); -nxt_int_t nxt_runtime_create(nxt_task_t *task); -void nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status); +nxt_int_t +nxt_runtime_create(nxt_task_t *task); +void +nxt_runtime_quit(nxt_task_t *task, nxt_uint_t status); -void nxt_runtime_event_engine_free(nxt_runtime_t *rt); +void +nxt_runtime_event_engine_free(nxt_runtime_t *rt); -nxt_int_t nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, +nxt_int_t +nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt, nxt_uint_t max_threads, nxt_nsec_t timeout); -void nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process); -void nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process); +void +nxt_runtime_process_add(nxt_task_t *task, nxt_process_t *process); +void +nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process); -nxt_process_t *nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid); +nxt_process_t * +nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid); -nxt_process_t *nxt_runtime_process_first(nxt_runtime_t *rt, - nxt_lvlhsh_each_t *lhe); +nxt_process_t * +nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe); -void nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process); +void +nxt_runtime_process_release(nxt_runtime_t *rt, nxt_process_t *process); -#define nxt_runtime_process_next(rt, lhe) \ - nxt_lvlhsh_each(&rt->processes, lhe) +#define nxt_runtime_process_next(rt, lhe) nxt_lvlhsh_each(&rt->processes, lhe) -nxt_port_t *nxt_runtime_process_port_create(nxt_task_t *task, nxt_runtime_t *rt, +nxt_port_t * +nxt_runtime_process_port_create(nxt_task_t *task, nxt_runtime_t *rt, nxt_pid_t pid, nxt_port_id_t id, nxt_process_type_t type); -void nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port); -void nxt_runtime_stop_app_processes(nxt_task_t *task, nxt_runtime_t *rt); +void +nxt_runtime_port_remove(nxt_task_t *task, nxt_port_t *port); +void +nxt_runtime_stop_app_processes(nxt_task_t *task, nxt_runtime_t *rt); -NXT_EXPORT nxt_port_t *nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, - nxt_port_id_t port_id); +NXT_EXPORT nxt_port_t * +nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid, nxt_port_id_t port_id); /* STUB */ -nxt_int_t nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt); +nxt_int_t +nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt); -nxt_str_t *nxt_current_directory(nxt_mp_t *mp); +nxt_str_t * +nxt_current_directory(nxt_mp_t *mp); -nxt_listen_socket_t *nxt_runtime_listen_socket_add(nxt_runtime_t *rt, - nxt_sockaddr_t *sa); -nxt_int_t nxt_runtime_listen_sockets_create(nxt_task_t *task, - nxt_runtime_t *rt); -nxt_int_t nxt_runtime_listen_sockets_enable(nxt_task_t *task, - nxt_runtime_t *rt); -nxt_file_t *nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name); +nxt_listen_socket_t * +nxt_runtime_listen_socket_add(nxt_runtime_t *rt, nxt_sockaddr_t *sa); +nxt_int_t +nxt_runtime_listen_sockets_create(nxt_task_t *task, nxt_runtime_t *rt); +nxt_int_t +nxt_runtime_listen_sockets_enable(nxt_task_t *task, nxt_runtime_t *rt); +nxt_file_t * +nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name); /* STUB */ -void nxt_cdecl nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, - const char *fmt, ...); +void nxt_cdecl +nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, ...); -void nxt_stream_connection_init(nxt_task_t *task, void *obj, void *data); +void +nxt_stream_connection_init(nxt_task_t *task, void *obj, void *data); -nxt_int_t nxt_http_register_variables(void); +nxt_int_t +nxt_http_register_variables(void); #if (NXT_HAVE_NJS) -void nxt_http_register_js_proto(nxt_js_conf_t *jcf); +void +nxt_http_register_js_proto(nxt_js_conf_t *jcf); #endif -#define nxt_runtime_process_each(rt, process) \ - do { \ - nxt_lvlhsh_each_t _lhe; \ - nxt_process_t *_nxt; \ - \ - for (process = nxt_runtime_process_first(rt, &_lhe); \ - process != NULL; \ - process = _nxt) { \ - \ - _nxt = nxt_runtime_process_next(rt, &_lhe); \ +#define nxt_runtime_process_each(rt, process) \ + do { \ + nxt_lvlhsh_each_t _lhe; \ + nxt_process_t *_nxt; \ + \ + for (process = nxt_runtime_process_first(rt, &_lhe); process != NULL; \ + process = _nxt) { \ + _nxt = nxt_runtime_process_next(rt, &_lhe); -#define nxt_runtime_process_loop \ - } \ - } while(0) +#define nxt_runtime_process_loop \ + } \ + } \ + while (0) -extern nxt_module_init_t nxt_init_modules[]; -extern nxt_uint_t nxt_init_modules_n; +extern nxt_module_init_t nxt_init_modules[]; +extern nxt_uint_t nxt_init_modules_n; #endif /* _NXT_RUNTIME_H_INCLIDED_ */ diff --git a/src/nxt_script.c b/src/nxt_script.c index 05d9561da..4bbe9fe76 100644 --- a/src/nxt_script.c +++ b/src/nxt_script.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Zhidao HONG @@ -9,50 +8,46 @@ #include #include - struct nxt_script_s { - nxt_str_t text; + nxt_str_t text; }; - typedef struct { nxt_str_t name; - nxt_conf_value_t *value; - nxt_mp_t *mp; + nxt_conf_value_t *value; + nxt_mp_t *mp; } nxt_script_info_t; - typedef struct { - nxt_str_t name; - nxt_fd_t fd; + nxt_str_t name; + nxt_fd_t fd; } nxt_script_item_t; +static nxt_script_t * +nxt_script_get(nxt_task_t *task, nxt_str_t *name, nxt_fd_t fd); +static nxt_conf_value_t * +nxt_script_details(nxt_mp_t *mp, nxt_script_t *cert); +static void +nxt_script_buf_completion(nxt_task_t *task, void *obj, void *data); -static nxt_script_t *nxt_script_get(nxt_task_t *task, nxt_str_t *name, - nxt_fd_t fd); -static nxt_conf_value_t *nxt_script_details(nxt_mp_t *mp, nxt_script_t *cert); -static void nxt_script_buf_completion(nxt_task_t *task, void *obj, void *data); - - -static nxt_lvlhsh_t nxt_script_info; +static nxt_lvlhsh_t nxt_script_info; nxt_script_t * nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, size_t size, - u_char *error) -{ - u_char *start; - njs_vm_t *vm; + u_char *error) { + u_char *start; + njs_vm_t *vm; njs_str_t mod_name; - njs_mod_t *mod; + njs_mod_t *mod; njs_vm_opt_t opts; - nxt_script_t *script; + nxt_script_t *script; njs_vm_opt_init(&opts); opts.backtrace = 1; - opts.file.start = (u_char *) "default"; + opts.file.start = (u_char *) "default"; opts.file.length = 7; vm = njs_vm_create(&opts); @@ -61,7 +56,7 @@ nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, size_t size, } mod_name.length = name->length; - mod_name.start = name->start; + mod_name.start = name->start; start = data; @@ -80,7 +75,7 @@ nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, size_t size, } script->text.length = size; - script->text.start = (u_char *) script + sizeof(nxt_script_t); + script->text.start = (u_char *) script + sizeof(nxt_script_t); nxt_memcpy(script->text.start, data, size); @@ -95,13 +90,11 @@ nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, size_t size, return NULL; } - static nxt_script_t * -nxt_script_get(nxt_task_t *task, nxt_str_t *name, nxt_fd_t fd) -{ +nxt_script_get(nxt_task_t *task, nxt_str_t *name, nxt_fd_t fd) { nxt_int_t ret; nxt_str_t text; - nxt_script_t *script; + nxt_script_t *script; u_char error[NXT_MAX_ERROR_STR]; ret = nxt_script_file_read(fd, &text); @@ -116,18 +109,14 @@ nxt_script_get(nxt_task_t *task, nxt_str_t *name, nxt_fd_t fd) return script; } - void -nxt_script_destroy(nxt_script_t *script) -{ +nxt_script_destroy(nxt_script_t *script) { nxt_free(script); } - static nxt_int_t -nxt_script_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_script_info_t *info; +nxt_script_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_script_info_t *info; info = data; @@ -138,23 +127,18 @@ nxt_script_info_hash_test(nxt_lvlhsh_query_t *lhq, void *data) return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t nxt_script_info_hash_proto - nxt_aligned(64) = -{ +static const nxt_lvlhsh_proto_t nxt_script_info_hash_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_script_info_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; - void -nxt_script_info_init(nxt_task_t *task, nxt_array_t *scripts) -{ +nxt_script_info_init(nxt_task_t *task, nxt_array_t *scripts) { uint32_t i; - nxt_script_t *script; - nxt_script_item_t *item; + nxt_script_t *script; + nxt_script_item_t *item; item = scripts->elts; @@ -173,15 +157,13 @@ nxt_script_info_init(nxt_task_t *task, nxt_array_t *scripts) } } - nxt_int_t -nxt_script_info_save(nxt_str_t *name, nxt_script_t *script) -{ - nxt_mp_t *mp; - nxt_int_t ret; - nxt_conf_value_t *value; - nxt_script_info_t *info; - nxt_lvlhsh_query_t lhq; +nxt_script_info_save(nxt_str_t *name, nxt_script_t *script) { + nxt_mp_t *mp; + nxt_int_t ret; + nxt_conf_value_t *value; + nxt_script_info_t *info; + nxt_lvlhsh_query_t lhq; mp = nxt_mp_create(1024, 128, 256, 32); if (nxt_slow_path(mp == NULL)) { @@ -203,14 +185,14 @@ nxt_script_info_save(nxt_str_t *name, nxt_script_t *script) goto fail; } - info->mp = mp; + info->mp = mp; info->value = value; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.replace = 1; - lhq.key = *name; - lhq.value = info; - lhq.proto = &nxt_script_info_hash_proto; + lhq.replace = 1; + lhq.key = *name; + lhq.value = info; + lhq.proto = &nxt_script_info_hash_proto; ret = nxt_lvlhsh_insert(&nxt_script_info, &lhq); if (nxt_slow_path(ret != NXT_OK)) { @@ -230,17 +212,15 @@ nxt_script_info_save(nxt_str_t *name, nxt_script_t *script) return NXT_ERROR; } - nxt_conf_value_t * -nxt_script_info_get(nxt_str_t *name) -{ - nxt_int_t ret; - nxt_script_info_t *info; - nxt_lvlhsh_query_t lhq; +nxt_script_info_get(nxt_str_t *name) { + nxt_int_t ret; + nxt_script_info_t *info; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_script_info_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_script_info_hash_proto; ret = nxt_lvlhsh_find(&nxt_script_info, &lhq); if (ret != NXT_OK) { @@ -252,13 +232,11 @@ nxt_script_info_get(nxt_str_t *name) return info->value; } - nxt_conf_value_t * -nxt_script_info_get_all(nxt_mp_t *mp) -{ +nxt_script_info_get_all(nxt_mp_t *mp) { uint32_t i; - nxt_conf_value_t *all; - nxt_script_info_t *info; + nxt_conf_value_t *all; + nxt_script_info_t *info; nxt_lvlhsh_each_t lhe; nxt_lvlhsh_each_init(&lhe, &nxt_script_info_hash_proto); @@ -291,11 +269,9 @@ nxt_script_info_get_all(nxt_mp_t *mp) return all; } - static nxt_conf_value_t * -nxt_script_details(nxt_mp_t *mp, nxt_script_t *script) -{ - nxt_conf_value_t *value; +nxt_script_details(nxt_mp_t *mp, nxt_script_t *script) { + nxt_conf_value_t *value; value = nxt_conf_create_object(mp, 0); if (nxt_slow_path(value == NULL)) { @@ -307,17 +283,15 @@ nxt_script_details(nxt_mp_t *mp, nxt_script_t *script) return value; } - nxt_int_t -nxt_script_info_delete(nxt_str_t *name) -{ - nxt_int_t ret; - nxt_script_info_t *info; - nxt_lvlhsh_query_t lhq; +nxt_script_info_delete(nxt_str_t *name) { + nxt_int_t ret; + nxt_script_info_t *info; + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_script_info_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_script_info_hash_proto; ret = nxt_lvlhsh_delete(&nxt_script_info, &lhq); @@ -329,20 +303,18 @@ nxt_script_info_delete(nxt_str_t *name) return ret; } - nxt_array_t * -nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) -{ - DIR *dir; +nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) { + DIR *dir; size_t size, alloc; - u_char *buf, *p; + u_char *buf, *p; nxt_str_t name; nxt_int_t ret; nxt_file_t file; - nxt_array_t *scripts; - nxt_runtime_t *rt; - struct dirent *de; - nxt_script_item_t *item; + nxt_array_t *scripts; + nxt_runtime_t *rt; + struct dirent *de; + nxt_script_item_t *item; rt = task->thread->runtime; @@ -356,27 +328,27 @@ nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) return NULL; } - buf = NULL; + buf = NULL; alloc = 0; dir = opendir((char *) rt->scripts.start); if (nxt_slow_path(dir == NULL)) { - nxt_alert(task, "opendir(\"%s\") failed %E", - rt->scripts.start, nxt_errno); + nxt_alert(task, "opendir(\"%s\") failed %E", rt->scripts.start, + nxt_errno); goto fail; } - for ( ;; ) { + for (;;) { de = readdir(dir); if (de == NULL) { break; } - nxt_debug(task, "readdir(\"%s\"): \"%s\"", - rt->scripts.start, de->d_name); + nxt_debug(task, "readdir(\"%s\"): \"%s\"", rt->scripts.start, + de->d_name); name.length = nxt_strlen(de->d_name); - name.start = (u_char *) de->d_name; + name.start = (u_char *) de->d_name; if (nxt_str_eq(&name, ".", 1) || nxt_str_eq(&name, "..", 2)) { continue; @@ -400,7 +372,7 @@ nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) } alloc = size; - buf = p; + buf = p; } p = nxt_cpymem(buf, rt->scripts.start, rt->scripts.length); @@ -411,7 +383,7 @@ nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) file.name = buf; ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); if (nxt_slow_path(ret != NXT_OK)) { nxt_array_remove_last(scripts); @@ -448,12 +420,10 @@ nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mp) return NULL; } - void -nxt_script_store_release(nxt_array_t *scripts) -{ +nxt_script_store_release(nxt_array_t *scripts) { uint32_t i; - nxt_script_item_t *item; + nxt_script_item_t *item; item = scripts->elts; @@ -464,16 +434,14 @@ nxt_script_store_release(nxt_array_t *scripts) nxt_array_destroy(scripts); } - void nxt_script_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, - nxt_port_rpc_handler_t handler, void *ctx) -{ + nxt_port_rpc_handler_t handler, void *ctx) { uint32_t stream; nxt_int_t ret; - nxt_buf_t *b; - nxt_port_t *main_port, *recv_port; - nxt_runtime_t *rt; + nxt_buf_t *b; + nxt_port_t *main_port, *recv_port; + nxt_runtime_t *rt; b = nxt_buf_mem_alloc(mp, name->length + 1, 0); if (nxt_slow_path(b == NULL)) { @@ -486,18 +454,18 @@ nxt_script_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, nxt_buf_cpystr(b, name); *b->mem.free++ = '\0'; - rt = task->thread->runtime; + rt = task->thread->runtime; main_port = rt->port_by_type[NXT_PROCESS_MAIN]; recv_port = rt->port_by_type[rt->type]; stream = nxt_port_rpc_register_handler(task, recv_port, handler, handler, - -1, ctx); + -1, ctx); if (nxt_slow_path(stream == 0)) { goto fail; } ret = nxt_port_socket_write(task, main_port, NXT_PORT_MSG_SCRIPT_GET, -1, - stream, recv_port->id, b); + stream, recv_port->id, b); if (nxt_slow_path(ret != NXT_OK)) { nxt_port_rpc_cancel(task, recv_port, stream); @@ -511,14 +479,12 @@ nxt_script_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, handler(task, NULL, ctx); } - static void -nxt_script_buf_completion(nxt_task_t *task, void *obj, void *data) -{ - nxt_mp_t *mp; - nxt_buf_t *b; +nxt_script_buf_completion(nxt_task_t *task, void *obj, void *data) { + nxt_mp_t *mp; + nxt_buf_t *b; - b = obj; + b = obj; mp = b->data; nxt_assert(b->next == NULL); @@ -526,39 +492,35 @@ nxt_script_buf_completion(nxt_task_t *task, void *obj, void *data) nxt_mp_release(mp); } - void -nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; - nxt_int_t ret; - nxt_str_t name; - nxt_file_t file; - nxt_port_t *port; - nxt_runtime_t *rt; - nxt_port_msg_type_t type; +nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; + nxt_int_t ret; + nxt_str_t name; + nxt_file_t file; + nxt_port_t *port; + nxt_runtime_t *rt; + nxt_port_msg_type_t type; port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid, - msg->port_msg.reply_port); + msg->port_msg.reply_port); if (nxt_slow_path(port == NULL)) { nxt_alert(task, "process port not found (pid %PI, reply_port %d)", - msg->port_msg.pid, msg->port_msg.reply_port); + msg->port_msg.pid, msg->port_msg.reply_port); return; } if (nxt_slow_path(port->type != NXT_PROCESS_CONTROLLER - && port->type != NXT_PROCESS_ROUTER)) - { - nxt_alert(task, "process %PI cannot store scripts", - msg->port_msg.pid); + && port->type != NXT_PROCESS_ROUTER)) { + nxt_alert(task, "process %PI cannot store scripts", msg->port_msg.pid); return; } nxt_memzero(&file, sizeof(nxt_file_t)); file.fd = -1; - type = NXT_PORT_MSG_RPC_ERROR; + type = NXT_PORT_MSG_RPC_ERROR; rt = task->thread->runtime; @@ -567,7 +529,7 @@ nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) goto error; } - name.start = msg->buf->mem.pos; + name.start = msg->buf->mem.pos; name.length = nxt_strlen(name.start); file.name = nxt_malloc(rt->scripts.length + name.length + 1); @@ -579,7 +541,7 @@ nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) p = nxt_cpymem(p, name.start, name.length + 1); ret = nxt_file_open(task, &file, NXT_FILE_RDWR, NXT_FILE_CREATE_OR_OPEN, - NXT_FILE_OWNER_ACCESS); + NXT_FILE_OWNER_ACCESS); nxt_free(file.name); @@ -590,16 +552,14 @@ nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) error: (void) nxt_port_socket_write(task, port, type, file.fd, - msg->port_msg.stream, 0, NULL); + msg->port_msg.stream, 0, NULL); } - void -nxt_script_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) -{ - nxt_buf_t *b; - nxt_port_t *main_port; - nxt_runtime_t *rt; +nxt_script_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) { + nxt_buf_t *b; + nxt_port_t *main_port; + nxt_runtime_t *rt; b = nxt_buf_mem_alloc(mp, name->length + 1, 0); @@ -607,25 +567,23 @@ nxt_script_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp) nxt_buf_cpystr(b, name); *b->mem.free++ = '\0'; - rt = task->thread->runtime; + rt = task->thread->runtime; main_port = rt->port_by_type[NXT_PROCESS_MAIN]; (void) nxt_port_socket_write(task, main_port, - NXT_PORT_MSG_SCRIPT_DELETE, -1, 0, 0, b); + NXT_PORT_MSG_SCRIPT_DELETE, -1, 0, 0, b); } } - void -nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ - u_char *p; +nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { + u_char *p; nxt_str_t name; - nxt_port_t *ctl_port; - nxt_runtime_t *rt; - nxt_file_name_t *path; + nxt_port_t *ctl_port; + nxt_runtime_t *rt; + nxt_file_name_t *path; - rt = task->thread->runtime; + rt = task->thread->runtime; ctl_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; if (nxt_slow_path(ctl_port == NULL)) { @@ -635,7 +593,7 @@ nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) if (nxt_slow_path(nxt_recv_msg_cmsg_pid(msg) != ctl_port->pid)) { nxt_alert(task, "process %PI cannot delete scripts", - nxt_recv_msg_cmsg_pid(msg)); + nxt_recv_msg_cmsg_pid(msg)); return; } @@ -644,7 +602,7 @@ nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) return; } - name.start = msg->buf->mem.pos; + name.start = msg->buf->mem.pos; name.length = nxt_strlen(name.start); path = nxt_malloc(rt->scripts.length + name.length + 1); @@ -659,14 +617,12 @@ nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) } } - nxt_int_t -nxt_script_file_read(nxt_fd_t fd, nxt_str_t *str) -{ - ssize_t n; - nxt_int_t ret; - nxt_file_t file; - nxt_file_info_t fi; +nxt_script_file_read(nxt_fd_t fd, nxt_str_t *str) { + ssize_t n; + nxt_int_t ret; + nxt_file_t file; + nxt_file_info_t fi; nxt_memzero(&file, sizeof(nxt_file_t)); @@ -683,7 +639,7 @@ nxt_script_file_read(nxt_fd_t fd, nxt_str_t *str) } str->length = nxt_file_size(&fi); - str->start = nxt_malloc(str->length); + str->start = nxt_malloc(str->length); if (nxt_slow_path(str->start == NULL)) { return NXT_ERROR; } diff --git a/src/nxt_script.h b/src/nxt_script.h index ffefc108f..b7c8f632e 100644 --- a/src/nxt_script.h +++ b/src/nxt_script.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Zhidao HONG @@ -8,30 +7,43 @@ #define _NXT_SCRIPT_INCLUDED_ -typedef struct nxt_script_s nxt_script_t; - -nxt_script_t *nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, - size_t size, u_char *error); -void nxt_script_destroy(nxt_script_t *script); - -void nxt_script_info_init(nxt_task_t *task, nxt_array_t *scripts); -nxt_int_t nxt_script_info_save(nxt_str_t *name, nxt_script_t *script); -nxt_conf_value_t *nxt_script_info_get(nxt_str_t *name); -nxt_conf_value_t *nxt_script_info_get_all(nxt_mp_t *mp); -nxt_int_t nxt_script_info_delete(nxt_str_t *name); - -nxt_array_t *nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mem_pool); -void nxt_script_store_release(nxt_array_t *scripts); - -void nxt_script_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, +typedef struct nxt_script_s nxt_script_t; + +nxt_script_t * +nxt_script_new(nxt_task_t *task, nxt_str_t *name, u_char *data, size_t size, + u_char *error); +void +nxt_script_destroy(nxt_script_t *script); + +void +nxt_script_info_init(nxt_task_t *task, nxt_array_t *scripts); +nxt_int_t +nxt_script_info_save(nxt_str_t *name, nxt_script_t *script); +nxt_conf_value_t * +nxt_script_info_get(nxt_str_t *name); +nxt_conf_value_t * +nxt_script_info_get_all(nxt_mp_t *mp); +nxt_int_t +nxt_script_info_delete(nxt_str_t *name); + +nxt_array_t * +nxt_script_store_load(nxt_task_t *task, nxt_mp_t *mem_pool); +void +nxt_script_store_release(nxt_array_t *scripts); + +void +nxt_script_store_get(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp, nxt_port_rpc_handler_t handler, void *ctx); -void nxt_script_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp); +void +nxt_script_store_delete(nxt_task_t *task, nxt_str_t *name, nxt_mp_t *mp); -void nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -void nxt_script_store_delete_handler(nxt_task_t *task, - nxt_port_recv_msg_t *msg); +void +nxt_script_store_get_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); +void +nxt_script_store_delete_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg); -nxt_int_t nxt_script_file_read(nxt_fd_t fd, nxt_str_t *str); +nxt_int_t +nxt_script_file_read(nxt_fd_t fd, nxt_str_t *str); #endif /* _NXT_SCRIPT_INCLUDED_ */ diff --git a/src/nxt_select_engine.c b/src/nxt_select_engine.c index 6f7600125..53642b523 100644 --- a/src/nxt_select_engine.c +++ b/src/nxt_select_engine.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,34 +6,40 @@ #include -static nxt_int_t nxt_select_create(nxt_event_engine_t *engine, - nxt_uint_t mchanges, nxt_uint_t mevents); -static void nxt_select_free(nxt_event_engine_t *engine); -static void nxt_select_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static void nxt_select_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); -static nxt_bool_t nxt_select_close(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_enable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_enable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_error_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_select_disable_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_disable_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_block_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_block_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_oneshot_read(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_oneshot_write(nxt_event_engine_t *engine, - nxt_fd_event_t *ev); -static void nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); - - -const nxt_event_interface_t nxt_select_engine = { +static nxt_int_t +nxt_select_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, + nxt_uint_t mevents); +static void +nxt_select_free(nxt_event_engine_t *engine); +static void +nxt_select_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static nxt_bool_t +nxt_select_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_error_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_select_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev); +static void +nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout); + + +const nxt_event_interface_t nxt_select_engine = { "select", nxt_select_create, nxt_select_free, @@ -63,12 +68,10 @@ const nxt_event_interface_t nxt_select_engine = { NXT_NO_SIGNAL_EVENTS, }; - static nxt_int_t nxt_select_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, - nxt_uint_t mevents) -{ - engine->u.select.nfds = -1; + nxt_uint_t mevents) { + engine->u.select.nfds = -1; engine->u.select.update_nfds = 0; engine->u.select.events = nxt_zalloc(FD_SETSIZE * sizeof(nxt_fd_event_t *)); @@ -82,10 +85,8 @@ nxt_select_create(nxt_event_engine_t *engine, nxt_uint_t mchanges, return NXT_ERROR; } - static void -nxt_select_free(nxt_event_engine_t *engine) -{ +nxt_select_free(nxt_event_engine_t *engine) { nxt_debug(&engine->task, "select free"); nxt_free(engine->u.select.events); @@ -93,18 +94,14 @@ nxt_select_free(nxt_event_engine_t *engine) nxt_memzero(&engine->u.select, sizeof(nxt_select_engine_t)); } - static void -nxt_select_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_enable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_select_enable_read(engine, ev); nxt_select_enable_write(engine, ev); } - static void -nxt_select_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { nxt_select_disable_read(engine, ev); } @@ -114,20 +111,16 @@ nxt_select_disable(nxt_event_engine_t *engine, nxt_fd_event_t *ev) } } - static nxt_bool_t -nxt_select_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_close(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_select_disable(engine, ev); return 0; } - static void -nxt_select_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_fd_t fd; +nxt_select_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_fd_t fd; fd = ev->fd; @@ -135,7 +128,7 @@ nxt_select_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) if (fd < 0 || fd >= (nxt_fd_t) FD_SETSIZE) { nxt_work_queue_add(&engine->fast_work_queue, nxt_select_error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); return; } @@ -145,16 +138,14 @@ nxt_select_enable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) engine->u.select.events[fd] = ev; if (engine->u.select.nfds < fd) { - engine->u.select.nfds = fd; + engine->u.select.nfds = fd; engine->u.select.update_nfds = 0; } } - static void -nxt_select_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_fd_t fd; +nxt_select_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_fd_t fd; fd = ev->fd; @@ -162,7 +153,7 @@ nxt_select_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) if (fd < 0 || fd >= (nxt_fd_t) FD_SETSIZE) { nxt_work_queue_add(&engine->fast_work_queue, nxt_select_error_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); return; } @@ -172,30 +163,26 @@ nxt_select_enable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) engine->u.select.events[fd] = ev; if (engine->u.select.nfds < fd) { - engine->u.select.nfds = fd; + engine->u.select.nfds = fd; engine->u.select.update_nfds = 0; } } - static void -nxt_select_error_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_fd_event_t *ev; +nxt_select_error_handler(nxt_task_t *task, void *obj, void *data) { + nxt_fd_event_t *ev; ev = obj; - ev->read = NXT_EVENT_INACTIVE; + ev->read = NXT_EVENT_INACTIVE; ev->write = NXT_EVENT_INACTIVE; ev->error_handler(task, ev, data); } - static void -nxt_select_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_fd_t fd; +nxt_select_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_fd_t fd; fd = ev->fd; @@ -210,16 +197,14 @@ nxt_select_disable_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->read = NXT_EVENT_INACTIVE; if (ev->write == NXT_EVENT_INACTIVE) { - engine->u.select.events[fd] = NULL; + engine->u.select.events[fd] = NULL; engine->u.select.update_nfds = 1; } } - static void -nxt_select_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ - nxt_fd_t fd; +nxt_select_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { + nxt_fd_t fd; fd = ev->fd; @@ -234,78 +219,68 @@ nxt_select_disable_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) ev->write = NXT_EVENT_INACTIVE; if (ev->read == NXT_EVENT_INACTIVE) { - engine->u.select.events[fd] = NULL; + engine->u.select.events[fd] = NULL; engine->u.select.update_nfds = 1; } } - static void -nxt_select_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_block_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->read != NXT_EVENT_INACTIVE) { nxt_select_disable_read(engine, ev); } } - static void -nxt_select_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_block_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { if (ev->write != NXT_EVENT_INACTIVE) { nxt_select_disable_write(engine, ev); } } - static void -nxt_select_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_oneshot_read(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_select_enable_read(engine, ev); ev->read = NXT_EVENT_ONESHOT; } - static void -nxt_select_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) -{ +nxt_select_oneshot_write(nxt_event_engine_t *engine, nxt_fd_event_t *ev) { nxt_select_enable_write(engine, ev); ev->write = NXT_EVENT_ONESHOT; } - static void -nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) -{ +nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) { int nevents, nfds, found; nxt_err_t err; nxt_int_t i; nxt_uint_t fd, level; - nxt_fd_event_t *ev; + nxt_fd_event_t *ev; struct timeval tv, *tp; if (timeout == NXT_INFINITE_MSEC) { tp = NULL; } else { - tv.tv_sec = (long) (timeout / 1000); + tv.tv_sec = (long) (timeout / 1000); tv.tv_usec = (long) ((timeout % 1000) * 1000); - tp = &tv; + tp = &tv; } if (engine->u.select.update_nfds) { for (i = engine->u.select.nfds; i >= 0; i--) { if (engine->u.select.events[i] != NULL) { - engine->u.select.nfds = i; + engine->u.select.nfds = i; engine->u.select.update_nfds = 0; break; } } } - engine->u.select.work_read_fd_set = engine->u.select.main_read_fd_set; + engine->u.select.work_read_fd_set = engine->u.select.main_read_fd_set; engine->u.select.work_write_fd_set = engine->u.select.main_write_fd_set; nfds = engine->u.select.nfds + 1; @@ -313,7 +288,7 @@ nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) nxt_debug(&engine->task, "select() nfds:%d timeout:%M", nfds, timeout); nevents = select(nfds, &engine->u.select.work_read_fd_set, - &engine->u.select.work_write_fd_set, NULL, tp); + &engine->u.select.work_write_fd_set, NULL, tp); err = (nevents == -1) ? nxt_errno : 0; @@ -328,14 +303,13 @@ nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } for (fd = 0; fd < (nxt_uint_t) nfds && nevents != 0; fd++) { - found = 0; if (FD_ISSET(fd, &engine->u.select.work_read_fd_set)) { ev = engine->u.select.events[fd]; - nxt_debug(ev->task, "select() fd:%ui read rd:%d wr:%d", - fd, ev->read, ev->write); + nxt_debug(ev->task, "select() fd:%ui read rd:%d wr:%d", fd, + ev->read, ev->write); ev->read_ready = 1; @@ -343,16 +317,16 @@ nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) nxt_select_disable_read(engine, ev); } - nxt_work_queue_add(ev->read_work_queue, ev->read_handler, - ev->task, ev, ev->data); + nxt_work_queue_add(ev->read_work_queue, ev->read_handler, ev->task, + ev, ev->data); found = 1; } if (FD_ISSET(fd, &engine->u.select.work_write_fd_set)) { ev = engine->u.select.events[fd]; - nxt_debug(ev->task, "select() fd:%ui write rd:%d wr:%d", - fd, ev->read, ev->write); + nxt_debug(ev->task, "select() fd:%ui write rd:%d wr:%d", fd, + ev->read, ev->write); ev->write_ready = 1; @@ -361,7 +335,7 @@ nxt_select_poll(nxt_event_engine_t *engine, nxt_msec_t timeout) } nxt_work_queue_add(ev->write_work_queue, ev->write_handler, - ev->task, ev, ev->data); + ev->task, ev, ev->data); found = 1; } diff --git a/src/nxt_semaphore.c b/src/nxt_semaphore.c index ad05d8b9a..dfdaf553d 100644 --- a/src/nxt_semaphore.c +++ b/src/nxt_semaphore.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -32,8 +31,7 @@ */ nxt_int_t -nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) -{ +nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) { if (sem_init(sem, 0, count) == 0) { nxt_thread_log_debug("sem_init(%p)", sem); return NXT_OK; @@ -43,10 +41,8 @@ nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) return NXT_ERROR; } - void -nxt_sem_destroy(nxt_sem_t *sem) -{ +nxt_sem_destroy(nxt_sem_t *sem) { if (sem_destroy(sem) == 0) { nxt_thread_log_debug("sem_destroy(%p)", sem); return; @@ -55,10 +51,8 @@ nxt_sem_destroy(nxt_sem_t *sem) nxt_thread_log_alert("sem_destroy(%p) failed %E", sem, nxt_errno); } - nxt_int_t -nxt_sem_post(nxt_sem_t *sem) -{ +nxt_sem_post(nxt_sem_t *sem) { nxt_thread_log_debug("sem_post(%p)", sem); if (nxt_fast_path(sem_post(sem) == 0)) { @@ -70,23 +64,21 @@ nxt_sem_post(nxt_sem_t *sem) return NXT_ERROR; } - nxt_err_t -nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) -{ - int n; - nxt_err_t err; - nxt_nsec_t ns; - nxt_thread_t *thr; - nxt_realtime_t *now; - struct timespec ts; +nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) { + int n; + nxt_err_t err; + nxt_nsec_t ns; + nxt_thread_t *thr; + nxt_realtime_t *now; + struct timespec ts; thr = nxt_thread(); if (timeout == NXT_INFINITE_NSEC) { nxt_log_debug(thr->log, "sem_wait(%p) enter", sem); - for ( ;; ) { + for (;;) { n = sem_wait(sem); err = nxt_errno; @@ -99,10 +91,9 @@ nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) } switch (err) { - case NXT_EINTR: nxt_log_error(NXT_LOG_INFO, thr->log, "sem_wait(%p) failed %E", - sem, err); + sem, err); continue; default: @@ -129,12 +120,12 @@ nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) nxt_log_debug(thr->log, "sem_timedwait(%p, %N) enter", sem, timeout); - now = nxt_thread_realtime(thr); - ns = now->nsec + timeout; - ts.tv_sec = now->sec + ns / 1000000000; + now = nxt_thread_realtime(thr); + ns = now->nsec + timeout; + ts.tv_sec = now->sec + ns / 1000000000; ts.tv_nsec = ns % 1000000000; - for ( ;; ) { + for (;;) { n = sem_timedwait(sem, &ts); err = nxt_errno; @@ -147,14 +138,13 @@ nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) } switch (err) { - case NXT_ETIMEDOUT: nxt_log_debug(thr->log, "sem_timedwait(%p) exit: %d", sem, err); return err; case NXT_EINTR: nxt_log_error(NXT_LOG_INFO, thr->log, "sem_timedwait(%p) failed %E", - sem, err); + sem, err); continue; default: @@ -169,10 +159,8 @@ nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) /* Semaphore implementation using pthread conditional variable. */ nxt_int_t -nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) -{ +nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) { if (nxt_thread_mutex_create(&sem->mutex) == NXT_OK) { - if (nxt_thread_cond_create(&sem->cond) == NXT_OK) { sem->count = count; return NXT_OK; @@ -184,19 +172,15 @@ nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count) return NXT_ERROR; } - void -nxt_sem_destroy(nxt_sem_t *sem) -{ +nxt_sem_destroy(nxt_sem_t *sem) { nxt_thread_cond_destroy(&sem->cond); nxt_thread_mutex_destroy(&sem->mutex); } - nxt_int_t -nxt_sem_post(nxt_sem_t *sem) -{ - nxt_int_t ret; +nxt_sem_post(nxt_sem_t *sem) { + nxt_int_t ret; if (nxt_slow_path(nxt_thread_mutex_lock(&sem->mutex) != NXT_OK)) { return NXT_ERROR; @@ -211,11 +195,9 @@ nxt_sem_post(nxt_sem_t *sem) return (nxt_thread_mutex_unlock(&sem->mutex) | ret); } - nxt_err_t -nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) -{ - nxt_err_t err; +nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) { + nxt_err_t err; err = 0; @@ -224,7 +206,6 @@ nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout) } while (sem->count == 0) { - err = nxt_thread_cond_wait(&sem->cond, &sem->mutex, timeout); if (err != 0) { diff --git a/src/nxt_semaphore.h b/src/nxt_semaphore.h index d1985342c..7b285178f 100644 --- a/src/nxt_semaphore.h +++ b/src/nxt_semaphore.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -10,23 +9,27 @@ #if (NXT_HAVE_SEM_TIMEDWAIT) -typedef sem_t nxt_sem_t; +typedef sem_t nxt_sem_t; #else typedef struct { - nxt_atomic_t count; - nxt_thread_mutex_t mutex; - nxt_thread_cond_t cond; + nxt_atomic_t count; + nxt_thread_mutex_t mutex; + nxt_thread_cond_t cond; } nxt_sem_t; #endif -NXT_EXPORT nxt_int_t nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count); -NXT_EXPORT void nxt_sem_destroy(nxt_sem_t *sem); -NXT_EXPORT nxt_int_t nxt_sem_post(nxt_sem_t *sem); -NXT_EXPORT nxt_err_t nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout); +NXT_EXPORT nxt_int_t +nxt_sem_init(nxt_sem_t *sem, nxt_uint_t count); +NXT_EXPORT void +nxt_sem_destroy(nxt_sem_t *sem); +NXT_EXPORT nxt_int_t +nxt_sem_post(nxt_sem_t *sem); +NXT_EXPORT nxt_err_t +nxt_sem_wait(nxt_sem_t *sem, nxt_nsec_t timeout); #endif /* _NXT_UNIX_SEMAPHORE_H_INCLUDED_ */ diff --git a/src/nxt_sendbuf.c b/src/nxt_sendbuf.c index 94f8e9eb9..91d6785e9 100644 --- a/src/nxt_sendbuf.c +++ b/src/nxt_sendbuf.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,27 +6,25 @@ #include -static nxt_bool_t nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, - size_t *copied); -static nxt_buf_t *nxt_sendbuf_coalesce_completion(nxt_task_t *task, - nxt_work_queue_t *wq, nxt_buf_t *start); - +static nxt_bool_t +nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, size_t *copied); +static nxt_buf_t * +nxt_sendbuf_coalesce_completion(nxt_task_t *task, nxt_work_queue_t *wq, + nxt_buf_t *start); nxt_uint_t nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, - struct iovec *iov, nxt_uint_t niov_max) -{ - u_char *last; - size_t size, total; - nxt_buf_t *b; - nxt_uint_t n; + struct iovec *iov, nxt_uint_t niov_max) { + u_char *last; + size_t size, total; + nxt_buf_t *b; + nxt_uint_t n; total = sb->size; - last = NULL; - n = (nxt_uint_t) -1; + last = NULL; + n = (nxt_uint_t) -1; for (b = sb->buf; b != NULL && total < sb->limit; b = b->next) { - nxt_prefetch(b->next); if (nxt_buf_is_file(b)) { @@ -35,11 +32,9 @@ nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, } if (nxt_buf_is_mem(b)) { - size = b->mem.free - b->mem.pos; if (size != 0) { - if (total + size > sb->limit) { size = sb->limit - total; @@ -49,27 +44,26 @@ nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, } if (b->mem.pos != last) { - if (++n >= niov_max) { goto done; } iov[n].iov_base = b->mem.pos; - iov[n].iov_len = size; + iov[n].iov_len = size; } else { iov[n].iov_len += size; } - nxt_debug(task, "sendbuf: %ui, %p, %uz", - n, iov[n].iov_base, iov[n].iov_len); + nxt_debug(task, "sendbuf: %ui, %p, %uz", n, iov[n].iov_base, + iov[n].iov_len); total += size; - last = b->mem.pos + size; + last = b->mem.pos + size; } } else { - sb->sync = 1; + sb->sync = 1; sb->last |= nxt_buf_is_last(b); } } @@ -83,21 +77,18 @@ nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, return n; } - nxt_uint_t -nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) -{ - u_char *last; - size_t size, total; - nxt_buf_t *b; - nxt_uint_t n; +nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) { + u_char *last; + size_t size, total; + nxt_buf_t *b; + nxt_uint_t n; total = sb->size; - last = NULL; - n = (nxt_uint_t) -1; + last = NULL; + n = (nxt_uint_t) -1; for (b = sb->buf; b != NULL && total < sb->limit; b = b->next) { - nxt_prefetch(b->next); if (nxt_buf_is_file(b)) { @@ -105,11 +96,9 @@ nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) } if (nxt_buf_is_mem(b)) { - size = b->mem.free - b->mem.pos; if (size != 0) { - if (total + size > sb->limit) { size = sb->limit - total; @@ -121,7 +110,6 @@ nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) } if (b->mem.pos != last) { - if (++n >= sb->nmax) { sb->nmax_reached = 1; @@ -129,21 +117,21 @@ nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) } sb->iobuf[n].iov_base = b->mem.pos; - sb->iobuf[n].iov_len = size; + sb->iobuf[n].iov_len = size; } else { sb->iobuf[n].iov_len += size; } - nxt_debug(task, "sendbuf: %ui, %p, %uz", - n, sb->iobuf[n].iov_base, sb->iobuf[n].iov_len); + nxt_debug(task, "sendbuf: %ui, %p, %uz", n, + sb->iobuf[n].iov_base, sb->iobuf[n].iov_len); total += size; - last = b->mem.pos + size; + last = b->mem.pos + size; } } else { - sb->sync = 1; + sb->sync = 1; sb->last |= nxt_buf_is_last(b); } } @@ -152,29 +140,26 @@ nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb) done: - sb->buf = b; + sb->buf = b; sb->size = total; sb->niov = n; return n; } - size_t -nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb) -{ +nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb) { size_t file_start, total; nxt_fd_t fd; nxt_off_t size, last; - nxt_buf_t *b; + nxt_buf_t *b; - b = sb->buf; + b = sb->buf; fd = b->file->fd; total = sb->size; - for ( ;; ) { - + for (;;) { nxt_prefetch(b->next); size = b->file_end - b->file_pos; @@ -185,7 +170,7 @@ nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb) } total += size; - last = b->file_pos + size; + last = b->file_pos + size; b = b->next; @@ -201,25 +186,22 @@ nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb) sb->buf = b; file_start = sb->size; - sb->size = total; + sb->size = total; return total - file_start; } - ssize_t nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, nxt_buf_t *b, - size_t limit) -{ - size_t size, bsize, copied; - ssize_t n; - nxt_bool_t flush; + size_t limit) { + size_t size, bsize, copied; + ssize_t n; + nxt_bool_t flush; - size = nxt_buf_mem_used_size(&b->mem); + size = nxt_buf_mem_used_size(&b->mem); bsize = nxt_buf_mem_size(bm); if (bsize != 0) { - if (size > bsize && bm->pos == bm->free) { /* * A data buffer size is larger than the internal @@ -234,9 +216,9 @@ nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, nxt_buf_t *b, return NXT_ERROR; } - bm->start = bm->pos; - bm->free = bm->pos; - bm->end += (uintptr_t) bm->pos; + bm->start = bm->pos; + bm->free = bm->pos; + bm->end += (uintptr_t) bm->pos; } copied = 0; @@ -263,7 +245,7 @@ nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, nxt_buf_t *b, bm->pos += n; if (bm->pos == bm->free) { - bm->pos = bm->start; + bm->pos = bm->start; bm->free = bm->start; } @@ -290,12 +272,10 @@ nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, nxt_buf_t *b, return 0; } - static nxt_bool_t -nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, size_t *copied) -{ - size_t size, bsize; - nxt_bool_t flush; +nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, size_t *copied) { + size_t size, bsize; + nxt_bool_t flush; flush = 0; @@ -304,12 +284,12 @@ nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, size_t *copied) if (nxt_buf_is_mem(b)) { bsize = bm->end - bm->free; - size = b->mem.free - b->mem.pos; - size = nxt_min(size, bsize); + size = b->mem.free - b->mem.pos; + size = nxt_min(size, bsize); nxt_memcpy(bm->free, b->mem.pos, size); - *copied += size; + *copied += size; bm->free += size; if (bm->free == bm->end) { @@ -326,28 +306,22 @@ nxt_sendbuf_copy(nxt_buf_mem_t *bm, nxt_buf_t *b, size_t *copied) return flush; } - nxt_buf_t * -nxt_sendbuf_update(nxt_buf_t *b, size_t sent) -{ - size_t size; +nxt_sendbuf_update(nxt_buf_t *b, size_t sent) { + size_t size; while (b != NULL) { - nxt_prefetch(b->next); if (!nxt_buf_is_sync(b)) { - size = nxt_buf_used_size(b); if (size != 0) { - if (sent == 0) { break; } if (sent < size) { - if (nxt_buf_is_mem(b)) { b->mem.pos += sent; } @@ -376,12 +350,9 @@ nxt_sendbuf_update(nxt_buf_t *b, size_t sent) return b; } - nxt_buf_t * -nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b) -{ +nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b) { while (b != NULL) { - if (!nxt_buf_is_sync(b) && nxt_buf_used_size(b) != 0) { break; } @@ -392,37 +363,33 @@ nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b) return b; } - void -nxt_sendbuf_drain(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b) -{ +nxt_sendbuf_drain(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b) { while (b != NULL) { b = nxt_sendbuf_coalesce_completion(task, wq, b); } } - static nxt_buf_t * nxt_sendbuf_coalesce_completion(nxt_task_t *task, nxt_work_queue_t *wq, - nxt_buf_t *start) -{ - nxt_buf_t *b, *next, **last, *rest, **last_rest; - nxt_work_handler_t handler; + nxt_buf_t *start) { + nxt_buf_t *b, *next, **last, *rest, **last_rest; + nxt_work_handler_t handler; - rest = NULL; + rest = NULL; last_rest = &rest; - last = &start->next; - b = start; - handler = b->completion_handler; + last = &start->next; + b = start; + handler = b->completion_handler; - for ( ;; ) { + for (;;) { next = b->next; if (next == NULL) { break; } b->next = NULL; - b = next; + b = next; if (!nxt_buf_is_sync(b) && nxt_buf_used_size(b) != 0) { *last_rest = b; @@ -431,11 +398,11 @@ nxt_sendbuf_coalesce_completion(nxt_task_t *task, nxt_work_queue_t *wq, if (handler == b->completion_handler) { *last = b; - last = &b->next; + last = &b->next; } else { *last_rest = b; - last_rest = &b->next; + last_rest = &b->next; } } diff --git a/src/nxt_sendbuf.h b/src/nxt_sendbuf.h index fcbe1a25b..cc9f831ac 100644 --- a/src/nxt_sendbuf.h +++ b/src/nxt_sendbuf.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_SENDBUF_H_INCLUDED_ #define _NXT_SENDBUF_H_INCLUDED_ - /* * The sendbuf interface is intended to send a buffer chain to a connection. * It uses sendfile interface if available. Otherwise it can send only @@ -36,82 +34,83 @@ typedef struct { - nxt_buf_t *buf; - void *tls; - nxt_socket_t socket; - nxt_err_t error; - nxt_off_t sent; - size_t size; - size_t limit; - - uint8_t ready; /* 1 bit */ - uint8_t once; /* 1 bit */ - uint8_t sync; /* 1 bit */ - uint8_t last; /* 1 bit */ -} nxt_sendbuf_t; + nxt_buf_t *buf; + void *tls; + nxt_socket_t socket; + nxt_err_t error; + nxt_off_t sent; + size_t size; + size_t limit; + uint8_t ready; /* 1 bit */ + uint8_t once; /* 1 bit */ + uint8_t sync; /* 1 bit */ + uint8_t last; /* 1 bit */ +} nxt_sendbuf_t; typedef struct { - nxt_buf_t *buf; - nxt_iobuf_t *iobuf; + nxt_buf_t *buf; + nxt_iobuf_t *iobuf; nxt_uint_t niov; - uint32_t nmax; - uint8_t sync; /* 1 bit */ - uint8_t last; /* 1 bit */ - uint8_t limit_reached; - uint8_t nmax_reached; + uint32_t nmax; + uint8_t sync; /* 1 bit */ + uint8_t last; /* 1 bit */ + uint8_t limit_reached; + uint8_t nmax_reached; - size_t size; - size_t limit; + size_t size; + size_t limit; } nxt_sendbuf_coalesce_t; #if (NXT_HAVE_LINUX_SENDFILE) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_linux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_linux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif #if (NXT_HAVE_FREEBSD_SENDFILE) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_freebsd_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_freebsd_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif #if (NXT_HAVE_SOLARIS_SENDFILEV) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_solaris_event_conn_io_sendfilev(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_solaris_event_conn_io_sendfilev(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif #if (NXT_HAVE_MACOSX_SENDFILE) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_macosx_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif #if (NXT_HAVE_AIX_SEND_FILE) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_aix_event_conn_io_send_file(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_aix_event_conn_io_send_file(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif #if (NXT_HAVE_HPUX_SENDFILE) -#define NXT_HAVE_SENDFILE 1 -ssize_t nxt_hpux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +#define NXT_HAVE_SENDFILE 1 +ssize_t +nxt_hpux_event_conn_io_sendfile(nxt_conn_t *c, nxt_buf_t *b, size_t limit); #endif -ssize_t nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, - size_t limit); +ssize_t +nxt_event_conn_io_sendbuf(nxt_conn_t *c, nxt_buf_t *b, size_t limit); -nxt_uint_t nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, +nxt_uint_t +nxt_sendbuf_mem_coalesce0(nxt_task_t *task, nxt_sendbuf_t *sb, struct iovec *iov, nxt_uint_t niov_max); -nxt_uint_t nxt_sendbuf_mem_coalesce(nxt_task_t *task, - nxt_sendbuf_coalesce_t *sb); -size_t nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb); +nxt_uint_t +nxt_sendbuf_mem_coalesce(nxt_task_t *task, nxt_sendbuf_coalesce_t *sb); +size_t +nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb); /* * Auxiliary nxt_sendbuf_copy_coalesce() interface copies small memory @@ -119,13 +118,16 @@ size_t nxt_sendbuf_file_coalesce(nxt_sendbuf_coalesce_t *sb); * SSL/TLS libraries which lack vector I/O interface yet add noticeable * overhead to each SSL/TLS record. */ -ssize_t nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, - nxt_buf_t *b, size_t limit); +ssize_t +nxt_sendbuf_copy_coalesce(nxt_conn_t *c, nxt_buf_mem_t *bm, nxt_buf_t *b, + size_t limit); -nxt_buf_t *nxt_sendbuf_update(nxt_buf_t *b, size_t sent); -nxt_buf_t *nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, - nxt_buf_t *b); -void nxt_sendbuf_drain(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b); +nxt_buf_t * +nxt_sendbuf_update(nxt_buf_t *b, size_t sent); +nxt_buf_t * +nxt_sendbuf_completion(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b); +void +nxt_sendbuf_drain(nxt_task_t *task, nxt_work_queue_t *wq, nxt_buf_t *b); #endif /* _NXT_SENDBUF_H_INCLUDED_ */ diff --git a/src/nxt_service.c b/src/nxt_service.c index cc9f8d894..a51d5ec96 100644 --- a/src/nxt_service.c +++ b/src/nxt_service.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,70 +6,68 @@ #include -static const nxt_service_t nxt_services[] = { +static const nxt_service_t nxt_services[] = { #if (NXT_HAVE_KQUEUE) - { "engine", "kqueue", &nxt_kqueue_engine }, + {"engine", "kqueue", &nxt_kqueue_engine}, #endif #if (NXT_HAVE_EPOLL_EDGE) - { "engine", "epoll", &nxt_epoll_edge_engine }, - { "engine", "epoll_edge", &nxt_epoll_edge_engine }, - { "engine", "epoll_level", &nxt_epoll_level_engine }, + {"engine", "epoll", &nxt_epoll_edge_engine}, + {"engine", "epoll_edge", &nxt_epoll_edge_engine}, + {"engine", "epoll_level", &nxt_epoll_level_engine}, #elif (NXT_HAVE_EPOLL) - { "engine", "epoll", &nxt_epoll_level_engine }, - { "engine", "epoll_level", &nxt_epoll_level_engine }, + {"engine", "epoll", &nxt_epoll_level_engine}, + {"engine", "epoll_level", &nxt_epoll_level_engine}, #endif #if (NXT_HAVE_EVENTPORT) - { "engine", "eventport", &nxt_eventport_engine }, + {"engine", "eventport", &nxt_eventport_engine}, #endif #if (NXT_HAVE_DEVPOLL) - { "engine", "devpoll", &nxt_devpoll_engine }, - { "engine", "/dev/poll", &nxt_devpoll_engine }, + {"engine", "devpoll", &nxt_devpoll_engine}, + {"engine", "/dev/poll", &nxt_devpoll_engine}, #endif #if (NXT_HAVE_POLLSET) - { "engine", "pollset", &nxt_pollset_engine }, + {"engine", "pollset", &nxt_pollset_engine}, #endif - { "engine", "poll", &nxt_poll_engine }, - { "engine", "select", &nxt_select_engine }, + {"engine", "poll", &nxt_poll_engine}, + {"engine", "select", &nxt_select_engine}, #if (NXT_HAVE_OPENSSL) - { "SSL/TLS", "OpenSSL", &nxt_openssl_lib }, - { "SSL/TLS", "openssl", &nxt_openssl_lib }, + {"SSL/TLS", "OpenSSL", &nxt_openssl_lib}, + {"SSL/TLS", "openssl", &nxt_openssl_lib}, #endif #if (NXT_HAVE_GNUTLS) - { "SSL/TLS", "GnuTLS", &nxt_gnutls_lib }, - { "SSL/TLS", "gnutls", &nxt_gnutls_lib }, + {"SSL/TLS", "GnuTLS", &nxt_gnutls_lib}, + {"SSL/TLS", "gnutls", &nxt_gnutls_lib}, #endif #if (NXT_HAVE_CYASSL) - { "SSL/TLS", "CyaSSL", &nxt_cyassl_lib }, - { "SSL/TLS", "cyassl", &nxt_cyassl_lib }, + {"SSL/TLS", "CyaSSL", &nxt_cyassl_lib}, + {"SSL/TLS", "cyassl", &nxt_cyassl_lib}, #endif }; nxt_array_t * -nxt_services_init(nxt_mp_t *mp) -{ +nxt_services_init(nxt_mp_t *mp) { nxt_uint_t n; - nxt_array_t *services; - nxt_service_t *s; - const nxt_service_t *service; + nxt_array_t *services; + nxt_service_t *s; + const nxt_service_t *service; services = nxt_array_create(mp, 32, sizeof(nxt_service_t)); if (nxt_fast_path(services != NULL)) { - service = nxt_services; - n = nxt_nitems(nxt_services); + n = nxt_nitems(nxt_services); while (n != 0) { s = nxt_array_add(services); @@ -88,12 +85,10 @@ nxt_services_init(nxt_mp_t *mp) return services; } - nxt_int_t -nxt_service_add(nxt_array_t *services, const nxt_service_t *service) -{ +nxt_service_add(nxt_array_t *services, const nxt_service_t *service) { nxt_uint_t n; - nxt_service_t *s; + nxt_service_t *s; s = services->elts; n = services->nelts; @@ -107,8 +102,8 @@ nxt_service_add(nxt_array_t *services, const nxt_service_t *service) goto next; } - nxt_thread_log_alert("service \"%s:%s\" is duplicate", - service->type, service->name); + nxt_thread_log_alert("service \"%s:%s\" is duplicate", service->type, + service->name); return NXT_ERROR; next: @@ -126,12 +121,10 @@ nxt_service_add(nxt_array_t *services, const nxt_service_t *service) return NXT_ERROR; } - const void * -nxt_service_get(nxt_array_t *services, const char *type, const char *name) -{ +nxt_service_get(nxt_array_t *services, const char *type, const char *name) { nxt_uint_t n; - const nxt_service_t *s; + const nxt_service_t *s; if (services != NULL) { s = services->elts; @@ -144,7 +137,6 @@ nxt_service_get(nxt_array_t *services, const char *type, const char *name) while (n != 0) { if (nxt_strcmp(s->type, type) == 0) { - if (name == NULL) { return s->service; } @@ -158,8 +150,8 @@ nxt_service_get(nxt_array_t *services, const char *type, const char *name) n--; } - nxt_thread_log_alert("service \"%s%s%s\" not found", - type, (name != NULL) ? ":" : "", name); + nxt_thread_log_alert("service \"%s%s%s\" not found", type, + (name != NULL) ? ":" : "", name); return NULL; } diff --git a/src/nxt_service.h b/src/nxt_service.h index c43d5394f..e83992ff3 100644 --- a/src/nxt_service.h +++ b/src/nxt_service.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,23 +6,21 @@ #ifndef _NXT_SERVICE_H_INCLUDED_ #define _NXT_SERVICE_H_INCLUDED_ - typedef struct { - const char *type; - const char *name; - const void *service; + const char *type; + const char *name; + const void *service; } nxt_service_t; - -#define nxt_service_is_module(s) \ - ((s)->type == NULL) +#define nxt_service_is_module(s) ((s)->type == NULL) -NXT_EXPORT nxt_array_t *nxt_services_init(nxt_mp_t *mp); -NXT_EXPORT nxt_int_t nxt_service_add(nxt_array_t *services, - const nxt_service_t *service); -NXT_EXPORT const void *nxt_service_get(nxt_array_t *services, const char *type, - const char *name); +NXT_EXPORT nxt_array_t * +nxt_services_init(nxt_mp_t *mp); +NXT_EXPORT nxt_int_t +nxt_service_add(nxt_array_t *services, const nxt_service_t *service); +NXT_EXPORT const void * +nxt_service_get(nxt_array_t *services, const char *type, const char *name); #endif /* _NXT_SERVICE_H_INCLUDED_ */ diff --git a/src/nxt_sha1.c b/src/nxt_sha1.c index 407c5933c..c0c448774 100644 --- a/src/nxt_sha1.c +++ b/src/nxt_sha1.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Maxim Dounin * Copyright (C) NGINX, Inc. @@ -11,13 +10,11 @@ #include -static const u_char *nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, - size_t size); - +static const u_char * +nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, size_t size); void -nxt_sha1_init(nxt_sha1_t *ctx) -{ +nxt_sha1_init(nxt_sha1_t *ctx) { ctx->a = 0x67452301; ctx->b = 0xefcdab89; ctx->c = 0x98badcfe; @@ -27,13 +24,11 @@ nxt_sha1_init(nxt_sha1_t *ctx) ctx->bytes = 0; } - void -nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size) -{ - size_t used, free; +nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size) { + size_t used, free; - used = (size_t) (ctx->bytes & 0x3f); + used = (size_t) (ctx->bytes & 0x3f); ctx->bytes += size; if (used) { @@ -45,24 +40,22 @@ nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size) } memcpy(&ctx->buffer[used], data, free); - data = (u_char *) data + free; + data = (u_char *) data + free; size -= free; (void) nxt_sha1_body(ctx, ctx->buffer, 64); } if (size >= 64) { - data = nxt_sha1_body(ctx, data, size & ~(size_t) 0x3f); + data = nxt_sha1_body(ctx, data, size & ~(size_t) 0x3f); size &= 0x3f; } memcpy(ctx->buffer, data, size); } - void -nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) -{ - size_t used, free; +nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) { + size_t used, free; used = (size_t) (ctx->bytes & 0x3f); @@ -79,60 +72,59 @@ nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) nxt_memzero(&ctx->buffer[used], free - 8); - ctx->bytes <<= 3; - ctx->buffer[56] = (u_char) (ctx->bytes >> 56); - ctx->buffer[57] = (u_char) (ctx->bytes >> 48); - ctx->buffer[58] = (u_char) (ctx->bytes >> 40); - ctx->buffer[59] = (u_char) (ctx->bytes >> 32); - ctx->buffer[60] = (u_char) (ctx->bytes >> 24); - ctx->buffer[61] = (u_char) (ctx->bytes >> 16); - ctx->buffer[62] = (u_char) (ctx->bytes >> 8); - ctx->buffer[63] = (u_char) ctx->bytes; + ctx->bytes <<= 3; + ctx->buffer[56] = (u_char) (ctx->bytes >> 56); + ctx->buffer[57] = (u_char) (ctx->bytes >> 48); + ctx->buffer[58] = (u_char) (ctx->bytes >> 40); + ctx->buffer[59] = (u_char) (ctx->bytes >> 32); + ctx->buffer[60] = (u_char) (ctx->bytes >> 24); + ctx->buffer[61] = (u_char) (ctx->bytes >> 16); + ctx->buffer[62] = (u_char) (ctx->bytes >> 8); + ctx->buffer[63] = (u_char) ctx->bytes; (void) nxt_sha1_body(ctx, ctx->buffer, 64); result[0] = (u_char) (ctx->a >> 24); result[1] = (u_char) (ctx->a >> 16); result[2] = (u_char) (ctx->a >> 8); - result[3] = (u_char) ctx->a; + result[3] = (u_char) ctx->a; result[4] = (u_char) (ctx->b >> 24); result[5] = (u_char) (ctx->b >> 16); result[6] = (u_char) (ctx->b >> 8); - result[7] = (u_char) ctx->b; + result[7] = (u_char) ctx->b; result[8] = (u_char) (ctx->c >> 24); result[9] = (u_char) (ctx->c >> 16); result[10] = (u_char) (ctx->c >> 8); - result[11] = (u_char) ctx->c; + result[11] = (u_char) ctx->c; result[12] = (u_char) (ctx->d >> 24); result[13] = (u_char) (ctx->d >> 16); result[14] = (u_char) (ctx->d >> 8); - result[15] = (u_char) ctx->d; + result[15] = (u_char) ctx->d; result[16] = (u_char) (ctx->e >> 24); result[17] = (u_char) (ctx->e >> 16); result[18] = (u_char) (ctx->e >> 8); - result[19] = (u_char) ctx->e; + result[19] = (u_char) ctx->e; nxt_memzero(ctx, sizeof(*ctx)); } - /* * Helper functions. */ -#define ROTATE(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) +#define ROTATE(bits, word) (((word) << (bits)) | ((word) >> (32 - (bits)))) -#define F1(b, c, d) (((b) & (c)) | ((~(b)) & (d))) -#define F2(b, c, d) ((b) ^ (c) ^ (d)) -#define F3(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) +#define F1(b, c, d) (((b) & (c)) | ((~(b)) & (d))) +#define F2(b, c, d) ((b) ^ (c) ^ (d)) +#define F3(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) -#define STEP(f, a, b, c, d, e, w, t) \ - temp = ROTATE(5, (a)) + f((b), (c), (d)) + (e) + (w) + (t); \ - (e) = (d); \ - (d) = (c); \ - (c) = ROTATE(30, (b)); \ - (b) = (a); \ - (a) = temp; +#define STEP(f, a, b, c, d, e, w, t) \ + temp = ROTATE(5, (a)) + f((b), (c), (d)) + (e) + (w) + (t); \ + (e) = (d); \ + (d) = (c); \ + (c) = ROTATE(30, (b)); \ + (b) = (a); \ + (a) = temp; /* @@ -140,12 +132,9 @@ nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) * them as uint32_t. */ -#define GET(n) \ - ( ((uint32_t) p[n * 4 + 3]) \ - | ((uint32_t) p[n * 4 + 2] << 8) \ - | ((uint32_t) p[n * 4 + 1] << 16) \ - | ((uint32_t) p[n * 4] << 24)) - +#define GET(n) \ + (((uint32_t) p[n * 4 + 3]) | ((uint32_t) p[n * 4 + 2] << 8) \ + | ((uint32_t) p[n * 4 + 1] << 16) | ((uint32_t) p[n * 4] << 24)) /* * This processes one or more 64-byte data blocks, but does not update @@ -153,13 +142,12 @@ nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx) */ static const u_char * -nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, size_t size) -{ - uint32_t a, b, c, d, e, temp; - uint32_t saved_a, saved_b, saved_c, saved_d, saved_e; - uint32_t words[80]; - nxt_uint_t i; - const u_char *p; +nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, size_t size) { + uint32_t a, b, c, d, e, temp; + uint32_t saved_a, saved_b, saved_c, saved_d, saved_e; + uint32_t words[80]; + nxt_uint_t i; + const u_char *p; p = data; @@ -183,24 +171,22 @@ nxt_sha1_body(nxt_sha1_t *ctx, const u_char *data, size_t size) } for (i = 16; i < 80; i++) { - words[i] = ROTATE(1, words[i - 3] - ^ words[i - 8] - ^ words[i - 14] - ^ words[i - 16]); + words[i] = ROTATE(1, + words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16]); } /* Transformations */ - STEP(F1, a, b, c, d, e, words[0], 0x5a827999); - STEP(F1, a, b, c, d, e, words[1], 0x5a827999); - STEP(F1, a, b, c, d, e, words[2], 0x5a827999); - STEP(F1, a, b, c, d, e, words[3], 0x5a827999); - STEP(F1, a, b, c, d, e, words[4], 0x5a827999); - STEP(F1, a, b, c, d, e, words[5], 0x5a827999); - STEP(F1, a, b, c, d, e, words[6], 0x5a827999); - STEP(F1, a, b, c, d, e, words[7], 0x5a827999); - STEP(F1, a, b, c, d, e, words[8], 0x5a827999); - STEP(F1, a, b, c, d, e, words[9], 0x5a827999); + STEP(F1, a, b, c, d, e, words[0], 0x5a827999); + STEP(F1, a, b, c, d, e, words[1], 0x5a827999); + STEP(F1, a, b, c, d, e, words[2], 0x5a827999); + STEP(F1, a, b, c, d, e, words[3], 0x5a827999); + STEP(F1, a, b, c, d, e, words[4], 0x5a827999); + STEP(F1, a, b, c, d, e, words[5], 0x5a827999); + STEP(F1, a, b, c, d, e, words[6], 0x5a827999); + STEP(F1, a, b, c, d, e, words[7], 0x5a827999); + STEP(F1, a, b, c, d, e, words[8], 0x5a827999); + STEP(F1, a, b, c, d, e, words[9], 0x5a827999); STEP(F1, a, b, c, d, e, words[10], 0x5a827999); STEP(F1, a, b, c, d, e, words[11], 0x5a827999); STEP(F1, a, b, c, d, e, words[12], 0x5a827999); diff --git a/src/nxt_sha1.h b/src/nxt_sha1.h index 2816982b6..10c9d34dc 100644 --- a/src/nxt_sha1.h +++ b/src/nxt_sha1.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,17 +7,18 @@ #ifndef _NXT_SHA1_H_INCLUDED_ #define _NXT_SHA1_H_INCLUDED_ - typedef struct { - uint64_t bytes; - uint32_t a, b, c, d, e; - u_char buffer[64]; + uint64_t bytes; + uint32_t a, b, c, d, e; + u_char buffer[64]; } nxt_sha1_t; - -NXT_EXPORT void nxt_sha1_init(nxt_sha1_t *ctx); -NXT_EXPORT void nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size); -NXT_EXPORT void nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx); +NXT_EXPORT void +nxt_sha1_init(nxt_sha1_t *ctx); +NXT_EXPORT void +nxt_sha1_update(nxt_sha1_t *ctx, const void *data, size_t size); +NXT_EXPORT void +nxt_sha1_final(u_char result[20], nxt_sha1_t *ctx); #endif /* _NXT_SHA1_H_INCLUDED_ */ diff --git a/src/nxt_signal.c b/src/nxt_signal.c index 0ab1ba996..df0837a9c 100644 --- a/src/nxt_signal.c +++ b/src/nxt_signal.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -22,14 +21,14 @@ */ -static nxt_int_t nxt_signal_action(int signo, void (*handler)(int)); -static void nxt_signal_thread(void *data); - +static nxt_int_t +nxt_signal_action(int signo, void (*handler)(int)); +static void +nxt_signal_thread(void *data); nxt_event_signals_t * -nxt_event_engine_signals(const nxt_sig_event_t *sigev) -{ - nxt_event_signals_t *signals; +nxt_event_engine_signals(const nxt_sig_event_t *sigev) { + nxt_event_signals_t *signals; signals = nxt_zalloc(sizeof(nxt_event_signals_t)); if (signals == NULL) { @@ -67,11 +66,9 @@ nxt_event_engine_signals(const nxt_sig_event_t *sigev) return NULL; } - static nxt_int_t -nxt_signal_action(int signo, void (*handler)(int)) -{ - struct sigaction sa; +nxt_signal_action(int signo, void (*handler)(int)) { + struct sigaction sa; nxt_memzero(&sa, sizeof(struct sigaction)); sigemptyset(&sa.sa_mask); @@ -86,11 +83,9 @@ nxt_signal_action(int signo, void (*handler)(int)) return NXT_ERROR; } - static void -nxt_signal_handler(int signo) -{ - nxt_thread_t *thr; +nxt_signal_handler(int signo) { + nxt_thread_t *thr; thr = nxt_thread(); @@ -106,12 +101,10 @@ nxt_signal_handler(int signo) thr->time.signal--; } - nxt_int_t -nxt_signal_thread_start(nxt_event_engine_t *engine) -{ - nxt_thread_link_t *link; - const nxt_sig_event_t *sigev; +nxt_signal_thread_start(nxt_event_engine_t *engine) { + nxt_thread_link_t *link; + const nxt_sig_event_t *sigev; if (engine->signals->process == nxt_pid) { return NXT_OK; @@ -136,7 +129,7 @@ nxt_signal_thread_start(nxt_event_engine_t *engine) link = nxt_zalloc(sizeof(nxt_thread_link_t)); if (nxt_fast_path(link != NULL)) { - link->start = nxt_signal_thread; + link->start = nxt_signal_thread; link->work.data = engine; if (nxt_thread_create(&engine->signals->thread, link) == NXT_OK) { @@ -148,14 +141,12 @@ nxt_signal_thread_start(nxt_event_engine_t *engine) return NXT_ERROR; } - static void -nxt_signal_thread(void *data) -{ +nxt_signal_thread(void *data) { int signo; nxt_err_t err; - nxt_thread_t *thr; - nxt_event_engine_t *engine; + nxt_thread_t *thr; + nxt_event_engine_t *engine; engine = data; @@ -163,7 +154,7 @@ nxt_signal_thread(void *data) nxt_main_log_debug("signal thread"); - for ( ;; ) { + for (;;) { err = sigwait(&engine->signals->sigmask, &signo); nxt_thread_time_update(thr); @@ -179,11 +170,9 @@ nxt_signal_thread(void *data) } } - void -nxt_signal_thread_stop(nxt_event_engine_t *engine) -{ - nxt_thread_handle_t thread; +nxt_signal_thread_stop(nxt_event_engine_t *engine) { + nxt_thread_handle_t thread; thread = engine->signals->thread; diff --git a/src/nxt_signal.h b/src/nxt_signal.h index bc61eb2f1..484081de9 100644 --- a/src/nxt_signal.h +++ b/src/nxt_signal.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,44 +6,40 @@ #ifndef _NXT_SIGNAL_H_INCLUDED_ #define _NXT_SIGNAL_H_INCLUDED_ - struct nxt_sig_event_s { - int signo; - nxt_work_handler_t handler; - const char *name; + int signo; + nxt_work_handler_t handler; + const char *name; }; -#define nxt_event_signal(sig, handler) \ - { sig, handler, #sig } - -#define nxt_event_signal_end \ - { 0, NULL, NULL } +#define nxt_event_signal(sig, handler) {sig, handler, #sig} +#define nxt_event_signal_end {0, NULL, NULL} typedef struct { /* Used by epoll and eventport. */ - nxt_work_handler_t handler; + nxt_work_handler_t handler; - const nxt_sig_event_t *sigev; - sigset_t sigmask; + const nxt_sig_event_t *sigev; + sigset_t sigmask; /* Used by the signal thread. */ - nxt_pid_t process; - nxt_thread_handle_t thread; + nxt_pid_t process; + nxt_thread_handle_t thread; } nxt_event_signals_t; +nxt_event_signals_t * +nxt_event_engine_signals(const nxt_sig_event_t *sigev); -nxt_event_signals_t *nxt_event_engine_signals(const nxt_sig_event_t *sigev); - -#define nxt_event_engine_signals_start(engine) \ - nxt_signal_thread_start(engine) +#define nxt_event_engine_signals_start(engine) nxt_signal_thread_start(engine) -#define nxt_event_engine_signals_stop(engine) \ - nxt_signal_thread_stop(engine) +#define nxt_event_engine_signals_stop(engine) nxt_signal_thread_stop(engine) -NXT_EXPORT nxt_int_t nxt_signal_thread_start(nxt_event_engine_t *engine); -NXT_EXPORT void nxt_signal_thread_stop(nxt_event_engine_t *engine); +NXT_EXPORT nxt_int_t +nxt_signal_thread_start(nxt_event_engine_t *engine); +NXT_EXPORT void +nxt_signal_thread_stop(nxt_event_engine_t *engine); #endif /* _NXT_SIGNAL_H_INCLUDED_ */ diff --git a/src/nxt_signal_handlers.c b/src/nxt_signal_handlers.c index 63b38fab8..0de92a364 100644 --- a/src/nxt_signal_handlers.c +++ b/src/nxt_signal_handlers.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,14 +10,17 @@ #include -static void nxt_signal_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_signal_sigterm_handler(nxt_task_t *task, void *obj, void *data); -static void nxt_signal_sigquit_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_signal_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_signal_sigterm_handler(nxt_task_t *task, void *obj, void *data); +static void +nxt_signal_sigquit_handler(nxt_task_t *task, void *obj, void *data); -const nxt_sig_event_t nxt_process_signals[] = { - nxt_event_signal(SIGHUP, nxt_signal_handler), - nxt_event_signal(SIGINT, nxt_signal_sigterm_handler), +const nxt_sig_event_t nxt_process_signals[] = { + nxt_event_signal(SIGHUP, nxt_signal_handler), + nxt_event_signal(SIGINT, nxt_signal_sigterm_handler), nxt_event_signal(SIGQUIT, nxt_signal_sigquit_handler), nxt_event_signal(SIGTERM, nxt_signal_sigterm_handler), nxt_event_signal(SIGCHLD, nxt_signal_handler), @@ -27,39 +29,31 @@ const nxt_sig_event_t nxt_process_signals[] = { nxt_event_signal_end, }; - static void -nxt_signal_handler(nxt_task_t *task, void *obj, void *data) -{ +nxt_signal_handler(nxt_task_t *task, void *obj, void *data) { nxt_trace(task, "signal signo:%d (%s) received, ignored", - (int) (uintptr_t) obj, data); + (int) (uintptr_t) obj, data); } - void -nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) -{ +nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg) { nxt_process_quit(task, 0); } - static void -nxt_signal_sigterm_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_debug(task, "sigterm handler signo:%d (%s)", - (int) (uintptr_t) obj, data); +nxt_signal_sigterm_handler(nxt_task_t *task, void *obj, void *data) { + nxt_debug(task, "sigterm handler signo:%d (%s)", (int) (uintptr_t) obj, + data); /* A fast exit. */ nxt_runtime_quit(task, 0); } - static void -nxt_signal_sigquit_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_debug(task, "sigquit handler signo:%d (%s)", - (int) (uintptr_t) obj, data); +nxt_signal_sigquit_handler(nxt_task_t *task, void *obj, void *data) { + nxt_debug(task, "sigquit handler signo:%d (%s)", (int) (uintptr_t) obj, + data); /* A graceful exit. */ diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c index 4d1e723b7..d2fc78d61 100644 --- a/src/nxt_sockaddr.c +++ b/src/nxt_sockaddr.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,20 +7,22 @@ #if (NXT_INET6) -static u_char *nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end); +static u_char * +nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end); #endif -static nxt_sockaddr_t *nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr); -static nxt_sockaddr_t *nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr); -static nxt_sockaddr_t *nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr); - +static nxt_sockaddr_t * +nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr); +static nxt_sockaddr_t * +nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr); +static nxt_sockaddr_t * +nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr); nxt_sockaddr_t * -nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls) -{ +nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls) { size_t size; uint8_t hint; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; hint = NXT_EVENT_ENGINE_NO_MEM_HINT; size = offsetof(nxt_sockaddr_t, u) + ls->socklen + ls->address_length; @@ -33,10 +34,10 @@ nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls) nxt_memzero(sa, offsetof(nxt_sockaddr_t, u.sockaddr.sa_data[1])); sa->cache_hint = hint; - sa->socklen = ls->socklen; - sa->length = ls->address_length; + sa->socklen = ls->socklen; + sa->length = ls->address_length; - sa->type = ls->sockaddr->type; + sa->type = ls->sockaddr->type; /* * Set address family for unspecified Unix domain socket, * because these sockaddr's are not updated by old BSD systems, @@ -48,23 +49,19 @@ nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls) return sa; } - void -nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c) -{ - nxt_sockaddr_t *sa; +nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c) { + nxt_sockaddr_t *sa; sa = c->remote; nxt_event_engine_mem_free(engine, sa->cache_hint, sa, 0); } - nxt_sockaddr_t * -nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, size_t address_length) -{ +nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, size_t address_length) { size_t size; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; size = offsetof(nxt_sockaddr_t, u) + socklen + address_length; @@ -80,19 +77,17 @@ nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, size_t address_length) if (nxt_fast_path(sa != NULL)) { sa->socklen = socklen; - sa->length = address_length; + sa->length = address_length; } return sa; } - nxt_sockaddr_t * nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t length, - size_t address_length) -{ + size_t address_length) { size_t size, copy; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; size = length; copy = length; @@ -142,7 +137,7 @@ nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t length, #endif } -#endif /* NXT_HAVE_UNIX_DOMAIN */ +#endif /* NXT_HAVE_UNIX_DOMAIN */ sa = nxt_sockaddr_alloc(mp, size, address_length); @@ -161,12 +156,10 @@ nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t length, return sa; } - nxt_sockaddr_t * -nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src) -{ +nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src) { size_t length; - nxt_sockaddr_t *dst; + nxt_sockaddr_t *dst; length = offsetof(nxt_sockaddr_t, u) + src->socklen; @@ -179,21 +172,18 @@ nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src) return dst; } - nxt_sockaddr_t * -nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_t s) -{ - int ret; - size_t length; - socklen_t socklen; - nxt_sockaddr_buf_t sockaddr; +nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_t s) { + int ret; + size_t length; + socklen_t socklen; + nxt_sockaddr_buf_t sockaddr; socklen = NXT_SOCKADDR_LEN; ret = getsockname(s, &sockaddr.buf, &socklen); if (nxt_fast_path(ret == 0)) { - switch (sockaddr.buf.sa_family) { #if (NXT_INET6) case AF_INET6: @@ -224,33 +214,30 @@ nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_t s) return NULL; } - void -nxt_sockaddr_text(nxt_sockaddr_t *sa) -{ - size_t offset; - u_char *p, *start, *end, *octet; - uint32_t port; - - offset = offsetof(nxt_sockaddr_t, u) + sa->socklen; - sa->start = offset; +nxt_sockaddr_text(nxt_sockaddr_t *sa) { + size_t offset; + u_char *p, *start, *end, *octet; + uint32_t port; + + offset = offsetof(nxt_sockaddr_t, u) + sa->socklen; + sa->start = offset; sa->port_start = offset; start = nxt_pointer_to(sa, offset); - end = start + sa->length; + end = start + sa->length; switch (sa->u.sockaddr.sa_family) { - case AF_INET: sa->address_start = offset; octet = (u_char *) &sa->u.sockaddr_in.sin_addr; - p = nxt_sprintf(start, end, "%ud.%ud.%ud.%ud", - octet[0], octet[1], octet[2], octet[3]); + p = nxt_sprintf(start, end, "%ud.%ud.%ud.%ud", octet[0], octet[1], + octet[2], octet[3]); - sa->address_length = p - start; - sa->port_start += sa->address_length + 1; + sa->address_length = p - start; + sa->port_start += sa->address_length + 1; port = sa->u.sockaddr_in.sin_port; @@ -261,13 +248,13 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa) case AF_INET6: sa->address_start = offset + 1; - p = start; + p = start; *p++ = '['; p = nxt_inet6_ntop(sa->u.sockaddr_in6.sin6_addr.s6_addr, p, end); - sa->address_length = p - (start + 1); - sa->port_start += sa->address_length + 3; + sa->address_length = p - (start + 1); + sa->port_start += sa->address_length + 3; *p++ = ']'; @@ -287,7 +274,7 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa) #if (NXT_LINUX) if (p[0] == '\0') { - size_t length; + size_t length; /* Linux abstract socket address has no trailing zero. */ length = sa->socklen - offsetof(struct sockaddr_un, sun_path); @@ -298,19 +285,19 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa) p = nxt_sprintf(start, end, "unix:%s", p); } -#else /* !(NXT_LINUX) */ +#else /* !(NXT_LINUX) */ p = nxt_sprintf(start, end, "unix:%s", p); #endif - sa->address_length = p - start; - sa->port_start += sa->address_length; - sa->length = p - start; + sa->address_length = p - start; + sa->port_start += sa->address_length; + sa->length = p - start; return; -#endif /* NXT_HAVE_UNIX_DOMAIN */ +#endif /* NXT_HAVE_UNIX_DOMAIN */ default: return; @@ -321,14 +308,11 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa) sa->length = p - start; } - uint32_t -nxt_sockaddr_port_number(nxt_sockaddr_t *sa) -{ - uint32_t port; +nxt_sockaddr_port_number(nxt_sockaddr_t *sa) { + uint32_t port; switch (sa->u.sockaddr.sa_family) { - #if (NXT_INET6) case AF_INET6: @@ -352,10 +336,8 @@ nxt_sockaddr_port_number(nxt_sockaddr_t *sa) return ntohs((uint16_t) port); } - nxt_bool_t -nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) -{ +nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) { if (sa1->socklen != sa2->socklen) { return 0; } @@ -374,7 +356,6 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) */ switch (sa1->u.sockaddr.sa_family) { - #if (NXT_INET6) case AF_INET6: @@ -383,9 +364,8 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) } if (memcmp(&sa1->u.sockaddr_in6.sin6_addr, - &sa2->u.sockaddr_in6.sin6_addr, 16) - != 0) - { + &sa2->u.sockaddr_in6.sin6_addr, 16) + != 0) { return 0; } @@ -395,22 +375,20 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) #if (NXT_HAVE_UNIX_DOMAIN) - case AF_UNIX: - { - size_t length; - - length = sa1->socklen - offsetof(struct sockaddr_un, sun_path); + case AF_UNIX: { + size_t length; - if (memcmp(&sa1->u.sockaddr_un.sun_path, - &sa2->u.sockaddr_un.sun_path, length) - != 0) - { - return 0; - } + length = sa1->socklen - offsetof(struct sockaddr_un, sun_path); - return 1; + if (memcmp(&sa1->u.sockaddr_un.sun_path, &sa2->u.sockaddr_un.sun_path, + length) + != 0) { + return 0; } + return 1; + } + #endif default: /* AF_INET */ @@ -419,8 +397,7 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) } if (sa1->u.sockaddr_in.sin_addr.s_addr - != sa2->u.sockaddr_in.sin_addr.s_addr) - { + != sa2->u.sockaddr_in.sin_addr.s_addr) { return 0; } @@ -432,28 +409,25 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2) #if (NXT_INET6) static u_char * -nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) -{ - u_char *p; - size_t zero_groups, last_zero_groups, ipv6_bytes; - nxt_uint_t i, zero_start, last_zero_start; +nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) { + u_char *p; + size_t zero_groups, last_zero_groups, ipv6_bytes; + nxt_uint_t i, zero_start, last_zero_start; - const size_t max_inet6_length = - nxt_length("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + const size_t max_inet6_length + = nxt_length("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); if (buf + max_inet6_length > end) { return buf; } - zero_start = 16; - zero_groups = 0; - last_zero_start = 16; + zero_start = 16; + zero_groups = 0; + last_zero_start = 16; last_zero_groups = 0; for (i = 0; i < 16; i += 2) { - if (addr[i] == 0 && addr[i + 1] == 0) { - if (last_zero_groups == 0) { last_zero_start = i; } @@ -463,7 +437,7 @@ nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) } else { if (zero_groups < last_zero_groups) { zero_groups = last_zero_groups; - zero_start = last_zero_start; + zero_start = last_zero_start; } last_zero_groups = 0; @@ -472,21 +446,19 @@ nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) if (zero_groups < last_zero_groups) { zero_groups = last_zero_groups; - zero_start = last_zero_start; + zero_start = last_zero_start; } ipv6_bytes = 16; - p = buf; + p = buf; if (zero_start == 0) { - - /* IPv4-mapped address */ + /* IPv4-mapped address */ if ((zero_groups == 5 && addr[10] == 0xFF && addr[11] == 0xFF) - /* IPv4-compatible address */ + /* IPv4-compatible address */ || (zero_groups == 6) - /* not IPv6 loopback address */ - || (zero_groups == 7 && addr[14] != 0 && addr[15] != 1)) - { + /* not IPv6 loopback address */ + || (zero_groups == 7 && addr[14] != 0 && addr[15] != 1)) { ipv6_bytes = 12; } @@ -494,11 +466,10 @@ nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) } for (i = 0; i < ipv6_bytes; i += 2) { - if (i == zero_start) { /* Output maximum number of consecutive zero groups as "::". */ - i += (zero_groups - 1) * 2; - *p++ = ':'; + i += (zero_groups - 1) * 2; + *p++ = ':'; continue; } @@ -510,8 +481,8 @@ nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) } if (ipv6_bytes == 12) { - p = nxt_sprintf(p, end, "%ud.%ud.%ud.%ud", - addr[12], addr[13], addr[14], addr[15]); + p = nxt_sprintf(p, end, "%ud.%ud.%ud.%ud", addr[12], addr[13], addr[14], + addr[15]); } return p; @@ -521,29 +492,24 @@ nxt_inet6_ntop(u_char *addr, u_char *buf, u_char *end) nxt_sockaddr_t * -nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr) -{ - nxt_sockaddr_t *sa; +nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr) { + nxt_sockaddr_t *sa; sa = nxt_sockaddr_parse_optport(mp, addr); - if (sa != NULL - && sa->u.sockaddr.sa_family != AF_UNIX - && nxt_sockaddr_port_number(sa) == 0) - { + if (sa != NULL && sa->u.sockaddr.sa_family != AF_UNIX + && nxt_sockaddr_port_number(sa) == 0) { nxt_thread_log_error(NXT_LOG_ERR, - "The address \"%V\" must specify a port.", addr); + "The address \"%V\" must specify a port.", addr); return NULL; } return sa; } - nxt_sockaddr_t * -nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr) -{ - nxt_sockaddr_t *sa; +nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr) { + nxt_sockaddr_t *sa; if (addr->length == 0) { nxt_thread_log_error(NXT_LOG_ERR, "socket address cannot be empty"); @@ -567,14 +533,12 @@ nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr) return sa; } - static nxt_sockaddr_t * -nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr) -{ +nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr) { #if (NXT_HAVE_UNIX_DOMAIN) size_t length, socklen; - u_char *path; - nxt_sockaddr_t *sa; + u_char *path; + nxt_sockaddr_t *sa; /* * Actual sockaddr_un length can be lesser or even larger than defined @@ -590,12 +554,11 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr) /* Cutting "unix:". */ length = addr->length - 5; - path = addr->start + 5; + path = addr->start + 5; if (length > max_len) { nxt_thread_log_error(NXT_LOG_ERR, - "unix domain socket \"%V\" name is too long", - addr); + "unix domain socket \"%V\" name is too long", addr); return NULL; } @@ -615,7 +578,7 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr) socklen--; #if !(NXT_LINUX) nxt_thread_log_error(NXT_LOG_ERR, - "abstract unix domain sockets are not supported"); + "abstract unix domain sockets are not supported"); return NULL; #endif } @@ -629,29 +592,27 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr) return sa; -#else /* !(NXT_HAVE_UNIX_DOMAIN) */ +#else /* !(NXT_HAVE_UNIX_DOMAIN) */ nxt_thread_log_error(NXT_LOG_ERR, - "unix domain socket \"%V\" is not supported", addr); + "unix domain socket \"%V\" is not supported", addr); return NULL; #endif } - static nxt_sockaddr_t * -nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) -{ +nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) { #if (NXT_INET6) - u_char *p, *start, *end; + u_char *p, *start, *end; size_t length; nxt_int_t ret, port; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; if (addr->start[0] == '[') { length = addr->length - 1; - start = addr->start + 1; + start = addr->start + 1; end = memchr(start, ']', length); if (nxt_slow_path(end == NULL)) { @@ -662,9 +623,9 @@ nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) } else { length = addr->length; - start = addr->start; - end = addr->start + addr->length; - p = NULL; + start = addr->start; + end = addr->start + addr->length; + p = NULL; } port = 0; @@ -674,7 +635,7 @@ nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) if (length < 2 || *p != ':') { nxt_thread_log_error(NXT_LOG_ERR, "invalid IPv6 address in \"%V\"", - addr); + addr); return NULL; } @@ -687,7 +648,7 @@ nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) } sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in6), - NXT_INET6_ADDR_STR_LEN); + NXT_INET6_ADDR_STR_LEN); if (nxt_slow_path(sa == NULL)) { return NULL; } @@ -695,33 +656,31 @@ nxt_sockaddr_inet6_parse(nxt_mp_t *mp, nxt_str_t *addr) ret = nxt_inet6_addr(&sa->u.sockaddr_in6.sin6_addr, start, end - start); if (nxt_slow_path(ret != NXT_OK)) { nxt_thread_log_error(NXT_LOG_ERR, "invalid IPv6 address in \"%V\"", - addr); + addr); return NULL; } sa->u.sockaddr_in6.sin6_family = AF_INET6; - sa->u.sockaddr_in6.sin6_port = htons((in_port_t) port); + sa->u.sockaddr_in6.sin6_port = htons((in_port_t) port); return sa; -#else /* !(NXT_INET6) */ +#else /* !(NXT_INET6) */ nxt_thread_log_error(NXT_LOG_ERR, "IPv6 socket \"%V\" is not supported", - addr); + addr); return NULL; #endif } - static nxt_sockaddr_t * -nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr) -{ - u_char *p; +nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr) { + u_char *p; size_t length; nxt_int_t port; in_addr_t inaddr; - nxt_sockaddr_t *sa; + nxt_sockaddr_t *sa; p = memchr(addr->start, ':', addr->length); @@ -762,38 +721,35 @@ nxt_sockaddr_inet_parse(nxt_mp_t *mp, nxt_str_t *addr) } sa = nxt_sockaddr_alloc(mp, sizeof(struct sockaddr_in), - NXT_INET_ADDR_STR_LEN); + NXT_INET_ADDR_STR_LEN); if (nxt_slow_path(sa == NULL)) { return NULL; } - sa->u.sockaddr_in.sin_family = AF_INET; + sa->u.sockaddr_in.sin_family = AF_INET; sa->u.sockaddr_in.sin_addr.s_addr = inaddr; - sa->u.sockaddr_in.sin_port = htons((in_port_t) port); + sa->u.sockaddr_in.sin_port = htons((in_port_t) port); return sa; } - in_addr_t -nxt_inet_addr(u_char *buf, size_t length) -{ - u_char c, *end; - in_addr_t addr; - nxt_uint_t digit, octet, dots; +nxt_inet_addr(u_char *buf, size_t length) { + u_char c, *end; + in_addr_t addr; + nxt_uint_t digit, octet, dots; if (nxt_slow_path(*(buf + length - 1) == '.')) { return INADDR_NONE; } - addr = 0; + addr = 0; octet = 0; - dots = 0; + dots = 0; end = buf + length; while (buf < end) { - c = *buf++; digit = c - '0'; @@ -805,7 +761,7 @@ nxt_inet_addr(u_char *buf, size_t length) } if (c == '.' && octet < 256) { - addr = (addr << 8) + octet; + addr = (addr << 8) + octet; octet = 0; dots++; continue; @@ -826,10 +782,9 @@ nxt_inet_addr(u_char *buf, size_t length) #if (NXT_INET6) nxt_int_t -nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) -{ - u_char c, *addr, *zero_start, *ipv4, *dst, *src, *end; - nxt_uint_t digit, group, nibbles, groups_left; +nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) { + u_char c, *addr, *zero_start, *ipv4, *dst, *src, *end; + nxt_uint_t digit, group, nibbles, groups_left; if (length == 0) { return NXT_ERROR; @@ -841,12 +796,12 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) buf++; } - addr = in6_addr->s6_addr; - zero_start = NULL; + addr = in6_addr->s6_addr; + zero_start = NULL; groups_left = 8; - nibbles = 0; - group = 0; - ipv4 = NULL; + nibbles = 0; + group = 0; + ipv4 = NULL; while (buf < end) { c = *buf++; @@ -861,13 +816,13 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) if (groups_left != 0) { nibbles = 0; - group = 0; + group = 0; continue; } } else { if (zero_start == NULL) { - ipv4 = buf; + ipv4 = buf; zero_start = addr; continue; } @@ -877,7 +832,6 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) } if (c == '.' && nibbles != 0) { - if (groups_left < 2 || ipv4 == NULL) { return NXT_ERROR; } @@ -913,8 +867,8 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) continue; } - c |= 0x20; - digit = c - 'a'; + c |= 0x20; + digit = c - 'a'; /* values below 'a' become large unsigned integers */ if (digit < 6) { @@ -934,14 +888,12 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) groups_left--; if (groups_left != 0) { - if (zero_start != NULL) { - /* moving part before consecutive zero groups to the end */ groups_left *= 2; - src = addr - 1; - dst = src + groups_left; + src = addr - 1; + dst = src + groups_left; while (src >= zero_start) { *dst-- = *src--; @@ -965,14 +917,13 @@ nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t length) nxt_bool_t -nxt_inet6_probe(nxt_str_t *str) -{ - u_char *colon, *end; +nxt_inet6_probe(nxt_str_t *str) { + u_char *colon, *end; colon = memchr(str->start, ':', str->length); if (colon != NULL) { - end = str->start + str->length; + end = str->start + str->length; colon = memchr(colon + 1, ':', end - (colon + 1)); } diff --git a/src/nxt_sockaddr.h b/src/nxt_sockaddr.h index 0f96f6dd6..883f45c61 100644 --- a/src/nxt_sockaddr.h +++ b/src/nxt_sockaddr.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,7 +6,6 @@ #ifndef _NXT_SOCKADDR_H_INCLUDED_ #define _NXT_SOCKADDR_H_INCLUDED_ - /* * The nxt_sockaddr_t should be allocated using nxt_sockaddr_alloc() * with actual "struct sockaddr_..." size: @@ -21,87 +19,91 @@ struct nxt_sockaddr_s { /* Socket type: SOCKS_STREAM, SOCK_DGRAM, etc. */ - uint8_t type; + uint8_t type; /* Size of struct sockaddr. */ - uint8_t socklen; + uint8_t socklen; /* * Textual sockaddr representation, e.g.: "127.0.0.1:8000", * "[::1]:8000", and "unix:/path/to/socket". */ - uint8_t start; - uint8_t length; + uint8_t start; + uint8_t length; /* * Textual address representation, e.g: "127.0.0.1", "::1", * and "unix:/path/to/socket". */ - uint8_t address_start; - uint8_t address_length; + uint8_t address_start; + uint8_t address_length; /* * Textual port representation, e.g. "8000". * Port length is (start + length) - port_start. */ - uint8_t port_start; + uint8_t port_start; /* A cache hist used to place sockaddr into appropriate free list. */ - uint8_t cache_hint; + uint8_t cache_hint; union { - struct sockaddr sockaddr; - struct sockaddr_in sockaddr_in; + struct sockaddr sockaddr; + struct sockaddr_in sockaddr_in; #if (NXT_INET6) - struct sockaddr_in6 sockaddr_in6; + struct sockaddr_in6 sockaddr_in6; #endif #if (NXT_HAVE_UNIX_DOMAIN) - struct sockaddr_un sockaddr_un; + struct sockaddr_un sockaddr_un; #endif } u; }; - -nxt_sockaddr_t *nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, - nxt_listen_socket_t *ls); -void nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c); - -NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, - size_t address_length) - NXT_MALLOC_LIKE; -NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_create(nxt_mp_t *mp, - struct sockaddr *sockaddr, socklen_t socklen, size_t address_length) - NXT_MALLOC_LIKE; -NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src) - NXT_MALLOC_LIKE; -NXT_EXPORT nxt_sockaddr_t *nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, - nxt_socket_t s) - NXT_MALLOC_LIKE; -NXT_EXPORT void nxt_sockaddr_text(nxt_sockaddr_t *sa); - - -NXT_EXPORT uint32_t nxt_sockaddr_port_number(nxt_sockaddr_t *sa); -NXT_EXPORT nxt_bool_t nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, - nxt_sockaddr_t *sa2); -NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr); -NXT_EXPORT nxt_sockaddr_t *nxt_sockaddr_parse_optport(nxt_mp_t *mp, - nxt_str_t *addr); -NXT_EXPORT in_addr_t nxt_inet_addr(u_char *buf, size_t len); +nxt_sockaddr_t * +nxt_sockaddr_cache_alloc(nxt_event_engine_t *engine, nxt_listen_socket_t *ls); +void +nxt_sockaddr_cache_free(nxt_event_engine_t *engine, nxt_conn_t *c); + +NXT_EXPORT nxt_sockaddr_t * +nxt_sockaddr_alloc(nxt_mp_t *mp, socklen_t socklen, + size_t address_length) NXT_MALLOC_LIKE; +NXT_EXPORT nxt_sockaddr_t * +nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t socklen, + size_t address_length) NXT_MALLOC_LIKE; +NXT_EXPORT nxt_sockaddr_t * +nxt_sockaddr_copy(nxt_mp_t *mp, nxt_sockaddr_t *src) NXT_MALLOC_LIKE; +NXT_EXPORT nxt_sockaddr_t * +nxt_getsockname(nxt_task_t *task, nxt_mp_t *mp, nxt_socket_t s) NXT_MALLOC_LIKE; +NXT_EXPORT void +nxt_sockaddr_text(nxt_sockaddr_t *sa); + + +NXT_EXPORT uint32_t +nxt_sockaddr_port_number(nxt_sockaddr_t *sa); +NXT_EXPORT nxt_bool_t +nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2); +NXT_EXPORT nxt_sockaddr_t * +nxt_sockaddr_parse(nxt_mp_t *mp, nxt_str_t *addr); +NXT_EXPORT nxt_sockaddr_t * +nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr); +NXT_EXPORT in_addr_t +nxt_inet_addr(u_char *buf, size_t len); #if (NXT_INET6) -NXT_EXPORT nxt_int_t nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, - size_t len); +NXT_EXPORT nxt_int_t +nxt_inet6_addr(struct in6_addr *in6_addr, u_char *buf, size_t len); #endif -NXT_EXPORT nxt_bool_t nxt_inet6_probe(nxt_str_t *addr); +NXT_EXPORT nxt_bool_t +nxt_inet6_probe(nxt_str_t *addr); -#define NXT_INET_ADDR_STR_LEN nxt_length("255.255.255.255:65535") +#define NXT_INET_ADDR_STR_LEN nxt_length("255.255.255.255:65535") -#define NXT_INET6_ADDR_STR_LEN \ +#define NXT_INET6_ADDR_STR_LEN \ nxt_length("[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535") -#define nxt_sockaddr_size(sa) \ +#define nxt_sockaddr_size(sa) \ (offsetof(nxt_sockaddr_t, u) + sa->socklen + sa->length) -#define nxt_sockaddr_start(sa) nxt_pointer_to(sa, (sa)->start) -#define nxt_sockaddr_address(sa) nxt_pointer_to(sa, (sa)->address_start) -#define nxt_sockaddr_port(sa) nxt_pointer_to(sa, (sa)->port_start) -#define nxt_sockaddr_port_length(sa) \ +#define nxt_sockaddr_start(sa) nxt_pointer_to(sa, (sa)->start) +#define nxt_sockaddr_address(sa) nxt_pointer_to(sa, (sa)->address_start) +#define nxt_sockaddr_port(sa) nxt_pointer_to(sa, (sa)->port_start) +#define nxt_sockaddr_port_length(sa) \ (((sa)->start + (sa)->length) - (sa)->port_start) diff --git a/src/nxt_socket.c b/src/nxt_socket.c index 9ac8ecd22..88fe81245 100644 --- a/src/nxt_socket.c +++ b/src/nxt_socket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,15 +6,13 @@ #include -static const char *nxt_socket_sockopt_name(nxt_uint_t level, - nxt_uint_t sockopt); - +static const char * +nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt); nxt_socket_t nxt_socket_create(nxt_task_t *task, nxt_uint_t domain, nxt_uint_t type, - nxt_uint_t protocol, nxt_uint_t flags) -{ - nxt_socket_t s; + nxt_uint_t protocol, nxt_uint_t flags) { + nxt_socket_t s; #if (NXT_HAVE_SOCK_NONBLOCK) @@ -28,8 +25,8 @@ nxt_socket_create(nxt_task_t *task, nxt_uint_t domain, nxt_uint_t type, s = socket(domain, type, protocol); if (nxt_slow_path(s == -1)) { - nxt_alert(task, "socket(%ui, 0x%uXi, %ui) failed %E", - domain, type, protocol, nxt_socket_errno); + nxt_alert(task, "socket(%ui, 0x%uXi, %ui) failed %E", domain, type, + protocol, nxt_socket_errno); return s; } @@ -49,10 +46,8 @@ nxt_socket_create(nxt_task_t *task, nxt_uint_t domain, nxt_uint_t type, return s; } - void -nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) -{ +nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) { #if (NXT_HAVE_UNIX_DOMAIN) if (sa->u.sockaddr.sa_family == AF_UNIX) { @@ -70,60 +65,50 @@ nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) #endif } - nxt_int_t nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, - nxt_uint_t sockopt) -{ - int val; - socklen_t len; + nxt_uint_t sockopt) { + int val; + socklen_t len; len = sizeof(val); if (nxt_fast_path(getsockopt(s, level, sockopt, &val, &len) == 0)) { - nxt_debug(task, "getsockopt(%d, %ui, %s): %d", - s, level, nxt_socket_sockopt_name(level, sockopt), val); + nxt_debug(task, "getsockopt(%d, %ui, %s): %d", s, level, + nxt_socket_sockopt_name(level, sockopt), val); return val; } - nxt_alert(task, "getsockopt(%d, %ui, %s) failed %E", - s, level, nxt_socket_sockopt_name(level, sockopt), - nxt_socket_errno); + nxt_alert(task, "getsockopt(%d, %ui, %s) failed %E", s, level, + nxt_socket_sockopt_name(level, sockopt), nxt_socket_errno); return -1; } - nxt_int_t nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, - nxt_uint_t sockopt, int val) -{ - socklen_t len; + nxt_uint_t sockopt, int val) { + socklen_t len; len = sizeof(val); if (nxt_fast_path(setsockopt(s, level, sockopt, &val, len) == 0)) { - nxt_debug(task, "setsockopt(%d, %ui, %s): %d", - s, level, nxt_socket_sockopt_name(level, sockopt), val); + nxt_debug(task, "setsockopt(%d, %ui, %s): %d", s, level, + nxt_socket_sockopt_name(level, sockopt), val); return NXT_OK; } - nxt_alert(task, "setsockopt(%d, %ui, %s, %d) failed %E", - s, level, nxt_socket_sockopt_name(level, sockopt), val, - nxt_socket_errno); + nxt_alert(task, "setsockopt(%d, %ui, %s, %d) failed %E", s, level, + nxt_socket_sockopt_name(level, sockopt), val, nxt_socket_errno); return NXT_ERROR; } - static const char * -nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) -{ +nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) { switch (level) { - case SOL_SOCKET: switch (sockopt) { - case SO_SNDBUF: return "SO_SNDBUF"; @@ -141,7 +126,6 @@ nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) case IPPROTO_TCP: switch (sockopt) { - case TCP_NODELAY: return "TCP_NODELAY"; @@ -157,46 +141,40 @@ nxt_socket_sockopt_name(nxt_uint_t level, nxt_uint_t sockopt) case IPPROTO_IPV6: switch (sockopt) { - case IPV6_V6ONLY: return "IPV6_V6ONLY"; } break; #endif - } return ""; } - nxt_int_t -nxt_socket_bind(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) -{ +nxt_socket_bind(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) { nxt_debug(task, "bind(%d, %*s)", s, (size_t) sa->length, - nxt_sockaddr_start(sa)); + nxt_sockaddr_start(sa)); if (nxt_fast_path(bind(s, &sa->u.sockaddr, sa->socklen) == 0)) { return NXT_OK; } - nxt_alert(task, "bind(%d, %*s) failed %E", - s, (size_t) sa->length, nxt_sockaddr_start(sa), nxt_socket_errno); + nxt_alert(task, "bind(%d, %*s) failed %E", s, (size_t) sa->length, + nxt_sockaddr_start(sa), nxt_socket_errno); return NXT_ERROR; } - nxt_int_t -nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) -{ - nxt_err_t err; - nxt_int_t ret; - nxt_uint_t level; +nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) { + nxt_err_t err; + nxt_int_t ret; + nxt_uint_t level; - nxt_debug(task, "connect(%d, %*s)", - s, (size_t) sa->length, nxt_sockaddr_start(sa)); + nxt_debug(task, "connect(%d, %*s)", s, (size_t) sa->length, + nxt_sockaddr_start(sa)); if (connect(s, &sa->u.sockaddr, sa->socklen) == 0) { return NXT_OK; @@ -205,10 +183,9 @@ nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) err = nxt_socket_errno; switch (err) { - case NXT_EINPROGRESS: - nxt_debug(task, "connect(%d, %*s) in progress", - s, (size_t) sa->length, nxt_sockaddr_start(sa)); + nxt_debug(task, "connect(%d, %*s) in progress", s, (size_t) sa->length, + nxt_sockaddr_start(sa)); return NXT_AGAIN; case NXT_ECONNREFUSED: @@ -220,7 +197,7 @@ nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) */ #endif level = NXT_LOG_ERR; - ret = NXT_DECLINED; + ret = NXT_DECLINED; break; case NXT_ECONNRESET: @@ -229,26 +206,24 @@ nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa) case NXT_EHOSTDOWN: case NXT_EHOSTUNREACH: level = NXT_LOG_ERR; - ret = NXT_ERROR; + ret = NXT_ERROR; break; default: level = NXT_LOG_ALERT; - ret = NXT_ERROR; + ret = NXT_ERROR; } - nxt_log(task, level, "connect(%d, %*s) failed %E", - s, (size_t) sa->length, nxt_sockaddr_start(sa), err); + nxt_log(task, level, "connect(%d, %*s) failed %E", s, (size_t) sa->length, + nxt_sockaddr_start(sa), err); return ret; } - void -nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how) -{ - nxt_err_t err; - nxt_uint_t level; +nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how) { + nxt_err_t err; + nxt_uint_t level; if (nxt_fast_path(shutdown(s, how) == 0)) { nxt_debug(task, "shutdown(%d, %ui)", s, how); @@ -258,7 +233,6 @@ nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how) err = nxt_socket_errno; switch (err) { - case NXT_ENOTCONN: level = NXT_LOG_DEBUG; break; @@ -278,12 +252,10 @@ nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how) nxt_log(task, level, "shutdown(%d, %ui) failed %E", s, how, err); } - void -nxt_socket_close(nxt_task_t *task, nxt_socket_t s) -{ - nxt_err_t err; - nxt_uint_t level; +nxt_socket_close(nxt_task_t *task, nxt_socket_t s) { + nxt_err_t err; + nxt_uint_t level; if (nxt_fast_path(close(s) == 0)) { nxt_debug(task, "socket close(%d)", s); @@ -293,7 +265,6 @@ nxt_socket_close(nxt_task_t *task, nxt_socket_t s) err = nxt_socket_errno; switch (err) { - case NXT_ENOTCONN: level = NXT_LOG_DEBUG; break; @@ -313,12 +284,10 @@ nxt_socket_close(nxt_task_t *task, nxt_socket_t s) nxt_log(task, level, "socket close(%d) failed %E", s, err); } - nxt_err_t -nxt_socket_error(nxt_socket_t s) -{ - int ret, err; - socklen_t len; +nxt_socket_error(nxt_socket_t s) { + int ret, err; + socklen_t len; err = 0; len = sizeof(int); @@ -335,12 +304,9 @@ nxt_socket_error(nxt_socket_t s) return err; } - nxt_uint_t -nxt_socket_error_level(nxt_err_t err) -{ +nxt_socket_error_level(nxt_err_t err) { switch (err) { - case NXT_EPIPE: case NXT_ECONNRESET: case NXT_ENOTCONN: diff --git a/src/nxt_socket.h b/src/nxt_socket.h index 69b09039f..ded0e4324 100644 --- a/src/nxt_socket.h +++ b/src/nxt_socket.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,9 +7,9 @@ #define _NXT_SOCKET_H_INCLUDED_ -typedef int nxt_socket_t; +typedef int nxt_socket_t; -#define NXT_NONBLOCK 1 +#define NXT_NONBLOCK 1 /* @@ -66,63 +65,69 @@ typedef int nxt_socket_t; */ #if (NXT_HAVE_UNIX_DOMAIN) -#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_un) +#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_un) #elif (NXT_HAVE_SOCKADDR_IN6) -#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_in6) +#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_in6) #else -#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_in) +#define NXT_SOCKADDR_LEN sizeof(struct sockaddr_in) #endif typedef union { - struct sockaddr buf; - uint64_t alignment; - char space[NXT_SOCKADDR_LEN]; + struct sockaddr buf; + uint64_t alignment; + char space[NXT_SOCKADDR_LEN]; } nxt_sockaddr_buf_t; - /* * MAXHOSTNAMELEN is: * 64 on Linux; * 256 on FreeBSD, Solaris, MacOSX, NetBSD, OpenBSD. */ -#define NXT_MAXHOSTNAMELEN MAXHOSTNAMELEN - - -NXT_EXPORT nxt_socket_t nxt_socket_create(nxt_task_t *task, nxt_uint_t family, - nxt_uint_t type, nxt_uint_t protocol, nxt_uint_t flags); -NXT_EXPORT void nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s, - nxt_sockaddr_t *sa); -NXT_EXPORT nxt_int_t nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s, - nxt_uint_t level, nxt_uint_t sockopt); -NXT_EXPORT nxt_int_t nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s, - nxt_uint_t level, nxt_uint_t sockopt, int val); -NXT_EXPORT nxt_int_t nxt_socket_bind(nxt_task_t *task, nxt_socket_t s, - nxt_sockaddr_t *sa); -NXT_EXPORT nxt_int_t nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, - nxt_sockaddr_t *sa); -NXT_EXPORT void nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, - nxt_uint_t how); -NXT_EXPORT void nxt_socket_close(nxt_task_t *task, nxt_socket_t s); -nxt_err_t nxt_socket_error(nxt_socket_t s); -nxt_uint_t nxt_socket_error_level(nxt_err_t err); - -NXT_EXPORT nxt_int_t nxt_socketpair_create(nxt_task_t *task, - nxt_socket_t *pair); -NXT_EXPORT void nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair); -NXT_EXPORT ssize_t nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, - nxt_iobuf_t *iob, nxt_uint_t niob); -NXT_EXPORT ssize_t nxt_socketpair_recv(nxt_fd_event_t *ev, - nxt_iobuf_t *iob, nxt_uint_t niob, void *oob); - - -#define nxt_socket_nonblocking(task, fd) \ - nxt_fd_nonblocking(task, fd) - -#define nxt_socket_blocking(task, fd) \ - nxt_fd_blocking(task, fd) +#define NXT_MAXHOSTNAMELEN MAXHOSTNAMELEN + + +NXT_EXPORT nxt_socket_t +nxt_socket_create(nxt_task_t *task, nxt_uint_t family, nxt_uint_t type, + nxt_uint_t protocol, nxt_uint_t flags); +NXT_EXPORT void +nxt_socket_defer_accept(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa); +NXT_EXPORT nxt_int_t +nxt_socket_getsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, + nxt_uint_t sockopt); +NXT_EXPORT nxt_int_t +nxt_socket_setsockopt(nxt_task_t *task, nxt_socket_t s, nxt_uint_t level, + nxt_uint_t sockopt, int val); +NXT_EXPORT nxt_int_t +nxt_socket_bind(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa); +NXT_EXPORT nxt_int_t +nxt_socket_connect(nxt_task_t *task, nxt_socket_t s, nxt_sockaddr_t *sa); +NXT_EXPORT void +nxt_socket_shutdown(nxt_task_t *task, nxt_socket_t s, nxt_uint_t how); +NXT_EXPORT void +nxt_socket_close(nxt_task_t *task, nxt_socket_t s); +nxt_err_t +nxt_socket_error(nxt_socket_t s); +nxt_uint_t +nxt_socket_error_level(nxt_err_t err); + +NXT_EXPORT nxt_int_t +nxt_socketpair_create(nxt_task_t *task, nxt_socket_t *pair); +NXT_EXPORT void +nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair); +NXT_EXPORT ssize_t +nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, nxt_iobuf_t *iob, + nxt_uint_t niob); +NXT_EXPORT ssize_t +nxt_socketpair_recv(nxt_fd_event_t *ev, nxt_iobuf_t *iob, nxt_uint_t niob, + void *oob); + + +#define nxt_socket_nonblocking(task, fd) nxt_fd_nonblocking(task, fd) + +#define nxt_socket_blocking(task, fd) nxt_fd_blocking(task, fd) #endif /* _NXT_SOCKET_H_INCLUDED_ */ diff --git a/src/nxt_socket_msg.c b/src/nxt_socket_msg.c index 3b35ab292..ca7a0ae70 100644 --- a/src/nxt_socket_msg.c +++ b/src/nxt_socket_msg.c @@ -6,45 +6,41 @@ #include #include - ssize_t nxt_sendmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob, - const nxt_send_oob_t *oob) -{ - struct msghdr msg; + const nxt_send_oob_t *oob) { + struct msghdr msg; - msg.msg_name = NULL; + msg.msg_name = NULL; msg.msg_namelen = 0; - msg.msg_iov = iob; - msg.msg_iovlen = niob; + msg.msg_iov = iob; + msg.msg_iovlen = niob; /* Flags are cleared just to suppress valgrind warning. */ - msg.msg_flags = 0; + msg.msg_flags = 0; if (oob != NULL && oob->size != 0) { - msg.msg_control = (void *) oob->buf; + msg.msg_control = (void *) oob->buf; msg.msg_controllen = oob->size; } else { - msg.msg_control = NULL; + msg.msg_control = NULL; msg.msg_controllen = 0; } return sendmsg(s, &msg, 0); } - ssize_t nxt_recvmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob, - nxt_recv_oob_t *oob) -{ - ssize_t n; - struct msghdr msg; - - msg.msg_name = NULL; - msg.msg_namelen = 0; - msg.msg_iov = iob; - msg.msg_iovlen = niob; - msg.msg_control = oob->buf; + nxt_recv_oob_t *oob) { + ssize_t n; + struct msghdr msg; + + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_iov = iob; + msg.msg_iovlen = niob; + msg.msg_control = oob->buf; msg.msg_controllen = sizeof(oob->buf); n = recvmsg(s, &msg, 0); diff --git a/src/nxt_socket_msg.h b/src/nxt_socket_msg.h index 81617bd69..9d7b76618 100644 --- a/src/nxt_socket_msg.h +++ b/src/nxt_socket_msg.h @@ -11,67 +11,63 @@ #if (NXT_HAVE_UCRED) -#define NXT_CRED_USECMSG 1 -#define NXT_CRED_CMSGTYPE SCM_CREDENTIALS -#define NXT_CRED_GETPID(u) (u->pid) +#define NXT_CRED_USECMSG 1 +#define NXT_CRED_CMSGTYPE SCM_CREDENTIALS +#define NXT_CRED_GETPID(u) (u->pid) -typedef struct ucred nxt_socket_cred_t; +typedef struct ucred nxt_socket_cred_t; #elif (NXT_HAVE_MSGHDR_CMSGCRED) -#define NXT_CRED_USECMSG 1 -#define NXT_CRED_CMSGTYPE SCM_CREDS -#define NXT_CRED_GETPID(u) (u->cmcred_pid) +#define NXT_CRED_USECMSG 1 +#define NXT_CRED_CMSGTYPE SCM_CREDS +#define NXT_CRED_GETPID(u) (u->cmcred_pid) -typedef struct cmsgcred nxt_socket_cred_t; +typedef struct cmsgcred nxt_socket_cred_t; #endif #if (NXT_CRED_USECMSG) -#define NXT_OOB_RECV_SIZE \ +#define NXT_OOB_RECV_SIZE \ (CMSG_SPACE(2 * sizeof(int)) + CMSG_SPACE(sizeof(nxt_socket_cred_t))) #else -#define NXT_OOB_RECV_SIZE \ - CMSG_SPACE(2 * sizeof(int)) +#define NXT_OOB_RECV_SIZE CMSG_SPACE(2 * sizeof(int)) #endif #if (NXT_HAVE_MSGHDR_CMSGCRED) -#define NXT_OOB_SEND_SIZE \ +#define NXT_OOB_SEND_SIZE \ (CMSG_SPACE(2 * sizeof(int)) + CMSG_SPACE(sizeof(nxt_socket_cred_t))) #else -#define NXT_OOB_SEND_SIZE \ - CMSG_SPACE(2 * sizeof(int)) +#define NXT_OOB_SEND_SIZE CMSG_SPACE(2 * sizeof(int)) #endif typedef struct { - size_t size; - u_char buf[NXT_OOB_RECV_SIZE]; + size_t size; + u_char buf[NXT_OOB_RECV_SIZE]; } nxt_recv_oob_t; - typedef struct { - size_t size; - u_char buf[NXT_OOB_SEND_SIZE]; + size_t size; + u_char buf[NXT_OOB_SEND_SIZE]; } nxt_send_oob_t; - /** * The nxt_sendmsg is a wrapper for sendmsg. * The oob struct must be initialized using nxt_socket_msg_oob_init(). */ -NXT_EXPORT ssize_t nxt_sendmsg(nxt_socket_t s, nxt_iobuf_t *iob, - nxt_uint_t niob, const nxt_send_oob_t *oob); +NXT_EXPORT ssize_t +nxt_sendmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob, + const nxt_send_oob_t *oob); /** * The nxt_recvmsg is a wrapper for recvmsg. * The oob buffer must be consumed by using nxt_socket_msg_oob_get(). */ -NXT_EXPORT ssize_t nxt_recvmsg(nxt_socket_t s, - nxt_iobuf_t *iob, nxt_uint_t niob, nxt_recv_oob_t *oob); - +NXT_EXPORT ssize_t +nxt_recvmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob, + nxt_recv_oob_t *oob); nxt_inline struct cmsghdr * -NXT_CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg) -{ +NXT_CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg) { #if !defined(__GLIBC__) && defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wsign-compare" @@ -82,12 +78,10 @@ NXT_CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg) #endif } - nxt_inline void -nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) -{ +nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) { int nfds; - struct cmsghdr *cmsg; + struct cmsghdr *cmsg; #if (NXT_HAVE_MSGHDR_CMSGCRED) cmsg = (struct cmsghdr *) (oob->buf); @@ -98,9 +92,9 @@ nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) */ nxt_memzero(cmsg, sizeof(struct cmsghdr)); - cmsg->cmsg_len = CMSG_LEN(sizeof(nxt_socket_cred_t)); + cmsg->cmsg_len = CMSG_LEN(sizeof(nxt_socket_cred_t)); cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = NXT_CRED_CMSGTYPE; + cmsg->cmsg_type = NXT_CRED_CMSGTYPE; oob->size = CMSG_SPACE(sizeof(nxt_socket_cred_t)); @@ -118,9 +112,9 @@ nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) nxt_memzero(cmsg, sizeof(struct cmsghdr)); - cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int)); + cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int)); cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_type = SCM_RIGHTS; /* * nxt_memcpy() is used instead of simple @@ -136,21 +130,17 @@ nxt_socket_msg_oob_init(nxt_send_oob_t *oob, int *fds) oob->size += CMSG_SPACE(nfds * sizeof(int)); } - nxt_inline nxt_int_t -nxt_socket_msg_oob_get_fds(nxt_recv_oob_t *oob, nxt_fd_t *fd) -{ +nxt_socket_msg_oob_get_fds(nxt_recv_oob_t *oob, nxt_fd_t *fd) { size_t size; struct msghdr msg; - struct cmsghdr *cmsg; + struct cmsghdr *cmsg; - msg.msg_control = oob->buf; + msg.msg_control = oob->buf; msg.msg_controllen = oob->size; - for (cmsg = CMSG_FIRSTHDR(&msg); - cmsg != NULL; - cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) - { + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; + cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) { size = cmsg->cmsg_len - CMSG_LEN(0); if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { @@ -167,13 +157,11 @@ nxt_socket_msg_oob_get_fds(nxt_recv_oob_t *oob, nxt_fd_t *fd) return NXT_OK; } - nxt_inline nxt_int_t -nxt_socket_msg_oob_get(nxt_recv_oob_t *oob, nxt_fd_t *fd, nxt_pid_t *pid) -{ +nxt_socket_msg_oob_get(nxt_recv_oob_t *oob, nxt_fd_t *fd, nxt_pid_t *pid) { size_t size; struct msghdr msg; - struct cmsghdr *cmsg; + struct cmsghdr *cmsg; if (oob->size == 0) { return NXT_OK; @@ -183,13 +171,11 @@ nxt_socket_msg_oob_get(nxt_recv_oob_t *oob, nxt_fd_t *fd, nxt_pid_t *pid) *pid = -1; #endif - msg.msg_control = oob->buf; + msg.msg_control = oob->buf; msg.msg_controllen = oob->size; - for (cmsg = CMSG_FIRSTHDR(&msg); - cmsg != NULL; - cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) - { + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; + cmsg = NXT_CMSG_NXTHDR(&msg, cmsg)) { size = cmsg->cmsg_len - CMSG_LEN(0); if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { @@ -203,19 +189,17 @@ nxt_socket_msg_oob_get(nxt_recv_oob_t *oob, nxt_fd_t *fd, nxt_pid_t *pid) break; #endif } - #if (NXT_CRED_USECMSG) else if (cmsg->cmsg_level == SOL_SOCKET - && cmsg->cmsg_type == NXT_CRED_CMSGTYPE) - { - nxt_socket_cred_t *creds; + && cmsg->cmsg_type == NXT_CRED_CMSGTYPE) { + nxt_socket_cred_t *creds; if (nxt_slow_path(size != sizeof(nxt_socket_cred_t))) { return NXT_ERROR; } creds = (nxt_socket_cred_t *) CMSG_DATA(cmsg); - *pid = NXT_CRED_GETPID(creds); + *pid = NXT_CRED_GETPID(creds); } #endif } diff --git a/src/nxt_socketpair.c b/src/nxt_socketpair.c index 45274b788..d7b875771 100644 --- a/src/nxt_socketpair.c +++ b/src/nxt_socketpair.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -14,15 +13,14 @@ /* SOCK_SEQPACKET is disabled to test SOCK_DGRAM on all platforms. */ #if (0 || NXT_HAVE_AF_UNIX_SOCK_SEQPACKET) -#define NXT_UNIX_SOCKET SOCK_SEQPACKET +#define NXT_UNIX_SOCKET SOCK_SEQPACKET #else -#define NXT_UNIX_SOCKET SOCK_DGRAM +#define NXT_UNIX_SOCKET SOCK_DGRAM #endif nxt_int_t -nxt_socketpair_create(nxt_task_t *task, nxt_socket_t *pair) -{ +nxt_socketpair_create(nxt_task_t *task, nxt_socket_t *pair) { if (nxt_slow_path(socketpair(AF_UNIX, NXT_UNIX_SOCKET, 0, pair) != 0)) { nxt_alert(task, "socketpair() failed %E", nxt_errno); return NXT_ERROR; @@ -47,18 +45,18 @@ nxt_socketpair_create(nxt_task_t *task, nxt_socket_t *pair) } #if NXT_HAVE_SOCKOPT_SO_PASSCRED - int enable_creds = 1; + int enable_creds = 1; if (nxt_slow_path(setsockopt(pair[0], SOL_SOCKET, SO_PASSCRED, - &enable_creds, sizeof(enable_creds)) == -1)) - { + &enable_creds, sizeof(enable_creds)) + == -1)) { nxt_alert(task, "failed to set SO_PASSCRED %E", nxt_errno); goto fail; } if (nxt_slow_path(setsockopt(pair[1], SOL_SOCKET, SO_PASSCRED, - &enable_creds, sizeof(enable_creds)) == -1)) - { + &enable_creds, sizeof(enable_creds)) + == -1)) { nxt_alert(task, "failed to set SO_PASSCRED %E", nxt_errno); goto fail; } @@ -73,32 +71,28 @@ nxt_socketpair_create(nxt_task_t *task, nxt_socket_t *pair) return NXT_ERROR; } - void -nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair) -{ +nxt_socketpair_close(nxt_task_t *task, nxt_socket_t *pair) { nxt_socket_close(task, pair[0]); nxt_socket_close(task, pair[1]); } - ssize_t nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, nxt_iobuf_t *iob, - nxt_uint_t niob) -{ - ssize_t n; - nxt_err_t err; - nxt_send_oob_t oob; + nxt_uint_t niob) { + ssize_t n; + nxt_err_t err; + nxt_send_oob_t oob; nxt_socket_msg_oob_init(&oob, fd); - for ( ;; ) { + for (;;) { n = nxt_sendmsg(ev->fd, iob, niob, &oob); err = (n == -1) ? nxt_socket_errno : 0; nxt_debug(ev->task, "sendmsg(%d, %FD, %FD, %ui): %z", ev->fd, fd[0], - fd[1], niob, n); + fd[1], niob, n); if (n > 0) { return n; @@ -107,7 +101,6 @@ nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, nxt_iobuf_t *iob, /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(ev->task, "sendmsg(%d) not ready", ev->fd); break; @@ -124,8 +117,8 @@ nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, nxt_iobuf_t *iob, continue; default: - nxt_alert(ev->task, "sendmsg(%d, %FD, %FD, %ui) failed %E", - ev->fd, fd[0], fd[1], niob, err); + nxt_alert(ev->task, "sendmsg(%d, %FD, %FD, %ui) failed %E", ev->fd, + fd[0], fd[1], niob, err); return NXT_ERROR; } @@ -136,28 +129,26 @@ nxt_socketpair_send(nxt_fd_event_t *ev, nxt_fd_t *fd, nxt_iobuf_t *iob, } } - ssize_t nxt_socketpair_recv(nxt_fd_event_t *ev, nxt_iobuf_t *iob, nxt_uint_t niob, - void *oob) -{ - ssize_t n; - nxt_err_t err; + void *oob) { + ssize_t n; + nxt_err_t err; - for ( ;; ) { + for (;;) { n = nxt_recvmsg(ev->fd, iob, niob, oob); err = (n == -1) ? nxt_socket_errno : 0; - nxt_debug(ev->task, "recvmsg(%d, %ui, %uz): %z", - ev->fd, niob, ((nxt_recv_oob_t *) oob)->size, n); + nxt_debug(ev->task, "recvmsg(%d, %ui, %uz): %z", ev->fd, niob, + ((nxt_recv_oob_t *) oob)->size, n); if (n > 0) { return n; } if (n == 0) { - ev->closed = 1; + ev->closed = 1; ev->read_ready = 0; return n; @@ -166,7 +157,6 @@ nxt_socketpair_recv(nxt_fd_event_t *ev, nxt_iobuf_t *iob, nxt_uint_t niob, /* n == -1 */ switch (err) { - case NXT_EAGAIN: nxt_debug(ev->task, "recvmsg(%d) not ready", ev->fd); ev->read_ready = 0; @@ -178,8 +168,8 @@ nxt_socketpair_recv(nxt_fd_event_t *ev, nxt_iobuf_t *iob, nxt_uint_t niob, continue; default: - nxt_alert(ev->task, "recvmsg(%d, %ui) failed %E", - ev->fd, niob, err); + nxt_alert(ev->task, "recvmsg(%d, %ui) failed %E", ev->fd, niob, + err); return NXT_ERROR; } diff --git a/src/nxt_solaris_sendfilev.c b/src/nxt_solaris_sendfilev.c index 1b46d0995..9046916a7 100644 --- a/src/nxt_solaris_sendfilev.c +++ b/src/nxt_solaris_sendfilev.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -15,23 +14,23 @@ * AF_INET6 family type SOCK_STREAM socket that is open for writing. */ -ssize_t nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, +ssize_t +nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit); -static size_t nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, - int32_t *nsfv, nxt_bool_t *sync, size_t limit); - +static size_t +nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, int32_t *nsfv, + nxt_bool_t *sync, size_t limit); ssize_t nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, - size_t limit) -{ - size_t sent; - ssize_t n; - int32_t nsfv; - nxt_err_t err; - nxt_off_t size; - nxt_bool_t sync; - sendfilevec_t sfv[NXT_IOBUF_MAX]; + size_t limit) { + size_t sent; + ssize_t n; + int32_t nsfv; + nxt_err_t err; + nxt_off_t size; + nxt_bool_t sync; + sendfilevec_t sfv[NXT_IOBUF_MAX]; if (c->sendfile == 0) { /* AF_UNIX does not support sendfilev(). */ @@ -49,7 +48,7 @@ nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, } sent = 0; - n = sendfilev(c->socket.fd, sfv, nsfv, &sent); + n = sendfilev(c->socket.fd, sfv, nsfv, &sent); err = (n == -1) ? nxt_errno : 0; @@ -57,7 +56,6 @@ nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, if (n == -1) { switch (err) { - case NXT_EAGAIN: c->socket.write_ready = 0; break; @@ -68,7 +66,7 @@ nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, default: c->socket.error = err; nxt_log(c->socket.task, nxt_socket_error_level(err), - "sendfilev(%d, %D) failed %E", c->socket.fd, nsfv, err); + "sendfilev(%d, %D) failed %E", c->socket.fd, nsfv, err); return NXT_ERROR; } @@ -85,27 +83,23 @@ nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b, return sent; } - static size_t nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, int32_t *nsfv, - nxt_bool_t *sync, size_t limit) -{ - size_t size, total; - nxt_fd_t fd, last_fd; - nxt_int_t i; - nxt_off_t pos, last_pos; - - i = -1; - last_fd = -1; + nxt_bool_t *sync, size_t limit) { + size_t size, total; + nxt_fd_t fd, last_fd; + nxt_int_t i; + nxt_off_t pos, last_pos; + + i = -1; + last_fd = -1; last_pos = 0; - total = 0; + total = 0; for (total = 0; b != NULL && total < limit; b = b->next) { - if (nxt_buf_is_file(b)) { - - fd = b->file->fd; - pos = b->file_pos; + fd = b->file->fd; + pos = b->file_pos; size = b->file_end - pos; if (size == 0) { @@ -117,9 +111,8 @@ nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, int32_t *nsfv, } } else if (nxt_buf_is_mem(b)) { - - fd = SFV_FD_SELF; - pos = (uintptr_t) b->mem.pos; + fd = SFV_FD_SELF; + pos = (uintptr_t) b->mem.pos; size = b->mem.free - b->mem.pos; if (size == 0) { @@ -140,23 +133,22 @@ nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, int32_t *nsfv, } if (fd != last_fd || pos != last_pos) { - if (++i >= NXT_IOBUF_MAX) { goto done; } - sfv[i].sfv_fd = fd; + sfv[i].sfv_fd = fd; sfv[i].sfv_flag = 0; - sfv[i].sfv_off = pos; - sfv[i].sfv_len = size; + sfv[i].sfv_off = pos; + sfv[i].sfv_len = size; } else { sfv[i].sfv_len += size; } - total += size; - last_pos = pos + size; - last_fd = fd; + total += size; + last_pos = pos + size; + last_fd = fd; } i++; diff --git a/src/nxt_sort.h b/src/nxt_sort.h index b79d0919c..4ab6c375a 100644 --- a/src/nxt_sort.h +++ b/src/nxt_sort.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,7 +7,7 @@ #define _NXT_SORT_H_INCLUDED_ -#define nxt_qsort qsort +#define nxt_qsort qsort #endif /* _NXT_SORT_H_INCLUDED_ */ diff --git a/src/nxt_source.h b/src/nxt_source.h index 0b4658dd7..93c8c958d 100644 --- a/src/nxt_source.h +++ b/src/nxt_source.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,36 +6,34 @@ #ifndef _NXT_SOURCE_H_INCLUDED_ #define _NXT_SOURCE_H_INCLUDED_ - /* * A source handler should store a pointer to a passed source hook, but not * the hook's values themselves, because a source filter may change the values. */ typedef struct { - void *context; - nxt_work_handler_t filter; + void *context; + nxt_work_handler_t filter; } nxt_source_hook_t; - -typedef void (*nxt_source_handler_t)(void *source_context, - nxt_source_hook_t *query); +typedef void ( + *nxt_source_handler_t)(void *source_context, nxt_source_hook_t *query); -#define nxt_source_filter(thr, wq, task, next, out) \ - do { \ - if (thr->engine->batch != 0) { \ - nxt_thread_work_queue_add(thr, wq, nxt_source_filter_handler, \ - task, next, out); \ - \ - } else { \ - (next)->filter(task, (next)->context, out); \ - } \ - \ +#define nxt_source_filter(thr, wq, task, next, out) \ + do { \ + if (thr->engine->batch != 0) { \ + nxt_thread_work_queue_add(thr, wq, nxt_source_filter_handler, \ + task, next, out); \ + \ + } else { \ + (next)->filter(task, (next)->context, out); \ + } \ + \ } while (0) -NXT_EXPORT void nxt_source_filter_handler(nxt_task_t *task, void *obj, - void *data); +NXT_EXPORT void +nxt_source_filter_handler(nxt_task_t *task, void *obj, void *data); #endif /* _NXT_SOURCE_H_INCLUDED_ */ diff --git a/src/nxt_spinlock.c b/src/nxt_spinlock.c index 940be724e..685e9fc73 100644 --- a/src/nxt_spinlock.c +++ b/src/nxt_spinlock.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -28,14 +27,11 @@ /* It should be adjusted with the "spinlock_count" directive. */ -static nxt_uint_t nxt_spinlock_count = 1000; - +static nxt_uint_t nxt_spinlock_count = 1000; void -nxt_thread_spin_init(nxt_uint_t ncpu, nxt_uint_t count) -{ +nxt_thread_spin_init(nxt_uint_t ncpu, nxt_uint_t count) { switch (ncpu) { - case 0: /* Explicit spinlock count. */ nxt_spinlock_count = count; @@ -58,16 +54,13 @@ nxt_thread_spin_init(nxt_uint_t ncpu, nxt_uint_t count) } } - void -nxt_thread_spin_lock(nxt_thread_spinlock_t *lock) -{ - nxt_uint_t n; +nxt_thread_spin_lock(nxt_thread_spinlock_t *lock) { + nxt_uint_t n; nxt_thread_log_debug("spin_lock(%p) enter", lock); - for ( ;; ) { - + for (;;) { again: if (nxt_fast_path(nxt_atomic_try_lock(lock))) { @@ -75,7 +68,6 @@ nxt_thread_spin_lock(nxt_thread_spinlock_t *lock) } for (n = nxt_spinlock_count; n != 0; n--) { - nxt_cpu_pause(); if (*lock == 0) { @@ -87,10 +79,8 @@ nxt_thread_spin_lock(nxt_thread_spinlock_t *lock) } } - nxt_bool_t -nxt_thread_spin_trylock(nxt_thread_spinlock_t *lock) -{ +nxt_thread_spin_trylock(nxt_thread_spinlock_t *lock) { nxt_thread_log_debug("spin_trylock(%p) enter", lock); if (nxt_fast_path(nxt_atomic_try_lock(lock))) { @@ -102,10 +92,8 @@ nxt_thread_spin_trylock(nxt_thread_spinlock_t *lock) return 0; } - void -nxt_thread_spin_unlock(nxt_thread_spinlock_t *lock) -{ +nxt_thread_spin_unlock(nxt_thread_spinlock_t *lock) { nxt_atomic_release(lock); nxt_thread_log_debug("spin_unlock(%p) exit", lock); diff --git a/src/nxt_spinlock.h b/src/nxt_spinlock.h index 28e054451..2888ab534 100644 --- a/src/nxt_spinlock.h +++ b/src/nxt_spinlock.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,12 +7,16 @@ #define _NXT_UNIX_SPINLOCK_H_INCLUDED_ -typedef nxt_atomic_t nxt_thread_spinlock_t; +typedef nxt_atomic_t nxt_thread_spinlock_t; -NXT_EXPORT void nxt_thread_spin_init(nxt_uint_t ncpu, nxt_uint_t count); -NXT_EXPORT void nxt_thread_spin_lock(nxt_thread_spinlock_t *lock); -NXT_EXPORT nxt_bool_t nxt_thread_spin_trylock(nxt_thread_spinlock_t *lock); -NXT_EXPORT void nxt_thread_spin_unlock(nxt_thread_spinlock_t *lock); +NXT_EXPORT void +nxt_thread_spin_init(nxt_uint_t ncpu, nxt_uint_t count); +NXT_EXPORT void +nxt_thread_spin_lock(nxt_thread_spinlock_t *lock); +NXT_EXPORT nxt_bool_t +nxt_thread_spin_trylock(nxt_thread_spinlock_t *lock); +NXT_EXPORT void +nxt_thread_spin_unlock(nxt_thread_spinlock_t *lock); #endif /* _NXT_UNIX_SPINLOCK_H_INCLUDED_ */ diff --git a/src/nxt_sprintf.c b/src/nxt_sprintf.c index 875f43a59..3b8df51c3 100644 --- a/src/nxt_sprintf.c +++ b/src/nxt_sprintf.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,7 +7,6 @@ #include #include - /* * Supported formats: * @@ -55,11 +53,10 @@ */ -u_char * nxt_cdecl -nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...) -{ - u_char *p; - va_list args; +u_char *nxt_cdecl +nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...) { + u_char *p; + va_list args; va_start(args, fmt); p = nxt_vsprintf(buf, end, fmt, args); @@ -68,7 +65,6 @@ nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...) return p; } - /* * nxt_sprintf_t is used: * to pass several parameters of nxt_integer() via single pointer @@ -76,52 +72,49 @@ nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...) */ typedef struct { - u_char *end; - const u_char *hex; + u_char *end; + const u_char *hex; uint32_t width; int32_t frac_width; uint8_t max_width; u_char padding; } nxt_sprintf_t; - -static u_char *nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64); -static u_char *nxt_number(nxt_sprintf_t *spf, u_char *buf, double n); +static u_char * +nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64); +static u_char * +nxt_number(nxt_sprintf_t *spf, u_char *buf, double n); /* A right way of "f == 0.0". */ -#define nxt_double_is_zero(f) \ - (fabs(f) <= FLT_EPSILON) - +#define nxt_double_is_zero(f) (fabs(f) <= FLT_EPSILON) u_char * -nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) -{ - int d; - double f, i; - size_t length; - int64_t i64; - uint64_t ui64, frac; - nxt_str_t *v; - nxt_err_t err; - nxt_uint_t scale, n; - nxt_msec_t ms; - nxt_nsec_t ns; - nxt_bool_t sign; - const u_char *p; - nxt_sprintf_t spf; - nxt_file_name_t *fn; - - static const u_char hexadecimal[16] = "0123456789abcdef"; - static const u_char HEXADECIMAL[16] = "0123456789ABCDEF"; - static const u_char nan[] = "[nan]"; - static const u_char null[] = "[null]"; - static const u_char infinity[] = "[infinity]"; +nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) { + int d; + double f, i; + size_t length; + int64_t i64; + uint64_t ui64, frac; + nxt_str_t *v; + nxt_err_t err; + nxt_uint_t scale, n; + nxt_msec_t ms; + nxt_nsec_t ns; + nxt_bool_t sign; + const u_char *p; + nxt_sprintf_t spf; + nxt_file_name_t *fn; + + static const u_char hexadecimal[16] = "0123456789abcdef"; + static const u_char HEXADECIMAL[16] = "0123456789ABCDEF"; + static const u_char nan[] = "[nan]"; + static const u_char null[] = "[null]"; + static const u_char infinity[] = "[infinity]"; spf.end = end; while (*fmt != '\0' && buf < end) { - /* * "buf < end" means that we could copy at least one character: * a plain character, "%%", "%c", or a minus without test. @@ -137,14 +130,13 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) /* Test some often used text formats first. */ switch (*fmt) { - case 'V': fmt++; v = va_arg(args, nxt_str_t *); if (nxt_fast_path(v != NULL)) { length = v->length; - p = v->start; + p = v->start; goto copy; } @@ -189,15 +181,15 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) break; } - spf.hex = NULL; - spf.width = 0; + spf.hex = NULL; + spf.width = 0; spf.frac_width = -1; - spf.max_width = 0; - spf.padding = (*fmt == '0') ? '0' : ' '; + spf.max_width = 0; + spf.padding = (*fmt == '0') ? '0' : ' '; sign = 1; - i64 = 0; + i64 = 0; ui64 = 0; while (*fmt >= '0' && *fmt <= '9') { @@ -205,9 +197,8 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) } - for ( ;; ) { + for (;;) { switch (*fmt) { - case 'u': sign = 0; fmt++; @@ -220,13 +211,13 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) case 'X': spf.hex = HEXADECIMAL; - sign = 0; + sign = 0; fmt++; continue; case 'x': spf.hex = hexadecimal; - sign = 0; + sign = 0; fmt++; continue; @@ -249,14 +240,13 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) switch (*fmt) { - case 'E': err = va_arg(args, nxt_err_t); - *buf++ = '('; - spf.hex = NULL; + *buf++ = '('; + spf.hex = NULL; spf.width = 0; - buf = nxt_integer(&spf, buf, err); + buf = nxt_integer(&spf, buf, err); if (buf < end - 1) { *buf++ = ':'; @@ -273,19 +263,19 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) continue; case 'O': - i64 = (int64_t) va_arg(args, nxt_off_t); + i64 = (int64_t) va_arg(args, nxt_off_t); sign = 1; goto number; case 'T': - i64 = (int64_t) va_arg(args, nxt_time_t); + i64 = (int64_t) va_arg(args, nxt_time_t); sign = 1; goto number; case 'M': ms = (nxt_msec_t) va_arg(args, nxt_msec_t); if ((nxt_msec_int_t) ms == -1 && spf.hex == NULL) { - i64 = -1; + i64 = -1; sign = 1; } else { ui64 = (uint64_t) ms; @@ -296,7 +286,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) case 'N': ns = (nxt_nsec_t) va_arg(args, nxt_nsec_t); if ((nxt_nsec_int_t) ns == -1) { - i64 = -1; + i64 = -1; sign = 1; } else { ui64 = (uint64_t) ns; @@ -382,17 +372,17 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) if (f < 0) { *buf++ = '-'; - f = -f; + f = -f; } if (nxt_slow_path(isnan(f))) { - p = nan; + p = nan; length = nxt_length(nan); goto copy; } else if (nxt_slow_path(isinf(f))) { - p = infinity; + p = infinity; length = nxt_length(infinity); goto copy; @@ -402,7 +392,6 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) frac = 0; if (spf.frac_width > 0) { - scale = 1; for (n = spf.frac_width; n != 0; n--) { scale *= 10; @@ -411,22 +400,21 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) frac = (uint64_t) ((f - i) * scale + 0.5); if (frac == scale) { - i += 1; - frac = 0; + i += 1; + frac = 0; } } buf = nxt_number(&spf, buf, i); if (spf.frac_width > 0) { - if (buf < end) { *buf++ = '.'; - spf.hex = NULL; + spf.hex = NULL; spf.padding = '0'; - spf.width = spf.frac_width; - buf = nxt_integer(&spf, buf, frac); + spf.width = spf.frac_width; + buf = nxt_integer(&spf, buf, frac); } } else if (spf.frac_width < 0) { @@ -436,9 +424,9 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) *buf++ = '.'; while (!nxt_double_is_zero(f) && buf < end) { - f *= 10; - f = modf(f, &i); - *buf++ = (u_char) i + '0'; + f *= 10; + f = modf(f, &i); + *buf++ = (u_char) i + '0'; } } } @@ -446,13 +434,13 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) continue; case 'r': - i64 = (int64_t) va_arg(args, rlim_t); + i64 = (int64_t) va_arg(args, rlim_t); sign = 1; break; case 'p': - ui64 = (uintptr_t) va_arg(args, void *); - sign = 0; + ui64 = (uintptr_t) va_arg(args, void *); + sign = 0; spf.hex = HEXADECIMAL; /* * spf.width = NXT_PTR_SIZE * 2; @@ -461,7 +449,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) goto number; case 'c': - d = va_arg(args, int); + d = va_arg(args, int); *buf++ = (u_char) (d & 0xFF); fmt++; @@ -471,16 +459,15 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) fmt++; switch (*fmt) { - case 'D': - i64 = (int64_t) va_arg(args, nxt_fd_t); + i64 = (int64_t) va_arg(args, nxt_fd_t); sign = 1; goto number; case 'N': fn = va_arg(args, nxt_file_name_t *); - p = fn; + p = fn; while (*p != '\0' && buf < end) { *buf++ = *p++; @@ -497,9 +484,8 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) fmt++; switch (*fmt) { - case 'I': - i64 = (int64_t) va_arg(args, nxt_pid_t); + i64 = (int64_t) va_arg(args, nxt_pid_t); sign = 1; goto number; @@ -514,9 +500,9 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) goto number; #endif case 'H': - ui64 = (uint64_t) (uintptr_t) va_arg(args, pthread_t); + ui64 = (uint64_t) (uintptr_t) va_arg(args, pthread_t); spf.hex = HEXADECIMAL; - sign = 0; + sign = 0; goto number; default: @@ -548,7 +534,7 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) if (sign) { if (i64 < 0) { *buf++ = '-'; - ui64 = (uint64_t) -i64; + ui64 = (uint64_t) -i64; } else { ui64 = (uint64_t) i64; @@ -563,30 +549,27 @@ nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args) copy: length = nxt_min((size_t) (end - buf), length); - buf = nxt_cpymem(buf, p, length); + buf = nxt_cpymem(buf, p, length); continue; } return buf; } - static u_char * -nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) -{ - u_char *p, *end; +nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) { + u_char *p, *end; size_t length; u_char temp[NXT_INT64_T_LEN]; p = temp + NXT_INT64_T_LEN; if (spf->hex == NULL) { - #if (NXT_32BIT) - for ( ;; ) { - u_char *start; - uint32_t ui32; + for (;;) { + u_char *start; + uint32_t ui32; /* * 32-bit platforms usually lack hardware support of 64-bit @@ -613,17 +596,17 @@ nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) */ if (ui64 <= 0xFFFFFFFF) { - ui32 = (uint32_t) ui64; + ui32 = (uint32_t) ui64; start = NULL; } else { - ui32 = (uint32_t) (ui64 % 1000000000); + ui32 = (uint32_t) (ui64 % 1000000000); start = p - 9; } do { - *(--p) = (u_char) (ui32 % 10 + '0'); - ui32 /= 10; + *(--p) = (u_char) (ui32 % 10 + '0'); + ui32 /= 10; } while (ui32 != 0); if (start == NULL) { @@ -639,30 +622,28 @@ nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) ui64 /= 1000000000; } -#else /* NXT_64BIT */ +#else /* NXT_64BIT */ do { - *(--p) = (u_char) (ui64 % 10 + '0'); - ui64 /= 10; + *(--p) = (u_char) (ui64 % 10 + '0'); + ui64 /= 10; } while (ui64 != 0); #endif } else { - do { - *(--p) = spf->hex[ui64 & 0xF]; - ui64 >>= 4; + *(--p) = spf->hex[ui64 & 0xF]; + ui64 >>= 4; } while (ui64 != 0); } /* Zero or space padding. */ if (spf->width != 0) { - length = (temp + NXT_INT64_T_LEN) - p; - end = buf + (spf->width - length); - end = nxt_min(end, spf->end); + end = buf + (spf->width - length); + end = nxt_min(end, spf->end); while (buf < end) { *buf++ = spf->padding; @@ -672,8 +653,8 @@ nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) /* Number copying. */ length = (temp + NXT_INT64_T_LEN) - p; - end = buf + length; - end = nxt_min(end, spf->end); + end = buf + length; + end = nxt_min(end, spf->end); while (buf < end) { *buf++ = *p++; @@ -682,11 +663,9 @@ nxt_integer(nxt_sprintf_t *spf, u_char *buf, uint64_t ui64) return buf; } - static u_char * -nxt_number(nxt_sprintf_t *spf, u_char *buf, double n) -{ - u_char *p, *end; +nxt_number(nxt_sprintf_t *spf, u_char *buf, double n) { + u_char *p, *end; size_t length; u_char temp[NXT_DOUBLE_LEN]; @@ -694,15 +673,15 @@ nxt_number(nxt_sprintf_t *spf, u_char *buf, double n) do { *(--p) = (u_char) (fmod(n, 10) + '0'); - n = trunc(n / 10); + n = trunc(n / 10); } while (!nxt_double_is_zero(n)); /* Zero or space padding. */ if (spf->width != 0) { length = (temp + NXT_DOUBLE_LEN) - p; - end = buf + (spf->width - length); - end = nxt_min(end, spf->end); + end = buf + (spf->width - length); + end = nxt_min(end, spf->end); while (buf < end) { *buf++ = spf->padding; diff --git a/src/nxt_sprintf.h b/src/nxt_sprintf.h index 0a927f710..d33e03d1c 100644 --- a/src/nxt_sprintf.h +++ b/src/nxt_sprintf.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,13 +8,13 @@ /* STUB */ -#define NXT_DOUBLE_LEN (1 + DBL_MAX_10_EXP) +#define NXT_DOUBLE_LEN (1 + DBL_MAX_10_EXP) -NXT_EXPORT u_char *nxt_cdecl nxt_sprintf(u_char *buf, u_char *end, - const char *fmt, ...); -NXT_EXPORT u_char *nxt_vsprintf(u_char *buf, u_char *end, - const char *fmt, va_list args); +NXT_EXPORT u_char *nxt_cdecl +nxt_sprintf(u_char *buf, u_char *end, const char *fmt, ...); +NXT_EXPORT u_char * +nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args); #endif /* _NXT_SPRINTF_H_INCLUDED_ */ diff --git a/src/nxt_status.c b/src/nxt_status.c index 92cbf2e65..0fe8054c9 100644 --- a/src/nxt_status.c +++ b/src/nxt_status.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,43 +7,41 @@ #include #include - nxt_conf_value_t * -nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) -{ +nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) { size_t i, nr_langs; - uint16_t lang_cnts[NXT_APP_UNKNOWN] = { 1 }; - uint32_t idx = 0; + uint16_t lang_cnts[NXT_APP_UNKNOWN] = {1}; + uint32_t idx = 0; nxt_str_t name; nxt_int_t ret; - nxt_array_t *langs; - nxt_thread_t *thr; + nxt_array_t *langs; + nxt_thread_t *thr; nxt_app_type_t type, prev_type; - nxt_status_app_t *app; - nxt_conf_value_t *status, *obj, *mods, *apps, *app_obj, *mod_obj; - nxt_app_lang_module_t *modules; - - static const nxt_str_t modules_str = nxt_string("modules"); - static const nxt_str_t version_str = nxt_string("version"); - static const nxt_str_t lib_str = nxt_string("lib"); - static const nxt_str_t conns_str = nxt_string("connections"); - static const nxt_str_t acc_str = nxt_string("accepted"); - static const nxt_str_t active_str = nxt_string("active"); - static const nxt_str_t idle_str = nxt_string("idle"); - static const nxt_str_t closed_str = nxt_string("closed"); - static const nxt_str_t reqs_str = nxt_string("requests"); - static const nxt_str_t total_str = nxt_string("total"); - static const nxt_str_t apps_str = nxt_string("applications"); - static const nxt_str_t procs_str = nxt_string("processes"); - static const nxt_str_t run_str = nxt_string("running"); - static const nxt_str_t start_str = nxt_string("starting"); + nxt_status_app_t *app; + nxt_conf_value_t *status, *obj, *mods, *apps, *app_obj, *mod_obj; + nxt_app_lang_module_t *modules; + + static const nxt_str_t modules_str = nxt_string("modules"); + static const nxt_str_t version_str = nxt_string("version"); + static const nxt_str_t lib_str = nxt_string("lib"); + static const nxt_str_t conns_str = nxt_string("connections"); + static const nxt_str_t acc_str = nxt_string("accepted"); + static const nxt_str_t active_str = nxt_string("active"); + static const nxt_str_t idle_str = nxt_string("idle"); + static const nxt_str_t closed_str = nxt_string("closed"); + static const nxt_str_t reqs_str = nxt_string("requests"); + static const nxt_str_t total_str = nxt_string("total"); + static const nxt_str_t apps_str = nxt_string("applications"); + static const nxt_str_t procs_str = nxt_string("processes"); + static const nxt_str_t run_str = nxt_string("running"); + static const nxt_str_t start_str = nxt_string("starting"); status = nxt_conf_create_object(mp, 4); if (nxt_slow_path(status == NULL)) { return NULL; } - thr = nxt_thread(); + thr = nxt_thread(); langs = thr->runtime->languages; modules = langs->elts; @@ -58,8 +55,7 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) * first entry. */ for (i = 1, nr_langs = 0, prev_type = NXT_APP_UNKNOWN; i < langs->nelts; - i++) - { + i++) { type = modules[i].type; lang_cnts[type]++; @@ -79,11 +75,11 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) nxt_conf_set_member(status, &modules_str, mods, idx++); - i = 1; + i = 1; obj = mod_obj = NULL; - prev_type = NXT_APP_UNKNOWN; + prev_type = NXT_APP_UNKNOWN; for (size_t l = 0, a = 0; i < langs->nelts; i++) { - nxt_str_t item, mod_name; + nxt_str_t item, mod_name; type = modules[i].type; if (type != prev_type) { @@ -91,7 +87,7 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) if (lang_cnts[type] == 1) { mod_obj = nxt_conf_create_object(mp, 2); - obj = mod_obj; + obj = mod_obj; } else { mod_obj = nxt_conf_create_array(mp, lang_cnts[type]); } @@ -100,7 +96,7 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) return NULL; } - mod_name.start = (u_char *)modules[i].name; + mod_name.start = (u_char *) modules[i].name; mod_name.length = strlen(modules[i].name); nxt_conf_set_member(mods, &mod_name, mod_obj, l++); } @@ -114,11 +110,11 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) nxt_conf_set_element(mod_obj, a++, obj); } - item.start = modules[i].version; + item.start = modules[i].version; item.length = nxt_strlen(modules[i].version); nxt_conf_set_member_string(obj, &version_str, &item, 0); - item.start = (u_char *)modules[i].file; + item.start = (u_char *) modules[i].file; item.length = strlen(modules[i].file); nxt_conf_set_member_string(obj, &lib_str, &item, 1); @@ -133,9 +129,8 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) nxt_conf_set_member(status, &conns_str, obj, idx++); nxt_conf_set_member_integer(obj, &acc_str, report->accepted_conns, 0); - nxt_conf_set_member_integer(obj, &active_str, report->accepted_conns - - report->closed_conns - - report->idle_conns, 1); + nxt_conf_set_member_integer(obj, &active_str, + report->accepted_conns - report->closed_conns - report->idle_conns, 1); nxt_conf_set_member_integer(obj, &idle_str, report->idle_conns, 2); nxt_conf_set_member_integer(obj, &closed_str, report->closed_conns, 3); @@ -164,7 +159,7 @@ nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp) } name.length = app->name.length; - name.start = nxt_pointer_to(report, (uintptr_t) app->name.start); + name.start = nxt_pointer_to(report, (uintptr_t) app->name.start); ret = nxt_conf_set_member_dup(apps, mp, &name, app_obj, i); if (nxt_slow_path(ret != NXT_OK)) { diff --git a/src/nxt_status.h b/src/nxt_status.h index a99ac7d0e..b8400032e 100644 --- a/src/nxt_status.h +++ b/src/nxt_status.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,28 +5,26 @@ #ifndef _NXT_STATUS_H_INCLUDED_ #define _NXT_STATUS_H_INCLUDED_ - typedef struct { - nxt_str_t name; - uint32_t active_requests; - uint32_t pending_processes; - uint32_t processes; - uint32_t idle_processes; + nxt_str_t name; + uint32_t active_requests; + uint32_t pending_processes; + uint32_t processes; + uint32_t idle_processes; } nxt_status_app_t; - typedef struct { - uint64_t accepted_conns; - uint64_t idle_conns; - uint64_t closed_conns; - uint64_t requests; + uint64_t accepted_conns; + uint64_t idle_conns; + uint64_t closed_conns; + uint64_t requests; - size_t apps_count; - nxt_status_app_t apps[]; + size_t apps_count; + nxt_status_app_t apps[]; } nxt_status_report_t; - -nxt_conf_value_t *nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp); +nxt_conf_value_t * +nxt_status_get(nxt_status_report_t *report, nxt_mp_t *mp); #endif /* _NXT_STATUS_H_INCLUDED_ */ diff --git a/src/nxt_string.c b/src/nxt_string.c index 1ca595a1c..a9f0a95a3 100644 --- a/src/nxt_string.c +++ b/src/nxt_string.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,24 +5,21 @@ #include - nxt_str_t * -nxt_str_alloc(nxt_mp_t *mp, size_t length) -{ - nxt_str_t *s; +nxt_str_alloc(nxt_mp_t *mp, size_t length) { + nxt_str_t *s; /* The string start is allocated aligned to be close to nxt_str_t. */ s = nxt_mp_get(mp, sizeof(nxt_str_t) + length); if (nxt_fast_path(s != NULL)) { s->length = length; - s->start = nxt_pointer_to(s, sizeof(nxt_str_t)); + s->start = nxt_pointer_to(s, sizeof(nxt_str_t)); } return s; } - /* * nxt_str_dup() creates a new string with a copy of a source string. * If length of the source string is zero, then the new string anyway @@ -31,9 +27,8 @@ nxt_str_alloc(nxt_mp_t *mp, size_t length) */ nxt_str_t * -nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src) -{ - u_char *p; +nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src) { + u_char *p; if (dst == NULL) { /* The string start is allocated aligned to be close to nxt_str_t. */ @@ -42,9 +37,9 @@ nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src) return NULL; } - p = (u_char *) dst; - p += sizeof(nxt_str_t); - dst->start = p; + p = (u_char *) dst; + p += sizeof(nxt_str_t); + dst->start = p; } else { dst->start = nxt_mp_nget(mp, src->length); @@ -59,7 +54,6 @@ nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src) return dst; } - /* * nxt_str_cstrz() creates a C style zero-terminated copy of a source * nxt_str_t. The function is intended to create strings suitable @@ -68,51 +62,44 @@ nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src) */ char * -nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src) -{ - char *p, *dst; +nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src) { + char *p, *dst; dst = nxt_mp_alloc(mp, src->length + 1); if (nxt_fast_path(dst != NULL)) { - p = nxt_cpymem(dst, src->start, src->length); + p = nxt_cpymem(dst, src->start, src->length); *p = '\0'; } return dst; } - void -nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t length) -{ - u_char c; +nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t length) { + u_char c; while (length != 0) { - c = *src++; + c = *src++; *dst++ = nxt_lowcase(c); length--; } } - void -nxt_memcpy_upcase(u_char *dst, const u_char *src, size_t length) -{ - u_char c; +nxt_memcpy_upcase(u_char *dst, const u_char *src, size_t length) { + u_char c; while (length != 0) { - c = *src++; + c = *src++; *dst++ = nxt_upcase(c); length--; } } - u_char * -nxt_cpystr(u_char *dst, const u_char *src) -{ - for ( ;; ) { +nxt_cpystr(u_char *dst, const u_char *src) { + for (;;) { *dst = *src; if (*dst == '\0') { @@ -126,10 +113,8 @@ nxt_cpystr(u_char *dst, const u_char *src) return dst; } - u_char * -nxt_cpystrn(u_char *dst, const u_char *src, size_t length) -{ +nxt_cpystrn(u_char *dst, const u_char *src, size_t length) { if (length == 0) { return dst; } @@ -150,14 +135,12 @@ nxt_cpystrn(u_char *dst, const u_char *src, size_t length) return dst; } - nxt_int_t -nxt_strcasecmp(const u_char *s1, const u_char *s2) -{ - u_char c1, c2; - nxt_int_t n; +nxt_strcasecmp(const u_char *s1, const u_char *s2) { + u_char c1, c2; + nxt_int_t n; - for ( ;; ) { + for (;;) { c1 = *s1++; c2 = *s2++; @@ -176,12 +159,10 @@ nxt_strcasecmp(const u_char *s1, const u_char *s2) } } - nxt_int_t -nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t length) -{ - u_char c1, c2; - nxt_int_t n; +nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t length) { + u_char c1, c2; + nxt_int_t n; while (length-- != 0) { c1 = *s1++; @@ -204,13 +185,11 @@ nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t length) return 0; } - nxt_int_t -nxt_memcasecmp(const void *p1, const void *p2, size_t length) -{ +nxt_memcasecmp(const void *p1, const void *p2, size_t length) { u_char c1, c2; nxt_int_t n; - const u_char *s1, *s2; + const u_char *s1, *s2; s1 = p1; s2 = p2; @@ -232,7 +211,6 @@ nxt_memcasecmp(const void *p1, const void *p2, size_t length) return 0; } - /* * nxt_memstrn() is intended for search of static substring "ss" * with known length "length" in string "s" limited by parameter "end". @@ -240,9 +218,8 @@ nxt_memcasecmp(const void *p1, const void *p2, size_t length) */ u_char * -nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length) -{ - u_char c1, c2, *s2; +nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length) { + u_char c1, c2, *s2; s2 = (u_char *) ss; c2 = *s2++; @@ -252,7 +229,6 @@ nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length) c1 = *s++; if (c1 == c2) { - if (s + length > end) { return NULL; } @@ -266,7 +242,6 @@ nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length) return NULL; } - /* * nxt_strcasestrn() is intended for caseless search of static substring * "ss" with known length "length" in string "s" limited by parameter "end". @@ -275,9 +250,8 @@ nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length) u_char * nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, - size_t length) -{ - u_char c1, c2, *s2; + size_t length) { + u_char c1, c2, *s2; s2 = (u_char *) ss; c2 = *s2++; @@ -289,7 +263,6 @@ nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, c1 = nxt_lowcase(c1); if (c1 == c2) { - if (s + length > end) { return NULL; } @@ -303,7 +276,6 @@ nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, return NULL; } - /* * nxt_rstrstrn() is intended to search for static substring "ss" * with known length "length" in string "s" limited by parameter "end" @@ -311,10 +283,10 @@ nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, */ u_char * -nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length) -{ +nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, + size_t length) { u_char c1, c2; - const u_char *s1, *s2; + const u_char *s1, *s2; s1 = end - length; s2 = (u_char *) ss; @@ -336,11 +308,9 @@ nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length) return NULL; } - size_t -nxt_str_strip(const u_char *start, u_char *end) -{ - u_char *p; +nxt_str_strip(const u_char *start, u_char *end) { + u_char *p; for (p = end - 1; p >= start; p--) { if (*p != '\r' && *p != '\n') { @@ -351,12 +321,10 @@ nxt_str_strip(const u_char *start, u_char *end) return (p + 1) - start; } - nxt_int_t -nxt_strverscmp(const u_char *s1, const u_char *s2) -{ - u_char c1, c2; - nxt_int_t diff; +nxt_strverscmp(const u_char *s1, const u_char *s2) { + u_char c1, c2; + nxt_int_t diff; enum { st_str = 0, @@ -367,7 +335,7 @@ nxt_strverscmp(const u_char *s1, const u_char *s2) state = st_str; - for ( ;; ) { + for (;;) { c1 = *s1++; c2 = *s2++; @@ -398,7 +366,6 @@ nxt_strverscmp(const u_char *s1, const u_char *s2) } switch (state) { - case st_str: if ((u_char) (c1 - '1') > 8 || (u_char) (c2 - '1') > 8) { @@ -445,18 +412,15 @@ nxt_strverscmp(const u_char *s1, const u_char *s2) } } - nxt_bool_t -nxt_strvers_match(u_char *version, u_char *prefix, size_t length) -{ - u_char next, last; +nxt_strvers_match(u_char *version, u_char *prefix, size_t length) { + u_char next, last; if (length == 0) { return 1; } if (nxt_strncmp(version, prefix, length) == 0) { - next = version[length]; if (next == '\0') { @@ -474,30 +438,267 @@ nxt_strvers_match(u_char *version, u_char *prefix, size_t length) return 0; } - -const uint8_t nxt_hex2int[256] - nxt_aligned(32) = -{ - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 16, 16, 16, 16, 16, - 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, - 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, +const uint8_t nxt_hex2int[256] nxt_aligned(32) = { + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, + 16, }; -static const uint32_t nxt_uri_escape[] = { +static const uint32_t nxt_uri_escape[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ @@ -515,12 +716,10 @@ static const uint32_t nxt_uri_escape[] = { 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; - u_char * -nxt_decode_uri(u_char *dst, u_char *src, size_t length) -{ - u_char *end, ch; - uint8_t d0, d1; +nxt_decode_uri(u_char *dst, u_char *src, size_t length) { + u_char *end, ch; + uint8_t d0, d1; nxt_prefetch(&nxt_hex2int['0']); @@ -550,12 +749,10 @@ nxt_decode_uri(u_char *dst, u_char *src, size_t length) return dst; } - u_char * -nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length) -{ - u_char *end, ch; - uint8_t d0, d1; +nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length) { + u_char *end, ch; + uint8_t d0, d1; nxt_prefetch(&nxt_hex2int['0']); @@ -591,25 +788,21 @@ nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length) return dst; } - uintptr_t -nxt_encode_uri(u_char *dst, u_char *src, size_t length) -{ - u_char *end; - nxt_uint_t n; +nxt_encode_uri(u_char *dst, u_char *src, size_t length) { + u_char *end; + nxt_uint_t n; - static const u_char hex[16] = "0123456789ABCDEF"; + static const u_char hex[16] = "0123456789ABCDEF"; end = src + length; if (dst == NULL) { - /* Find the number of the characters to be escaped. */ n = 0; while (src < end) { - if (nxt_uri_escape[*src >> 5] & (1U << (*src & 0x1f))) { n++; } @@ -621,7 +814,6 @@ nxt_encode_uri(u_char *dst, u_char *src, size_t length) } while (src < end) { - if (nxt_uri_escape[*src >> 5] & (1U << (*src & 0x1f))) { *dst++ = '%'; *dst++ = hex[*src >> 4]; @@ -637,21 +829,18 @@ nxt_encode_uri(u_char *dst, u_char *src, size_t length) return (uintptr_t) dst; } - uintptr_t -nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length) -{ - u_char *reserved, *end, ch; - nxt_uint_t n; +nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length) { + u_char *reserved, *end, ch; + nxt_uint_t n; - static const u_char hex[16] = "0123456789ABCDEF"; + static const u_char hex[16] = "0123456789ABCDEF"; reserved = (u_char *) "?#\0"; end = src + length; if (dst == NULL) { - /* Find the number of the characters to be escaped. */ n = 0; @@ -701,12 +890,10 @@ nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length) return (uintptr_t) dst; } - nxt_bool_t -nxt_is_complex_uri_encoded(u_char *src, size_t length) -{ - u_char *reserved, *end, ch; - uint8_t d0, d1; +nxt_is_complex_uri_encoded(u_char *src, size_t length) { + u_char *reserved, *end, ch; + uint8_t d0, d1; reserved = (u_char *) "?#\0"; @@ -746,33 +933,29 @@ nxt_is_complex_uri_encoded(u_char *src, size_t length) return 1; } - ssize_t -nxt_base64_decode(u_char *dst, u_char *src, size_t length) -{ - u_char *end, *p; - size_t pad; - uint8_t v1, v2, v3, v4; - - static const uint8_t decode[] = { - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77, - 77, 0, 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, 77, 77, 77, 77, 77, - 77, 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, 77, 77, 77, 77, 77, - - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, - 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 - }; +nxt_base64_decode(u_char *dst, u_char *src, size_t length) { + u_char *end, *p; + size_t pad; + uint8_t v1, v2, v3, v4; + + static const uint8_t decode[] = {77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, + 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77, 77, + 0, 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, 77, 77, 77, 77, 77, 77, 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, 77, 77, 77, 77, 77, + + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, + 77, 77}; end = src + length; pad = (4 - (length % 4)) % 4; @@ -791,8 +974,8 @@ nxt_base64_decode(u_char *dst, u_char *src, size_t length) if (pad == 0) { pad = end - src; - if ((pad == 1 || (pad == 2 && src[1] == '=')) && src[0] == '=') - { + if ((pad == 1 || (pad == 2 && src[1] == '=')) + && src[0] == '=') { break; } } @@ -806,7 +989,7 @@ nxt_base64_decode(u_char *dst, u_char *src, size_t length) nxt_assert(length != 0); if (pad == 0) { - pad = (end[-1] == '=') + (end[-2] == '='); + pad = (end[-1] == '=') + (end[-2] == '='); end -= (pad + 3) & 4; } else { @@ -835,7 +1018,7 @@ nxt_base64_decode(u_char *dst, u_char *src, size_t length) *p++ = (v1 << 2 | v2 >> 4); if (pad == 1) { - v3 = decode[src[2]]; + v3 = decode[src[2]]; *p++ = (v2 << 4 | v3 >> 2); } } diff --git a/src/nxt_string.h b/src/nxt_string.h index 18ea5490d..570ff42c8 100644 --- a/src/nxt_string.h +++ b/src/nxt_string.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,160 +7,153 @@ #define _NXT_STRING_H_INCLUDED_ -#define nxt_lowcase(c) \ - (u_char) ((c >= 'A' && c <= 'Z') ? c | 0x20 : c) +#define nxt_lowcase(c) (u_char)((c >= 'A' && c <= 'Z') ? c | 0x20 : c) -#define nxt_upcase(c) \ - (u_char) ((c >= 'a' && c <= 'z') ? c & ~0x20 : c) +#define nxt_upcase(c) (u_char)((c >= 'a' && c <= 'z') ? c & ~0x20 : c) -#define nxt_isdigit(c) \ - ((u_char) ((c) - '0') <= 9) +#define nxt_isdigit(c) ((u_char) ((c) - '0') <= 9) -#define nxt_strtod(s, endptr) \ - strtod((char *) s, (char **) endptr) +#define nxt_strtod(s, endptr) strtod((char *) s, (char **) endptr) -#define nxt_strlen(s) \ - strlen((char *) s) +#define nxt_strlen(s) strlen((char *) s) -#define nxt_strdup(s) \ - strdup((char *) s) +#define nxt_strdup(s) strdup((char *) s) -#define nxt_strchr(buf, delim) \ - (u_char *) strchr((char *) buf, delim) +#define nxt_strchr(buf, delim) (u_char *) strchr((char *) buf, delim) -#define nxt_memzero(buf, length) \ - (void) memset(buf, 0, length) +#define nxt_memzero(buf, length) (void) memset(buf, 0, length) -#define nxt_memset(buf, c, length) \ - (void) memset(buf, c, length) +#define nxt_memset(buf, c, length) (void) memset(buf, c, length) -#define nxt_memcpy(dst, src, length) \ - (void) memcpy(dst, src, length) +#define nxt_memcpy(dst, src, length) (void) memcpy(dst, src, length) -NXT_EXPORT void nxt_memcpy_lowcase(u_char *dst, const u_char *src, - size_t length); -NXT_EXPORT void nxt_memcpy_upcase(u_char *dst, const u_char *src, - size_t length); - +NXT_EXPORT void +nxt_memcpy_lowcase(u_char *dst, const u_char *src, size_t length); +NXT_EXPORT void +nxt_memcpy_upcase(u_char *dst, const u_char *src, size_t length); /* * nxt_cpymem() is an inline function but not a macro to * eliminate possible double evaluation of length "length". */ nxt_inline void * -nxt_cpymem(void *dst, const void *src, size_t length) -{ +nxt_cpymem(void *dst, const void *src, size_t length) { return ((u_char *) memcpy(dst, src, length)) + length; } +#define nxt_memmove(dst, src, length) (void) memmove(dst, src, length) -#define nxt_memmove(dst, src, length) \ - (void) memmove(dst, src, length) +#define nxt_strcmp(s1, s2) strcmp((char *) s1, (char *) s2) -#define nxt_strcmp(s1, s2) \ - strcmp((char *) s1, (char *) s2) +#define nxt_strncmp(s1, s2, length) strncmp((char *) s1, (char *) s2, length) -#define nxt_strncmp(s1, s2, length) \ - strncmp((char *) s1, (char *) s2, length) +NXT_EXPORT u_char * +nxt_cpystr(u_char *dst, const u_char *src); +NXT_EXPORT u_char * +nxt_cpystrn(u_char *dst, const u_char *src, size_t length); +NXT_EXPORT nxt_int_t +nxt_strcasecmp(const u_char *s1, const u_char *s2); +NXT_EXPORT nxt_int_t +nxt_strncasecmp(const u_char *s1, const u_char *s2, size_t length); +NXT_EXPORT nxt_int_t +nxt_memcasecmp(const void *p1, const void *p2, size_t length); -NXT_EXPORT u_char *nxt_cpystr(u_char *dst, const u_char *src); -NXT_EXPORT u_char *nxt_cpystrn(u_char *dst, const u_char *src, size_t length); -NXT_EXPORT nxt_int_t nxt_strcasecmp(const u_char *s1, const u_char *s2); -NXT_EXPORT nxt_int_t nxt_strncasecmp(const u_char *s1, const u_char *s2, - size_t length); -NXT_EXPORT nxt_int_t nxt_memcasecmp(const void *p1, const void *p2, +NXT_EXPORT u_char * +nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length); +NXT_EXPORT u_char * +nxt_memcasestrn(const u_char *s, const u_char *end, const char *ss, size_t length); - -NXT_EXPORT u_char *nxt_memstrn(const u_char *s, const u_char *end, - const char *ss, size_t length); -NXT_EXPORT u_char *nxt_memcasestrn(const u_char *s, const u_char *end, - const char *ss, size_t length); -NXT_EXPORT u_char *nxt_rmemstrn(const u_char *s, const u_char *end, - const char *ss, size_t length); -NXT_EXPORT size_t nxt_str_strip(const u_char *start, u_char *end); - +NXT_EXPORT u_char * +nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length); +NXT_EXPORT size_t +nxt_str_strip(const u_char *start, u_char *end); typedef struct { - size_t length; - u_char *start; + size_t length; + u_char *start; } nxt_str_t; - -#define nxt_string(str) { nxt_length(str), (u_char *) str } -#define nxt_string_zero(str) { sizeof(str), (u_char *) str } -#define nxt_null_string { 0, NULL } +#define nxt_string(str) {nxt_length(str), (u_char *) str} +#define nxt_string_zero(str) {sizeof(str), (u_char *) str} +#define nxt_null_string {0, NULL} -#define nxt_str_set(str, text) \ - do { \ - (str)->length = nxt_length(text); \ - (str)->start = (u_char *) text; \ +#define nxt_str_set(str, text) \ + do { \ + (str)->length = nxt_length(text); \ + (str)->start = (u_char *) text; \ } while (0) -#define nxt_str_null(str) \ - do { \ - (str)->length = 0; \ - (str)->start = NULL; \ +#define nxt_str_null(str) \ + do { \ + (str)->length = 0; \ + (str)->start = NULL; \ } while (0) -NXT_EXPORT nxt_str_t *nxt_str_alloc(nxt_mp_t *mp, size_t length); -NXT_EXPORT nxt_str_t *nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, - const nxt_str_t *src); -NXT_EXPORT char *nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src); +NXT_EXPORT nxt_str_t * +nxt_str_alloc(nxt_mp_t *mp, size_t length); +NXT_EXPORT nxt_str_t * +nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst, const nxt_str_t *src); +NXT_EXPORT char * +nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src); -#define nxt_strstr_eq(s1, s2) \ - (((s1)->length == (s2)->length) \ - && (memcmp((s1)->start, (s2)->start, (s1)->length) == 0)) +#define nxt_strstr_eq(s1, s2) \ + (((s1)->length == (s2)->length) \ + && (memcmp((s1)->start, (s2)->start, (s1)->length) == 0)) -#define nxt_strcasestr_eq(s1, s2) \ - (((s1)->length == (s2)->length) \ - && (nxt_memcasecmp((s1)->start, (s2)->start, (s1)->length) == 0)) +#define nxt_strcasestr_eq(s1, s2) \ + (((s1)->length == (s2)->length) \ + && (nxt_memcasecmp((s1)->start, (s2)->start, (s1)->length) == 0)) -#define nxt_str_eq(s, p, _length) \ +#define nxt_str_eq(s, p, _length) \ (((s)->length == _length) && (memcmp((s)->start, p, _length) == 0)) -#define nxt_str_start(s, p, _length) \ +#define nxt_str_start(s, p, _length) \ (((s)->length >= _length) && (memcmp((s)->start, p, _length) == 0)) -#define nxt_strchr_eq(s, c) \ - (((s)->length == 1) && ((s)->start[0] == c)) +#define nxt_strchr_eq(s, c) (((s)->length == 1) && ((s)->start[0] == c)) -#define nxt_strchr_start(s, c) \ - (((s)->length != 0) && ((s)->start[0] == c)) +#define nxt_strchr_start(s, c) (((s)->length != 0) && ((s)->start[0] == c)) -NXT_EXPORT nxt_int_t nxt_strverscmp(const u_char *s1, const u_char *s2); -NXT_EXPORT nxt_bool_t nxt_strvers_match(u_char *version, u_char *prefix, - size_t length); +NXT_EXPORT nxt_int_t +nxt_strverscmp(const u_char *s1, const u_char *s2); +NXT_EXPORT nxt_bool_t +nxt_strvers_match(u_char *version, u_char *prefix, size_t length); -NXT_EXPORT u_char *nxt_decode_uri(u_char *dst, u_char *src, size_t length); -NXT_EXPORT u_char *nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length); -NXT_EXPORT uintptr_t nxt_encode_uri(u_char *dst, u_char *src, size_t length); -NXT_EXPORT uintptr_t nxt_encode_complex_uri(u_char *dst, u_char *src, - size_t length); -NXT_EXPORT nxt_bool_t nxt_is_complex_uri_encoded(u_char *s, size_t length); +NXT_EXPORT u_char * +nxt_decode_uri(u_char *dst, u_char *src, size_t length); +NXT_EXPORT u_char * +nxt_decode_uri_plus(u_char *dst, u_char *src, size_t length); +NXT_EXPORT uintptr_t +nxt_encode_uri(u_char *dst, u_char *src, size_t length); +NXT_EXPORT uintptr_t +nxt_encode_complex_uri(u_char *dst, u_char *src, size_t length); +NXT_EXPORT nxt_bool_t +nxt_is_complex_uri_encoded(u_char *s, size_t length); -NXT_EXPORT ssize_t nxt_base64_decode(u_char *dst, u_char *src, size_t length); +NXT_EXPORT ssize_t +nxt_base64_decode(u_char *dst, u_char *src, size_t length); -extern const uint8_t nxt_hex2int[256]; +extern const uint8_t nxt_hex2int[256]; #endif /* _NXT_STRING_H_INCLUDED_ */ diff --git a/src/nxt_test_build.c b/src/nxt_test_build.c index aefd8c13d..cb0f7e79a 100644 --- a/src/nxt_test_build.c +++ b/src/nxt_test_build.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,42 +10,32 @@ #if (NXT_TEST_BUILD_EPOLL) int -epoll_create(int size) -{ +epoll_create(int size) { return -1; } - int -epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) -{ +epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { return -1; } - int -epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout) -{ +epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout) { return -1; } int -eventfd(u_int initval, int flags) -{ +eventfd(u_int initval, int flags) { return -1; } - int -signalfd(int fd, const sigset_t *mask, int flags) -{ +signalfd(int fd, const sigset_t *mask, int flags) { return -1; } - int -accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) -{ +accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { return -1; } @@ -56,36 +45,28 @@ accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) #if (NXT_TEST_BUILD_EVENTPORT) int -port_create(void) -{ +port_create(void) { return -1; } - int -port_associate(int port, int source, uintptr_t object, int events, void *user) -{ +port_associate(int port, int source, uintptr_t object, int events, void *user) { return -1; } - int -port_dissociate(int port, int source, uintptr_t object) -{ +port_dissociate(int port, int source, uintptr_t object) { return -1; } - int -port_send(int port, int events, void *user) -{ +port_send(int port, int events, void *user) { return -1; } - -int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, - const timespec_t *timeout) -{ +int +port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, + const timespec_t *timeout) { return -1; } @@ -95,37 +76,28 @@ int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, #if (NXT_TEST_BUILD_POLLSET) pollset_t -pollset_create(int maxfd) -{ +pollset_create(int maxfd) { return -1; } - int -pollset_destroy(pollset_t ps) -{ +pollset_destroy(pollset_t ps) { return -1; } - int -pollset_query(pollset_t ps, struct pollfd *pollfd_query) -{ +pollset_query(pollset_t ps, struct pollfd *pollfd_query) { return -1; } - int -pollset_ctl(pollset_t ps, struct poll_ctl *pollctl_array, int array_length) -{ +pollset_ctl(pollset_t ps, struct poll_ctl *pollctl_array, int array_length) { return -1; } - int pollset_poll(pollset_t ps, struct pollfd *polldata_array, int array_length, - int timeout) -{ + int timeout) { return -1; } @@ -134,9 +106,8 @@ pollset_poll(pollset_t ps, struct pollfd *polldata_array, int array_length, #if (NXT_TEST_BUILD_SOLARIS_SENDFILEV) -ssize_t sendfilev(int fd, const struct sendfilevec *vec, - int sfvcnt, size_t *xferred) -{ +ssize_t +sendfilev(int fd, const struct sendfilevec *vec, int sfvcnt, size_t *xferred) { return -1; } @@ -145,8 +116,8 @@ ssize_t sendfilev(int fd, const struct sendfilevec *vec, #if (NXT_TEST_BUILD_AIX_SEND_FILE) -ssize_t send_file(int *s, struct sf_parms *sf_iobuf, uint_t flags) -{ +ssize_t +send_file(int *s, struct sf_parms *sf_iobuf, uint_t flags) { return -1; } diff --git a/src/nxt_test_build.h b/src/nxt_test_build.h index 6513e748f..55aa186e0 100644 --- a/src/nxt_test_build.h +++ b/src/nxt_test_build.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,84 +10,86 @@ #if (NXT_TEST_BUILD_EPOLL) -#define NXT_HAVE_EPOLL 1 -#define NXT_HAVE_EPOLL_EDGE 1 -#define NXT_HAVE_EVENTFD 1 -#define NXT_HAVE_SIGNALFD 1 -#define NXT_HAVE_ACCEPT4 1 +#define NXT_HAVE_EPOLL 1 +#define NXT_HAVE_EPOLL_EDGE 1 +#define NXT_HAVE_EVENTFD 1 +#define NXT_HAVE_SIGNALFD 1 +#define NXT_HAVE_ACCEPT4 1 /* Linux epoll declarations */ -#define EPOLLIN 0x00000001 -#define EPOLLPRI 0x00000002 -#define EPOLLOUT 0x00000004 -#define EPOLLERR 0x00000008 -#define EPOLLHUP 0x00000010 -#define EPOLLRDNORM 0x00000040 -#define EPOLLRDBAND 0x00000080 -#define EPOLLWRNORM 00000x0100 -#define EPOLLWRBAND 0x00000200 -#define EPOLLMSG 0x00000400 -#define EPOLLRDHUP 0x00002000 - -#define EPOLLET 0x80000000 -#define EPOLLONESHOT 0x40000000 +#define EPOLLIN 0x00000001 +#define EPOLLPRI 0x00000002 +#define EPOLLOUT 0x00000004 +#define EPOLLERR 0x00000008 +#define EPOLLHUP 0x00000010 +#define EPOLLRDNORM 0x00000040 +#define EPOLLRDBAND 0x00000080 +#define EPOLLWRNORM 00000x0100 +#define EPOLLWRBAND 0x00000200 +#define EPOLLMSG 0x00000400 +#define EPOLLRDHUP 0x00002000 -#define EPOLL_CTL_ADD 1 -#define EPOLL_CTL_DEL 2 -#define EPOLL_CTL_MOD 3 +#define EPOLLET 0x80000000 +#define EPOLLONESHOT 0x40000000 -#define EFD_SEMAPHORE 1 -#define EFD_NONBLOCK 04000 +#define EPOLL_CTL_ADD 1 +#define EPOLL_CTL_DEL 2 +#define EPOLL_CTL_MOD 3 +#define EFD_SEMAPHORE 1 +#define EFD_NONBLOCK 04000 typedef union epoll_data { - void *ptr; - int fd; - uint32_t u32; - uint64_t u64; + void *ptr; + int fd; + uint32_t u32; + uint64_t u64; } epoll_data_t; - struct epoll_event { - uint32_t events; - epoll_data_t data; + uint32_t events; + epoll_data_t data; }; - struct signalfd_siginfo { - uint32_t ssi_signo; /* Signal number */ - int32_t ssi_errno; /* Error number (unused) */ - int32_t ssi_code; /* Signal code */ - uint32_t ssi_pid; /* PID of sender */ - uint32_t ssi_uid; /* Real UID of sender */ - int32_t ssi_fd; /* File descriptor (SIGIO) */ - uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */ - uint32_t ssi_band; /* Band event (SIGIO) */ - uint32_t ssi_overrun; /* POSIX timer overrun count */ - uint32_t ssi_trapno; /* Trap number that caused signal */ - int32_t ssi_status; /* Exit status or signal (SIGCHLD) */ - int32_t ssi_int; /* Integer sent by sigqueue(2) */ - uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */ - uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */ - uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */ - uint64_t ssi_addr; /* Address that generated signal - (for hardware-generated signals) */ - uint8_t pad[8]; /* Pad size to 128 bytes (allow for - additional fields in the future) */ + uint32_t ssi_signo; /* Signal number */ + int32_t ssi_errno; /* Error number (unused) */ + int32_t ssi_code; /* Signal code */ + uint32_t ssi_pid; /* PID of sender */ + uint32_t ssi_uid; /* Real UID of sender */ + int32_t ssi_fd; /* File descriptor (SIGIO) */ + uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */ + uint32_t ssi_band; /* Band event (SIGIO) */ + uint32_t ssi_overrun; /* POSIX timer overrun count */ + uint32_t ssi_trapno; /* Trap number that caused signal */ + int32_t ssi_status; /* Exit status or signal (SIGCHLD) */ + int32_t ssi_int; /* Integer sent by sigqueue(2) */ + uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */ + uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */ + uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */ + uint64_t ssi_addr; /* Address that generated signal + (for hardware-generated signals) */ + uint8_t pad[8]; /* Pad size to 128 bytes (allow for + additional fields in the future) */ }; +int +epoll_create(int size); +int +epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); +int +epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout); -int epoll_create(int size); -int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); -int epoll_wait(int epfd, struct epoll_event *events, int nevents, int timeout); - -int eventfd(u_int initval, int flags); -int signalfd(int fd, const sigset_t *mask, int flags); +int +eventfd(u_int initval, int flags); +int +signalfd(int fd, const sigset_t *mask, int flags); -#define SOCK_NONBLOCK 04000 +#define SOCK_NONBLOCK 04000 -int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); +int +accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); #endif @@ -97,54 +98,56 @@ int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); #include -#define NXT_HAVE_EVENTPORT 1 +#define NXT_HAVE_EVENTPORT 1 -#define ushort_t u_short -#define uint_t u_int +#define ushort_t u_short +#define uint_t u_int /* Solaris eventport declarations */ -#define PORT_SOURCE_AIO 1 -#define PORT_SOURCE_TIMER 2 -#define PORT_SOURCE_USER 3 -#define PORT_SOURCE_FD 4 -#define PORT_SOURCE_ALERT 5 -#define PORT_SOURCE_MQ 6 -#define PORT_SOURCE_FILE 7 +#define PORT_SOURCE_AIO 1 +#define PORT_SOURCE_TIMER 2 +#define PORT_SOURCE_USER 3 +#define PORT_SOURCE_FD 4 +#define PORT_SOURCE_ALERT 5 +#define PORT_SOURCE_MQ 6 +#define PORT_SOURCE_FILE 7 #ifndef ETIME -#define ETIME 62 +#define ETIME 62 #endif typedef struct { - int portev_events; /* event data is source specific */ - ushort_t portev_source; /* event source */ - ushort_t portev_pad; /* port internal use */ - uintptr_t portev_object; /* source specific object */ - void *portev_user; /* user cookie */ + int portev_events; /* event data is source specific */ + ushort_t portev_source; /* event source */ + ushort_t portev_pad; /* port internal use */ + uintptr_t portev_object; /* source specific object */ + void *portev_user; /* user cookie */ } port_event_t; -typedef struct timespec timespec_t; -typedef struct timespec timestruc_t; - +typedef struct timespec timespec_t; +typedef struct timespec timestruc_t; typedef struct file_obj { - timestruc_t fo_atime; /* Access time from stat(2) */ - timestruc_t fo_mtime; /* Modification time from stat(2) */ - timestruc_t fo_ctime; /* Change time from stat(2) */ - uintptr_t fo_pad[3]; /* For future expansion */ - char *fo_name; /* Null terminated file name */ + timestruc_t fo_atime; /* Access time from stat(2) */ + timestruc_t fo_mtime; /* Modification time from stat(2) */ + timestruc_t fo_ctime; /* Change time from stat(2) */ + uintptr_t fo_pad[3]; /* For future expansion */ + char *fo_name; /* Null terminated file name */ } file_obj_t; - -int port_create(void); -int port_associate(int port, int source, uintptr_t object, int events, - void *user); -int port_dissociate(int port, int source, uintptr_t object); -int port_send(int port, int events, void *user); -int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, +int +port_create(void); +int +port_associate(int port, int source, uintptr_t object, int events, void *user); +int +port_dissociate(int port, int source, uintptr_t object); +int +port_send(int port, int events, void *user); +int +port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, const timespec_t *timeout); #endif @@ -152,22 +155,21 @@ int port_getn(int port, port_event_t list[], uint_t max, uint_t *nget, #if (NXT_TEST_BUILD_DEVPOLL) -#define NXT_HAVE_DEVPOLL 1 +#define NXT_HAVE_DEVPOLL 1 #include #include /* Solaris /dev/poll declarations */ -#define POLLREMOVE 0x0800 -#define DP_POLL 0xD001 -#define DP_ISPOLLED 0xD002 - +#define POLLREMOVE 0x0800 +#define DP_POLL 0xD001 +#define DP_ISPOLLED 0xD002 struct dvpoll { - struct pollfd *dp_fds; - int dp_nfds; - int dp_timeout; + struct pollfd *dp_fds; + int dp_nfds; + int dp_timeout; }; #endif @@ -175,31 +177,35 @@ struct dvpoll { #if (NXT_TEST_BUILD_POLLSET) -#define NXT_HAVE_POLLSET 1 +#define NXT_HAVE_POLLSET 1 #include /* AIX pollset declarations */ -#define PS_ADD 0x0 -#define PS_MOD 0x1 -#define PS_DELETE 0x2 +#define PS_ADD 0x0 +#define PS_MOD 0x1 +#define PS_DELETE 0x2 -typedef int pollset_t; +typedef int pollset_t; struct poll_ctl { - short cmd; - short events; - int fd; + short cmd; + short events; + int fd; }; - -pollset_t pollset_create(int maxfd); -int pollset_destroy(pollset_t ps); -int pollset_query(pollset_t ps, struct pollfd *pollfd_query); -int pollset_ctl(pollset_t ps, struct poll_ctl *pollctl_array, int array_length); -int pollset_poll(pollset_t ps, struct pollfd *polldata_array, int array_length, +pollset_t +pollset_create(int maxfd); +int +pollset_destroy(pollset_t ps); +int +pollset_query(pollset_t ps, struct pollfd *pollfd_query); +int +pollset_ctl(pollset_t ps, struct poll_ctl *pollctl_array, int array_length); +int +pollset_poll(pollset_t ps, struct pollfd *polldata_array, int array_length, int timeout); #endif @@ -210,9 +216,9 @@ int pollset_poll(pollset_t ps, struct pollfd *polldata_array, int array_length, #if !(NXT_FREEBSD) && !(NXT_MACOSX) struct sf_hdtr { - struct iovec *headers; + struct iovec *headers; int hdr_cnt; - struct iovec *trailers; + struct iovec *trailers; int trl_cnt; }; @@ -226,16 +232,16 @@ struct sf_hdtr { /* Solaris declarations */ typedef struct sendfilevec { - int sfv_fd; - u_int sfv_flag; - off_t sfv_off; - size_t sfv_len; + int sfv_fd; + u_int sfv_flag; + off_t sfv_off; + size_t sfv_len; } sendfilevec_t; -#define SFV_FD_SELF -2 +#define SFV_FD_SELF -2 -ssize_t sendfilev(int fd, const struct sendfilevec *vec, int sfvcnt, - size_t *xferred); +ssize_t +sendfilev(int fd, const struct sendfilevec *vec, int sfvcnt, size_t *xferred); #endif @@ -243,30 +249,31 @@ ssize_t sendfilev(int fd, const struct sendfilevec *vec, int sfvcnt, #if (NXT_TEST_BUILD_AIX_SEND_FILE) #ifndef uint_t -#define uint_t u_int +#define uint_t u_int #endif struct sf_parms { - void *header_data; - uint_t header_length; + void *header_data; + uint_t header_length; - int file_descriptor; - uint64_t file_size; - uint64_t file_offset; - int64_t file_bytes; + int file_descriptor; + uint64_t file_size; + uint64_t file_offset; + int64_t file_bytes; - void *trailer_data; - uint_t trailer_length; + void *trailer_data; + uint_t trailer_length; - uint64_t bytes_sent; + uint64_t bytes_sent; }; -#define SF_CLOSE 0x00000001 /* close the socket after completion */ -#define SF_REUSE 0x00000002 /* reuse socket. not supported */ -#define SF_DONT_CACHE 0x00000004 /* don't apply network buffer cache */ -#define SF_SYNC_CACHE 0x00000008 /* sync/update network buffer cache */ +#define SF_CLOSE 0x00000001 /* close the socket after completion */ +#define SF_REUSE 0x00000002 /* reuse socket. not supported */ +#define SF_DONT_CACHE 0x00000004 /* don't apply network buffer cache */ +#define SF_SYNC_CACHE 0x00000008 /* sync/update network buffer cache */ -ssize_t send_file(int *s, struct sf_parms *sf_iobuf, uint_t flags); +ssize_t +send_file(int *s, struct sf_parms *sf_iobuf, uint_t flags); #endif diff --git a/src/nxt_thread.c b/src/nxt_thread.c index f38047198..db27ddb1d 100644 --- a/src/nxt_thread.c +++ b/src/nxt_thread.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,21 +6,22 @@ #include -static void *nxt_thread_trampoline(void *data); -static void nxt_thread_time_cleanup(void *data); +static void * +nxt_thread_trampoline(void *data); +static void +nxt_thread_time_cleanup(void *data); #if (NXT_HAVE_PTHREAD_SPECIFIC_DATA) -static void nxt_thread_key_dtor(void *data); - +static void +nxt_thread_key_dtor(void *data); void -nxt_thread_init_data(nxt_thread_specific_data_t tsd) -{ - void *p; - nxt_err_t err; - pthread_key_t key; +nxt_thread_init_data(nxt_thread_specific_data_t tsd) { + void *p; + nxt_err_t err; + pthread_key_t key; while ((nxt_atomic_int_t) tsd->key < 0) { /* @@ -30,7 +30,6 @@ nxt_thread_init_data(nxt_thread_specific_data_t tsd) * -2 is the initializing lock to assure the single value for the key. */ if (nxt_atomic_cmp_set(&tsd->key, -1, -2)) { - err = pthread_key_create(&key, nxt_thread_key_dtor); if (err != 0) { nxt_main_log_alert("pthread_key_create() failed %E", err); @@ -65,10 +64,8 @@ nxt_thread_init_data(nxt_thread_specific_data_t tsd) nxt_unreachable(); } - static void -nxt_thread_key_dtor(void *data) -{ +nxt_thread_key_dtor(void *data) { nxt_main_log_debug("pthread key dtor: %p", data); nxt_free(data); @@ -78,9 +75,8 @@ nxt_thread_key_dtor(void *data) nxt_int_t -nxt_thread_create(nxt_thread_handle_t *handle, nxt_thread_link_t *link) -{ - nxt_err_t err; +nxt_thread_create(nxt_thread_handle_t *handle, nxt_thread_link_t *link) { + nxt_err_t err; err = pthread_create(handle, NULL, nxt_thread_trampoline, link); @@ -97,13 +93,11 @@ nxt_thread_create(nxt_thread_handle_t *handle, nxt_thread_link_t *link) return NXT_ERROR; } - static void * -nxt_thread_trampoline(void *data) -{ - nxt_thread_t *thr; - nxt_thread_link_t *link; - nxt_thread_start_t start; +nxt_thread_trampoline(void *data) { + nxt_thread_t *thr; + nxt_thread_link_t *link; + nxt_thread_start_t start; link = data; @@ -114,7 +108,7 @@ nxt_thread_trampoline(void *data) pthread_cleanup_push(nxt_thread_time_cleanup, thr); start = link->start; - data = link->work.data; + data = link->work.data; if (link->work.handler != NULL) { thr->link = link; @@ -137,18 +131,16 @@ nxt_thread_trampoline(void *data) return NULL; } - nxt_thread_t * -nxt_thread_init(void) -{ - nxt_thread_t *thr; +nxt_thread_init(void) { + nxt_thread_t *thr; nxt_thread_init_data(nxt_thread_context); thr = nxt_thread(); if (thr->log == NULL) { - thr->log = &nxt_main_log; + thr->log = &nxt_main_log; thr->handle = nxt_thread_handle(); /* @@ -165,11 +157,9 @@ nxt_thread_init(void) return thr; } - static void -nxt_thread_time_cleanup(void *data) -{ - nxt_thread_t *thr; +nxt_thread_time_cleanup(void *data) { + nxt_thread_t *thr; thr = data; @@ -178,16 +168,14 @@ nxt_thread_time_cleanup(void *data) nxt_thread_time_free(thr); } - void -nxt_thread_exit(nxt_thread_t *thr) -{ - nxt_thread_link_t *link; - nxt_event_engine_t *engine; +nxt_thread_exit(nxt_thread_t *thr) { + nxt_thread_link_t *link; + nxt_event_engine_t *engine; nxt_log_debug(thr->log, "thread exit"); - link = thr->link; + link = thr->link; thr->link = NULL; if (link != NULL) { @@ -208,11 +196,9 @@ nxt_thread_exit(nxt_thread_t *thr) nxt_unreachable(); } - void -nxt_thread_cancel(nxt_thread_handle_t handle) -{ - nxt_err_t err; +nxt_thread_cancel(nxt_thread_handle_t handle) { + nxt_err_t err; nxt_thread_log_debug("thread cancel: %PH", handle); @@ -223,11 +209,9 @@ nxt_thread_cancel(nxt_thread_handle_t handle) } } - void -nxt_thread_wait(nxt_thread_handle_t handle) -{ - nxt_err_t err; +nxt_thread_wait(nxt_thread_handle_t handle) { + nxt_err_t err; nxt_thread_log_debug("thread wait: %PH", handle); @@ -238,10 +222,8 @@ nxt_thread_wait(nxt_thread_handle_t handle) } } - nxt_tid_t -nxt_thread_tid(nxt_thread_t *thr) -{ +nxt_thread_tid(nxt_thread_t *thr) { #if (NXT_HAVE_THREAD_STORAGE_CLASS) if (nxt_slow_path(thr->tid == 0)) { @@ -253,7 +235,6 @@ nxt_thread_tid(nxt_thread_t *thr) #else if (nxt_fast_path(thr != NULL)) { - if (nxt_slow_path(thr->tid == 0)) { thr->tid = nxt_thread_get_tid(); } diff --git a/src/nxt_thread.h b/src/nxt_thread.h index 53b2d8c0f..7dc83e9e1 100644 --- a/src/nxt_thread.h +++ b/src/nxt_thread.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -30,16 +29,13 @@ #if (NXT_HAVE_THREAD_STORAGE_CLASS) -#define nxt_thread_extern_data(type, tsd) \ - NXT_EXPORT extern __thread type tsd +#define nxt_thread_extern_data(type, tsd) NXT_EXPORT extern __thread type tsd -#define nxt_thread_declare_data(type, tsd) \ - __thread type tsd +#define nxt_thread_declare_data(type, tsd) __thread type tsd #define nxt_thread_init_data(tsd) -#define nxt_thread_get_data(tsd) \ - &tsd +#define nxt_thread_get_data(tsd) &tsd #else /* NXT_HAVE_PTHREAD_SPECIFIC_DATA */ @@ -58,21 +54,20 @@ */ typedef struct { - nxt_atomic_t key; - size_t size; + nxt_atomic_t key; + size_t size; } nxt_thread_specific_data_t[1]; +#define nxt_thread_extern_data(type, tsd) \ + NXT_EXPORT extern nxt_thread_specific_data_t tsd -#define nxt_thread_extern_data(type, tsd) \ - NXT_EXPORT extern nxt_thread_specific_data_t tsd - -#define nxt_thread_declare_data(type, tsd) \ - nxt_thread_specific_data_t tsd = { { (nxt_atomic_int_t) -1, sizeof(type) } } +#define nxt_thread_declare_data(type, tsd) \ + nxt_thread_specific_data_t tsd = {{(nxt_atomic_int_t) - 1, sizeof(type)}} -NXT_EXPORT void nxt_thread_init_data(nxt_thread_specific_data_t tsd); +NXT_EXPORT void +nxt_thread_init_data(nxt_thread_specific_data_t tsd); -#define nxt_thread_get_data(tsd) \ - pthread_getspecific((pthread_key_t) tsd->key) +#define nxt_thread_get_data(tsd) pthread_getspecific((pthread_key_t) tsd->key) #endif @@ -80,73 +75,81 @@ NXT_EXPORT void nxt_thread_init_data(nxt_thread_specific_data_t tsd); typedef void (*nxt_thread_start_t)(void *data); typedef struct { - nxt_thread_start_t start; - nxt_event_engine_t *engine; - nxt_work_t work; + nxt_thread_start_t start; + nxt_event_engine_t *engine; + nxt_work_t work; } nxt_thread_link_t; - -NXT_EXPORT nxt_int_t nxt_thread_create(nxt_thread_handle_t *handle, - nxt_thread_link_t *link); -NXT_EXPORT nxt_thread_t *nxt_thread_init(void); -NXT_EXPORT void nxt_thread_exit(nxt_thread_t *thr); -NXT_EXPORT void nxt_thread_cancel(nxt_thread_handle_t handle); -NXT_EXPORT void nxt_thread_wait(nxt_thread_handle_t handle); +NXT_EXPORT nxt_int_t +nxt_thread_create(nxt_thread_handle_t *handle, nxt_thread_link_t *link); +NXT_EXPORT nxt_thread_t * +nxt_thread_init(void); +NXT_EXPORT void +nxt_thread_exit(nxt_thread_t *thr); +NXT_EXPORT void +nxt_thread_cancel(nxt_thread_handle_t handle); +NXT_EXPORT void +nxt_thread_wait(nxt_thread_handle_t handle); -#define nxt_thread_handle() \ - pthread_self() +#define nxt_thread_handle() pthread_self() -typedef pthread_mutex_t nxt_thread_mutex_t; +typedef pthread_mutex_t nxt_thread_mutex_t; -NXT_EXPORT nxt_int_t nxt_thread_mutex_create(nxt_thread_mutex_t *mtx); -NXT_EXPORT void nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx); -NXT_EXPORT nxt_int_t nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx); -NXT_EXPORT nxt_bool_t nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx); -NXT_EXPORT nxt_int_t nxt_thread_mutex_unlock(nxt_thread_mutex_t *mtx); +NXT_EXPORT nxt_int_t +nxt_thread_mutex_create(nxt_thread_mutex_t *mtx); +NXT_EXPORT void +nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx); +NXT_EXPORT nxt_int_t +nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx); +NXT_EXPORT nxt_bool_t +nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx); +NXT_EXPORT nxt_int_t +nxt_thread_mutex_unlock(nxt_thread_mutex_t *mtx); -typedef pthread_cond_t nxt_thread_cond_t; +typedef pthread_cond_t nxt_thread_cond_t; -NXT_EXPORT nxt_int_t nxt_thread_cond_create(nxt_thread_cond_t *cond); -NXT_EXPORT void nxt_thread_cond_destroy(nxt_thread_cond_t *cond); -NXT_EXPORT nxt_int_t nxt_thread_cond_signal(nxt_thread_cond_t *cond); -NXT_EXPORT nxt_err_t nxt_thread_cond_wait(nxt_thread_cond_t *cond, - nxt_thread_mutex_t *mtx, nxt_nsec_t timeout); +NXT_EXPORT nxt_int_t +nxt_thread_cond_create(nxt_thread_cond_t *cond); +NXT_EXPORT void +nxt_thread_cond_destroy(nxt_thread_cond_t *cond); +NXT_EXPORT nxt_int_t +nxt_thread_cond_signal(nxt_thread_cond_t *cond); +NXT_EXPORT nxt_err_t +nxt_thread_cond_wait(nxt_thread_cond_t *cond, nxt_thread_mutex_t *mtx, + nxt_nsec_t timeout); #if (NXT_HAVE_PTHREAD_YIELD) -#define nxt_thread_yield() \ - pthread_yield() +#define nxt_thread_yield() pthread_yield() #elif (NXT_HAVE_PTHREAD_YIELD_NP) -#define nxt_thread_yield() \ - pthread_yield_np() +#define nxt_thread_yield() pthread_yield_np() #else -#define nxt_thread_yield() \ - nxt_sched_yield() +#define nxt_thread_yield() nxt_sched_yield() #endif struct nxt_thread_s { - nxt_log_t *log; - nxt_log_t main_log; + nxt_log_t *log; + nxt_log_t main_log; - nxt_task_t *task; + nxt_task_t *task; - nxt_tid_t tid; - nxt_thread_handle_t handle; - nxt_thread_link_t *link; - nxt_thread_pool_t *thread_pool; + nxt_tid_t tid; + nxt_thread_handle_t handle; + nxt_thread_link_t *link; + nxt_thread_pool_t *thread_pool; - nxt_thread_time_t time; + nxt_thread_time_t time; - nxt_runtime_t *runtime; - nxt_event_engine_t *engine; - void *data; + nxt_runtime_t *runtime; + nxt_event_engine_t *engine; + void *data; #if 0 /* @@ -156,7 +159,7 @@ struct nxt_thread_s { nxt_fiber_t *fiber; #endif - nxt_random_t random; + nxt_random_t random; }; diff --git a/src/nxt_thread_cond.c b/src/nxt_thread_cond.c index fcd108e4a..8adb6ccb2 100644 --- a/src/nxt_thread_cond.c +++ b/src/nxt_thread_cond.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,11 +5,9 @@ #include - nxt_int_t -nxt_thread_cond_create(nxt_thread_cond_t *cond) -{ - nxt_err_t err; +nxt_thread_cond_create(nxt_thread_cond_t *cond) { + nxt_err_t err; err = pthread_cond_init(cond, NULL); if (err == 0) { @@ -22,11 +19,9 @@ nxt_thread_cond_create(nxt_thread_cond_t *cond) return NXT_ERROR; } - void -nxt_thread_cond_destroy(nxt_thread_cond_t *cond) -{ - nxt_err_t err; +nxt_thread_cond_destroy(nxt_thread_cond_t *cond) { + nxt_err_t err; err = pthread_cond_destroy(cond); if (err != 0) { @@ -36,11 +31,9 @@ nxt_thread_cond_destroy(nxt_thread_cond_t *cond) nxt_thread_log_debug("pthread_cond_destroy(%p)", cond); } - nxt_int_t -nxt_thread_cond_signal(nxt_thread_cond_t *cond) -{ - nxt_err_t err; +nxt_thread_cond_signal(nxt_thread_cond_t *cond) { + nxt_err_t err; err = pthread_cond_signal(cond); if (nxt_fast_path(err == 0)) { @@ -53,16 +46,14 @@ nxt_thread_cond_signal(nxt_thread_cond_t *cond) return NXT_ERROR; } - nxt_err_t nxt_thread_cond_wait(nxt_thread_cond_t *cond, nxt_thread_mutex_t *mtx, - nxt_nsec_t timeout) -{ - nxt_err_t err; - nxt_nsec_t ns; - nxt_thread_t *thr; - nxt_realtime_t *now; - struct timespec ts; + nxt_nsec_t timeout) { + nxt_err_t err; + nxt_nsec_t ns; + nxt_thread_t *thr; + nxt_realtime_t *now; + struct timespec ts; thr = nxt_thread(); @@ -81,13 +72,13 @@ nxt_thread_cond_wait(nxt_thread_cond_t *cond, nxt_thread_mutex_t *mtx, nxt_log_alert(thr->log, "pthread_cond_wait() failed %E", err); } else { - nxt_log_debug(thr->log, "pthread_cond_timedwait(%p, %N) enter", - cond, timeout); + nxt_log_debug(thr->log, "pthread_cond_timedwait(%p, %N) enter", cond, + timeout); now = nxt_thread_realtime(thr); - ns = now->nsec + timeout; - ts.tv_sec = now->sec + ns / 1000000000; + ns = now->nsec + timeout; + ts.tv_sec = now->sec + ns / 1000000000; ts.tv_nsec = ns % 1000000000; err = pthread_cond_timedwait(cond, mtx, &ts); @@ -95,8 +86,8 @@ nxt_thread_cond_wait(nxt_thread_cond_t *cond, nxt_thread_mutex_t *mtx, nxt_thread_time_update(thr); if (nxt_fast_path(err == 0 || err == NXT_ETIMEDOUT)) { - nxt_log_debug(thr->log, "pthread_cond_timedwait(%p) exit: %d", - cond, err); + nxt_log_debug(thr->log, "pthread_cond_timedwait(%p) exit: %d", cond, + err); return err; } diff --git a/src/nxt_thread_id.h b/src/nxt_thread_id.h index 764c99344..cf9a0288e 100644 --- a/src/nxt_thread_id.h +++ b/src/nxt_thread_id.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -15,11 +14,10 @@ * glibc does not provide a wrapper for gettid(). */ -typedef pid_t nxt_tid_t; +typedef pid_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return syscall(SYS_gettid); } @@ -32,11 +30,10 @@ nxt_thread_get_tid(void) * Thread id is a number above 100,000. */ -typedef uint32_t nxt_tid_t; +typedef uint32_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return (uint32_t) (*(long *) pthread_self()); } @@ -44,11 +41,10 @@ nxt_thread_get_tid(void) /* Solaris pthread_t are numbers starting with 1. */ -typedef pthread_t nxt_tid_t; +typedef pthread_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return pthread_self(); } @@ -62,12 +58,11 @@ nxt_thread_get_tid(void) * syscall. It is a number above 300,000. */ -typedef uint64_t nxt_tid_t; +typedef uint64_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - uint64_t tid; +nxt_thread_get_tid(void) { + uint64_t tid; (void) pthread_threadid_np(NULL, &tid); return tid; @@ -82,13 +77,12 @@ nxt_thread_get_tid(void) #elif (NXT_OPENBSD) -typedef pid_t nxt_tid_t; +typedef pid_t nxt_tid_t; /* OpenBSD 3.9 getthrid(). */ nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return getthrid(); } @@ -102,27 +96,26 @@ nxt_thread_get_tid(void) * shown in "ps -ef -m -o THREAD" output. */ -typedef tid_t nxt_tid_t; +typedef tid_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ - int err, size; - pthread_t pt; - struct __pthrdsinfo ti; +nxt_thread_get_tid(void) { + int err, size; + pthread_t pt; + struct __pthrdsinfo ti; size = 0; - pt = pthread_self(); + pt = pthread_self(); err = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &ti, - sizeof(struct __pthrdsinfo), NULL, size); + sizeof(struct __pthrdsinfo), NULL, size); if (nxt_fast_path(err == 0)) { return ti.__pi_tid; } nxt_main_log_alert("pthread_getthrds_np(PTHRDSINFO_QUERY_TID) failed %E", - err); + err); return 0; } @@ -144,28 +137,27 @@ nxt_thread_get_tid(void) /* HP-UX pthread_t are numbers starting with 1. */ -typedef pthread_t nxt_tid_t; +typedef pthread_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return pthread_self(); } #else -typedef pthread_t nxt_tid_t; +typedef pthread_t nxt_tid_t; nxt_inline nxt_tid_t -nxt_thread_get_tid(void) -{ +nxt_thread_get_tid(void) { return pthread_self(); } #endif -NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr); +NXT_EXPORT nxt_tid_t +nxt_thread_tid(nxt_thread_t *thr); /* @@ -176,14 +168,12 @@ NXT_EXPORT nxt_tid_t nxt_thread_tid(nxt_thread_t *thr); * On Cygwin pthread_t is pointer to void. * On z/OS pthread_t is "struct { char __[0x08]; }". */ -typedef pthread_t nxt_thread_handle_t; +typedef pthread_t nxt_thread_handle_t; -#define nxt_thread_handle_clear(th) \ - th = (pthread_t) 0 +#define nxt_thread_handle_clear(th) th = (pthread_t) 0 -#define nxt_thread_handle_equal(th0, th1) \ - pthread_equal(th0, th1) +#define nxt_thread_handle_equal(th0, th1) pthread_equal(th0, th1) #endif /* _NXT_UNIX_THREAD_ID_H_INCLUDED_ */ diff --git a/src/nxt_thread_log.h b/src/nxt_thread_log.h index 214098ffa..2fa3f07b3 100644 --- a/src/nxt_thread_log.h +++ b/src/nxt_thread_log.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,37 +7,36 @@ #define _NXT_THREAD_LOG_H_INCLUDED_ -#define nxt_thread_log_alert(...) \ - do { \ - nxt_thread_t *_thr = nxt_thread(); \ - \ - nxt_log_alert(_thr->log, __VA_ARGS__); \ - \ +#define nxt_thread_log_alert(...) \ + do { \ + nxt_thread_t *_thr = nxt_thread(); \ + \ + nxt_log_alert(_thr->log, __VA_ARGS__); \ + \ } while (0) -#define nxt_thread_log_error(_level, ...) \ - do { \ - nxt_thread_t *_thr = nxt_thread(); \ - \ - nxt_log_error(_level, _thr->log, __VA_ARGS__); \ - \ +#define nxt_thread_log_error(_level, ...) \ + do { \ + nxt_thread_t *_thr = nxt_thread(); \ + \ + nxt_log_error(_level, _thr->log, __VA_ARGS__); \ + \ } while (0) #if (NXT_DEBUG) -#define nxt_thread_log_debug(...) \ - do { \ - nxt_thread_t *_thr = nxt_thread(); \ - \ - nxt_log_debug(_thr->log, __VA_ARGS__); \ - \ +#define nxt_thread_log_debug(...) \ + do { \ + nxt_thread_t *_thr = nxt_thread(); \ + \ + nxt_log_debug(_thr->log, __VA_ARGS__); \ + \ } while (0) -#define nxt_thread_debug(thr) \ - nxt_thread_t *thr = nxt_thread() +#define nxt_thread_debug(thr) nxt_thread_t *thr = nxt_thread() #else @@ -49,9 +47,8 @@ nxt_inline nxt_log_t * -nxt_thread_log(void) -{ - nxt_thread_t *thr; +nxt_thread_log(void) { + nxt_thread_t *thr; thr = nxt_thread(); return thr->log; diff --git a/src/nxt_thread_mutex.c b/src/nxt_thread_mutex.c index 472a69f05..f429ea53c 100644 --- a/src/nxt_thread_mutex.c +++ b/src/nxt_thread_mutex.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,7 +5,6 @@ #include - /* * All modern pthread mutex implementations try to acquire a lock atomically * in userland before going to sleep in kernel. Some spins on SMP systems @@ -78,10 +76,9 @@ nxt_int_t -nxt_thread_mutex_create(nxt_thread_mutex_t *mtx) -{ - nxt_err_t err; - pthread_mutexattr_t attr; +nxt_thread_mutex_create(nxt_thread_mutex_t *mtx) { + nxt_err_t err; + pthread_mutexattr_t attr; err = pthread_mutexattr_init(&attr); if (err != 0) { @@ -92,7 +89,8 @@ nxt_thread_mutex_create(nxt_thread_mutex_t *mtx) err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); if (err != 0) { nxt_thread_log_alert("pthread_mutexattr_settype" - "(PTHREAD_MUTEX_ERRORCHECK) failed %E", err); + "(PTHREAD_MUTEX_ERRORCHECK) failed %E", + err); return NXT_ERROR; } @@ -112,11 +110,9 @@ nxt_thread_mutex_create(nxt_thread_mutex_t *mtx) return NXT_OK; } - void -nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx) -{ - nxt_err_t err; +nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx) { + nxt_err_t err; err = pthread_mutex_destroy(mtx); if (nxt_slow_path(err != 0)) { @@ -126,11 +122,9 @@ nxt_thread_mutex_destroy(nxt_thread_mutex_t *mtx) nxt_thread_log_debug("pthread_mutex_destroy(%p)", mtx); } - nxt_int_t -nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx) -{ - nxt_err_t err; +nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx) { + nxt_err_t err; nxt_thread_log_debug("pthread_mutex_lock(%p) enter", mtx); @@ -144,11 +138,9 @@ nxt_thread_mutex_lock(nxt_thread_mutex_t *mtx) return NXT_ERROR; } - nxt_bool_t -nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx) -{ - nxt_err_t err; +nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx) { + nxt_err_t err; nxt_thread_debug(thr); @@ -169,12 +161,10 @@ nxt_thread_mutex_trylock(nxt_thread_mutex_t *mtx) return 0; } - nxt_int_t -nxt_thread_mutex_unlock(nxt_thread_mutex_t *mtx) -{ +nxt_thread_mutex_unlock(nxt_thread_mutex_t *mtx) { nxt_err_t err; - nxt_thread_t *thr; + nxt_thread_t *thr; err = pthread_mutex_unlock(mtx); diff --git a/src/nxt_thread_pool.c b/src/nxt_thread_pool.c index 0be2ddc06..412ce7cde 100644 --- a/src/nxt_thread_pool.c +++ b/src/nxt_thread_pool.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,19 +6,22 @@ #include -static nxt_int_t nxt_thread_pool_init(nxt_thread_pool_t *tp); -static void nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data); -static void nxt_thread_pool_start(void *ctx); -static void nxt_thread_pool_loop(void *ctx); -static void nxt_thread_pool_wait(nxt_thread_pool_t *tp); - +static nxt_int_t +nxt_thread_pool_init(nxt_thread_pool_t *tp); +static void +nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data); +static void +nxt_thread_pool_start(void *ctx); +static void +nxt_thread_pool_loop(void *ctx); +static void +nxt_thread_pool_wait(nxt_thread_pool_t *tp); nxt_thread_pool_t * nxt_thread_pool_create(nxt_uint_t max_threads, nxt_nsec_t timeout, nxt_thread_pool_init_t init, nxt_event_engine_t *engine, - nxt_work_handler_t exit) -{ - nxt_thread_pool_t *tp; + nxt_work_handler_t exit) { + nxt_thread_pool_t *tp; tp = nxt_zalloc(sizeof(nxt_thread_pool_t)); if (tp == NULL) { @@ -27,20 +29,18 @@ nxt_thread_pool_create(nxt_uint_t max_threads, nxt_nsec_t timeout, } tp->max_threads = max_threads; - tp->timeout = timeout; - tp->engine = engine; + tp->timeout = timeout; + tp->engine = engine; tp->task.thread = engine->task.thread; - tp->task.log = engine->task.log; - tp->init = init; - tp->exit = exit; + tp->task.log = engine->task.log; + tp->init = init; + tp->exit = exit; return tp; } - nxt_int_t -nxt_thread_pool_post(nxt_thread_pool_t *tp, nxt_work_t *work) -{ +nxt_thread_pool_post(nxt_thread_pool_t *tp, nxt_work_t *work) { nxt_thread_log_debug("thread pool post"); if (nxt_slow_path(nxt_thread_pool_init(tp) != NXT_OK)) { @@ -54,13 +54,11 @@ nxt_thread_pool_post(nxt_thread_pool_t *tp, nxt_work_t *work) return NXT_OK; } - static nxt_int_t -nxt_thread_pool_init(nxt_thread_pool_t *tp) -{ - nxt_int_t ret; - nxt_thread_link_t *link; - nxt_thread_handle_t handle; +nxt_thread_pool_init(nxt_thread_pool_t *tp) { + nxt_int_t ret; + nxt_thread_link_t *link; + nxt_thread_handle_t handle; if (nxt_fast_path(tp->ready)) { return NXT_OK; @@ -76,17 +74,15 @@ nxt_thread_pool_init(nxt_thread_pool_t *tp) ret = NXT_OK; if (!tp->ready) { - nxt_thread_log_debug("thread pool init"); (void) nxt_atomic_fetch_add(&tp->threads, 1); if (nxt_fast_path(nxt_sem_init(&tp->sem, 0) == NXT_OK)) { - link = nxt_zalloc(sizeof(nxt_thread_link_t)); if (nxt_fast_path(link != NULL)) { - link->start = nxt_thread_pool_start; + link->start = nxt_thread_pool_start; link->work.data = tp; if (nxt_thread_create(&handle, link) == NXT_OK) { @@ -110,44 +106,40 @@ nxt_thread_pool_init(nxt_thread_pool_t *tp) return ret; } - static void -nxt_thread_pool_start(void *ctx) -{ - nxt_thread_t *thr; - nxt_thread_pool_t *tp; +nxt_thread_pool_start(void *ctx) { + nxt_thread_t *thr; + nxt_thread_pool_t *tp; - tp = ctx; + tp = ctx; thr = nxt_thread(); - tp->main = thr->handle; + tp->main = thr->handle; tp->task.thread = thr; nxt_thread_pool_loop(ctx); } - static void -nxt_thread_pool_loop(void *ctx) -{ - void *obj, *data; - nxt_task_t *task; - nxt_thread_t *thr; - nxt_thread_pool_t *tp; - nxt_work_handler_t handler; - - tp = ctx; +nxt_thread_pool_loop(void *ctx) { + void *obj, *data; + nxt_task_t *task; + nxt_thread_t *thr; + nxt_thread_pool_t *tp; + nxt_work_handler_t handler; + + tp = ctx; thr = nxt_thread(); if (tp->init != NULL) { tp->init(); } - for ( ;; ) { + for (;;) { nxt_thread_pool_wait(tp); - handler = nxt_locked_work_queue_pop(&tp->work_queue, &task, &obj, - &data); + handler + = nxt_locked_work_queue_pop(&tp->work_queue, &task, &obj, &data); if (nxt_fast_path(handler != NULL)) { task->thread = thr; @@ -161,15 +153,13 @@ nxt_thread_pool_loop(void *ctx) } } - static void -nxt_thread_pool_wait(nxt_thread_pool_t *tp) -{ - nxt_err_t err; - nxt_thread_t *thr; - nxt_atomic_uint_t waiting, threads; - nxt_thread_link_t *link; - nxt_thread_handle_t handle; +nxt_thread_pool_wait(nxt_thread_pool_t *tp) { + nxt_err_t err; + nxt_thread_t *thr; + nxt_atomic_uint_t waiting, threads; + nxt_thread_link_t *link; + nxt_thread_handle_t handle; thr = nxt_thread(); @@ -177,7 +167,7 @@ nxt_thread_pool_wait(nxt_thread_pool_t *tp) (void) nxt_atomic_fetch_add(&tp->waiting, 1); - for ( ;; ) { + for (;;) { err = nxt_sem_wait(&tp->sem, tp->timeout); if (err == 0) { @@ -216,7 +206,7 @@ nxt_thread_pool_wait(nxt_thread_pool_t *tp) link = nxt_zalloc(sizeof(nxt_thread_link_t)); if (nxt_fast_path(link != NULL)) { - link->start = nxt_thread_pool_loop; + link->start = nxt_thread_pool_loop; link->work.data = tp; if (nxt_thread_create(&handle, link) != NXT_OK) { @@ -225,11 +215,9 @@ nxt_thread_pool_wait(nxt_thread_pool_t *tp) } } - void -nxt_thread_pool_destroy(nxt_thread_pool_t *tp) -{ - nxt_thread_t *thr; +nxt_thread_pool_destroy(nxt_thread_pool_t *tp) { + nxt_thread_t *thr; thr = nxt_thread(); @@ -237,7 +225,7 @@ nxt_thread_pool_destroy(nxt_thread_pool_t *tp) if (!tp->ready) { nxt_work_queue_add(&thr->engine->fast_work_queue, tp->exit, - &tp->engine->task, tp, NULL); + &tp->engine->task, tp, NULL); return; } @@ -251,7 +239,6 @@ nxt_thread_pool_destroy(nxt_thread_pool_t *tp) } } - /* * A thread handle (pthread_t) is either pointer or integer, so it can be * passed as work handler pointer "data" argument. To convert void pointer @@ -264,14 +251,13 @@ nxt_thread_pool_destroy(nxt_thread_pool_t *tp) */ static void -nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data) -{ - nxt_thread_t *thread; - nxt_thread_pool_t *tp; - nxt_atomic_uint_t threads; - nxt_thread_handle_t handle; - - tp = obj; +nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data) { + nxt_thread_t *thread; + nxt_thread_pool_t *tp; + nxt_atomic_uint_t threads; + nxt_thread_handle_t handle; + + tp = obj; thread = task->thread; nxt_debug(task, "thread pool exit"); @@ -287,7 +273,7 @@ nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data) if (threads > 1) { nxt_work_set(&tp->work, nxt_thread_pool_exit, &tp->task, tp, - (void *) (uintptr_t) thread->handle); + (void *) (uintptr_t) thread->handle); nxt_thread_pool_post(tp, &tp->work); @@ -297,7 +283,7 @@ nxt_thread_pool_exit(nxt_task_t *task, void *obj, void *data) nxt_sem_destroy(&tp->sem); nxt_work_set(&tp->work, tp->exit, &tp->engine->task, tp, - (void *) (uintptr_t) thread->handle); + (void *) (uintptr_t) thread->handle); nxt_event_engine_post(tp->engine, &tp->work); diff --git a/src/nxt_thread_pool.h b/src/nxt_thread_pool.h index 750b98f82..a466ef7f9 100644 --- a/src/nxt_thread_pool.h +++ b/src/nxt_thread_pool.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -10,35 +9,35 @@ typedef void (*nxt_thread_pool_init_t)(void); - struct nxt_thread_pool_s { - nxt_atomic_t ready; - nxt_atomic_t waiting; - nxt_atomic_t threads; - nxt_uint_t max_threads; + nxt_atomic_t ready; + nxt_atomic_t waiting; + nxt_atomic_t threads; + nxt_uint_t max_threads; - nxt_sem_t sem; - nxt_nsec_t timeout; + nxt_sem_t sem; + nxt_nsec_t timeout; - nxt_work_t work; - nxt_task_t task; + nxt_work_t work; + nxt_task_t task; - nxt_locked_work_queue_t work_queue; + nxt_locked_work_queue_t work_queue; - nxt_thread_handle_t main; + nxt_thread_handle_t main; - nxt_event_engine_t *engine; - nxt_thread_pool_init_t init; - nxt_work_handler_t exit; + nxt_event_engine_t *engine; + nxt_thread_pool_init_t init; + nxt_work_handler_t exit; }; - -NXT_EXPORT nxt_thread_pool_t *nxt_thread_pool_create(nxt_uint_t max_threads, - nxt_nsec_t timeout, nxt_thread_pool_init_t init, - nxt_event_engine_t *engine, nxt_work_handler_t exit); -NXT_EXPORT void nxt_thread_pool_destroy(nxt_thread_pool_t *tp); -NXT_EXPORT nxt_int_t nxt_thread_pool_post(nxt_thread_pool_t *tp, - nxt_work_t *work); +NXT_EXPORT nxt_thread_pool_t * +nxt_thread_pool_create(nxt_uint_t max_threads, nxt_nsec_t timeout, + nxt_thread_pool_init_t init, nxt_event_engine_t *engine, + nxt_work_handler_t exit); +NXT_EXPORT void +nxt_thread_pool_destroy(nxt_thread_pool_t *tp); +NXT_EXPORT nxt_int_t +nxt_thread_pool_post(nxt_thread_pool_t *tp, nxt_work_t *work); #endif /* _NXT_UNIX_THREAD_POOL_H_INCLUDED_ */ diff --git a/src/nxt_thread_time.c b/src/nxt_thread_time.c index d084af0af..be0317760 100644 --- a/src/nxt_thread_time.c +++ b/src/nxt_thread_time.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -30,25 +29,27 @@ */ -static void nxt_time_thread(void *data); -static void nxt_thread_time_shared(nxt_monotonic_time_t *now); -static void nxt_thread_realtime_update(nxt_thread_t *thr, - nxt_monotonic_time_t *now); -static u_char *nxt_thread_time_string_no_cache(nxt_thread_t *thr, - nxt_time_string_t *ts, u_char *buf); -static nxt_atomic_uint_t nxt_thread_time_string_slot(nxt_time_string_t *ts); -static nxt_time_string_cache_t *nxt_thread_time_string_cache(nxt_thread_t *thr, - nxt_atomic_uint_t slot); - +static void +nxt_time_thread(void *data); +static void +nxt_thread_time_shared(nxt_monotonic_time_t *now); +static void +nxt_thread_realtime_update(nxt_thread_t *thr, nxt_monotonic_time_t *now); +static u_char * +nxt_thread_time_string_no_cache(nxt_thread_t *thr, nxt_time_string_t *ts, + u_char *buf); +static nxt_atomic_uint_t +nxt_thread_time_string_slot(nxt_time_string_t *ts); +static nxt_time_string_cache_t * +nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot); -static nxt_atomic_int_t nxt_gmtoff; -static nxt_bool_t nxt_use_shared_time = 0; -static volatile nxt_monotonic_time_t nxt_shared_time; +static nxt_atomic_int_t nxt_gmtoff; +static nxt_bool_t nxt_use_shared_time = 0; +static volatile nxt_monotonic_time_t nxt_shared_time; void -nxt_thread_time_update(nxt_thread_t *thr) -{ +nxt_thread_time_update(nxt_thread_t *thr) { if (nxt_use_shared_time) { nxt_thread_time_shared(&thr->time.now); @@ -57,12 +58,10 @@ nxt_thread_time_update(nxt_thread_t *thr) } } - void -nxt_thread_time_free(nxt_thread_t *thr) -{ +nxt_thread_time_free(nxt_thread_t *thr) { nxt_uint_t i; - nxt_time_string_cache_t *tsc; + nxt_time_string_cache_t *tsc; tsc = thr->time.strings; @@ -78,35 +77,31 @@ nxt_thread_time_free(nxt_thread_t *thr) } } - void -nxt_time_thread_start(nxt_msec_t interval) -{ - nxt_thread_link_t *link; - nxt_thread_handle_t handle; +nxt_time_thread_start(nxt_msec_t interval) { + nxt_thread_link_t *link; + nxt_thread_handle_t handle; link = nxt_zalloc(sizeof(nxt_thread_link_t)); if (nxt_fast_path(link != NULL)) { - link->start = nxt_time_thread; + link->start = nxt_time_thread; link->work.data = (void *) (uintptr_t) interval; (void) nxt_thread_create(&handle, link); } } - static void -nxt_time_thread(void *data) -{ - nxt_nsec_t interval, rest; - nxt_thread_t *thr; - nxt_monotonic_time_t now; +nxt_time_thread(void *data) { + nxt_nsec_t interval, rest; + nxt_thread_t *thr; + nxt_monotonic_time_t now; - interval = (uintptr_t) data; + interval = (uintptr_t) data; interval *= 1000000; - thr = nxt_thread(); + thr = nxt_thread(); /* * The time thread is never preempted by asynchronous signals, since * the signals are processed synchronously by dedicated thread. @@ -120,10 +115,10 @@ nxt_time_thread(void *data) nxt_monotonic_time(&now); nxt_thread_realtime_update(thr, &now); - nxt_shared_time = now; + nxt_shared_time = now; nxt_use_shared_time = 1; - for ( ;; ) { + for (;;) { rest = 1000000000 - now.realtime.nsec; nxt_nanosleep(nxt_min(interval, rest)); @@ -147,17 +142,15 @@ nxt_time_thread(void *data) } } - static void -nxt_thread_time_shared(nxt_monotonic_time_t *now) -{ - nxt_uint_t n; - nxt_time_t t; - nxt_nsec_t m, u; +nxt_thread_time_shared(nxt_monotonic_time_t *now) { + nxt_uint_t n; + nxt_time_t t; + nxt_nsec_t m, u; /* Lock-free thread time update. */ - for ( ;; ) { + for (;;) { *now = nxt_shared_time; t = nxt_shared_time.realtime.sec; @@ -166,45 +159,37 @@ nxt_thread_time_shared(nxt_monotonic_time_t *now) u = nxt_shared_time.update; if (now->realtime.sec == t && now->realtime.nsec == n - && now->monotonic == m && now->update == u) - { + && now->monotonic == m && now->update == u) { return; } } } - nxt_time_t -nxt_thread_time(nxt_thread_t *thr) -{ +nxt_thread_time(nxt_thread_t *thr) { nxt_thread_realtime_update(thr, &thr->time.now); return thr->time.now.realtime.sec; } - nxt_realtime_t * -nxt_thread_realtime(nxt_thread_t *thr) -{ +nxt_thread_realtime(nxt_thread_t *thr) { nxt_thread_realtime_update(thr, &thr->time.now); return &thr->time.now.realtime; } - static void -nxt_thread_realtime_update(nxt_thread_t *thr, nxt_monotonic_time_t *now) -{ - nxt_nsec_t delta; +nxt_thread_realtime_update(nxt_thread_t *thr, nxt_monotonic_time_t *now) { + nxt_nsec_t delta; #if (NXT_DEBUG) if (nxt_slow_path(thr->log->level == NXT_LOG_DEBUG || nxt_debug)) { - if (now->monotonic >= now->update) { nxt_realtime(&now->realtime); - delta = 1000000 - now->realtime.nsec % 1000000; + delta = 1000000 - now->realtime.nsec % 1000000; now->update = now->monotonic + delta; } @@ -216,21 +201,19 @@ nxt_thread_realtime_update(nxt_thread_t *thr, nxt_monotonic_time_t *now) if (now->monotonic >= now->update) { nxt_realtime(&now->realtime); - delta = 1000000000 - now->realtime.nsec; + delta = 1000000000 - now->realtime.nsec; now->update = now->monotonic + delta; } } - u_char * -nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf) -{ - u_char *p; - struct tm *tm; +nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf) { + u_char *p; + struct tm *tm; nxt_time_t s; nxt_bool_t update; nxt_atomic_uint_t slot; - nxt_time_string_cache_t *tsc; + nxt_time_string_cache_t *tsc; if (nxt_slow_path(thr == NULL || thr->time.no_cache)) { return nxt_thread_time_string_no_cache(thr, ts, buf); @@ -258,25 +241,21 @@ nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf) #if (NXT_DEBUG) if (ts->msec == NXT_THREAD_TIME_MSEC - && (nxt_slow_path(thr->log->level == NXT_LOG_DEBUG || nxt_debug))) - { - nxt_msec_t ms; + && (nxt_slow_path(thr->log->level == NXT_LOG_DEBUG || nxt_debug))) { + nxt_msec_t ms; - ms = thr->time.now.realtime.nsec / 1000000; - update |= (ms != tsc->last_msec); - tsc->last_msec = ms; + ms = thr->time.now.realtime.nsec / 1000000; + update |= (ms != tsc->last_msec); + tsc->last_msec = ms; } #endif if (nxt_slow_path(update)) { - if (ts->timezone == NXT_THREAD_TIME_LOCAL) { - tm = &thr->time.localtime; if (nxt_slow_path(s != thr->time.last_localtime)) { - if (thr->time.signal < 0) { /* * Lazy local time update: @@ -314,15 +293,13 @@ nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf) nxt_gmtime(s, tm); thr->time.last_gmtime = s; } - } p = tsc->string.start; if (nxt_slow_path(p == NULL)) { - thr->time.no_cache = 1; - p = nxt_zalloc(ts->size); + p = nxt_zalloc(ts->size); thr->time.no_cache = 0; if (p == NULL) { @@ -346,18 +323,15 @@ nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf) return nxt_cpymem(buf, tsc->string.start, tsc->string.length); } - static u_char * nxt_thread_time_string_no_cache(nxt_thread_t *thr, nxt_time_string_t *ts, - u_char *buf) -{ - struct tm tm; - nxt_realtime_t now; + u_char *buf) { + struct tm tm; + nxt_realtime_t now; nxt_realtime(&now); if (ts->timezone == NXT_THREAD_TIME_LOCAL) { - if (thr == NULL || thr->time.signal <= 0) { /* Non-signal context */ nxt_localtime(now.sec, &tm); @@ -373,11 +347,9 @@ nxt_thread_time_string_no_cache(nxt_thread_t *thr, nxt_time_string_t *ts, return ts->handler(buf, &now, &tm, ts->size, ts->format); } - static nxt_atomic_uint_t -nxt_thread_time_string_slot(nxt_time_string_t *ts) -{ - static nxt_atomic_t slot; +nxt_thread_time_string_slot(nxt_time_string_t *ts) { + static nxt_atomic_t slot; while (nxt_slow_path((nxt_atomic_int_t) ts->slot < 0)) { /* @@ -395,13 +367,11 @@ nxt_thread_time_string_slot(nxt_time_string_t *ts) return (nxt_atomic_uint_t) ts->slot; } - static nxt_time_string_cache_t * -nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot) -{ +nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot) { size_t size; nxt_atomic_uint_t i, nstrings; - nxt_time_string_cache_t *tsc; + nxt_time_string_cache_t *tsc; if (nxt_fast_path(slot < thr->time.nstrings)) { tsc = &thr->time.strings[slot]; @@ -410,10 +380,10 @@ nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot) } nstrings = slot + 1; - size = nstrings * sizeof(nxt_time_string_cache_t); + size = nstrings * sizeof(nxt_time_string_cache_t); thr->time.no_cache = 1; - tsc = nxt_realloc(thr->time.strings, size); + tsc = nxt_realloc(thr->time.strings, size); thr->time.no_cache = 0; if (tsc == NULL) { @@ -421,11 +391,11 @@ nxt_thread_time_string_cache(nxt_thread_t *thr, nxt_atomic_uint_t slot) } for (i = thr->time.nstrings; i < nstrings; i++) { - tsc[i].last = -1; + tsc[i].last = -1; tsc[i].string.start = NULL; } - thr->time.strings = tsc; + thr->time.strings = tsc; thr->time.nstrings = nstrings; return &tsc[slot]; diff --git a/src/nxt_thread_time.h b/src/nxt_thread_time.h index 77eaea493..7df987d7e 100644 --- a/src/nxt_thread_time.h +++ b/src/nxt_thread_time.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,44 +7,40 @@ #define _NXT_THREAD_TIME_H_INCLUDED_ -#define NXT_THREAD_TIME_LOCAL 0 -#define NXT_THREAD_TIME_GMT 1 - -#define NXT_THREAD_TIME_SEC 0 -#define NXT_THREAD_TIME_MSEC 1 +#define NXT_THREAD_TIME_LOCAL 0 +#define NXT_THREAD_TIME_GMT 1 +#define NXT_THREAD_TIME_SEC 0 +#define NXT_THREAD_TIME_MSEC 1 typedef struct { - nxt_atomic_t slot; - u_char *(*handler)(u_char *buf, nxt_realtime_t *now, - struct tm *tm, size_t size, - const char *format); - const char *format; - size_t size; - - uint8_t timezone; /* 1 bit */ - uint8_t msec; /* 1 bit */ + nxt_atomic_t slot; + u_char *(*handler)(u_char *buf, nxt_realtime_t *now, struct tm *tm, + size_t size, const char *format); + const char *format; + size_t size; + + uint8_t timezone; /* 1 bit */ + uint8_t msec; /* 1 bit */ } nxt_time_string_t; - typedef struct { - nxt_time_t last; + nxt_time_t last; #if (NXT_DEBUG) - nxt_msec_t last_msec; + nxt_msec_t last_msec; #endif - nxt_str_t string; + nxt_str_t string; } nxt_time_string_cache_t; - typedef struct { - nxt_monotonic_time_t now; + nxt_monotonic_time_t now; - nxt_time_t last_gmtime; - nxt_time_t last_localtime; - struct tm gmtime; - struct tm localtime; + nxt_time_t last_gmtime; + nxt_time_t last_localtime; + struct tm gmtime; + struct tm localtime; - uint32_t no_cache; /* 1 bit */ + uint32_t no_cache; /* 1 bit */ /* * The flag indicating a signal state of a thread. @@ -54,30 +49,32 @@ typedef struct { * 0 means that the thread may run in a signal context but not now; * >0 means that the thread runs in a signal context right now. */ - nxt_atomic_int_t signal; + nxt_atomic_int_t signal; - nxt_atomic_uint_t nstrings; - nxt_time_string_cache_t *strings; + nxt_atomic_uint_t nstrings; + nxt_time_string_cache_t *strings; } nxt_thread_time_t; - -NXT_EXPORT void nxt_thread_time_update(nxt_thread_t *thr); -void nxt_thread_time_free(nxt_thread_t *thr); -NXT_EXPORT nxt_time_t nxt_thread_time(nxt_thread_t *thr); -NXT_EXPORT nxt_realtime_t *nxt_thread_realtime(nxt_thread_t *thr); -NXT_EXPORT u_char *nxt_thread_time_string(nxt_thread_t *thr, - nxt_time_string_t *ts, u_char *buf); -void nxt_time_thread_start(nxt_msec_t interval); +NXT_EXPORT void +nxt_thread_time_update(nxt_thread_t *thr); +void +nxt_thread_time_free(nxt_thread_t *thr); +NXT_EXPORT nxt_time_t +nxt_thread_time(nxt_thread_t *thr); +NXT_EXPORT nxt_realtime_t * +nxt_thread_realtime(nxt_thread_t *thr); +NXT_EXPORT u_char * +nxt_thread_time_string(nxt_thread_t *thr, nxt_time_string_t *ts, u_char *buf); +void +nxt_time_thread_start(nxt_msec_t interval); -#define nxt_thread_monotonic_time(thr) \ - (thr)->time.now.monotonic +#define nxt_thread_monotonic_time(thr) (thr)->time.now.monotonic #if (NXT_DEBUG) -#define nxt_thread_time_debug_update(thr) \ - nxt_thread_time_update(thr) +#define nxt_thread_time_debug_update(thr) nxt_thread_time_update(thr) #else @@ -86,7 +83,8 @@ void nxt_time_thread_start(nxt_msec_t interval); #endif -NXT_EXPORT void nxt_gmtime(nxt_time_t s, struct tm *tm); +NXT_EXPORT void +nxt_gmtime(nxt_time_t s, struct tm *tm); #endif /* _NXT_THREAD_TIME_H_INCLUDED_ */ diff --git a/src/nxt_time.c b/src/nxt_time.c index dfead51c5..b579e8c23 100644 --- a/src/nxt_time.c +++ b/src/nxt_time.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -22,13 +21,12 @@ */ void -nxt_realtime(nxt_realtime_t *now) -{ - struct timespec ts; +nxt_realtime(nxt_realtime_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_REALTIME_COARSE, &ts); - now->sec = (nxt_time_t) ts.tv_sec; + now->sec = (nxt_time_t) ts.tv_sec; now->nsec = ts.tv_nsec; } @@ -46,13 +44,12 @@ nxt_realtime(nxt_realtime_t *now) */ void -nxt_realtime(nxt_realtime_t *now) -{ - struct timespec ts; +nxt_realtime(nxt_realtime_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_REALTIME_FAST, &ts); - now->sec = (nxt_time_t) ts.tv_sec; + now->sec = (nxt_time_t) ts.tv_sec; now->nsec = ts.tv_nsec; } @@ -69,13 +66,12 @@ nxt_realtime(nxt_realtime_t *now) */ void -nxt_realtime(nxt_realtime_t *now) -{ - struct timespec ts; +nxt_realtime(nxt_realtime_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_REALTIME, &ts); - now->sec = (nxt_time_t) ts.tv_sec; + now->sec = (nxt_time_t) ts.tv_sec; now->nsec = ts.tv_nsec; } @@ -85,13 +81,12 @@ nxt_realtime(nxt_realtime_t *now) /* MacOSX, HP-UX. */ void -nxt_realtime(nxt_realtime_t *now) -{ - struct timeval tv; +nxt_realtime(nxt_realtime_t *now) { + struct timeval tv; (void) gettimeofday(&tv, NULL); - now->sec = (nxt_time_t) tv.tv_sec; + now->sec = (nxt_time_t) tv.tv_sec; now->nsec = tv.tv_usec * 1000; } @@ -110,9 +105,8 @@ nxt_realtime(nxt_realtime_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ - struct timespec ts; +nxt_monotonic_time(nxt_monotonic_time_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); @@ -133,9 +127,8 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ - struct timespec ts; +nxt_monotonic_time(nxt_monotonic_time_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_MONOTONIC_FAST, &ts); @@ -152,8 +145,7 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ +nxt_monotonic_time(nxt_monotonic_time_t *now) { now->monotonic = (nxt_nsec_t) hg_gethrtime(); } @@ -171,8 +163,7 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ +nxt_monotonic_time(nxt_monotonic_time_t *now) { now->monotonic = (nxt_nsec_t) gethrtime(); } @@ -187,9 +178,8 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ - struct timespec ts; +nxt_monotonic_time(nxt_monotonic_time_t *now) { + struct timespec ts; (void) clock_gettime(CLOCK_MONOTONIC, &ts); @@ -209,8 +199,7 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) */ void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ +nxt_monotonic_time(nxt_monotonic_time_t *now) { now->monotonic = mach_absolute_time(); } @@ -218,15 +207,14 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) #else void -nxt_monotonic_time(nxt_monotonic_time_t *now) -{ - nxt_nsec_t current; - nxt_nsec_int_t delta; - struct timeval tv; +nxt_monotonic_time(nxt_monotonic_time_t *now) { + nxt_nsec_t current; + nxt_nsec_int_t delta; + struct timeval tv; (void) gettimeofday(&tv, NULL); - now->realtime.sec = (nxt_time_t) tv.tv_sec; + now->realtime.sec = (nxt_time_t) tv.tv_sec; now->realtime.nsec = tv.tv_usec * 1000; /* @@ -234,8 +222,8 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) * for platforms which lack monotonic time. */ - current = (nxt_nsec_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000; - delta = current - now->previous; + current = (nxt_nsec_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000; + delta = current - now->previous; now->previous = current; if (delta > 0) { @@ -261,9 +249,8 @@ nxt_monotonic_time(nxt_monotonic_time_t *now) #if (NXT_HAVE_LOCALTIME_R) void -nxt_localtime(nxt_time_t s, struct tm *tm) -{ - time_t _s; +nxt_localtime(nxt_time_t s, struct tm *tm) { + time_t _s; _s = (time_t) s; (void) localtime_r(&_s, tm); @@ -273,12 +260,11 @@ nxt_localtime(nxt_time_t s, struct tm *tm) #else void -nxt_localtime(nxt_time_t s, struct tm *tm) -{ +nxt_localtime(nxt_time_t s, struct tm *tm) { time_t _s; - struct tm *_tm; + struct tm *_tm; - _s = (time_t) s; + _s = (time_t) s; _tm = localtime(&_s); *tm = *_tm; } @@ -296,9 +282,8 @@ nxt_localtime(nxt_time_t s, struct tm *tm) */ void -nxt_timezone_update(void) -{ - time_t s; +nxt_timezone_update(void) { + time_t s; s = time(NULL); (void) localtime(&s); @@ -314,8 +299,7 @@ nxt_timezone_update(void) */ void -nxt_timezone_update(void) -{ +nxt_timezone_update(void) { if (getenv("TZ") != NULL) { return; } @@ -345,9 +329,8 @@ nxt_timezone_update(void) */ void -nxt_timezone_update(void) -{ - time_t s; +nxt_timezone_update(void) { + time_t s; s = time(NULL); (void) ctime(&s); @@ -357,8 +340,7 @@ nxt_timezone_update(void) #else void -nxt_timezone_update(void) -{ +nxt_timezone_update(void) { return; } diff --git a/src/nxt_time.h b/src/nxt_time.h index 9617b3d41..a0698dab9 100644 --- a/src/nxt_time.h +++ b/src/nxt_time.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,37 +7,38 @@ #define _NXT_UNIX_TIME_H_INCLUDED_ -typedef uint64_t nxt_nsec_t; -typedef int64_t nxt_nsec_int_t; -#define NXT_INFINITE_NSEC ((nxt_nsec_t) -1) - +typedef uint64_t nxt_nsec_t; +typedef int64_t nxt_nsec_int_t; +#define NXT_INFINITE_NSEC ((nxt_nsec_t) - 1) typedef struct { - nxt_time_t sec; - nxt_uint_t nsec; + nxt_time_t sec; + nxt_uint_t nsec; } nxt_realtime_t; - /* * nxt_monotonic_time_t includes nxt_realtime_t to eliminate * surplus gettimeofday() call on platform without monotonic time. */ typedef struct { - nxt_realtime_t realtime; - nxt_nsec_t monotonic; - nxt_nsec_t update; + nxt_realtime_t realtime; + nxt_nsec_t monotonic; + nxt_nsec_t update; #if !(NXT_HAVE_CLOCK_MONOTONIC || NXT_SOLARIS || NXT_HPUX || NXT_MACOSX) - nxt_nsec_t previous; + nxt_nsec_t previous; #endif } nxt_monotonic_time_t; - -NXT_EXPORT void nxt_realtime(nxt_realtime_t *now); -NXT_EXPORT void nxt_monotonic_time(nxt_monotonic_time_t *now); -NXT_EXPORT void nxt_localtime(nxt_time_t s, struct tm *tm); -NXT_EXPORT void nxt_timezone_update(void); +NXT_EXPORT void +nxt_realtime(nxt_realtime_t *now); +NXT_EXPORT void +nxt_monotonic_time(nxt_monotonic_time_t *now); +NXT_EXPORT void +nxt_localtime(nxt_time_t s, struct tm *tm); +NXT_EXPORT void +nxt_timezone_update(void); /* * Both localtime() and localtime_r() are not Async-Signal-Safe, therefore, @@ -74,25 +74,22 @@ NXT_EXPORT void nxt_timezone_update(void); #if (NXT_HAVE_TM_GMTOFF) -#define nxt_timezone(tm) \ - ((tm)->tm_gmtoff) +#define nxt_timezone(tm) ((tm)->tm_gmtoff) #elif (NXT_HAVE_ALTZONE) -#define nxt_timezone(tm) \ - (-(((tm)->tm_isdst > 0) ? altzone : timezone)) +#define nxt_timezone(tm) (-(((tm)->tm_isdst > 0) ? altzone : timezone)) #else -#define nxt_timezone(tm) \ - (-(((tm)->tm_isdst > 0) ? timezone + 3600 : timezone)) +#define nxt_timezone(tm) (-(((tm)->tm_isdst > 0) ? timezone + 3600 : timezone)) #endif -typedef uint32_t nxt_msec_t; -typedef int32_t nxt_msec_int_t; -#define NXT_INFINITE_MSEC ((nxt_msec_t) -1) +typedef uint32_t nxt_msec_t; +typedef int32_t nxt_msec_int_t; +#define NXT_INFINITE_MSEC ((nxt_msec_t) - 1) /* @@ -100,8 +97,7 @@ typedef int32_t nxt_msec_int_t; * every 49 days. This signed subtraction takes into account that overflow. * "nxt_msec_diff(m1, m2) < 0" means that m1 is lesser than m2. */ -#define nxt_msec_diff(m1, m2) \ - ((int32_t) ((m1) - (m2))) +#define nxt_msec_diff(m1, m2) ((int32_t) ((m1) - (m2))) #endif /* _NXT_UNIX_TIME_H_INCLUDED_ */ diff --git a/src/nxt_time_parse.c b/src/nxt_time_parse.c index 1ac52fe4a..7f7e5defa 100644 --- a/src/nxt_time_parse.c +++ b/src/nxt_time_parse.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -6,30 +5,27 @@ #include - /* * nxt_time_parse() parses a time string given in RFC822, RFC850, or ISOC * formats and returns nxt_time_t value >= 0 on success or -1 on failure. */ nxt_time_t -nxt_time_parse(const u_char *p, size_t len) -{ - size_t n; - u_char c; - uint64_t s; - nxt_int_t yr, month, day, hour, min, sec; - nxt_uint_t year, days; - const u_char *end; - - static const nxt_int_t mday[12] = { - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - }; +nxt_time_parse(const u_char *p, size_t len) { + size_t n; + u_char c; + uint64_t s; + nxt_int_t yr, month, day, hour, min, sec; + nxt_uint_t year, days; + const u_char *end; + + static const nxt_int_t mday[12] + = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; enum { - RFC822 = 0, /* "Mon, 28 Sep 1970 12:00:00" */ - RFC850, /* "Monday, 28-Sep-70 12:00:00" */ - ISOC, /* "Mon Sep 28 12:00:00 1970" */ + RFC822 = 0, /* "Mon, 28 Sep 1970 12:00:00" */ + RFC850, /* "Monday, 28-Sep-70 12:00:00" */ + ISOC, /* "Mon Sep 28 12:00:00 1970" */ } fmt; fmt = RFC822; @@ -89,7 +85,6 @@ nxt_time_parse(const u_char *p, size_t len) } switch (*p) { - case 'J': month = p[1] == 'a' ? 0 : p[2] == 'n' ? 5 : 6; break; @@ -126,11 +121,10 @@ nxt_time_parse(const u_char *p, size_t len) return -1; } - p += 3; - yr = 0; + p += 3; + yr = 0; switch (fmt) { - case RFC822: if (nxt_slow_path(*p++ != ' ')) { return -1; @@ -239,7 +233,6 @@ nxt_time_parse(const u_char *p, size_t len) year = yr; if (day == 29 && month == 1) { - if (nxt_slow_path((year & 3) != 0)) { /* Not a leap year. */ return -1; @@ -261,16 +254,18 @@ nxt_time_parse(const u_char *p, size_t len) if (--month <= 0) { month += 12; - year -= 1; + year -= 1; } /* Gauss' formula for Gregorian days since March 1, 1 BCE. */ - /* Days in years including leap years since March 1, 1 BCE. */ - days = 365 * year + year / 4 - year / 100 + year / 400 + /* Days in years including leap years since March 1, 1 BCE. */ + days = 365 * year + year / 4 - year / 100 + + year / 400 /* Days before the month. */ - + 367 * (nxt_uint_t) month / 12 - 30 + + 367 * (nxt_uint_t) month / 12 + - 30 /* Days before the day. */ + (nxt_uint_t) day - 1; @@ -281,10 +276,8 @@ nxt_time_parse(const u_char *p, size_t len) */ days = days - 719527 + 31 + 28; - s = (uint64_t) days * 86400 - + (nxt_uint_t) hour * 3600 - + (nxt_uint_t) min * 60 - + (nxt_uint_t) sec; + s = (uint64_t) days * 86400 + (nxt_uint_t) hour * 3600 + + (nxt_uint_t) min * 60 + (nxt_uint_t) sec; #if (NXT_TIME_T_SIZE <= 4) @@ -299,7 +292,6 @@ nxt_time_parse(const u_char *p, size_t len) return (nxt_time_t) s; } - /* * nxt_term_parse() parses term string given in format "200", "10m", * or "1d 1h" and returns nxt_int_t value >= 0 on success, -1 on failure, @@ -308,11 +300,10 @@ nxt_time_parse(const u_char *p, size_t len) */ nxt_int_t -nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) -{ +nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) { u_char c, ch; nxt_uint_t val, term, scale, max; - const u_char *end; + const u_char *end; enum { st_first_digit = 0, @@ -333,19 +324,17 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) st_last, } step; - val = 0; - term = 0; + val = 0; + term = 0; state = st_first_digit; - step = seconds ? st_start : st_month; + step = seconds ? st_start : st_month; end = p + len; while (p < end) { - ch = *p++; if (state == st_space) { - if (ch == ' ') { continue; } @@ -357,7 +346,7 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) c = ch - '0'; if (c <= 9) { - val = val * 10 + c; + val = val * 10 + c; state = st_digit; continue; } @@ -367,13 +356,12 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) } switch (ch) { - case 'y': if (step > st_start) { return -1; } - step = st_year; - max = NXT_INT32_T_MAX / (365 * 24 * 60 * 60); + step = st_year; + max = NXT_INT32_T_MAX / (365 * 24 * 60 * 60); scale = 365 * 24 * 60 * 60; break; @@ -381,8 +369,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_month) { return -1; } - step = st_month; - max = NXT_INT32_T_MAX / (30 * 24 * 60 * 60); + step = st_month; + max = NXT_INT32_T_MAX / (30 * 24 * 60 * 60); scale = 30 * 24 * 60 * 60; break; @@ -390,8 +378,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_week) { return -1; } - step = st_week; - max = NXT_INT32_T_MAX / (7 * 24 * 60 * 60); + step = st_week; + max = NXT_INT32_T_MAX / (7 * 24 * 60 * 60); scale = 7 * 24 * 60 * 60; break; @@ -399,8 +387,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_day) { return -1; } - step = st_day; - max = NXT_INT32_T_MAX / (24 * 60 * 60); + step = st_day; + max = NXT_INT32_T_MAX / (24 * 60 * 60); scale = 24 * 60 * 60; break; @@ -408,8 +396,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_hour) { return -1; } - step = st_hour; - max = NXT_INT32_T_MAX / (60 * 60); + step = st_hour; + max = NXT_INT32_T_MAX / (60 * 60); scale = 60 * 60; break; @@ -419,8 +407,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) return -1; } p++; - step = st_msec; - max = NXT_INT32_T_MAX; + step = st_msec; + max = NXT_INT32_T_MAX; scale = 1; break; } @@ -428,8 +416,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_min) { return -1; } - step = st_min; - max = NXT_INT32_T_MAX / 60; + step = st_min; + max = NXT_INT32_T_MAX / 60; scale = 60; break; @@ -437,8 +425,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_sec) { return -1; } - step = st_sec; - max = NXT_INT32_T_MAX; + step = st_sec; + max = NXT_INT32_T_MAX; scale = 1; break; @@ -446,8 +434,8 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (step >= st_sec) { return -1; } - step = st_last; - max = NXT_INT32_T_MAX; + step = st_last; + max = NXT_INT32_T_MAX; scale = 1; break; @@ -457,7 +445,7 @@ nxt_term_parse(const u_char *p, size_t len, nxt_bool_t seconds) if (!seconds && step != st_msec) { scale *= 1000; - max /= 1000; + max /= 1000; } if (val > max) { diff --git a/src/nxt_timer.c b/src/nxt_timer.c index cb94b77c8..7c7b5d787 100644 --- a/src/nxt_timer.c +++ b/src/nxt_timer.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -19,17 +18,18 @@ * changes in the changes array or 0 otherwise. */ -static intptr_t nxt_timer_rbtree_compare(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2); -static void nxt_timer_change(nxt_event_engine_t *engine, nxt_timer_t *timer, +static intptr_t +nxt_timer_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2); +static void +nxt_timer_change(nxt_event_engine_t *engine, nxt_timer_t *timer, nxt_timer_operation_t change, nxt_msec_t time); -static void nxt_timer_changes_commit(nxt_event_engine_t *engine); -static void nxt_timer_handler(nxt_task_t *task, void *obj, void *data); - +static void +nxt_timer_changes_commit(nxt_event_engine_t *engine); +static void +nxt_timer_handler(nxt_task_t *task, void *obj, void *data); nxt_int_t -nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges) -{ +nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges) { nxt_rbtree_init(&timers->tree, nxt_timer_rbtree_compare); if (mchanges > NXT_TIMER_MAX_CHANGES) { @@ -47,11 +47,9 @@ nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges) return NXT_ERROR; } - static intptr_t -nxt_timer_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) -{ - nxt_timer_t *timer1, *timer2; +nxt_timer_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) { + nxt_timer_t *timer1, *timer2; timer1 = (nxt_timer_t *) node1; timer2 = (nxt_timer_t *) node2; @@ -61,27 +59,24 @@ nxt_timer_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) * and overflow every 49 days if nxt_msec_t is stored in 32 bits. * This signed comparison takes into account that overflow. */ - /* timer1->time < timer2->time */ - return nxt_msec_diff(timer1->time , timer2->time); + /* timer1->time < timer2->time */ + return nxt_msec_diff(timer1->time, timer2->time); } - void nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, - nxt_msec_t timeout) -{ - int32_t diff; - uint32_t time; + nxt_msec_t timeout) { + int32_t diff; + uint32_t time; time = engine->timers.now + timeout; - nxt_debug(timer->task, "timer add: %M±%d %M:%M", - timer->time, timer->bias, timeout, time); + nxt_debug(timer->task, "timer add: %M±%d %M:%M", timer->time, timer->bias, + timeout, time); timer->enabled = 1; if (nxt_timer_is_in_tree(timer)) { - diff = nxt_msec_diff(time, timer->time); /* * Use the previous timer if difference between it and the @@ -89,8 +84,7 @@ nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, * operations for fast connections. */ if (nxt_abs(diff) <= timer->bias) { - nxt_debug(timer->task, "timer previous: %M±%d", - time, timer->bias); + nxt_debug(timer->task, "timer previous: %M±%d", time, timer->bias); nxt_timer_change(engine, timer, NXT_TIMER_NOPE, 0); return; @@ -100,17 +94,13 @@ nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, nxt_timer_change(engine, timer, NXT_TIMER_ADD, time); } - nxt_bool_t -nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer) -{ - nxt_debug(timer->task, "timer delete: %M±%d", - timer->time, timer->bias); +nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer) { + nxt_debug(timer->task, "timer delete: %M±%d", timer->time, timer->bias); timer->enabled = 0; if (nxt_timer_is_in_tree(timer)) { - nxt_timer_change(engine, timer, NXT_TIMER_DELETE, 0); return 1; @@ -121,18 +111,15 @@ nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer) return (timer->queued || timer->change != NXT_TIMER_NO_CHANGE); } - static void nxt_timer_change(nxt_event_engine_t *engine, nxt_timer_t *timer, - nxt_timer_operation_t change, nxt_msec_t time) -{ - nxt_timers_t *timers; - nxt_timer_change_t *ch; + nxt_timer_operation_t change, nxt_msec_t time) { + nxt_timers_t *timers; + nxt_timer_change_t *ch; timers = &engine->timers; if (timer->change == NXT_TIMER_NO_CHANGE) { - if (change == NXT_TIMER_NOPE) { return; } @@ -145,39 +132,35 @@ nxt_timer_change(nxt_event_engine_t *engine, nxt_timer_t *timer, timer->change = timers->nchanges; } - nxt_debug(timer->task, "timer change: %M±%d:%d", - time, timer->bias, change); + nxt_debug(timer->task, "timer change: %M±%d:%d", time, timer->bias, change); ch = &timers->changes[timer->change - 1]; ch->change = change; - ch->time = time; - ch->timer = timer; + ch->time = time; + ch->timer = timer; } - static void -nxt_timer_changes_commit(nxt_event_engine_t *engine) -{ - nxt_timer_t *timer; - nxt_timers_t *timers; - nxt_timer_change_t *ch, *end, *add, *add_end; +nxt_timer_changes_commit(nxt_event_engine_t *engine) { + nxt_timer_t *timer; + nxt_timers_t *timers; + nxt_timer_change_t *ch, *end, *add, *add_end; timers = &engine->timers; nxt_debug(&engine->task, "timers changes: %ui", timers->nchanges); - ch = timers->changes; + ch = timers->changes; end = ch + timers->nchanges; - add = ch; + add = ch; add_end = add; while (ch < end) { timer = ch->timer; switch (ch->change) { - case NXT_TIMER_NOPE: break; @@ -195,8 +178,8 @@ nxt_timer_changes_commit(nxt_event_engine_t *engine) /* Fall through. */ case NXT_TIMER_DELETE: - nxt_debug(timer->task, "timer rbtree delete: %M±%d", - timer->time, timer->bias); + nxt_debug(timer->task, "timer rbtree delete: %M±%d", timer->time, + timer->bias); nxt_rbtree_delete(&timers->tree, &timer->node); nxt_timer_in_tree_clear(timer); @@ -212,8 +195,8 @@ nxt_timer_changes_commit(nxt_event_engine_t *engine) while (add < add_end) { timer = add->timer; - nxt_debug(timer->task, "timer rbtree insert: %M±%d", - timer->time, timer->bias); + nxt_debug(timer->task, "timer rbtree insert: %M±%d", timer->time, + timer->bias); nxt_rbtree_insert(&timers->tree, &timer->node); nxt_timer_in_tree_set(timer); @@ -224,16 +207,14 @@ nxt_timer_changes_commit(nxt_event_engine_t *engine) timers->nchanges = 0; } - nxt_msec_t -nxt_timer_find(nxt_event_engine_t *engine) -{ +nxt_timer_find(nxt_event_engine_t *engine) { int32_t delta; nxt_msec_t time; - nxt_timer_t *timer; - nxt_timers_t *timers; - nxt_rbtree_t *tree; - nxt_rbtree_node_t *node, *next; + nxt_timer_t *timer; + nxt_timers_t *timers; + nxt_rbtree_t *tree; + nxt_rbtree_node_t *node, *next; timers = &engine->timers; @@ -243,10 +224,8 @@ nxt_timer_find(nxt_event_engine_t *engine) tree = &timers->tree; - for (node = nxt_rbtree_min(tree); - nxt_rbtree_is_there_successor(tree, node); - node = next) - { + for (node = nxt_rbtree_min(tree); nxt_rbtree_is_there_successor(tree, node); + node = next) { next = nxt_rbtree_node_successor(tree, node); timer = (nxt_timer_t *) node; @@ -258,11 +237,11 @@ nxt_timer_find(nxt_event_engine_t *engine) */ if (timer->enabled) { - time = timer->time; + time = timer->time; timers->minimum = time - timer->bias; - nxt_debug(timer->task, "timer found minimum: %M±%d:%M", - time, timer->bias, timers->now); + nxt_debug(timer->task, "timer found minimum: %M±%d:%M", time, + timer->bias, timers->now); delta = nxt_msec_diff(time, timers->now); @@ -276,43 +255,39 @@ nxt_timer_find(nxt_event_engine_t *engine) return NXT_INFINITE_MSEC; } - void -nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now) -{ - nxt_timer_t *timer; - nxt_timers_t *timers; - nxt_rbtree_t *tree; - nxt_rbtree_node_t *node, *next; +nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now) { + nxt_timer_t *timer; + nxt_timers_t *timers; + nxt_rbtree_t *tree; + nxt_rbtree_node_t *node, *next; - timers = &engine->timers; + timers = &engine->timers; timers->now = now; - nxt_debug(&engine->task, "timer expire minimum: %M:%M", - timers->minimum, now); + nxt_debug(&engine->task, "timer expire minimum: %M:%M", timers->minimum, + now); - /* timers->minimum > now */ - if (nxt_msec_diff(timers->minimum , now) > 0) { + /* timers->minimum > now */ + if (nxt_msec_diff(timers->minimum, now) > 0) { return; } tree = &timers->tree; - for (node = nxt_rbtree_min(tree); - nxt_rbtree_is_there_successor(tree, node); - node = next) - { + for (node = nxt_rbtree_min(tree); nxt_rbtree_is_there_successor(tree, node); + node = next) { timer = (nxt_timer_t *) node; - /* timer->time > now + timer->bias */ - if (nxt_msec_diff(timer->time , now) > (int32_t) timer->bias) { + /* timer->time > now + timer->bias */ + if (nxt_msec_diff(timer->time, now) > (int32_t) timer->bias) { return; } next = nxt_rbtree_node_successor(tree, node); - nxt_debug(timer->task, "timer expire delete: %M±%d", - timer->time, timer->bias); + nxt_debug(timer->task, "timer expire delete: %M±%d", timer->time, + timer->bias); nxt_rbtree_delete(tree, &timer->node); nxt_timer_in_tree_clear(timer); @@ -321,16 +296,14 @@ nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now) timer->queued = 1; nxt_work_queue_add(timer->work_queue, nxt_timer_handler, - timer->task, timer, NULL); + timer->task, timer, NULL); } } } - static void -nxt_timer_handler(nxt_task_t *task, void *obj, void *data) -{ - nxt_timer_t *timer; +nxt_timer_handler(nxt_task_t *task, void *obj, void *data) { + nxt_timer_t *timer; timer = obj; diff --git a/src/nxt_timer.h b/src/nxt_timer.h index 3ccff848d..1b3764909 100644 --- a/src/nxt_timer.h +++ b/src/nxt_timer.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,41 +8,39 @@ /* Valid values are between 0ms to 255ms. */ -#define NXT_TIMER_DEFAULT_BIAS 50 -//#define NXT_TIMER_DEFAULT_BIAS 0 +#define NXT_TIMER_DEFAULT_BIAS 50 +// #define NXT_TIMER_DEFAULT_BIAS 0 /* * The nxt_timer_t structure can hold up to 14 bits of change index, * but 0 reserved for NXT_TIMER_NO_CHANGE. */ -#define NXT_TIMER_MAX_CHANGES 16383 -#define NXT_TIMER_NO_CHANGE 0 - +#define NXT_TIMER_MAX_CHANGES 16383 +#define NXT_TIMER_NO_CHANGE 0 typedef struct { /* The rbtree node must be the first field. */ - NXT_RBTREE_NODE (node); + NXT_RBTREE_NODE(node); - uint8_t bias; + uint8_t bias; - uint16_t change:14; - uint16_t enabled:1; - uint16_t queued:1; + uint16_t change :14; + uint16_t enabled:1; + uint16_t queued :1; - nxt_msec_t time; + nxt_msec_t time; - nxt_work_queue_t *work_queue; - nxt_work_handler_t handler; + nxt_work_queue_t *work_queue; + nxt_work_handler_t handler; - nxt_task_t *task; - nxt_log_t *log; + nxt_task_t *task; + nxt_log_t *log; } nxt_timer_t; - -#define NXT_TIMER { NXT_RBTREE_NODE_INIT, 0, NXT_TIMER_NO_CHANGE, \ - 0, 0, 0, NULL, NULL, NULL, NULL } - +#define NXT_TIMER \ + {NXT_RBTREE_NODE_INIT, 0, NXT_TIMER_NO_CHANGE, 0, 0, 0, NULL, NULL, NULL, \ + NULL} typedef enum { NXT_TIMER_NOPE = 0, @@ -51,30 +48,26 @@ typedef enum { NXT_TIMER_DELETE, } nxt_timer_operation_t; - typedef struct { - nxt_timer_operation_t change:8; - nxt_msec_t time; - nxt_timer_t *timer; + nxt_timer_operation_t change:8; + nxt_msec_t time; + nxt_timer_t *timer; } nxt_timer_change_t; - typedef struct { - nxt_rbtree_t tree; + nxt_rbtree_t tree; /* An overflown milliseconds counter. */ - nxt_msec_t now; - nxt_msec_t minimum; + nxt_msec_t now; + nxt_msec_t minimum; - nxt_uint_t mchanges; - nxt_uint_t nchanges; + nxt_uint_t mchanges; + nxt_uint_t nchanges; - nxt_timer_change_t *changes; + nxt_timer_change_t *changes; } nxt_timers_t; - -#define nxt_timer_data(obj, type, timer) \ - nxt_container_of(obj, type, timer) +#define nxt_timer_data(obj, type, timer) nxt_container_of(obj, type, timer) /* @@ -82,28 +75,29 @@ typedef struct { * A parent link is the nearst to other timer flags. */ -#define nxt_timer_is_in_tree(timer) \ - ((timer)->node.parent != NULL) +#define nxt_timer_is_in_tree(timer) ((timer)->node.parent != NULL) #define nxt_timer_in_tree_set(timer) - /* Noop, because rbtree insertion sets a node's parent link. */ +/* Noop, because rbtree insertion sets a node's parent link. */ -#define nxt_timer_in_tree_clear(timer) \ - (timer)->node.parent = NULL +#define nxt_timer_in_tree_clear(timer) (timer)->node.parent = NULL -nxt_int_t nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges); -nxt_msec_t nxt_timer_find(nxt_event_engine_t *engine); -void nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now); +nxt_int_t +nxt_timers_init(nxt_timers_t *timers, nxt_uint_t mchanges); +nxt_msec_t +nxt_timer_find(nxt_event_engine_t *engine); +void +nxt_timer_expire(nxt_event_engine_t *engine, nxt_msec_t now); -NXT_EXPORT void nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, +NXT_EXPORT void +nxt_timer_add(nxt_event_engine_t *engine, nxt_timer_t *timer, nxt_msec_t timeout); -NXT_EXPORT nxt_bool_t nxt_timer_delete(nxt_event_engine_t *engine, - nxt_timer_t *timer); +NXT_EXPORT nxt_bool_t +nxt_timer_delete(nxt_event_engine_t *engine, nxt_timer_t *timer); nxt_inline void -nxt_timer_disable(nxt_event_engine_t *engine, nxt_timer_t *timer) -{ +nxt_timer_disable(nxt_event_engine_t *engine, nxt_timer_t *timer) { nxt_debug(timer->task, "timer disable: %M", timer->time); timer->enabled = 0; diff --git a/src/nxt_tls.h b/src/nxt_tls.h index 0667ade3c..90cc7e69a 100644 --- a/src/nxt_tls.h +++ b/src/nxt_tls.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -23,93 +22,87 @@ * and compatible with tunnels. */ -#define NXT_TLS_BUFFER_SIZE 4096 +#define NXT_TLS_BUFFER_SIZE 4096 -typedef struct nxt_tls_conf_s nxt_tls_conf_t; -typedef struct nxt_tls_bundle_conf_s nxt_tls_bundle_conf_t; -typedef struct nxt_tls_init_s nxt_tls_init_t; -typedef struct nxt_tls_ticket_s nxt_tls_ticket_t; -typedef struct nxt_tls_tickets_s nxt_tls_tickets_t; +typedef struct nxt_tls_conf_s nxt_tls_conf_t; +typedef struct nxt_tls_bundle_conf_s nxt_tls_bundle_conf_t; +typedef struct nxt_tls_init_s nxt_tls_init_t; +typedef struct nxt_tls_ticket_s nxt_tls_ticket_t; +typedef struct nxt_tls_tickets_s nxt_tls_tickets_t; typedef struct { - nxt_int_t (*library_init)(nxt_task_t *task); - void (*library_free)(nxt_task_t *task); - - nxt_int_t (*server_init)(nxt_task_t *task, nxt_mp_t *mp, - nxt_tls_init_t *tls_init, - nxt_bool_t last); - void (*server_free)(nxt_task_t *task, - nxt_tls_conf_t *conf); -} nxt_tls_lib_t; + nxt_int_t (*library_init)(nxt_task_t *task); + void (*library_free)(nxt_task_t *task); + nxt_int_t (*server_init)(nxt_task_t *task, nxt_mp_t *mp, + nxt_tls_init_t *tls_init, nxt_bool_t last); + void (*server_free)(nxt_task_t *task, nxt_tls_conf_t *conf); +} nxt_tls_lib_t; typedef struct { - nxt_tls_bundle_conf_t *bundle; + nxt_tls_bundle_conf_t *bundle; - nxt_str_t name; + nxt_str_t name; } nxt_tls_bundle_hash_item_t; - struct nxt_tls_bundle_conf_s { - void *ctx; + void *ctx; - nxt_fd_t chain_file; - nxt_str_t name; + nxt_fd_t chain_file; + nxt_str_t name; - nxt_tls_bundle_conf_t *next; + nxt_tls_bundle_conf_t *next; }; - struct nxt_tls_conf_s { - nxt_tls_bundle_conf_t *bundle; - nxt_lvlhsh_t bundle_hash; + nxt_tls_bundle_conf_t *bundle; + nxt_lvlhsh_t bundle_hash; - nxt_tls_tickets_t *tickets; + nxt_tls_tickets_t *tickets; - void (*conn_init)(nxt_task_t *task, - nxt_tls_conf_t *conf, nxt_conn_t *c); + void (*conn_init)(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c); - const nxt_tls_lib_t *lib; + const nxt_tls_lib_t *lib; - char *ciphers; + char *ciphers; - char *ca_certificate; + char *ca_certificate; - size_t buffer_size; + size_t buffer_size; - uint8_t no_wait_shutdown; /* 1 bit */ + uint8_t no_wait_shutdown; /* 1 bit */ }; - struct nxt_tls_init_s { - size_t cache_size; - nxt_time_t timeout; - nxt_conf_value_t *conf_cmds; - nxt_conf_value_t *tickets_conf; + size_t cache_size; + nxt_time_t timeout; + nxt_conf_value_t *conf_cmds; + nxt_conf_value_t *tickets_conf; - nxt_tls_conf_t *conf; + nxt_tls_conf_t *conf; }; #if (NXT_HAVE_OPENSSL) -extern const nxt_tls_lib_t nxt_openssl_lib; +extern const nxt_tls_lib_t nxt_openssl_lib; -void nxt_cdecl nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, - const char *fmt, ...); -u_char *nxt_openssl_copy_error(u_char *p, u_char *end); +void nxt_cdecl +nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, ...); +u_char * +nxt_openssl_copy_error(u_char *p, u_char *end); #endif #if (NXT_HAVE_GNUTLS) -extern const nxt_tls_lib_t nxt_gnutls_lib; +extern const nxt_tls_lib_t nxt_gnutls_lib; #endif #if (NXT_HAVE_CYASSL) -extern const nxt_tls_lib_t nxt_cyassl_lib; +extern const nxt_tls_lib_t nxt_cyassl_lib; #endif #if (NXT_HAVE_POLARSSL) -extern const nxt_tls_lib_t nxt_polar_lib; +extern const nxt_tls_lib_t nxt_polar_lib; #endif diff --git a/src/nxt_tstr.c b/src/nxt_tstr.c index 36d77e69e..49800298e 100644 --- a/src/nxt_tstr.c +++ b/src/nxt_tstr.c @@ -1,11 +1,9 @@ - /* * Copyright (C) NGINX, Inc. */ #include - typedef enum { NXT_TSTR_CONST = 0, NXT_TSTR_VAR, @@ -14,41 +12,35 @@ typedef enum { #endif } nxt_tstr_type_t; - struct nxt_tstr_s { - nxt_str_t str; + nxt_str_t str; union { - nxt_var_t *var; + nxt_var_t *var; #if (NXT_HAVE_NJS) - nxt_js_t *js; + nxt_js_t *js; #endif } u; - nxt_tstr_flags_t flags; - nxt_tstr_type_t type; + nxt_tstr_flags_t flags; + nxt_tstr_type_t type; }; - struct nxt_tstr_query_s { - nxt_mp_t *pool; + nxt_mp_t *pool; - nxt_tstr_state_t *state; - nxt_tstr_cache_t *cache; + nxt_tstr_state_t *state; + nxt_tstr_cache_t *cache; - void *ctx; - void *data; + void *ctx; + void *data; }; - -#define nxt_tstr_is_js(str) \ - nxt_strchr_start(str, '`') - +#define nxt_tstr_is_js(str) nxt_strchr_start(str, '`') nxt_tstr_state_t * -nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test) -{ - nxt_tstr_state_t *state; +nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test) { + nxt_tstr_state_t *state; state = nxt_mp_get(mp, sizeof(nxt_tstr_state_t)); if (nxt_slow_path(state == NULL)) { @@ -73,16 +65,14 @@ nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test) return state; } - nxt_tstr_t * nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, - nxt_tstr_flags_t flags) -{ - u_char *p; - nxt_tstr_t *tstr; + nxt_tstr_flags_t flags) { + u_char *p; + nxt_tstr_t *tstr; nxt_bool_t strz, newline; - strz = (flags & NXT_TSTR_STRZ) != 0; + strz = (flags & NXT_TSTR_STRZ) != 0; newline = (flags & NXT_TSTR_NEWLINE) != 0; tstr = nxt_mp_get(state->pool, sizeof(nxt_tstr_t)); @@ -110,10 +100,9 @@ nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, tstr->flags = flags; if (nxt_tstr_is_js(str)) { - #if (NXT_HAVE_NJS) - nxt_str_t tpl; + nxt_str_t tpl; tstr->type = NXT_TSTR_JS; @@ -145,11 +134,9 @@ nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, return tstr; } - nxt_int_t -nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) -{ - u_char *p; +nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) { + u_char *p; if (nxt_tstr_is_js(str)) { #if (NXT_HAVE_NJS) @@ -157,8 +144,8 @@ nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) #else nxt_sprintf(error, error + NXT_MAX_ERROR_STR, - "Unit is built without support of njs: " - "\"--njs\" ./configure option is missing.%Z"); + "Unit is built without support of njs: " + "\"--njs\" ./configure option is missing.%Z"); return NXT_ERROR; #endif @@ -173,13 +160,11 @@ nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) return NXT_OK; } - nxt_int_t -nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error) -{ +nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error) { #if (NXT_HAVE_NJS) if (!state->test) { - nxt_int_t ret; + nxt_int_t ret; ret = nxt_js_compile(state->jcf); if (nxt_slow_path(ret != NXT_OK)) { @@ -188,23 +173,19 @@ nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error) } #endif - return NXT_OK; + return NXT_OK; } - void -nxt_tstr_state_release(nxt_tstr_state_t *state) -{ +nxt_tstr_state_release(nxt_tstr_state_t *state) { #if (NXT_HAVE_NJS) nxt_js_conf_release(state->jcf); #endif } - nxt_int_t nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str, - nxt_tstr_cond_t *cond) -{ + nxt_tstr_cond_t *cond) { if (str->length > 0 && str->start[0] == '!') { cond->negate = 1; @@ -220,17 +201,13 @@ nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str, return NXT_OK; } - nxt_bool_t -nxt_tstr_is_const(nxt_tstr_t *tstr) -{ +nxt_tstr_is_const(nxt_tstr_t *tstr) { return (tstr->type == NXT_TSTR_CONST); } - void -nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str) -{ +nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str) { *str = tstr->str; if (tstr->flags & NXT_TSTR_STRZ) { @@ -238,12 +215,10 @@ nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str) } } - nxt_int_t nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state, - nxt_tstr_cache_t *cache, void *ctx, nxt_mp_t *mp) -{ - nxt_tstr_query_t *query; + nxt_tstr_cache_t *cache, void *ctx, nxt_mp_t *mp) { + nxt_tstr_query_t *query; query = *query_p; @@ -254,22 +229,20 @@ nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state, } } - query->pool = mp; + query->pool = mp; query->state = state; query->cache = cache; - query->ctx = ctx; + query->ctx = ctx; *query_p = query; return NXT_OK; } - nxt_int_t nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, - nxt_str_t *val) -{ - nxt_int_t ret; + nxt_str_t *val) { + nxt_int_t ret; if (nxt_tstr_is_const(tstr)) { nxt_tstr_str(tstr, val); @@ -278,8 +251,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, if (tstr->type == NXT_TSTR_VAR) { ret = nxt_var_interpreter(task, query->state, &query->cache->var, - tstr->u.var, val, query->ctx, - tstr->flags & NXT_TSTR_LOGGING); + tstr->u.var, val, query->ctx, tstr->flags & NXT_TSTR_LOGGING); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -288,7 +260,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, } else { #if (NXT_HAVE_NJS) ret = nxt_js_call(task, query->state->jcf, &query->cache->js, - tstr->u.js, val, query->ctx); + tstr->u.js, val, query->ctx); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; @@ -301,7 +273,7 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, } #if (NXT_DEBUG) - nxt_str_t str; + nxt_str_t str; nxt_tstr_str(tstr, &str); @@ -311,10 +283,8 @@ nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, return NXT_OK; } - void -nxt_tstr_query_release(nxt_tstr_query_t *query) -{ +nxt_tstr_query_release(nxt_tstr_query_t *query) { #if (NXT_HAVE_NJS) nxt_js_release(&query->cache->js); #endif diff --git a/src/nxt_tstr.h b/src/nxt_tstr.h index 8e3cdb93e..7a008f58f 100644 --- a/src/nxt_tstr.h +++ b/src/nxt_tstr.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -9,65 +8,68 @@ #include -typedef struct nxt_tstr_s nxt_tstr_t; -typedef struct nxt_tstr_query_s nxt_tstr_query_t; - +typedef struct nxt_tstr_s nxt_tstr_t; +typedef struct nxt_tstr_query_s nxt_tstr_query_t; struct nxt_tstr_state_s { - nxt_mp_t *pool; - nxt_array_t *var_refs; + nxt_mp_t *pool; + nxt_array_t *var_refs; #if (NXT_HAVE_NJS) - nxt_js_conf_t *jcf; + nxt_js_conf_t *jcf; #endif - uint8_t test; /* 1 bit */ + uint8_t test; /* 1 bit */ }; - typedef struct { - nxt_var_cache_t var; + nxt_var_cache_t var; #if (NXT_HAVE_NJS) - nxt_js_cache_t js; + nxt_js_cache_t js; #endif } nxt_tstr_cache_t; - typedef enum { - NXT_TSTR_STRZ = 1 << 0, - NXT_TSTR_LOGGING = 1 << 1, - NXT_TSTR_NEWLINE = 1 << 2, + NXT_TSTR_STRZ = 1 << 0, + NXT_TSTR_LOGGING = 1 << 1, + NXT_TSTR_NEWLINE = 1 << 2, } nxt_tstr_flags_t; - typedef struct { - nxt_tstr_t *expr; - uint8_t negate; /* 1 bit */ + nxt_tstr_t *expr; + uint8_t negate; /* 1 bit */ } nxt_tstr_cond_t; - -nxt_tstr_state_t *nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test); -nxt_tstr_t *nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, +nxt_tstr_state_t * +nxt_tstr_state_new(nxt_mp_t *mp, nxt_bool_t test); +nxt_tstr_t * +nxt_tstr_compile(nxt_tstr_state_t *state, const nxt_str_t *str, nxt_tstr_flags_t flags); -nxt_int_t nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error); -nxt_int_t nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error); -void nxt_tstr_state_release(nxt_tstr_state_t *state); -nxt_int_t nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str, +nxt_int_t +nxt_tstr_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error); +nxt_int_t +nxt_tstr_state_done(nxt_tstr_state_t *state, u_char *error); +void +nxt_tstr_state_release(nxt_tstr_state_t *state); +nxt_int_t +nxt_tstr_cond_compile(nxt_tstr_state_t *state, nxt_str_t *str, nxt_tstr_cond_t *cond); -nxt_bool_t nxt_tstr_is_const(nxt_tstr_t *tstr); -void nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str); - -nxt_int_t nxt_tstr_query_init(nxt_tstr_query_t **query_p, - nxt_tstr_state_t *state, nxt_tstr_cache_t *cache, void *ctx, - nxt_mp_t *mp); -nxt_int_t nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, - nxt_tstr_t *tstr, nxt_str_t *val); -void nxt_tstr_query_release(nxt_tstr_query_t *query); +nxt_bool_t +nxt_tstr_is_const(nxt_tstr_t *tstr); +void +nxt_tstr_str(nxt_tstr_t *tstr, nxt_str_t *str); +nxt_int_t +nxt_tstr_query_init(nxt_tstr_query_t **query_p, nxt_tstr_state_t *state, + nxt_tstr_cache_t *cache, void *ctx, nxt_mp_t *mp); +nxt_int_t +nxt_tstr_query(nxt_task_t *task, nxt_tstr_query_t *query, nxt_tstr_t *tstr, + nxt_str_t *val); +void +nxt_tstr_query_release(nxt_tstr_query_t *query); nxt_inline nxt_bool_t -nxt_is_tstr(nxt_str_t *str) -{ - u_char *p; +nxt_is_tstr(nxt_str_t *str) { + u_char *p; p = memchr(str->start, '`', str->length); if (p != NULL) { diff --git a/src/nxt_types.h b/src/nxt_types.h index 723346d9f..255e3485e 100644 --- a/src/nxt_types.h +++ b/src/nxt_types.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -20,18 +19,18 @@ * AMD64 64-bit multiplication and division operations * are slower and 64-bit instructions are longer. */ -#define NXT_INT_T_SIZE 4 -typedef int nxt_int_t; -typedef u_int nxt_uint_t; +#define NXT_INT_T_SIZE 4 +typedef int nxt_int_t; +typedef u_int nxt_uint_t; #else -#define NXT_INT_T_SIZE NXT_PTR_SIZE -typedef intptr_t nxt_int_t; -typedef uintptr_t nxt_uint_t; +#define NXT_INT_T_SIZE NXT_PTR_SIZE +typedef intptr_t nxt_int_t; +typedef uintptr_t nxt_uint_t; #endif -typedef nxt_uint_t nxt_bool_t; +typedef nxt_uint_t nxt_bool_t; /* @@ -39,7 +38,7 @@ typedef nxt_uint_t nxt_bool_t; * Although Linux, Solaris, and HP-UX define both off_t and off64_t, * setting _FILE_OFFSET_BITS to 64 defines off_t as off64_t. */ -typedef off_t nxt_off_t; +typedef off_t nxt_off_t; /* @@ -57,93 +56,93 @@ typedef off_t nxt_off_t; * QNX defines time_t as uint32_t. * Y2038 fix: "typedef int64_t nxt_time_t". */ -typedef int32_t nxt_time_t; +typedef int32_t nxt_time_t; #else /* Y2038, if time_t is 32-bit integer. */ -typedef time_t nxt_time_t; +typedef time_t nxt_time_t; #endif #if (NXT_PTR_SIZE == 8) -#define NXT_64BIT 1 -#define NXT_32BIT 0 +#define NXT_64BIT 1 +#define NXT_32BIT 0 #else -#define NXT_64BIT 0 -#define NXT_32BIT 1 +#define NXT_64BIT 0 +#define NXT_32BIT 1 #endif -#define NXT_INT64_T_LEN nxt_length("-9223372036854775808") -#define NXT_INT32_T_LEN nxt_length("-2147483648") +#define NXT_INT64_T_LEN nxt_length("-9223372036854775808") +#define NXT_INT32_T_LEN nxt_length("-2147483648") -#define NXT_INT64_T_HEXLEN nxt_length("FFFFFFFFFFFFFFFF") -#define NXT_INT32_T_HEXLEN nxt_length("FFFFFFFF") +#define NXT_INT64_T_HEXLEN nxt_length("FFFFFFFFFFFFFFFF") +#define NXT_INT32_T_HEXLEN nxt_length("FFFFFFFF") -#define NXT_INT64_T_MAX 0x7FFFFFFFFFFFFFFFLL -#define NXT_INT32_T_MAX 0x7FFFFFFF +#define NXT_INT64_T_MAX 0x7FFFFFFFFFFFFFFFLL +#define NXT_INT32_T_MAX 0x7FFFFFFF #if (NXT_INT_T_SIZE == 8) -#define NXT_INT_T_LEN NXT_INT64_T_LEN -#define NXT_INT_T_HEXLEN NXT_INT64_T_HEXLEN -#define NXT_INT_T_MAX NXT_INT64_T_MAX +#define NXT_INT_T_LEN NXT_INT64_T_LEN +#define NXT_INT_T_HEXLEN NXT_INT64_T_HEXLEN +#define NXT_INT_T_MAX NXT_INT64_T_MAX #else -#define NXT_INT_T_LEN NXT_INT32_T_LEN -#define NXT_INT_T_HEXLEN NXT_INT32_T_HEXLEN -#define NXT_INT_T_MAX NXT_INT32_T_MAX +#define NXT_INT_T_LEN NXT_INT32_T_LEN +#define NXT_INT_T_HEXLEN NXT_INT32_T_HEXLEN +#define NXT_INT_T_MAX NXT_INT32_T_MAX #endif #if (NXT_64BIT) -#define NXT_ATOMIC_T_LEN NXT_INT64_T_LEN -#define NXT_ATOMIC_T_HEXLEN NXT_INT64_T_HEXLEN -#define NXT_ATOMIC_T_MAX NXT_INT64_T_MAX +#define NXT_ATOMIC_T_LEN NXT_INT64_T_LEN +#define NXT_ATOMIC_T_HEXLEN NXT_INT64_T_HEXLEN +#define NXT_ATOMIC_T_MAX NXT_INT64_T_MAX #else -#define NXT_ATOMIC_T_LEN NXT_INT32_T_LEN -#define NXT_ATOMIC_T_HEXLEN NXT_INT32_T_HEXLEN -#define NXT_ATOMIC_T_MAX NXT_INT32_T_MAX +#define NXT_ATOMIC_T_LEN NXT_INT32_T_LEN +#define NXT_ATOMIC_T_HEXLEN NXT_INT32_T_HEXLEN +#define NXT_ATOMIC_T_MAX NXT_INT32_T_MAX #endif #if (NXT_OFF_T_SIZE == 8) -typedef uint64_t nxt_uoff_t; -#define NXT_OFF_T_LEN NXT_INT64_T_LEN -#define NXT_OFF_T_HEXLEN NXT_INT64_T_HEXLEN -#define NXT_OFF_T_MAX NXT_INT64_T_MAX +typedef uint64_t nxt_uoff_t; +#define NXT_OFF_T_LEN NXT_INT64_T_LEN +#define NXT_OFF_T_HEXLEN NXT_INT64_T_HEXLEN +#define NXT_OFF_T_MAX NXT_INT64_T_MAX #else -typedef uint32_t nxt_uoff_t; -#define NXT_OFF_T_LEN NXT_INT32_T_LEN -#define NXT_OFF_T_HEXLEN NXT_INT32_T_HEXLEN -#define NXT_OFF_T_MAX NXT_INT32_T_MAX +typedef uint32_t nxt_uoff_t; +#define NXT_OFF_T_LEN NXT_INT32_T_LEN +#define NXT_OFF_T_HEXLEN NXT_INT32_T_HEXLEN +#define NXT_OFF_T_MAX NXT_INT32_T_MAX #endif #if (NXT_SIZE_T_SIZE == 8) -#define NXT_SIZE_T_LEN NXT_INT64_T_LEN -#define NXT_SIZE_T_HEXLEN NXT_INT64_T_HEXLEN -#define NXT_SIZE_T_MAX NXT_INT64_T_MAX +#define NXT_SIZE_T_LEN NXT_INT64_T_LEN +#define NXT_SIZE_T_HEXLEN NXT_INT64_T_HEXLEN +#define NXT_SIZE_T_MAX NXT_INT64_T_MAX #else -#define NXT_SIZE_T_LEN NXT_INT32_T_LEN -#define NXT_SIZE_T_HEXLEN NXT_INT32_T_HEXLEN -#define NXT_SIZE_T_MAX NXT_INT32_T_MAX +#define NXT_SIZE_T_LEN NXT_INT32_T_LEN +#define NXT_SIZE_T_HEXLEN NXT_INT32_T_HEXLEN +#define NXT_SIZE_T_MAX NXT_INT32_T_MAX #endif #if (NXT_TIME_T_SIZE == 8) -#define NXT_TIME_T_LEN NXT_INT64_T_LEN -#define NXT_TIME_T_HEXLEN NXT_INT64_T_HEXLEN -#define NXT_TIME_T_MAX NXT_INT64_T_MAX +#define NXT_TIME_T_LEN NXT_INT64_T_LEN +#define NXT_TIME_T_HEXLEN NXT_INT64_T_HEXLEN +#define NXT_TIME_T_MAX NXT_INT64_T_MAX #else -#define NXT_TIME_T_LEN NXT_INT32_T_LEN -#define NXT_TIME_T_HEXLEN NXT_INT32_T_HEXLEN -#define NXT_TIME_T_MAX NXT_INT32_T_MAX +#define NXT_TIME_T_LEN NXT_INT32_T_LEN +#define NXT_TIME_T_HEXLEN NXT_INT32_T_HEXLEN +#define NXT_TIME_T_MAX NXT_INT32_T_MAX #endif diff --git a/src/nxt_unicode_lowcase.h b/src/nxt_unicode_lowcase.h index 2f25ab69e..e39c12f72 100644 --- a/src/nxt_unicode_lowcase.h +++ b/src/nxt_unicode_lowcase.h @@ -1,524 +1,3358 @@ - /* * 26 128-bytes blocks, 521 pointers. * 14920 bytes on 32-bit platforms, 17000 bytes on 64-bit platforms. */ -#define NXT_UNICODE_MAX_LOWCASE 0x10427 +#define NXT_UNICODE_MAX_LOWCASE 0x10427 -#define NXT_UNICODE_BLOCK_SIZE 128 +#define NXT_UNICODE_BLOCK_SIZE 128 -static const uint32_t nxt_unicode_block_000[128] nxt_aligned(64) = { - 0x00000, 0x00001, 0x00002, 0x00003, 0x00004, 0x00005, 0x00006, 0x00007, - 0x00008, 0x00009, 0x0000A, 0x0000B, 0x0000C, 0x0000D, 0x0000E, 0x0000F, - 0x00010, 0x00011, 0x00012, 0x00013, 0x00014, 0x00015, 0x00016, 0x00017, - 0x00018, 0x00019, 0x0001A, 0x0001B, 0x0001C, 0x0001D, 0x0001E, 0x0001F, - 0x00020, 0x00021, 0x00022, 0x00023, 0x00024, 0x00025, 0x00026, 0x00027, - 0x00028, 0x00029, 0x0002A, 0x0002B, 0x0002C, 0x0002D, 0x0002E, 0x0002F, - 0x00030, 0x00031, 0x00032, 0x00033, 0x00034, 0x00035, 0x00036, 0x00037, - 0x00038, 0x00039, 0x0003A, 0x0003B, 0x0003C, 0x0003D, 0x0003E, 0x0003F, - 0x00040, 0x00061, 0x00062, 0x00063, 0x00064, 0x00065, 0x00066, 0x00067, - 0x00068, 0x00069, 0x0006A, 0x0006B, 0x0006C, 0x0006D, 0x0006E, 0x0006F, - 0x00070, 0x00071, 0x00072, 0x00073, 0x00074, 0x00075, 0x00076, 0x00077, - 0x00078, 0x00079, 0x0007A, 0x0005B, 0x0005C, 0x0005D, 0x0005E, 0x0005F, - 0x00060, 0x00061, 0x00062, 0x00063, 0x00064, 0x00065, 0x00066, 0x00067, - 0x00068, 0x00069, 0x0006A, 0x0006B, 0x0006C, 0x0006D, 0x0006E, 0x0006F, - 0x00070, 0x00071, 0x00072, 0x00073, 0x00074, 0x00075, 0x00076, 0x00077, - 0x00078, 0x00079, 0x0007A, 0x0007B, 0x0007C, 0x0007D, 0x0007E, 0x0007F, +static const uint32_t nxt_unicode_block_000[128] nxt_aligned(64) = { + 0x00000, + 0x00001, + 0x00002, + 0x00003, + 0x00004, + 0x00005, + 0x00006, + 0x00007, + 0x00008, + 0x00009, + 0x0000A, + 0x0000B, + 0x0000C, + 0x0000D, + 0x0000E, + 0x0000F, + 0x00010, + 0x00011, + 0x00012, + 0x00013, + 0x00014, + 0x00015, + 0x00016, + 0x00017, + 0x00018, + 0x00019, + 0x0001A, + 0x0001B, + 0x0001C, + 0x0001D, + 0x0001E, + 0x0001F, + 0x00020, + 0x00021, + 0x00022, + 0x00023, + 0x00024, + 0x00025, + 0x00026, + 0x00027, + 0x00028, + 0x00029, + 0x0002A, + 0x0002B, + 0x0002C, + 0x0002D, + 0x0002E, + 0x0002F, + 0x00030, + 0x00031, + 0x00032, + 0x00033, + 0x00034, + 0x00035, + 0x00036, + 0x00037, + 0x00038, + 0x00039, + 0x0003A, + 0x0003B, + 0x0003C, + 0x0003D, + 0x0003E, + 0x0003F, + 0x00040, + 0x00061, + 0x00062, + 0x00063, + 0x00064, + 0x00065, + 0x00066, + 0x00067, + 0x00068, + 0x00069, + 0x0006A, + 0x0006B, + 0x0006C, + 0x0006D, + 0x0006E, + 0x0006F, + 0x00070, + 0x00071, + 0x00072, + 0x00073, + 0x00074, + 0x00075, + 0x00076, + 0x00077, + 0x00078, + 0x00079, + 0x0007A, + 0x0005B, + 0x0005C, + 0x0005D, + 0x0005E, + 0x0005F, + 0x00060, + 0x00061, + 0x00062, + 0x00063, + 0x00064, + 0x00065, + 0x00066, + 0x00067, + 0x00068, + 0x00069, + 0x0006A, + 0x0006B, + 0x0006C, + 0x0006D, + 0x0006E, + 0x0006F, + 0x00070, + 0x00071, + 0x00072, + 0x00073, + 0x00074, + 0x00075, + 0x00076, + 0x00077, + 0x00078, + 0x00079, + 0x0007A, + 0x0007B, + 0x0007C, + 0x0007D, + 0x0007E, + 0x0007F, }; -static const uint32_t nxt_unicode_block_001[128] nxt_aligned(64) = { - 0x00080, 0x00081, 0x00082, 0x00083, 0x00084, 0x00085, 0x00086, 0x00087, - 0x00088, 0x00089, 0x0008A, 0x0008B, 0x0008C, 0x0008D, 0x0008E, 0x0008F, - 0x00090, 0x00091, 0x00092, 0x00093, 0x00094, 0x00095, 0x00096, 0x00097, - 0x00098, 0x00099, 0x0009A, 0x0009B, 0x0009C, 0x0009D, 0x0009E, 0x0009F, - 0x000A0, 0x000A1, 0x000A2, 0x000A3, 0x000A4, 0x000A5, 0x000A6, 0x000A7, - 0x000A8, 0x000A9, 0x000AA, 0x000AB, 0x000AC, 0x000AD, 0x000AE, 0x000AF, - 0x000B0, 0x000B1, 0x000B2, 0x000B3, 0x000B4, 0x003BC, 0x000B6, 0x000B7, - 0x000B8, 0x000B9, 0x000BA, 0x000BB, 0x000BC, 0x000BD, 0x000BE, 0x000BF, - 0x000E0, 0x000E1, 0x000E2, 0x000E3, 0x000E4, 0x000E5, 0x000E6, 0x000E7, - 0x000E8, 0x000E9, 0x000EA, 0x000EB, 0x000EC, 0x000ED, 0x000EE, 0x000EF, - 0x000F0, 0x000F1, 0x000F2, 0x000F3, 0x000F4, 0x000F5, 0x000F6, 0x000D7, - 0x000F8, 0x000F9, 0x000FA, 0x000FB, 0x000FC, 0x000FD, 0x000FE, 0x000DF, - 0x000E0, 0x000E1, 0x000E2, 0x000E3, 0x000E4, 0x000E5, 0x000E6, 0x000E7, - 0x000E8, 0x000E9, 0x000EA, 0x000EB, 0x000EC, 0x000ED, 0x000EE, 0x000EF, - 0x000F0, 0x000F1, 0x000F2, 0x000F3, 0x000F4, 0x000F5, 0x000F6, 0x000F7, - 0x000F8, 0x000F9, 0x000FA, 0x000FB, 0x000FC, 0x000FD, 0x000FE, 0x000FF, +static const uint32_t nxt_unicode_block_001[128] nxt_aligned(64) = { + 0x00080, + 0x00081, + 0x00082, + 0x00083, + 0x00084, + 0x00085, + 0x00086, + 0x00087, + 0x00088, + 0x00089, + 0x0008A, + 0x0008B, + 0x0008C, + 0x0008D, + 0x0008E, + 0x0008F, + 0x00090, + 0x00091, + 0x00092, + 0x00093, + 0x00094, + 0x00095, + 0x00096, + 0x00097, + 0x00098, + 0x00099, + 0x0009A, + 0x0009B, + 0x0009C, + 0x0009D, + 0x0009E, + 0x0009F, + 0x000A0, + 0x000A1, + 0x000A2, + 0x000A3, + 0x000A4, + 0x000A5, + 0x000A6, + 0x000A7, + 0x000A8, + 0x000A9, + 0x000AA, + 0x000AB, + 0x000AC, + 0x000AD, + 0x000AE, + 0x000AF, + 0x000B0, + 0x000B1, + 0x000B2, + 0x000B3, + 0x000B4, + 0x003BC, + 0x000B6, + 0x000B7, + 0x000B8, + 0x000B9, + 0x000BA, + 0x000BB, + 0x000BC, + 0x000BD, + 0x000BE, + 0x000BF, + 0x000E0, + 0x000E1, + 0x000E2, + 0x000E3, + 0x000E4, + 0x000E5, + 0x000E6, + 0x000E7, + 0x000E8, + 0x000E9, + 0x000EA, + 0x000EB, + 0x000EC, + 0x000ED, + 0x000EE, + 0x000EF, + 0x000F0, + 0x000F1, + 0x000F2, + 0x000F3, + 0x000F4, + 0x000F5, + 0x000F6, + 0x000D7, + 0x000F8, + 0x000F9, + 0x000FA, + 0x000FB, + 0x000FC, + 0x000FD, + 0x000FE, + 0x000DF, + 0x000E0, + 0x000E1, + 0x000E2, + 0x000E3, + 0x000E4, + 0x000E5, + 0x000E6, + 0x000E7, + 0x000E8, + 0x000E9, + 0x000EA, + 0x000EB, + 0x000EC, + 0x000ED, + 0x000EE, + 0x000EF, + 0x000F0, + 0x000F1, + 0x000F2, + 0x000F3, + 0x000F4, + 0x000F5, + 0x000F6, + 0x000F7, + 0x000F8, + 0x000F9, + 0x000FA, + 0x000FB, + 0x000FC, + 0x000FD, + 0x000FE, + 0x000FF, }; -static const uint32_t nxt_unicode_block_002[128] nxt_aligned(64) = { - 0x00101, 0x00101, 0x00103, 0x00103, 0x00105, 0x00105, 0x00107, 0x00107, - 0x00109, 0x00109, 0x0010B, 0x0010B, 0x0010D, 0x0010D, 0x0010F, 0x0010F, - 0x00111, 0x00111, 0x00113, 0x00113, 0x00115, 0x00115, 0x00117, 0x00117, - 0x00119, 0x00119, 0x0011B, 0x0011B, 0x0011D, 0x0011D, 0x0011F, 0x0011F, - 0x00121, 0x00121, 0x00123, 0x00123, 0x00125, 0x00125, 0x00127, 0x00127, - 0x00129, 0x00129, 0x0012B, 0x0012B, 0x0012D, 0x0012D, 0x0012F, 0x0012F, - 0x00130, 0x00131, 0x00133, 0x00133, 0x00135, 0x00135, 0x00137, 0x00137, - 0x00138, 0x0013A, 0x0013A, 0x0013C, 0x0013C, 0x0013E, 0x0013E, 0x00140, - 0x00140, 0x00142, 0x00142, 0x00144, 0x00144, 0x00146, 0x00146, 0x00148, - 0x00148, 0x00149, 0x0014B, 0x0014B, 0x0014D, 0x0014D, 0x0014F, 0x0014F, - 0x00151, 0x00151, 0x00153, 0x00153, 0x00155, 0x00155, 0x00157, 0x00157, - 0x00159, 0x00159, 0x0015B, 0x0015B, 0x0015D, 0x0015D, 0x0015F, 0x0015F, - 0x00161, 0x00161, 0x00163, 0x00163, 0x00165, 0x00165, 0x00167, 0x00167, - 0x00169, 0x00169, 0x0016B, 0x0016B, 0x0016D, 0x0016D, 0x0016F, 0x0016F, - 0x00171, 0x00171, 0x00173, 0x00173, 0x00175, 0x00175, 0x00177, 0x00177, - 0x000FF, 0x0017A, 0x0017A, 0x0017C, 0x0017C, 0x0017E, 0x0017E, 0x00073, +static const uint32_t nxt_unicode_block_002[128] nxt_aligned(64) = { + 0x00101, + 0x00101, + 0x00103, + 0x00103, + 0x00105, + 0x00105, + 0x00107, + 0x00107, + 0x00109, + 0x00109, + 0x0010B, + 0x0010B, + 0x0010D, + 0x0010D, + 0x0010F, + 0x0010F, + 0x00111, + 0x00111, + 0x00113, + 0x00113, + 0x00115, + 0x00115, + 0x00117, + 0x00117, + 0x00119, + 0x00119, + 0x0011B, + 0x0011B, + 0x0011D, + 0x0011D, + 0x0011F, + 0x0011F, + 0x00121, + 0x00121, + 0x00123, + 0x00123, + 0x00125, + 0x00125, + 0x00127, + 0x00127, + 0x00129, + 0x00129, + 0x0012B, + 0x0012B, + 0x0012D, + 0x0012D, + 0x0012F, + 0x0012F, + 0x00130, + 0x00131, + 0x00133, + 0x00133, + 0x00135, + 0x00135, + 0x00137, + 0x00137, + 0x00138, + 0x0013A, + 0x0013A, + 0x0013C, + 0x0013C, + 0x0013E, + 0x0013E, + 0x00140, + 0x00140, + 0x00142, + 0x00142, + 0x00144, + 0x00144, + 0x00146, + 0x00146, + 0x00148, + 0x00148, + 0x00149, + 0x0014B, + 0x0014B, + 0x0014D, + 0x0014D, + 0x0014F, + 0x0014F, + 0x00151, + 0x00151, + 0x00153, + 0x00153, + 0x00155, + 0x00155, + 0x00157, + 0x00157, + 0x00159, + 0x00159, + 0x0015B, + 0x0015B, + 0x0015D, + 0x0015D, + 0x0015F, + 0x0015F, + 0x00161, + 0x00161, + 0x00163, + 0x00163, + 0x00165, + 0x00165, + 0x00167, + 0x00167, + 0x00169, + 0x00169, + 0x0016B, + 0x0016B, + 0x0016D, + 0x0016D, + 0x0016F, + 0x0016F, + 0x00171, + 0x00171, + 0x00173, + 0x00173, + 0x00175, + 0x00175, + 0x00177, + 0x00177, + 0x000FF, + 0x0017A, + 0x0017A, + 0x0017C, + 0x0017C, + 0x0017E, + 0x0017E, + 0x00073, }; -static const uint32_t nxt_unicode_block_003[128] nxt_aligned(64) = { - 0x00180, 0x00253, 0x00183, 0x00183, 0x00185, 0x00185, 0x00254, 0x00188, - 0x00188, 0x00256, 0x00257, 0x0018C, 0x0018C, 0x0018D, 0x001DD, 0x00259, - 0x0025B, 0x00192, 0x00192, 0x00260, 0x00263, 0x00195, 0x00269, 0x00268, - 0x00199, 0x00199, 0x0019A, 0x0019B, 0x0026F, 0x00272, 0x0019E, 0x00275, - 0x001A1, 0x001A1, 0x001A3, 0x001A3, 0x001A5, 0x001A5, 0x00280, 0x001A8, - 0x001A8, 0x00283, 0x001AA, 0x001AB, 0x001AD, 0x001AD, 0x00288, 0x001B0, - 0x001B0, 0x0028A, 0x0028B, 0x001B4, 0x001B4, 0x001B6, 0x001B6, 0x00292, - 0x001B9, 0x001B9, 0x001BA, 0x001BB, 0x001BD, 0x001BD, 0x001BE, 0x001BF, - 0x001C0, 0x001C1, 0x001C2, 0x001C3, 0x001C6, 0x001C6, 0x001C6, 0x001C9, - 0x001C9, 0x001C9, 0x001CC, 0x001CC, 0x001CC, 0x001CE, 0x001CE, 0x001D0, - 0x001D0, 0x001D2, 0x001D2, 0x001D4, 0x001D4, 0x001D6, 0x001D6, 0x001D8, - 0x001D8, 0x001DA, 0x001DA, 0x001DC, 0x001DC, 0x001DD, 0x001DF, 0x001DF, - 0x001E1, 0x001E1, 0x001E3, 0x001E3, 0x001E5, 0x001E5, 0x001E7, 0x001E7, - 0x001E9, 0x001E9, 0x001EB, 0x001EB, 0x001ED, 0x001ED, 0x001EF, 0x001EF, - 0x001F0, 0x001F3, 0x001F3, 0x001F3, 0x001F5, 0x001F5, 0x00195, 0x001BF, - 0x001F9, 0x001F9, 0x001FB, 0x001FB, 0x001FD, 0x001FD, 0x001FF, 0x001FF, +static const uint32_t nxt_unicode_block_003[128] nxt_aligned(64) = { + 0x00180, + 0x00253, + 0x00183, + 0x00183, + 0x00185, + 0x00185, + 0x00254, + 0x00188, + 0x00188, + 0x00256, + 0x00257, + 0x0018C, + 0x0018C, + 0x0018D, + 0x001DD, + 0x00259, + 0x0025B, + 0x00192, + 0x00192, + 0x00260, + 0x00263, + 0x00195, + 0x00269, + 0x00268, + 0x00199, + 0x00199, + 0x0019A, + 0x0019B, + 0x0026F, + 0x00272, + 0x0019E, + 0x00275, + 0x001A1, + 0x001A1, + 0x001A3, + 0x001A3, + 0x001A5, + 0x001A5, + 0x00280, + 0x001A8, + 0x001A8, + 0x00283, + 0x001AA, + 0x001AB, + 0x001AD, + 0x001AD, + 0x00288, + 0x001B0, + 0x001B0, + 0x0028A, + 0x0028B, + 0x001B4, + 0x001B4, + 0x001B6, + 0x001B6, + 0x00292, + 0x001B9, + 0x001B9, + 0x001BA, + 0x001BB, + 0x001BD, + 0x001BD, + 0x001BE, + 0x001BF, + 0x001C0, + 0x001C1, + 0x001C2, + 0x001C3, + 0x001C6, + 0x001C6, + 0x001C6, + 0x001C9, + 0x001C9, + 0x001C9, + 0x001CC, + 0x001CC, + 0x001CC, + 0x001CE, + 0x001CE, + 0x001D0, + 0x001D0, + 0x001D2, + 0x001D2, + 0x001D4, + 0x001D4, + 0x001D6, + 0x001D6, + 0x001D8, + 0x001D8, + 0x001DA, + 0x001DA, + 0x001DC, + 0x001DC, + 0x001DD, + 0x001DF, + 0x001DF, + 0x001E1, + 0x001E1, + 0x001E3, + 0x001E3, + 0x001E5, + 0x001E5, + 0x001E7, + 0x001E7, + 0x001E9, + 0x001E9, + 0x001EB, + 0x001EB, + 0x001ED, + 0x001ED, + 0x001EF, + 0x001EF, + 0x001F0, + 0x001F3, + 0x001F3, + 0x001F3, + 0x001F5, + 0x001F5, + 0x00195, + 0x001BF, + 0x001F9, + 0x001F9, + 0x001FB, + 0x001FB, + 0x001FD, + 0x001FD, + 0x001FF, + 0x001FF, }; -static const uint32_t nxt_unicode_block_004[128] nxt_aligned(64) = { - 0x00201, 0x00201, 0x00203, 0x00203, 0x00205, 0x00205, 0x00207, 0x00207, - 0x00209, 0x00209, 0x0020B, 0x0020B, 0x0020D, 0x0020D, 0x0020F, 0x0020F, - 0x00211, 0x00211, 0x00213, 0x00213, 0x00215, 0x00215, 0x00217, 0x00217, - 0x00219, 0x00219, 0x0021B, 0x0021B, 0x0021D, 0x0021D, 0x0021F, 0x0021F, - 0x0019E, 0x00221, 0x00223, 0x00223, 0x00225, 0x00225, 0x00227, 0x00227, - 0x00229, 0x00229, 0x0022B, 0x0022B, 0x0022D, 0x0022D, 0x0022F, 0x0022F, - 0x00231, 0x00231, 0x00233, 0x00233, 0x00234, 0x00235, 0x00236, 0x00237, - 0x00238, 0x00239, 0x02C65, 0x0023C, 0x0023C, 0x0019A, 0x02C66, 0x0023F, - 0x00240, 0x00242, 0x00242, 0x00180, 0x00289, 0x0028C, 0x00247, 0x00247, - 0x00249, 0x00249, 0x0024B, 0x0024B, 0x0024D, 0x0024D, 0x0024F, 0x0024F, - 0x00250, 0x00251, 0x00252, 0x00253, 0x00254, 0x00255, 0x00256, 0x00257, - 0x00258, 0x00259, 0x0025A, 0x0025B, 0x0025C, 0x0025D, 0x0025E, 0x0025F, - 0x00260, 0x00261, 0x00262, 0x00263, 0x00264, 0x00265, 0x00266, 0x00267, - 0x00268, 0x00269, 0x0026A, 0x0026B, 0x0026C, 0x0026D, 0x0026E, 0x0026F, - 0x00270, 0x00271, 0x00272, 0x00273, 0x00274, 0x00275, 0x00276, 0x00277, - 0x00278, 0x00279, 0x0027A, 0x0027B, 0x0027C, 0x0027D, 0x0027E, 0x0027F, +static const uint32_t nxt_unicode_block_004[128] nxt_aligned(64) = { + 0x00201, + 0x00201, + 0x00203, + 0x00203, + 0x00205, + 0x00205, + 0x00207, + 0x00207, + 0x00209, + 0x00209, + 0x0020B, + 0x0020B, + 0x0020D, + 0x0020D, + 0x0020F, + 0x0020F, + 0x00211, + 0x00211, + 0x00213, + 0x00213, + 0x00215, + 0x00215, + 0x00217, + 0x00217, + 0x00219, + 0x00219, + 0x0021B, + 0x0021B, + 0x0021D, + 0x0021D, + 0x0021F, + 0x0021F, + 0x0019E, + 0x00221, + 0x00223, + 0x00223, + 0x00225, + 0x00225, + 0x00227, + 0x00227, + 0x00229, + 0x00229, + 0x0022B, + 0x0022B, + 0x0022D, + 0x0022D, + 0x0022F, + 0x0022F, + 0x00231, + 0x00231, + 0x00233, + 0x00233, + 0x00234, + 0x00235, + 0x00236, + 0x00237, + 0x00238, + 0x00239, + 0x02C65, + 0x0023C, + 0x0023C, + 0x0019A, + 0x02C66, + 0x0023F, + 0x00240, + 0x00242, + 0x00242, + 0x00180, + 0x00289, + 0x0028C, + 0x00247, + 0x00247, + 0x00249, + 0x00249, + 0x0024B, + 0x0024B, + 0x0024D, + 0x0024D, + 0x0024F, + 0x0024F, + 0x00250, + 0x00251, + 0x00252, + 0x00253, + 0x00254, + 0x00255, + 0x00256, + 0x00257, + 0x00258, + 0x00259, + 0x0025A, + 0x0025B, + 0x0025C, + 0x0025D, + 0x0025E, + 0x0025F, + 0x00260, + 0x00261, + 0x00262, + 0x00263, + 0x00264, + 0x00265, + 0x00266, + 0x00267, + 0x00268, + 0x00269, + 0x0026A, + 0x0026B, + 0x0026C, + 0x0026D, + 0x0026E, + 0x0026F, + 0x00270, + 0x00271, + 0x00272, + 0x00273, + 0x00274, + 0x00275, + 0x00276, + 0x00277, + 0x00278, + 0x00279, + 0x0027A, + 0x0027B, + 0x0027C, + 0x0027D, + 0x0027E, + 0x0027F, }; -static const uint32_t nxt_unicode_block_006[128] nxt_aligned(64) = { - 0x00300, 0x00301, 0x00302, 0x00303, 0x00304, 0x00305, 0x00306, 0x00307, - 0x00308, 0x00309, 0x0030A, 0x0030B, 0x0030C, 0x0030D, 0x0030E, 0x0030F, - 0x00310, 0x00311, 0x00312, 0x00313, 0x00314, 0x00315, 0x00316, 0x00317, - 0x00318, 0x00319, 0x0031A, 0x0031B, 0x0031C, 0x0031D, 0x0031E, 0x0031F, - 0x00320, 0x00321, 0x00322, 0x00323, 0x00324, 0x00325, 0x00326, 0x00327, - 0x00328, 0x00329, 0x0032A, 0x0032B, 0x0032C, 0x0032D, 0x0032E, 0x0032F, - 0x00330, 0x00331, 0x00332, 0x00333, 0x00334, 0x00335, 0x00336, 0x00337, - 0x00338, 0x00339, 0x0033A, 0x0033B, 0x0033C, 0x0033D, 0x0033E, 0x0033F, - 0x00340, 0x00341, 0x00342, 0x00343, 0x00344, 0x003B9, 0x00346, 0x00347, - 0x00348, 0x00349, 0x0034A, 0x0034B, 0x0034C, 0x0034D, 0x0034E, 0x0034F, - 0x00350, 0x00351, 0x00352, 0x00353, 0x00354, 0x00355, 0x00356, 0x00357, - 0x00358, 0x00359, 0x0035A, 0x0035B, 0x0035C, 0x0035D, 0x0035E, 0x0035F, - 0x00360, 0x00361, 0x00362, 0x00363, 0x00364, 0x00365, 0x00366, 0x00367, - 0x00368, 0x00369, 0x0036A, 0x0036B, 0x0036C, 0x0036D, 0x0036E, 0x0036F, - 0x00371, 0x00371, 0x00373, 0x00373, 0x00374, 0x00375, 0x00377, 0x00377, - 0x00378, 0x00379, 0x0037A, 0x0037B, 0x0037C, 0x0037D, 0x0037E, 0x0037F, +static const uint32_t nxt_unicode_block_006[128] nxt_aligned(64) = { + 0x00300, + 0x00301, + 0x00302, + 0x00303, + 0x00304, + 0x00305, + 0x00306, + 0x00307, + 0x00308, + 0x00309, + 0x0030A, + 0x0030B, + 0x0030C, + 0x0030D, + 0x0030E, + 0x0030F, + 0x00310, + 0x00311, + 0x00312, + 0x00313, + 0x00314, + 0x00315, + 0x00316, + 0x00317, + 0x00318, + 0x00319, + 0x0031A, + 0x0031B, + 0x0031C, + 0x0031D, + 0x0031E, + 0x0031F, + 0x00320, + 0x00321, + 0x00322, + 0x00323, + 0x00324, + 0x00325, + 0x00326, + 0x00327, + 0x00328, + 0x00329, + 0x0032A, + 0x0032B, + 0x0032C, + 0x0032D, + 0x0032E, + 0x0032F, + 0x00330, + 0x00331, + 0x00332, + 0x00333, + 0x00334, + 0x00335, + 0x00336, + 0x00337, + 0x00338, + 0x00339, + 0x0033A, + 0x0033B, + 0x0033C, + 0x0033D, + 0x0033E, + 0x0033F, + 0x00340, + 0x00341, + 0x00342, + 0x00343, + 0x00344, + 0x003B9, + 0x00346, + 0x00347, + 0x00348, + 0x00349, + 0x0034A, + 0x0034B, + 0x0034C, + 0x0034D, + 0x0034E, + 0x0034F, + 0x00350, + 0x00351, + 0x00352, + 0x00353, + 0x00354, + 0x00355, + 0x00356, + 0x00357, + 0x00358, + 0x00359, + 0x0035A, + 0x0035B, + 0x0035C, + 0x0035D, + 0x0035E, + 0x0035F, + 0x00360, + 0x00361, + 0x00362, + 0x00363, + 0x00364, + 0x00365, + 0x00366, + 0x00367, + 0x00368, + 0x00369, + 0x0036A, + 0x0036B, + 0x0036C, + 0x0036D, + 0x0036E, + 0x0036F, + 0x00371, + 0x00371, + 0x00373, + 0x00373, + 0x00374, + 0x00375, + 0x00377, + 0x00377, + 0x00378, + 0x00379, + 0x0037A, + 0x0037B, + 0x0037C, + 0x0037D, + 0x0037E, + 0x0037F, }; -static const uint32_t nxt_unicode_block_007[128] nxt_aligned(64) = { - 0x00380, 0x00381, 0x00382, 0x00383, 0x00384, 0x00385, 0x003AC, 0x00387, - 0x003AD, 0x003AE, 0x003AF, 0x0038B, 0x003CC, 0x0038D, 0x003CD, 0x003CE, - 0x00390, 0x003B1, 0x003B2, 0x003B3, 0x003B4, 0x003B5, 0x003B6, 0x003B7, - 0x003B8, 0x003B9, 0x003BA, 0x003BB, 0x003BC, 0x003BD, 0x003BE, 0x003BF, - 0x003C0, 0x003C1, 0x003A2, 0x003C3, 0x003C4, 0x003C5, 0x003C6, 0x003C7, - 0x003C8, 0x003C9, 0x003CA, 0x003CB, 0x003AC, 0x003AD, 0x003AE, 0x003AF, - 0x003B0, 0x003B1, 0x003B2, 0x003B3, 0x003B4, 0x003B5, 0x003B6, 0x003B7, - 0x003B8, 0x003B9, 0x003BA, 0x003BB, 0x003BC, 0x003BD, 0x003BE, 0x003BF, - 0x003C0, 0x003C1, 0x003C3, 0x003C3, 0x003C4, 0x003C5, 0x003C6, 0x003C7, - 0x003C8, 0x003C9, 0x003CA, 0x003CB, 0x003CC, 0x003CD, 0x003CE, 0x003D7, - 0x003B2, 0x003B8, 0x003D2, 0x003D3, 0x003D4, 0x003C6, 0x003C0, 0x003D7, - 0x003D9, 0x003D9, 0x003DB, 0x003DB, 0x003DD, 0x003DD, 0x003DF, 0x003DF, - 0x003E1, 0x003E1, 0x003E3, 0x003E3, 0x003E5, 0x003E5, 0x003E7, 0x003E7, - 0x003E9, 0x003E9, 0x003EB, 0x003EB, 0x003ED, 0x003ED, 0x003EF, 0x003EF, - 0x003BA, 0x003C1, 0x003F2, 0x003F3, 0x003B8, 0x003B5, 0x003F6, 0x003F8, - 0x003F8, 0x003F2, 0x003FB, 0x003FB, 0x003FC, 0x0037B, 0x0037C, 0x0037D, +static const uint32_t nxt_unicode_block_007[128] nxt_aligned(64) = { + 0x00380, + 0x00381, + 0x00382, + 0x00383, + 0x00384, + 0x00385, + 0x003AC, + 0x00387, + 0x003AD, + 0x003AE, + 0x003AF, + 0x0038B, + 0x003CC, + 0x0038D, + 0x003CD, + 0x003CE, + 0x00390, + 0x003B1, + 0x003B2, + 0x003B3, + 0x003B4, + 0x003B5, + 0x003B6, + 0x003B7, + 0x003B8, + 0x003B9, + 0x003BA, + 0x003BB, + 0x003BC, + 0x003BD, + 0x003BE, + 0x003BF, + 0x003C0, + 0x003C1, + 0x003A2, + 0x003C3, + 0x003C4, + 0x003C5, + 0x003C6, + 0x003C7, + 0x003C8, + 0x003C9, + 0x003CA, + 0x003CB, + 0x003AC, + 0x003AD, + 0x003AE, + 0x003AF, + 0x003B0, + 0x003B1, + 0x003B2, + 0x003B3, + 0x003B4, + 0x003B5, + 0x003B6, + 0x003B7, + 0x003B8, + 0x003B9, + 0x003BA, + 0x003BB, + 0x003BC, + 0x003BD, + 0x003BE, + 0x003BF, + 0x003C0, + 0x003C1, + 0x003C3, + 0x003C3, + 0x003C4, + 0x003C5, + 0x003C6, + 0x003C7, + 0x003C8, + 0x003C9, + 0x003CA, + 0x003CB, + 0x003CC, + 0x003CD, + 0x003CE, + 0x003D7, + 0x003B2, + 0x003B8, + 0x003D2, + 0x003D3, + 0x003D4, + 0x003C6, + 0x003C0, + 0x003D7, + 0x003D9, + 0x003D9, + 0x003DB, + 0x003DB, + 0x003DD, + 0x003DD, + 0x003DF, + 0x003DF, + 0x003E1, + 0x003E1, + 0x003E3, + 0x003E3, + 0x003E5, + 0x003E5, + 0x003E7, + 0x003E7, + 0x003E9, + 0x003E9, + 0x003EB, + 0x003EB, + 0x003ED, + 0x003ED, + 0x003EF, + 0x003EF, + 0x003BA, + 0x003C1, + 0x003F2, + 0x003F3, + 0x003B8, + 0x003B5, + 0x003F6, + 0x003F8, + 0x003F8, + 0x003F2, + 0x003FB, + 0x003FB, + 0x003FC, + 0x0037B, + 0x0037C, + 0x0037D, }; -static const uint32_t nxt_unicode_block_008[128] nxt_aligned(64) = { - 0x00450, 0x00451, 0x00452, 0x00453, 0x00454, 0x00455, 0x00456, 0x00457, - 0x00458, 0x00459, 0x0045A, 0x0045B, 0x0045C, 0x0045D, 0x0045E, 0x0045F, - 0x00430, 0x00431, 0x00432, 0x00433, 0x00434, 0x00435, 0x00436, 0x00437, - 0x00438, 0x00439, 0x0043A, 0x0043B, 0x0043C, 0x0043D, 0x0043E, 0x0043F, - 0x00440, 0x00441, 0x00442, 0x00443, 0x00444, 0x00445, 0x00446, 0x00447, - 0x00448, 0x00449, 0x0044A, 0x0044B, 0x0044C, 0x0044D, 0x0044E, 0x0044F, - 0x00430, 0x00431, 0x00432, 0x00433, 0x00434, 0x00435, 0x00436, 0x00437, - 0x00438, 0x00439, 0x0043A, 0x0043B, 0x0043C, 0x0043D, 0x0043E, 0x0043F, - 0x00440, 0x00441, 0x00442, 0x00443, 0x00444, 0x00445, 0x00446, 0x00447, - 0x00448, 0x00449, 0x0044A, 0x0044B, 0x0044C, 0x0044D, 0x0044E, 0x0044F, - 0x00450, 0x00451, 0x00452, 0x00453, 0x00454, 0x00455, 0x00456, 0x00457, - 0x00458, 0x00459, 0x0045A, 0x0045B, 0x0045C, 0x0045D, 0x0045E, 0x0045F, - 0x00461, 0x00461, 0x00463, 0x00463, 0x00465, 0x00465, 0x00467, 0x00467, - 0x00469, 0x00469, 0x0046B, 0x0046B, 0x0046D, 0x0046D, 0x0046F, 0x0046F, - 0x00471, 0x00471, 0x00473, 0x00473, 0x00475, 0x00475, 0x00477, 0x00477, - 0x00479, 0x00479, 0x0047B, 0x0047B, 0x0047D, 0x0047D, 0x0047F, 0x0047F, +static const uint32_t nxt_unicode_block_008[128] nxt_aligned(64) = { + 0x00450, + 0x00451, + 0x00452, + 0x00453, + 0x00454, + 0x00455, + 0x00456, + 0x00457, + 0x00458, + 0x00459, + 0x0045A, + 0x0045B, + 0x0045C, + 0x0045D, + 0x0045E, + 0x0045F, + 0x00430, + 0x00431, + 0x00432, + 0x00433, + 0x00434, + 0x00435, + 0x00436, + 0x00437, + 0x00438, + 0x00439, + 0x0043A, + 0x0043B, + 0x0043C, + 0x0043D, + 0x0043E, + 0x0043F, + 0x00440, + 0x00441, + 0x00442, + 0x00443, + 0x00444, + 0x00445, + 0x00446, + 0x00447, + 0x00448, + 0x00449, + 0x0044A, + 0x0044B, + 0x0044C, + 0x0044D, + 0x0044E, + 0x0044F, + 0x00430, + 0x00431, + 0x00432, + 0x00433, + 0x00434, + 0x00435, + 0x00436, + 0x00437, + 0x00438, + 0x00439, + 0x0043A, + 0x0043B, + 0x0043C, + 0x0043D, + 0x0043E, + 0x0043F, + 0x00440, + 0x00441, + 0x00442, + 0x00443, + 0x00444, + 0x00445, + 0x00446, + 0x00447, + 0x00448, + 0x00449, + 0x0044A, + 0x0044B, + 0x0044C, + 0x0044D, + 0x0044E, + 0x0044F, + 0x00450, + 0x00451, + 0x00452, + 0x00453, + 0x00454, + 0x00455, + 0x00456, + 0x00457, + 0x00458, + 0x00459, + 0x0045A, + 0x0045B, + 0x0045C, + 0x0045D, + 0x0045E, + 0x0045F, + 0x00461, + 0x00461, + 0x00463, + 0x00463, + 0x00465, + 0x00465, + 0x00467, + 0x00467, + 0x00469, + 0x00469, + 0x0046B, + 0x0046B, + 0x0046D, + 0x0046D, + 0x0046F, + 0x0046F, + 0x00471, + 0x00471, + 0x00473, + 0x00473, + 0x00475, + 0x00475, + 0x00477, + 0x00477, + 0x00479, + 0x00479, + 0x0047B, + 0x0047B, + 0x0047D, + 0x0047D, + 0x0047F, + 0x0047F, }; -static const uint32_t nxt_unicode_block_009[128] nxt_aligned(64) = { - 0x00481, 0x00481, 0x00482, 0x00483, 0x00484, 0x00485, 0x00486, 0x00487, - 0x00488, 0x00489, 0x0048B, 0x0048B, 0x0048D, 0x0048D, 0x0048F, 0x0048F, - 0x00491, 0x00491, 0x00493, 0x00493, 0x00495, 0x00495, 0x00497, 0x00497, - 0x00499, 0x00499, 0x0049B, 0x0049B, 0x0049D, 0x0049D, 0x0049F, 0x0049F, - 0x004A1, 0x004A1, 0x004A3, 0x004A3, 0x004A5, 0x004A5, 0x004A7, 0x004A7, - 0x004A9, 0x004A9, 0x004AB, 0x004AB, 0x004AD, 0x004AD, 0x004AF, 0x004AF, - 0x004B1, 0x004B1, 0x004B3, 0x004B3, 0x004B5, 0x004B5, 0x004B7, 0x004B7, - 0x004B9, 0x004B9, 0x004BB, 0x004BB, 0x004BD, 0x004BD, 0x004BF, 0x004BF, - 0x004CF, 0x004C2, 0x004C2, 0x004C4, 0x004C4, 0x004C6, 0x004C6, 0x004C8, - 0x004C8, 0x004CA, 0x004CA, 0x004CC, 0x004CC, 0x004CE, 0x004CE, 0x004CF, - 0x004D1, 0x004D1, 0x004D3, 0x004D3, 0x004D5, 0x004D5, 0x004D7, 0x004D7, - 0x004D9, 0x004D9, 0x004DB, 0x004DB, 0x004DD, 0x004DD, 0x004DF, 0x004DF, - 0x004E1, 0x004E1, 0x004E3, 0x004E3, 0x004E5, 0x004E5, 0x004E7, 0x004E7, - 0x004E9, 0x004E9, 0x004EB, 0x004EB, 0x004ED, 0x004ED, 0x004EF, 0x004EF, - 0x004F1, 0x004F1, 0x004F3, 0x004F3, 0x004F5, 0x004F5, 0x004F7, 0x004F7, - 0x004F9, 0x004F9, 0x004FB, 0x004FB, 0x004FD, 0x004FD, 0x004FF, 0x004FF, +static const uint32_t nxt_unicode_block_009[128] nxt_aligned(64) = { + 0x00481, + 0x00481, + 0x00482, + 0x00483, + 0x00484, + 0x00485, + 0x00486, + 0x00487, + 0x00488, + 0x00489, + 0x0048B, + 0x0048B, + 0x0048D, + 0x0048D, + 0x0048F, + 0x0048F, + 0x00491, + 0x00491, + 0x00493, + 0x00493, + 0x00495, + 0x00495, + 0x00497, + 0x00497, + 0x00499, + 0x00499, + 0x0049B, + 0x0049B, + 0x0049D, + 0x0049D, + 0x0049F, + 0x0049F, + 0x004A1, + 0x004A1, + 0x004A3, + 0x004A3, + 0x004A5, + 0x004A5, + 0x004A7, + 0x004A7, + 0x004A9, + 0x004A9, + 0x004AB, + 0x004AB, + 0x004AD, + 0x004AD, + 0x004AF, + 0x004AF, + 0x004B1, + 0x004B1, + 0x004B3, + 0x004B3, + 0x004B5, + 0x004B5, + 0x004B7, + 0x004B7, + 0x004B9, + 0x004B9, + 0x004BB, + 0x004BB, + 0x004BD, + 0x004BD, + 0x004BF, + 0x004BF, + 0x004CF, + 0x004C2, + 0x004C2, + 0x004C4, + 0x004C4, + 0x004C6, + 0x004C6, + 0x004C8, + 0x004C8, + 0x004CA, + 0x004CA, + 0x004CC, + 0x004CC, + 0x004CE, + 0x004CE, + 0x004CF, + 0x004D1, + 0x004D1, + 0x004D3, + 0x004D3, + 0x004D5, + 0x004D5, + 0x004D7, + 0x004D7, + 0x004D9, + 0x004D9, + 0x004DB, + 0x004DB, + 0x004DD, + 0x004DD, + 0x004DF, + 0x004DF, + 0x004E1, + 0x004E1, + 0x004E3, + 0x004E3, + 0x004E5, + 0x004E5, + 0x004E7, + 0x004E7, + 0x004E9, + 0x004E9, + 0x004EB, + 0x004EB, + 0x004ED, + 0x004ED, + 0x004EF, + 0x004EF, + 0x004F1, + 0x004F1, + 0x004F3, + 0x004F3, + 0x004F5, + 0x004F5, + 0x004F7, + 0x004F7, + 0x004F9, + 0x004F9, + 0x004FB, + 0x004FB, + 0x004FD, + 0x004FD, + 0x004FF, + 0x004FF, }; -static const uint32_t nxt_unicode_block_00a[128] nxt_aligned(64) = { - 0x00501, 0x00501, 0x00503, 0x00503, 0x00505, 0x00505, 0x00507, 0x00507, - 0x00509, 0x00509, 0x0050B, 0x0050B, 0x0050D, 0x0050D, 0x0050F, 0x0050F, - 0x00511, 0x00511, 0x00513, 0x00513, 0x00515, 0x00515, 0x00517, 0x00517, - 0x00519, 0x00519, 0x0051B, 0x0051B, 0x0051D, 0x0051D, 0x0051F, 0x0051F, - 0x00521, 0x00521, 0x00523, 0x00523, 0x00525, 0x00525, 0x00527, 0x00527, - 0x00528, 0x00529, 0x0052A, 0x0052B, 0x0052C, 0x0052D, 0x0052E, 0x0052F, - 0x00530, 0x00561, 0x00562, 0x00563, 0x00564, 0x00565, 0x00566, 0x00567, - 0x00568, 0x00569, 0x0056A, 0x0056B, 0x0056C, 0x0056D, 0x0056E, 0x0056F, - 0x00570, 0x00571, 0x00572, 0x00573, 0x00574, 0x00575, 0x00576, 0x00577, - 0x00578, 0x00579, 0x0057A, 0x0057B, 0x0057C, 0x0057D, 0x0057E, 0x0057F, - 0x00580, 0x00581, 0x00582, 0x00583, 0x00584, 0x00585, 0x00586, 0x00557, - 0x00558, 0x00559, 0x0055A, 0x0055B, 0x0055C, 0x0055D, 0x0055E, 0x0055F, - 0x00560, 0x00561, 0x00562, 0x00563, 0x00564, 0x00565, 0x00566, 0x00567, - 0x00568, 0x00569, 0x0056A, 0x0056B, 0x0056C, 0x0056D, 0x0056E, 0x0056F, - 0x00570, 0x00571, 0x00572, 0x00573, 0x00574, 0x00575, 0x00576, 0x00577, - 0x00578, 0x00579, 0x0057A, 0x0057B, 0x0057C, 0x0057D, 0x0057E, 0x0057F, +static const uint32_t nxt_unicode_block_00a[128] nxt_aligned(64) = { + 0x00501, + 0x00501, + 0x00503, + 0x00503, + 0x00505, + 0x00505, + 0x00507, + 0x00507, + 0x00509, + 0x00509, + 0x0050B, + 0x0050B, + 0x0050D, + 0x0050D, + 0x0050F, + 0x0050F, + 0x00511, + 0x00511, + 0x00513, + 0x00513, + 0x00515, + 0x00515, + 0x00517, + 0x00517, + 0x00519, + 0x00519, + 0x0051B, + 0x0051B, + 0x0051D, + 0x0051D, + 0x0051F, + 0x0051F, + 0x00521, + 0x00521, + 0x00523, + 0x00523, + 0x00525, + 0x00525, + 0x00527, + 0x00527, + 0x00528, + 0x00529, + 0x0052A, + 0x0052B, + 0x0052C, + 0x0052D, + 0x0052E, + 0x0052F, + 0x00530, + 0x00561, + 0x00562, + 0x00563, + 0x00564, + 0x00565, + 0x00566, + 0x00567, + 0x00568, + 0x00569, + 0x0056A, + 0x0056B, + 0x0056C, + 0x0056D, + 0x0056E, + 0x0056F, + 0x00570, + 0x00571, + 0x00572, + 0x00573, + 0x00574, + 0x00575, + 0x00576, + 0x00577, + 0x00578, + 0x00579, + 0x0057A, + 0x0057B, + 0x0057C, + 0x0057D, + 0x0057E, + 0x0057F, + 0x00580, + 0x00581, + 0x00582, + 0x00583, + 0x00584, + 0x00585, + 0x00586, + 0x00557, + 0x00558, + 0x00559, + 0x0055A, + 0x0055B, + 0x0055C, + 0x0055D, + 0x0055E, + 0x0055F, + 0x00560, + 0x00561, + 0x00562, + 0x00563, + 0x00564, + 0x00565, + 0x00566, + 0x00567, + 0x00568, + 0x00569, + 0x0056A, + 0x0056B, + 0x0056C, + 0x0056D, + 0x0056E, + 0x0056F, + 0x00570, + 0x00571, + 0x00572, + 0x00573, + 0x00574, + 0x00575, + 0x00576, + 0x00577, + 0x00578, + 0x00579, + 0x0057A, + 0x0057B, + 0x0057C, + 0x0057D, + 0x0057E, + 0x0057F, }; -static const uint32_t nxt_unicode_block_021[128] nxt_aligned(64) = { - 0x01080, 0x01081, 0x01082, 0x01083, 0x01084, 0x01085, 0x01086, 0x01087, - 0x01088, 0x01089, 0x0108A, 0x0108B, 0x0108C, 0x0108D, 0x0108E, 0x0108F, - 0x01090, 0x01091, 0x01092, 0x01093, 0x01094, 0x01095, 0x01096, 0x01097, - 0x01098, 0x01099, 0x0109A, 0x0109B, 0x0109C, 0x0109D, 0x0109E, 0x0109F, - 0x02D00, 0x02D01, 0x02D02, 0x02D03, 0x02D04, 0x02D05, 0x02D06, 0x02D07, - 0x02D08, 0x02D09, 0x02D0A, 0x02D0B, 0x02D0C, 0x02D0D, 0x02D0E, 0x02D0F, - 0x02D10, 0x02D11, 0x02D12, 0x02D13, 0x02D14, 0x02D15, 0x02D16, 0x02D17, - 0x02D18, 0x02D19, 0x02D1A, 0x02D1B, 0x02D1C, 0x02D1D, 0x02D1E, 0x02D1F, - 0x02D20, 0x02D21, 0x02D22, 0x02D23, 0x02D24, 0x02D25, 0x010C6, 0x02D27, - 0x010C8, 0x010C9, 0x010CA, 0x010CB, 0x010CC, 0x02D2D, 0x010CE, 0x010CF, - 0x010D0, 0x010D1, 0x010D2, 0x010D3, 0x010D4, 0x010D5, 0x010D6, 0x010D7, - 0x010D8, 0x010D9, 0x010DA, 0x010DB, 0x010DC, 0x010DD, 0x010DE, 0x010DF, - 0x010E0, 0x010E1, 0x010E2, 0x010E3, 0x010E4, 0x010E5, 0x010E6, 0x010E7, - 0x010E8, 0x010E9, 0x010EA, 0x010EB, 0x010EC, 0x010ED, 0x010EE, 0x010EF, - 0x010F0, 0x010F1, 0x010F2, 0x010F3, 0x010F4, 0x010F5, 0x010F6, 0x010F7, - 0x010F8, 0x010F9, 0x010FA, 0x010FB, 0x010FC, 0x010FD, 0x010FE, 0x010FF, +static const uint32_t nxt_unicode_block_021[128] nxt_aligned(64) = { + 0x01080, + 0x01081, + 0x01082, + 0x01083, + 0x01084, + 0x01085, + 0x01086, + 0x01087, + 0x01088, + 0x01089, + 0x0108A, + 0x0108B, + 0x0108C, + 0x0108D, + 0x0108E, + 0x0108F, + 0x01090, + 0x01091, + 0x01092, + 0x01093, + 0x01094, + 0x01095, + 0x01096, + 0x01097, + 0x01098, + 0x01099, + 0x0109A, + 0x0109B, + 0x0109C, + 0x0109D, + 0x0109E, + 0x0109F, + 0x02D00, + 0x02D01, + 0x02D02, + 0x02D03, + 0x02D04, + 0x02D05, + 0x02D06, + 0x02D07, + 0x02D08, + 0x02D09, + 0x02D0A, + 0x02D0B, + 0x02D0C, + 0x02D0D, + 0x02D0E, + 0x02D0F, + 0x02D10, + 0x02D11, + 0x02D12, + 0x02D13, + 0x02D14, + 0x02D15, + 0x02D16, + 0x02D17, + 0x02D18, + 0x02D19, + 0x02D1A, + 0x02D1B, + 0x02D1C, + 0x02D1D, + 0x02D1E, + 0x02D1F, + 0x02D20, + 0x02D21, + 0x02D22, + 0x02D23, + 0x02D24, + 0x02D25, + 0x010C6, + 0x02D27, + 0x010C8, + 0x010C9, + 0x010CA, + 0x010CB, + 0x010CC, + 0x02D2D, + 0x010CE, + 0x010CF, + 0x010D0, + 0x010D1, + 0x010D2, + 0x010D3, + 0x010D4, + 0x010D5, + 0x010D6, + 0x010D7, + 0x010D8, + 0x010D9, + 0x010DA, + 0x010DB, + 0x010DC, + 0x010DD, + 0x010DE, + 0x010DF, + 0x010E0, + 0x010E1, + 0x010E2, + 0x010E3, + 0x010E4, + 0x010E5, + 0x010E6, + 0x010E7, + 0x010E8, + 0x010E9, + 0x010EA, + 0x010EB, + 0x010EC, + 0x010ED, + 0x010EE, + 0x010EF, + 0x010F0, + 0x010F1, + 0x010F2, + 0x010F3, + 0x010F4, + 0x010F5, + 0x010F6, + 0x010F7, + 0x010F8, + 0x010F9, + 0x010FA, + 0x010FB, + 0x010FC, + 0x010FD, + 0x010FE, + 0x010FF, }; -static const uint32_t nxt_unicode_block_03c[128] nxt_aligned(64) = { - 0x01E01, 0x01E01, 0x01E03, 0x01E03, 0x01E05, 0x01E05, 0x01E07, 0x01E07, - 0x01E09, 0x01E09, 0x01E0B, 0x01E0B, 0x01E0D, 0x01E0D, 0x01E0F, 0x01E0F, - 0x01E11, 0x01E11, 0x01E13, 0x01E13, 0x01E15, 0x01E15, 0x01E17, 0x01E17, - 0x01E19, 0x01E19, 0x01E1B, 0x01E1B, 0x01E1D, 0x01E1D, 0x01E1F, 0x01E1F, - 0x01E21, 0x01E21, 0x01E23, 0x01E23, 0x01E25, 0x01E25, 0x01E27, 0x01E27, - 0x01E29, 0x01E29, 0x01E2B, 0x01E2B, 0x01E2D, 0x01E2D, 0x01E2F, 0x01E2F, - 0x01E31, 0x01E31, 0x01E33, 0x01E33, 0x01E35, 0x01E35, 0x01E37, 0x01E37, - 0x01E39, 0x01E39, 0x01E3B, 0x01E3B, 0x01E3D, 0x01E3D, 0x01E3F, 0x01E3F, - 0x01E41, 0x01E41, 0x01E43, 0x01E43, 0x01E45, 0x01E45, 0x01E47, 0x01E47, - 0x01E49, 0x01E49, 0x01E4B, 0x01E4B, 0x01E4D, 0x01E4D, 0x01E4F, 0x01E4F, - 0x01E51, 0x01E51, 0x01E53, 0x01E53, 0x01E55, 0x01E55, 0x01E57, 0x01E57, - 0x01E59, 0x01E59, 0x01E5B, 0x01E5B, 0x01E5D, 0x01E5D, 0x01E5F, 0x01E5F, - 0x01E61, 0x01E61, 0x01E63, 0x01E63, 0x01E65, 0x01E65, 0x01E67, 0x01E67, - 0x01E69, 0x01E69, 0x01E6B, 0x01E6B, 0x01E6D, 0x01E6D, 0x01E6F, 0x01E6F, - 0x01E71, 0x01E71, 0x01E73, 0x01E73, 0x01E75, 0x01E75, 0x01E77, 0x01E77, - 0x01E79, 0x01E79, 0x01E7B, 0x01E7B, 0x01E7D, 0x01E7D, 0x01E7F, 0x01E7F, +static const uint32_t nxt_unicode_block_03c[128] nxt_aligned(64) = { + 0x01E01, + 0x01E01, + 0x01E03, + 0x01E03, + 0x01E05, + 0x01E05, + 0x01E07, + 0x01E07, + 0x01E09, + 0x01E09, + 0x01E0B, + 0x01E0B, + 0x01E0D, + 0x01E0D, + 0x01E0F, + 0x01E0F, + 0x01E11, + 0x01E11, + 0x01E13, + 0x01E13, + 0x01E15, + 0x01E15, + 0x01E17, + 0x01E17, + 0x01E19, + 0x01E19, + 0x01E1B, + 0x01E1B, + 0x01E1D, + 0x01E1D, + 0x01E1F, + 0x01E1F, + 0x01E21, + 0x01E21, + 0x01E23, + 0x01E23, + 0x01E25, + 0x01E25, + 0x01E27, + 0x01E27, + 0x01E29, + 0x01E29, + 0x01E2B, + 0x01E2B, + 0x01E2D, + 0x01E2D, + 0x01E2F, + 0x01E2F, + 0x01E31, + 0x01E31, + 0x01E33, + 0x01E33, + 0x01E35, + 0x01E35, + 0x01E37, + 0x01E37, + 0x01E39, + 0x01E39, + 0x01E3B, + 0x01E3B, + 0x01E3D, + 0x01E3D, + 0x01E3F, + 0x01E3F, + 0x01E41, + 0x01E41, + 0x01E43, + 0x01E43, + 0x01E45, + 0x01E45, + 0x01E47, + 0x01E47, + 0x01E49, + 0x01E49, + 0x01E4B, + 0x01E4B, + 0x01E4D, + 0x01E4D, + 0x01E4F, + 0x01E4F, + 0x01E51, + 0x01E51, + 0x01E53, + 0x01E53, + 0x01E55, + 0x01E55, + 0x01E57, + 0x01E57, + 0x01E59, + 0x01E59, + 0x01E5B, + 0x01E5B, + 0x01E5D, + 0x01E5D, + 0x01E5F, + 0x01E5F, + 0x01E61, + 0x01E61, + 0x01E63, + 0x01E63, + 0x01E65, + 0x01E65, + 0x01E67, + 0x01E67, + 0x01E69, + 0x01E69, + 0x01E6B, + 0x01E6B, + 0x01E6D, + 0x01E6D, + 0x01E6F, + 0x01E6F, + 0x01E71, + 0x01E71, + 0x01E73, + 0x01E73, + 0x01E75, + 0x01E75, + 0x01E77, + 0x01E77, + 0x01E79, + 0x01E79, + 0x01E7B, + 0x01E7B, + 0x01E7D, + 0x01E7D, + 0x01E7F, + 0x01E7F, }; -static const uint32_t nxt_unicode_block_03d[128] nxt_aligned(64) = { - 0x01E81, 0x01E81, 0x01E83, 0x01E83, 0x01E85, 0x01E85, 0x01E87, 0x01E87, - 0x01E89, 0x01E89, 0x01E8B, 0x01E8B, 0x01E8D, 0x01E8D, 0x01E8F, 0x01E8F, - 0x01E91, 0x01E91, 0x01E93, 0x01E93, 0x01E95, 0x01E95, 0x01E96, 0x01E97, - 0x01E98, 0x01E99, 0x01E9A, 0x01E61, 0x01E9C, 0x01E9D, 0x000DF, 0x01E9F, - 0x01EA1, 0x01EA1, 0x01EA3, 0x01EA3, 0x01EA5, 0x01EA5, 0x01EA7, 0x01EA7, - 0x01EA9, 0x01EA9, 0x01EAB, 0x01EAB, 0x01EAD, 0x01EAD, 0x01EAF, 0x01EAF, - 0x01EB1, 0x01EB1, 0x01EB3, 0x01EB3, 0x01EB5, 0x01EB5, 0x01EB7, 0x01EB7, - 0x01EB9, 0x01EB9, 0x01EBB, 0x01EBB, 0x01EBD, 0x01EBD, 0x01EBF, 0x01EBF, - 0x01EC1, 0x01EC1, 0x01EC3, 0x01EC3, 0x01EC5, 0x01EC5, 0x01EC7, 0x01EC7, - 0x01EC9, 0x01EC9, 0x01ECB, 0x01ECB, 0x01ECD, 0x01ECD, 0x01ECF, 0x01ECF, - 0x01ED1, 0x01ED1, 0x01ED3, 0x01ED3, 0x01ED5, 0x01ED5, 0x01ED7, 0x01ED7, - 0x01ED9, 0x01ED9, 0x01EDB, 0x01EDB, 0x01EDD, 0x01EDD, 0x01EDF, 0x01EDF, - 0x01EE1, 0x01EE1, 0x01EE3, 0x01EE3, 0x01EE5, 0x01EE5, 0x01EE7, 0x01EE7, - 0x01EE9, 0x01EE9, 0x01EEB, 0x01EEB, 0x01EED, 0x01EED, 0x01EEF, 0x01EEF, - 0x01EF1, 0x01EF1, 0x01EF3, 0x01EF3, 0x01EF5, 0x01EF5, 0x01EF7, 0x01EF7, - 0x01EF9, 0x01EF9, 0x01EFB, 0x01EFB, 0x01EFD, 0x01EFD, 0x01EFF, 0x01EFF, +static const uint32_t nxt_unicode_block_03d[128] nxt_aligned(64) = { + 0x01E81, + 0x01E81, + 0x01E83, + 0x01E83, + 0x01E85, + 0x01E85, + 0x01E87, + 0x01E87, + 0x01E89, + 0x01E89, + 0x01E8B, + 0x01E8B, + 0x01E8D, + 0x01E8D, + 0x01E8F, + 0x01E8F, + 0x01E91, + 0x01E91, + 0x01E93, + 0x01E93, + 0x01E95, + 0x01E95, + 0x01E96, + 0x01E97, + 0x01E98, + 0x01E99, + 0x01E9A, + 0x01E61, + 0x01E9C, + 0x01E9D, + 0x000DF, + 0x01E9F, + 0x01EA1, + 0x01EA1, + 0x01EA3, + 0x01EA3, + 0x01EA5, + 0x01EA5, + 0x01EA7, + 0x01EA7, + 0x01EA9, + 0x01EA9, + 0x01EAB, + 0x01EAB, + 0x01EAD, + 0x01EAD, + 0x01EAF, + 0x01EAF, + 0x01EB1, + 0x01EB1, + 0x01EB3, + 0x01EB3, + 0x01EB5, + 0x01EB5, + 0x01EB7, + 0x01EB7, + 0x01EB9, + 0x01EB9, + 0x01EBB, + 0x01EBB, + 0x01EBD, + 0x01EBD, + 0x01EBF, + 0x01EBF, + 0x01EC1, + 0x01EC1, + 0x01EC3, + 0x01EC3, + 0x01EC5, + 0x01EC5, + 0x01EC7, + 0x01EC7, + 0x01EC9, + 0x01EC9, + 0x01ECB, + 0x01ECB, + 0x01ECD, + 0x01ECD, + 0x01ECF, + 0x01ECF, + 0x01ED1, + 0x01ED1, + 0x01ED3, + 0x01ED3, + 0x01ED5, + 0x01ED5, + 0x01ED7, + 0x01ED7, + 0x01ED9, + 0x01ED9, + 0x01EDB, + 0x01EDB, + 0x01EDD, + 0x01EDD, + 0x01EDF, + 0x01EDF, + 0x01EE1, + 0x01EE1, + 0x01EE3, + 0x01EE3, + 0x01EE5, + 0x01EE5, + 0x01EE7, + 0x01EE7, + 0x01EE9, + 0x01EE9, + 0x01EEB, + 0x01EEB, + 0x01EED, + 0x01EED, + 0x01EEF, + 0x01EEF, + 0x01EF1, + 0x01EF1, + 0x01EF3, + 0x01EF3, + 0x01EF5, + 0x01EF5, + 0x01EF7, + 0x01EF7, + 0x01EF9, + 0x01EF9, + 0x01EFB, + 0x01EFB, + 0x01EFD, + 0x01EFD, + 0x01EFF, + 0x01EFF, }; -static const uint32_t nxt_unicode_block_03e[128] nxt_aligned(64) = { - 0x01F00, 0x01F01, 0x01F02, 0x01F03, 0x01F04, 0x01F05, 0x01F06, 0x01F07, - 0x01F00, 0x01F01, 0x01F02, 0x01F03, 0x01F04, 0x01F05, 0x01F06, 0x01F07, - 0x01F10, 0x01F11, 0x01F12, 0x01F13, 0x01F14, 0x01F15, 0x01F16, 0x01F17, - 0x01F10, 0x01F11, 0x01F12, 0x01F13, 0x01F14, 0x01F15, 0x01F1E, 0x01F1F, - 0x01F20, 0x01F21, 0x01F22, 0x01F23, 0x01F24, 0x01F25, 0x01F26, 0x01F27, - 0x01F20, 0x01F21, 0x01F22, 0x01F23, 0x01F24, 0x01F25, 0x01F26, 0x01F27, - 0x01F30, 0x01F31, 0x01F32, 0x01F33, 0x01F34, 0x01F35, 0x01F36, 0x01F37, - 0x01F30, 0x01F31, 0x01F32, 0x01F33, 0x01F34, 0x01F35, 0x01F36, 0x01F37, - 0x01F40, 0x01F41, 0x01F42, 0x01F43, 0x01F44, 0x01F45, 0x01F46, 0x01F47, - 0x01F40, 0x01F41, 0x01F42, 0x01F43, 0x01F44, 0x01F45, 0x01F4E, 0x01F4F, - 0x01F50, 0x01F51, 0x01F52, 0x01F53, 0x01F54, 0x01F55, 0x01F56, 0x01F57, - 0x01F58, 0x01F51, 0x01F5A, 0x01F53, 0x01F5C, 0x01F55, 0x01F5E, 0x01F57, - 0x01F60, 0x01F61, 0x01F62, 0x01F63, 0x01F64, 0x01F65, 0x01F66, 0x01F67, - 0x01F60, 0x01F61, 0x01F62, 0x01F63, 0x01F64, 0x01F65, 0x01F66, 0x01F67, - 0x01F70, 0x01F71, 0x01F72, 0x01F73, 0x01F74, 0x01F75, 0x01F76, 0x01F77, - 0x01F78, 0x01F79, 0x01F7A, 0x01F7B, 0x01F7C, 0x01F7D, 0x01F7E, 0x01F7F, +static const uint32_t nxt_unicode_block_03e[128] nxt_aligned(64) = { + 0x01F00, + 0x01F01, + 0x01F02, + 0x01F03, + 0x01F04, + 0x01F05, + 0x01F06, + 0x01F07, + 0x01F00, + 0x01F01, + 0x01F02, + 0x01F03, + 0x01F04, + 0x01F05, + 0x01F06, + 0x01F07, + 0x01F10, + 0x01F11, + 0x01F12, + 0x01F13, + 0x01F14, + 0x01F15, + 0x01F16, + 0x01F17, + 0x01F10, + 0x01F11, + 0x01F12, + 0x01F13, + 0x01F14, + 0x01F15, + 0x01F1E, + 0x01F1F, + 0x01F20, + 0x01F21, + 0x01F22, + 0x01F23, + 0x01F24, + 0x01F25, + 0x01F26, + 0x01F27, + 0x01F20, + 0x01F21, + 0x01F22, + 0x01F23, + 0x01F24, + 0x01F25, + 0x01F26, + 0x01F27, + 0x01F30, + 0x01F31, + 0x01F32, + 0x01F33, + 0x01F34, + 0x01F35, + 0x01F36, + 0x01F37, + 0x01F30, + 0x01F31, + 0x01F32, + 0x01F33, + 0x01F34, + 0x01F35, + 0x01F36, + 0x01F37, + 0x01F40, + 0x01F41, + 0x01F42, + 0x01F43, + 0x01F44, + 0x01F45, + 0x01F46, + 0x01F47, + 0x01F40, + 0x01F41, + 0x01F42, + 0x01F43, + 0x01F44, + 0x01F45, + 0x01F4E, + 0x01F4F, + 0x01F50, + 0x01F51, + 0x01F52, + 0x01F53, + 0x01F54, + 0x01F55, + 0x01F56, + 0x01F57, + 0x01F58, + 0x01F51, + 0x01F5A, + 0x01F53, + 0x01F5C, + 0x01F55, + 0x01F5E, + 0x01F57, + 0x01F60, + 0x01F61, + 0x01F62, + 0x01F63, + 0x01F64, + 0x01F65, + 0x01F66, + 0x01F67, + 0x01F60, + 0x01F61, + 0x01F62, + 0x01F63, + 0x01F64, + 0x01F65, + 0x01F66, + 0x01F67, + 0x01F70, + 0x01F71, + 0x01F72, + 0x01F73, + 0x01F74, + 0x01F75, + 0x01F76, + 0x01F77, + 0x01F78, + 0x01F79, + 0x01F7A, + 0x01F7B, + 0x01F7C, + 0x01F7D, + 0x01F7E, + 0x01F7F, }; -static const uint32_t nxt_unicode_block_03f[128] nxt_aligned(64) = { - 0x01F80, 0x01F81, 0x01F82, 0x01F83, 0x01F84, 0x01F85, 0x01F86, 0x01F87, - 0x01F80, 0x01F81, 0x01F82, 0x01F83, 0x01F84, 0x01F85, 0x01F86, 0x01F87, - 0x01F90, 0x01F91, 0x01F92, 0x01F93, 0x01F94, 0x01F95, 0x01F96, 0x01F97, - 0x01F90, 0x01F91, 0x01F92, 0x01F93, 0x01F94, 0x01F95, 0x01F96, 0x01F97, - 0x01FA0, 0x01FA1, 0x01FA2, 0x01FA3, 0x01FA4, 0x01FA5, 0x01FA6, 0x01FA7, - 0x01FA0, 0x01FA1, 0x01FA2, 0x01FA3, 0x01FA4, 0x01FA5, 0x01FA6, 0x01FA7, - 0x01FB0, 0x01FB1, 0x01FB2, 0x01FB3, 0x01FB4, 0x01FB5, 0x01FB6, 0x01FB7, - 0x01FB0, 0x01FB1, 0x01F70, 0x01F71, 0x01FB3, 0x01FBD, 0x003B9, 0x01FBF, - 0x01FC0, 0x01FC1, 0x01FC2, 0x01FC3, 0x01FC4, 0x01FC5, 0x01FC6, 0x01FC7, - 0x01F72, 0x01F73, 0x01F74, 0x01F75, 0x01FC3, 0x01FCD, 0x01FCE, 0x01FCF, - 0x01FD0, 0x01FD1, 0x01FD2, 0x01FD3, 0x01FD4, 0x01FD5, 0x01FD6, 0x01FD7, - 0x01FD0, 0x01FD1, 0x01F76, 0x01F77, 0x01FDC, 0x01FDD, 0x01FDE, 0x01FDF, - 0x01FE0, 0x01FE1, 0x01FE2, 0x01FE3, 0x01FE4, 0x01FE5, 0x01FE6, 0x01FE7, - 0x01FE0, 0x01FE1, 0x01F7A, 0x01F7B, 0x01FE5, 0x01FED, 0x01FEE, 0x01FEF, - 0x01FF0, 0x01FF1, 0x01FF2, 0x01FF3, 0x01FF4, 0x01FF5, 0x01FF6, 0x01FF7, - 0x01F78, 0x01F79, 0x01F7C, 0x01F7D, 0x01FF3, 0x01FFD, 0x01FFE, 0x01FFF, +static const uint32_t nxt_unicode_block_03f[128] nxt_aligned(64) = { + 0x01F80, + 0x01F81, + 0x01F82, + 0x01F83, + 0x01F84, + 0x01F85, + 0x01F86, + 0x01F87, + 0x01F80, + 0x01F81, + 0x01F82, + 0x01F83, + 0x01F84, + 0x01F85, + 0x01F86, + 0x01F87, + 0x01F90, + 0x01F91, + 0x01F92, + 0x01F93, + 0x01F94, + 0x01F95, + 0x01F96, + 0x01F97, + 0x01F90, + 0x01F91, + 0x01F92, + 0x01F93, + 0x01F94, + 0x01F95, + 0x01F96, + 0x01F97, + 0x01FA0, + 0x01FA1, + 0x01FA2, + 0x01FA3, + 0x01FA4, + 0x01FA5, + 0x01FA6, + 0x01FA7, + 0x01FA0, + 0x01FA1, + 0x01FA2, + 0x01FA3, + 0x01FA4, + 0x01FA5, + 0x01FA6, + 0x01FA7, + 0x01FB0, + 0x01FB1, + 0x01FB2, + 0x01FB3, + 0x01FB4, + 0x01FB5, + 0x01FB6, + 0x01FB7, + 0x01FB0, + 0x01FB1, + 0x01F70, + 0x01F71, + 0x01FB3, + 0x01FBD, + 0x003B9, + 0x01FBF, + 0x01FC0, + 0x01FC1, + 0x01FC2, + 0x01FC3, + 0x01FC4, + 0x01FC5, + 0x01FC6, + 0x01FC7, + 0x01F72, + 0x01F73, + 0x01F74, + 0x01F75, + 0x01FC3, + 0x01FCD, + 0x01FCE, + 0x01FCF, + 0x01FD0, + 0x01FD1, + 0x01FD2, + 0x01FD3, + 0x01FD4, + 0x01FD5, + 0x01FD6, + 0x01FD7, + 0x01FD0, + 0x01FD1, + 0x01F76, + 0x01F77, + 0x01FDC, + 0x01FDD, + 0x01FDE, + 0x01FDF, + 0x01FE0, + 0x01FE1, + 0x01FE2, + 0x01FE3, + 0x01FE4, + 0x01FE5, + 0x01FE6, + 0x01FE7, + 0x01FE0, + 0x01FE1, + 0x01F7A, + 0x01F7B, + 0x01FE5, + 0x01FED, + 0x01FEE, + 0x01FEF, + 0x01FF0, + 0x01FF1, + 0x01FF2, + 0x01FF3, + 0x01FF4, + 0x01FF5, + 0x01FF6, + 0x01FF7, + 0x01F78, + 0x01F79, + 0x01F7C, + 0x01F7D, + 0x01FF3, + 0x01FFD, + 0x01FFE, + 0x01FFF, }; -static const uint32_t nxt_unicode_block_042[128] nxt_aligned(64) = { - 0x02100, 0x02101, 0x02102, 0x02103, 0x02104, 0x02105, 0x02106, 0x02107, - 0x02108, 0x02109, 0x0210A, 0x0210B, 0x0210C, 0x0210D, 0x0210E, 0x0210F, - 0x02110, 0x02111, 0x02112, 0x02113, 0x02114, 0x02115, 0x02116, 0x02117, - 0x02118, 0x02119, 0x0211A, 0x0211B, 0x0211C, 0x0211D, 0x0211E, 0x0211F, - 0x02120, 0x02121, 0x02122, 0x02123, 0x02124, 0x02125, 0x003C9, 0x02127, - 0x02128, 0x02129, 0x0006B, 0x000E5, 0x0212C, 0x0212D, 0x0212E, 0x0212F, - 0x02130, 0x02131, 0x0214E, 0x02133, 0x02134, 0x02135, 0x02136, 0x02137, - 0x02138, 0x02139, 0x0213A, 0x0213B, 0x0213C, 0x0213D, 0x0213E, 0x0213F, - 0x02140, 0x02141, 0x02142, 0x02143, 0x02144, 0x02145, 0x02146, 0x02147, - 0x02148, 0x02149, 0x0214A, 0x0214B, 0x0214C, 0x0214D, 0x0214E, 0x0214F, - 0x02150, 0x02151, 0x02152, 0x02153, 0x02154, 0x02155, 0x02156, 0x02157, - 0x02158, 0x02159, 0x0215A, 0x0215B, 0x0215C, 0x0215D, 0x0215E, 0x0215F, - 0x02170, 0x02171, 0x02172, 0x02173, 0x02174, 0x02175, 0x02176, 0x02177, - 0x02178, 0x02179, 0x0217A, 0x0217B, 0x0217C, 0x0217D, 0x0217E, 0x0217F, - 0x02170, 0x02171, 0x02172, 0x02173, 0x02174, 0x02175, 0x02176, 0x02177, - 0x02178, 0x02179, 0x0217A, 0x0217B, 0x0217C, 0x0217D, 0x0217E, 0x0217F, +static const uint32_t nxt_unicode_block_042[128] nxt_aligned(64) = { + 0x02100, + 0x02101, + 0x02102, + 0x02103, + 0x02104, + 0x02105, + 0x02106, + 0x02107, + 0x02108, + 0x02109, + 0x0210A, + 0x0210B, + 0x0210C, + 0x0210D, + 0x0210E, + 0x0210F, + 0x02110, + 0x02111, + 0x02112, + 0x02113, + 0x02114, + 0x02115, + 0x02116, + 0x02117, + 0x02118, + 0x02119, + 0x0211A, + 0x0211B, + 0x0211C, + 0x0211D, + 0x0211E, + 0x0211F, + 0x02120, + 0x02121, + 0x02122, + 0x02123, + 0x02124, + 0x02125, + 0x003C9, + 0x02127, + 0x02128, + 0x02129, + 0x0006B, + 0x000E5, + 0x0212C, + 0x0212D, + 0x0212E, + 0x0212F, + 0x02130, + 0x02131, + 0x0214E, + 0x02133, + 0x02134, + 0x02135, + 0x02136, + 0x02137, + 0x02138, + 0x02139, + 0x0213A, + 0x0213B, + 0x0213C, + 0x0213D, + 0x0213E, + 0x0213F, + 0x02140, + 0x02141, + 0x02142, + 0x02143, + 0x02144, + 0x02145, + 0x02146, + 0x02147, + 0x02148, + 0x02149, + 0x0214A, + 0x0214B, + 0x0214C, + 0x0214D, + 0x0214E, + 0x0214F, + 0x02150, + 0x02151, + 0x02152, + 0x02153, + 0x02154, + 0x02155, + 0x02156, + 0x02157, + 0x02158, + 0x02159, + 0x0215A, + 0x0215B, + 0x0215C, + 0x0215D, + 0x0215E, + 0x0215F, + 0x02170, + 0x02171, + 0x02172, + 0x02173, + 0x02174, + 0x02175, + 0x02176, + 0x02177, + 0x02178, + 0x02179, + 0x0217A, + 0x0217B, + 0x0217C, + 0x0217D, + 0x0217E, + 0x0217F, + 0x02170, + 0x02171, + 0x02172, + 0x02173, + 0x02174, + 0x02175, + 0x02176, + 0x02177, + 0x02178, + 0x02179, + 0x0217A, + 0x0217B, + 0x0217C, + 0x0217D, + 0x0217E, + 0x0217F, }; -static const uint32_t nxt_unicode_block_043[128] nxt_aligned(64) = { - 0x02180, 0x02181, 0x02182, 0x02184, 0x02184, 0x02185, 0x02186, 0x02187, - 0x02188, 0x02189, 0x0218A, 0x0218B, 0x0218C, 0x0218D, 0x0218E, 0x0218F, - 0x02190, 0x02191, 0x02192, 0x02193, 0x02194, 0x02195, 0x02196, 0x02197, - 0x02198, 0x02199, 0x0219A, 0x0219B, 0x0219C, 0x0219D, 0x0219E, 0x0219F, - 0x021A0, 0x021A1, 0x021A2, 0x021A3, 0x021A4, 0x021A5, 0x021A6, 0x021A7, - 0x021A8, 0x021A9, 0x021AA, 0x021AB, 0x021AC, 0x021AD, 0x021AE, 0x021AF, - 0x021B0, 0x021B1, 0x021B2, 0x021B3, 0x021B4, 0x021B5, 0x021B6, 0x021B7, - 0x021B8, 0x021B9, 0x021BA, 0x021BB, 0x021BC, 0x021BD, 0x021BE, 0x021BF, - 0x021C0, 0x021C1, 0x021C2, 0x021C3, 0x021C4, 0x021C5, 0x021C6, 0x021C7, - 0x021C8, 0x021C9, 0x021CA, 0x021CB, 0x021CC, 0x021CD, 0x021CE, 0x021CF, - 0x021D0, 0x021D1, 0x021D2, 0x021D3, 0x021D4, 0x021D5, 0x021D6, 0x021D7, - 0x021D8, 0x021D9, 0x021DA, 0x021DB, 0x021DC, 0x021DD, 0x021DE, 0x021DF, - 0x021E0, 0x021E1, 0x021E2, 0x021E3, 0x021E4, 0x021E5, 0x021E6, 0x021E7, - 0x021E8, 0x021E9, 0x021EA, 0x021EB, 0x021EC, 0x021ED, 0x021EE, 0x021EF, - 0x021F0, 0x021F1, 0x021F2, 0x021F3, 0x021F4, 0x021F5, 0x021F6, 0x021F7, - 0x021F8, 0x021F9, 0x021FA, 0x021FB, 0x021FC, 0x021FD, 0x021FE, 0x021FF, +static const uint32_t nxt_unicode_block_043[128] nxt_aligned(64) = { + 0x02180, + 0x02181, + 0x02182, + 0x02184, + 0x02184, + 0x02185, + 0x02186, + 0x02187, + 0x02188, + 0x02189, + 0x0218A, + 0x0218B, + 0x0218C, + 0x0218D, + 0x0218E, + 0x0218F, + 0x02190, + 0x02191, + 0x02192, + 0x02193, + 0x02194, + 0x02195, + 0x02196, + 0x02197, + 0x02198, + 0x02199, + 0x0219A, + 0x0219B, + 0x0219C, + 0x0219D, + 0x0219E, + 0x0219F, + 0x021A0, + 0x021A1, + 0x021A2, + 0x021A3, + 0x021A4, + 0x021A5, + 0x021A6, + 0x021A7, + 0x021A8, + 0x021A9, + 0x021AA, + 0x021AB, + 0x021AC, + 0x021AD, + 0x021AE, + 0x021AF, + 0x021B0, + 0x021B1, + 0x021B2, + 0x021B3, + 0x021B4, + 0x021B5, + 0x021B6, + 0x021B7, + 0x021B8, + 0x021B9, + 0x021BA, + 0x021BB, + 0x021BC, + 0x021BD, + 0x021BE, + 0x021BF, + 0x021C0, + 0x021C1, + 0x021C2, + 0x021C3, + 0x021C4, + 0x021C5, + 0x021C6, + 0x021C7, + 0x021C8, + 0x021C9, + 0x021CA, + 0x021CB, + 0x021CC, + 0x021CD, + 0x021CE, + 0x021CF, + 0x021D0, + 0x021D1, + 0x021D2, + 0x021D3, + 0x021D4, + 0x021D5, + 0x021D6, + 0x021D7, + 0x021D8, + 0x021D9, + 0x021DA, + 0x021DB, + 0x021DC, + 0x021DD, + 0x021DE, + 0x021DF, + 0x021E0, + 0x021E1, + 0x021E2, + 0x021E3, + 0x021E4, + 0x021E5, + 0x021E6, + 0x021E7, + 0x021E8, + 0x021E9, + 0x021EA, + 0x021EB, + 0x021EC, + 0x021ED, + 0x021EE, + 0x021EF, + 0x021F0, + 0x021F1, + 0x021F2, + 0x021F3, + 0x021F4, + 0x021F5, + 0x021F6, + 0x021F7, + 0x021F8, + 0x021F9, + 0x021FA, + 0x021FB, + 0x021FC, + 0x021FD, + 0x021FE, + 0x021FF, }; -static const uint32_t nxt_unicode_block_049[128] nxt_aligned(64) = { - 0x02480, 0x02481, 0x02482, 0x02483, 0x02484, 0x02485, 0x02486, 0x02487, - 0x02488, 0x02489, 0x0248A, 0x0248B, 0x0248C, 0x0248D, 0x0248E, 0x0248F, - 0x02490, 0x02491, 0x02492, 0x02493, 0x02494, 0x02495, 0x02496, 0x02497, - 0x02498, 0x02499, 0x0249A, 0x0249B, 0x0249C, 0x0249D, 0x0249E, 0x0249F, - 0x024A0, 0x024A1, 0x024A2, 0x024A3, 0x024A4, 0x024A5, 0x024A6, 0x024A7, - 0x024A8, 0x024A9, 0x024AA, 0x024AB, 0x024AC, 0x024AD, 0x024AE, 0x024AF, - 0x024B0, 0x024B1, 0x024B2, 0x024B3, 0x024B4, 0x024B5, 0x024D0, 0x024D1, - 0x024D2, 0x024D3, 0x024D4, 0x024D5, 0x024D6, 0x024D7, 0x024D8, 0x024D9, - 0x024DA, 0x024DB, 0x024DC, 0x024DD, 0x024DE, 0x024DF, 0x024E0, 0x024E1, - 0x024E2, 0x024E3, 0x024E4, 0x024E5, 0x024E6, 0x024E7, 0x024E8, 0x024E9, - 0x024D0, 0x024D1, 0x024D2, 0x024D3, 0x024D4, 0x024D5, 0x024D6, 0x024D7, - 0x024D8, 0x024D9, 0x024DA, 0x024DB, 0x024DC, 0x024DD, 0x024DE, 0x024DF, - 0x024E0, 0x024E1, 0x024E2, 0x024E3, 0x024E4, 0x024E5, 0x024E6, 0x024E7, - 0x024E8, 0x024E9, 0x024EA, 0x024EB, 0x024EC, 0x024ED, 0x024EE, 0x024EF, - 0x024F0, 0x024F1, 0x024F2, 0x024F3, 0x024F4, 0x024F5, 0x024F6, 0x024F7, - 0x024F8, 0x024F9, 0x024FA, 0x024FB, 0x024FC, 0x024FD, 0x024FE, 0x024FF, +static const uint32_t nxt_unicode_block_049[128] nxt_aligned(64) = { + 0x02480, + 0x02481, + 0x02482, + 0x02483, + 0x02484, + 0x02485, + 0x02486, + 0x02487, + 0x02488, + 0x02489, + 0x0248A, + 0x0248B, + 0x0248C, + 0x0248D, + 0x0248E, + 0x0248F, + 0x02490, + 0x02491, + 0x02492, + 0x02493, + 0x02494, + 0x02495, + 0x02496, + 0x02497, + 0x02498, + 0x02499, + 0x0249A, + 0x0249B, + 0x0249C, + 0x0249D, + 0x0249E, + 0x0249F, + 0x024A0, + 0x024A1, + 0x024A2, + 0x024A3, + 0x024A4, + 0x024A5, + 0x024A6, + 0x024A7, + 0x024A8, + 0x024A9, + 0x024AA, + 0x024AB, + 0x024AC, + 0x024AD, + 0x024AE, + 0x024AF, + 0x024B0, + 0x024B1, + 0x024B2, + 0x024B3, + 0x024B4, + 0x024B5, + 0x024D0, + 0x024D1, + 0x024D2, + 0x024D3, + 0x024D4, + 0x024D5, + 0x024D6, + 0x024D7, + 0x024D8, + 0x024D9, + 0x024DA, + 0x024DB, + 0x024DC, + 0x024DD, + 0x024DE, + 0x024DF, + 0x024E0, + 0x024E1, + 0x024E2, + 0x024E3, + 0x024E4, + 0x024E5, + 0x024E6, + 0x024E7, + 0x024E8, + 0x024E9, + 0x024D0, + 0x024D1, + 0x024D2, + 0x024D3, + 0x024D4, + 0x024D5, + 0x024D6, + 0x024D7, + 0x024D8, + 0x024D9, + 0x024DA, + 0x024DB, + 0x024DC, + 0x024DD, + 0x024DE, + 0x024DF, + 0x024E0, + 0x024E1, + 0x024E2, + 0x024E3, + 0x024E4, + 0x024E5, + 0x024E6, + 0x024E7, + 0x024E8, + 0x024E9, + 0x024EA, + 0x024EB, + 0x024EC, + 0x024ED, + 0x024EE, + 0x024EF, + 0x024F0, + 0x024F1, + 0x024F2, + 0x024F3, + 0x024F4, + 0x024F5, + 0x024F6, + 0x024F7, + 0x024F8, + 0x024F9, + 0x024FA, + 0x024FB, + 0x024FC, + 0x024FD, + 0x024FE, + 0x024FF, }; -static const uint32_t nxt_unicode_block_058[128] nxt_aligned(64) = { - 0x02C30, 0x02C31, 0x02C32, 0x02C33, 0x02C34, 0x02C35, 0x02C36, 0x02C37, - 0x02C38, 0x02C39, 0x02C3A, 0x02C3B, 0x02C3C, 0x02C3D, 0x02C3E, 0x02C3F, - 0x02C40, 0x02C41, 0x02C42, 0x02C43, 0x02C44, 0x02C45, 0x02C46, 0x02C47, - 0x02C48, 0x02C49, 0x02C4A, 0x02C4B, 0x02C4C, 0x02C4D, 0x02C4E, 0x02C4F, - 0x02C50, 0x02C51, 0x02C52, 0x02C53, 0x02C54, 0x02C55, 0x02C56, 0x02C57, - 0x02C58, 0x02C59, 0x02C5A, 0x02C5B, 0x02C5C, 0x02C5D, 0x02C5E, 0x02C2F, - 0x02C30, 0x02C31, 0x02C32, 0x02C33, 0x02C34, 0x02C35, 0x02C36, 0x02C37, - 0x02C38, 0x02C39, 0x02C3A, 0x02C3B, 0x02C3C, 0x02C3D, 0x02C3E, 0x02C3F, - 0x02C40, 0x02C41, 0x02C42, 0x02C43, 0x02C44, 0x02C45, 0x02C46, 0x02C47, - 0x02C48, 0x02C49, 0x02C4A, 0x02C4B, 0x02C4C, 0x02C4D, 0x02C4E, 0x02C4F, - 0x02C50, 0x02C51, 0x02C52, 0x02C53, 0x02C54, 0x02C55, 0x02C56, 0x02C57, - 0x02C58, 0x02C59, 0x02C5A, 0x02C5B, 0x02C5C, 0x02C5D, 0x02C5E, 0x02C5F, - 0x02C61, 0x02C61, 0x0026B, 0x01D7D, 0x0027D, 0x02C65, 0x02C66, 0x02C68, - 0x02C68, 0x02C6A, 0x02C6A, 0x02C6C, 0x02C6C, 0x00251, 0x00271, 0x00250, - 0x00252, 0x02C71, 0x02C73, 0x02C73, 0x02C74, 0x02C76, 0x02C76, 0x02C77, - 0x02C78, 0x02C79, 0x02C7A, 0x02C7B, 0x02C7C, 0x02C7D, 0x0023F, 0x00240, +static const uint32_t nxt_unicode_block_058[128] nxt_aligned(64) = { + 0x02C30, + 0x02C31, + 0x02C32, + 0x02C33, + 0x02C34, + 0x02C35, + 0x02C36, + 0x02C37, + 0x02C38, + 0x02C39, + 0x02C3A, + 0x02C3B, + 0x02C3C, + 0x02C3D, + 0x02C3E, + 0x02C3F, + 0x02C40, + 0x02C41, + 0x02C42, + 0x02C43, + 0x02C44, + 0x02C45, + 0x02C46, + 0x02C47, + 0x02C48, + 0x02C49, + 0x02C4A, + 0x02C4B, + 0x02C4C, + 0x02C4D, + 0x02C4E, + 0x02C4F, + 0x02C50, + 0x02C51, + 0x02C52, + 0x02C53, + 0x02C54, + 0x02C55, + 0x02C56, + 0x02C57, + 0x02C58, + 0x02C59, + 0x02C5A, + 0x02C5B, + 0x02C5C, + 0x02C5D, + 0x02C5E, + 0x02C2F, + 0x02C30, + 0x02C31, + 0x02C32, + 0x02C33, + 0x02C34, + 0x02C35, + 0x02C36, + 0x02C37, + 0x02C38, + 0x02C39, + 0x02C3A, + 0x02C3B, + 0x02C3C, + 0x02C3D, + 0x02C3E, + 0x02C3F, + 0x02C40, + 0x02C41, + 0x02C42, + 0x02C43, + 0x02C44, + 0x02C45, + 0x02C46, + 0x02C47, + 0x02C48, + 0x02C49, + 0x02C4A, + 0x02C4B, + 0x02C4C, + 0x02C4D, + 0x02C4E, + 0x02C4F, + 0x02C50, + 0x02C51, + 0x02C52, + 0x02C53, + 0x02C54, + 0x02C55, + 0x02C56, + 0x02C57, + 0x02C58, + 0x02C59, + 0x02C5A, + 0x02C5B, + 0x02C5C, + 0x02C5D, + 0x02C5E, + 0x02C5F, + 0x02C61, + 0x02C61, + 0x0026B, + 0x01D7D, + 0x0027D, + 0x02C65, + 0x02C66, + 0x02C68, + 0x02C68, + 0x02C6A, + 0x02C6A, + 0x02C6C, + 0x02C6C, + 0x00251, + 0x00271, + 0x00250, + 0x00252, + 0x02C71, + 0x02C73, + 0x02C73, + 0x02C74, + 0x02C76, + 0x02C76, + 0x02C77, + 0x02C78, + 0x02C79, + 0x02C7A, + 0x02C7B, + 0x02C7C, + 0x02C7D, + 0x0023F, + 0x00240, }; -static const uint32_t nxt_unicode_block_059[128] nxt_aligned(64) = { - 0x02C81, 0x02C81, 0x02C83, 0x02C83, 0x02C85, 0x02C85, 0x02C87, 0x02C87, - 0x02C89, 0x02C89, 0x02C8B, 0x02C8B, 0x02C8D, 0x02C8D, 0x02C8F, 0x02C8F, - 0x02C91, 0x02C91, 0x02C93, 0x02C93, 0x02C95, 0x02C95, 0x02C97, 0x02C97, - 0x02C99, 0x02C99, 0x02C9B, 0x02C9B, 0x02C9D, 0x02C9D, 0x02C9F, 0x02C9F, - 0x02CA1, 0x02CA1, 0x02CA3, 0x02CA3, 0x02CA5, 0x02CA5, 0x02CA7, 0x02CA7, - 0x02CA9, 0x02CA9, 0x02CAB, 0x02CAB, 0x02CAD, 0x02CAD, 0x02CAF, 0x02CAF, - 0x02CB1, 0x02CB1, 0x02CB3, 0x02CB3, 0x02CB5, 0x02CB5, 0x02CB7, 0x02CB7, - 0x02CB9, 0x02CB9, 0x02CBB, 0x02CBB, 0x02CBD, 0x02CBD, 0x02CBF, 0x02CBF, - 0x02CC1, 0x02CC1, 0x02CC3, 0x02CC3, 0x02CC5, 0x02CC5, 0x02CC7, 0x02CC7, - 0x02CC9, 0x02CC9, 0x02CCB, 0x02CCB, 0x02CCD, 0x02CCD, 0x02CCF, 0x02CCF, - 0x02CD1, 0x02CD1, 0x02CD3, 0x02CD3, 0x02CD5, 0x02CD5, 0x02CD7, 0x02CD7, - 0x02CD9, 0x02CD9, 0x02CDB, 0x02CDB, 0x02CDD, 0x02CDD, 0x02CDF, 0x02CDF, - 0x02CE1, 0x02CE1, 0x02CE3, 0x02CE3, 0x02CE4, 0x02CE5, 0x02CE6, 0x02CE7, - 0x02CE8, 0x02CE9, 0x02CEA, 0x02CEC, 0x02CEC, 0x02CEE, 0x02CEE, 0x02CEF, - 0x02CF0, 0x02CF1, 0x02CF3, 0x02CF3, 0x02CF4, 0x02CF5, 0x02CF6, 0x02CF7, - 0x02CF8, 0x02CF9, 0x02CFA, 0x02CFB, 0x02CFC, 0x02CFD, 0x02CFE, 0x02CFF, +static const uint32_t nxt_unicode_block_059[128] nxt_aligned(64) = { + 0x02C81, + 0x02C81, + 0x02C83, + 0x02C83, + 0x02C85, + 0x02C85, + 0x02C87, + 0x02C87, + 0x02C89, + 0x02C89, + 0x02C8B, + 0x02C8B, + 0x02C8D, + 0x02C8D, + 0x02C8F, + 0x02C8F, + 0x02C91, + 0x02C91, + 0x02C93, + 0x02C93, + 0x02C95, + 0x02C95, + 0x02C97, + 0x02C97, + 0x02C99, + 0x02C99, + 0x02C9B, + 0x02C9B, + 0x02C9D, + 0x02C9D, + 0x02C9F, + 0x02C9F, + 0x02CA1, + 0x02CA1, + 0x02CA3, + 0x02CA3, + 0x02CA5, + 0x02CA5, + 0x02CA7, + 0x02CA7, + 0x02CA9, + 0x02CA9, + 0x02CAB, + 0x02CAB, + 0x02CAD, + 0x02CAD, + 0x02CAF, + 0x02CAF, + 0x02CB1, + 0x02CB1, + 0x02CB3, + 0x02CB3, + 0x02CB5, + 0x02CB5, + 0x02CB7, + 0x02CB7, + 0x02CB9, + 0x02CB9, + 0x02CBB, + 0x02CBB, + 0x02CBD, + 0x02CBD, + 0x02CBF, + 0x02CBF, + 0x02CC1, + 0x02CC1, + 0x02CC3, + 0x02CC3, + 0x02CC5, + 0x02CC5, + 0x02CC7, + 0x02CC7, + 0x02CC9, + 0x02CC9, + 0x02CCB, + 0x02CCB, + 0x02CCD, + 0x02CCD, + 0x02CCF, + 0x02CCF, + 0x02CD1, + 0x02CD1, + 0x02CD3, + 0x02CD3, + 0x02CD5, + 0x02CD5, + 0x02CD7, + 0x02CD7, + 0x02CD9, + 0x02CD9, + 0x02CDB, + 0x02CDB, + 0x02CDD, + 0x02CDD, + 0x02CDF, + 0x02CDF, + 0x02CE1, + 0x02CE1, + 0x02CE3, + 0x02CE3, + 0x02CE4, + 0x02CE5, + 0x02CE6, + 0x02CE7, + 0x02CE8, + 0x02CE9, + 0x02CEA, + 0x02CEC, + 0x02CEC, + 0x02CEE, + 0x02CEE, + 0x02CEF, + 0x02CF0, + 0x02CF1, + 0x02CF3, + 0x02CF3, + 0x02CF4, + 0x02CF5, + 0x02CF6, + 0x02CF7, + 0x02CF8, + 0x02CF9, + 0x02CFA, + 0x02CFB, + 0x02CFC, + 0x02CFD, + 0x02CFE, + 0x02CFF, }; -static const uint32_t nxt_unicode_block_14c[128] nxt_aligned(64) = { - 0x0A600, 0x0A601, 0x0A602, 0x0A603, 0x0A604, 0x0A605, 0x0A606, 0x0A607, - 0x0A608, 0x0A609, 0x0A60A, 0x0A60B, 0x0A60C, 0x0A60D, 0x0A60E, 0x0A60F, - 0x0A610, 0x0A611, 0x0A612, 0x0A613, 0x0A614, 0x0A615, 0x0A616, 0x0A617, - 0x0A618, 0x0A619, 0x0A61A, 0x0A61B, 0x0A61C, 0x0A61D, 0x0A61E, 0x0A61F, - 0x0A620, 0x0A621, 0x0A622, 0x0A623, 0x0A624, 0x0A625, 0x0A626, 0x0A627, - 0x0A628, 0x0A629, 0x0A62A, 0x0A62B, 0x0A62C, 0x0A62D, 0x0A62E, 0x0A62F, - 0x0A630, 0x0A631, 0x0A632, 0x0A633, 0x0A634, 0x0A635, 0x0A636, 0x0A637, - 0x0A638, 0x0A639, 0x0A63A, 0x0A63B, 0x0A63C, 0x0A63D, 0x0A63E, 0x0A63F, - 0x0A641, 0x0A641, 0x0A643, 0x0A643, 0x0A645, 0x0A645, 0x0A647, 0x0A647, - 0x0A649, 0x0A649, 0x0A64B, 0x0A64B, 0x0A64D, 0x0A64D, 0x0A64F, 0x0A64F, - 0x0A651, 0x0A651, 0x0A653, 0x0A653, 0x0A655, 0x0A655, 0x0A657, 0x0A657, - 0x0A659, 0x0A659, 0x0A65B, 0x0A65B, 0x0A65D, 0x0A65D, 0x0A65F, 0x0A65F, - 0x0A661, 0x0A661, 0x0A663, 0x0A663, 0x0A665, 0x0A665, 0x0A667, 0x0A667, - 0x0A669, 0x0A669, 0x0A66B, 0x0A66B, 0x0A66D, 0x0A66D, 0x0A66E, 0x0A66F, - 0x0A670, 0x0A671, 0x0A672, 0x0A673, 0x0A674, 0x0A675, 0x0A676, 0x0A677, - 0x0A678, 0x0A679, 0x0A67A, 0x0A67B, 0x0A67C, 0x0A67D, 0x0A67E, 0x0A67F, +static const uint32_t nxt_unicode_block_14c[128] nxt_aligned(64) = { + 0x0A600, + 0x0A601, + 0x0A602, + 0x0A603, + 0x0A604, + 0x0A605, + 0x0A606, + 0x0A607, + 0x0A608, + 0x0A609, + 0x0A60A, + 0x0A60B, + 0x0A60C, + 0x0A60D, + 0x0A60E, + 0x0A60F, + 0x0A610, + 0x0A611, + 0x0A612, + 0x0A613, + 0x0A614, + 0x0A615, + 0x0A616, + 0x0A617, + 0x0A618, + 0x0A619, + 0x0A61A, + 0x0A61B, + 0x0A61C, + 0x0A61D, + 0x0A61E, + 0x0A61F, + 0x0A620, + 0x0A621, + 0x0A622, + 0x0A623, + 0x0A624, + 0x0A625, + 0x0A626, + 0x0A627, + 0x0A628, + 0x0A629, + 0x0A62A, + 0x0A62B, + 0x0A62C, + 0x0A62D, + 0x0A62E, + 0x0A62F, + 0x0A630, + 0x0A631, + 0x0A632, + 0x0A633, + 0x0A634, + 0x0A635, + 0x0A636, + 0x0A637, + 0x0A638, + 0x0A639, + 0x0A63A, + 0x0A63B, + 0x0A63C, + 0x0A63D, + 0x0A63E, + 0x0A63F, + 0x0A641, + 0x0A641, + 0x0A643, + 0x0A643, + 0x0A645, + 0x0A645, + 0x0A647, + 0x0A647, + 0x0A649, + 0x0A649, + 0x0A64B, + 0x0A64B, + 0x0A64D, + 0x0A64D, + 0x0A64F, + 0x0A64F, + 0x0A651, + 0x0A651, + 0x0A653, + 0x0A653, + 0x0A655, + 0x0A655, + 0x0A657, + 0x0A657, + 0x0A659, + 0x0A659, + 0x0A65B, + 0x0A65B, + 0x0A65D, + 0x0A65D, + 0x0A65F, + 0x0A65F, + 0x0A661, + 0x0A661, + 0x0A663, + 0x0A663, + 0x0A665, + 0x0A665, + 0x0A667, + 0x0A667, + 0x0A669, + 0x0A669, + 0x0A66B, + 0x0A66B, + 0x0A66D, + 0x0A66D, + 0x0A66E, + 0x0A66F, + 0x0A670, + 0x0A671, + 0x0A672, + 0x0A673, + 0x0A674, + 0x0A675, + 0x0A676, + 0x0A677, + 0x0A678, + 0x0A679, + 0x0A67A, + 0x0A67B, + 0x0A67C, + 0x0A67D, + 0x0A67E, + 0x0A67F, }; -static const uint32_t nxt_unicode_block_14d[128] nxt_aligned(64) = { - 0x0A681, 0x0A681, 0x0A683, 0x0A683, 0x0A685, 0x0A685, 0x0A687, 0x0A687, - 0x0A689, 0x0A689, 0x0A68B, 0x0A68B, 0x0A68D, 0x0A68D, 0x0A68F, 0x0A68F, - 0x0A691, 0x0A691, 0x0A693, 0x0A693, 0x0A695, 0x0A695, 0x0A697, 0x0A697, - 0x0A698, 0x0A699, 0x0A69A, 0x0A69B, 0x0A69C, 0x0A69D, 0x0A69E, 0x0A69F, - 0x0A6A0, 0x0A6A1, 0x0A6A2, 0x0A6A3, 0x0A6A4, 0x0A6A5, 0x0A6A6, 0x0A6A7, - 0x0A6A8, 0x0A6A9, 0x0A6AA, 0x0A6AB, 0x0A6AC, 0x0A6AD, 0x0A6AE, 0x0A6AF, - 0x0A6B0, 0x0A6B1, 0x0A6B2, 0x0A6B3, 0x0A6B4, 0x0A6B5, 0x0A6B6, 0x0A6B7, - 0x0A6B8, 0x0A6B9, 0x0A6BA, 0x0A6BB, 0x0A6BC, 0x0A6BD, 0x0A6BE, 0x0A6BF, - 0x0A6C0, 0x0A6C1, 0x0A6C2, 0x0A6C3, 0x0A6C4, 0x0A6C5, 0x0A6C6, 0x0A6C7, - 0x0A6C8, 0x0A6C9, 0x0A6CA, 0x0A6CB, 0x0A6CC, 0x0A6CD, 0x0A6CE, 0x0A6CF, - 0x0A6D0, 0x0A6D1, 0x0A6D2, 0x0A6D3, 0x0A6D4, 0x0A6D5, 0x0A6D6, 0x0A6D7, - 0x0A6D8, 0x0A6D9, 0x0A6DA, 0x0A6DB, 0x0A6DC, 0x0A6DD, 0x0A6DE, 0x0A6DF, - 0x0A6E0, 0x0A6E1, 0x0A6E2, 0x0A6E3, 0x0A6E4, 0x0A6E5, 0x0A6E6, 0x0A6E7, - 0x0A6E8, 0x0A6E9, 0x0A6EA, 0x0A6EB, 0x0A6EC, 0x0A6ED, 0x0A6EE, 0x0A6EF, - 0x0A6F0, 0x0A6F1, 0x0A6F2, 0x0A6F3, 0x0A6F4, 0x0A6F5, 0x0A6F6, 0x0A6F7, - 0x0A6F8, 0x0A6F9, 0x0A6FA, 0x0A6FB, 0x0A6FC, 0x0A6FD, 0x0A6FE, 0x0A6FF, +static const uint32_t nxt_unicode_block_14d[128] nxt_aligned(64) = { + 0x0A681, + 0x0A681, + 0x0A683, + 0x0A683, + 0x0A685, + 0x0A685, + 0x0A687, + 0x0A687, + 0x0A689, + 0x0A689, + 0x0A68B, + 0x0A68B, + 0x0A68D, + 0x0A68D, + 0x0A68F, + 0x0A68F, + 0x0A691, + 0x0A691, + 0x0A693, + 0x0A693, + 0x0A695, + 0x0A695, + 0x0A697, + 0x0A697, + 0x0A698, + 0x0A699, + 0x0A69A, + 0x0A69B, + 0x0A69C, + 0x0A69D, + 0x0A69E, + 0x0A69F, + 0x0A6A0, + 0x0A6A1, + 0x0A6A2, + 0x0A6A3, + 0x0A6A4, + 0x0A6A5, + 0x0A6A6, + 0x0A6A7, + 0x0A6A8, + 0x0A6A9, + 0x0A6AA, + 0x0A6AB, + 0x0A6AC, + 0x0A6AD, + 0x0A6AE, + 0x0A6AF, + 0x0A6B0, + 0x0A6B1, + 0x0A6B2, + 0x0A6B3, + 0x0A6B4, + 0x0A6B5, + 0x0A6B6, + 0x0A6B7, + 0x0A6B8, + 0x0A6B9, + 0x0A6BA, + 0x0A6BB, + 0x0A6BC, + 0x0A6BD, + 0x0A6BE, + 0x0A6BF, + 0x0A6C0, + 0x0A6C1, + 0x0A6C2, + 0x0A6C3, + 0x0A6C4, + 0x0A6C5, + 0x0A6C6, + 0x0A6C7, + 0x0A6C8, + 0x0A6C9, + 0x0A6CA, + 0x0A6CB, + 0x0A6CC, + 0x0A6CD, + 0x0A6CE, + 0x0A6CF, + 0x0A6D0, + 0x0A6D1, + 0x0A6D2, + 0x0A6D3, + 0x0A6D4, + 0x0A6D5, + 0x0A6D6, + 0x0A6D7, + 0x0A6D8, + 0x0A6D9, + 0x0A6DA, + 0x0A6DB, + 0x0A6DC, + 0x0A6DD, + 0x0A6DE, + 0x0A6DF, + 0x0A6E0, + 0x0A6E1, + 0x0A6E2, + 0x0A6E3, + 0x0A6E4, + 0x0A6E5, + 0x0A6E6, + 0x0A6E7, + 0x0A6E8, + 0x0A6E9, + 0x0A6EA, + 0x0A6EB, + 0x0A6EC, + 0x0A6ED, + 0x0A6EE, + 0x0A6EF, + 0x0A6F0, + 0x0A6F1, + 0x0A6F2, + 0x0A6F3, + 0x0A6F4, + 0x0A6F5, + 0x0A6F6, + 0x0A6F7, + 0x0A6F8, + 0x0A6F9, + 0x0A6FA, + 0x0A6FB, + 0x0A6FC, + 0x0A6FD, + 0x0A6FE, + 0x0A6FF, }; -static const uint32_t nxt_unicode_block_14e[128] nxt_aligned(64) = { - 0x0A700, 0x0A701, 0x0A702, 0x0A703, 0x0A704, 0x0A705, 0x0A706, 0x0A707, - 0x0A708, 0x0A709, 0x0A70A, 0x0A70B, 0x0A70C, 0x0A70D, 0x0A70E, 0x0A70F, - 0x0A710, 0x0A711, 0x0A712, 0x0A713, 0x0A714, 0x0A715, 0x0A716, 0x0A717, - 0x0A718, 0x0A719, 0x0A71A, 0x0A71B, 0x0A71C, 0x0A71D, 0x0A71E, 0x0A71F, - 0x0A720, 0x0A721, 0x0A723, 0x0A723, 0x0A725, 0x0A725, 0x0A727, 0x0A727, - 0x0A729, 0x0A729, 0x0A72B, 0x0A72B, 0x0A72D, 0x0A72D, 0x0A72F, 0x0A72F, - 0x0A730, 0x0A731, 0x0A733, 0x0A733, 0x0A735, 0x0A735, 0x0A737, 0x0A737, - 0x0A739, 0x0A739, 0x0A73B, 0x0A73B, 0x0A73D, 0x0A73D, 0x0A73F, 0x0A73F, - 0x0A741, 0x0A741, 0x0A743, 0x0A743, 0x0A745, 0x0A745, 0x0A747, 0x0A747, - 0x0A749, 0x0A749, 0x0A74B, 0x0A74B, 0x0A74D, 0x0A74D, 0x0A74F, 0x0A74F, - 0x0A751, 0x0A751, 0x0A753, 0x0A753, 0x0A755, 0x0A755, 0x0A757, 0x0A757, - 0x0A759, 0x0A759, 0x0A75B, 0x0A75B, 0x0A75D, 0x0A75D, 0x0A75F, 0x0A75F, - 0x0A761, 0x0A761, 0x0A763, 0x0A763, 0x0A765, 0x0A765, 0x0A767, 0x0A767, - 0x0A769, 0x0A769, 0x0A76B, 0x0A76B, 0x0A76D, 0x0A76D, 0x0A76F, 0x0A76F, - 0x0A770, 0x0A771, 0x0A772, 0x0A773, 0x0A774, 0x0A775, 0x0A776, 0x0A777, - 0x0A778, 0x0A77A, 0x0A77A, 0x0A77C, 0x0A77C, 0x01D79, 0x0A77F, 0x0A77F, +static const uint32_t nxt_unicode_block_14e[128] nxt_aligned(64) = { + 0x0A700, + 0x0A701, + 0x0A702, + 0x0A703, + 0x0A704, + 0x0A705, + 0x0A706, + 0x0A707, + 0x0A708, + 0x0A709, + 0x0A70A, + 0x0A70B, + 0x0A70C, + 0x0A70D, + 0x0A70E, + 0x0A70F, + 0x0A710, + 0x0A711, + 0x0A712, + 0x0A713, + 0x0A714, + 0x0A715, + 0x0A716, + 0x0A717, + 0x0A718, + 0x0A719, + 0x0A71A, + 0x0A71B, + 0x0A71C, + 0x0A71D, + 0x0A71E, + 0x0A71F, + 0x0A720, + 0x0A721, + 0x0A723, + 0x0A723, + 0x0A725, + 0x0A725, + 0x0A727, + 0x0A727, + 0x0A729, + 0x0A729, + 0x0A72B, + 0x0A72B, + 0x0A72D, + 0x0A72D, + 0x0A72F, + 0x0A72F, + 0x0A730, + 0x0A731, + 0x0A733, + 0x0A733, + 0x0A735, + 0x0A735, + 0x0A737, + 0x0A737, + 0x0A739, + 0x0A739, + 0x0A73B, + 0x0A73B, + 0x0A73D, + 0x0A73D, + 0x0A73F, + 0x0A73F, + 0x0A741, + 0x0A741, + 0x0A743, + 0x0A743, + 0x0A745, + 0x0A745, + 0x0A747, + 0x0A747, + 0x0A749, + 0x0A749, + 0x0A74B, + 0x0A74B, + 0x0A74D, + 0x0A74D, + 0x0A74F, + 0x0A74F, + 0x0A751, + 0x0A751, + 0x0A753, + 0x0A753, + 0x0A755, + 0x0A755, + 0x0A757, + 0x0A757, + 0x0A759, + 0x0A759, + 0x0A75B, + 0x0A75B, + 0x0A75D, + 0x0A75D, + 0x0A75F, + 0x0A75F, + 0x0A761, + 0x0A761, + 0x0A763, + 0x0A763, + 0x0A765, + 0x0A765, + 0x0A767, + 0x0A767, + 0x0A769, + 0x0A769, + 0x0A76B, + 0x0A76B, + 0x0A76D, + 0x0A76D, + 0x0A76F, + 0x0A76F, + 0x0A770, + 0x0A771, + 0x0A772, + 0x0A773, + 0x0A774, + 0x0A775, + 0x0A776, + 0x0A777, + 0x0A778, + 0x0A77A, + 0x0A77A, + 0x0A77C, + 0x0A77C, + 0x01D79, + 0x0A77F, + 0x0A77F, }; -static const uint32_t nxt_unicode_block_14f[128] nxt_aligned(64) = { - 0x0A781, 0x0A781, 0x0A783, 0x0A783, 0x0A785, 0x0A785, 0x0A787, 0x0A787, - 0x0A788, 0x0A789, 0x0A78A, 0x0A78C, 0x0A78C, 0x00265, 0x0A78E, 0x0A78F, - 0x0A791, 0x0A791, 0x0A793, 0x0A793, 0x0A794, 0x0A795, 0x0A796, 0x0A797, - 0x0A798, 0x0A799, 0x0A79A, 0x0A79B, 0x0A79C, 0x0A79D, 0x0A79E, 0x0A79F, - 0x0A7A1, 0x0A7A1, 0x0A7A3, 0x0A7A3, 0x0A7A5, 0x0A7A5, 0x0A7A7, 0x0A7A7, - 0x0A7A9, 0x0A7A9, 0x00266, 0x0A7AB, 0x0A7AC, 0x0A7AD, 0x0A7AE, 0x0A7AF, - 0x0A7B0, 0x0A7B1, 0x0A7B2, 0x0A7B3, 0x0A7B4, 0x0A7B5, 0x0A7B6, 0x0A7B7, - 0x0A7B8, 0x0A7B9, 0x0A7BA, 0x0A7BB, 0x0A7BC, 0x0A7BD, 0x0A7BE, 0x0A7BF, - 0x0A7C0, 0x0A7C1, 0x0A7C2, 0x0A7C3, 0x0A7C4, 0x0A7C5, 0x0A7C6, 0x0A7C7, - 0x0A7C8, 0x0A7C9, 0x0A7CA, 0x0A7CB, 0x0A7CC, 0x0A7CD, 0x0A7CE, 0x0A7CF, - 0x0A7D0, 0x0A7D1, 0x0A7D2, 0x0A7D3, 0x0A7D4, 0x0A7D5, 0x0A7D6, 0x0A7D7, - 0x0A7D8, 0x0A7D9, 0x0A7DA, 0x0A7DB, 0x0A7DC, 0x0A7DD, 0x0A7DE, 0x0A7DF, - 0x0A7E0, 0x0A7E1, 0x0A7E2, 0x0A7E3, 0x0A7E4, 0x0A7E5, 0x0A7E6, 0x0A7E7, - 0x0A7E8, 0x0A7E9, 0x0A7EA, 0x0A7EB, 0x0A7EC, 0x0A7ED, 0x0A7EE, 0x0A7EF, - 0x0A7F0, 0x0A7F1, 0x0A7F2, 0x0A7F3, 0x0A7F4, 0x0A7F5, 0x0A7F6, 0x0A7F7, - 0x0A7F8, 0x0A7F9, 0x0A7FA, 0x0A7FB, 0x0A7FC, 0x0A7FD, 0x0A7FE, 0x0A7FF, +static const uint32_t nxt_unicode_block_14f[128] nxt_aligned(64) = { + 0x0A781, + 0x0A781, + 0x0A783, + 0x0A783, + 0x0A785, + 0x0A785, + 0x0A787, + 0x0A787, + 0x0A788, + 0x0A789, + 0x0A78A, + 0x0A78C, + 0x0A78C, + 0x00265, + 0x0A78E, + 0x0A78F, + 0x0A791, + 0x0A791, + 0x0A793, + 0x0A793, + 0x0A794, + 0x0A795, + 0x0A796, + 0x0A797, + 0x0A798, + 0x0A799, + 0x0A79A, + 0x0A79B, + 0x0A79C, + 0x0A79D, + 0x0A79E, + 0x0A79F, + 0x0A7A1, + 0x0A7A1, + 0x0A7A3, + 0x0A7A3, + 0x0A7A5, + 0x0A7A5, + 0x0A7A7, + 0x0A7A7, + 0x0A7A9, + 0x0A7A9, + 0x00266, + 0x0A7AB, + 0x0A7AC, + 0x0A7AD, + 0x0A7AE, + 0x0A7AF, + 0x0A7B0, + 0x0A7B1, + 0x0A7B2, + 0x0A7B3, + 0x0A7B4, + 0x0A7B5, + 0x0A7B6, + 0x0A7B7, + 0x0A7B8, + 0x0A7B9, + 0x0A7BA, + 0x0A7BB, + 0x0A7BC, + 0x0A7BD, + 0x0A7BE, + 0x0A7BF, + 0x0A7C0, + 0x0A7C1, + 0x0A7C2, + 0x0A7C3, + 0x0A7C4, + 0x0A7C5, + 0x0A7C6, + 0x0A7C7, + 0x0A7C8, + 0x0A7C9, + 0x0A7CA, + 0x0A7CB, + 0x0A7CC, + 0x0A7CD, + 0x0A7CE, + 0x0A7CF, + 0x0A7D0, + 0x0A7D1, + 0x0A7D2, + 0x0A7D3, + 0x0A7D4, + 0x0A7D5, + 0x0A7D6, + 0x0A7D7, + 0x0A7D8, + 0x0A7D9, + 0x0A7DA, + 0x0A7DB, + 0x0A7DC, + 0x0A7DD, + 0x0A7DE, + 0x0A7DF, + 0x0A7E0, + 0x0A7E1, + 0x0A7E2, + 0x0A7E3, + 0x0A7E4, + 0x0A7E5, + 0x0A7E6, + 0x0A7E7, + 0x0A7E8, + 0x0A7E9, + 0x0A7EA, + 0x0A7EB, + 0x0A7EC, + 0x0A7ED, + 0x0A7EE, + 0x0A7EF, + 0x0A7F0, + 0x0A7F1, + 0x0A7F2, + 0x0A7F3, + 0x0A7F4, + 0x0A7F5, + 0x0A7F6, + 0x0A7F7, + 0x0A7F8, + 0x0A7F9, + 0x0A7FA, + 0x0A7FB, + 0x0A7FC, + 0x0A7FD, + 0x0A7FE, + 0x0A7FF, }; -static const uint32_t nxt_unicode_block_1fe[128] nxt_aligned(64) = { - 0x0FF00, 0x0FF01, 0x0FF02, 0x0FF03, 0x0FF04, 0x0FF05, 0x0FF06, 0x0FF07, - 0x0FF08, 0x0FF09, 0x0FF0A, 0x0FF0B, 0x0FF0C, 0x0FF0D, 0x0FF0E, 0x0FF0F, - 0x0FF10, 0x0FF11, 0x0FF12, 0x0FF13, 0x0FF14, 0x0FF15, 0x0FF16, 0x0FF17, - 0x0FF18, 0x0FF19, 0x0FF1A, 0x0FF1B, 0x0FF1C, 0x0FF1D, 0x0FF1E, 0x0FF1F, - 0x0FF20, 0x0FF41, 0x0FF42, 0x0FF43, 0x0FF44, 0x0FF45, 0x0FF46, 0x0FF47, - 0x0FF48, 0x0FF49, 0x0FF4A, 0x0FF4B, 0x0FF4C, 0x0FF4D, 0x0FF4E, 0x0FF4F, - 0x0FF50, 0x0FF51, 0x0FF52, 0x0FF53, 0x0FF54, 0x0FF55, 0x0FF56, 0x0FF57, - 0x0FF58, 0x0FF59, 0x0FF5A, 0x0FF3B, 0x0FF3C, 0x0FF3D, 0x0FF3E, 0x0FF3F, - 0x0FF40, 0x0FF41, 0x0FF42, 0x0FF43, 0x0FF44, 0x0FF45, 0x0FF46, 0x0FF47, - 0x0FF48, 0x0FF49, 0x0FF4A, 0x0FF4B, 0x0FF4C, 0x0FF4D, 0x0FF4E, 0x0FF4F, - 0x0FF50, 0x0FF51, 0x0FF52, 0x0FF53, 0x0FF54, 0x0FF55, 0x0FF56, 0x0FF57, - 0x0FF58, 0x0FF59, 0x0FF5A, 0x0FF5B, 0x0FF5C, 0x0FF5D, 0x0FF5E, 0x0FF5F, - 0x0FF60, 0x0FF61, 0x0FF62, 0x0FF63, 0x0FF64, 0x0FF65, 0x0FF66, 0x0FF67, - 0x0FF68, 0x0FF69, 0x0FF6A, 0x0FF6B, 0x0FF6C, 0x0FF6D, 0x0FF6E, 0x0FF6F, - 0x0FF70, 0x0FF71, 0x0FF72, 0x0FF73, 0x0FF74, 0x0FF75, 0x0FF76, 0x0FF77, - 0x0FF78, 0x0FF79, 0x0FF7A, 0x0FF7B, 0x0FF7C, 0x0FF7D, 0x0FF7E, 0x0FF7F, +static const uint32_t nxt_unicode_block_1fe[128] nxt_aligned(64) = { + 0x0FF00, + 0x0FF01, + 0x0FF02, + 0x0FF03, + 0x0FF04, + 0x0FF05, + 0x0FF06, + 0x0FF07, + 0x0FF08, + 0x0FF09, + 0x0FF0A, + 0x0FF0B, + 0x0FF0C, + 0x0FF0D, + 0x0FF0E, + 0x0FF0F, + 0x0FF10, + 0x0FF11, + 0x0FF12, + 0x0FF13, + 0x0FF14, + 0x0FF15, + 0x0FF16, + 0x0FF17, + 0x0FF18, + 0x0FF19, + 0x0FF1A, + 0x0FF1B, + 0x0FF1C, + 0x0FF1D, + 0x0FF1E, + 0x0FF1F, + 0x0FF20, + 0x0FF41, + 0x0FF42, + 0x0FF43, + 0x0FF44, + 0x0FF45, + 0x0FF46, + 0x0FF47, + 0x0FF48, + 0x0FF49, + 0x0FF4A, + 0x0FF4B, + 0x0FF4C, + 0x0FF4D, + 0x0FF4E, + 0x0FF4F, + 0x0FF50, + 0x0FF51, + 0x0FF52, + 0x0FF53, + 0x0FF54, + 0x0FF55, + 0x0FF56, + 0x0FF57, + 0x0FF58, + 0x0FF59, + 0x0FF5A, + 0x0FF3B, + 0x0FF3C, + 0x0FF3D, + 0x0FF3E, + 0x0FF3F, + 0x0FF40, + 0x0FF41, + 0x0FF42, + 0x0FF43, + 0x0FF44, + 0x0FF45, + 0x0FF46, + 0x0FF47, + 0x0FF48, + 0x0FF49, + 0x0FF4A, + 0x0FF4B, + 0x0FF4C, + 0x0FF4D, + 0x0FF4E, + 0x0FF4F, + 0x0FF50, + 0x0FF51, + 0x0FF52, + 0x0FF53, + 0x0FF54, + 0x0FF55, + 0x0FF56, + 0x0FF57, + 0x0FF58, + 0x0FF59, + 0x0FF5A, + 0x0FF5B, + 0x0FF5C, + 0x0FF5D, + 0x0FF5E, + 0x0FF5F, + 0x0FF60, + 0x0FF61, + 0x0FF62, + 0x0FF63, + 0x0FF64, + 0x0FF65, + 0x0FF66, + 0x0FF67, + 0x0FF68, + 0x0FF69, + 0x0FF6A, + 0x0FF6B, + 0x0FF6C, + 0x0FF6D, + 0x0FF6E, + 0x0FF6F, + 0x0FF70, + 0x0FF71, + 0x0FF72, + 0x0FF73, + 0x0FF74, + 0x0FF75, + 0x0FF76, + 0x0FF77, + 0x0FF78, + 0x0FF79, + 0x0FF7A, + 0x0FF7B, + 0x0FF7C, + 0x0FF7D, + 0x0FF7E, + 0x0FF7F, }; -static const uint32_t nxt_unicode_block_208[40] nxt_aligned(64) = { - 0x10428, 0x10429, 0x1042A, 0x1042B, 0x1042C, 0x1042D, 0x1042E, 0x1042F, - 0x10430, 0x10431, 0x10432, 0x10433, 0x10434, 0x10435, 0x10436, 0x10437, - 0x10438, 0x10439, 0x1043A, 0x1043B, 0x1043C, 0x1043D, 0x1043E, 0x1043F, - 0x10440, 0x10441, 0x10442, 0x10443, 0x10444, 0x10445, 0x10446, 0x10447, - 0x10448, 0x10449, 0x1044A, 0x1044B, 0x1044C, 0x1044D, 0x1044E, 0x1044F, +static const uint32_t nxt_unicode_block_208[40] nxt_aligned(64) = { + 0x10428, + 0x10429, + 0x1042A, + 0x1042B, + 0x1042C, + 0x1042D, + 0x1042E, + 0x1042F, + 0x10430, + 0x10431, + 0x10432, + 0x10433, + 0x10434, + 0x10435, + 0x10436, + 0x10437, + 0x10438, + 0x10439, + 0x1043A, + 0x1043B, + 0x1043C, + 0x1043D, + 0x1043E, + 0x1043F, + 0x10440, + 0x10441, + 0x10442, + 0x10443, + 0x10444, + 0x10445, + 0x10446, + 0x10447, + 0x10448, + 0x10449, + 0x1044A, + 0x1044B, + 0x1044C, + 0x1044D, + 0x1044E, + 0x1044F, }; -static const uint32_t *nxt_unicode_blocks[] nxt_aligned(64) = { +static const uint32_t *nxt_unicode_blocks[] nxt_aligned(64) = { nxt_unicode_block_000, nxt_unicode_block_001, nxt_unicode_block_002, diff --git a/src/nxt_unicode_macosx_lowcase.h b/src/nxt_unicode_macosx_lowcase.h index d0cc15186..e9060973a 100644 --- a/src/nxt_unicode_macosx_lowcase.h +++ b/src/nxt_unicode_macosx_lowcase.h @@ -1,307 +1,1925 @@ - /* * 15 128-bytes blocks, 511 pointers. * 9267 bytes on 32-bit platforms, 11307 bytes on 64-bit platforms. */ -#define NXT_UNICODE_MAX_LOWCASE 0x0FF3A - -#define NXT_UNICODE_BLOCK_SIZE 128 - - -static const uint32_t nxt_unicode_block_000[128] nxt_aligned(64) = { - 0x00000, 0x00001, 0x00002, 0x00003, 0x00004, 0x00005, 0x00006, 0x00007, - 0x00008, 0x00009, 0x0000A, 0x0000B, 0x0000C, 0x0000D, 0x0000E, 0x0000F, - 0x00010, 0x00011, 0x00012, 0x00013, 0x00014, 0x00015, 0x00016, 0x00017, - 0x00018, 0x00019, 0x0001A, 0x0001B, 0x0001C, 0x0001D, 0x0001E, 0x0001F, - 0x00020, 0x00021, 0x00022, 0x00023, 0x00024, 0x00025, 0x00026, 0x00027, - 0x00028, 0x00029, 0x0002A, 0x0002B, 0x0002C, 0x0002D, 0x0002E, 0x0002F, - 0x00030, 0x00031, 0x00032, 0x00033, 0x00034, 0x00035, 0x00036, 0x00037, - 0x00038, 0x00039, 0x0003A, 0x0003B, 0x0003C, 0x0003D, 0x0003E, 0x0003F, - 0x00040, 0x00061, 0x00062, 0x00063, 0x00064, 0x00065, 0x00066, 0x00067, - 0x00068, 0x00069, 0x0006A, 0x0006B, 0x0006C, 0x0006D, 0x0006E, 0x0006F, - 0x00070, 0x00071, 0x00072, 0x00073, 0x00074, 0x00075, 0x00076, 0x00077, - 0x00078, 0x00079, 0x0007A, 0x0005B, 0x0005C, 0x0005D, 0x0005E, 0x0005F, - 0x00060, 0x00061, 0x00062, 0x00063, 0x00064, 0x00065, 0x00066, 0x00067, - 0x00068, 0x00069, 0x0006A, 0x0006B, 0x0006C, 0x0006D, 0x0006E, 0x0006F, - 0x00070, 0x00071, 0x00072, 0x00073, 0x00074, 0x00075, 0x00076, 0x00077, - 0x00078, 0x00079, 0x0007A, 0x0007B, 0x0007C, 0x0007D, 0x0007E, 0x0007F, +#define NXT_UNICODE_MAX_LOWCASE 0x0FF3A + +#define NXT_UNICODE_BLOCK_SIZE 128 + + +static const uint32_t nxt_unicode_block_000[128] nxt_aligned(64) = { + 0x00000, + 0x00001, + 0x00002, + 0x00003, + 0x00004, + 0x00005, + 0x00006, + 0x00007, + 0x00008, + 0x00009, + 0x0000A, + 0x0000B, + 0x0000C, + 0x0000D, + 0x0000E, + 0x0000F, + 0x00010, + 0x00011, + 0x00012, + 0x00013, + 0x00014, + 0x00015, + 0x00016, + 0x00017, + 0x00018, + 0x00019, + 0x0001A, + 0x0001B, + 0x0001C, + 0x0001D, + 0x0001E, + 0x0001F, + 0x00020, + 0x00021, + 0x00022, + 0x00023, + 0x00024, + 0x00025, + 0x00026, + 0x00027, + 0x00028, + 0x00029, + 0x0002A, + 0x0002B, + 0x0002C, + 0x0002D, + 0x0002E, + 0x0002F, + 0x00030, + 0x00031, + 0x00032, + 0x00033, + 0x00034, + 0x00035, + 0x00036, + 0x00037, + 0x00038, + 0x00039, + 0x0003A, + 0x0003B, + 0x0003C, + 0x0003D, + 0x0003E, + 0x0003F, + 0x00040, + 0x00061, + 0x00062, + 0x00063, + 0x00064, + 0x00065, + 0x00066, + 0x00067, + 0x00068, + 0x00069, + 0x0006A, + 0x0006B, + 0x0006C, + 0x0006D, + 0x0006E, + 0x0006F, + 0x00070, + 0x00071, + 0x00072, + 0x00073, + 0x00074, + 0x00075, + 0x00076, + 0x00077, + 0x00078, + 0x00079, + 0x0007A, + 0x0005B, + 0x0005C, + 0x0005D, + 0x0005E, + 0x0005F, + 0x00060, + 0x00061, + 0x00062, + 0x00063, + 0x00064, + 0x00065, + 0x00066, + 0x00067, + 0x00068, + 0x00069, + 0x0006A, + 0x0006B, + 0x0006C, + 0x0006D, + 0x0006E, + 0x0006F, + 0x00070, + 0x00071, + 0x00072, + 0x00073, + 0x00074, + 0x00075, + 0x00076, + 0x00077, + 0x00078, + 0x00079, + 0x0007A, + 0x0007B, + 0x0007C, + 0x0007D, + 0x0007E, + 0x0007F, }; -static const uint32_t nxt_unicode_block_001[128] nxt_aligned(64) = { - 0x00080, 0x00081, 0x00082, 0x00083, 0x00084, 0x00085, 0x00086, 0x00087, - 0x00088, 0x00089, 0x0008A, 0x0008B, 0x0008C, 0x0008D, 0x0008E, 0x0008F, - 0x00090, 0x00091, 0x00092, 0x00093, 0x00094, 0x00095, 0x00096, 0x00097, - 0x00098, 0x00099, 0x0009A, 0x0009B, 0x0009C, 0x0009D, 0x0009E, 0x0009F, - 0x000A0, 0x000A1, 0x000A2, 0x000A3, 0x000A4, 0x000A5, 0x000A6, 0x000A7, - 0x000A8, 0x000A9, 0x000AA, 0x000AB, 0x000AC, 0x000AD, 0x000AE, 0x000AF, - 0x000B0, 0x000B1, 0x000B2, 0x000B3, 0x000B4, 0x000B5, 0x000B6, 0x000B7, - 0x000B8, 0x000B9, 0x000BA, 0x000BB, 0x000BC, 0x000BD, 0x000BE, 0x000BF, - 0x000E0, 0x000E1, 0x000E2, 0x000E3, 0x000E4, 0x000E5, 0x000E6, 0x000E7, - 0x000E8, 0x000E9, 0x000EA, 0x000EB, 0x000EC, 0x000ED, 0x000EE, 0x000EF, - 0x000F0, 0x000F1, 0x000F2, 0x000F3, 0x000F4, 0x000F5, 0x000F6, 0x000D7, - 0x000F8, 0x000F9, 0x000FA, 0x000FB, 0x000FC, 0x000FD, 0x000FE, 0x000DF, - 0x000E0, 0x000E1, 0x000E2, 0x000E3, 0x000E4, 0x000E5, 0x000E6, 0x000E7, - 0x000E8, 0x000E9, 0x000EA, 0x000EB, 0x000EC, 0x000ED, 0x000EE, 0x000EF, - 0x000F0, 0x000F1, 0x000F2, 0x000F3, 0x000F4, 0x000F5, 0x000F6, 0x000F7, - 0x000F8, 0x000F9, 0x000FA, 0x000FB, 0x000FC, 0x000FD, 0x000FE, 0x000FF, +static const uint32_t nxt_unicode_block_001[128] nxt_aligned(64) = { + 0x00080, + 0x00081, + 0x00082, + 0x00083, + 0x00084, + 0x00085, + 0x00086, + 0x00087, + 0x00088, + 0x00089, + 0x0008A, + 0x0008B, + 0x0008C, + 0x0008D, + 0x0008E, + 0x0008F, + 0x00090, + 0x00091, + 0x00092, + 0x00093, + 0x00094, + 0x00095, + 0x00096, + 0x00097, + 0x00098, + 0x00099, + 0x0009A, + 0x0009B, + 0x0009C, + 0x0009D, + 0x0009E, + 0x0009F, + 0x000A0, + 0x000A1, + 0x000A2, + 0x000A3, + 0x000A4, + 0x000A5, + 0x000A6, + 0x000A7, + 0x000A8, + 0x000A9, + 0x000AA, + 0x000AB, + 0x000AC, + 0x000AD, + 0x000AE, + 0x000AF, + 0x000B0, + 0x000B1, + 0x000B2, + 0x000B3, + 0x000B4, + 0x000B5, + 0x000B6, + 0x000B7, + 0x000B8, + 0x000B9, + 0x000BA, + 0x000BB, + 0x000BC, + 0x000BD, + 0x000BE, + 0x000BF, + 0x000E0, + 0x000E1, + 0x000E2, + 0x000E3, + 0x000E4, + 0x000E5, + 0x000E6, + 0x000E7, + 0x000E8, + 0x000E9, + 0x000EA, + 0x000EB, + 0x000EC, + 0x000ED, + 0x000EE, + 0x000EF, + 0x000F0, + 0x000F1, + 0x000F2, + 0x000F3, + 0x000F4, + 0x000F5, + 0x000F6, + 0x000D7, + 0x000F8, + 0x000F9, + 0x000FA, + 0x000FB, + 0x000FC, + 0x000FD, + 0x000FE, + 0x000DF, + 0x000E0, + 0x000E1, + 0x000E2, + 0x000E3, + 0x000E4, + 0x000E5, + 0x000E6, + 0x000E7, + 0x000E8, + 0x000E9, + 0x000EA, + 0x000EB, + 0x000EC, + 0x000ED, + 0x000EE, + 0x000EF, + 0x000F0, + 0x000F1, + 0x000F2, + 0x000F3, + 0x000F4, + 0x000F5, + 0x000F6, + 0x000F7, + 0x000F8, + 0x000F9, + 0x000FA, + 0x000FB, + 0x000FC, + 0x000FD, + 0x000FE, + 0x000FF, }; -static const uint32_t nxt_unicode_block_002[128] nxt_aligned(64) = { - 0x00101, 0x00101, 0x00103, 0x00103, 0x00105, 0x00105, 0x00107, 0x00107, - 0x00109, 0x00109, 0x0010B, 0x0010B, 0x0010D, 0x0010D, 0x0010F, 0x0010F, - 0x00111, 0x00111, 0x00113, 0x00113, 0x00115, 0x00115, 0x00117, 0x00117, - 0x00119, 0x00119, 0x0011B, 0x0011B, 0x0011D, 0x0011D, 0x0011F, 0x0011F, - 0x00121, 0x00121, 0x00123, 0x00123, 0x00125, 0x00125, 0x00127, 0x00127, - 0x00129, 0x00129, 0x0012B, 0x0012B, 0x0012D, 0x0012D, 0x0012F, 0x0012F, - 0x00130, 0x00131, 0x00133, 0x00133, 0x00135, 0x00135, 0x00137, 0x00137, - 0x00138, 0x0013A, 0x0013A, 0x0013C, 0x0013C, 0x0013E, 0x0013E, 0x00140, - 0x00140, 0x00142, 0x00142, 0x00144, 0x00144, 0x00146, 0x00146, 0x00148, - 0x00148, 0x00149, 0x0014B, 0x0014B, 0x0014D, 0x0014D, 0x0014F, 0x0014F, - 0x00151, 0x00151, 0x00153, 0x00153, 0x00155, 0x00155, 0x00157, 0x00157, - 0x00159, 0x00159, 0x0015B, 0x0015B, 0x0015D, 0x0015D, 0x0015F, 0x0015F, - 0x00161, 0x00161, 0x00163, 0x00163, 0x00165, 0x00165, 0x00167, 0x00167, - 0x00169, 0x00169, 0x0016B, 0x0016B, 0x0016D, 0x0016D, 0x0016F, 0x0016F, - 0x00171, 0x00171, 0x00173, 0x00173, 0x00175, 0x00175, 0x00177, 0x00177, - 0x000FF, 0x0017A, 0x0017A, 0x0017C, 0x0017C, 0x0017E, 0x0017E, 0x0017F, +static const uint32_t nxt_unicode_block_002[128] nxt_aligned(64) = { + 0x00101, + 0x00101, + 0x00103, + 0x00103, + 0x00105, + 0x00105, + 0x00107, + 0x00107, + 0x00109, + 0x00109, + 0x0010B, + 0x0010B, + 0x0010D, + 0x0010D, + 0x0010F, + 0x0010F, + 0x00111, + 0x00111, + 0x00113, + 0x00113, + 0x00115, + 0x00115, + 0x00117, + 0x00117, + 0x00119, + 0x00119, + 0x0011B, + 0x0011B, + 0x0011D, + 0x0011D, + 0x0011F, + 0x0011F, + 0x00121, + 0x00121, + 0x00123, + 0x00123, + 0x00125, + 0x00125, + 0x00127, + 0x00127, + 0x00129, + 0x00129, + 0x0012B, + 0x0012B, + 0x0012D, + 0x0012D, + 0x0012F, + 0x0012F, + 0x00130, + 0x00131, + 0x00133, + 0x00133, + 0x00135, + 0x00135, + 0x00137, + 0x00137, + 0x00138, + 0x0013A, + 0x0013A, + 0x0013C, + 0x0013C, + 0x0013E, + 0x0013E, + 0x00140, + 0x00140, + 0x00142, + 0x00142, + 0x00144, + 0x00144, + 0x00146, + 0x00146, + 0x00148, + 0x00148, + 0x00149, + 0x0014B, + 0x0014B, + 0x0014D, + 0x0014D, + 0x0014F, + 0x0014F, + 0x00151, + 0x00151, + 0x00153, + 0x00153, + 0x00155, + 0x00155, + 0x00157, + 0x00157, + 0x00159, + 0x00159, + 0x0015B, + 0x0015B, + 0x0015D, + 0x0015D, + 0x0015F, + 0x0015F, + 0x00161, + 0x00161, + 0x00163, + 0x00163, + 0x00165, + 0x00165, + 0x00167, + 0x00167, + 0x00169, + 0x00169, + 0x0016B, + 0x0016B, + 0x0016D, + 0x0016D, + 0x0016F, + 0x0016F, + 0x00171, + 0x00171, + 0x00173, + 0x00173, + 0x00175, + 0x00175, + 0x00177, + 0x00177, + 0x000FF, + 0x0017A, + 0x0017A, + 0x0017C, + 0x0017C, + 0x0017E, + 0x0017E, + 0x0017F, }; -static const uint32_t nxt_unicode_block_003[128] nxt_aligned(64) = { - 0x00180, 0x00253, 0x00183, 0x00183, 0x00185, 0x00185, 0x00254, 0x00188, - 0x00188, 0x00256, 0x00257, 0x0018C, 0x0018C, 0x0018D, 0x001DD, 0x00259, - 0x0025B, 0x00192, 0x00192, 0x00260, 0x00263, 0x00195, 0x00269, 0x00268, - 0x00199, 0x00199, 0x0019A, 0x0019B, 0x0026F, 0x00272, 0x0019E, 0x00275, - 0x001A1, 0x001A1, 0x001A3, 0x001A3, 0x001A5, 0x001A5, 0x001A6, 0x001A8, - 0x001A8, 0x00283, 0x001AA, 0x001AB, 0x001AD, 0x001AD, 0x00288, 0x001B0, - 0x001B0, 0x0028A, 0x0028B, 0x001B4, 0x001B4, 0x001B6, 0x001B6, 0x00292, - 0x001B9, 0x001B9, 0x001BA, 0x001BB, 0x001BD, 0x001BD, 0x001BE, 0x001BF, - 0x001C0, 0x001C1, 0x001C2, 0x001C3, 0x001C6, 0x001C6, 0x001C6, 0x001C9, - 0x001C9, 0x001C9, 0x001CC, 0x001CC, 0x001CC, 0x001CE, 0x001CE, 0x001D0, - 0x001D0, 0x001D2, 0x001D2, 0x001D4, 0x001D4, 0x001D6, 0x001D6, 0x001D8, - 0x001D8, 0x001DA, 0x001DA, 0x001DC, 0x001DC, 0x001DD, 0x001DF, 0x001DF, - 0x001E1, 0x001E1, 0x001E3, 0x001E3, 0x001E5, 0x001E5, 0x001E7, 0x001E7, - 0x001E9, 0x001E9, 0x001EB, 0x001EB, 0x001ED, 0x001ED, 0x001EF, 0x001EF, - 0x001F0, 0x001F3, 0x001F3, 0x001F3, 0x001F5, 0x001F5, 0x001F6, 0x001F7, - 0x001F9, 0x001F9, 0x001FB, 0x001FB, 0x001FD, 0x001FD, 0x001FF, 0x001FF, +static const uint32_t nxt_unicode_block_003[128] nxt_aligned(64) = { + 0x00180, + 0x00253, + 0x00183, + 0x00183, + 0x00185, + 0x00185, + 0x00254, + 0x00188, + 0x00188, + 0x00256, + 0x00257, + 0x0018C, + 0x0018C, + 0x0018D, + 0x001DD, + 0x00259, + 0x0025B, + 0x00192, + 0x00192, + 0x00260, + 0x00263, + 0x00195, + 0x00269, + 0x00268, + 0x00199, + 0x00199, + 0x0019A, + 0x0019B, + 0x0026F, + 0x00272, + 0x0019E, + 0x00275, + 0x001A1, + 0x001A1, + 0x001A3, + 0x001A3, + 0x001A5, + 0x001A5, + 0x001A6, + 0x001A8, + 0x001A8, + 0x00283, + 0x001AA, + 0x001AB, + 0x001AD, + 0x001AD, + 0x00288, + 0x001B0, + 0x001B0, + 0x0028A, + 0x0028B, + 0x001B4, + 0x001B4, + 0x001B6, + 0x001B6, + 0x00292, + 0x001B9, + 0x001B9, + 0x001BA, + 0x001BB, + 0x001BD, + 0x001BD, + 0x001BE, + 0x001BF, + 0x001C0, + 0x001C1, + 0x001C2, + 0x001C3, + 0x001C6, + 0x001C6, + 0x001C6, + 0x001C9, + 0x001C9, + 0x001C9, + 0x001CC, + 0x001CC, + 0x001CC, + 0x001CE, + 0x001CE, + 0x001D0, + 0x001D0, + 0x001D2, + 0x001D2, + 0x001D4, + 0x001D4, + 0x001D6, + 0x001D6, + 0x001D8, + 0x001D8, + 0x001DA, + 0x001DA, + 0x001DC, + 0x001DC, + 0x001DD, + 0x001DF, + 0x001DF, + 0x001E1, + 0x001E1, + 0x001E3, + 0x001E3, + 0x001E5, + 0x001E5, + 0x001E7, + 0x001E7, + 0x001E9, + 0x001E9, + 0x001EB, + 0x001EB, + 0x001ED, + 0x001ED, + 0x001EF, + 0x001EF, + 0x001F0, + 0x001F3, + 0x001F3, + 0x001F3, + 0x001F5, + 0x001F5, + 0x001F6, + 0x001F7, + 0x001F9, + 0x001F9, + 0x001FB, + 0x001FB, + 0x001FD, + 0x001FD, + 0x001FF, + 0x001FF, }; -static const uint32_t nxt_unicode_block_004[128] nxt_aligned(64) = { - 0x00201, 0x00201, 0x00203, 0x00203, 0x00205, 0x00205, 0x00207, 0x00207, - 0x00209, 0x00209, 0x0020B, 0x0020B, 0x0020D, 0x0020D, 0x0020F, 0x0020F, - 0x00211, 0x00211, 0x00213, 0x00213, 0x00215, 0x00215, 0x00217, 0x00217, - 0x00219, 0x00219, 0x0021B, 0x0021B, 0x0021C, 0x0021D, 0x0021F, 0x0021F, - 0x00220, 0x00221, 0x00222, 0x00223, 0x00224, 0x00225, 0x00227, 0x00227, - 0x00229, 0x00229, 0x0022B, 0x0022B, 0x0022D, 0x0022D, 0x0022F, 0x0022F, - 0x00231, 0x00231, 0x00233, 0x00233, 0x00234, 0x00235, 0x00236, 0x00237, - 0x00238, 0x00239, 0x0023A, 0x0023B, 0x0023C, 0x0023D, 0x0023E, 0x0023F, - 0x00240, 0x00241, 0x00242, 0x00243, 0x00244, 0x00245, 0x00246, 0x00247, - 0x00248, 0x00249, 0x0024A, 0x0024B, 0x0024C, 0x0024D, 0x0024E, 0x0024F, - 0x00250, 0x00251, 0x00252, 0x00253, 0x00254, 0x00255, 0x00256, 0x00257, - 0x00258, 0x00259, 0x0025A, 0x0025B, 0x0025C, 0x0025D, 0x0025E, 0x0025F, - 0x00260, 0x00261, 0x00262, 0x00263, 0x00264, 0x00265, 0x00266, 0x00267, - 0x00268, 0x00269, 0x0026A, 0x0026B, 0x0026C, 0x0026D, 0x0026E, 0x0026F, - 0x00270, 0x00271, 0x00272, 0x00273, 0x00274, 0x00275, 0x00276, 0x00277, - 0x00278, 0x00279, 0x0027A, 0x0027B, 0x0027C, 0x0027D, 0x0027E, 0x0027F, +static const uint32_t nxt_unicode_block_004[128] nxt_aligned(64) = { + 0x00201, + 0x00201, + 0x00203, + 0x00203, + 0x00205, + 0x00205, + 0x00207, + 0x00207, + 0x00209, + 0x00209, + 0x0020B, + 0x0020B, + 0x0020D, + 0x0020D, + 0x0020F, + 0x0020F, + 0x00211, + 0x00211, + 0x00213, + 0x00213, + 0x00215, + 0x00215, + 0x00217, + 0x00217, + 0x00219, + 0x00219, + 0x0021B, + 0x0021B, + 0x0021C, + 0x0021D, + 0x0021F, + 0x0021F, + 0x00220, + 0x00221, + 0x00222, + 0x00223, + 0x00224, + 0x00225, + 0x00227, + 0x00227, + 0x00229, + 0x00229, + 0x0022B, + 0x0022B, + 0x0022D, + 0x0022D, + 0x0022F, + 0x0022F, + 0x00231, + 0x00231, + 0x00233, + 0x00233, + 0x00234, + 0x00235, + 0x00236, + 0x00237, + 0x00238, + 0x00239, + 0x0023A, + 0x0023B, + 0x0023C, + 0x0023D, + 0x0023E, + 0x0023F, + 0x00240, + 0x00241, + 0x00242, + 0x00243, + 0x00244, + 0x00245, + 0x00246, + 0x00247, + 0x00248, + 0x00249, + 0x0024A, + 0x0024B, + 0x0024C, + 0x0024D, + 0x0024E, + 0x0024F, + 0x00250, + 0x00251, + 0x00252, + 0x00253, + 0x00254, + 0x00255, + 0x00256, + 0x00257, + 0x00258, + 0x00259, + 0x0025A, + 0x0025B, + 0x0025C, + 0x0025D, + 0x0025E, + 0x0025F, + 0x00260, + 0x00261, + 0x00262, + 0x00263, + 0x00264, + 0x00265, + 0x00266, + 0x00267, + 0x00268, + 0x00269, + 0x0026A, + 0x0026B, + 0x0026C, + 0x0026D, + 0x0026E, + 0x0026F, + 0x00270, + 0x00271, + 0x00272, + 0x00273, + 0x00274, + 0x00275, + 0x00276, + 0x00277, + 0x00278, + 0x00279, + 0x0027A, + 0x0027B, + 0x0027C, + 0x0027D, + 0x0027E, + 0x0027F, }; -static const uint32_t nxt_unicode_block_007[128] nxt_aligned(64) = { - 0x00380, 0x00381, 0x00382, 0x00383, 0x00384, 0x00385, 0x003AC, 0x00387, - 0x003AD, 0x003AE, 0x003AF, 0x0038B, 0x003CC, 0x0038D, 0x003CD, 0x003CE, - 0x00390, 0x003B1, 0x003B2, 0x003B3, 0x003B4, 0x003B5, 0x003B6, 0x003B7, - 0x003B8, 0x003B9, 0x003BA, 0x003BB, 0x003BC, 0x003BD, 0x003BE, 0x003BF, - 0x003C0, 0x003C1, 0x003A2, 0x003C3, 0x003C4, 0x003C5, 0x003C6, 0x003C7, - 0x003C8, 0x003C9, 0x003CA, 0x003CB, 0x003AC, 0x003AD, 0x003AE, 0x003AF, - 0x003B0, 0x003B1, 0x003B2, 0x003B3, 0x003B4, 0x003B5, 0x003B6, 0x003B7, - 0x003B8, 0x003B9, 0x003BA, 0x003BB, 0x003BC, 0x003BD, 0x003BE, 0x003BF, - 0x003C0, 0x003C1, 0x003C2, 0x003C3, 0x003C4, 0x003C5, 0x003C6, 0x003C7, - 0x003C8, 0x003C9, 0x003CA, 0x003CB, 0x003CC, 0x003CD, 0x003CE, 0x003CF, - 0x003D0, 0x003D1, 0x003D2, 0x003D3, 0x003D4, 0x003D5, 0x003D6, 0x003D7, - 0x003D8, 0x003D9, 0x003DA, 0x003DB, 0x003DC, 0x003DD, 0x003DE, 0x003DF, - 0x003E0, 0x003E1, 0x003E3, 0x003E3, 0x003E5, 0x003E5, 0x003E7, 0x003E7, - 0x003E9, 0x003E9, 0x003EB, 0x003EB, 0x003ED, 0x003ED, 0x003EF, 0x003EF, - 0x003F0, 0x003F1, 0x003F2, 0x003F3, 0x003F4, 0x003F5, 0x003F6, 0x003F7, - 0x003F8, 0x003F9, 0x003FA, 0x003FB, 0x003FC, 0x003FD, 0x003FE, 0x003FF, +static const uint32_t nxt_unicode_block_007[128] nxt_aligned(64) = { + 0x00380, + 0x00381, + 0x00382, + 0x00383, + 0x00384, + 0x00385, + 0x003AC, + 0x00387, + 0x003AD, + 0x003AE, + 0x003AF, + 0x0038B, + 0x003CC, + 0x0038D, + 0x003CD, + 0x003CE, + 0x00390, + 0x003B1, + 0x003B2, + 0x003B3, + 0x003B4, + 0x003B5, + 0x003B6, + 0x003B7, + 0x003B8, + 0x003B9, + 0x003BA, + 0x003BB, + 0x003BC, + 0x003BD, + 0x003BE, + 0x003BF, + 0x003C0, + 0x003C1, + 0x003A2, + 0x003C3, + 0x003C4, + 0x003C5, + 0x003C6, + 0x003C7, + 0x003C8, + 0x003C9, + 0x003CA, + 0x003CB, + 0x003AC, + 0x003AD, + 0x003AE, + 0x003AF, + 0x003B0, + 0x003B1, + 0x003B2, + 0x003B3, + 0x003B4, + 0x003B5, + 0x003B6, + 0x003B7, + 0x003B8, + 0x003B9, + 0x003BA, + 0x003BB, + 0x003BC, + 0x003BD, + 0x003BE, + 0x003BF, + 0x003C0, + 0x003C1, + 0x003C2, + 0x003C3, + 0x003C4, + 0x003C5, + 0x003C6, + 0x003C7, + 0x003C8, + 0x003C9, + 0x003CA, + 0x003CB, + 0x003CC, + 0x003CD, + 0x003CE, + 0x003CF, + 0x003D0, + 0x003D1, + 0x003D2, + 0x003D3, + 0x003D4, + 0x003D5, + 0x003D6, + 0x003D7, + 0x003D8, + 0x003D9, + 0x003DA, + 0x003DB, + 0x003DC, + 0x003DD, + 0x003DE, + 0x003DF, + 0x003E0, + 0x003E1, + 0x003E3, + 0x003E3, + 0x003E5, + 0x003E5, + 0x003E7, + 0x003E7, + 0x003E9, + 0x003E9, + 0x003EB, + 0x003EB, + 0x003ED, + 0x003ED, + 0x003EF, + 0x003EF, + 0x003F0, + 0x003F1, + 0x003F2, + 0x003F3, + 0x003F4, + 0x003F5, + 0x003F6, + 0x003F7, + 0x003F8, + 0x003F9, + 0x003FA, + 0x003FB, + 0x003FC, + 0x003FD, + 0x003FE, + 0x003FF, }; -static const uint32_t nxt_unicode_block_008[128] nxt_aligned(64) = { - 0x00450, 0x00451, 0x00452, 0x00453, 0x00454, 0x00455, 0x00456, 0x00457, - 0x00458, 0x00459, 0x0045A, 0x0045B, 0x0045C, 0x0045D, 0x0045E, 0x0045F, - 0x00430, 0x00431, 0x00432, 0x00433, 0x00434, 0x00435, 0x00436, 0x00437, - 0x00438, 0x00439, 0x0043A, 0x0043B, 0x0043C, 0x0043D, 0x0043E, 0x0043F, - 0x00440, 0x00441, 0x00442, 0x00443, 0x00444, 0x00445, 0x00446, 0x00447, - 0x00448, 0x00449, 0x0044A, 0x0044B, 0x0044C, 0x0044D, 0x0044E, 0x0044F, - 0x00430, 0x00431, 0x00432, 0x00433, 0x00434, 0x00435, 0x00436, 0x00437, - 0x00438, 0x00439, 0x0043A, 0x0043B, 0x0043C, 0x0043D, 0x0043E, 0x0043F, - 0x00440, 0x00441, 0x00442, 0x00443, 0x00444, 0x00445, 0x00446, 0x00447, - 0x00448, 0x00449, 0x0044A, 0x0044B, 0x0044C, 0x0044D, 0x0044E, 0x0044F, - 0x00450, 0x00451, 0x00452, 0x00453, 0x00454, 0x00455, 0x00456, 0x00457, - 0x00458, 0x00459, 0x0045A, 0x0045B, 0x0045C, 0x0045D, 0x0045E, 0x0045F, - 0x00461, 0x00461, 0x00463, 0x00463, 0x00465, 0x00465, 0x00467, 0x00467, - 0x00469, 0x00469, 0x0046B, 0x0046B, 0x0046D, 0x0046D, 0x0046F, 0x0046F, - 0x00471, 0x00471, 0x00473, 0x00473, 0x00475, 0x00475, 0x00477, 0x00477, - 0x00479, 0x00479, 0x0047B, 0x0047B, 0x0047D, 0x0047D, 0x0047F, 0x0047F, +static const uint32_t nxt_unicode_block_008[128] nxt_aligned(64) = { + 0x00450, + 0x00451, + 0x00452, + 0x00453, + 0x00454, + 0x00455, + 0x00456, + 0x00457, + 0x00458, + 0x00459, + 0x0045A, + 0x0045B, + 0x0045C, + 0x0045D, + 0x0045E, + 0x0045F, + 0x00430, + 0x00431, + 0x00432, + 0x00433, + 0x00434, + 0x00435, + 0x00436, + 0x00437, + 0x00438, + 0x00439, + 0x0043A, + 0x0043B, + 0x0043C, + 0x0043D, + 0x0043E, + 0x0043F, + 0x00440, + 0x00441, + 0x00442, + 0x00443, + 0x00444, + 0x00445, + 0x00446, + 0x00447, + 0x00448, + 0x00449, + 0x0044A, + 0x0044B, + 0x0044C, + 0x0044D, + 0x0044E, + 0x0044F, + 0x00430, + 0x00431, + 0x00432, + 0x00433, + 0x00434, + 0x00435, + 0x00436, + 0x00437, + 0x00438, + 0x00439, + 0x0043A, + 0x0043B, + 0x0043C, + 0x0043D, + 0x0043E, + 0x0043F, + 0x00440, + 0x00441, + 0x00442, + 0x00443, + 0x00444, + 0x00445, + 0x00446, + 0x00447, + 0x00448, + 0x00449, + 0x0044A, + 0x0044B, + 0x0044C, + 0x0044D, + 0x0044E, + 0x0044F, + 0x00450, + 0x00451, + 0x00452, + 0x00453, + 0x00454, + 0x00455, + 0x00456, + 0x00457, + 0x00458, + 0x00459, + 0x0045A, + 0x0045B, + 0x0045C, + 0x0045D, + 0x0045E, + 0x0045F, + 0x00461, + 0x00461, + 0x00463, + 0x00463, + 0x00465, + 0x00465, + 0x00467, + 0x00467, + 0x00469, + 0x00469, + 0x0046B, + 0x0046B, + 0x0046D, + 0x0046D, + 0x0046F, + 0x0046F, + 0x00471, + 0x00471, + 0x00473, + 0x00473, + 0x00475, + 0x00475, + 0x00477, + 0x00477, + 0x00479, + 0x00479, + 0x0047B, + 0x0047B, + 0x0047D, + 0x0047D, + 0x0047F, + 0x0047F, }; -static const uint32_t nxt_unicode_block_009[128] nxt_aligned(64) = { - 0x00481, 0x00481, 0x00482, 0x00483, 0x00484, 0x00485, 0x00486, 0x00487, - 0x00488, 0x00489, 0x0048A, 0x0048B, 0x0048C, 0x0048D, 0x0048E, 0x0048F, - 0x00491, 0x00491, 0x00493, 0x00493, 0x00495, 0x00495, 0x00497, 0x00497, - 0x00499, 0x00499, 0x0049B, 0x0049B, 0x0049D, 0x0049D, 0x0049F, 0x0049F, - 0x004A1, 0x004A1, 0x004A3, 0x004A3, 0x004A5, 0x004A5, 0x004A7, 0x004A7, - 0x004A9, 0x004A9, 0x004AB, 0x004AB, 0x004AD, 0x004AD, 0x004AF, 0x004AF, - 0x004B1, 0x004B1, 0x004B3, 0x004B3, 0x004B5, 0x004B5, 0x004B7, 0x004B7, - 0x004B9, 0x004B9, 0x004BB, 0x004BB, 0x004BD, 0x004BD, 0x004BF, 0x004BF, - 0x004C0, 0x004C2, 0x004C2, 0x004C4, 0x004C4, 0x004C5, 0x004C6, 0x004C8, - 0x004C8, 0x004C9, 0x004CA, 0x004CC, 0x004CC, 0x004CD, 0x004CE, 0x004CF, - 0x004D1, 0x004D1, 0x004D3, 0x004D3, 0x004D4, 0x004D5, 0x004D7, 0x004D7, - 0x004D8, 0x004D9, 0x004DA, 0x004DB, 0x004DD, 0x004DD, 0x004DF, 0x004DF, - 0x004E0, 0x004E1, 0x004E3, 0x004E3, 0x004E5, 0x004E5, 0x004E7, 0x004E7, - 0x004E8, 0x004E9, 0x004EA, 0x004EB, 0x004ED, 0x004ED, 0x004EF, 0x004EF, - 0x004F1, 0x004F1, 0x004F3, 0x004F3, 0x004F5, 0x004F5, 0x004F6, 0x004F7, - 0x004F9, 0x004F9, 0x004FA, 0x004FB, 0x004FC, 0x004FD, 0x004FE, 0x004FF, +static const uint32_t nxt_unicode_block_009[128] nxt_aligned(64) = { + 0x00481, + 0x00481, + 0x00482, + 0x00483, + 0x00484, + 0x00485, + 0x00486, + 0x00487, + 0x00488, + 0x00489, + 0x0048A, + 0x0048B, + 0x0048C, + 0x0048D, + 0x0048E, + 0x0048F, + 0x00491, + 0x00491, + 0x00493, + 0x00493, + 0x00495, + 0x00495, + 0x00497, + 0x00497, + 0x00499, + 0x00499, + 0x0049B, + 0x0049B, + 0x0049D, + 0x0049D, + 0x0049F, + 0x0049F, + 0x004A1, + 0x004A1, + 0x004A3, + 0x004A3, + 0x004A5, + 0x004A5, + 0x004A7, + 0x004A7, + 0x004A9, + 0x004A9, + 0x004AB, + 0x004AB, + 0x004AD, + 0x004AD, + 0x004AF, + 0x004AF, + 0x004B1, + 0x004B1, + 0x004B3, + 0x004B3, + 0x004B5, + 0x004B5, + 0x004B7, + 0x004B7, + 0x004B9, + 0x004B9, + 0x004BB, + 0x004BB, + 0x004BD, + 0x004BD, + 0x004BF, + 0x004BF, + 0x004C0, + 0x004C2, + 0x004C2, + 0x004C4, + 0x004C4, + 0x004C5, + 0x004C6, + 0x004C8, + 0x004C8, + 0x004C9, + 0x004CA, + 0x004CC, + 0x004CC, + 0x004CD, + 0x004CE, + 0x004CF, + 0x004D1, + 0x004D1, + 0x004D3, + 0x004D3, + 0x004D4, + 0x004D5, + 0x004D7, + 0x004D7, + 0x004D8, + 0x004D9, + 0x004DA, + 0x004DB, + 0x004DD, + 0x004DD, + 0x004DF, + 0x004DF, + 0x004E0, + 0x004E1, + 0x004E3, + 0x004E3, + 0x004E5, + 0x004E5, + 0x004E7, + 0x004E7, + 0x004E8, + 0x004E9, + 0x004EA, + 0x004EB, + 0x004ED, + 0x004ED, + 0x004EF, + 0x004EF, + 0x004F1, + 0x004F1, + 0x004F3, + 0x004F3, + 0x004F5, + 0x004F5, + 0x004F6, + 0x004F7, + 0x004F9, + 0x004F9, + 0x004FA, + 0x004FB, + 0x004FC, + 0x004FD, + 0x004FE, + 0x004FF, }; -static const uint32_t nxt_unicode_block_00a[128] nxt_aligned(64) = { - 0x00500, 0x00501, 0x00502, 0x00503, 0x00504, 0x00505, 0x00506, 0x00507, - 0x00508, 0x00509, 0x0050A, 0x0050B, 0x0050C, 0x0050D, 0x0050E, 0x0050F, - 0x00510, 0x00511, 0x00512, 0x00513, 0x00514, 0x00515, 0x00516, 0x00517, - 0x00518, 0x00519, 0x0051A, 0x0051B, 0x0051C, 0x0051D, 0x0051E, 0x0051F, - 0x00520, 0x00521, 0x00522, 0x00523, 0x00524, 0x00525, 0x00526, 0x00527, - 0x00528, 0x00529, 0x0052A, 0x0052B, 0x0052C, 0x0052D, 0x0052E, 0x0052F, - 0x00530, 0x00561, 0x00562, 0x00563, 0x00564, 0x00565, 0x00566, 0x00567, - 0x00568, 0x00569, 0x0056A, 0x0056B, 0x0056C, 0x0056D, 0x0056E, 0x0056F, - 0x00570, 0x00571, 0x00572, 0x00573, 0x00574, 0x00575, 0x00576, 0x00577, - 0x00578, 0x00579, 0x0057A, 0x0057B, 0x0057C, 0x0057D, 0x0057E, 0x0057F, - 0x00580, 0x00581, 0x00582, 0x00583, 0x00584, 0x00585, 0x00586, 0x00557, - 0x00558, 0x00559, 0x0055A, 0x0055B, 0x0055C, 0x0055D, 0x0055E, 0x0055F, - 0x00560, 0x00561, 0x00562, 0x00563, 0x00564, 0x00565, 0x00566, 0x00567, - 0x00568, 0x00569, 0x0056A, 0x0056B, 0x0056C, 0x0056D, 0x0056E, 0x0056F, - 0x00570, 0x00571, 0x00572, 0x00573, 0x00574, 0x00575, 0x00576, 0x00577, - 0x00578, 0x00579, 0x0057A, 0x0057B, 0x0057C, 0x0057D, 0x0057E, 0x0057F, +static const uint32_t nxt_unicode_block_00a[128] nxt_aligned(64) = { + 0x00500, + 0x00501, + 0x00502, + 0x00503, + 0x00504, + 0x00505, + 0x00506, + 0x00507, + 0x00508, + 0x00509, + 0x0050A, + 0x0050B, + 0x0050C, + 0x0050D, + 0x0050E, + 0x0050F, + 0x00510, + 0x00511, + 0x00512, + 0x00513, + 0x00514, + 0x00515, + 0x00516, + 0x00517, + 0x00518, + 0x00519, + 0x0051A, + 0x0051B, + 0x0051C, + 0x0051D, + 0x0051E, + 0x0051F, + 0x00520, + 0x00521, + 0x00522, + 0x00523, + 0x00524, + 0x00525, + 0x00526, + 0x00527, + 0x00528, + 0x00529, + 0x0052A, + 0x0052B, + 0x0052C, + 0x0052D, + 0x0052E, + 0x0052F, + 0x00530, + 0x00561, + 0x00562, + 0x00563, + 0x00564, + 0x00565, + 0x00566, + 0x00567, + 0x00568, + 0x00569, + 0x0056A, + 0x0056B, + 0x0056C, + 0x0056D, + 0x0056E, + 0x0056F, + 0x00570, + 0x00571, + 0x00572, + 0x00573, + 0x00574, + 0x00575, + 0x00576, + 0x00577, + 0x00578, + 0x00579, + 0x0057A, + 0x0057B, + 0x0057C, + 0x0057D, + 0x0057E, + 0x0057F, + 0x00580, + 0x00581, + 0x00582, + 0x00583, + 0x00584, + 0x00585, + 0x00586, + 0x00557, + 0x00558, + 0x00559, + 0x0055A, + 0x0055B, + 0x0055C, + 0x0055D, + 0x0055E, + 0x0055F, + 0x00560, + 0x00561, + 0x00562, + 0x00563, + 0x00564, + 0x00565, + 0x00566, + 0x00567, + 0x00568, + 0x00569, + 0x0056A, + 0x0056B, + 0x0056C, + 0x0056D, + 0x0056E, + 0x0056F, + 0x00570, + 0x00571, + 0x00572, + 0x00573, + 0x00574, + 0x00575, + 0x00576, + 0x00577, + 0x00578, + 0x00579, + 0x0057A, + 0x0057B, + 0x0057C, + 0x0057D, + 0x0057E, + 0x0057F, }; -static const uint32_t nxt_unicode_block_03c[128] nxt_aligned(64) = { - 0x01E01, 0x01E01, 0x01E03, 0x01E03, 0x01E05, 0x01E05, 0x01E07, 0x01E07, - 0x01E09, 0x01E09, 0x01E0B, 0x01E0B, 0x01E0D, 0x01E0D, 0x01E0F, 0x01E0F, - 0x01E11, 0x01E11, 0x01E13, 0x01E13, 0x01E15, 0x01E15, 0x01E17, 0x01E17, - 0x01E19, 0x01E19, 0x01E1B, 0x01E1B, 0x01E1D, 0x01E1D, 0x01E1F, 0x01E1F, - 0x01E21, 0x01E21, 0x01E23, 0x01E23, 0x01E25, 0x01E25, 0x01E27, 0x01E27, - 0x01E29, 0x01E29, 0x01E2B, 0x01E2B, 0x01E2D, 0x01E2D, 0x01E2F, 0x01E2F, - 0x01E31, 0x01E31, 0x01E33, 0x01E33, 0x01E35, 0x01E35, 0x01E37, 0x01E37, - 0x01E39, 0x01E39, 0x01E3B, 0x01E3B, 0x01E3D, 0x01E3D, 0x01E3F, 0x01E3F, - 0x01E41, 0x01E41, 0x01E43, 0x01E43, 0x01E45, 0x01E45, 0x01E47, 0x01E47, - 0x01E49, 0x01E49, 0x01E4B, 0x01E4B, 0x01E4D, 0x01E4D, 0x01E4F, 0x01E4F, - 0x01E51, 0x01E51, 0x01E53, 0x01E53, 0x01E55, 0x01E55, 0x01E57, 0x01E57, - 0x01E59, 0x01E59, 0x01E5B, 0x01E5B, 0x01E5D, 0x01E5D, 0x01E5F, 0x01E5F, - 0x01E61, 0x01E61, 0x01E63, 0x01E63, 0x01E65, 0x01E65, 0x01E67, 0x01E67, - 0x01E69, 0x01E69, 0x01E6B, 0x01E6B, 0x01E6D, 0x01E6D, 0x01E6F, 0x01E6F, - 0x01E71, 0x01E71, 0x01E73, 0x01E73, 0x01E75, 0x01E75, 0x01E77, 0x01E77, - 0x01E79, 0x01E79, 0x01E7B, 0x01E7B, 0x01E7D, 0x01E7D, 0x01E7F, 0x01E7F, +static const uint32_t nxt_unicode_block_03c[128] nxt_aligned(64) = { + 0x01E01, + 0x01E01, + 0x01E03, + 0x01E03, + 0x01E05, + 0x01E05, + 0x01E07, + 0x01E07, + 0x01E09, + 0x01E09, + 0x01E0B, + 0x01E0B, + 0x01E0D, + 0x01E0D, + 0x01E0F, + 0x01E0F, + 0x01E11, + 0x01E11, + 0x01E13, + 0x01E13, + 0x01E15, + 0x01E15, + 0x01E17, + 0x01E17, + 0x01E19, + 0x01E19, + 0x01E1B, + 0x01E1B, + 0x01E1D, + 0x01E1D, + 0x01E1F, + 0x01E1F, + 0x01E21, + 0x01E21, + 0x01E23, + 0x01E23, + 0x01E25, + 0x01E25, + 0x01E27, + 0x01E27, + 0x01E29, + 0x01E29, + 0x01E2B, + 0x01E2B, + 0x01E2D, + 0x01E2D, + 0x01E2F, + 0x01E2F, + 0x01E31, + 0x01E31, + 0x01E33, + 0x01E33, + 0x01E35, + 0x01E35, + 0x01E37, + 0x01E37, + 0x01E39, + 0x01E39, + 0x01E3B, + 0x01E3B, + 0x01E3D, + 0x01E3D, + 0x01E3F, + 0x01E3F, + 0x01E41, + 0x01E41, + 0x01E43, + 0x01E43, + 0x01E45, + 0x01E45, + 0x01E47, + 0x01E47, + 0x01E49, + 0x01E49, + 0x01E4B, + 0x01E4B, + 0x01E4D, + 0x01E4D, + 0x01E4F, + 0x01E4F, + 0x01E51, + 0x01E51, + 0x01E53, + 0x01E53, + 0x01E55, + 0x01E55, + 0x01E57, + 0x01E57, + 0x01E59, + 0x01E59, + 0x01E5B, + 0x01E5B, + 0x01E5D, + 0x01E5D, + 0x01E5F, + 0x01E5F, + 0x01E61, + 0x01E61, + 0x01E63, + 0x01E63, + 0x01E65, + 0x01E65, + 0x01E67, + 0x01E67, + 0x01E69, + 0x01E69, + 0x01E6B, + 0x01E6B, + 0x01E6D, + 0x01E6D, + 0x01E6F, + 0x01E6F, + 0x01E71, + 0x01E71, + 0x01E73, + 0x01E73, + 0x01E75, + 0x01E75, + 0x01E77, + 0x01E77, + 0x01E79, + 0x01E79, + 0x01E7B, + 0x01E7B, + 0x01E7D, + 0x01E7D, + 0x01E7F, + 0x01E7F, }; -static const uint32_t nxt_unicode_block_03d[128] nxt_aligned(64) = { - 0x01E81, 0x01E81, 0x01E83, 0x01E83, 0x01E85, 0x01E85, 0x01E87, 0x01E87, - 0x01E89, 0x01E89, 0x01E8B, 0x01E8B, 0x01E8D, 0x01E8D, 0x01E8F, 0x01E8F, - 0x01E91, 0x01E91, 0x01E93, 0x01E93, 0x01E95, 0x01E95, 0x01E96, 0x01E97, - 0x01E98, 0x01E99, 0x01E9A, 0x01E9B, 0x01E9C, 0x01E9D, 0x01E9E, 0x01E9F, - 0x01EA1, 0x01EA1, 0x01EA3, 0x01EA3, 0x01EA5, 0x01EA5, 0x01EA7, 0x01EA7, - 0x01EA9, 0x01EA9, 0x01EAB, 0x01EAB, 0x01EAD, 0x01EAD, 0x01EAF, 0x01EAF, - 0x01EB1, 0x01EB1, 0x01EB3, 0x01EB3, 0x01EB5, 0x01EB5, 0x01EB7, 0x01EB7, - 0x01EB9, 0x01EB9, 0x01EBB, 0x01EBB, 0x01EBD, 0x01EBD, 0x01EBF, 0x01EBF, - 0x01EC1, 0x01EC1, 0x01EC3, 0x01EC3, 0x01EC5, 0x01EC5, 0x01EC7, 0x01EC7, - 0x01EC9, 0x01EC9, 0x01ECB, 0x01ECB, 0x01ECD, 0x01ECD, 0x01ECF, 0x01ECF, - 0x01ED1, 0x01ED1, 0x01ED3, 0x01ED3, 0x01ED5, 0x01ED5, 0x01ED7, 0x01ED7, - 0x01ED9, 0x01ED9, 0x01EDB, 0x01EDB, 0x01EDD, 0x01EDD, 0x01EDF, 0x01EDF, - 0x01EE1, 0x01EE1, 0x01EE3, 0x01EE3, 0x01EE5, 0x01EE5, 0x01EE7, 0x01EE7, - 0x01EE9, 0x01EE9, 0x01EEB, 0x01EEB, 0x01EED, 0x01EED, 0x01EEF, 0x01EEF, - 0x01EF1, 0x01EF1, 0x01EF3, 0x01EF3, 0x01EF5, 0x01EF5, 0x01EF7, 0x01EF7, - 0x01EF9, 0x01EF9, 0x01EFA, 0x01EFB, 0x01EFC, 0x01EFD, 0x01EFE, 0x01EFF, +static const uint32_t nxt_unicode_block_03d[128] nxt_aligned(64) = { + 0x01E81, + 0x01E81, + 0x01E83, + 0x01E83, + 0x01E85, + 0x01E85, + 0x01E87, + 0x01E87, + 0x01E89, + 0x01E89, + 0x01E8B, + 0x01E8B, + 0x01E8D, + 0x01E8D, + 0x01E8F, + 0x01E8F, + 0x01E91, + 0x01E91, + 0x01E93, + 0x01E93, + 0x01E95, + 0x01E95, + 0x01E96, + 0x01E97, + 0x01E98, + 0x01E99, + 0x01E9A, + 0x01E9B, + 0x01E9C, + 0x01E9D, + 0x01E9E, + 0x01E9F, + 0x01EA1, + 0x01EA1, + 0x01EA3, + 0x01EA3, + 0x01EA5, + 0x01EA5, + 0x01EA7, + 0x01EA7, + 0x01EA9, + 0x01EA9, + 0x01EAB, + 0x01EAB, + 0x01EAD, + 0x01EAD, + 0x01EAF, + 0x01EAF, + 0x01EB1, + 0x01EB1, + 0x01EB3, + 0x01EB3, + 0x01EB5, + 0x01EB5, + 0x01EB7, + 0x01EB7, + 0x01EB9, + 0x01EB9, + 0x01EBB, + 0x01EBB, + 0x01EBD, + 0x01EBD, + 0x01EBF, + 0x01EBF, + 0x01EC1, + 0x01EC1, + 0x01EC3, + 0x01EC3, + 0x01EC5, + 0x01EC5, + 0x01EC7, + 0x01EC7, + 0x01EC9, + 0x01EC9, + 0x01ECB, + 0x01ECB, + 0x01ECD, + 0x01ECD, + 0x01ECF, + 0x01ECF, + 0x01ED1, + 0x01ED1, + 0x01ED3, + 0x01ED3, + 0x01ED5, + 0x01ED5, + 0x01ED7, + 0x01ED7, + 0x01ED9, + 0x01ED9, + 0x01EDB, + 0x01EDB, + 0x01EDD, + 0x01EDD, + 0x01EDF, + 0x01EDF, + 0x01EE1, + 0x01EE1, + 0x01EE3, + 0x01EE3, + 0x01EE5, + 0x01EE5, + 0x01EE7, + 0x01EE7, + 0x01EE9, + 0x01EE9, + 0x01EEB, + 0x01EEB, + 0x01EED, + 0x01EED, + 0x01EEF, + 0x01EEF, + 0x01EF1, + 0x01EF1, + 0x01EF3, + 0x01EF3, + 0x01EF5, + 0x01EF5, + 0x01EF7, + 0x01EF7, + 0x01EF9, + 0x01EF9, + 0x01EFA, + 0x01EFB, + 0x01EFC, + 0x01EFD, + 0x01EFE, + 0x01EFF, }; -static const uint32_t nxt_unicode_block_03e[128] nxt_aligned(64) = { - 0x01F00, 0x01F01, 0x01F02, 0x01F03, 0x01F04, 0x01F05, 0x01F06, 0x01F07, - 0x01F00, 0x01F01, 0x01F02, 0x01F03, 0x01F04, 0x01F05, 0x01F06, 0x01F07, - 0x01F10, 0x01F11, 0x01F12, 0x01F13, 0x01F14, 0x01F15, 0x01F16, 0x01F17, - 0x01F10, 0x01F11, 0x01F12, 0x01F13, 0x01F14, 0x01F15, 0x01F1E, 0x01F1F, - 0x01F20, 0x01F21, 0x01F22, 0x01F23, 0x01F24, 0x01F25, 0x01F26, 0x01F27, - 0x01F20, 0x01F21, 0x01F22, 0x01F23, 0x01F24, 0x01F25, 0x01F26, 0x01F27, - 0x01F30, 0x01F31, 0x01F32, 0x01F33, 0x01F34, 0x01F35, 0x01F36, 0x01F37, - 0x01F30, 0x01F31, 0x01F32, 0x01F33, 0x01F34, 0x01F35, 0x01F36, 0x01F37, - 0x01F40, 0x01F41, 0x01F42, 0x01F43, 0x01F44, 0x01F45, 0x01F46, 0x01F47, - 0x01F40, 0x01F41, 0x01F42, 0x01F43, 0x01F44, 0x01F45, 0x01F4E, 0x01F4F, - 0x01F50, 0x01F51, 0x01F52, 0x01F53, 0x01F54, 0x01F55, 0x01F56, 0x01F57, - 0x01F58, 0x01F51, 0x01F5A, 0x01F53, 0x01F5C, 0x01F55, 0x01F5E, 0x01F57, - 0x01F60, 0x01F61, 0x01F62, 0x01F63, 0x01F64, 0x01F65, 0x01F66, 0x01F67, - 0x01F60, 0x01F61, 0x01F62, 0x01F63, 0x01F64, 0x01F65, 0x01F66, 0x01F67, - 0x01F70, 0x01F71, 0x01F72, 0x01F73, 0x01F74, 0x01F75, 0x01F76, 0x01F77, - 0x01F78, 0x01F79, 0x01F7A, 0x01F7B, 0x01F7C, 0x01F7D, 0x01F7E, 0x01F7F, +static const uint32_t nxt_unicode_block_03e[128] nxt_aligned(64) = { + 0x01F00, + 0x01F01, + 0x01F02, + 0x01F03, + 0x01F04, + 0x01F05, + 0x01F06, + 0x01F07, + 0x01F00, + 0x01F01, + 0x01F02, + 0x01F03, + 0x01F04, + 0x01F05, + 0x01F06, + 0x01F07, + 0x01F10, + 0x01F11, + 0x01F12, + 0x01F13, + 0x01F14, + 0x01F15, + 0x01F16, + 0x01F17, + 0x01F10, + 0x01F11, + 0x01F12, + 0x01F13, + 0x01F14, + 0x01F15, + 0x01F1E, + 0x01F1F, + 0x01F20, + 0x01F21, + 0x01F22, + 0x01F23, + 0x01F24, + 0x01F25, + 0x01F26, + 0x01F27, + 0x01F20, + 0x01F21, + 0x01F22, + 0x01F23, + 0x01F24, + 0x01F25, + 0x01F26, + 0x01F27, + 0x01F30, + 0x01F31, + 0x01F32, + 0x01F33, + 0x01F34, + 0x01F35, + 0x01F36, + 0x01F37, + 0x01F30, + 0x01F31, + 0x01F32, + 0x01F33, + 0x01F34, + 0x01F35, + 0x01F36, + 0x01F37, + 0x01F40, + 0x01F41, + 0x01F42, + 0x01F43, + 0x01F44, + 0x01F45, + 0x01F46, + 0x01F47, + 0x01F40, + 0x01F41, + 0x01F42, + 0x01F43, + 0x01F44, + 0x01F45, + 0x01F4E, + 0x01F4F, + 0x01F50, + 0x01F51, + 0x01F52, + 0x01F53, + 0x01F54, + 0x01F55, + 0x01F56, + 0x01F57, + 0x01F58, + 0x01F51, + 0x01F5A, + 0x01F53, + 0x01F5C, + 0x01F55, + 0x01F5E, + 0x01F57, + 0x01F60, + 0x01F61, + 0x01F62, + 0x01F63, + 0x01F64, + 0x01F65, + 0x01F66, + 0x01F67, + 0x01F60, + 0x01F61, + 0x01F62, + 0x01F63, + 0x01F64, + 0x01F65, + 0x01F66, + 0x01F67, + 0x01F70, + 0x01F71, + 0x01F72, + 0x01F73, + 0x01F74, + 0x01F75, + 0x01F76, + 0x01F77, + 0x01F78, + 0x01F79, + 0x01F7A, + 0x01F7B, + 0x01F7C, + 0x01F7D, + 0x01F7E, + 0x01F7F, }; -static const uint32_t nxt_unicode_block_03f[128] nxt_aligned(64) = { - 0x01F80, 0x01F81, 0x01F82, 0x01F83, 0x01F84, 0x01F85, 0x01F86, 0x01F87, - 0x01F80, 0x01F81, 0x01F82, 0x01F83, 0x01F84, 0x01F85, 0x01F86, 0x01F87, - 0x01F90, 0x01F91, 0x01F92, 0x01F93, 0x01F94, 0x01F95, 0x01F96, 0x01F97, - 0x01F90, 0x01F91, 0x01F92, 0x01F93, 0x01F94, 0x01F95, 0x01F96, 0x01F97, - 0x01FA0, 0x01FA1, 0x01FA2, 0x01FA3, 0x01FA4, 0x01FA5, 0x01FA6, 0x01FA7, - 0x01FA0, 0x01FA1, 0x01FA2, 0x01FA3, 0x01FA4, 0x01FA5, 0x01FA6, 0x01FA7, - 0x01FB0, 0x01FB1, 0x01FB2, 0x01FB3, 0x01FB4, 0x01FB5, 0x01FB6, 0x01FB7, - 0x01FB0, 0x01FB1, 0x01F70, 0x01F71, 0x01FB3, 0x01FBD, 0x003B9, 0x01FBF, - 0x01FC0, 0x01FC1, 0x01FC2, 0x01FC3, 0x01FC4, 0x01FC5, 0x01FC6, 0x01FC7, - 0x01F72, 0x01F73, 0x01F74, 0x01F75, 0x01FC3, 0x01FCD, 0x01FCE, 0x01FCF, - 0x01FD0, 0x01FD1, 0x01FD2, 0x01FD3, 0x01FD4, 0x01FD5, 0x01FD6, 0x01FD7, - 0x01FD0, 0x01FD1, 0x01F76, 0x01F77, 0x01FDC, 0x01FDD, 0x01FDE, 0x01FDF, - 0x01FE0, 0x01FE1, 0x01FE2, 0x01FE3, 0x01FE4, 0x01FE5, 0x01FE6, 0x01FE7, - 0x01FE0, 0x01FE1, 0x01F7A, 0x01F7B, 0x01FE5, 0x01FED, 0x01FEE, 0x01FEF, - 0x01FF0, 0x01FF1, 0x01FF2, 0x01FF3, 0x01FF4, 0x01FF5, 0x01FF6, 0x01FF7, - 0x01F78, 0x01F79, 0x01F7C, 0x01F7D, 0x01FF3, 0x01FFD, 0x01FFE, 0x01FFF, +static const uint32_t nxt_unicode_block_03f[128] nxt_aligned(64) = { + 0x01F80, + 0x01F81, + 0x01F82, + 0x01F83, + 0x01F84, + 0x01F85, + 0x01F86, + 0x01F87, + 0x01F80, + 0x01F81, + 0x01F82, + 0x01F83, + 0x01F84, + 0x01F85, + 0x01F86, + 0x01F87, + 0x01F90, + 0x01F91, + 0x01F92, + 0x01F93, + 0x01F94, + 0x01F95, + 0x01F96, + 0x01F97, + 0x01F90, + 0x01F91, + 0x01F92, + 0x01F93, + 0x01F94, + 0x01F95, + 0x01F96, + 0x01F97, + 0x01FA0, + 0x01FA1, + 0x01FA2, + 0x01FA3, + 0x01FA4, + 0x01FA5, + 0x01FA6, + 0x01FA7, + 0x01FA0, + 0x01FA1, + 0x01FA2, + 0x01FA3, + 0x01FA4, + 0x01FA5, + 0x01FA6, + 0x01FA7, + 0x01FB0, + 0x01FB1, + 0x01FB2, + 0x01FB3, + 0x01FB4, + 0x01FB5, + 0x01FB6, + 0x01FB7, + 0x01FB0, + 0x01FB1, + 0x01F70, + 0x01F71, + 0x01FB3, + 0x01FBD, + 0x003B9, + 0x01FBF, + 0x01FC0, + 0x01FC1, + 0x01FC2, + 0x01FC3, + 0x01FC4, + 0x01FC5, + 0x01FC6, + 0x01FC7, + 0x01F72, + 0x01F73, + 0x01F74, + 0x01F75, + 0x01FC3, + 0x01FCD, + 0x01FCE, + 0x01FCF, + 0x01FD0, + 0x01FD1, + 0x01FD2, + 0x01FD3, + 0x01FD4, + 0x01FD5, + 0x01FD6, + 0x01FD7, + 0x01FD0, + 0x01FD1, + 0x01F76, + 0x01F77, + 0x01FDC, + 0x01FDD, + 0x01FDE, + 0x01FDF, + 0x01FE0, + 0x01FE1, + 0x01FE2, + 0x01FE3, + 0x01FE4, + 0x01FE5, + 0x01FE6, + 0x01FE7, + 0x01FE0, + 0x01FE1, + 0x01F7A, + 0x01F7B, + 0x01FE5, + 0x01FED, + 0x01FEE, + 0x01FEF, + 0x01FF0, + 0x01FF1, + 0x01FF2, + 0x01FF3, + 0x01FF4, + 0x01FF5, + 0x01FF6, + 0x01FF7, + 0x01F78, + 0x01F79, + 0x01F7C, + 0x01F7D, + 0x01FF3, + 0x01FFD, + 0x01FFE, + 0x01FFF, }; -static const uint32_t nxt_unicode_block_042[128] nxt_aligned(64) = { - 0x02100, 0x02101, 0x02102, 0x02103, 0x02104, 0x02105, 0x02106, 0x02107, - 0x02108, 0x02109, 0x0210A, 0x0210B, 0x0210C, 0x0210D, 0x0210E, 0x0210F, - 0x02110, 0x02111, 0x02112, 0x02113, 0x02114, 0x02115, 0x02116, 0x02117, - 0x02118, 0x02119, 0x0211A, 0x0211B, 0x0211C, 0x0211D, 0x0211E, 0x0211F, - 0x02120, 0x02121, 0x02122, 0x02123, 0x02124, 0x02125, 0x02126, 0x02127, - 0x02128, 0x02129, 0x0212A, 0x0212B, 0x0212C, 0x0212D, 0x0212E, 0x0212F, - 0x02130, 0x02131, 0x02132, 0x02133, 0x02134, 0x02135, 0x02136, 0x02137, - 0x02138, 0x02139, 0x0213A, 0x0213B, 0x0213C, 0x0213D, 0x0213E, 0x0213F, - 0x02140, 0x02141, 0x02142, 0x02143, 0x02144, 0x02145, 0x02146, 0x02147, - 0x02148, 0x02149, 0x0214A, 0x0214B, 0x0214C, 0x0214D, 0x0214E, 0x0214F, - 0x02150, 0x02151, 0x02152, 0x02153, 0x02154, 0x02155, 0x02156, 0x02157, - 0x02158, 0x02159, 0x0215A, 0x0215B, 0x0215C, 0x0215D, 0x0215E, 0x0215F, - 0x02170, 0x02171, 0x02172, 0x02173, 0x02174, 0x02175, 0x02176, 0x02177, - 0x02178, 0x02179, 0x0217A, 0x0217B, 0x0217C, 0x0217D, 0x0217E, 0x0217F, - 0x02170, 0x02171, 0x02172, 0x02173, 0x02174, 0x02175, 0x02176, 0x02177, - 0x02178, 0x02179, 0x0217A, 0x0217B, 0x0217C, 0x0217D, 0x0217E, 0x0217F, +static const uint32_t nxt_unicode_block_042[128] nxt_aligned(64) = { + 0x02100, + 0x02101, + 0x02102, + 0x02103, + 0x02104, + 0x02105, + 0x02106, + 0x02107, + 0x02108, + 0x02109, + 0x0210A, + 0x0210B, + 0x0210C, + 0x0210D, + 0x0210E, + 0x0210F, + 0x02110, + 0x02111, + 0x02112, + 0x02113, + 0x02114, + 0x02115, + 0x02116, + 0x02117, + 0x02118, + 0x02119, + 0x0211A, + 0x0211B, + 0x0211C, + 0x0211D, + 0x0211E, + 0x0211F, + 0x02120, + 0x02121, + 0x02122, + 0x02123, + 0x02124, + 0x02125, + 0x02126, + 0x02127, + 0x02128, + 0x02129, + 0x0212A, + 0x0212B, + 0x0212C, + 0x0212D, + 0x0212E, + 0x0212F, + 0x02130, + 0x02131, + 0x02132, + 0x02133, + 0x02134, + 0x02135, + 0x02136, + 0x02137, + 0x02138, + 0x02139, + 0x0213A, + 0x0213B, + 0x0213C, + 0x0213D, + 0x0213E, + 0x0213F, + 0x02140, + 0x02141, + 0x02142, + 0x02143, + 0x02144, + 0x02145, + 0x02146, + 0x02147, + 0x02148, + 0x02149, + 0x0214A, + 0x0214B, + 0x0214C, + 0x0214D, + 0x0214E, + 0x0214F, + 0x02150, + 0x02151, + 0x02152, + 0x02153, + 0x02154, + 0x02155, + 0x02156, + 0x02157, + 0x02158, + 0x02159, + 0x0215A, + 0x0215B, + 0x0215C, + 0x0215D, + 0x0215E, + 0x0215F, + 0x02170, + 0x02171, + 0x02172, + 0x02173, + 0x02174, + 0x02175, + 0x02176, + 0x02177, + 0x02178, + 0x02179, + 0x0217A, + 0x0217B, + 0x0217C, + 0x0217D, + 0x0217E, + 0x0217F, + 0x02170, + 0x02171, + 0x02172, + 0x02173, + 0x02174, + 0x02175, + 0x02176, + 0x02177, + 0x02178, + 0x02179, + 0x0217A, + 0x0217B, + 0x0217C, + 0x0217D, + 0x0217E, + 0x0217F, }; -static const uint32_t nxt_unicode_block_1fe[59] nxt_aligned(64) = { - 0x0FF00, 0x0FF01, 0x0FF02, 0x0FF03, 0x0FF04, 0x0FF05, 0x0FF06, 0x0FF07, - 0x0FF08, 0x0FF09, 0x0FF0A, 0x0FF0B, 0x0FF0C, 0x0FF0D, 0x0FF0E, 0x0FF0F, - 0x0FF10, 0x0FF11, 0x0FF12, 0x0FF13, 0x0FF14, 0x0FF15, 0x0FF16, 0x0FF17, - 0x0FF18, 0x0FF19, 0x0FF1A, 0x0FF1B, 0x0FF1C, 0x0FF1D, 0x0FF1E, 0x0FF1F, - 0x0FF20, 0x0FF41, 0x0FF42, 0x0FF43, 0x0FF44, 0x0FF45, 0x0FF46, 0x0FF47, - 0x0FF48, 0x0FF49, 0x0FF4A, 0x0FF4B, 0x0FF4C, 0x0FF4D, 0x0FF4E, 0x0FF4F, - 0x0FF50, 0x0FF51, 0x0FF52, 0x0FF53, 0x0FF54, 0x0FF55, 0x0FF56, 0x0FF57, - 0x0FF58, 0x0FF59, 0x0FF5A, +static const uint32_t nxt_unicode_block_1fe[59] nxt_aligned(64) = { + 0x0FF00, + 0x0FF01, + 0x0FF02, + 0x0FF03, + 0x0FF04, + 0x0FF05, + 0x0FF06, + 0x0FF07, + 0x0FF08, + 0x0FF09, + 0x0FF0A, + 0x0FF0B, + 0x0FF0C, + 0x0FF0D, + 0x0FF0E, + 0x0FF0F, + 0x0FF10, + 0x0FF11, + 0x0FF12, + 0x0FF13, + 0x0FF14, + 0x0FF15, + 0x0FF16, + 0x0FF17, + 0x0FF18, + 0x0FF19, + 0x0FF1A, + 0x0FF1B, + 0x0FF1C, + 0x0FF1D, + 0x0FF1E, + 0x0FF1F, + 0x0FF20, + 0x0FF41, + 0x0FF42, + 0x0FF43, + 0x0FF44, + 0x0FF45, + 0x0FF46, + 0x0FF47, + 0x0FF48, + 0x0FF49, + 0x0FF4A, + 0x0FF4B, + 0x0FF4C, + 0x0FF4D, + 0x0FF4E, + 0x0FF4F, + 0x0FF50, + 0x0FF51, + 0x0FF52, + 0x0FF53, + 0x0FF54, + 0x0FF55, + 0x0FF56, + 0x0FF57, + 0x0FF58, + 0x0FF59, + 0x0FF5A, }; -static const uint32_t *nxt_unicode_blocks[] nxt_aligned(64) = { +static const uint32_t *nxt_unicode_blocks[] nxt_aligned(64) = { nxt_unicode_block_000, nxt_unicode_block_001, nxt_unicode_block_002, diff --git a/src/nxt_unit.c b/src/nxt_unit.c index 966a6c0fa..3e85b8ab6 100644 --- a/src/nxt_unit.c +++ b/src/nxt_unit.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -20,8 +19,8 @@ #include #endif -#define NXT_UNIT_MAX_PLAIN_SIZE 1024 -#define NXT_UNIT_LOCAL_BUF_SIZE \ +#define NXT_UNIT_MAX_PLAIN_SIZE 1024 +#define NXT_UNIT_LOCAL_BUF_SIZE \ (NXT_UNIT_MAX_PLAIN_SIZE + sizeof(nxt_port_msg_t)) enum { @@ -29,408 +28,465 @@ enum { NXT_QUIT_GRACEFUL = 1, }; -typedef struct nxt_unit_impl_s nxt_unit_impl_t; -typedef struct nxt_unit_mmap_s nxt_unit_mmap_t; -typedef struct nxt_unit_mmaps_s nxt_unit_mmaps_t; -typedef struct nxt_unit_process_s nxt_unit_process_t; -typedef struct nxt_unit_mmap_buf_s nxt_unit_mmap_buf_t; -typedef struct nxt_unit_recv_msg_s nxt_unit_recv_msg_t; -typedef struct nxt_unit_read_buf_s nxt_unit_read_buf_t; -typedef struct nxt_unit_ctx_impl_s nxt_unit_ctx_impl_t; -typedef struct nxt_unit_port_impl_s nxt_unit_port_impl_t; -typedef struct nxt_unit_request_info_impl_s nxt_unit_request_info_impl_t; -typedef struct nxt_unit_websocket_frame_impl_s nxt_unit_websocket_frame_impl_t; - -static nxt_unit_impl_t *nxt_unit_create(nxt_unit_init_t *init); -static int nxt_unit_ctx_init(nxt_unit_impl_t *lib, - nxt_unit_ctx_impl_t *ctx_impl, void *data); -nxt_inline void nxt_unit_ctx_use(nxt_unit_ctx_t *ctx); -nxt_inline void nxt_unit_ctx_release(nxt_unit_ctx_t *ctx); -nxt_inline void nxt_unit_lib_use(nxt_unit_impl_t *lib); -nxt_inline void nxt_unit_lib_release(nxt_unit_impl_t *lib); -nxt_inline void nxt_unit_mmap_buf_insert(nxt_unit_mmap_buf_t **head, - nxt_unit_mmap_buf_t *mmap_buf); -nxt_inline void nxt_unit_mmap_buf_insert_tail(nxt_unit_mmap_buf_t **prev, - nxt_unit_mmap_buf_t *mmap_buf); -nxt_inline void nxt_unit_mmap_buf_unlink(nxt_unit_mmap_buf_t *mmap_buf); -static int nxt_unit_read_env(nxt_unit_port_t *ready_port, - nxt_unit_port_t *router_port, nxt_unit_port_t *read_port, - int *shared_port_fd, int *shared_queue_fd, +typedef struct nxt_unit_impl_s nxt_unit_impl_t; +typedef struct nxt_unit_mmap_s nxt_unit_mmap_t; +typedef struct nxt_unit_mmaps_s nxt_unit_mmaps_t; +typedef struct nxt_unit_process_s nxt_unit_process_t; +typedef struct nxt_unit_mmap_buf_s nxt_unit_mmap_buf_t; +typedef struct nxt_unit_recv_msg_s nxt_unit_recv_msg_t; +typedef struct nxt_unit_read_buf_s nxt_unit_read_buf_t; +typedef struct nxt_unit_ctx_impl_s nxt_unit_ctx_impl_t; +typedef struct nxt_unit_port_impl_s nxt_unit_port_impl_t; +typedef struct nxt_unit_request_info_impl_s nxt_unit_request_info_impl_t; +typedef struct nxt_unit_websocket_frame_impl_s nxt_unit_websocket_frame_impl_t; + +static nxt_unit_impl_t * +nxt_unit_create(nxt_unit_init_t *init); +static int +nxt_unit_ctx_init(nxt_unit_impl_t *lib, nxt_unit_ctx_impl_t *ctx_impl, + void *data); +nxt_inline void +nxt_unit_ctx_use(nxt_unit_ctx_t *ctx); +nxt_inline void +nxt_unit_ctx_release(nxt_unit_ctx_t *ctx); +nxt_inline void +nxt_unit_lib_use(nxt_unit_impl_t *lib); +nxt_inline void +nxt_unit_lib_release(nxt_unit_impl_t *lib); +nxt_inline void +nxt_unit_mmap_buf_insert(nxt_unit_mmap_buf_t **head, + nxt_unit_mmap_buf_t *mmap_buf); +nxt_inline void +nxt_unit_mmap_buf_insert_tail(nxt_unit_mmap_buf_t **prev, + nxt_unit_mmap_buf_t *mmap_buf); +nxt_inline void +nxt_unit_mmap_buf_unlink(nxt_unit_mmap_buf_t *mmap_buf); +static int +nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, + nxt_unit_port_t *read_port, int *shared_port_fd, int *shared_queue_fd, int *log_fd, uint32_t *stream, uint32_t *shm_limit, uint32_t *request_limit); -static int nxt_unit_ready(nxt_unit_ctx_t *ctx, int ready_fd, uint32_t stream, +static int +nxt_unit_ready(nxt_unit_ctx_t *ctx, int ready_fd, uint32_t stream, int queue_fd); -static int nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, +static int +nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, nxt_unit_request_info_t **preq); -static int nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, - nxt_unit_recv_msg_t *recv_msg); -static int nxt_unit_ctx_ready(nxt_unit_ctx_t *ctx); -static int nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, - nxt_unit_recv_msg_t *recv_msg, nxt_unit_request_info_t **preq); -static int nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, - nxt_unit_recv_msg_t *recv_msg); -static int nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, - nxt_unit_port_id_t *port_id); -static int nxt_unit_send_req_headers_ack(nxt_unit_request_info_t *req); -static int nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, - nxt_unit_recv_msg_t *recv_msg); -static int nxt_unit_process_shm_ack(nxt_unit_ctx_t *ctx); -static nxt_unit_request_info_impl_t *nxt_unit_request_info_get( - nxt_unit_ctx_t *ctx); -static void nxt_unit_request_info_release(nxt_unit_request_info_t *req); -static void nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req); -static nxt_unit_websocket_frame_impl_t *nxt_unit_websocket_frame_get( - nxt_unit_ctx_t *ctx); -static void nxt_unit_websocket_frame_release(nxt_unit_websocket_frame_t *ws); -static void nxt_unit_websocket_frame_free(nxt_unit_ctx_t *ctx, - nxt_unit_websocket_frame_impl_t *ws); -static nxt_unit_mmap_buf_t *nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx); -static void nxt_unit_mmap_buf_release(nxt_unit_mmap_buf_t *mmap_buf); -static int nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, +static int +nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg); +static int +nxt_unit_ctx_ready(nxt_unit_ctx_t *ctx); +static int +nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, + nxt_unit_request_info_t **preq); +static int +nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg); +static int +nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, + nxt_unit_port_id_t *port_id); +static int +nxt_unit_send_req_headers_ack(nxt_unit_request_info_t *req); +static int +nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg); +static int +nxt_unit_process_shm_ack(nxt_unit_ctx_t *ctx); +static nxt_unit_request_info_impl_t * +nxt_unit_request_info_get(nxt_unit_ctx_t *ctx); +static void +nxt_unit_request_info_release(nxt_unit_request_info_t *req); +static void +nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req); +static nxt_unit_websocket_frame_impl_t * +nxt_unit_websocket_frame_get(nxt_unit_ctx_t *ctx); +static void +nxt_unit_websocket_frame_release(nxt_unit_websocket_frame_t *ws); +static void +nxt_unit_websocket_frame_free(nxt_unit_ctx_t *ctx, + nxt_unit_websocket_frame_impl_t *ws); +static nxt_unit_mmap_buf_t * +nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx); +static void +nxt_unit_mmap_buf_release(nxt_unit_mmap_buf_t *mmap_buf); +static int +nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, nxt_unit_mmap_buf_t *mmap_buf, int last); -static void nxt_unit_mmap_buf_free(nxt_unit_mmap_buf_t *mmap_buf); -static void nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf); -static nxt_unit_read_buf_t *nxt_unit_read_buf_get(nxt_unit_ctx_t *ctx); -static nxt_unit_read_buf_t *nxt_unit_read_buf_get_impl( - nxt_unit_ctx_impl_t *ctx_impl); -static void nxt_unit_read_buf_release(nxt_unit_ctx_t *ctx, +static void +nxt_unit_mmap_buf_free(nxt_unit_mmap_buf_t *mmap_buf); +static void +nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf); +static nxt_unit_read_buf_t * +nxt_unit_read_buf_get(nxt_unit_ctx_t *ctx); +static nxt_unit_read_buf_t * +nxt_unit_read_buf_get_impl(nxt_unit_ctx_impl_t *ctx_impl); +static void +nxt_unit_read_buf_release(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf); +static nxt_unit_mmap_buf_t * +nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size); +static ssize_t +nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, size_t size); +static nxt_port_mmap_header_t * +nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_chunk_id_t *c, + int *n, int min_n); +static int +nxt_unit_send_oosm(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +static int +nxt_unit_wait_shm_ack(nxt_unit_ctx_t *ctx); +static nxt_unit_mmap_t * +nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i); +static nxt_port_mmap_header_t * +nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n); +static int +nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size); +static int +nxt_unit_send_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int fd); +static int +nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, + uint32_t size, uint32_t min_size, nxt_unit_mmap_buf_t *mmap_buf, + char *local_buf); +static int +nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd); + +static void +nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, nxt_unit_ctx_impl_t *ctx_impl); +static int +nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps); +nxt_inline void +nxt_unit_process_use(nxt_unit_process_t *process); +nxt_inline void +nxt_unit_process_release(nxt_unit_process_t *process); +static void +nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps); +static int +nxt_unit_check_rbuf_mmap(nxt_unit_ctx_t *ctx, nxt_unit_mmaps_t *mmaps, + pid_t pid, uint32_t id, nxt_port_mmap_header_t **hdr, + nxt_unit_read_buf_t *rbuf); +static int +nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, nxt_unit_read_buf_t *rbuf); -static nxt_unit_mmap_buf_t *nxt_unit_request_preread( - nxt_unit_request_info_t *req, size_t size); -static ssize_t nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, - size_t size); -static nxt_port_mmap_header_t *nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port, nxt_chunk_id_t *c, int *n, int min_n); -static int nxt_unit_send_oosm(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); -static int nxt_unit_wait_shm_ack(nxt_unit_ctx_t *ctx); -static nxt_unit_mmap_t *nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i); -static nxt_port_mmap_header_t *nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port, int n); -static int nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size); -static int nxt_unit_send_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - int fd); -static int nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port, uint32_t size, - uint32_t min_size, nxt_unit_mmap_buf_t *mmap_buf, char *local_buf); -static int nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd); - -static void nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, - nxt_unit_ctx_impl_t *ctx_impl); -static int nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps); -nxt_inline void nxt_unit_process_use(nxt_unit_process_t *process); -nxt_inline void nxt_unit_process_release(nxt_unit_process_t *process); -static void nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps); -static int nxt_unit_check_rbuf_mmap(nxt_unit_ctx_t *ctx, - nxt_unit_mmaps_t *mmaps, pid_t pid, uint32_t id, - nxt_port_mmap_header_t **hdr, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, - nxt_unit_recv_msg_t *recv_msg, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_get_mmap(nxt_unit_ctx_t *ctx, pid_t pid, uint32_t id); -static void nxt_unit_mmap_release(nxt_unit_ctx_t *ctx, - nxt_port_mmap_header_t *hdr, void *start, uint32_t size); -static int nxt_unit_send_shm_ack(nxt_unit_ctx_t *ctx, pid_t pid); - -static nxt_unit_process_t *nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid); -static nxt_unit_process_t *nxt_unit_process_find(nxt_unit_impl_t *lib, - pid_t pid, int remove); -static nxt_unit_process_t *nxt_unit_process_pop_first(nxt_unit_impl_t *lib); -static int nxt_unit_run_once_impl(nxt_unit_ctx_t *ctx); -static int nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_chk_ready(nxt_unit_ctx_t *ctx); -static int nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx); -static void nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx); -nxt_inline int nxt_unit_is_read_queue(nxt_unit_read_buf_t *rbuf); -nxt_inline int nxt_unit_is_read_socket(nxt_unit_read_buf_t *rbuf); -nxt_inline int nxt_unit_is_shm_ack(nxt_unit_read_buf_t *rbuf); -nxt_inline int nxt_unit_is_quit(nxt_unit_read_buf_t *rbuf); -static int nxt_unit_process_port_msg_impl(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port); -static void nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl); -static nxt_unit_port_t *nxt_unit_create_port(nxt_unit_ctx_t *ctx); - -static int nxt_unit_send_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *dst, +static int +nxt_unit_get_mmap(nxt_unit_ctx_t *ctx, pid_t pid, uint32_t id); +static void +nxt_unit_mmap_release(nxt_unit_ctx_t *ctx, nxt_port_mmap_header_t *hdr, + void *start, uint32_t size); +static int +nxt_unit_send_shm_ack(nxt_unit_ctx_t *ctx, pid_t pid); + +static nxt_unit_process_t * +nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid); +static nxt_unit_process_t * +nxt_unit_process_find(nxt_unit_impl_t *lib, pid_t pid, int remove); +static nxt_unit_process_t * +nxt_unit_process_pop_first(nxt_unit_impl_t *lib); +static int +nxt_unit_run_once_impl(nxt_unit_ctx_t *ctx); +static int +nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf); +static int +nxt_unit_chk_ready(nxt_unit_ctx_t *ctx); +static int +nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx); +static void +nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx); +nxt_inline int +nxt_unit_is_read_queue(nxt_unit_read_buf_t *rbuf); +nxt_inline int +nxt_unit_is_read_socket(nxt_unit_read_buf_t *rbuf); +nxt_inline int +nxt_unit_is_shm_ack(nxt_unit_read_buf_t *rbuf); +nxt_inline int +nxt_unit_is_quit(nxt_unit_read_buf_t *rbuf); +static int +nxt_unit_process_port_msg_impl(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +static void +nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl); +static nxt_unit_port_t * +nxt_unit_create_port(nxt_unit_ctx_t *ctx); + +static int +nxt_unit_send_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *dst, nxt_unit_port_t *port, int queue_fd); -nxt_inline void nxt_unit_port_use(nxt_unit_port_t *port); -nxt_inline void nxt_unit_port_release(nxt_unit_port_t *port); -static nxt_unit_port_t *nxt_unit_add_port(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port, void *queue); -static void nxt_unit_process_awaiting_req(nxt_unit_ctx_t *ctx, - nxt_queue_t *awaiting_req); -static void nxt_unit_remove_port(nxt_unit_impl_t *lib, nxt_unit_ctx_t *ctx, - nxt_unit_port_id_t *port_id); -static nxt_unit_port_t *nxt_unit_remove_port_unsafe(nxt_unit_impl_t *lib, +nxt_inline void +nxt_unit_port_use(nxt_unit_port_t *port); +nxt_inline void +nxt_unit_port_release(nxt_unit_port_t *port); +static nxt_unit_port_t * +nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue); +static void +nxt_unit_process_awaiting_req(nxt_unit_ctx_t *ctx, nxt_queue_t *awaiting_req); +static void +nxt_unit_remove_port(nxt_unit_impl_t *lib, nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id); -static void nxt_unit_remove_pid(nxt_unit_impl_t *lib, pid_t pid); -static void nxt_unit_remove_process(nxt_unit_impl_t *lib, - nxt_unit_process_t *process); -static void nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param); -static int nxt_unit_get_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id); -static ssize_t nxt_unit_port_send(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port, const void *buf, size_t buf_size, +static nxt_unit_port_t * +nxt_unit_remove_port_unsafe(nxt_unit_impl_t *lib, nxt_unit_port_id_t *port_id); +static void +nxt_unit_remove_pid(nxt_unit_impl_t *lib, pid_t pid); +static void +nxt_unit_remove_process(nxt_unit_impl_t *lib, nxt_unit_process_t *process); +static void +nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param); +static int +nxt_unit_get_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id); +static ssize_t +nxt_unit_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, const void *buf, + size_t buf_size, const nxt_send_oob_t *oob); +static ssize_t +nxt_unit_sendmsg(nxt_unit_ctx_t *ctx, int fd, const void *buf, size_t buf_size, const nxt_send_oob_t *oob); -static ssize_t nxt_unit_sendmsg(nxt_unit_ctx_t *ctx, int fd, - const void *buf, size_t buf_size, const nxt_send_oob_t *oob); -static int nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_unit_read_buf_t *rbuf); -nxt_inline void nxt_unit_rbuf_cpy(nxt_unit_read_buf_t *dst, - nxt_unit_read_buf_t *src); -static int nxt_unit_shared_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, +static int +nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, +nxt_inline void +nxt_unit_rbuf_cpy(nxt_unit_read_buf_t *dst, nxt_unit_read_buf_t *src); +static int +nxt_unit_shared_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_port_queue_recv(nxt_unit_port_t *port, +static int +nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf); -static int nxt_unit_app_queue_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, +static int +nxt_unit_port_queue_recv(nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf); +static int +nxt_unit_app_queue_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf); -nxt_inline int nxt_unit_close(int fd); -static int nxt_unit_fd_blocking(int fd); - -static int nxt_unit_port_hash_add(nxt_lvlhsh_t *port_hash, - nxt_unit_port_t *port); -static nxt_unit_port_t *nxt_unit_port_hash_find(nxt_lvlhsh_t *port_hash, - nxt_unit_port_id_t *port_id, int remove); +nxt_inline int +nxt_unit_close(int fd); +static int +nxt_unit_fd_blocking(int fd); -static int nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, - nxt_unit_request_info_t *req); -static nxt_unit_request_info_t *nxt_unit_request_hash_find( - nxt_unit_ctx_t *ctx, uint32_t stream, int remove); +static int +nxt_unit_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_unit_port_t *port); +static nxt_unit_port_t * +nxt_unit_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_unit_port_id_t *port_id, + int remove); -static char * nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, - int level); -static void *nxt_unit_lvlhsh_alloc(void *data, size_t size); -static void nxt_unit_lvlhsh_free(void *data, void *p); -static int nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length); +static int +nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, nxt_unit_request_info_t *req); +static nxt_unit_request_info_t * +nxt_unit_request_hash_find(nxt_unit_ctx_t *ctx, uint32_t stream, int remove); +static char * +nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level); +static void * +nxt_unit_lvlhsh_alloc(void *data, size_t size); +static void +nxt_unit_lvlhsh_free(void *data, void *p); +static int +nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length); struct nxt_unit_mmap_buf_s { - nxt_unit_buf_t buf; + nxt_unit_buf_t buf; - nxt_unit_mmap_buf_t *next; - nxt_unit_mmap_buf_t **prev; + nxt_unit_mmap_buf_t *next; + nxt_unit_mmap_buf_t **prev; - nxt_port_mmap_header_t *hdr; - nxt_unit_request_info_t *req; - nxt_unit_ctx_impl_t *ctx_impl; - char *free_ptr; - char *plain_ptr; + nxt_port_mmap_header_t *hdr; + nxt_unit_request_info_t *req; + nxt_unit_ctx_impl_t *ctx_impl; + char *free_ptr; + char *plain_ptr; }; - struct nxt_unit_recv_msg_s { - uint32_t stream; - nxt_pid_t pid; - nxt_port_id_t reply_port; + uint32_t stream; + nxt_pid_t pid; + nxt_port_id_t reply_port; - uint8_t last; /* 1 bit */ - uint8_t mmap; /* 1 bit */ + uint8_t last; /* 1 bit */ + uint8_t mmap; /* 1 bit */ - void *start; - uint32_t size; + void *start; + uint32_t size; - int fd[2]; + int fd[2]; - nxt_unit_mmap_buf_t *incoming_buf; + nxt_unit_mmap_buf_t *incoming_buf; }; - typedef enum { - NXT_UNIT_RS_START = 0, + NXT_UNIT_RS_START = 0, NXT_UNIT_RS_RESPONSE_INIT, NXT_UNIT_RS_RESPONSE_HAS_CONTENT, NXT_UNIT_RS_RESPONSE_SENT, NXT_UNIT_RS_RELEASED, } nxt_unit_req_state_t; - struct nxt_unit_request_info_impl_s { - nxt_unit_request_info_t req; + nxt_unit_request_info_t req; - uint32_t stream; + uint32_t stream; - nxt_unit_mmap_buf_t *outgoing_buf; - nxt_unit_mmap_buf_t *incoming_buf; + nxt_unit_mmap_buf_t *outgoing_buf; + nxt_unit_mmap_buf_t *incoming_buf; - nxt_unit_req_state_t state; - uint8_t websocket; - uint8_t in_hash; + nxt_unit_req_state_t state; + uint8_t websocket; + uint8_t in_hash; /* for nxt_unit_ctx_impl_t.free_req or active_req */ - nxt_queue_link_t link; + nxt_queue_link_t link; /* for nxt_unit_port_impl_t.awaiting_req */ - nxt_queue_link_t port_wait_link; + nxt_queue_link_t port_wait_link; - char extra_data[]; + char extra_data[]; }; - struct nxt_unit_websocket_frame_impl_s { - nxt_unit_websocket_frame_t ws; + nxt_unit_websocket_frame_t ws; - nxt_unit_mmap_buf_t *buf; + nxt_unit_mmap_buf_t *buf; - nxt_queue_link_t link; + nxt_queue_link_t link; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_ctx_impl_t *ctx_impl; }; - struct nxt_unit_read_buf_s { - nxt_queue_link_t link; - nxt_unit_ctx_impl_t *ctx_impl; - ssize_t size; - nxt_recv_oob_t oob; - char buf[16384]; + nxt_queue_link_t link; + nxt_unit_ctx_impl_t *ctx_impl; + ssize_t size; + nxt_recv_oob_t oob; + char buf[16384]; }; - struct nxt_unit_ctx_impl_s { - nxt_unit_ctx_t ctx; + nxt_unit_ctx_t ctx; - nxt_atomic_t use_count; - nxt_atomic_t wait_items; + nxt_atomic_t use_count; + nxt_atomic_t wait_items; - pthread_mutex_t mutex; + pthread_mutex_t mutex; - nxt_unit_port_t *read_port; + nxt_unit_port_t *read_port; - nxt_queue_link_t link; + nxt_queue_link_t link; - nxt_unit_mmap_buf_t *free_buf; + nxt_unit_mmap_buf_t *free_buf; /* of nxt_unit_request_info_impl_t */ - nxt_queue_t free_req; + nxt_queue_t free_req; /* of nxt_unit_websocket_frame_impl_t */ - nxt_queue_t free_ws; + nxt_queue_t free_ws; /* of nxt_unit_request_info_impl_t */ - nxt_queue_t active_req; + nxt_queue_t active_req; /* of nxt_unit_request_info_impl_t */ - nxt_lvlhsh_t requests; + nxt_lvlhsh_t requests; /* of nxt_unit_request_info_impl_t */ - nxt_queue_t ready_req; + nxt_queue_t ready_req; /* of nxt_unit_read_buf_t */ - nxt_queue_t pending_rbuf; + nxt_queue_t pending_rbuf; /* of nxt_unit_read_buf_t */ - nxt_queue_t free_rbuf; + nxt_queue_t free_rbuf; - uint8_t online; /* 1 bit */ - uint8_t ready; /* 1 bit */ - uint8_t quit_param; + uint8_t online; /* 1 bit */ + uint8_t ready; /* 1 bit */ + uint8_t quit_param; - nxt_unit_mmap_buf_t ctx_buf[2]; - nxt_unit_read_buf_t ctx_read_buf; + nxt_unit_mmap_buf_t ctx_buf[2]; + nxt_unit_read_buf_t ctx_read_buf; - nxt_unit_request_info_impl_t req; + nxt_unit_request_info_impl_t req; }; - struct nxt_unit_mmap_s { - nxt_port_mmap_header_t *hdr; - pthread_t src_thread; + nxt_port_mmap_header_t *hdr; + pthread_t src_thread; /* of nxt_unit_read_buf_t */ - nxt_queue_t awaiting_rbuf; + nxt_queue_t awaiting_rbuf; }; - struct nxt_unit_mmaps_s { - pthread_mutex_t mutex; - uint32_t size; - uint32_t cap; - nxt_atomic_t allocated_chunks; - nxt_unit_mmap_t *elts; + pthread_mutex_t mutex; + uint32_t size; + uint32_t cap; + nxt_atomic_t allocated_chunks; + nxt_unit_mmap_t *elts; }; - struct nxt_unit_impl_s { - nxt_unit_t unit; - nxt_unit_callbacks_t callbacks; + nxt_unit_t unit; + nxt_unit_callbacks_t callbacks; - nxt_atomic_t use_count; - nxt_atomic_t request_count; + nxt_atomic_t use_count; + nxt_atomic_t request_count; - uint32_t request_data_size; - uint32_t shm_mmap_limit; - uint32_t request_limit; + uint32_t request_data_size; + uint32_t shm_mmap_limit; + uint32_t request_limit; - pthread_mutex_t mutex; + pthread_mutex_t mutex; - nxt_lvlhsh_t processes; /* of nxt_unit_process_t */ - nxt_lvlhsh_t ports; /* of nxt_unit_port_impl_t */ + nxt_lvlhsh_t processes; /* of nxt_unit_process_t */ + nxt_lvlhsh_t ports; /* of nxt_unit_port_impl_t */ - nxt_unit_port_t *router_port; - nxt_unit_port_t *shared_port; + nxt_unit_port_t *router_port; + nxt_unit_port_t *shared_port; - nxt_queue_t contexts; /* of nxt_unit_ctx_impl_t */ + nxt_queue_t contexts; /* of nxt_unit_ctx_impl_t */ - nxt_unit_mmaps_t incoming; - nxt_unit_mmaps_t outgoing; + nxt_unit_mmaps_t incoming; + nxt_unit_mmaps_t outgoing; - pid_t pid; - int log_fd; + pid_t pid; + int log_fd; - nxt_unit_ctx_impl_t main_ctx; + nxt_unit_ctx_impl_t main_ctx; }; - struct nxt_unit_port_impl_s { - nxt_unit_port_t port; + nxt_unit_port_t port; - nxt_atomic_t use_count; + nxt_atomic_t use_count; /* for nxt_unit_process_t.ports */ - nxt_queue_link_t link; - nxt_unit_process_t *process; + nxt_queue_link_t link; + nxt_unit_process_t *process; /* of nxt_unit_request_info_impl_t */ - nxt_queue_t awaiting_req; + nxt_queue_t awaiting_req; - int ready; + int ready; - void *queue; + void *queue; - int from_socket; - nxt_unit_read_buf_t *socket_rbuf; + int from_socket; + nxt_unit_read_buf_t *socket_rbuf; }; - struct nxt_unit_process_s { - pid_t pid; + pid_t pid; - nxt_queue_t ports; /* of nxt_unit_port_impl_t */ + nxt_queue_t ports; /* of nxt_unit_port_impl_t */ - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; - nxt_atomic_t use_count; + nxt_atomic_t use_count; - uint32_t next_port_id; + uint32_t next_port_id; }; - /* Explicitly using 32 bit types to avoid possible alignment. */ typedef struct { - int32_t pid; - uint32_t id; + int32_t pid; + uint32_t id; } nxt_unit_port_hash_id_t; - -static pid_t nxt_unit_pid; - +static pid_t nxt_unit_pid; nxt_unit_ctx_t * -nxt_unit_init(nxt_unit_init_t *init) -{ +nxt_unit_init(nxt_unit_init_t *init) { int rc, queue_fd, shared_queue_fd; - void *mem; + void *mem; uint32_t ready_stream, shm_limit, request_limit; - nxt_unit_ctx_t *ctx; - nxt_unit_impl_t *lib; + nxt_unit_ctx_t *ctx; + nxt_unit_impl_t *lib; nxt_unit_port_t ready_port, router_port, read_port, shared_port; nxt_unit_pid = getpid(); @@ -440,42 +496,38 @@ nxt_unit_init(nxt_unit_init_t *init) return NULL; } - queue_fd = -1; - mem = MAP_FAILED; + queue_fd = -1; + mem = MAP_FAILED; shared_port.out_fd = -1; - shared_port.data = NULL; + shared_port.data = NULL; - if (init->ready_port.id.pid != 0 - && init->ready_stream != 0 - && init->read_port.id.pid != 0) - { - ready_port = init->ready_port; + if (init->ready_port.id.pid != 0 && init->ready_stream != 0 + && init->read_port.id.pid != 0) { + ready_port = init->ready_port; ready_stream = init->ready_stream; - router_port = init->router_port; - read_port = init->read_port; - lib->log_fd = init->log_fd; + router_port = init->router_port; + read_port = init->read_port; + lib->log_fd = init->log_fd; nxt_unit_port_id_init(&ready_port.id, ready_port.id.pid, - ready_port.id.id); + ready_port.id.id); nxt_unit_port_id_init(&router_port.id, router_port.id.pid, - router_port.id.id); - nxt_unit_port_id_init(&read_port.id, read_port.id.pid, - read_port.id.id); + router_port.id.id); + nxt_unit_port_id_init(&read_port.id, read_port.id.pid, read_port.id.id); shared_port.in_fd = init->shared_port_fd; - shared_queue_fd = init->shared_queue_fd; + shared_queue_fd = init->shared_queue_fd; } else { rc = nxt_unit_read_env(&ready_port, &router_port, &read_port, - &shared_port.in_fd, &shared_queue_fd, - &lib->log_fd, &ready_stream, &shm_limit, - &request_limit); + &shared_port.in_fd, &shared_queue_fd, &lib->log_fd, &ready_stream, + &shm_limit, &request_limit); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto fail; } - lib->shm_mmap_limit = (shm_limit + PORT_MMAP_DATA_SIZE - 1) - / PORT_MMAP_DATA_SIZE; + lib->shm_mmap_limit + = (shm_limit + PORT_MMAP_DATA_SIZE - 1) / PORT_MMAP_DATA_SIZE; lib->request_limit = request_limit; } @@ -483,7 +535,7 @@ nxt_unit_init(nxt_unit_init_t *init) lib->shm_mmap_limit = 1; } - lib->pid = read_port.id.pid; + lib->pid = read_port.id.pid; nxt_unit_pid = lib->pid; ctx = &lib->main_ctx.ctx; @@ -505,11 +557,11 @@ nxt_unit_init(nxt_unit_init_t *init) goto fail; } - mem = mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, queue_fd, 0); + mem = mmap(NULL, sizeof(nxt_port_queue_t), PROT_READ | PROT_WRITE, + MAP_SHARED, queue_fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", queue_fd, - strerror(errno), errno); + strerror(errno), errno); goto fail; } @@ -534,13 +586,13 @@ nxt_unit_init(nxt_unit_init_t *init) } nxt_unit_port_id_init(&shared_port.id, read_port.id.pid, - NXT_UNIT_SHARED_PORT_ID); + NXT_UNIT_SHARED_PORT_ID); mem = mmap(NULL, sizeof(nxt_app_queue_t), PROT_READ | PROT_WRITE, - MAP_SHARED, shared_queue_fd, 0); + MAP_SHARED, shared_queue_fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", shared_queue_fd, - strerror(errno), errno); + strerror(errno), errno); goto fail; } @@ -581,12 +633,10 @@ nxt_unit_init(nxt_unit_init_t *init) return NULL; } - static nxt_unit_impl_t * -nxt_unit_create(nxt_unit_init_t *init) -{ +nxt_unit_create(nxt_unit_init_t *init) { int rc; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; if (nxt_slow_path(init->callbacks.request_handler == NULL)) { nxt_unit_alert(NULL, "request_handler is NULL"); @@ -595,7 +645,7 @@ nxt_unit_create(nxt_unit_init_t *init) } lib = nxt_unit_malloc(NULL, - sizeof(nxt_unit_impl_t) + init->request_data_size); + sizeof(nxt_unit_impl_t) + init->request_data_size); if (nxt_slow_path(lib == NULL)) { nxt_unit_alert(NULL, "failed to allocate unit struct"); @@ -613,21 +663,21 @@ nxt_unit_create(nxt_unit_init_t *init) lib->callbacks = init->callbacks; lib->request_data_size = init->request_data_size; - lib->shm_mmap_limit = (init->shm_limit + PORT_MMAP_DATA_SIZE - 1) - / PORT_MMAP_DATA_SIZE; + lib->shm_mmap_limit + = (init->shm_limit + PORT_MMAP_DATA_SIZE - 1) / PORT_MMAP_DATA_SIZE; lib->request_limit = init->request_limit; lib->processes.slot = NULL; - lib->ports.slot = NULL; + lib->ports.slot = NULL; lib->log_fd = STDERR_FILENO; nxt_queue_init(&lib->contexts); - lib->use_count = 0; + lib->use_count = 0; lib->request_count = 0; - lib->router_port = NULL; - lib->shared_port = NULL; + lib->router_port = NULL; + lib->shared_port = NULL; rc = nxt_unit_ctx_init(lib, &lib->main_ctx, init->ctx_data); if (nxt_slow_path(rc != NXT_UNIT_OK)) { @@ -665,12 +715,10 @@ nxt_unit_create(nxt_unit_init_t *init) return NULL; } - static int nxt_unit_ctx_init(nxt_unit_impl_t *lib, nxt_unit_ctx_impl_t *ctx_impl, - void *data) -{ - int rc; + void *data) { + int rc; ctx_impl->ctx.data = data; ctx_impl->ctx.unit = &lib->unit; @@ -690,10 +738,10 @@ nxt_unit_ctx_init(nxt_unit_impl_t *lib, nxt_unit_ctx_impl_t *ctx_impl, pthread_mutex_unlock(&lib->mutex); - ctx_impl->use_count = 1; + ctx_impl->use_count = 1; ctx_impl->wait_items = 0; - ctx_impl->online = 1; - ctx_impl->ready = 0; + ctx_impl->online = 1; + ctx_impl->ready = 0; ctx_impl->quit_param = NXT_QUIT_GRACEFUL; nxt_queue_init(&ctx_impl->free_req); @@ -712,32 +760,28 @@ nxt_unit_ctx_init(nxt_unit_impl_t *lib, nxt_unit_ctx_impl_t *ctx_impl, ctx_impl->ctx_read_buf.ctx_impl = ctx_impl; - ctx_impl->req.req.ctx = &ctx_impl->ctx; + ctx_impl->req.req.ctx = &ctx_impl->ctx; ctx_impl->req.req.unit = &lib->unit; - ctx_impl->read_port = NULL; + ctx_impl->read_port = NULL; ctx_impl->requests.slot = 0; return NXT_UNIT_OK; } - nxt_inline void -nxt_unit_ctx_use(nxt_unit_ctx_t *ctx) -{ - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_ctx_use(nxt_unit_ctx_t *ctx) { + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); nxt_atomic_fetch_add(&ctx_impl->use_count, 1); } - nxt_inline void -nxt_unit_ctx_release(nxt_unit_ctx_t *ctx) -{ +nxt_unit_ctx_release(nxt_unit_ctx_t *ctx) { long c; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -748,24 +792,20 @@ nxt_unit_ctx_release(nxt_unit_ctx_t *ctx) } } - nxt_inline void -nxt_unit_lib_use(nxt_unit_impl_t *lib) -{ +nxt_unit_lib_use(nxt_unit_impl_t *lib) { nxt_atomic_fetch_add(&lib->use_count, 1); } - nxt_inline void -nxt_unit_lib_release(nxt_unit_impl_t *lib) -{ +nxt_unit_lib_release(nxt_unit_impl_t *lib) { long c; - nxt_unit_process_t *process; + nxt_unit_process_t *process; c = nxt_atomic_fetch_add(&lib->use_count, -1); if (c == 1) { - for ( ;; ) { + for (;;) { pthread_mutex_lock(&lib->mutex); process = nxt_unit_process_pop_first(lib); @@ -795,26 +835,22 @@ nxt_unit_lib_release(nxt_unit_impl_t *lib) } } - nxt_inline void nxt_unit_mmap_buf_insert(nxt_unit_mmap_buf_t **head, - nxt_unit_mmap_buf_t *mmap_buf) -{ + nxt_unit_mmap_buf_t *mmap_buf) { mmap_buf->next = *head; if (mmap_buf->next != NULL) { mmap_buf->next->prev = &mmap_buf->next; } - *head = mmap_buf; + *head = mmap_buf; mmap_buf->prev = head; } - nxt_inline void nxt_unit_mmap_buf_insert_tail(nxt_unit_mmap_buf_t **prev, - nxt_unit_mmap_buf_t *mmap_buf) -{ + nxt_unit_mmap_buf_t *mmap_buf) { while (*prev != NULL) { prev = &(*prev)->next; } @@ -822,11 +858,9 @@ nxt_unit_mmap_buf_insert_tail(nxt_unit_mmap_buf_t **prev, nxt_unit_mmap_buf_insert(prev, mmap_buf); } - nxt_inline void -nxt_unit_mmap_buf_unlink(nxt_unit_mmap_buf_t *mmap_buf) -{ - nxt_unit_mmap_buf_t **prev; +nxt_unit_mmap_buf_unlink(nxt_unit_mmap_buf_t *mmap_buf) { + nxt_unit_mmap_buf_t **prev; prev = mmap_buf->prev; @@ -839,24 +873,22 @@ nxt_unit_mmap_buf_unlink(nxt_unit_mmap_buf_t *mmap_buf) } } - static int nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, nxt_unit_port_t *read_port, int *shared_port_fd, int *shared_queue_fd, - int *log_fd, uint32_t *stream, - uint32_t *shm_limit, uint32_t *request_limit) -{ - int rc; - int ready_fd, router_fd, read_in_fd, read_out_fd; - char *unit_init, *version_end, *vars; - size_t version_length; - int64_t ready_pid, router_pid, read_pid; - uint32_t ready_stream, router_id, ready_id, read_id; + int *log_fd, uint32_t *stream, uint32_t *shm_limit, + uint32_t *request_limit) { + int rc; + int ready_fd, router_fd, read_in_fd, read_out_fd; + char *unit_init, *version_end, *vars; + size_t version_length; + int64_t ready_pid, router_pid, read_pid; + uint32_t ready_stream, router_id, ready_id, read_id; unit_init = getenv(NXT_UNIT_INIT_ENV); if (nxt_slow_path(unit_init == NULL)) { nxt_unit_alert(NULL, "%s is not in the current environment", - NXT_UNIT_INIT_ENV); + NXT_UNIT_INIT_ENV); return NXT_UNIT_ERROR; } @@ -864,7 +896,7 @@ nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, version_end = strchr(unit_init, ';'); if (nxt_slow_path(version_end == NULL)) { nxt_unit_alert(NULL, "Unit version not found in %s=\"%s\"", - NXT_UNIT_INIT_ENV, unit_init); + NXT_UNIT_INIT_ENV, unit_init); return NXT_UNIT_ERROR; } @@ -875,9 +907,10 @@ nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, || memcmp(unit_init, NXT_VERSION, nxt_length(NXT_VERSION)); if (nxt_slow_path(rc != 0)) { - nxt_unit_alert(NULL, "versions mismatch: the Unit daemon has version " - "%.*s, while the app was compiled with libunit %s", - (int) version_length, unit_init, NXT_VERSION); + nxt_unit_alert(NULL, + "versions mismatch: the Unit daemon has version " + "%.*s, while the app was compiled with libunit %s", + (int) version_length, unit_init, NXT_VERSION); return NXT_UNIT_ERROR; } @@ -885,29 +918,28 @@ nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, vars = version_end + 1; rc = sscanf(vars, - "%"PRIu32";" - "%"PRId64",%"PRIu32",%d;" - "%"PRId64",%"PRIu32",%d;" - "%"PRId64",%"PRIu32",%d,%d;" - "%d,%d;" - "%d,%"PRIu32",%"PRIu32, - &ready_stream, - &ready_pid, &ready_id, &ready_fd, - &router_pid, &router_id, &router_fd, - &read_pid, &read_id, &read_in_fd, &read_out_fd, - shared_port_fd, shared_queue_fd, - log_fd, shm_limit, request_limit); + "%" PRIu32 ";" + "%" PRId64 ",%" PRIu32 ",%d;" + "%" PRId64 ",%" PRIu32 ",%d;" + "%" PRId64 ",%" PRIu32 ",%d,%d;" + "%d,%d;" + "%d,%" PRIu32 ",%" PRIu32, + &ready_stream, &ready_pid, &ready_id, &ready_fd, &router_pid, + &router_id, &router_fd, &read_pid, &read_id, &read_in_fd, &read_out_fd, + shared_port_fd, shared_queue_fd, log_fd, shm_limit, request_limit); if (nxt_slow_path(rc == EOF)) { - nxt_unit_alert(NULL, "sscanf(%s) failed: %s (%d) for %s env", - vars, strerror(errno), errno, NXT_UNIT_INIT_ENV); + nxt_unit_alert(NULL, "sscanf(%s) failed: %s (%d) for %s env", vars, + strerror(errno), errno, NXT_UNIT_INIT_ENV); return NXT_UNIT_ERROR; } if (nxt_slow_path(rc != 16)) { - nxt_unit_alert(NULL, "invalid number of variables in %s env: " - "found %d of %d in %s", NXT_UNIT_INIT_ENV, rc, 16, vars); + nxt_unit_alert(NULL, + "invalid number of variables in %s env: " + "found %d of %d in %s", + NXT_UNIT_INIT_ENV, rc, 16, vars); return NXT_UNIT_ERROR; } @@ -916,47 +948,46 @@ nxt_unit_read_env(nxt_unit_port_t *ready_port, nxt_unit_port_t *router_port, nxt_unit_port_id_init(&ready_port->id, (pid_t) ready_pid, ready_id); - ready_port->in_fd = -1; + ready_port->in_fd = -1; ready_port->out_fd = ready_fd; - ready_port->data = NULL; + ready_port->data = NULL; nxt_unit_port_id_init(&router_port->id, (pid_t) router_pid, router_id); - router_port->in_fd = -1; + router_port->in_fd = -1; router_port->out_fd = router_fd; - router_port->data = NULL; + router_port->data = NULL; nxt_unit_port_id_init(&read_port->id, (pid_t) read_pid, read_id); - read_port->in_fd = read_in_fd; + read_port->in_fd = read_in_fd; read_port->out_fd = read_out_fd; - read_port->data = NULL; + read_port->data = NULL; *stream = ready_stream; return NXT_UNIT_OK; } - static int -nxt_unit_ready(nxt_unit_ctx_t *ctx, int ready_fd, uint32_t stream, int queue_fd) -{ +nxt_unit_ready(nxt_unit_ctx_t *ctx, int ready_fd, uint32_t stream, + int queue_fd) { ssize_t res; nxt_send_oob_t oob; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; int fds[2] = {queue_fd, -1}; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - msg.stream = stream; - msg.pid = lib->pid; + msg.stream = stream; + msg.pid = lib->pid; msg.reply_port = 0; - msg.type = _NXT_PORT_MSG_PROCESS_READY; - msg.last = 1; - msg.mmap = 0; - msg.nf = 0; - msg.mf = 0; + msg.type = _NXT_PORT_MSG_PROCESS_READY; + msg.last = 1; + msg.mmap = 0; + msg.nf = 0; + msg.mf = 0; nxt_socket_msg_oob_init(&oob, fds); @@ -968,23 +999,21 @@ nxt_unit_ready(nxt_unit_ctx_t *ctx, int ready_fd, uint32_t stream, int queue_fd) return NXT_UNIT_OK; } - static int nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, - nxt_unit_request_info_t **preq) -{ - int rc; - pid_t pid; - uint8_t quit_param; - nxt_port_msg_t *port_msg; - nxt_unit_impl_t *lib; - nxt_unit_recv_msg_t recv_msg; + nxt_unit_request_info_t **preq) { + int rc; + pid_t pid; + uint8_t quit_param; + nxt_port_msg_t *port_msg; + nxt_unit_impl_t *lib; + nxt_unit_recv_msg_t recv_msg; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); recv_msg.incoming_buf = NULL; - recv_msg.fd[0] = -1; - recv_msg.fd[1] = -1; + recv_msg.fd[0] = -1; + recv_msg.fd[1] = -1; rc = nxt_socket_msg_oob_get_fds(&rbuf->oob, recv_msg.fd); if (nxt_slow_path(rc != NXT_OK)) { @@ -1010,30 +1039,29 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, port_msg = (nxt_port_msg_t *) rbuf->buf; - nxt_unit_debug(ctx, "#%"PRIu32": process message %d fd[0] %d fd[1] %d", - port_msg->stream, (int) port_msg->type, - recv_msg.fd[0], recv_msg.fd[1]); + nxt_unit_debug(ctx, "#%" PRIu32 ": process message %d fd[0] %d fd[1] %d", + port_msg->stream, (int) port_msg->type, recv_msg.fd[0], recv_msg.fd[1]); - recv_msg.stream = port_msg->stream; - recv_msg.pid = port_msg->pid; + recv_msg.stream = port_msg->stream; + recv_msg.pid = port_msg->pid; recv_msg.reply_port = port_msg->reply_port; - recv_msg.last = port_msg->last; - recv_msg.mmap = port_msg->mmap; + recv_msg.last = port_msg->last; + recv_msg.mmap = port_msg->mmap; recv_msg.start = port_msg + 1; - recv_msg.size = rbuf->size - sizeof(nxt_port_msg_t); + recv_msg.size = rbuf->size - sizeof(nxt_port_msg_t); if (nxt_slow_path(port_msg->type >= NXT_PORT_MSG_MAX)) { - nxt_unit_alert(ctx, "#%"PRIu32": unknown message type (%d)", - port_msg->stream, (int) port_msg->type); + nxt_unit_alert(ctx, "#%" PRIu32 ": unknown message type (%d)", + port_msg->stream, (int) port_msg->type); rc = NXT_UNIT_ERROR; goto done; } /* Fragmentation is unsupported. */ if (nxt_slow_path(port_msg->nf != 0 || port_msg->mf != 0)) { - nxt_unit_alert(ctx, "#%"PRIu32": fragmented message type (%d)", - port_msg->stream, (int) port_msg->type); + nxt_unit_alert(ctx, "#%" PRIu32 ": fragmented message type (%d)", + port_msg->stream, (int) port_msg->type); rc = NXT_UNIT_ERROR; goto done; } @@ -1052,7 +1080,6 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, } switch (port_msg->type) { - case _NXT_PORT_MSG_RPC_READY: rc = NXT_UNIT_OK; break; @@ -1065,8 +1092,8 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, quit_param = NXT_QUIT_NORMAL; } - nxt_unit_debug(ctx, "#%"PRIu32": %squit", port_msg->stream, - (quit_param == NXT_QUIT_GRACEFUL ? "graceful " : "")); + nxt_unit_debug(ctx, "#%" PRIu32 ": %squit", port_msg->stream, + (quit_param == NXT_QUIT_GRACEFUL ? "graceful " : "")); nxt_unit_quit(ctx, quit_param); @@ -1082,13 +1109,13 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, break; case _NXT_PORT_MSG_CHANGE_FILE: - nxt_unit_debug(ctx, "#%"PRIu32": change_file: fd %d", - port_msg->stream, recv_msg.fd[0]); + nxt_unit_debug(ctx, "#%" PRIu32 ": change_file: fd %d", + port_msg->stream, recv_msg.fd[0]); if (dup2(recv_msg.fd[0], lib->log_fd) == -1) { - nxt_unit_alert(ctx, "#%"PRIu32": dup2(%d, %d) failed: %s (%d)", - port_msg->stream, recv_msg.fd[0], lib->log_fd, - strerror(errno), errno); + nxt_unit_alert(ctx, "#%" PRIu32 ": dup2(%d, %d) failed: %s (%d)", + port_msg->stream, recv_msg.fd[0], lib->log_fd, strerror(errno), + errno); rc = NXT_UNIT_ERROR; goto done; @@ -1099,8 +1126,8 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, case _NXT_PORT_MSG_MMAP: if (nxt_slow_path(recv_msg.fd[0] < 0)) { - nxt_unit_alert(ctx, "#%"PRIu32": invalid fd %d for mmap", - port_msg->stream, recv_msg.fd[0]); + nxt_unit_alert(ctx, "#%" PRIu32 ": invalid fd %d for mmap", + port_msg->stream, recv_msg.fd[0]); rc = NXT_UNIT_ERROR; goto done; @@ -1123,9 +1150,10 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, case _NXT_PORT_MSG_REMOVE_PID: if (nxt_slow_path(recv_msg.size != sizeof(pid))) { - nxt_unit_alert(ctx, "#%"PRIu32": remove_pid: invalid message size " - "(%d != %d)", port_msg->stream, (int) recv_msg.size, - (int) sizeof(pid)); + nxt_unit_alert(ctx, + "#%" PRIu32 ": remove_pid: invalid message size " + "(%d != %d)", + port_msg->stream, (int) recv_msg.size, (int) sizeof(pid)); rc = NXT_UNIT_ERROR; goto done; @@ -1133,8 +1161,8 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, memcpy(&pid, recv_msg.start, sizeof(pid)); - nxt_unit_debug(ctx, "#%"PRIu32": remove_pid: %d", - port_msg->stream, (int) pid); + nxt_unit_debug(ctx, "#%" PRIu32 ": remove_pid: %d", port_msg->stream, + (int) pid); nxt_unit_remove_pid(lib, pid); @@ -1146,8 +1174,8 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, break; default: - nxt_unit_alert(ctx, "#%"PRIu32": ignore message type: %d", - port_msg->stream, (int) port_msg->type); + nxt_unit_alert(ctx, "#%" PRIu32 ": ignore message type: %d", + port_msg->stream, (int) port_msg->type); rc = NXT_UNIT_ERROR; goto done; @@ -1177,34 +1205,33 @@ nxt_unit_process_msg(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf, return rc; } - static int -nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) -{ - void *mem; +nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) { + void *mem; nxt_unit_port_t new_port, *port; - nxt_port_msg_new_port_t *new_port_msg; + nxt_port_msg_new_port_t *new_port_msg; if (nxt_slow_path(recv_msg->size != sizeof(nxt_port_msg_new_port_t))) { - nxt_unit_warn(ctx, "#%"PRIu32": new_port: " - "invalid message size (%d)", - recv_msg->stream, (int) recv_msg->size); + nxt_unit_warn(ctx, + "#%" PRIu32 ": new_port: " + "invalid message size (%d)", + recv_msg->stream, (int) recv_msg->size); return NXT_UNIT_ERROR; } if (nxt_slow_path(recv_msg->fd[0] < 0)) { - nxt_unit_alert(ctx, "#%"PRIu32": invalid fd %d for new port", - recv_msg->stream, recv_msg->fd[0]); + nxt_unit_alert(ctx, "#%" PRIu32 ": invalid fd %d for new port", + recv_msg->stream, recv_msg->fd[0]); return NXT_UNIT_ERROR; } new_port_msg = recv_msg->start; - nxt_unit_debug(ctx, "#%"PRIu32": new_port: port{%d,%d} fd[0] %d fd[1] %d", - recv_msg->stream, (int) new_port_msg->pid, - (int) new_port_msg->id, recv_msg->fd[0], recv_msg->fd[1]); + nxt_unit_debug(ctx, "#%" PRIu32 ": new_port: port{%d,%d} fd[0] %d fd[1] %d", + recv_msg->stream, (int) new_port_msg->pid, (int) new_port_msg->id, + recv_msg->fd[0], recv_msg->fd[1]); if (nxt_slow_path(nxt_unit_fd_blocking(recv_msg->fd[0]) != NXT_UNIT_OK)) { return NXT_UNIT_ERROR; @@ -1212,15 +1239,15 @@ nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) nxt_unit_port_id_init(&new_port.id, new_port_msg->pid, new_port_msg->id); - new_port.in_fd = -1; + new_port.in_fd = -1; new_port.out_fd = recv_msg->fd[0]; mem = mmap(NULL, sizeof(nxt_port_queue_t), PROT_READ | PROT_WRITE, - MAP_SHARED, recv_msg->fd[1], 0); + MAP_SHARED, recv_msg->fd[1], 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", recv_msg->fd[1], - strerror(errno), errno); + strerror(errno), errno); return NXT_UNIT_ERROR; } @@ -1239,12 +1266,10 @@ nxt_unit_process_new_port(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) return NXT_UNIT_OK; } - static int -nxt_unit_ctx_ready(nxt_unit_ctx_t *ctx) -{ - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_ctx_ready(nxt_unit_ctx_t *ctx) { + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -1279,38 +1304,38 @@ nxt_unit_ctx_ready(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static int nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, - nxt_unit_request_info_t **preq) -{ + nxt_unit_request_info_t **preq) { int res; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; nxt_unit_port_id_t port_id; - nxt_unit_request_t *r; - nxt_unit_mmap_buf_t *b; - nxt_unit_request_info_t *req; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_request_t *r; + nxt_unit_mmap_buf_t *b; + nxt_unit_request_info_t *req; + nxt_unit_request_info_impl_t *req_impl; if (nxt_slow_path(recv_msg->mmap == 0)) { - nxt_unit_warn(ctx, "#%"PRIu32": data is not in shared memory", - recv_msg->stream); + nxt_unit_warn(ctx, "#%" PRIu32 ": data is not in shared memory", + recv_msg->stream); return NXT_UNIT_ERROR; } if (nxt_slow_path(recv_msg->size < sizeof(nxt_unit_request_t))) { - nxt_unit_warn(ctx, "#%"PRIu32": data too short: %d while at least " - "%d expected", recv_msg->stream, (int) recv_msg->size, - (int) sizeof(nxt_unit_request_t)); + nxt_unit_warn(ctx, + "#%" PRIu32 ": data too short: %d while at least " + "%d expected", + recv_msg->stream, (int) recv_msg->size, + (int) sizeof(nxt_unit_request_t)); return NXT_UNIT_ERROR; } req_impl = nxt_unit_request_info_get(ctx); if (nxt_slow_path(req_impl == NULL)) { - nxt_unit_warn(ctx, "#%"PRIu32": request info allocation failed", - recv_msg->stream); + nxt_unit_warn(ctx, "#%" PRIu32 ": request info allocation failed", + recv_msg->stream); return NXT_UNIT_ERROR; } @@ -1321,15 +1346,15 @@ nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, b = recv_msg->incoming_buf; - req->request_buf = &b->buf; - req->response = NULL; + req->request_buf = &b->buf; + req->response = NULL; req->response_buf = NULL; r = req->request; req->content_length = r->content_length; - req->content_buf = req->request_buf; + req->content_buf = req->request_buf; req->content_buf->free = nxt_unit_sptr_get(&r->preread_content); req_impl->stream = recv_msg->stream; @@ -1341,24 +1366,22 @@ nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, } /* "Move" incoming buffer list to req_impl. */ - req_impl->incoming_buf = recv_msg->incoming_buf; + req_impl->incoming_buf = recv_msg->incoming_buf; req_impl->incoming_buf->prev = &req_impl->incoming_buf; - recv_msg->incoming_buf = NULL; + recv_msg->incoming_buf = NULL; req->content_fd = recv_msg->fd[0]; recv_msg->fd[0] = -1; req->response_max_fields = 0; - req_impl->state = NXT_UNIT_RS_START; - req_impl->websocket = 0; - req_impl->in_hash = 0; + req_impl->state = NXT_UNIT_RS_START; + req_impl->websocket = 0; + req_impl->in_hash = 0; - nxt_unit_debug(ctx, "#%"PRIu32": %.*s %.*s (%d)", recv_msg->stream, - (int) r->method_length, - (char *) nxt_unit_sptr_get(&r->method), - (int) r->target_length, - (char *) nxt_unit_sptr_get(&r->target), - (int) r->content_length); + nxt_unit_debug(ctx, "#%" PRIu32 ": %.*s %.*s (%d)", recv_msg->stream, + (int) r->method_length, (char *) nxt_unit_sptr_get(&r->method), + (int) r->target_length, (char *) nxt_unit_sptr_get(&r->target), + (int) r->content_length); nxt_unit_port_id_init(&port_id, recv_msg->pid, recv_msg->reply_port); @@ -1378,8 +1401,7 @@ nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); if (req->content_length - > (uint64_t) (req->content_buf->end - req->content_buf->free)) - { + > (uint64_t) (req->content_buf->end - req->content_buf->free)) { res = nxt_unit_request_hash_add(ctx, req); if (nxt_slow_path(res != NXT_UNIT_OK)) { nxt_unit_req_warn(req, "failed to add request to hash"); @@ -1409,14 +1431,12 @@ nxt_unit_process_req_headers(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, return NXT_UNIT_OK; } - static int -nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) -{ +nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) { uint64_t l; - nxt_unit_impl_t *lib; - nxt_unit_mmap_buf_t *b; - nxt_unit_request_info_t *req; + nxt_unit_impl_t *lib; + nxt_unit_mmap_buf_t *b; + nxt_unit_request_info_t *req; req = nxt_unit_request_hash_find(ctx, recv_msg->stream, recv_msg->last); if (req == NULL) { @@ -1426,8 +1446,8 @@ nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) l = req->content_buf->end - req->content_buf->free; for (b = recv_msg->incoming_buf; b != NULL; b = b->next) { - b->req = req; - l += b->buf.end - b->buf.free; + b->req = req; + l += b->buf.end - b->buf.free; } if (recv_msg->incoming_buf != NULL) { @@ -1438,7 +1458,7 @@ nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) } /* "Move" incoming buffer list to req_impl. */ - b->next = recv_msg->incoming_buf; + b->next = recv_msg->incoming_buf; b->next->prev = &b->next; recv_msg->incoming_buf = NULL; @@ -1462,22 +1482,20 @@ nxt_unit_process_req_body(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) return NXT_UNIT_OK; } - static int nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, - nxt_unit_port_id_t *port_id) -{ + nxt_unit_port_id_t *port_id) { int res; - nxt_unit_ctx_t *ctx; - nxt_unit_impl_t *lib; - nxt_unit_port_t *port; - nxt_unit_process_t *process; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_port_impl_t *port_impl; - nxt_unit_request_info_impl_t *req_impl; - - ctx = req->ctx; - lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); + nxt_unit_ctx_t *ctx; + nxt_unit_impl_t *lib; + nxt_unit_port_t *port; + nxt_unit_process_t *process; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_port_impl_t *port_impl; + nxt_unit_request_info_impl_t *req_impl; + + ctx = req->ctx; + lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); pthread_mutex_lock(&lib->mutex); @@ -1485,26 +1503,27 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, port = nxt_unit_port_hash_find(&lib->ports, port_id, 0); if (nxt_fast_path(port != NULL)) { - port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); + port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); req->response_port = port; if (nxt_fast_path(port_impl->ready)) { pthread_mutex_unlock(&lib->mutex); nxt_unit_debug(ctx, "check_response_port: found port{%d,%d}", - (int) port->id.pid, (int) port->id.id); + (int) port->id.pid, (int) port->id.id); return NXT_UNIT_OK; } - nxt_unit_debug(ctx, "check_response_port: " - "port{%d,%d} already requested", - (int) port->id.pid, (int) port->id.id); + nxt_unit_debug(ctx, + "check_response_port: " + "port{%d,%d} already requested", + (int) port->id.pid, (int) port->id.id); req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); nxt_queue_insert_tail(&port_impl->awaiting_req, - &req_impl->port_wait_link); + &req_impl->port_wait_link); pthread_mutex_unlock(&lib->mutex); @@ -1516,7 +1535,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, port_impl = nxt_unit_malloc(ctx, sizeof(nxt_unit_port_impl_t)); if (nxt_slow_path(port_impl == NULL)) { nxt_unit_alert(ctx, "check_response_port: malloc(%d) failed", - (int) sizeof(nxt_unit_port_impl_t)); + (int) sizeof(nxt_unit_port_impl_t)); pthread_mutex_unlock(&lib->mutex); @@ -1525,15 +1544,15 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, port = &port_impl->port; - port->id = *port_id; - port->in_fd = -1; + port->id = *port_id; + port->in_fd = -1; port->out_fd = -1; - port->data = NULL; + port->data = NULL; res = nxt_unit_port_hash_add(&lib->ports, port); if (nxt_slow_path(res != NXT_UNIT_OK)) { nxt_unit_alert(ctx, "check_response_port: %d,%d hash_add failed", - port->id.pid, port->id.id); + port->id.pid, port->id.id); pthread_mutex_unlock(&lib->mutex); @@ -1545,7 +1564,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, process = nxt_unit_process_find(lib, port_id->pid, 0); if (nxt_slow_path(process == NULL)) { nxt_unit_alert(ctx, "check_response_port: process %d not found", - port->id.pid); + port->id.pid); nxt_unit_port_hash_find(&lib->ports, port_id, 1); @@ -1558,8 +1577,8 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, nxt_queue_insert_tail(&process->ports, &port_impl->link); - port_impl->process = process; - port_impl->queue = NULL; + port_impl->process = process; + port_impl->queue = NULL; port_impl->from_socket = 0; port_impl->socket_rbuf = NULL; @@ -1570,7 +1589,7 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, nxt_queue_insert_tail(&port_impl->awaiting_req, &req_impl->port_wait_link); port_impl->use_count = 2; - port_impl->ready = 0; + port_impl->ready = 0; req->response_port = port; @@ -1586,29 +1605,27 @@ nxt_unit_request_check_response_port(nxt_unit_request_info_t *req, return NXT_UNIT_AGAIN; } - static int -nxt_unit_send_req_headers_ack(nxt_unit_request_info_t *req) -{ +nxt_unit_send_req_headers_ack(nxt_unit_request_info_t *req) { ssize_t res; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; - lib = nxt_container_of(req->ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(req->ctx->unit, nxt_unit_impl_t, unit); ctx_impl = nxt_container_of(req->ctx, nxt_unit_ctx_impl_t, ctx); req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); memset(&msg, 0, sizeof(nxt_port_msg_t)); - msg.stream = req_impl->stream; - msg.pid = lib->pid; + msg.stream = req_impl->stream; + msg.pid = lib->pid; msg.reply_port = ctx_impl->read_port->id.id; - msg.type = _NXT_PORT_MSG_REQ_HEADERS_ACK; + msg.type = _NXT_PORT_MSG_REQ_HEADERS_ACK; - res = nxt_unit_port_send(req->ctx, req->response_port, - &msg, sizeof(msg), NULL); + res = nxt_unit_port_send(req->ctx, req->response_port, &msg, sizeof(msg), + NULL); if (nxt_slow_path(res != sizeof(msg))) { return NXT_UNIT_ERROR; } @@ -1616,17 +1633,15 @@ nxt_unit_send_req_headers_ack(nxt_unit_request_info_t *req) return NXT_UNIT_OK; } - static int -nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) -{ +nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) { size_t hsize; - nxt_unit_impl_t *lib; - nxt_unit_mmap_buf_t *b; - nxt_unit_callbacks_t *cb; - nxt_unit_request_info_t *req; - nxt_unit_request_info_impl_t *req_impl; - nxt_unit_websocket_frame_impl_t *ws_impl; + nxt_unit_impl_t *lib; + nxt_unit_mmap_buf_t *b; + nxt_unit_callbacks_t *cb; + nxt_unit_request_info_t *req; + nxt_unit_request_info_impl_t *req_impl; + nxt_unit_websocket_frame_impl_t *ws_impl; req = nxt_unit_request_hash_find(ctx, recv_msg->stream, recv_msg->last); if (nxt_slow_path(req == NULL)) { @@ -1636,13 +1651,14 @@ nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - cb = &lib->callbacks; + cb = &lib->callbacks; if (cb->websocket_handler && recv_msg->size >= 2) { ws_impl = nxt_unit_websocket_frame_get(ctx); if (nxt_slow_path(ws_impl == NULL)) { - nxt_unit_warn(ctx, "#%"PRIu32": websocket frame allocation failed", - req_impl->stream); + nxt_unit_warn(ctx, + "#%" PRIu32 ": websocket frame allocation failed", + req_impl->stream); return NXT_UNIT_ERROR; } @@ -1657,8 +1673,8 @@ nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) } /* "Move" incoming buffer list to ws_impl. */ - ws_impl->buf = recv_msg->incoming_buf; - ws_impl->buf->prev = &ws_impl->buf; + ws_impl->buf = recv_msg->incoming_buf; + ws_impl->buf->prev = &ws_impl->buf; recv_msg->incoming_buf = NULL; b = ws_impl->buf; @@ -1666,25 +1682,25 @@ nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) } else { b = nxt_unit_mmap_buf_get(ctx); if (nxt_slow_path(b == NULL)) { - nxt_unit_alert(ctx, "#%"PRIu32": failed to allocate buf", - req_impl->stream); + nxt_unit_alert(ctx, "#%" PRIu32 ": failed to allocate buf", + req_impl->stream); nxt_unit_websocket_frame_release(&ws_impl->ws); return NXT_UNIT_ERROR; } - b->req = req; + b->req = req; b->buf.start = recv_msg->start; - b->buf.free = b->buf.start; - b->buf.end = b->buf.start + recv_msg->size; + b->buf.free = b->buf.start; + b->buf.end = b->buf.start + recv_msg->size; nxt_unit_mmap_buf_insert(&ws_impl->buf, b); } ws_impl->ws.header = (void *) b->buf.start; - ws_impl->ws.payload_len = nxt_websocket_frame_payload_len( - ws_impl->ws.header); + ws_impl->ws.payload_len + = nxt_websocket_frame_payload_len(ws_impl->ws.header); hsize = nxt_websocket_frame_header_size(ws_impl->ws.header); @@ -1697,13 +1713,13 @@ nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) b->buf.free += hsize; - ws_impl->ws.content_buf = &b->buf; + ws_impl->ws.content_buf = &b->buf; ws_impl->ws.content_length = ws_impl->ws.payload_len; - nxt_unit_req_debug(req, "websocket_handler: opcode=%d, " - "payload_len=%"PRIu64, - ws_impl->ws.header->opcode, - ws_impl->ws.payload_len); + nxt_unit_req_debug(req, + "websocket_handler: opcode=%d, " + "payload_len=%" PRIu64, + ws_impl->ws.header->opcode, ws_impl->ws.payload_len); cb->websocket_handler(&ws_impl->ws); } @@ -1722,15 +1738,13 @@ nxt_unit_process_websocket(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg) return NXT_UNIT_OK; } - static int -nxt_unit_process_shm_ack(nxt_unit_ctx_t *ctx) -{ - nxt_unit_impl_t *lib; - nxt_unit_callbacks_t *cb; +nxt_unit_process_shm_ack(nxt_unit_ctx_t *ctx) { + nxt_unit_impl_t *lib; + nxt_unit_callbacks_t *cb; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - cb = &lib->callbacks; + cb = &lib->callbacks; if (cb->shm_ack_handler != NULL) { cb->shm_ack_handler(ctx); @@ -1739,14 +1753,12 @@ nxt_unit_process_shm_ack(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static nxt_unit_request_info_impl_t * -nxt_unit_request_info_get(nxt_unit_ctx_t *ctx) -{ - nxt_unit_impl_t *lib; - nxt_queue_link_t *lnk; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_request_info_get(nxt_unit_ctx_t *ctx) { + nxt_unit_impl_t *lib; + nxt_queue_link_t *lnk; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -1757,14 +1769,14 @@ nxt_unit_request_info_get(nxt_unit_ctx_t *ctx) if (nxt_queue_is_empty(&ctx_impl->free_req)) { pthread_mutex_unlock(&ctx_impl->mutex); - req_impl = nxt_unit_malloc(ctx, sizeof(nxt_unit_request_info_impl_t) - + lib->request_data_size); + req_impl = nxt_unit_malloc(ctx, + sizeof(nxt_unit_request_info_impl_t) + lib->request_data_size); if (nxt_slow_path(req_impl == NULL)) { return NULL; } req_impl->req.unit = ctx->unit; - req_impl->req.ctx = ctx; + req_impl->req.ctx = ctx; pthread_mutex_lock(&ctx_impl->mutex); @@ -1784,19 +1796,17 @@ nxt_unit_request_info_get(nxt_unit_ctx_t *ctx) return req_impl; } - static void -nxt_unit_request_info_release(nxt_unit_request_info_t *req) -{ - nxt_unit_ctx_t *ctx; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_request_info_release(nxt_unit_request_info_t *req) { + nxt_unit_ctx_t *ctx; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; - ctx = req->ctx; + ctx = req->ctx; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); - req->response = NULL; + req->response = NULL; req->response_buf = NULL; if (req_impl->in_hash) { @@ -1838,11 +1848,9 @@ nxt_unit_request_info_release(nxt_unit_request_info_t *req) } } - static void -nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req_impl) -{ - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req_impl) { + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(req_impl->req.ctx, nxt_unit_ctx_impl_t, ctx); @@ -1853,13 +1861,11 @@ nxt_unit_request_info_free(nxt_unit_request_info_impl_t *req_impl) } } - static nxt_unit_websocket_frame_impl_t * -nxt_unit_websocket_frame_get(nxt_unit_ctx_t *ctx) -{ - nxt_queue_link_t *lnk; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_websocket_frame_impl_t *ws_impl; +nxt_unit_websocket_frame_get(nxt_unit_ctx_t *ctx) { + nxt_queue_link_t *lnk; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_websocket_frame_impl_t *ws_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -1887,11 +1893,9 @@ nxt_unit_websocket_frame_get(nxt_unit_ctx_t *ctx) return ws_impl; } - static void -nxt_unit_websocket_frame_release(nxt_unit_websocket_frame_t *ws) -{ - nxt_unit_websocket_frame_impl_t *ws_impl; +nxt_unit_websocket_frame_release(nxt_unit_websocket_frame_t *ws) { + nxt_unit_websocket_frame_impl_t *ws_impl; ws_impl = nxt_container_of(ws, nxt_unit_websocket_frame_impl_t, ws); @@ -1908,29 +1912,25 @@ nxt_unit_websocket_frame_release(nxt_unit_websocket_frame_t *ws) pthread_mutex_unlock(&ws_impl->ctx_impl->mutex); } - static void nxt_unit_websocket_frame_free(nxt_unit_ctx_t *ctx, - nxt_unit_websocket_frame_impl_t *ws_impl) -{ + nxt_unit_websocket_frame_impl_t *ws_impl) { nxt_queue_remove(&ws_impl->link); nxt_unit_free(ctx, ws_impl); } - uint16_t -nxt_unit_field_hash(const char *name, size_t name_length) -{ +nxt_unit_field_hash(const char *name, size_t name_length) { u_char ch; uint32_t hash; - const char *p, *end; + const char *p, *end; hash = 159406; /* Magic value copied from nxt_http_parse.c */ - end = name + name_length; + end = name + name_length; for (p = name; p < end; p++) { - ch = *p; + ch = *p; hash = (hash << 4) + hash + nxt_lowcase(ch); } @@ -1939,22 +1939,20 @@ nxt_unit_field_hash(const char *name, size_t name_length) return hash; } - void -nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) -{ - char *name; +nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) { + char *name; uint32_t i, j; - nxt_unit_field_t *fields, f; - nxt_unit_request_t *r; + nxt_unit_field_t *fields, f; + nxt_unit_request_t *r; - static const nxt_str_t content_length = nxt_string("content-length"); - static const nxt_str_t content_type = nxt_string("content-type"); - static const nxt_str_t cookie = nxt_string("cookie"); + static const nxt_str_t content_length = nxt_string("content-length"); + static const nxt_str_t content_type = nxt_string("content-type"); + static const nxt_str_t cookie = nxt_string("cookie"); nxt_unit_req_debug(req, "group_dup_fields"); - r = req->request; + r = req->request; fields = r->fields; for (i = 0; i < r->fields_count; i++) { @@ -1964,8 +1962,8 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) case NXT_UNIT_HASH_CONTENT_LENGTH: if (fields[i].name_length == content_length.length && nxt_unit_memcasecmp(name, content_length.start, - content_length.length) == 0) - { + content_length.length) + == 0) { r->content_length_field = i; } @@ -1974,8 +1972,8 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) case NXT_UNIT_HASH_CONTENT_TYPE: if (fields[i].name_length == content_type.length && nxt_unit_memcasecmp(name, content_type.start, - content_type.length) == 0) - { + content_type.length) + == 0) { r->content_type_field = i; } @@ -1983,9 +1981,8 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) case NXT_UNIT_HASH_COOKIE: if (fields[i].name_length == cookie.length - && nxt_unit_memcasecmp(name, cookie.start, - cookie.length) == 0) - { + && nxt_unit_memcasecmp(name, cookie.start, cookie.length) + == 0) { r->cookie_field = i; } @@ -1995,19 +1992,18 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) for (j = i + 1; j < r->fields_count; j++) { if (fields[i].hash != fields[j].hash || fields[i].name_length != fields[j].name_length - || nxt_unit_memcasecmp(name, - nxt_unit_sptr_get(&fields[j].name), - fields[j].name_length) != 0) - { + || nxt_unit_memcasecmp(name, nxt_unit_sptr_get(&fields[j].name), + fields[j].name_length) + != 0) { continue; } - f = fields[j]; + f = fields[j]; f.value.offset += (j - (i + 1)) * sizeof(f); while (j > i + 1) { - fields[j] = fields[j - 1]; - fields[j].name.offset -= sizeof(f); + fields[j] = fields[j - 1]; + fields[j].name.offset -= sizeof(f); fields[j].value.offset -= sizeof(f); j--; } @@ -2022,14 +2018,12 @@ nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req) } } - int -nxt_unit_response_init(nxt_unit_request_info_t *req, - uint16_t status, uint32_t max_fields_count, uint32_t max_fields_size) -{ +nxt_unit_response_init(nxt_unit_request_info_t *req, uint16_t status, + uint32_t max_fields_count, uint32_t max_fields_size) { uint32_t buf_size; - nxt_unit_buf_t *buf; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_buf_t *buf; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2040,7 +2034,7 @@ nxt_unit_response_init(nxt_unit_request_info_t *req, } nxt_unit_req_debug(req, "init: %d, max fields %d/%d", (int) status, - (int) max_fields_count, (int) max_fields_size); + (int) max_fields_count, (int) max_fields_size); if (nxt_slow_path(req_impl->state >= NXT_UNIT_RS_RESPONSE_INIT)) { nxt_unit_req_debug(req, "duplicate response init"); @@ -2063,8 +2057,8 @@ nxt_unit_response_init(nxt_unit_request_info_t *req, nxt_unit_buf_free(buf); - req->response_buf = NULL; - req->response = NULL; + req->response_buf = NULL; + req->response = NULL; req->response_max_fields = 0; req_impl->state = NXT_UNIT_RS_START; @@ -2081,29 +2075,27 @@ nxt_unit_response_init(nxt_unit_request_info_t *req, req->response_buf = buf; - req->response = (nxt_unit_response_t *) buf->start; + req->response = (nxt_unit_response_t *) buf->start; req->response->status = status; buf->free = buf->start + sizeof(nxt_unit_response_t) + max_fields_count * sizeof(nxt_unit_field_t); req->response_max_fields = max_fields_count; - req_impl->state = NXT_UNIT_RS_RESPONSE_INIT; + req_impl->state = NXT_UNIT_RS_RESPONSE_INIT; return NXT_UNIT_OK; } - int nxt_unit_response_realloc(nxt_unit_request_info_t *req, - uint32_t max_fields_count, uint32_t max_fields_size) -{ - char *p; + uint32_t max_fields_count, uint32_t max_fields_size) { + char *p; uint32_t i, buf_size; - nxt_unit_buf_t *buf; - nxt_unit_field_t *f, *src; - nxt_unit_response_t *resp; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_buf_t *buf; + nxt_unit_field_t *f, *src; + nxt_unit_response_t *resp; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2133,7 +2125,7 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, + max_fields_count * (sizeof(nxt_unit_field_t) + 2) + max_fields_size; - nxt_unit_req_debug(req, "realloc %"PRIu32"", buf_size); + nxt_unit_req_debug(req, "realloc %" PRIu32 "", buf_size); buf = nxt_unit_response_buf_alloc(req, buf_size); if (nxt_slow_path(buf == NULL)) { @@ -2145,7 +2137,7 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, memset(resp, 0, sizeof(nxt_unit_response_t)); - resp->status = req->response->status; + resp->status = req->response->status; resp->content_length = req->response->content_length; p = buf->start + sizeof(nxt_unit_response_t) @@ -2160,26 +2152,26 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, } if (nxt_slow_path(src->name_length + src->value_length + 2 - > (uint32_t) (buf->end - p))) - { - nxt_unit_req_warn(req, "realloc: not enough space for field" - " #%"PRIu32" (%p), (%"PRIu32" + %"PRIu32") required", - i, src, src->name_length, src->value_length); + > (uint32_t) (buf->end - p))) { + nxt_unit_req_warn(req, + "realloc: not enough space for field" + " #%" PRIu32 " (%p), (%" PRIu32 " + %" PRIu32 ") required", + i, src, src->name_length, src->value_length); goto fail; } nxt_unit_sptr_set(&f->name, p); - p = nxt_cpymem(p, nxt_unit_sptr_get(&src->name), src->name_length); + p = nxt_cpymem(p, nxt_unit_sptr_get(&src->name), src->name_length); *p++ = '\0'; nxt_unit_sptr_set(&f->value, p); - p = nxt_cpymem(p, nxt_unit_sptr_get(&src->value), src->value_length); + p = nxt_cpymem(p, nxt_unit_sptr_get(&src->value), src->value_length); *p++ = '\0'; - f->hash = src->hash; - f->skip = 0; - f->name_length = src->name_length; + f->hash = src->hash; + f->skip = 0; + f->name_length = src->name_length; f->value_length = src->value_length; resp->fields_count++; @@ -2188,29 +2180,29 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, if (req->response->piggyback_content_length > 0) { if (nxt_slow_path(req->response->piggyback_content_length - > (uint32_t) (buf->end - p))) - { - nxt_unit_req_warn(req, "realloc: not enought space for content" - " #%"PRIu32", %"PRIu32" required", - i, req->response->piggyback_content_length); + > (uint32_t) (buf->end - p))) { + nxt_unit_req_warn(req, + "realloc: not enought space for content" + " #%" PRIu32 ", %" PRIu32 " required", + i, req->response->piggyback_content_length); goto fail; } - resp->piggyback_content_length = - req->response->piggyback_content_length; + resp->piggyback_content_length + = req->response->piggyback_content_length; nxt_unit_sptr_set(&resp->piggyback_content, p); p = nxt_cpymem(p, nxt_unit_sptr_get(&req->response->piggyback_content), - req->response->piggyback_content_length); + req->response->piggyback_content_length); } buf->free = p; nxt_unit_buf_free(req->response_buf); - req->response = resp; - req->response_buf = buf; + req->response = resp; + req->response_buf = buf; req->response_max_fields = max_fields_count; return NXT_UNIT_OK; @@ -2222,33 +2214,28 @@ nxt_unit_response_realloc(nxt_unit_request_info_t *req, return NXT_UNIT_ERROR; } - int -nxt_unit_response_is_init(nxt_unit_request_info_t *req) -{ - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_response_is_init(nxt_unit_request_info_t *req) { + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); return req_impl->state >= NXT_UNIT_RS_RESPONSE_INIT; } - int -nxt_unit_response_add_field(nxt_unit_request_info_t *req, - const char *name, uint8_t name_length, - const char *value, uint32_t value_length) -{ - nxt_unit_buf_t *buf; - nxt_unit_field_t *f; - nxt_unit_response_t *resp; - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_response_add_field(nxt_unit_request_info_t *req, const char *name, + uint8_t name_length, const char *value, uint32_t value_length) { + nxt_unit_buf_t *buf; + nxt_unit_field_t *f; + nxt_unit_response_t *resp; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); if (nxt_slow_path(req_impl->state != NXT_UNIT_RS_RESPONSE_INIT)) { nxt_unit_req_warn(req, "add_field: response not initialized or " - "already sent"); + "already sent"); return NXT_UNIT_ERROR; } @@ -2257,7 +2244,7 @@ nxt_unit_response_add_field(nxt_unit_request_info_t *req, if (nxt_slow_path(resp->fields_count >= req->response_max_fields)) { nxt_unit_req_warn(req, "add_field: too many response fields (%d)", - (int) resp->fields_count); + (int) resp->fields_count); return NXT_UNIT_ERROR; } @@ -2265,31 +2252,28 @@ nxt_unit_response_add_field(nxt_unit_request_info_t *req, buf = req->response_buf; if (nxt_slow_path(name_length + value_length + 2 - > (uint32_t) (buf->end - buf->free))) - { + > (uint32_t) (buf->end - buf->free))) { nxt_unit_req_warn(req, "add_field: response buffer overflow"); return NXT_UNIT_ERROR; } - nxt_unit_req_debug(req, "add_field #%"PRIu32": %.*s: %.*s", - resp->fields_count, - (int) name_length, name, - (int) value_length, value); + nxt_unit_req_debug(req, "add_field #%" PRIu32 ": %.*s: %.*s", + resp->fields_count, (int) name_length, name, (int) value_length, value); f = resp->fields + resp->fields_count; nxt_unit_sptr_set(&f->name, buf->free); - buf->free = nxt_cpymem(buf->free, name, name_length); + buf->free = nxt_cpymem(buf->free, name, name_length); *buf->free++ = '\0'; nxt_unit_sptr_set(&f->value, buf->free); - buf->free = nxt_cpymem(buf->free, value, value_length); + buf->free = nxt_cpymem(buf->free, value, value_length); *buf->free++ = '\0'; - f->hash = nxt_unit_field_hash(name, name_length); - f->skip = 0; - f->name_length = name_length; + f->hash = nxt_unit_field_hash(name, name_length); + f->skip = 0; + f->name_length = name_length; f->value_length = value_length; resp->fields_count++; @@ -2297,14 +2281,12 @@ nxt_unit_response_add_field(nxt_unit_request_info_t *req, return NXT_UNIT_OK; } - int -nxt_unit_response_add_content(nxt_unit_request_info_t *req, - const void* src, uint32_t size) -{ - nxt_unit_buf_t *buf; - nxt_unit_response_t *resp; - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_response_add_content(nxt_unit_request_info_t *req, const void *src, + uint32_t size) { + nxt_unit_buf_t *buf; + nxt_unit_response_t *resp; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2342,13 +2324,11 @@ nxt_unit_response_add_content(nxt_unit_request_info_t *req, return NXT_UNIT_OK; } - int -nxt_unit_response_send(nxt_unit_request_info_t *req) -{ +nxt_unit_response_send(nxt_unit_request_info_t *req) { int rc; - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2368,18 +2348,17 @@ nxt_unit_response_send(nxt_unit_request_info_t *req) nxt_unit_response_upgrade(req); } - nxt_unit_req_debug(req, "send: %"PRIu32" fields, %d bytes", - req->response->fields_count, - (int) (req->response_buf->free - - req->response_buf->start)); + nxt_unit_req_debug(req, "send: %" PRIu32 " fields, %d bytes", + req->response->fields_count, + (int) (req->response_buf->free - req->response_buf->start)); mmap_buf = nxt_container_of(req->response_buf, nxt_unit_mmap_buf_t, buf); rc = nxt_unit_mmap_buf_send(req, mmap_buf, 0); if (nxt_fast_path(rc == NXT_UNIT_OK)) { - req->response = NULL; + req->response = NULL; req->response_buf = NULL; - req_impl->state = NXT_UNIT_RS_RESPONSE_SENT; + req_impl->state = NXT_UNIT_RS_RESPONSE_SENT; nxt_unit_mmap_buf_free(mmap_buf); } @@ -2387,33 +2366,31 @@ nxt_unit_response_send(nxt_unit_request_info_t *req) return rc; } - int -nxt_unit_response_is_sent(nxt_unit_request_info_t *req) -{ - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_response_is_sent(nxt_unit_request_info_t *req) { + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); return req_impl->state >= NXT_UNIT_RS_RESPONSE_SENT; } - nxt_unit_buf_t * -nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, uint32_t size) -{ +nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, uint32_t size) { int rc; - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_request_info_impl_t *req_impl; if (nxt_slow_path(size > PORT_MMAP_DATA_SIZE)) { - nxt_unit_req_warn(req, "response_buf_alloc: " - "requested buffer (%"PRIu32") too big", size); + nxt_unit_req_warn(req, + "response_buf_alloc: " + "requested buffer (%" PRIu32 ") too big", + size); return NULL; } - nxt_unit_req_debug(req, "response_buf_alloc: %"PRIu32, size); + nxt_unit_req_debug(req, "response_buf_alloc: %" PRIu32, size); req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2428,9 +2405,8 @@ nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, uint32_t size) nxt_unit_mmap_buf_insert_tail(&req_impl->outgoing_buf, mmap_buf); - rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, - size, size, mmap_buf, - NULL); + rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, size, size, + mmap_buf, NULL); if (nxt_slow_path(rc != NXT_UNIT_OK)) { nxt_unit_mmap_buf_release(mmap_buf); @@ -2442,12 +2418,10 @@ nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, uint32_t size) return &mmap_buf->buf; } - static nxt_unit_mmap_buf_t * -nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx) -{ - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx) { + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -2471,16 +2445,14 @@ nxt_unit_mmap_buf_get(nxt_unit_ctx_t *ctx) mmap_buf->ctx_impl = ctx_impl; - mmap_buf->hdr = NULL; + mmap_buf->hdr = NULL; mmap_buf->free_ptr = NULL; return mmap_buf; } - static void -nxt_unit_mmap_buf_release(nxt_unit_mmap_buf_t *mmap_buf) -{ +nxt_unit_mmap_buf_release(nxt_unit_mmap_buf_t *mmap_buf) { nxt_unit_mmap_buf_unlink(mmap_buf); pthread_mutex_lock(&mmap_buf->ctx_impl->mutex); @@ -2490,19 +2462,15 @@ nxt_unit_mmap_buf_release(nxt_unit_mmap_buf_t *mmap_buf) pthread_mutex_unlock(&mmap_buf->ctx_impl->mutex); } - int -nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req) -{ +nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req) { return req->request->websocket_handshake; } - int -nxt_unit_response_upgrade(nxt_unit_request_info_t *req) -{ +nxt_unit_response_upgrade(nxt_unit_request_info_t *req) { int rc; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2538,44 +2506,38 @@ nxt_unit_response_upgrade(nxt_unit_request_info_t *req) return NXT_UNIT_OK; } - int -nxt_unit_response_is_websocket(nxt_unit_request_info_t *req) -{ - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_response_is_websocket(nxt_unit_request_info_t *req) { + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); return req_impl->websocket; } - nxt_unit_request_info_t * -nxt_unit_get_request_info_from_data(void *data) -{ - nxt_unit_request_info_impl_t *req_impl; +nxt_unit_get_request_info_from_data(void *data) { + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(data, nxt_unit_request_info_impl_t, extra_data); return &req_impl->req; } - int -nxt_unit_buf_send(nxt_unit_buf_t *buf) -{ +nxt_unit_buf_send(nxt_unit_buf_t *buf) { int rc; - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_request_info_t *req; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_request_info_t *req; + nxt_unit_request_info_impl_t *req_impl; mmap_buf = nxt_container_of(buf, nxt_unit_mmap_buf_t, buf); - req = mmap_buf->req; + req = mmap_buf->req; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); nxt_unit_req_debug(req, "buf_send: %d bytes", - (int) (buf->free - buf->start)); + (int) (buf->free - buf->start)); if (nxt_slow_path(req_impl->state < NXT_UNIT_RS_RESPONSE_INIT)) { nxt_unit_req_warn(req, "buf_send: response not initialized yet"); @@ -2601,13 +2563,11 @@ nxt_unit_buf_send(nxt_unit_buf_t *buf) return NXT_UNIT_OK; } - static void -nxt_unit_buf_send_done(nxt_unit_buf_t *buf) -{ +nxt_unit_buf_send_done(nxt_unit_buf_t *buf) { int rc; - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_request_info_t *req; + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_request_info_t *req; mmap_buf = nxt_container_of(buf, nxt_unit_mmap_buf_t, buf); @@ -2624,26 +2584,24 @@ nxt_unit_buf_send_done(nxt_unit_buf_t *buf) } } - static int nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, - nxt_unit_mmap_buf_t *mmap_buf, int last) -{ + nxt_unit_mmap_buf_t *mmap_buf, int last) { struct { - nxt_port_msg_t msg; - nxt_port_mmap_msg_t mmap_msg; + nxt_port_msg_t msg; + nxt_port_mmap_msg_t mmap_msg; } m; int rc; - u_char *last_used, *first_free; + u_char *last_used, *first_free; ssize_t res; nxt_chunk_id_t first_free_chunk; - nxt_unit_buf_t *buf; - nxt_unit_impl_t *lib; - nxt_port_mmap_header_t *hdr; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_buf_t *buf; + nxt_unit_impl_t *lib; + nxt_port_mmap_header_t *hdr; + nxt_unit_request_info_impl_t *req_impl; - lib = nxt_container_of(req->ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(req->ctx->unit, nxt_unit_impl_t, unit); req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); buf = &mmap_buf->buf; @@ -2651,42 +2609,40 @@ nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, m.mmap_msg.size = buf->free - buf->start; - m.msg.stream = req_impl->stream; - m.msg.pid = lib->pid; + m.msg.stream = req_impl->stream; + m.msg.pid = lib->pid; m.msg.reply_port = 0; - m.msg.type = _NXT_PORT_MSG_DATA; - m.msg.last = last != 0; - m.msg.mmap = hdr != NULL && m.mmap_msg.size > 0; - m.msg.nf = 0; - m.msg.mf = 0; + m.msg.type = _NXT_PORT_MSG_DATA; + m.msg.last = last != 0; + m.msg.mmap = hdr != NULL && m.mmap_msg.size > 0; + m.msg.nf = 0; + m.msg.mf = 0; rc = NXT_UNIT_ERROR; if (m.msg.mmap) { m.mmap_msg.mmap_id = hdr->id; - m.mmap_msg.chunk_id = nxt_port_mmap_chunk_id(hdr, - (u_char *) buf->start); + m.mmap_msg.chunk_id + = nxt_port_mmap_chunk_id(hdr, (u_char *) buf->start); - nxt_unit_debug(req->ctx, "#%"PRIu32": send mmap: (%d,%d,%d)", - req_impl->stream, - (int) m.mmap_msg.mmap_id, - (int) m.mmap_msg.chunk_id, - (int) m.mmap_msg.size); + nxt_unit_debug(req->ctx, "#%" PRIu32 ": send mmap: (%d,%d,%d)", + req_impl->stream, (int) m.mmap_msg.mmap_id, + (int) m.mmap_msg.chunk_id, (int) m.mmap_msg.size); res = nxt_unit_port_send(req->ctx, req->response_port, &m, sizeof(m), - NULL); + NULL); if (nxt_slow_path(res != sizeof(m))) { goto free_buf; } - last_used = (u_char *) buf->free - 1; + last_used = (u_char *) buf->free - 1; first_free_chunk = nxt_port_mmap_chunk_id(hdr, last_used) + 1; if (buf->end - buf->free >= PORT_MMAP_CHUNK_SIZE) { first_free = nxt_port_mmap_chunk_start(hdr, first_free_chunk); buf->start = (char *) first_free; - buf->free = buf->start; + buf->free = buf->start; if (buf->end < buf->start) { buf->end = buf->start; @@ -2694,39 +2650,37 @@ nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, } else { buf->start = NULL; - buf->free = NULL; - buf->end = NULL; + buf->free = NULL; + buf->end = NULL; mmap_buf->hdr = NULL; } nxt_atomic_fetch_add(&lib->outgoing.allocated_chunks, - (int) m.mmap_msg.chunk_id - (int) first_free_chunk); + (int) m.mmap_msg.chunk_id - (int) first_free_chunk); nxt_unit_debug(req->ctx, "allocated_chunks %d", - (int) lib->outgoing.allocated_chunks); + (int) lib->outgoing.allocated_chunks); } else { - if (nxt_slow_path(mmap_buf->plain_ptr == NULL - || mmap_buf->plain_ptr > buf->start - sizeof(m.msg))) - { + if (nxt_slow_path( + mmap_buf->plain_ptr == NULL + || mmap_buf->plain_ptr > buf->start - sizeof(m.msg))) { nxt_unit_alert(req->ctx, - "#%"PRIu32": failed to send plain memory buffer" - ": no space reserved for message header", - req_impl->stream); + "#%" PRIu32 ": failed to send plain memory buffer" + ": no space reserved for message header", + req_impl->stream); goto free_buf; } memcpy(buf->start - sizeof(m.msg), &m.msg, sizeof(m.msg)); - nxt_unit_debug(req->ctx, "#%"PRIu32": send plain: %d", - req_impl->stream, - (int) (sizeof(m.msg) + m.mmap_msg.size)); + nxt_unit_debug(req->ctx, "#%" PRIu32 ": send plain: %d", + req_impl->stream, (int) (sizeof(m.msg) + m.mmap_msg.size)); res = nxt_unit_port_send(req->ctx, req->response_port, - buf->start - sizeof(m.msg), - m.mmap_msg.size + sizeof(m.msg), NULL); + buf->start - sizeof(m.msg), m.mmap_msg.size + sizeof(m.msg), NULL); if (nxt_slow_path(res != (ssize_t) (m.mmap_msg.size + sizeof(m.msg)))) { goto free_buf; @@ -2742,30 +2696,23 @@ nxt_unit_mmap_buf_send(nxt_unit_request_info_t *req, return rc; } - void -nxt_unit_buf_free(nxt_unit_buf_t *buf) -{ +nxt_unit_buf_free(nxt_unit_buf_t *buf) { nxt_unit_mmap_buf_free(nxt_container_of(buf, nxt_unit_mmap_buf_t, buf)); } - static void -nxt_unit_mmap_buf_free(nxt_unit_mmap_buf_t *mmap_buf) -{ +nxt_unit_mmap_buf_free(nxt_unit_mmap_buf_t *mmap_buf) { nxt_unit_free_outgoing_buf(mmap_buf); nxt_unit_mmap_buf_release(mmap_buf); } - static void -nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf) -{ +nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf) { if (mmap_buf->hdr != NULL) { - nxt_unit_mmap_release(&mmap_buf->ctx_impl->ctx, - mmap_buf->hdr, mmap_buf->buf.start, - mmap_buf->buf.end - mmap_buf->buf.start); + nxt_unit_mmap_release(&mmap_buf->ctx_impl->ctx, mmap_buf->hdr, + mmap_buf->buf.start, mmap_buf->buf.end - mmap_buf->buf.start); mmap_buf->hdr = NULL; @@ -2779,12 +2726,10 @@ nxt_unit_free_outgoing_buf(nxt_unit_mmap_buf_t *mmap_buf) } } - static nxt_unit_read_buf_t * -nxt_unit_read_buf_get(nxt_unit_ctx_t *ctx) -{ - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_read_buf_t *rbuf; +nxt_unit_read_buf_get(nxt_unit_ctx_t *ctx) { + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_read_buf_t *rbuf; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -2799,12 +2744,10 @@ nxt_unit_read_buf_get(nxt_unit_ctx_t *ctx) return rbuf; } - static nxt_unit_read_buf_t * -nxt_unit_read_buf_get_impl(nxt_unit_ctx_impl_t *ctx_impl) -{ - nxt_queue_link_t *link; - nxt_unit_read_buf_t *rbuf; +nxt_unit_read_buf_get_impl(nxt_unit_ctx_impl_t *ctx_impl) { + nxt_queue_link_t *link; + nxt_unit_read_buf_t *rbuf; if (!nxt_queue_is_empty(&ctx_impl->free_rbuf)) { link = nxt_queue_first(&ctx_impl->free_rbuf); @@ -2824,12 +2767,9 @@ nxt_unit_read_buf_get_impl(nxt_unit_ctx_impl_t *ctx_impl) return rbuf; } - static void -nxt_unit_read_buf_release(nxt_unit_ctx_t *ctx, - nxt_unit_read_buf_t *rbuf) -{ - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_read_buf_release(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) { + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -2840,11 +2780,9 @@ nxt_unit_read_buf_release(nxt_unit_ctx_t *ctx, pthread_mutex_unlock(&ctx_impl->mutex); } - nxt_unit_buf_t * -nxt_unit_buf_next(nxt_unit_buf_t *buf) -{ - nxt_unit_mmap_buf_t *mmap_buf; +nxt_unit_buf_next(nxt_unit_buf_t *buf) { + nxt_unit_mmap_buf_t *mmap_buf; mmap_buf = nxt_container_of(buf, nxt_unit_mmap_buf_t, buf); @@ -2855,43 +2793,35 @@ nxt_unit_buf_next(nxt_unit_buf_t *buf) return &mmap_buf->next->buf; } - uint32_t -nxt_unit_buf_max(void) -{ +nxt_unit_buf_max(void) { return PORT_MMAP_DATA_SIZE; } - uint32_t -nxt_unit_buf_min(void) -{ +nxt_unit_buf_min(void) { return PORT_MMAP_CHUNK_SIZE; } - int nxt_unit_response_write(nxt_unit_request_info_t *req, const void *start, - size_t size) -{ - ssize_t res; + size_t size) { + ssize_t res; res = nxt_unit_response_write_nb(req, start, size, size); return res < 0 ? -res : NXT_UNIT_OK; } - ssize_t nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, - size_t size, size_t min_size) -{ + size_t size, size_t min_size) { int rc; ssize_t sent; uint32_t part_size, min_part_size, buf_size; - const char *part_start; + const char *part_start; nxt_unit_mmap_buf_t mmap_buf; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_request_info_impl_t *req_impl; char local_buf[NXT_UNIT_LOCAL_BUF_SIZE]; nxt_unit_req_debug(req, "write: %d", (int) size); @@ -2899,7 +2829,7 @@ nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); part_start = start; - sent = 0; + sent = 0; if (nxt_slow_path(req_impl->state < NXT_UNIT_RS_RESPONSE_INIT)) { nxt_unit_req_alert(req, "write: response not initialized yet"); @@ -2922,20 +2852,20 @@ nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, return -rc; } - size -= part_size; + size -= part_size; part_start += part_size; - sent += part_size; + sent += part_size; min_size -= nxt_min(min_size, part_size); } while (size > 0) { - part_size = nxt_min(size, PORT_MMAP_DATA_SIZE); + part_size = nxt_min(size, PORT_MMAP_DATA_SIZE); min_part_size = nxt_min(min_size, part_size); min_part_size = nxt_min(min_part_size, PORT_MMAP_CHUNK_SIZE); rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, part_size, - min_part_size, &mmap_buf, local_buf); + min_part_size, &mmap_buf, local_buf); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return -rc; } @@ -2946,17 +2876,17 @@ nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, } part_size = nxt_min(buf_size, part_size); - mmap_buf.buf.free = nxt_cpymem(mmap_buf.buf.free, - part_start, part_size); + mmap_buf.buf.free + = nxt_cpymem(mmap_buf.buf.free, part_start, part_size); rc = nxt_unit_mmap_buf_send(req, &mmap_buf, 0); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return -rc; } - size -= part_size; + size -= part_size; part_start += part_size; - sent += part_size; + sent += part_size; min_size -= nxt_min(min_size, part_size); } @@ -2964,17 +2894,15 @@ nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, return sent; } - int nxt_unit_response_write_cb(nxt_unit_request_info_t *req, - nxt_unit_read_info_t *read_info) -{ + nxt_unit_read_info_t *read_info) { int rc; ssize_t n; uint32_t buf_size; - nxt_unit_buf_t *buf; + nxt_unit_buf_t *buf; nxt_unit_mmap_buf_t mmap_buf; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_request_info_impl_t *req_impl; char local_buf[NXT_UNIT_LOCAL_BUF_SIZE]; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -2987,7 +2915,6 @@ nxt_unit_response_write_cb(nxt_unit_request_info_t *req, /* Check if response is not send yet. */ if (nxt_slow_path(req->response_buf != NULL)) { - /* Enable content in headers buf. */ rc = nxt_unit_response_add_content(req, "", 0); if (nxt_slow_path(rc != NXT_UNIT_OK)) { @@ -3007,7 +2934,7 @@ nxt_unit_response_write_cb(nxt_unit_request_info_t *req, } /* Manually increase sizes. */ - buf->free += n; + buf->free += n; req->response->piggyback_content_length += n; if (read_info->eof) { @@ -3028,14 +2955,13 @@ nxt_unit_response_write_cb(nxt_unit_request_info_t *req, } while (!read_info->eof) { - nxt_unit_req_debug(req, "write_cb, alloc %"PRIu32"", - read_info->buf_size); + nxt_unit_req_debug(req, "write_cb, alloc %" PRIu32 "", + read_info->buf_size); buf_size = nxt_min(read_info->buf_size, PORT_MMAP_DATA_SIZE); - rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, - buf_size, buf_size, - &mmap_buf, local_buf); + rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, buf_size, + buf_size, &mmap_buf, local_buf); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } @@ -3066,20 +2992,18 @@ nxt_unit_response_write_cb(nxt_unit_request_info_t *req, return NXT_UNIT_OK; } - ssize_t -nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, size_t size) -{ - ssize_t buf_res, res; +nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, size_t size) { + ssize_t buf_res, res; - buf_res = nxt_unit_buf_read(&req->content_buf, &req->content_length, - dst, size); + buf_res + = nxt_unit_buf_read(&req->content_buf, &req->content_length, dst, size); if (buf_res < (ssize_t) size && req->content_fd != -1) { res = read(req->content_fd, dst, size); if (nxt_slow_path(res < 0)) { nxt_unit_req_alert(req, "failed to read content: %s (%d)", - strerror(errno), errno); + strerror(errno), errno); return res; } @@ -3101,14 +3025,12 @@ nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, size_t size) return buf_res + res; } - ssize_t -nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size) -{ - char *p; +nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size) { + char *p; size_t l_size, b_size; - nxt_unit_buf_t *b; - nxt_unit_mmap_buf_t *mmap_buf, *preread_buf; + nxt_unit_buf_t *b; + nxt_unit_mmap_buf_t *mmap_buf, *preread_buf; if (req->content_length == 0) { return 0; @@ -3120,7 +3042,7 @@ nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size) while (b != NULL) { b_size = b->end - b->free; - p = memchr(b->free, '\n', b_size); + p = memchr(b->free, '\n', b_size); if (p != NULL) { p++; @@ -3135,10 +3057,8 @@ nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size) } mmap_buf = nxt_container_of(b, nxt_unit_mmap_buf_t, buf); - if (mmap_buf->next == NULL - && req->content_fd != -1 - && l_size < req->content_length) - { + if (mmap_buf->next == NULL && req->content_fd != -1 + && l_size < req->content_length) { preread_buf = nxt_unit_request_preread(req, 16384); if (nxt_slow_path(preread_buf == NULL)) { return -1; @@ -3153,12 +3073,10 @@ nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size) return nxt_min(max_size, l_size); } - static nxt_unit_mmap_buf_t * -nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size) -{ +nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size) { ssize_t res; - nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_mmap_buf_t *mmap_buf; if (req->content_fd == -1) { nxt_unit_req_alert(req, "preread: content_fd == -1"); @@ -3180,15 +3098,15 @@ nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size) mmap_buf->plain_ptr = mmap_buf->free_ptr; - mmap_buf->hdr = NULL; + mmap_buf->hdr = NULL; mmap_buf->buf.start = mmap_buf->free_ptr; - mmap_buf->buf.free = mmap_buf->buf.start; - mmap_buf->buf.end = mmap_buf->buf.start + size; + mmap_buf->buf.free = mmap_buf->buf.start; + mmap_buf->buf.end = mmap_buf->buf.start + size; res = read(req->content_fd, mmap_buf->free_ptr, size); if (res < 0) { nxt_unit_req_alert(req, "failed to read content: %s (%d)", - strerror(errno), errno); + strerror(errno), errno); nxt_unit_mmap_buf_free(mmap_buf); @@ -3208,18 +3126,16 @@ nxt_unit_request_preread(nxt_unit_request_info_t *req, size_t size) return mmap_buf; } - static ssize_t -nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, size_t size) -{ - u_char *p; +nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, size_t size) { + u_char *p; size_t rest, copy, read; - nxt_unit_buf_t *buf, *last_buf; + nxt_unit_buf_t *buf, *last_buf; - p = dst; + p = dst; rest = size; - buf = *b; + buf = *b; last_buf = buf; while (buf != NULL) { @@ -3231,7 +3147,7 @@ nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, size_t size) p = nxt_cpymem(p, buf->free, copy); buf->free += copy; - rest -= copy; + rest -= copy; if (rest == 0) { if (buf->end == buf->free) { @@ -3253,14 +3169,12 @@ nxt_unit_buf_read(nxt_unit_buf_t **b, uint64_t *len, void *dst, size_t size) return read; } - void -nxt_unit_request_done(nxt_unit_request_info_t *req, int rc) -{ +nxt_unit_request_done(nxt_unit_request_info_t *req, int rc) { uint32_t size; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_impl_t *lib; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); @@ -3271,7 +3185,6 @@ nxt_unit_request_done(nxt_unit_request_info_t *req, int rc) } if (nxt_slow_path(req_impl->state < NXT_UNIT_RS_RESPONSE_INIT)) { - size = nxt_length("Content-Type") + nxt_length("text/plain"); rc = nxt_unit_response_init(req, 200, 1, size); @@ -3280,15 +3193,13 @@ nxt_unit_request_done(nxt_unit_request_info_t *req, int rc) } rc = nxt_unit_response_add_field(req, "Content-Type", - nxt_length("Content-Type"), - "text/plain", nxt_length("text/plain")); + nxt_length("Content-Type"), "text/plain", nxt_length("text/plain")); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto skip_response_send; } } if (nxt_slow_path(req_impl->state < NXT_UNIT_RS_RESPONSE_SENT)) { - req_impl->state = NXT_UNIT_RS_RESPONSE_SENT; nxt_unit_buf_send_done(req->response_buf); @@ -3300,44 +3211,40 @@ nxt_unit_request_done(nxt_unit_request_info_t *req, int rc) lib = nxt_container_of(req->unit, nxt_unit_impl_t, unit); - msg.stream = req_impl->stream; - msg.pid = lib->pid; + msg.stream = req_impl->stream; + msg.pid = lib->pid; msg.reply_port = 0; - msg.type = (rc == NXT_UNIT_OK) ? _NXT_PORT_MSG_DATA - : _NXT_PORT_MSG_RPC_ERROR; + msg.type + = (rc == NXT_UNIT_OK) ? _NXT_PORT_MSG_DATA : _NXT_PORT_MSG_RPC_ERROR; msg.last = 1; msg.mmap = 0; - msg.nf = 0; - msg.mf = 0; + msg.nf = 0; + msg.mf = 0; - (void) nxt_unit_port_send(req->ctx, req->response_port, - &msg, sizeof(msg), NULL); + (void) nxt_unit_port_send(req->ctx, req->response_port, &msg, sizeof(msg), + NULL); nxt_unit_request_info_release(req); } - int nxt_unit_websocket_send(nxt_unit_request_info_t *req, uint8_t opcode, - uint8_t last, const void *start, size_t size) -{ - const struct iovec iov = { (void *) start, size }; + uint8_t last, const void *start, size_t size) { + const struct iovec iov = {(void *) start, size}; return nxt_unit_websocket_sendv(req, opcode, last, &iov, 1); } - int nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, - uint8_t last, const struct iovec *iov, int iovcnt) -{ + uint8_t last, const struct iovec *iov, int iovcnt) { int i, rc; size_t l, copy; uint32_t payload_len, buf_size, alloc_size; - const uint8_t *b; - nxt_unit_buf_t *buf; + const uint8_t *b; + nxt_unit_buf_t *buf; nxt_unit_mmap_buf_t mmap_buf; - nxt_websocket_header_t *wh; + nxt_websocket_header_t *wh; char local_buf[NXT_UNIT_LOCAL_BUF_SIZE]; payload_len = 0; @@ -3346,12 +3253,11 @@ nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, payload_len += iov[i].iov_len; } - buf_size = 10 + payload_len; + buf_size = 10 + payload_len; alloc_size = nxt_min(buf_size, PORT_MMAP_DATA_SIZE); - rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, - alloc_size, alloc_size, - &mmap_buf, local_buf); + rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, alloc_size, + alloc_size, &mmap_buf, local_buf); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } @@ -3365,8 +3271,8 @@ nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, wh = (void *) buf->free; - buf->free = nxt_websocket_frame_init(wh, payload_len); - wh->fin = last; + buf->free = nxt_websocket_frame_init(wh, payload_len); + wh->fin = last; wh->opcode = opcode; for (i = 0; i < iovcnt; i++) { @@ -3377,9 +3283,9 @@ nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, copy = buf->end - buf->free; copy = nxt_min(l, copy); - buf->free = nxt_cpymem(buf->free, b, copy); - b += copy; - l -= copy; + buf->free = nxt_cpymem(buf->free, b, copy); + b += copy; + l -= copy; if (l > 0) { if (nxt_fast_path(buf->free > buf->start)) { @@ -3393,8 +3299,7 @@ nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, alloc_size = nxt_min(buf_size, PORT_MMAP_DATA_SIZE); rc = nxt_unit_get_outgoing_buf(req->ctx, req->response_port, - alloc_size, alloc_size, - &mmap_buf, local_buf); + alloc_size, alloc_size, &mmap_buf, local_buf); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } @@ -3411,17 +3316,14 @@ nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, return rc; } - ssize_t nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst, - size_t size) -{ - ssize_t res; - uint8_t *b; - uint64_t i, d; + size_t size) { + ssize_t res; + uint8_t *b; + uint64_t i, d; - res = nxt_unit_buf_read(&ws->content_buf, &ws->content_length, - dst, size); + res = nxt_unit_buf_read(&ws->content_buf, &ws->content_length, dst, size); if (ws->mask == NULL) { return res; @@ -3431,19 +3333,17 @@ nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst, d = (ws->payload_len - ws->content_length - res) % 4; for (i = 0; i < (uint64_t) res; i++) { - b[i] ^= ws->mask[ (i + d) % 4 ]; + b[i] ^= ws->mask[(i + d) % 4]; } return res; } - int -nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws) -{ - char *b; +nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws) { + char *b; size_t size, hsize; - nxt_unit_websocket_frame_impl_t *ws_impl; + nxt_unit_websocket_frame_impl_t *ws_impl; ws_impl = nxt_container_of(ws, nxt_unit_websocket_frame_impl_t, ws); @@ -3463,8 +3363,8 @@ nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws) hsize = nxt_websocket_frame_header_size(b); ws_impl->buf->buf.start = b; - ws_impl->buf->buf.free = b + hsize; - ws_impl->buf->buf.end = b + size; + ws_impl->buf->buf.free = b + hsize; + ws_impl->buf->buf.end = b + size; ws_impl->buf->free_ptr = b; @@ -3480,23 +3380,19 @@ nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws) return NXT_UNIT_OK; } - void -nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws) -{ +nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws) { nxt_unit_websocket_frame_release(ws); } - static nxt_port_mmap_header_t * -nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_chunk_id_t *c, int *n, int min_n) -{ +nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_chunk_id_t *c, + int *n, int min_n) { int res, nchunks, i; uint32_t outgoing_size; - nxt_unit_mmap_t *mm, *mm_end; - nxt_unit_impl_t *lib; - nxt_port_mmap_header_t *hdr; + nxt_unit_mmap_t *mm, *mm_end; + nxt_unit_impl_t *lib; + nxt_port_mmap_header_t *hdr; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -3517,8 +3413,7 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, if (hdr->sent_over != 0xFFFFu && (hdr->sent_over != port->id.id - || mm->src_thread != pthread_self())) - { + || mm->src_thread != pthread_self())) { continue; } @@ -3529,7 +3424,7 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, while (nchunks < *n) { res = nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, - *c + nchunks); + *c + nchunks); if (res == 0) { if (nchunks >= min_n) { @@ -3542,8 +3437,8 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_port_mmap_set_chunk_free(hdr->free_map, *c + i); } - *c += nchunks + 1; - nchunks = 0; + *c += nchunks + 1; + nchunks = 0; break; } @@ -3569,8 +3464,7 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, } if (nxt_slow_path(lib->outgoing.allocated_chunks + min_n - >= lib->shm_mmap_limit * PORT_MMAP_CHUNK_COUNT)) - { + >= lib->shm_mmap_limit * PORT_MMAP_CHUNK_COUNT)) { /* Memory allocated by application, but not send to router. */ return NULL; } @@ -3604,7 +3498,7 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, skip: - *c = 0; + *c = 0; hdr = nxt_unit_new_mmap(ctx, port, *n); unlock: @@ -3612,31 +3506,29 @@ nxt_unit_mmap_get(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_atomic_fetch_add(&lib->outgoing.allocated_chunks, *n); nxt_unit_debug(ctx, "allocated_chunks %d", - (int) lib->outgoing.allocated_chunks); + (int) lib->outgoing.allocated_chunks); pthread_mutex_unlock(&lib->outgoing.mutex); return hdr; } - static int -nxt_unit_send_oosm(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ +nxt_unit_send_oosm(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { ssize_t res; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - msg.stream = 0; - msg.pid = lib->pid; + msg.stream = 0; + msg.pid = lib->pid; msg.reply_port = 0; - msg.type = _NXT_PORT_MSG_OOSM; - msg.last = 0; - msg.mmap = 0; - msg.nf = 0; - msg.mf = 0; + msg.type = _NXT_PORT_MSG_OOSM; + msg.last = 0; + msg.mmap = 0; + msg.nf = 0; + msg.mf = 0; res = nxt_unit_port_send(ctx, lib->router_port, &msg, sizeof(msg), NULL); if (nxt_slow_path(res != sizeof(msg))) { @@ -3646,13 +3538,11 @@ nxt_unit_send_oosm(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return NXT_UNIT_OK; } - static int -nxt_unit_wait_shm_ack(nxt_unit_ctx_t *ctx) -{ +nxt_unit_wait_shm_ack(nxt_unit_ctx_t *ctx) { int res; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_read_buf_t *rbuf; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_read_buf_t *rbuf; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -3693,12 +3583,10 @@ nxt_unit_wait_shm_ack(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static nxt_unit_mmap_t * -nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i) -{ +nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i) { uint32_t cap, n; - nxt_unit_mmap_t *e; + nxt_unit_mmap_t *e; if (nxt_fast_path(mmaps->size > i)) { return mmaps->elts + i; @@ -3711,7 +3599,6 @@ nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i) } while (i + 1 > cap) { - if (cap < 16) { cap = cap * 2; @@ -3721,7 +3608,6 @@ nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i) } if (cap != mmaps->cap) { - e = realloc(mmaps->elts, cap * sizeof(nxt_unit_mmap_t)); if (nxt_slow_path(e == NULL)) { return NULL; @@ -3746,15 +3632,13 @@ nxt_unit_mmap_at(nxt_unit_mmaps_t *mmaps, uint32_t i) return mmaps->elts + i; } - static nxt_port_mmap_header_t * -nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) -{ +nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) { int i, fd, rc; - void *mem; - nxt_unit_mmap_t *mm; - nxt_unit_impl_t *lib; - nxt_port_mmap_header_t *hdr; + void *mem; + nxt_unit_mmap_t *mm; + nxt_unit_impl_t *lib; + nxt_port_mmap_header_t *hdr; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -3772,8 +3656,8 @@ nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) mem = mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { - nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", fd, - strerror(errno), errno); + nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", fd, strerror(errno), + errno); nxt_unit_close(fd); @@ -3781,14 +3665,14 @@ nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) } mm->hdr = mem; - hdr = mem; + hdr = mem; memset(hdr->free_map, 0xFFU, sizeof(hdr->free_map)); memset(hdr->free_tracking_map, 0xFFU, sizeof(hdr->free_tracking_map)); - hdr->id = lib->outgoing.size - 1; - hdr->src_pid = lib->pid; - hdr->dst_pid = port->id.pid; + hdr->id = lib->outgoing.size - 1; + hdr->src_pid = lib->pid; + hdr->dst_pid = port->id.pid; hdr->sent_over = port->id.id; mm->src_thread = pthread_self(); @@ -3809,8 +3693,8 @@ nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) hdr = NULL; } else { - nxt_unit_debug(ctx, "new mmap #%"PRIu32" created for %d -> %d", - hdr->id, (int) lib->pid, (int) port->id.pid); + nxt_unit_debug(ctx, "new mmap #%" PRIu32 " created for %d -> %d", + hdr->id, (int) lib->pid, (int) port->id.pid); } nxt_unit_close(fd); @@ -3828,19 +3712,17 @@ nxt_unit_new_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int n) return NULL; } - static int -nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) -{ - int fd; +nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) { + int fd; #if (NXT_HAVE_MEMFD_CREATE || NXT_HAVE_SHM_OPEN) char name[64]; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - snprintf(name, sizeof(name), NXT_SHM_PREFIX "unit.%d.%p", - lib->pid, (void *) (uintptr_t) pthread_self()); + snprintf(name, sizeof(name), NXT_SHM_PREFIX "unit.%d.%p", lib->pid, + (void *) (uintptr_t) pthread_self()); #endif #if (NXT_HAVE_MEMFD_CREATE) @@ -3848,7 +3730,7 @@ nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC); if (nxt_slow_path(fd == -1)) { nxt_unit_alert(ctx, "memfd_create(%s) failed: %s (%d)", name, - strerror(errno), errno); + strerror(errno), errno); return -1; } @@ -3860,7 +3742,7 @@ nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) fd = shm_open(SHM_ANON, O_RDWR, 0600); if (nxt_slow_path(fd == -1)) { nxt_unit_alert(ctx, "shm_open(SHM_ANON) failed: %s (%d)", - strerror(errno), errno); + strerror(errno), errno); return -1; } @@ -3873,14 +3755,14 @@ nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) fd = shm_open(name, O_CREAT | O_EXCL | O_RDWR, 0600); if (nxt_slow_path(fd == -1)) { nxt_unit_alert(ctx, "shm_open(%s) failed: %s (%d)", name, - strerror(errno), errno); + strerror(errno), errno); return -1; } if (nxt_slow_path(shm_unlink(name) == -1)) { nxt_unit_alert(ctx, "shm_unlink(%s) failed: %s (%d)", name, - strerror(errno), errno); + strerror(errno), errno); } #else @@ -3891,7 +3773,7 @@ nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) if (nxt_slow_path(ftruncate(fd, size) == -1)) { nxt_unit_alert(ctx, "ftruncate(%d) failed: %s (%d)", fd, - strerror(errno), errno); + strerror(errno), errno); nxt_unit_close(fd); @@ -3901,26 +3783,24 @@ nxt_unit_shm_open(nxt_unit_ctx_t *ctx, size_t size) return fd; } - static int -nxt_unit_send_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int fd) -{ +nxt_unit_send_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int fd) { ssize_t res; nxt_send_oob_t oob; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; int fds[2] = {fd, -1}; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - msg.stream = 0; - msg.pid = lib->pid; + msg.stream = 0; + msg.pid = lib->pid; msg.reply_port = 0; - msg.type = _NXT_PORT_MSG_MMAP; - msg.last = 0; - msg.mmap = 0; - msg.nf = 0; - msg.mf = 0; + msg.type = _NXT_PORT_MSG_MMAP; + msg.last = 0; + msg.mmap = 0; + msg.nf = 0; + msg.mf = 0; nxt_socket_msg_oob_init(&oob, fds); @@ -3932,24 +3812,22 @@ nxt_unit_send_mmap(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, int fd) return NXT_UNIT_OK; } - static int nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - uint32_t size, uint32_t min_size, - nxt_unit_mmap_buf_t *mmap_buf, char *local_buf) -{ + uint32_t size, uint32_t min_size, nxt_unit_mmap_buf_t *mmap_buf, + char *local_buf) { int nchunks, min_nchunks; nxt_chunk_id_t c; - nxt_port_mmap_header_t *hdr; + nxt_port_mmap_header_t *hdr; if (size <= NXT_UNIT_MAX_PLAIN_SIZE) { if (local_buf != NULL) { - mmap_buf->free_ptr = NULL; + mmap_buf->free_ptr = NULL; mmap_buf->plain_ptr = local_buf; } else { - mmap_buf->free_ptr = nxt_unit_malloc(ctx, - size + sizeof(nxt_port_msg_t)); + mmap_buf->free_ptr + = nxt_unit_malloc(ctx, size + sizeof(nxt_port_msg_t)); if (nxt_slow_path(mmap_buf->free_ptr == NULL)) { return NXT_UNIT_ERROR; } @@ -3957,28 +3835,28 @@ nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, mmap_buf->plain_ptr = mmap_buf->free_ptr; } - mmap_buf->hdr = NULL; + mmap_buf->hdr = NULL; mmap_buf->buf.start = mmap_buf->plain_ptr + sizeof(nxt_port_msg_t); - mmap_buf->buf.free = mmap_buf->buf.start; - mmap_buf->buf.end = mmap_buf->buf.start + size; + mmap_buf->buf.free = mmap_buf->buf.start; + mmap_buf->buf.end = mmap_buf->buf.start + size; nxt_unit_debug(ctx, "outgoing plain buffer allocation: (%p, %d)", - mmap_buf->buf.start, (int) size); + mmap_buf->buf.start, (int) size); return NXT_UNIT_OK; } - nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE; + nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE; min_nchunks = (min_size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE; hdr = nxt_unit_mmap_get(ctx, port, &c, &nchunks, min_nchunks); if (nxt_slow_path(hdr == NULL)) { if (nxt_fast_path(min_nchunks == 0 && nchunks == 0)) { - mmap_buf->hdr = NULL; + mmap_buf->hdr = NULL; mmap_buf->buf.start = NULL; - mmap_buf->buf.free = NULL; - mmap_buf->buf.end = NULL; - mmap_buf->free_ptr = NULL; + mmap_buf->buf.free = NULL; + mmap_buf->buf.end = NULL; + mmap_buf->free_ptr = NULL; return NXT_UNIT_OK; } @@ -3986,33 +3864,30 @@ nxt_unit_get_outgoing_buf(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, return NXT_UNIT_ERROR; } - mmap_buf->hdr = hdr; + mmap_buf->hdr = hdr; mmap_buf->buf.start = (char *) nxt_port_mmap_chunk_start(hdr, c); - mmap_buf->buf.free = mmap_buf->buf.start; - mmap_buf->buf.end = mmap_buf->buf.start + nchunks * PORT_MMAP_CHUNK_SIZE; - mmap_buf->free_ptr = NULL; - mmap_buf->ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); + mmap_buf->buf.free = mmap_buf->buf.start; + mmap_buf->buf.end = mmap_buf->buf.start + nchunks * PORT_MMAP_CHUNK_SIZE; + mmap_buf->free_ptr = NULL; + mmap_buf->ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); - nxt_unit_debug(ctx, "outgoing mmap allocation: (%d,%d,%d)", - (int) hdr->id, (int) c, - (int) (nchunks * PORT_MMAP_CHUNK_SIZE)); + nxt_unit_debug(ctx, "outgoing mmap allocation: (%d,%d,%d)", (int) hdr->id, + (int) c, (int) (nchunks * PORT_MMAP_CHUNK_SIZE)); return NXT_UNIT_OK; } - static int -nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) -{ +nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) { int rc; - void *mem; + void *mem; nxt_queue_t awaiting_rbuf; struct stat mmap_stat; - nxt_unit_mmap_t *mm; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_read_buf_t *rbuf; - nxt_port_mmap_header_t *hdr; + nxt_unit_mmap_t *mm; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_read_buf_t *rbuf; + nxt_port_mmap_header_t *hdr; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -4020,16 +3895,16 @@ nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) if (fstat(fd, &mmap_stat) == -1) { nxt_unit_alert(ctx, "incoming_mmap: fstat(%d) failed: %s (%d)", fd, - strerror(errno), errno); + strerror(errno), errno); return NXT_UNIT_ERROR; } - mem = mmap(NULL, mmap_stat.st_size, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); + mem = mmap(NULL, mmap_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, + 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_unit_alert(ctx, "incoming_mmap: mmap() failed: %s (%d)", - strerror(errno), errno); + strerror(errno), errno); return NXT_UNIT_ERROR; } @@ -4037,10 +3912,10 @@ nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) hdr = mem; if (nxt_slow_path(hdr->src_pid != pid)) { - - nxt_unit_alert(ctx, "incoming_mmap: unexpected pid in mmap header " - "detected: %d != %d or %d != %d", (int) hdr->src_pid, - (int) pid, (int) hdr->dst_pid, (int) lib->pid); + nxt_unit_alert(ctx, + "incoming_mmap: unexpected pid in mmap header " + "detected: %d != %d or %d != %d", + (int) hdr->src_pid, (int) pid, (int) hdr->dst_pid, (int) lib->pid); munmap(mem, PORT_MMAP_SIZE); @@ -4073,7 +3948,6 @@ nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) pthread_mutex_unlock(&lib->incoming.mutex); nxt_queue_each(rbuf, &awaiting_rbuf, nxt_unit_read_buf_t, link) { - ctx_impl = rbuf->ctx_impl; pthread_mutex_lock(&ctx_impl->mutex); @@ -4085,25 +3959,22 @@ nxt_unit_incoming_mmap(nxt_unit_ctx_t *ctx, pid_t pid, int fd) nxt_atomic_fetch_add(&ctx_impl->wait_items, -1); nxt_unit_awake_ctx(ctx, ctx_impl); - - } nxt_queue_loop; + } + nxt_queue_loop; return rc; } - static void -nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, nxt_unit_ctx_impl_t *ctx_impl) -{ - nxt_port_msg_t msg; +nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, nxt_unit_ctx_impl_t *ctx_impl) { + nxt_port_msg_t msg; if (nxt_fast_path(ctx == &ctx_impl->ctx)) { return; } - if (nxt_slow_path(ctx_impl->read_port == NULL - || ctx_impl->read_port->out_fd == -1)) - { + if (nxt_slow_path( + ctx_impl->read_port == NULL || ctx_impl->read_port->out_fd == -1)) { nxt_unit_alert(ctx, "target context read_port is NULL or not writable"); return; @@ -4113,33 +3984,27 @@ nxt_unit_awake_ctx(nxt_unit_ctx_t *ctx, nxt_unit_ctx_impl_t *ctx_impl) msg.type = _NXT_PORT_MSG_RPC_READY; - (void) nxt_unit_port_send(ctx, ctx_impl->read_port, - &msg, sizeof(msg), NULL); + (void) nxt_unit_port_send(ctx, ctx_impl->read_port, &msg, sizeof(msg), + NULL); } - static int -nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps) -{ - mmaps->size = 0; - mmaps->cap = 0; - mmaps->elts = NULL; +nxt_unit_mmaps_init(nxt_unit_mmaps_t *mmaps) { + mmaps->size = 0; + mmaps->cap = 0; + mmaps->elts = NULL; mmaps->allocated_chunks = 0; return pthread_mutex_init(&mmaps->mutex, NULL); } - nxt_inline void -nxt_unit_process_use(nxt_unit_process_t *process) -{ +nxt_unit_process_use(nxt_unit_process_t *process) { nxt_atomic_fetch_add(&process->use_count, 1); } - nxt_inline void -nxt_unit_process_release(nxt_unit_process_t *process) -{ +nxt_unit_process_release(nxt_unit_process_t *process) { long c; c = nxt_atomic_fetch_add(&process->use_count, -1); @@ -4151,11 +4016,9 @@ nxt_unit_process_release(nxt_unit_process_t *process) } } - static void -nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps) -{ - nxt_unit_mmap_t *mm, *end; +nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps) { + nxt_unit_mmap_t *mm, *end; if (mmaps->elts != NULL) { end = mmaps->elts + mmaps->size; @@ -4170,15 +4033,13 @@ nxt_unit_mmaps_destroy(nxt_unit_mmaps_t *mmaps) pthread_mutex_destroy(&mmaps->mutex); } - static int nxt_unit_check_rbuf_mmap(nxt_unit_ctx_t *ctx, nxt_unit_mmaps_t *mmaps, pid_t pid, uint32_t id, nxt_port_mmap_header_t **hdr, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { int res, need_rbuf; - nxt_unit_mmap_t *mm; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_mmap_t *mm; + nxt_unit_ctx_impl_t *ctx_impl; mm = nxt_unit_mmap_at(mmaps, id); if (nxt_slow_path(mm == NULL)) { @@ -4217,29 +4078,27 @@ nxt_unit_check_rbuf_mmap(nxt_unit_ctx_t *ctx, nxt_unit_mmaps_t *mmaps, return NXT_UNIT_AGAIN; } - static int nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { int res; - void *start; + void *start; uint32_t size; - nxt_unit_impl_t *lib; - nxt_unit_mmaps_t *mmaps; - nxt_unit_mmap_buf_t *b, **incoming_tail; - nxt_port_mmap_msg_t *mmap_msg, *end; - nxt_port_mmap_header_t *hdr; + nxt_unit_impl_t *lib; + nxt_unit_mmaps_t *mmaps; + nxt_unit_mmap_buf_t *b, **incoming_tail; + nxt_port_mmap_msg_t *mmap_msg, *end; + nxt_port_mmap_header_t *hdr; if (nxt_slow_path(recv_msg->size < sizeof(nxt_port_mmap_msg_t))) { - nxt_unit_warn(ctx, "#%"PRIu32": mmap_read: too small message (%d)", - recv_msg->stream, (int) recv_msg->size); + nxt_unit_warn(ctx, "#%" PRIu32 ": mmap_read: too small message (%d)", + recv_msg->stream, (int) recv_msg->size); return NXT_UNIT_ERROR; } mmap_msg = recv_msg->start; - end = nxt_pointer_to(recv_msg->start, recv_msg->size); + end = nxt_pointer_to(recv_msg->start, recv_msg->size); incoming_tail = &recv_msg->incoming_buf; @@ -4247,8 +4106,9 @@ nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, for (; mmap_msg < end; mmap_msg++) { b = nxt_unit_mmap_buf_get(ctx); if (nxt_slow_path(b == NULL)) { - nxt_unit_warn(ctx, "#%"PRIu32": mmap_read: failed to allocate buf", - recv_msg->stream); + nxt_unit_warn(ctx, + "#%" PRIu32 ": mmap_read: failed to allocate buf", + recv_msg->stream); while (recv_msg->incoming_buf != NULL) { nxt_unit_mmap_buf_release(recv_msg->incoming_buf); @@ -4261,7 +4121,7 @@ nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, incoming_tail = &b->next; } - b = recv_msg->incoming_buf; + b = recv_msg->incoming_buf; mmap_msg = recv_msg->start; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -4271,9 +4131,8 @@ nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, pthread_mutex_lock(&mmaps->mutex); for (; mmap_msg < end; mmap_msg++) { - res = nxt_unit_check_rbuf_mmap(ctx, mmaps, - recv_msg->pid, mmap_msg->mmap_id, - &hdr, rbuf); + res = nxt_unit_check_rbuf_mmap(ctx, mmaps, recv_msg->pid, + mmap_msg->mmap_id, &hdr, rbuf); if (nxt_slow_path(res != NXT_UNIT_OK)) { while (recv_msg->incoming_buf != NULL) { @@ -4284,26 +4143,25 @@ nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, } start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id); - size = mmap_msg->size; + size = mmap_msg->size; if (recv_msg->start == mmap_msg) { recv_msg->start = start; - recv_msg->size = size; + recv_msg->size = size; } b->buf.start = start; - b->buf.free = start; - b->buf.end = b->buf.start + size; - b->hdr = hdr; + b->buf.free = start; + b->buf.end = b->buf.start + size; + b->hdr = hdr; b = b->next; - nxt_unit_debug(ctx, "#%"PRIu32": mmap_read: [%p,%d] %d->%d,(%d,%d,%d)", - recv_msg->stream, - start, (int) size, - (int) hdr->src_pid, (int) hdr->dst_pid, - (int) hdr->id, (int) mmap_msg->chunk_id, - (int) mmap_msg->size); + nxt_unit_debug(ctx, + "#%" PRIu32 ": mmap_read: [%p,%d] %d->%d,(%d,%d,%d)", + recv_msg->stream, start, (int) size, (int) hdr->src_pid, + (int) hdr->dst_pid, (int) hdr->id, (int) mmap_msg->chunk_id, + (int) mmap_msg->size); } pthread_mutex_unlock(&mmaps->mutex); @@ -4311,27 +4169,25 @@ nxt_unit_mmap_read(nxt_unit_ctx_t *ctx, nxt_unit_recv_msg_t *recv_msg, return NXT_UNIT_OK; } - static int -nxt_unit_get_mmap(nxt_unit_ctx_t *ctx, pid_t pid, uint32_t id) -{ +nxt_unit_get_mmap(nxt_unit_ctx_t *ctx, pid_t pid, uint32_t id) { ssize_t res; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; struct { - nxt_port_msg_t msg; - nxt_port_msg_get_mmap_t get_mmap; + nxt_port_msg_t msg; + nxt_port_msg_get_mmap_t get_mmap; } m; - lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); memset(&m.msg, 0, sizeof(nxt_port_msg_t)); - m.msg.pid = lib->pid; + m.msg.pid = lib->pid; m.msg.reply_port = ctx_impl->read_port->id.id; - m.msg.type = _NXT_PORT_MSG_GET_MMAP; + m.msg.type = _NXT_PORT_MSG_GET_MMAP; m.get_mmap.id = id; @@ -4345,21 +4201,19 @@ nxt_unit_get_mmap(nxt_unit_ctx_t *ctx, pid_t pid, uint32_t id) return NXT_UNIT_OK; } - static void nxt_unit_mmap_release(nxt_unit_ctx_t *ctx, nxt_port_mmap_header_t *hdr, - void *start, uint32_t size) -{ + void *start, uint32_t size) { int freed_chunks; - u_char *p, *end; + u_char *p, *end; nxt_chunk_id_t c; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; memset(start, 0xA5, size); - p = start; - end = p + size; - c = nxt_port_mmap_chunk_id(hdr, p); + p = start; + end = p + size; + c = nxt_port_mmap_chunk_id(hdr, p); freed_chunks = 0; while (p < end) { @@ -4376,35 +4230,31 @@ nxt_unit_mmap_release(nxt_unit_ctx_t *ctx, nxt_port_mmap_header_t *hdr, nxt_atomic_fetch_add(&lib->outgoing.allocated_chunks, -freed_chunks); nxt_unit_debug(ctx, "allocated_chunks %d", - (int) lib->outgoing.allocated_chunks); + (int) lib->outgoing.allocated_chunks); } - if (hdr->dst_pid == lib->pid - && freed_chunks != 0 - && nxt_atomic_cmp_set(&hdr->oosm, 1, 0)) - { + if (hdr->dst_pid == lib->pid && freed_chunks != 0 + && nxt_atomic_cmp_set(&hdr->oosm, 1, 0)) { nxt_unit_send_shm_ack(ctx, hdr->src_pid); } } - static int -nxt_unit_send_shm_ack(nxt_unit_ctx_t *ctx, pid_t pid) -{ +nxt_unit_send_shm_ack(nxt_unit_ctx_t *ctx, pid_t pid) { ssize_t res; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - msg.stream = 0; - msg.pid = lib->pid; + msg.stream = 0; + msg.pid = lib->pid; msg.reply_port = 0; - msg.type = _NXT_PORT_MSG_SHM_ACK; - msg.last = 0; - msg.mmap = 0; - msg.nf = 0; - msg.mf = 0; + msg.type = _NXT_PORT_MSG_SHM_ACK; + msg.last = 0; + msg.mmap = 0; + msg.nf = 0; + msg.mf = 0; res = nxt_unit_port_send(ctx, lib->router_port, &msg, sizeof(msg), NULL); if (nxt_slow_path(res != sizeof(msg))) { @@ -4414,47 +4264,39 @@ nxt_unit_send_shm_ack(nxt_unit_ctx_t *ctx, pid_t pid) return NXT_UNIT_OK; } - static nxt_int_t -nxt_unit_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_process_t *process; +nxt_unit_lvlhsh_pid_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_process_t *process; process = data; if (lhq->key.length == sizeof(pid_t) - && *(pid_t *) lhq->key.start == process->pid) - { + && *(pid_t *) lhq->key.start == process->pid) { return NXT_OK; } return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_processes_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_unit_lvlhsh_pid_test, nxt_unit_lvlhsh_alloc, nxt_unit_lvlhsh_free, }; - static inline void -nxt_unit_process_lhq_pid(nxt_lvlhsh_query_t *lhq, pid_t *pid) -{ - lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); +nxt_unit_process_lhq_pid(nxt_lvlhsh_query_t *lhq, pid_t *pid) { + lhq->key_hash = nxt_murmur_hash2(pid, sizeof(*pid)); lhq->key.length = sizeof(*pid); - lhq->key.start = (u_char *) pid; - lhq->proto = &lvlhsh_processes_proto; + lhq->key.start = (u_char *) pid; + lhq->proto = &lvlhsh_processes_proto; } - static nxt_unit_process_t * -nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid) -{ - nxt_unit_impl_t *lib; - nxt_unit_process_t *process; +nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid) { + nxt_unit_impl_t *lib; + nxt_unit_process_t *process; nxt_lvlhsh_query_t lhq; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -4475,18 +4317,17 @@ nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid) return NULL; } - process->pid = pid; - process->use_count = 2; + process->pid = pid; + process->use_count = 2; process->next_port_id = 0; - process->lib = lib; + process->lib = lib; nxt_queue_init(&process->ports); lhq.replace = 0; - lhq.value = process; + lhq.value = process; switch (nxt_lvlhsh_insert(&lib->processes, &lhq)) { - case NXT_OK: break; @@ -4501,12 +4342,10 @@ nxt_unit_process_get(nxt_unit_ctx_t *ctx, pid_t pid) return process; } - static nxt_unit_process_t * -nxt_unit_process_find(nxt_unit_impl_t *lib, pid_t pid, int remove) -{ - int rc; - nxt_lvlhsh_query_t lhq; +nxt_unit_process_find(nxt_unit_impl_t *lib, pid_t pid, int remove) { + int rc; + nxt_lvlhsh_query_t lhq; nxt_unit_process_lhq_pid(&lhq, &pid); @@ -4528,19 +4367,15 @@ nxt_unit_process_find(nxt_unit_impl_t *lib, pid_t pid, int remove) return NULL; } - static nxt_unit_process_t * -nxt_unit_process_pop_first(nxt_unit_impl_t *lib) -{ +nxt_unit_process_pop_first(nxt_unit_impl_t *lib) { return nxt_lvlhsh_retrieve(&lib->processes, &lvlhsh_processes_proto, NULL); } - int -nxt_unit_run(nxt_unit_ctx_t *ctx) -{ +nxt_unit_run(nxt_unit_ctx_t *ctx) { int rc; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_ctx_impl_t *ctx_impl; nxt_unit_ctx_use(ctx); @@ -4562,11 +4397,9 @@ nxt_unit_run(nxt_unit_ctx_t *ctx) return rc; } - int -nxt_unit_run_once(nxt_unit_ctx_t *ctx) -{ - int rc; +nxt_unit_run_once(nxt_unit_ctx_t *ctx) { + int rc; nxt_unit_ctx_use(ctx); @@ -4577,12 +4410,10 @@ nxt_unit_run_once(nxt_unit_ctx_t *ctx) return rc; } - static int -nxt_unit_run_once_impl(nxt_unit_ctx_t *ctx) -{ +nxt_unit_run_once_impl(nxt_unit_ctx_t *ctx) { int rc; - nxt_unit_read_buf_t *rbuf; + nxt_unit_read_buf_t *rbuf; rbuf = nxt_unit_read_buf_get(ctx); if (nxt_slow_path(rbuf == NULL)) { @@ -4611,15 +4442,13 @@ nxt_unit_run_once_impl(nxt_unit_ctx_t *ctx) return rc; } - static int -nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) -{ +nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) { int nevents, res, err; nxt_uint_t nfds; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_port_impl_t *port_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_port_impl_t *port_impl; struct pollfd fds[2]; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -4628,8 +4457,8 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) return nxt_unit_ctx_port_recv(ctx, ctx_impl->read_port, rbuf); } - port_impl = nxt_container_of(ctx_impl->read_port, nxt_unit_port_impl_t, - port); + port_impl + = nxt_container_of(ctx_impl->read_port, nxt_unit_port_impl_t, port); lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -4642,15 +4471,13 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) port_impl->from_socket++; nxt_unit_debug(ctx, "port{%d,%d} dequeue 1 read_socket %d", - (int) ctx_impl->read_port->id.pid, - (int) ctx_impl->read_port->id.id, - port_impl->from_socket); + (int) ctx_impl->read_port->id.pid, + (int) ctx_impl->read_port->id.id, port_impl->from_socket); } else { nxt_unit_debug(ctx, "port{%d,%d} dequeue %d", - (int) ctx_impl->read_port->id.pid, - (int) ctx_impl->read_port->id.id, - (int) rbuf->size); + (int) ctx_impl->read_port->id.pid, + (int) ctx_impl->read_port->id.id, (int) rbuf->size); return NXT_UNIT_OK; } @@ -4663,7 +4490,7 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) return NXT_UNIT_OK; } - fds[1].fd = lib->shared_port->in_fd; + fds[1].fd = lib->shared_port->in_fd; fds[1].events = POLLIN; nfds = 2; @@ -4672,8 +4499,8 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) nfds = 1; } - fds[0].fd = ctx_impl->read_port->in_fd; - fds[0].events = POLLIN; + fds[0].fd = ctx_impl->read_port->in_fd; + fds[0].events = POLLIN; fds[0].revents = 0; fds[1].revents = 0; @@ -4686,17 +4513,16 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) goto retry; } - nxt_unit_alert(ctx, "poll(%d,%d) failed: %s (%d)", - fds[0].fd, fds[1].fd, strerror(err), err); + nxt_unit_alert(ctx, "poll(%d,%d) failed: %s (%d)", fds[0].fd, fds[1].fd, + strerror(err), err); rbuf->size = -1; return (err == EAGAIN) ? NXT_UNIT_AGAIN : NXT_UNIT_ERROR; } - nxt_unit_debug(ctx, "poll(%d,%d): %d, revents [%04X, %04X]", - fds[0].fd, fds[1].fd, nevents, fds[0].revents, - fds[1].revents); + nxt_unit_debug(ctx, "poll(%d,%d): %d, revents [%04X, %04X]", fds[0].fd, + fds[1].fd, nevents, fds[0].revents, fds[1].revents); if ((fds[0].revents & POLLIN) != 0) { res = nxt_unit_ctx_port_recv(ctx, ctx_impl->read_port, rbuf); @@ -4717,35 +4543,30 @@ nxt_unit_read_buf(nxt_unit_ctx_t *ctx, nxt_unit_read_buf_t *rbuf) } nxt_unit_alert(ctx, "poll(%d,%d): %d unexpected revents [%04uXi, %04uXi]", - fds[0].fd, fds[1].fd, nevents, fds[0].revents, - fds[1].revents); + fds[0].fd, fds[1].fd, nevents, fds[0].revents, fds[1].revents); return NXT_UNIT_ERROR; } - static int -nxt_unit_chk_ready(nxt_unit_ctx_t *ctx) -{ - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; +nxt_unit_chk_ready(nxt_unit_ctx_t *ctx) { + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); - lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); return (ctx_impl->ready && (lib->request_limit == 0 || lib->request_count < lib->request_limit)); } - static int -nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx) -{ +nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx) { int rc; nxt_queue_t pending_rbuf; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_read_buf_t *rbuf; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_read_buf_t *rbuf; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -4767,15 +4588,14 @@ nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx) rc = NXT_UNIT_OK; nxt_queue_each(rbuf, &pending_rbuf, nxt_unit_read_buf_t, link) { - if (nxt_fast_path(rc != NXT_UNIT_ERROR)) { rc = nxt_unit_process_msg(&ctx_impl->ctx, rbuf, NULL); } else { nxt_unit_read_buf_release(ctx, rbuf); } - - } nxt_queue_loop; + } + nxt_queue_loop; if (!ctx_impl->ready) { nxt_unit_quit(ctx, NXT_QUIT_GRACEFUL); @@ -4784,16 +4604,14 @@ nxt_unit_process_pending_rbuf(nxt_unit_ctx_t *ctx) return rc; } - static void -nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx) -{ +nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx) { int res; nxt_queue_t ready_req; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_t *req; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_t *req; + nxt_unit_request_info_impl_t *req_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -4812,9 +4630,8 @@ nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx) pthread_mutex_unlock(&ctx_impl->mutex); - nxt_queue_each(req_impl, &ready_req, - nxt_unit_request_info_impl_t, port_wait_link) - { + nxt_queue_each(req_impl, &ready_req, nxt_unit_request_info_impl_t, + port_wait_link) { lib = nxt_container_of(ctx_impl->ctx.unit, nxt_unit_impl_t, unit); req = &req_impl->req; @@ -4827,8 +4644,7 @@ nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx) } if (req->content_length - > (uint64_t) (req->content_buf->end - req->content_buf->free)) - { + > (uint64_t) (req->content_buf->end - req->content_buf->free)) { res = nxt_unit_request_hash_add(ctx, req); if (nxt_slow_path(res != NXT_UNIT_OK)) { nxt_unit_req_warn(req, "failed to add request to hash"); @@ -4848,17 +4664,15 @@ nxt_unit_process_ready_req(nxt_unit_ctx_t *ctx) } lib->callbacks.request_handler(&req_impl->req); - - } nxt_queue_loop; + } + nxt_queue_loop; } - int -nxt_unit_run_ctx(nxt_unit_ctx_t *ctx) -{ +nxt_unit_run_ctx(nxt_unit_ctx_t *ctx) { int rc; - nxt_unit_read_buf_t *rbuf; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_read_buf_t *rbuf; + nxt_unit_ctx_impl_t *ctx_impl; nxt_unit_ctx_use(ctx); @@ -4898,11 +4712,9 @@ nxt_unit_run_ctx(nxt_unit_ctx_t *ctx) return rc; } - nxt_inline int -nxt_unit_is_read_queue(nxt_unit_read_buf_t *rbuf) -{ - nxt_port_msg_t *port_msg; +nxt_unit_is_read_queue(nxt_unit_read_buf_t *rbuf) { + nxt_port_msg_t *port_msg; if (nxt_fast_path(rbuf->size == (ssize_t) sizeof(nxt_port_msg_t))) { port_msg = (nxt_port_msg_t *) rbuf->buf; @@ -4913,10 +4725,8 @@ nxt_unit_is_read_queue(nxt_unit_read_buf_t *rbuf) return 0; } - nxt_inline int -nxt_unit_is_read_socket(nxt_unit_read_buf_t *rbuf) -{ +nxt_unit_is_read_socket(nxt_unit_read_buf_t *rbuf) { if (nxt_fast_path(rbuf->size == 1)) { return rbuf->buf[0] == _NXT_PORT_MSG_READ_SOCKET; } @@ -4924,11 +4734,9 @@ nxt_unit_is_read_socket(nxt_unit_read_buf_t *rbuf) return 0; } - nxt_inline int -nxt_unit_is_shm_ack(nxt_unit_read_buf_t *rbuf) -{ - nxt_port_msg_t *port_msg; +nxt_unit_is_shm_ack(nxt_unit_read_buf_t *rbuf) { + nxt_port_msg_t *port_msg; if (nxt_fast_path(rbuf->size == (ssize_t) sizeof(nxt_port_msg_t))) { port_msg = (nxt_port_msg_t *) rbuf->buf; @@ -4939,11 +4747,9 @@ nxt_unit_is_shm_ack(nxt_unit_read_buf_t *rbuf) return 0; } - nxt_inline int -nxt_unit_is_quit(nxt_unit_read_buf_t *rbuf) -{ - nxt_port_msg_t *port_msg; +nxt_unit_is_quit(nxt_unit_read_buf_t *rbuf) { + nxt_port_msg_t *port_msg; if (nxt_fast_path(rbuf->size == (ssize_t) sizeof(nxt_port_msg_t))) { port_msg = (nxt_port_msg_t *) rbuf->buf; @@ -4954,13 +4760,11 @@ nxt_unit_is_quit(nxt_unit_read_buf_t *rbuf) return 0; } - int -nxt_unit_run_shared(nxt_unit_ctx_t *ctx) -{ +nxt_unit_run_shared(nxt_unit_ctx_t *ctx) { int rc; - nxt_unit_impl_t *lib; - nxt_unit_read_buf_t *rbuf; + nxt_unit_impl_t *lib; + nxt_unit_read_buf_t *rbuf; nxt_unit_ctx_use(ctx); @@ -4998,14 +4802,12 @@ nxt_unit_run_shared(nxt_unit_ctx_t *ctx) return rc; } - nxt_unit_request_info_t * -nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx) -{ +nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx) { int rc; - nxt_unit_impl_t *lib; - nxt_unit_read_buf_t *rbuf; - nxt_unit_request_info_t *req; + nxt_unit_impl_t *lib; + nxt_unit_read_buf_t *rbuf; + nxt_unit_request_info_t *req; nxt_unit_ctx_use(ctx); @@ -5037,11 +4839,9 @@ nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx) return req; } - int -nxt_unit_process_port_msg(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ - int rc; +nxt_unit_process_port_msg(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { + int rc; nxt_unit_ctx_use(ctx); @@ -5052,13 +4852,11 @@ nxt_unit_process_port_msg(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return rc; } - static int -nxt_unit_process_port_msg_impl(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ +nxt_unit_process_port_msg_impl(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { int rc; - nxt_unit_impl_t *lib; - nxt_unit_read_buf_t *rbuf; + nxt_unit_impl_t *lib; + nxt_unit_read_buf_t *rbuf; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -5098,28 +4896,24 @@ nxt_unit_process_port_msg_impl(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return rc; } - void -nxt_unit_done(nxt_unit_ctx_t *ctx) -{ +nxt_unit_done(nxt_unit_ctx_t *ctx) { nxt_unit_ctx_release(ctx); } - nxt_unit_ctx_t * -nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data) -{ +nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data) { int rc, queue_fd; - void *mem; - nxt_unit_impl_t *lib; - nxt_unit_port_t *port; - nxt_unit_ctx_impl_t *new_ctx; - nxt_unit_port_impl_t *port_impl; + void *mem; + nxt_unit_impl_t *lib; + nxt_unit_port_t *port; + nxt_unit_ctx_impl_t *new_ctx; + nxt_unit_port_impl_t *port_impl; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - new_ctx = nxt_unit_malloc(ctx, sizeof(nxt_unit_ctx_impl_t) - + lib->request_data_size); + new_ctx = nxt_unit_malloc(ctx, + sizeof(nxt_unit_ctx_impl_t) + lib->request_data_size); if (nxt_slow_path(new_ctx == NULL)) { nxt_unit_alert(ctx, "failed to allocate context"); @@ -5147,18 +4941,18 @@ nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data) goto fail; } - mem = mmap(NULL, sizeof(nxt_port_queue_t), - PROT_READ | PROT_WRITE, MAP_SHARED, queue_fd, 0); + mem = mmap(NULL, sizeof(nxt_port_queue_t), PROT_READ | PROT_WRITE, + MAP_SHARED, queue_fd, 0); if (nxt_slow_path(mem == MAP_FAILED)) { nxt_unit_alert(ctx, "mmap(%d) failed: %s (%d)", queue_fd, - strerror(errno), errno); + strerror(errno), errno); goto fail; } nxt_port_queue_init(mem); - port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); + port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); port_impl->queue = mem; rc = nxt_unit_send_port(&new_ctx->ctx, lib->router_port, port, queue_fd); @@ -5181,26 +4975,23 @@ nxt_unit_ctx_alloc(nxt_unit_ctx_t *ctx, void *data) return NULL; } - static void -nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl) -{ - nxt_unit_impl_t *lib; - nxt_unit_mmap_buf_t *mmap_buf; - nxt_unit_read_buf_t *rbuf; - nxt_unit_request_info_impl_t *req_impl; - nxt_unit_websocket_frame_impl_t *ws_impl; +nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl) { + nxt_unit_impl_t *lib; + nxt_unit_mmap_buf_t *mmap_buf; + nxt_unit_read_buf_t *rbuf; + nxt_unit_request_info_impl_t *req_impl; + nxt_unit_websocket_frame_impl_t *ws_impl; lib = nxt_container_of(ctx_impl->ctx.unit, nxt_unit_impl_t, unit); nxt_queue_each(req_impl, &ctx_impl->active_req, - nxt_unit_request_info_impl_t, link) - { + nxt_unit_request_info_impl_t, link) { nxt_unit_req_warn(&req_impl->req, "active request on ctx free"); nxt_unit_request_done(&req_impl->req, NXT_UNIT_ERROR); - - } nxt_queue_loop; + } + nxt_queue_loop; nxt_unit_mmap_buf_unlink(&ctx_impl->ctx_buf[0]); nxt_unit_mmap_buf_unlink(&ctx_impl->ctx_buf[1]); @@ -5211,26 +5002,24 @@ nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl) nxt_unit_free(&ctx_impl->ctx, mmap_buf); } - nxt_queue_each(req_impl, &ctx_impl->free_req, - nxt_unit_request_info_impl_t, link) - { + nxt_queue_each(req_impl, &ctx_impl->free_req, nxt_unit_request_info_impl_t, + link) { nxt_unit_request_info_free(req_impl); + } + nxt_queue_loop; - } nxt_queue_loop; - - nxt_queue_each(ws_impl, &ctx_impl->free_ws, - nxt_unit_websocket_frame_impl_t, link) - { + nxt_queue_each(ws_impl, &ctx_impl->free_ws, nxt_unit_websocket_frame_impl_t, + link) { nxt_unit_websocket_frame_free(&ctx_impl->ctx, ws_impl); + } + nxt_queue_loop; - } nxt_queue_loop; - - nxt_queue_each(rbuf, &ctx_impl->free_rbuf, nxt_unit_read_buf_t, link) - { + nxt_queue_each(rbuf, &ctx_impl->free_rbuf, nxt_unit_read_buf_t, link) { if (rbuf != &ctx_impl->ctx_read_buf) { nxt_unit_free(&ctx_impl->ctx, rbuf); } - } nxt_queue_loop; + } + nxt_queue_loop; pthread_mutex_destroy(&ctx_impl->mutex); @@ -5252,67 +5041,63 @@ nxt_unit_ctx_free(nxt_unit_ctx_impl_t *ctx_impl) nxt_unit_lib_release(lib); } - /* SOCK_SEQPACKET is disabled to test SOCK_DGRAM on all platforms. */ #if (0 || NXT_HAVE_AF_UNIX_SOCK_SEQPACKET) -#define NXT_UNIX_SOCKET SOCK_SEQPACKET +#define NXT_UNIX_SOCKET SOCK_SEQPACKET #else -#define NXT_UNIX_SOCKET SOCK_DGRAM +#define NXT_UNIX_SOCKET SOCK_DGRAM #endif void -nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id) -{ - nxt_unit_port_hash_id_t port_hash_id; +nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id) { + nxt_unit_port_hash_id_t port_hash_id; port_hash_id.pid = pid; - port_hash_id.id = id; + port_hash_id.id = id; - port_id->pid = pid; + port_id->pid = pid; port_id->hash = nxt_murmur_hash2(&port_hash_id, sizeof(port_hash_id)); - port_id->id = id; + port_id->id = id; } - static nxt_unit_port_t * -nxt_unit_create_port(nxt_unit_ctx_t *ctx) -{ +nxt_unit_create_port(nxt_unit_ctx_t *ctx) { int rc, port_sockets[2]; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; nxt_unit_port_t new_port, *port; - nxt_unit_process_t *process; + nxt_unit_process_t *process; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); rc = socketpair(AF_UNIX, NXT_UNIX_SOCKET, 0, port_sockets); if (nxt_slow_path(rc != 0)) { nxt_unit_warn(ctx, "create_port: socketpair() failed: %s (%d)", - strerror(errno), errno); + strerror(errno), errno); return NULL; } #if (NXT_HAVE_SOCKOPT_SO_PASSCRED) - int enable_creds = 1; + int enable_creds = 1; if (nxt_slow_path(setsockopt(port_sockets[0], SOL_SOCKET, SO_PASSCRED, - &enable_creds, sizeof(enable_creds)) == -1)) - { + &enable_creds, sizeof(enable_creds)) + == -1)) { nxt_unit_warn(ctx, "failed to set SO_PASSCRED %s", strerror(errno)); return NULL; } if (nxt_slow_path(setsockopt(port_sockets[1], SOL_SOCKET, SO_PASSCRED, - &enable_creds, sizeof(enable_creds)) == -1)) - { + &enable_creds, sizeof(enable_creds)) + == -1)) { nxt_unit_warn(ctx, "failed to set SO_PASSCRED %s", strerror(errno)); return NULL; } #endif - nxt_unit_debug(ctx, "create_port: new socketpair: %d->%d", - port_sockets[0], port_sockets[1]); + nxt_unit_debug(ctx, "create_port: new socketpair: %d->%d", port_sockets[0], + port_sockets[1]); pthread_mutex_lock(&lib->mutex); @@ -5328,9 +5113,9 @@ nxt_unit_create_port(nxt_unit_ctx_t *ctx) nxt_unit_port_id_init(&new_port.id, lib->pid, process->next_port_id++); - new_port.in_fd = port_sockets[0]; + new_port.in_fd = port_sockets[0]; new_port.out_fd = port_sockets[1]; - new_port.data = NULL; + new_port.data = NULL; pthread_mutex_unlock(&lib->mutex); @@ -5345,36 +5130,34 @@ nxt_unit_create_port(nxt_unit_ctx_t *ctx) return port; } - static int nxt_unit_send_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *dst, - nxt_unit_port_t *port, int queue_fd) -{ + nxt_unit_port_t *port, int queue_fd) { ssize_t res; nxt_send_oob_t oob; - nxt_unit_impl_t *lib; - int fds[2] = { port->out_fd, queue_fd }; + nxt_unit_impl_t *lib; + int fds[2] = {port->out_fd, queue_fd}; struct { - nxt_port_msg_t msg; - nxt_port_msg_new_port_t new_port; + nxt_port_msg_t msg; + nxt_port_msg_new_port_t new_port; } m; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - m.msg.stream = 0; - m.msg.pid = lib->pid; + m.msg.stream = 0; + m.msg.pid = lib->pid; m.msg.reply_port = 0; - m.msg.type = _NXT_PORT_MSG_NEW_PORT; - m.msg.last = 0; - m.msg.mmap = 0; - m.msg.nf = 0; - m.msg.mf = 0; - - m.new_port.id = port->id.id; - m.new_port.pid = port->id.pid; - m.new_port.type = NXT_PROCESS_APP; - m.new_port.max_size = 16 * 1024; + m.msg.type = _NXT_PORT_MSG_NEW_PORT; + m.msg.last = 0; + m.msg.mmap = 0; + m.msg.nf = 0; + m.msg.mf = 0; + + m.new_port.id = port->id.id; + m.new_port.pid = port->id.pid; + m.new_port.type = NXT_PROCESS_APP; + m.new_port.max_size = 16 * 1024; m.new_port.max_share = 64 * 1024; nxt_socket_msg_oob_init(&oob, fds); @@ -5384,21 +5167,19 @@ nxt_unit_send_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *dst, return (res == sizeof(m)) ? NXT_UNIT_OK : NXT_UNIT_ERROR; } - -nxt_inline void nxt_unit_port_use(nxt_unit_port_t *port) -{ - nxt_unit_port_impl_t *port_impl; +nxt_inline void +nxt_unit_port_use(nxt_unit_port_t *port) { + nxt_unit_port_impl_t *port_impl; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); nxt_atomic_fetch_add(&port_impl->use_count, 1); } - -nxt_inline void nxt_unit_port_release(nxt_unit_port_t *port) -{ +nxt_inline void +nxt_unit_port_release(nxt_unit_port_t *port) { long c; - nxt_unit_port_impl_t *port_impl; + nxt_unit_port_impl_t *port_impl; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); @@ -5406,8 +5187,7 @@ nxt_inline void nxt_unit_port_release(nxt_unit_port_t *port) if (c == 1) { nxt_unit_debug(NULL, "destroy port{%d,%d} in_fd %d out_fd %d", - (int) port->id.pid, (int) port->id.id, - port->in_fd, port->out_fd); + (int) port->id.pid, (int) port->id.id, port->in_fd, port->out_fd); nxt_unit_process_release(port_impl->process); @@ -5425,24 +5205,22 @@ nxt_inline void nxt_unit_port_release(nxt_unit_port_t *port) if (port_impl->queue != NULL) { munmap(port_impl->queue, (port->id.id == NXT_UNIT_SHARED_PORT_ID) - ? sizeof(nxt_app_queue_t) - : sizeof(nxt_port_queue_t)); + ? sizeof(nxt_app_queue_t) + : sizeof(nxt_port_queue_t)); } nxt_unit_free(NULL, port_impl); } } - static nxt_unit_port_t * -nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) -{ +nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) { int rc, ready; nxt_queue_t awaiting_req; - nxt_unit_impl_t *lib; - nxt_unit_port_t *old_port; - nxt_unit_process_t *process; - nxt_unit_port_impl_t *new_port, *old_port_impl; + nxt_unit_impl_t *lib; + nxt_unit_port_t *old_port; + nxt_unit_process_t *process; + nxt_unit_port_impl_t *new_port, *old_port_impl; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); @@ -5451,19 +5229,19 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) old_port = nxt_unit_port_hash_find(&lib->ports, &port->id, 0); if (nxt_slow_path(old_port != NULL)) { - nxt_unit_debug(ctx, "add_port: duplicate port{%d,%d} " - "in_fd %d out_fd %d queue %p", - port->id.pid, port->id.id, - port->in_fd, port->out_fd, queue); + nxt_unit_debug(ctx, + "add_port: duplicate port{%d,%d} " + "in_fd %d out_fd %d queue %p", + port->id.pid, port->id.id, port->in_fd, port->out_fd, queue); if (old_port->data == NULL) { old_port->data = port->data; - port->data = NULL; + port->data = NULL; } if (old_port->in_fd == -1) { old_port->in_fd = port->in_fd; - port->in_fd = -1; + port->in_fd = -1; } if (port->in_fd != -1) { @@ -5473,7 +5251,7 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) if (old_port->out_fd == -1) { old_port->out_fd = port->out_fd; - port->out_fd = -1; + port->out_fd = -1; } if (port->out_fd != -1) { @@ -5529,11 +5307,10 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) } new_port = NULL; - ready = 0; + ready = 0; nxt_unit_debug(ctx, "add_port: port{%d,%d} in_fd %d out_fd %d queue %p", - port->id.pid, port->id.id, - port->in_fd, port->out_fd, queue); + port->id.pid, port->id.id, port->in_fd, port->out_fd, queue); process = nxt_unit_process_get(ctx, port->id.pid); if (nxt_slow_path(process == NULL)) { @@ -5541,15 +5318,14 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) } if (port->id.id != NXT_UNIT_SHARED_PORT_ID - && port->id.id >= process->next_port_id) - { + && port->id.id >= process->next_port_id) { process->next_port_id = port->id.id + 1; } new_port = nxt_unit_malloc(ctx, sizeof(nxt_unit_port_impl_t)); if (nxt_slow_path(new_port == NULL)) { - nxt_unit_alert(ctx, "add_port: %d,%d malloc() failed", - port->id.pid, port->id.id); + nxt_unit_alert(ctx, "add_port: %d,%d malloc() failed", port->id.pid, + port->id.id); goto unlock; } @@ -5558,8 +5334,8 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) rc = nxt_unit_port_hash_add(&lib->ports, &new_port->port); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - nxt_unit_alert(ctx, "add_port: %d,%d hash_add failed", - port->id.pid, port->id.id); + nxt_unit_alert(ctx, "add_port: %d,%d hash_add failed", port->id.pid, + port->id.id); nxt_unit_free(ctx, new_port); @@ -5570,9 +5346,9 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) nxt_queue_insert_tail(&process->ports, &new_port->link); - new_port->use_count = 2; - new_port->process = process; - new_port->queue = queue; + new_port->use_count = 2; + new_port->process = process; + new_port->queue = queue; new_port->from_socket = 0; new_port->socket_rbuf = NULL; @@ -5619,42 +5395,36 @@ nxt_unit_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, void *queue) return (new_port == NULL) ? NULL : &new_port->port; } - static void -nxt_unit_process_awaiting_req(nxt_unit_ctx_t *ctx, nxt_queue_t *awaiting_req) -{ - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; - - nxt_queue_each(req_impl, awaiting_req, - nxt_unit_request_info_impl_t, port_wait_link) - { +nxt_unit_process_awaiting_req(nxt_unit_ctx_t *ctx, nxt_queue_t *awaiting_req) { + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; + + nxt_queue_each(req_impl, awaiting_req, nxt_unit_request_info_impl_t, + port_wait_link) { nxt_queue_remove(&req_impl->port_wait_link); - ctx_impl = nxt_container_of(req_impl->req.ctx, nxt_unit_ctx_impl_t, - ctx); + ctx_impl + = nxt_container_of(req_impl->req.ctx, nxt_unit_ctx_impl_t, ctx); pthread_mutex_lock(&ctx_impl->mutex); - nxt_queue_insert_tail(&ctx_impl->ready_req, - &req_impl->port_wait_link); + nxt_queue_insert_tail(&ctx_impl->ready_req, &req_impl->port_wait_link); pthread_mutex_unlock(&ctx_impl->mutex); nxt_atomic_fetch_add(&ctx_impl->wait_items, -1); nxt_unit_awake_ctx(ctx, ctx_impl); - - } nxt_queue_loop; + } + nxt_queue_loop; } - static void nxt_unit_remove_port(nxt_unit_impl_t *lib, nxt_unit_ctx_t *ctx, - nxt_unit_port_id_t *port_id) -{ - nxt_unit_port_t *port; - nxt_unit_port_impl_t *port_impl; + nxt_unit_port_id_t *port_id) { + nxt_unit_port_t *port; + nxt_unit_port_impl_t *port_impl; pthread_mutex_lock(&lib->mutex); @@ -5677,32 +5447,28 @@ nxt_unit_remove_port(nxt_unit_impl_t *lib, nxt_unit_ctx_t *ctx, } } - static nxt_unit_port_t * -nxt_unit_remove_port_unsafe(nxt_unit_impl_t *lib, nxt_unit_port_id_t *port_id) -{ - nxt_unit_port_t *port; +nxt_unit_remove_port_unsafe(nxt_unit_impl_t *lib, nxt_unit_port_id_t *port_id) { + nxt_unit_port_t *port; port = nxt_unit_port_hash_find(&lib->ports, port_id, 1); if (nxt_slow_path(port == NULL)) { nxt_unit_debug(NULL, "remove_port: port{%d,%d} not found", - (int) port_id->pid, (int) port_id->id); + (int) port_id->pid, (int) port_id->id); return NULL; } nxt_unit_debug(NULL, "remove_port: port{%d,%d}, fds %d,%d, data %p", - (int) port_id->pid, (int) port_id->id, - port->in_fd, port->out_fd, port->data); + (int) port_id->pid, (int) port_id->id, port->in_fd, port->out_fd, + port->data); return port; } - static void -nxt_unit_remove_pid(nxt_unit_impl_t *lib, pid_t pid) -{ - nxt_unit_process_t *process; +nxt_unit_remove_pid(nxt_unit_impl_t *lib, pid_t pid) { + nxt_unit_process_t *process; pthread_mutex_lock(&lib->mutex); @@ -5722,27 +5488,23 @@ nxt_unit_remove_pid(nxt_unit_impl_t *lib, pid_t pid) } } - static void -nxt_unit_remove_process(nxt_unit_impl_t *lib, nxt_unit_process_t *process) -{ +nxt_unit_remove_process(nxt_unit_impl_t *lib, nxt_unit_process_t *process) { nxt_queue_t ports; - nxt_unit_port_impl_t *port; + nxt_unit_port_impl_t *port; nxt_queue_init(&ports); nxt_queue_add(&ports, &process->ports); nxt_queue_each(port, &ports, nxt_unit_port_impl_t, link) { - nxt_unit_remove_port_unsafe(lib, &port->port.id); - - } nxt_queue_loop; + } + nxt_queue_loop; pthread_mutex_unlock(&lib->mutex); nxt_queue_each(port, &ports, nxt_unit_port_impl_t, link) { - nxt_queue_remove(&port->link); if (lib->callbacks.remove_port != NULL) { @@ -5750,40 +5512,38 @@ nxt_unit_remove_process(nxt_unit_impl_t *lib, nxt_unit_process_t *process) } nxt_unit_port_release(&port->port); - - } nxt_queue_loop; + } + nxt_queue_loop; nxt_unit_process_release(process); } - static void -nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) -{ +nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) { nxt_bool_t skip_graceful_broadcast, quit; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_callbacks_t *cb; - nxt_unit_request_info_t *req; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_callbacks_t *cb; + nxt_unit_request_info_t *req; + nxt_unit_request_info_impl_t *req_impl; struct { - nxt_port_msg_t msg; - uint8_t quit_param; + nxt_port_msg_t msg; + uint8_t quit_param; } nxt_packed m; - lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); nxt_unit_debug(ctx, "quit: %d/%d/%d", (int) quit_param, ctx_impl->ready, - ctx_impl->online); + ctx_impl->online); if (nxt_slow_path(!ctx_impl->online)) { return; } - skip_graceful_broadcast = quit_param == NXT_QUIT_GRACEFUL - && !ctx_impl->ready; + skip_graceful_broadcast + = quit_param == NXT_QUIT_GRACEFUL && !ctx_impl->ready; cb = &lib->callbacks; @@ -5805,7 +5565,7 @@ nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) pthread_mutex_unlock(&ctx_impl->mutex); } else { - quit = 1; + quit = 1; ctx_impl->quit_param = NXT_QUIT_GRACEFUL; } @@ -5817,8 +5577,7 @@ nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) } nxt_queue_each(req_impl, &ctx_impl->active_req, - nxt_unit_request_info_impl_t, link) - { + nxt_unit_request_info_impl_t, link) { req = &req_impl->req; nxt_unit_req_warn(req, "active request on ctx quit"); @@ -5831,8 +5590,8 @@ nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) } else { nxt_unit_request_done(req, NXT_UNIT_ERROR); } - - } nxt_queue_loop; + } + nxt_queue_loop; if (nxt_fast_path(ctx_impl->read_port != NULL)) { nxt_unit_remove_port(lib, ctx, &ctx_impl->read_port->id); @@ -5845,56 +5604,51 @@ nxt_unit_quit(nxt_unit_ctx_t *ctx, uint8_t quit_param) memset(&m.msg, 0, sizeof(nxt_port_msg_t)); - m.msg.pid = lib->pid; - m.msg.type = _NXT_PORT_MSG_QUIT; + m.msg.pid = lib->pid; + m.msg.type = _NXT_PORT_MSG_QUIT; m.quit_param = quit_param; pthread_mutex_lock(&lib->mutex); nxt_queue_each(ctx_impl, &lib->contexts, nxt_unit_ctx_impl_t, link) { - - if (ctx == &ctx_impl->ctx - || ctx_impl->read_port == NULL - || ctx_impl->read_port->out_fd == -1) - { + if (ctx == &ctx_impl->ctx || ctx_impl->read_port == NULL + || ctx_impl->read_port->out_fd == -1) { continue; } - (void) nxt_unit_port_send(ctx, ctx_impl->read_port, - &m, sizeof(m), NULL); - - } nxt_queue_loop; + (void) nxt_unit_port_send(ctx, ctx_impl->read_port, &m, sizeof(m), + NULL); + } + nxt_queue_loop; pthread_mutex_unlock(&lib->mutex); } - static int -nxt_unit_get_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id) -{ +nxt_unit_get_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id) { ssize_t res; - nxt_unit_impl_t *lib; - nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_impl_t *lib; + nxt_unit_ctx_impl_t *ctx_impl; struct { - nxt_port_msg_t msg; - nxt_port_msg_get_port_t get_port; + nxt_port_msg_t msg; + nxt_port_msg_get_port_t get_port; } m; - lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); + lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); memset(&m.msg, 0, sizeof(nxt_port_msg_t)); - m.msg.pid = lib->pid; + m.msg.pid = lib->pid; m.msg.reply_port = ctx_impl->read_port->id.id; - m.msg.type = _NXT_PORT_MSG_GET_PORT; + m.msg.type = _NXT_PORT_MSG_GET_PORT; - m.get_port.id = port_id->id; + m.get_port.id = port_id->id; m.get_port.pid = port_id->pid; nxt_unit_debug(ctx, "get_port: %d %d", (int) port_id->pid, - (int) port_id->id); + (int) port_id->id); res = nxt_unit_port_send(ctx, lib->router_port, &m, sizeof(m), NULL); if (nxt_slow_path(res != sizeof(m))) { @@ -5904,35 +5658,31 @@ nxt_unit_get_port(nxt_unit_ctx_t *ctx, nxt_unit_port_id_t *port_id) return NXT_UNIT_OK; } - static ssize_t -nxt_unit_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - const void *buf, size_t buf_size, const nxt_send_oob_t *oob) -{ +nxt_unit_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, const void *buf, + size_t buf_size, const nxt_send_oob_t *oob) { int notify; ssize_t ret; nxt_int_t rc; nxt_port_msg_t msg; - nxt_unit_impl_t *lib; - nxt_unit_port_impl_t *port_impl; + nxt_unit_impl_t *lib; + nxt_unit_port_impl_t *port_impl; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); if (port_impl->queue != NULL && (oob == NULL || oob->size == 0) - && buf_size <= NXT_PORT_QUEUE_MSG_SIZE) - { + && buf_size <= NXT_PORT_QUEUE_MSG_SIZE) { rc = nxt_port_queue_send(port_impl->queue, buf, buf_size, ¬ify); if (nxt_slow_path(rc != NXT_OK)) { nxt_unit_alert(ctx, "port_send: port %d,%d queue overflow", - (int) port->id.pid, (int) port->id.id); + (int) port->id.pid, (int) port->id.id); return -1; } nxt_unit_debug(ctx, "port{%d,%d} enqueue %d notify %d", - (int) port->id.pid, (int) port->id.id, - (int) buf_size, notify); + (int) port->id.pid, (int) port->id.id, (int) buf_size, notify); if (notify) { memcpy(&msg, buf, sizeof(nxt_port_msg_t)); @@ -5941,21 +5691,18 @@ nxt_unit_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, if (lib->callbacks.port_send == NULL) { ret = nxt_unit_sendmsg(ctx, port->out_fd, &msg, - sizeof(nxt_port_msg_t), NULL); + sizeof(nxt_port_msg_t), NULL); nxt_unit_debug(ctx, "port{%d,%d} send %d read_queue", - (int) port->id.pid, (int) port->id.id, - (int) ret); + (int) port->id.pid, (int) port->id.id, (int) ret); } else { ret = lib->callbacks.port_send(ctx, port, &msg, - sizeof(nxt_port_msg_t), NULL, 0); + sizeof(nxt_port_msg_t), NULL, 0); nxt_unit_debug(ctx, "port{%d,%d} sendcb %d read_queue", - (int) port->id.pid, (int) port->id.id, - (int) ret); + (int) port->id.pid, (int) port->id.id, (int) ret); } - } return buf_size; @@ -5967,46 +5714,41 @@ nxt_unit_port_send(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, rc = nxt_port_queue_send(port_impl->queue, &msg.type, 1, ¬ify); if (nxt_slow_path(rc != NXT_OK)) { nxt_unit_alert(ctx, "port_send: port %d,%d queue overflow", - (int) port->id.pid, (int) port->id.id); + (int) port->id.pid, (int) port->id.id); return -1; } nxt_unit_debug(ctx, "port{%d,%d} enqueue 1 read_socket notify %d", - (int) port->id.pid, (int) port->id.id, notify); + (int) port->id.pid, (int) port->id.id, notify); } if (lib->callbacks.port_send != NULL) { ret = lib->callbacks.port_send(ctx, port, buf, buf_size, - oob != NULL ? oob->buf : NULL, - oob != NULL ? oob->size : 0); + oob != NULL ? oob->buf : NULL, oob != NULL ? oob->size : 0); - nxt_unit_debug(ctx, "port{%d,%d} sendcb %d", - (int) port->id.pid, (int) port->id.id, - (int) ret); + nxt_unit_debug(ctx, "port{%d,%d} sendcb %d", (int) port->id.pid, + (int) port->id.id, (int) ret); } else { ret = nxt_unit_sendmsg(ctx, port->out_fd, buf, buf_size, oob); - nxt_unit_debug(ctx, "port{%d,%d} sendmsg %d", - (int) port->id.pid, (int) port->id.id, - (int) ret); + nxt_unit_debug(ctx, "port{%d,%d} sendmsg %d", (int) port->id.pid, + (int) port->id.id, (int) ret); } return ret; } - static ssize_t -nxt_unit_sendmsg(nxt_unit_ctx_t *ctx, int fd, - const void *buf, size_t buf_size, const nxt_send_oob_t *oob) -{ - int err; - ssize_t n; - struct iovec iov[1]; +nxt_unit_sendmsg(nxt_unit_ctx_t *ctx, int fd, const void *buf, size_t buf_size, + const nxt_send_oob_t *oob) { + int err; + ssize_t n; + struct iovec iov[1]; iov[0].iov_base = (void *) buf; - iov[0].iov_len = buf_size; + iov[0].iov_len = buf_size; retry: @@ -6023,24 +5765,22 @@ nxt_unit_sendmsg(nxt_unit_ctx_t *ctx, int fd, * FIXME: This should be "alert" after router graceful shutdown * implementation. */ - nxt_unit_warn(ctx, "sendmsg(%d, %d) failed: %s (%d)", - fd, (int) buf_size, strerror(err), err); + nxt_unit_warn(ctx, "sendmsg(%d, %d) failed: %s (%d)", fd, + (int) buf_size, strerror(err), err); } else { nxt_unit_debug(ctx, "sendmsg(%d, %d, %d): %d", fd, (int) buf_size, - (oob != NULL ? (int) oob->size : 0), (int) n); + (oob != NULL ? (int) oob->size : 0), (int) n); } return n; } - static int nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { int res, read; - nxt_unit_port_impl_t *port_impl; + nxt_unit_port_impl_t *port_impl; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); @@ -6050,16 +5790,14 @@ nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, if (port_impl->from_socket > 0) { if (port_impl->socket_rbuf != NULL - && port_impl->socket_rbuf->size > 0) - { + && port_impl->socket_rbuf->size > 0) { port_impl->from_socket--; nxt_unit_rbuf_cpy(rbuf, port_impl->socket_rbuf); port_impl->socket_rbuf->size = 0; nxt_unit_debug(ctx, "port{%d,%d} use suspended message %d", - (int) port->id.pid, (int) port->id.id, - (int) rbuf->size); + (int) port->id.pid, (int) port->id.id, (int) rbuf->size); return NXT_UNIT_OK; } @@ -6072,15 +5810,14 @@ nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, port_impl->from_socket++; nxt_unit_debug(ctx, "port{%d,%d} dequeue 1 read_socket %d", - (int) port->id.pid, (int) port->id.id, - port_impl->from_socket); + (int) port->id.pid, (int) port->id.id, + port_impl->from_socket); goto retry; } - nxt_unit_debug(ctx, "port{%d,%d} dequeue %d", - (int) port->id.pid, (int) port->id.id, - (int) rbuf->size); + nxt_unit_debug(ctx, "port{%d,%d} dequeue %d", (int) port->id.pid, + (int) port->id.id, (int) rbuf->size); return NXT_UNIT_OK; } @@ -6099,14 +5836,13 @@ nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, if (nxt_unit_is_read_queue(rbuf)) { nxt_unit_debug(ctx, "port{%d,%d} recv %d read_queue", - (int) port->id.pid, (int) port->id.id, (int) rbuf->size); + (int) port->id.pid, (int) port->id.id, (int) rbuf->size); goto retry; } - nxt_unit_debug(ctx, "port{%d,%d} recvmsg %d", - (int) port->id.pid, (int) port->id.id, - (int) rbuf->size); + nxt_unit_debug(ctx, "port{%d,%d} recvmsg %d", (int) port->id.pid, + (int) port->id.id, (int) rbuf->size); if (res == NXT_UNIT_AGAIN) { return NXT_UNIT_AGAIN; @@ -6118,9 +5854,8 @@ nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, return NXT_UNIT_OK; } - nxt_unit_debug(ctx, "port{%d,%d} suspend message %d", - (int) port->id.pid, (int) port->id.id, - (int) rbuf->size); + nxt_unit_debug(ctx, "port{%d,%d} suspend message %d", (int) port->id.pid, + (int) port->id.id, (int) rbuf->size); if (port_impl->socket_rbuf == NULL) { port_impl->socket_rbuf = nxt_unit_read_buf_get(ctx); @@ -6145,23 +5880,19 @@ nxt_unit_ctx_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, goto retry; } - nxt_inline void -nxt_unit_rbuf_cpy(nxt_unit_read_buf_t *dst, nxt_unit_read_buf_t *src) -{ +nxt_unit_rbuf_cpy(nxt_unit_read_buf_t *dst, nxt_unit_read_buf_t *src) { memcpy(dst->buf, src->buf, src->size); - dst->size = src->size; + dst->size = src->size; dst->oob.size = src->oob.size; memcpy(dst->oob.buf, src->oob.buf, src->oob.size); } - static int nxt_unit_shared_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { int res; - nxt_unit_port_impl_t *port_impl; + nxt_unit_port_impl_t *port_impl; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); @@ -6183,7 +5914,7 @@ nxt_unit_shared_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, nxt_app_queue_notification_received(port_impl->queue); nxt_unit_debug(ctx, "port{%d,%d} recv %d read_queue", - (int) port->id.pid, (int) port->id.id, (int) rbuf->size); + (int) port->id.pid, (int) port->id.id, (int) rbuf->size); goto retry; } @@ -6192,27 +5923,24 @@ nxt_unit_shared_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, return res; } - static int nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { int fd, err; size_t oob_size; struct iovec iov[1]; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); if (lib->callbacks.port_recv != NULL) { oob_size = sizeof(rbuf->oob.buf); - rbuf->size = lib->callbacks.port_recv(ctx, port, - rbuf->buf, sizeof(rbuf->buf), - rbuf->oob.buf, &oob_size); + rbuf->size = lib->callbacks.port_recv(ctx, port, rbuf->buf, + sizeof(rbuf->buf), rbuf->oob.buf, &oob_size); - nxt_unit_debug(ctx, "port{%d,%d} recvcb %d", - (int) port->id.pid, (int) port->id.id, (int) rbuf->size); + nxt_unit_debug(ctx, "port{%d,%d} recvcb %d", (int) port->id.pid, + (int) port->id.id, (int) rbuf->size); if (nxt_slow_path(rbuf->size < 0)) { return NXT_UNIT_ERROR; @@ -6223,7 +5951,7 @@ nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, } iov[0].iov_base = rbuf->buf; - iov[0].iov_len = sizeof(rbuf->buf); + iov[0].iov_len = sizeof(rbuf->buf); fd = port->in_fd; @@ -6239,14 +5967,14 @@ nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, } if (err == EAGAIN) { - nxt_unit_debug(ctx, "recvmsg(%d) failed: %s (%d)", - fd, strerror(err), err); + nxt_unit_debug(ctx, "recvmsg(%d) failed: %s (%d)", fd, + strerror(err), err); return NXT_UNIT_AGAIN; } - nxt_unit_alert(ctx, "recvmsg(%d) failed: %s (%d)", - fd, strerror(err), err); + nxt_unit_alert(ctx, "recvmsg(%d) failed: %s (%d)", fd, strerror(err), + err); return NXT_UNIT_ERROR; } @@ -6256,11 +5984,9 @@ nxt_unit_port_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, return NXT_UNIT_OK; } - static int -nxt_unit_port_queue_recv(nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf) -{ - nxt_unit_port_impl_t *port_impl; +nxt_unit_port_queue_recv(nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf) { + nxt_unit_port_impl_t *port_impl; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); @@ -6269,24 +5995,22 @@ nxt_unit_port_queue_recv(nxt_unit_port_t *port, nxt_unit_read_buf_t *rbuf) return (rbuf->size == -1) ? NXT_UNIT_AGAIN : NXT_UNIT_OK; } - static int nxt_unit_app_queue_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, - nxt_unit_read_buf_t *rbuf) -{ + nxt_unit_read_buf_t *rbuf) { uint32_t cookie; - nxt_port_msg_t *port_msg; - nxt_app_queue_t *queue; - nxt_unit_impl_t *lib; - nxt_unit_port_impl_t *port_impl; + nxt_port_msg_t *port_msg; + nxt_app_queue_t *queue; + nxt_unit_impl_t *lib; + nxt_unit_port_impl_t *port_impl; struct { - nxt_port_msg_t msg; - uint8_t quit_param; + nxt_port_msg_t msg; + uint8_t quit_param; } nxt_packed m; port_impl = nxt_container_of(port, nxt_unit_port_impl_t, port); - queue = port_impl->queue; + queue = port_impl->queue; retry: @@ -6308,12 +6032,12 @@ nxt_unit_app_queue_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, memset(&m.msg, 0, sizeof(nxt_port_msg_t)); - m.msg.pid = lib->pid; - m.msg.type = _NXT_PORT_MSG_QUIT; + m.msg.pid = lib->pid; + m.msg.type = _NXT_PORT_MSG_QUIT; m.quit_param = NXT_QUIT_GRACEFUL; - (void) nxt_unit_port_send(ctx, lib->main_ctx.read_port, - &m, sizeof(m), NULL); + (void) nxt_unit_port_send(ctx, lib->main_ctx.read_port, &m, + sizeof(m), NULL); } } @@ -6328,17 +6052,15 @@ nxt_unit_app_queue_recv(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port, return (rbuf->size == -1) ? NXT_UNIT_AGAIN : NXT_UNIT_OK; } - nxt_inline int -nxt_unit_close(int fd) -{ - int res; +nxt_unit_close(int fd) { + int res; res = close(fd); if (nxt_slow_path(res == -1)) { - nxt_unit_alert(NULL, "close(%d) failed: %s (%d)", - fd, strerror(errno), errno); + nxt_unit_alert(NULL, "close(%d) failed: %s (%d)", fd, strerror(errno), + errno); } else { nxt_unit_debug(NULL, "close(%d): %d", fd, res); @@ -6347,17 +6069,15 @@ nxt_unit_close(int fd) return res; } - static int -nxt_unit_fd_blocking(int fd) -{ - int nb; +nxt_unit_fd_blocking(int fd) { + int nb; nb = 0; if (nxt_slow_path(ioctl(fd, FIONBIO, &nb) == -1)) { - nxt_unit_alert(NULL, "ioctl(%d, FIONBIO, 0) failed: %s (%d)", - fd, strerror(errno), errno); + nxt_unit_alert(NULL, "ioctl(%d, FIONBIO, 0) failed: %s (%d)", fd, + strerror(errno), errno); return NXT_UNIT_ERROR; } @@ -6365,42 +6085,34 @@ nxt_unit_fd_blocking(int fd) return NXT_UNIT_OK; } - static nxt_int_t -nxt_unit_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_unit_port_t *port; - nxt_unit_port_hash_id_t *port_id; +nxt_unit_port_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_unit_port_t *port; + nxt_unit_port_hash_id_t *port_id; - port = data; + port = data; port_id = (nxt_unit_port_hash_id_t *) lhq->key.start; if (lhq->key.length == sizeof(nxt_unit_port_hash_id_t) - && port_id->pid == port->id.pid - && port_id->id == port->id.id) - { + && port_id->pid == port->id.pid && port_id->id == port->id.id) { return NXT_OK; } return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_ports_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_unit_port_hash_test, nxt_unit_lvlhsh_alloc, nxt_unit_lvlhsh_free, }; - static inline void nxt_unit_port_hash_lhq(nxt_lvlhsh_query_t *lhq, - nxt_unit_port_hash_id_t *port_hash_id, - nxt_unit_port_id_t *port_id) -{ + nxt_unit_port_hash_id_t *port_hash_id, nxt_unit_port_id_t *port_id) { port_hash_id->pid = port_id->pid; - port_hash_id->id = port_id->id; + port_hash_id->id = port_id->id; if (nxt_fast_path(port_id->hash != 0)) { lhq->key_hash = port_id->hash; @@ -6411,32 +6123,28 @@ nxt_unit_port_hash_lhq(nxt_lvlhsh_query_t *lhq, port_id->hash = lhq->key_hash; nxt_unit_debug(NULL, "calculate hash for port_id (%d, %d): %04X", - (int) port_id->pid, (int) port_id->id, - (int) port_id->hash); + (int) port_id->pid, (int) port_id->id, (int) port_id->hash); } lhq->key.length = sizeof(nxt_unit_port_hash_id_t); - lhq->key.start = (u_char *) port_hash_id; - lhq->proto = &lvlhsh_ports_proto; - lhq->pool = NULL; + lhq->key.start = (u_char *) port_hash_id; + lhq->proto = &lvlhsh_ports_proto; + lhq->pool = NULL; } - static int -nxt_unit_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_unit_port_t *port) -{ - nxt_int_t res; - nxt_lvlhsh_query_t lhq; - nxt_unit_port_hash_id_t port_hash_id; +nxt_unit_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_unit_port_t *port) { + nxt_int_t res; + nxt_lvlhsh_query_t lhq; + nxt_unit_port_hash_id_t port_hash_id; nxt_unit_port_hash_lhq(&lhq, &port_hash_id, &port->id); lhq.replace = 0; - lhq.value = port; + lhq.value = port; res = nxt_lvlhsh_insert(port_hash, &lhq); switch (res) { - case NXT_OK: return NXT_UNIT_OK; @@ -6445,14 +6153,12 @@ nxt_unit_port_hash_add(nxt_lvlhsh_t *port_hash, nxt_unit_port_t *port) } } - static nxt_unit_port_t * nxt_unit_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_unit_port_id_t *port_id, - int remove) -{ - nxt_int_t res; - nxt_lvlhsh_query_t lhq; - nxt_unit_port_hash_id_t port_hash_id; + int remove) { + nxt_int_t res; + nxt_lvlhsh_query_t lhq; + nxt_unit_port_hash_id_t port_hash_id; nxt_unit_port_hash_lhq(&lhq, &port_hash_id, port_id); @@ -6464,7 +6170,6 @@ nxt_unit_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_unit_port_id_t *port_id, } switch (res) { - case NXT_OK: if (!remove) { nxt_unit_port_use(lhq.value); @@ -6477,31 +6182,25 @@ nxt_unit_port_hash_find(nxt_lvlhsh_t *port_hash, nxt_unit_port_id_t *port_id, } } - static nxt_int_t -nxt_unit_request_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_unit_request_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { return NXT_OK; } - -static const nxt_lvlhsh_proto_t lvlhsh_requests_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t lvlhsh_requests_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_unit_request_hash_test, nxt_unit_lvlhsh_alloc, nxt_unit_lvlhsh_free, }; - static int -nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, - nxt_unit_request_info_t *req) -{ - uint32_t *stream; +nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, nxt_unit_request_info_t *req) { + uint32_t *stream; nxt_int_t res; nxt_lvlhsh_query_t lhq; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); if (req_impl->in_hash) { @@ -6510,13 +6209,13 @@ nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, stream = &req_impl->stream; - lhq.key_hash = nxt_murmur_hash2(stream, sizeof(*stream)); + lhq.key_hash = nxt_murmur_hash2(stream, sizeof(*stream)); lhq.key.length = sizeof(*stream); - lhq.key.start = (u_char *) stream; - lhq.proto = &lvlhsh_requests_proto; - lhq.pool = NULL; - lhq.replace = 0; - lhq.value = req_impl; + lhq.key.start = (u_char *) stream; + lhq.proto = &lvlhsh_requests_proto; + lhq.pool = NULL; + lhq.replace = 0; + lhq.value = req_impl; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -6527,7 +6226,6 @@ nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, pthread_mutex_unlock(&ctx_impl->mutex); switch (res) { - case NXT_OK: req_impl->in_hash = 1; return NXT_UNIT_OK; @@ -6537,20 +6235,18 @@ nxt_unit_request_hash_add(nxt_unit_ctx_t *ctx, } } - static nxt_unit_request_info_t * -nxt_unit_request_hash_find(nxt_unit_ctx_t *ctx, uint32_t stream, int remove) -{ +nxt_unit_request_hash_find(nxt_unit_ctx_t *ctx, uint32_t stream, int remove) { nxt_int_t res; nxt_lvlhsh_query_t lhq; - nxt_unit_ctx_impl_t *ctx_impl; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_ctx_impl_t *ctx_impl; + nxt_unit_request_info_impl_t *req_impl; - lhq.key_hash = nxt_murmur_hash2(&stream, sizeof(stream)); + lhq.key_hash = nxt_murmur_hash2(&stream, sizeof(stream)); lhq.key.length = sizeof(stream); - lhq.key.start = (u_char *) &stream; - lhq.proto = &lvlhsh_requests_proto; - lhq.pool = NULL; + lhq.key.start = (u_char *) &stream; + lhq.proto = &lvlhsh_requests_proto; + lhq.pool = NULL; ctx_impl = nxt_container_of(ctx, nxt_unit_ctx_impl_t, ctx); @@ -6566,10 +6262,9 @@ nxt_unit_request_hash_find(nxt_unit_ctx_t *ctx, uint32_t stream, int remove) pthread_mutex_unlock(&ctx_impl->mutex); switch (res) { - case NXT_OK: - req_impl = nxt_container_of(lhq.value, nxt_unit_request_info_impl_t, - req); + req_impl + = nxt_container_of(lhq.value, nxt_unit_request_info_impl_t, req); if (remove) { req_impl->in_hash = 0; } @@ -6581,28 +6276,26 @@ nxt_unit_request_hash_find(nxt_unit_ctx_t *ctx, uint32_t stream, int remove) } } - void -nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char *fmt, ...) -{ +nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char *fmt, ...) { int log_fd, n; char msg[NXT_MAX_ERROR_STR], *p, *end; pid_t pid; va_list ap; - nxt_unit_impl_t *lib; + nxt_unit_impl_t *lib; if (nxt_fast_path(ctx != NULL)) { lib = nxt_container_of(ctx->unit, nxt_unit_impl_t, unit); - pid = lib->pid; + pid = lib->pid; log_fd = lib->log_fd; } else { - pid = nxt_unit_pid; + pid = nxt_unit_pid; log_fd = STDERR_FILENO; } - p = msg; + p = msg; end = p + sizeof(msg) - 1; p = nxt_unit_snprint_prefix(p, end, pid, level); @@ -6624,29 +6317,28 @@ nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char *fmt, ...) } } - void -nxt_unit_req_log(nxt_unit_request_info_t *req, int level, const char *fmt, ...) -{ +nxt_unit_req_log(nxt_unit_request_info_t *req, int level, const char *fmt, + ...) { int log_fd, n; char msg[NXT_MAX_ERROR_STR], *p, *end; pid_t pid; va_list ap; - nxt_unit_impl_t *lib; - nxt_unit_request_info_impl_t *req_impl; + nxt_unit_impl_t *lib; + nxt_unit_request_info_impl_t *req_impl; if (nxt_fast_path(req != NULL)) { lib = nxt_container_of(req->ctx->unit, nxt_unit_impl_t, unit); - pid = lib->pid; + pid = lib->pid; log_fd = lib->log_fd; } else { - pid = nxt_unit_pid; + pid = nxt_unit_pid; log_fd = STDERR_FILENO; } - p = msg; + p = msg; end = p + sizeof(msg) - 1; p = nxt_unit_snprint_prefix(p, end, pid, level); @@ -6654,7 +6346,7 @@ nxt_unit_req_log(nxt_unit_request_info_t *req, int level, const char *fmt, ...) if (nxt_fast_path(req != NULL)) { req_impl = nxt_container_of(req, nxt_unit_request_info_impl_t, req); - p += snprintf(p, end - p, "#%"PRIu32": ", req_impl->stream); + p += snprintf(p, end - p, "#%" PRIu32 ": ", req_impl->stream); } va_start(ap, fmt); @@ -6674,8 +6366,7 @@ nxt_unit_req_log(nxt_unit_request_info_t *req, int level, const char *fmt, ...) } } - -static const char * nxt_unit_log_levels[] = { +static const char *nxt_unit_log_levels[] = { "alert", "error", "warn", @@ -6684,12 +6375,10 @@ static const char * nxt_unit_log_levels[] = { "debug", }; - static char * -nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level) -{ - struct tm tm; - struct timespec ts; +nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level) { + struct tm tm; + struct timespec ts; (void) clock_gettime(CLOCK_REALTIME, &ts); @@ -6700,58 +6389,48 @@ nxt_unit_snprint_prefix(char *p, char *end, pid_t pid, int level) #endif #if (NXT_DEBUG) - p += snprintf(p, end - p, - "%4d/%02d/%02d %02d:%02d:%02d.%03d ", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec, - (int) ts.tv_nsec / 1000000); + p += snprintf(p, end - p, "%4d/%02d/%02d %02d:%02d:%02d.%03d ", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, + tm.tm_sec, (int) ts.tv_nsec / 1000000); #else - p += snprintf(p, end - p, - "%4d/%02d/%02d %02d:%02d:%02d ", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); + p += snprintf(p, end - p, "%4d/%02d/%02d %02d:%02d:%02d ", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, + tm.tm_sec); #endif - p += snprintf(p, end - p, - "[%s] %d#%"PRIu64" [unit] ", nxt_unit_log_levels[level], - (int) pid, - (uint64_t) (uintptr_t) nxt_thread_get_tid()); + p += snprintf(p, end - p, "[%s] %d#%" PRIu64 " [unit] ", + nxt_unit_log_levels[level], (int) pid, + (uint64_t) (uintptr_t) nxt_thread_get_tid()); return p; } - static void * -nxt_unit_lvlhsh_alloc(void *data, size_t size) -{ +nxt_unit_lvlhsh_alloc(void *data, size_t size) { int err; - void *p; + void *p; err = posix_memalign(&p, size, size); if (nxt_fast_path(err == 0)) { - nxt_unit_debug(NULL, "posix_memalign(%d, %d): %p", - (int) size, (int) size, p); + nxt_unit_debug(NULL, "posix_memalign(%d, %d): %p", (int) size, + (int) size, p); return p; } - nxt_unit_alert(NULL, "posix_memalign(%d, %d) failed: %s (%d)", - (int) size, (int) size, strerror(err), err); + nxt_unit_alert(NULL, "posix_memalign(%d, %d) failed: %s (%d)", (int) size, + (int) size, strerror(err), err); return NULL; } - static void -nxt_unit_lvlhsh_free(void *data, void *p) -{ +nxt_unit_lvlhsh_free(void *data, void *p) { nxt_unit_free(NULL, p); } - void * -nxt_unit_malloc(nxt_unit_ctx_t *ctx, size_t size) -{ - void *p; +nxt_unit_malloc(nxt_unit_ctx_t *ctx, size_t size) { + void *p; p = malloc(size); @@ -6761,17 +6440,15 @@ nxt_unit_malloc(nxt_unit_ctx_t *ctx, size_t size) #endif } else { - nxt_unit_alert(ctx, "malloc(%d) failed: %s (%d)", - (int) size, strerror(errno), errno); + nxt_unit_alert(ctx, "malloc(%d) failed: %s (%d)", (int) size, + strerror(errno), errno); } return p; } - void -nxt_unit_free(nxt_unit_ctx_t *ctx, void *p) -{ +nxt_unit_free(nxt_unit_ctx_t *ctx, void *p) { #if (NXT_DEBUG_ALLOC) nxt_unit_debug(ctx, "free(%p)", p); #endif @@ -6779,13 +6456,11 @@ nxt_unit_free(nxt_unit_ctx_t *ctx, void *p) free(p); } - static int -nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length) -{ +nxt_unit_memcasecmp(const void *p1, const void *p2, size_t length) { u_char c1, c2; nxt_int_t n; - const u_char *s1, *s2; + const u_char *s1, *s2; s1 = p1; s2 = p2; diff --git a/src/nxt_unit.h b/src/nxt_unit.h index a50046a52..42bad08df 100644 --- a/src/nxt_unit.h +++ b/src/nxt_unit.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -16,26 +15,25 @@ #include "nxt_version.h" #include "nxt_unit_typedefs.h" - enum { - NXT_UNIT_OK = 0, - NXT_UNIT_ERROR = 1, - NXT_UNIT_AGAIN = 2, - NXT_UNIT_CANCELLED = 3, + NXT_UNIT_OK = 0, + NXT_UNIT_ERROR = 1, + NXT_UNIT_AGAIN = 2, + NXT_UNIT_CANCELLED = 3, }; enum { - NXT_UNIT_LOG_ALERT = 0, - NXT_UNIT_LOG_ERR = 1, - NXT_UNIT_LOG_WARN = 2, - NXT_UNIT_LOG_NOTICE = 3, - NXT_UNIT_LOG_INFO = 4, - NXT_UNIT_LOG_DEBUG = 5, + NXT_UNIT_LOG_ALERT = 0, + NXT_UNIT_LOG_ERR = 1, + NXT_UNIT_LOG_WARN = 2, + NXT_UNIT_LOG_NOTICE = 3, + NXT_UNIT_LOG_INFO = 4, + NXT_UNIT_LOG_DEBUG = 5, }; -#define NXT_UNIT_INIT_ENV "NXT_UNIT_INIT" +#define NXT_UNIT_INIT_ENV "NXT_UNIT_INIT" -#define NXT_UNIT_SHARED_PORT_ID ((uint16_t) 0xFFFFu) +#define NXT_UNIT_SHARED_PORT_ID ((uint16_t) 0xFFFFu) /* * Mostly opaque structure with library state. @@ -44,7 +42,7 @@ enum { * implementation specific and hidden. */ struct nxt_unit_s { - void *data; /* User defined data. */ + void *data; /* User defined data. */ }; /* @@ -55,8 +53,8 @@ struct nxt_unit_s { * further in that thread. */ struct nxt_unit_ctx_s { - void *data; /* User context-specific data. */ - nxt_unit_t *unit; + void *data; /* User context-specific data. */ + nxt_unit_t *unit; }; /* @@ -66,9 +64,9 @@ struct nxt_unit_ctx_s { * This identification is required to refer the port from different process. */ struct nxt_unit_port_id_s { - pid_t pid; - uint32_t hash; - uint16_t id; + pid_t pid; + uint32_t hash; + uint16_t id; }; /* @@ -76,43 +74,40 @@ struct nxt_unit_port_id_s { * data structures. */ struct nxt_unit_port_s { - nxt_unit_port_id_t id; + nxt_unit_port_id_t id; - int in_fd; - int out_fd; + int in_fd; + int out_fd; - void *data; + void *data; }; - struct nxt_unit_buf_s { - char *start; - char *free; - char *end; + char *start; + char *free; + char *end; }; - struct nxt_unit_request_info_s { - nxt_unit_t *unit; - nxt_unit_ctx_t *ctx; + nxt_unit_t *unit; + nxt_unit_ctx_t *ctx; - nxt_unit_port_t *response_port; + nxt_unit_port_t *response_port; - nxt_unit_request_t *request; - nxt_unit_buf_t *request_buf; + nxt_unit_request_t *request; + nxt_unit_buf_t *request_buf; - nxt_unit_response_t *response; - nxt_unit_buf_t *response_buf; - uint32_t response_max_fields; + nxt_unit_response_t *response; + nxt_unit_buf_t *response_buf; + uint32_t response_max_fields; - nxt_unit_buf_t *content_buf; - uint64_t content_length; - int content_fd; + nxt_unit_buf_t *content_buf; + uint64_t content_length; + int content_fd; - void *data; + void *data; }; - /* * Set of application-specific callbacks. The application may leave all * optional callbacks as NULL. @@ -122,83 +117,78 @@ struct nxt_unit_callbacks_s { * Process request. Unlike all other callbacks, this callback is required * and needs to be defined by the application. */ - void (*request_handler)(nxt_unit_request_info_t *req); + void (*request_handler)(nxt_unit_request_info_t *req); - void (*data_handler)(nxt_unit_request_info_t *req); + void (*data_handler)(nxt_unit_request_info_t *req); /* Process websocket frame. */ - void (*websocket_handler)(nxt_unit_websocket_frame_t *ws); + void (*websocket_handler)(nxt_unit_websocket_frame_t *ws); /* Connection closed. */ - void (*close_handler)(nxt_unit_request_info_t *req); + void (*close_handler)(nxt_unit_request_info_t *req); /* Add new Unit port to communicate with process pid. Optional. */ - int (*add_port)(nxt_unit_ctx_t *, nxt_unit_port_t *port); + int (*add_port)(nxt_unit_ctx_t *, nxt_unit_port_t *port); /* Remove previously added port. Optional. */ - void (*remove_port)(nxt_unit_t *, nxt_unit_ctx_t *, - nxt_unit_port_t *port); + void (*remove_port)(nxt_unit_t *, nxt_unit_ctx_t *, nxt_unit_port_t *port); /* Remove all data associated with process pid including ports. Optional. */ - void (*remove_pid)(nxt_unit_t *, pid_t pid); + void (*remove_pid)(nxt_unit_t *, pid_t pid); /* Gracefully quit the application. Optional. */ - void (*quit)(nxt_unit_ctx_t *); + void (*quit)(nxt_unit_ctx_t *); /* Shared memory release acknowledgement. Optional. */ - void (*shm_ack_handler)(nxt_unit_ctx_t *); + void (*shm_ack_handler)(nxt_unit_ctx_t *); /* Send data and control to process pid using port id. Optional. */ - ssize_t (*port_send)(nxt_unit_ctx_t *, nxt_unit_port_t *port, - const void *buf, size_t buf_size, - const void *oob, size_t oob_size); + ssize_t (*port_send)(nxt_unit_ctx_t *, nxt_unit_port_t *port, + const void *buf, size_t buf_size, const void *oob, size_t oob_size); /* Receive data on port id. Optional. */ - ssize_t (*port_recv)(nxt_unit_ctx_t *, nxt_unit_port_t *port, - void *buf, size_t buf_size, void *oob, size_t *oob_size); + ssize_t (*port_recv)(nxt_unit_ctx_t *, nxt_unit_port_t *port, void *buf, + size_t buf_size, void *oob, size_t *oob_size); - int (*ready_handler)(nxt_unit_ctx_t *); + int (*ready_handler)(nxt_unit_ctx_t *); }; - struct nxt_unit_init_s { - void *data; /* Opaque pointer to user-defined data. */ - void *ctx_data; /* Opaque pointer to user-defined data. */ - int max_pending_requests; - - uint32_t request_data_size; - uint32_t shm_limit; - uint32_t request_limit; - - nxt_unit_callbacks_t callbacks; - - nxt_unit_port_t ready_port; - uint32_t ready_stream; - nxt_unit_port_t router_port; - nxt_unit_port_t read_port; - int shared_port_fd; - int shared_queue_fd; - int log_fd; + void *data; /* Opaque pointer to user-defined data. */ + void *ctx_data; /* Opaque pointer to user-defined data. */ + int max_pending_requests; + + uint32_t request_data_size; + uint32_t shm_limit; + uint32_t request_limit; + + nxt_unit_callbacks_t callbacks; + + nxt_unit_port_t ready_port; + uint32_t ready_stream; + nxt_unit_port_t router_port; + nxt_unit_port_t read_port; + int shared_port_fd; + int shared_queue_fd; + int log_fd; }; - typedef ssize_t (*nxt_unit_read_func_t)(nxt_unit_read_info_t *read_info, void *dst, size_t size); - struct nxt_unit_read_info_s { - nxt_unit_read_func_t read; - int eof; - uint32_t buf_size; - void *data; + nxt_unit_read_func_t read; + int eof; + uint32_t buf_size; + void *data; }; - /* * Initialize Unit application library with necessary callbacks and * ready/reply port parameters, send 'READY' response to main. */ -nxt_unit_ctx_t *nxt_unit_init(nxt_unit_init_t *); +nxt_unit_ctx_t * +nxt_unit_init(nxt_unit_init_t *); /* * Main function, useful in case the application does not have its own event @@ -211,13 +201,17 @@ nxt_unit_ctx_t *nxt_unit_init(nxt_unit_init_t *); * * The function returns normally when a QUIT message is received from Unit. */ -int nxt_unit_run(nxt_unit_ctx_t *); +int +nxt_unit_run(nxt_unit_ctx_t *); -int nxt_unit_run_ctx(nxt_unit_ctx_t *ctx); +int +nxt_unit_run_ctx(nxt_unit_ctx_t *ctx); -int nxt_unit_run_shared(nxt_unit_ctx_t *ctx); +int +nxt_unit_run_shared(nxt_unit_ctx_t *ctx); -nxt_unit_request_info_t *nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx); +nxt_unit_request_info_t * +nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx); /* * Receive and process one message, and invoke configured callbacks. @@ -226,122 +220,155 @@ nxt_unit_request_info_t *nxt_unit_dequeue_request(nxt_unit_ctx_t *ctx); * from the port socket should be initially processed by unit. This function * may invoke other application-defined callback for message processing. */ -int nxt_unit_run_once(nxt_unit_ctx_t *ctx); +int +nxt_unit_run_once(nxt_unit_ctx_t *ctx); -int nxt_unit_process_port_msg(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +int +nxt_unit_process_port_msg(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); /* Destroy application library object. */ -void nxt_unit_done(nxt_unit_ctx_t *); +void +nxt_unit_done(nxt_unit_ctx_t *); /* * Allocate and initialize a new execution context with a new listen port to * process requests in another thread. */ -nxt_unit_ctx_t *nxt_unit_ctx_alloc(nxt_unit_ctx_t *, void *); +nxt_unit_ctx_t * +nxt_unit_ctx_alloc(nxt_unit_ctx_t *, void *); /* Initialize port_id, calculate hash. */ -void nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id); +void +nxt_unit_port_id_init(nxt_unit_port_id_t *port_id, pid_t pid, uint16_t id); /* Calculates hash for given field name. */ -uint16_t nxt_unit_field_hash(const char* name, size_t name_length); +uint16_t +nxt_unit_field_hash(const char *name, size_t name_length); /* Split host for server name and port. */ -void nxt_unit_split_host(char *host_start, uint32_t host_length, - char **name, uint32_t *name_length, char **port, uint32_t *port_length); +void +nxt_unit_split_host(char *host_start, uint32_t host_length, char **name, + uint32_t *name_length, char **port, uint32_t *port_length); /* Group duplicate fields for easy enumeration. */ -void nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req); +void +nxt_unit_request_group_dup_fields(nxt_unit_request_info_t *req); /* * Allocate response structure capable of storing a limited number of fields. * The structure may be accessed directly via req->response pointer or * filled step-by-step using functions add_field and add_content. */ -int nxt_unit_response_init(nxt_unit_request_info_t *req, - uint16_t status, uint32_t max_fields_count, uint32_t max_fields_size); +int +nxt_unit_response_init(nxt_unit_request_info_t *req, uint16_t status, + uint32_t max_fields_count, uint32_t max_fields_size); -int nxt_unit_response_realloc(nxt_unit_request_info_t *req, +int +nxt_unit_response_realloc(nxt_unit_request_info_t *req, uint32_t max_fields_count, uint32_t max_fields_size); -int nxt_unit_response_is_init(nxt_unit_request_info_t *req); +int +nxt_unit_response_is_init(nxt_unit_request_info_t *req); -int nxt_unit_response_add_field(nxt_unit_request_info_t *req, - const char* name, uint8_t name_length, - const char* value, uint32_t value_length); +int +nxt_unit_response_add_field(nxt_unit_request_info_t *req, const char *name, + uint8_t name_length, const char *value, uint32_t value_length); -int nxt_unit_response_add_content(nxt_unit_request_info_t *req, - const void* src, uint32_t size); +int +nxt_unit_response_add_content(nxt_unit_request_info_t *req, const void *src, + uint32_t size); /* * Send the prepared response to the Unit server. The Response structure is * destroyed during this call. */ -int nxt_unit_response_send(nxt_unit_request_info_t *req); +int +nxt_unit_response_send(nxt_unit_request_info_t *req); -int nxt_unit_response_is_sent(nxt_unit_request_info_t *req); +int +nxt_unit_response_is_sent(nxt_unit_request_info_t *req); -nxt_unit_buf_t *nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, - uint32_t size); +nxt_unit_buf_t * +nxt_unit_response_buf_alloc(nxt_unit_request_info_t *req, uint32_t size); -int nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req); +int +nxt_unit_request_is_websocket_handshake(nxt_unit_request_info_t *req); -int nxt_unit_response_upgrade(nxt_unit_request_info_t *req); +int +nxt_unit_response_upgrade(nxt_unit_request_info_t *req); -int nxt_unit_response_is_websocket(nxt_unit_request_info_t *req); +int +nxt_unit_response_is_websocket(nxt_unit_request_info_t *req); -nxt_unit_request_info_t *nxt_unit_get_request_info_from_data(void *data); +nxt_unit_request_info_t * +nxt_unit_get_request_info_from_data(void *data); -int nxt_unit_buf_send(nxt_unit_buf_t *buf); +int +nxt_unit_buf_send(nxt_unit_buf_t *buf); -void nxt_unit_buf_free(nxt_unit_buf_t *buf); +void +nxt_unit_buf_free(nxt_unit_buf_t *buf); -nxt_unit_buf_t *nxt_unit_buf_next(nxt_unit_buf_t *buf); +nxt_unit_buf_t * +nxt_unit_buf_next(nxt_unit_buf_t *buf); -uint32_t nxt_unit_buf_max(void); +uint32_t +nxt_unit_buf_max(void); -uint32_t nxt_unit_buf_min(void); +uint32_t +nxt_unit_buf_min(void); -int nxt_unit_response_write(nxt_unit_request_info_t *req, const void *start, +int +nxt_unit_response_write(nxt_unit_request_info_t *req, const void *start, size_t size); -ssize_t nxt_unit_response_write_nb(nxt_unit_request_info_t *req, - const void *start, size_t size, size_t min_size); +ssize_t +nxt_unit_response_write_nb(nxt_unit_request_info_t *req, const void *start, + size_t size, size_t min_size); -int nxt_unit_response_write_cb(nxt_unit_request_info_t *req, - nxt_unit_read_info_t *read_info); +int +nxt_unit_response_write_cb(nxt_unit_request_info_t *req, + nxt_unit_read_info_t *read_info); -ssize_t nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, - size_t size); +ssize_t +nxt_unit_request_read(nxt_unit_request_info_t *req, void *dst, size_t size); -ssize_t nxt_unit_request_readline_size(nxt_unit_request_info_t *req, - size_t max_size); +ssize_t +nxt_unit_request_readline_size(nxt_unit_request_info_t *req, size_t max_size); -void nxt_unit_request_done(nxt_unit_request_info_t *req, int rc); +void +nxt_unit_request_done(nxt_unit_request_info_t *req, int rc); -int nxt_unit_websocket_send(nxt_unit_request_info_t *req, uint8_t opcode, +int +nxt_unit_websocket_send(nxt_unit_request_info_t *req, uint8_t opcode, uint8_t last, const void *start, size_t size); -int nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, +int +nxt_unit_websocket_sendv(nxt_unit_request_info_t *req, uint8_t opcode, uint8_t last, const struct iovec *iov, int iovcnt); -ssize_t nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst, - size_t size); +ssize_t +nxt_unit_websocket_read(nxt_unit_websocket_frame_t *ws, void *dst, size_t size); -int nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws); +int +nxt_unit_websocket_retain(nxt_unit_websocket_frame_t *ws); -void nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws); +void +nxt_unit_websocket_done(nxt_unit_websocket_frame_t *ws); -void *nxt_unit_malloc(nxt_unit_ctx_t *ctx, size_t size); +void * +nxt_unit_malloc(nxt_unit_ctx_t *ctx, size_t size); -void nxt_unit_free(nxt_unit_ctx_t *ctx, void *p); +void +nxt_unit_free(nxt_unit_ctx_t *ctx, void *p); #if defined __has_attribute #if __has_attribute(format) -#define NXT_ATTR_FORMAT __attribute__((format(printf, 3, 4))) +#define NXT_ATTR_FORMAT __attribute__((format(printf, 3, 4))) #endif @@ -355,18 +382,20 @@ void nxt_unit_free(nxt_unit_ctx_t *ctx, void *p); #endif -void nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char* fmt, ...) - NXT_ATTR_FORMAT; +void +nxt_unit_log(nxt_unit_ctx_t *ctx, int level, const char *fmt, + ...) NXT_ATTR_FORMAT; -void nxt_unit_req_log(nxt_unit_request_info_t *req, int level, - const char* fmt, ...) NXT_ATTR_FORMAT; +void +nxt_unit_req_log(nxt_unit_request_info_t *req, int level, const char *fmt, + ...) NXT_ATTR_FORMAT; #if (NXT_DEBUG) -#define nxt_unit_debug(ctx, fmt, ARGS...) \ +#define nxt_unit_debug(ctx, fmt, ARGS...) \ nxt_unit_log(ctx, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS) -#define nxt_unit_req_debug(req, fmt, ARGS...) \ +#define nxt_unit_req_debug(req, fmt, ARGS...) \ nxt_unit_req_log(req, NXT_UNIT_LOG_DEBUG, fmt, ##ARGS) #else @@ -378,22 +407,22 @@ void nxt_unit_req_log(nxt_unit_request_info_t *req, int level, #endif -#define nxt_unit_warn(ctx, fmt, ARGS...) \ +#define nxt_unit_warn(ctx, fmt, ARGS...) \ nxt_unit_log(ctx, NXT_UNIT_LOG_WARN, fmt, ##ARGS) -#define nxt_unit_req_warn(req, fmt, ARGS...) \ +#define nxt_unit_req_warn(req, fmt, ARGS...) \ nxt_unit_req_log(req, NXT_UNIT_LOG_WARN, fmt, ##ARGS) -#define nxt_unit_error(ctx, fmt, ARGS...) \ +#define nxt_unit_error(ctx, fmt, ARGS...) \ nxt_unit_log(ctx, NXT_UNIT_LOG_ERR, fmt, ##ARGS) -#define nxt_unit_req_error(req, fmt, ARGS...) \ +#define nxt_unit_req_error(req, fmt, ARGS...) \ nxt_unit_req_log(req, NXT_UNIT_LOG_ERR, fmt, ##ARGS) -#define nxt_unit_alert(ctx, fmt, ARGS...) \ +#define nxt_unit_alert(ctx, fmt, ARGS...) \ nxt_unit_log(ctx, NXT_UNIT_LOG_ALERT, fmt, ##ARGS) -#define nxt_unit_req_alert(req, fmt, ARGS...) \ +#define nxt_unit_req_alert(req, fmt, ARGS...) \ nxt_unit_req_log(req, NXT_UNIT_LOG_ALERT, fmt, ##ARGS) diff --git a/src/nxt_unit_field.h b/src/nxt_unit_field.h index b07d3046b..c4ad78a0c 100644 --- a/src/nxt_unit_field.h +++ b/src/nxt_unit_field.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -17,17 +16,16 @@ enum { NXT_UNIT_HASH_COOKIE = 0x23F2, }; - /* Name and Value field aka HTTP header. */ struct nxt_unit_field_s { - uint16_t hash; - uint8_t skip:1; - uint8_t hopbyhop:1; - uint8_t name_length; - uint32_t value_length; - - nxt_unit_sptr_t name; - nxt_unit_sptr_t value; + uint16_t hash; + uint8_t skip :1; + uint8_t hopbyhop:1; + uint8_t name_length; + uint32_t value_length; + + nxt_unit_sptr_t name; + nxt_unit_sptr_t value; }; diff --git a/src/nxt_unit_request.h b/src/nxt_unit_request.h index a6ebf0b63..4ad50e2ec 100644 --- a/src/nxt_unit_request.h +++ b/src/nxt_unit_request.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,44 +11,43 @@ #include "nxt_unit_sptr.h" #include "nxt_unit_field.h" -#define NXT_UNIT_NONE_FIELD 0xFFFFFFFFU +#define NXT_UNIT_NONE_FIELD 0xFFFFFFFFU struct nxt_unit_request_s { - uint8_t method_length; - uint8_t version_length; - uint8_t remote_length; - uint8_t local_addr_length; - uint8_t local_port_length; - uint8_t tls; - uint8_t websocket_handshake; - uint8_t app_target; - uint32_t server_name_length; - uint32_t target_length; - uint32_t path_length; - uint32_t query_length; - uint32_t fields_count; - - uint32_t content_length_field; - uint32_t content_type_field; - uint32_t cookie_field; - uint32_t authorization_field; - - uint64_t content_length; - - nxt_unit_sptr_t method; - nxt_unit_sptr_t version; - nxt_unit_sptr_t remote; - nxt_unit_sptr_t local_addr; - nxt_unit_sptr_t local_port; - nxt_unit_sptr_t server_name; - nxt_unit_sptr_t target; - nxt_unit_sptr_t path; - nxt_unit_sptr_t query; - nxt_unit_sptr_t preread_content; - - nxt_unit_field_t fields[]; + uint8_t method_length; + uint8_t version_length; + uint8_t remote_length; + uint8_t local_addr_length; + uint8_t local_port_length; + uint8_t tls; + uint8_t websocket_handshake; + uint8_t app_target; + uint32_t server_name_length; + uint32_t target_length; + uint32_t path_length; + uint32_t query_length; + uint32_t fields_count; + + uint32_t content_length_field; + uint32_t content_type_field; + uint32_t cookie_field; + uint32_t authorization_field; + + uint64_t content_length; + + nxt_unit_sptr_t method; + nxt_unit_sptr_t version; + nxt_unit_sptr_t remote; + nxt_unit_sptr_t local_addr; + nxt_unit_sptr_t local_port; + nxt_unit_sptr_t server_name; + nxt_unit_sptr_t target; + nxt_unit_sptr_t path; + nxt_unit_sptr_t query; + nxt_unit_sptr_t preread_content; + + nxt_unit_field_t fields[]; }; #endif /* _NXT_UNIT_REQUEST_H_INCLUDED_ */ - diff --git a/src/nxt_unit_response.h b/src/nxt_unit_response.h index 481ed701c..db8f150f9 100644 --- a/src/nxt_unit_response.h +++ b/src/nxt_unit_response.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -13,14 +12,14 @@ #include "nxt_unit_field.h" struct nxt_unit_response_s { - uint64_t content_length; - uint32_t fields_count; - uint32_t piggyback_content_length; - uint16_t status; + uint64_t content_length; + uint32_t fields_count; + uint32_t piggyback_content_length; + uint16_t status; - nxt_unit_sptr_t piggyback_content; + nxt_unit_sptr_t piggyback_content; - nxt_unit_field_t fields[]; + nxt_unit_field_t fields[]; }; diff --git a/src/nxt_unit_sptr.h b/src/nxt_unit_sptr.h index 314416e4e..04d09cdff 100644 --- a/src/nxt_unit_sptr.h +++ b/src/nxt_unit_sptr.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -13,24 +12,19 @@ #include "nxt_unit_typedefs.h" - /* Serialized pointer. */ union nxt_unit_sptr_u { - uint8_t base[1]; - uint32_t offset; + uint8_t base[1]; + uint32_t offset; }; - static inline void -nxt_unit_sptr_set(nxt_unit_sptr_t *sptr, void *ptr) -{ +nxt_unit_sptr_set(nxt_unit_sptr_t *sptr, void *ptr) { sptr->offset = (uint8_t *) ptr - sptr->base; } - static inline void * -nxt_unit_sptr_get(nxt_unit_sptr_t *sptr) -{ +nxt_unit_sptr_get(nxt_unit_sptr_t *sptr) { return sptr->base + sptr->offset; } diff --git a/src/nxt_unit_typedefs.h b/src/nxt_unit_typedefs.h index 26e54f913..948f8fa7f 100644 --- a/src/nxt_unit_typedefs.h +++ b/src/nxt_unit_typedefs.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,20 +6,20 @@ #define _NXT_UNIT_TYPEDEFS_H_INCLUDED_ -typedef struct nxt_unit_s nxt_unit_t; -typedef struct nxt_unit_ctx_s nxt_unit_ctx_t; -typedef struct nxt_unit_port_id_s nxt_unit_port_id_t; -typedef struct nxt_unit_port_s nxt_unit_port_t; -typedef struct nxt_unit_buf_s nxt_unit_buf_t; -typedef struct nxt_unit_request_info_s nxt_unit_request_info_t; -typedef struct nxt_unit_callbacks_s nxt_unit_callbacks_t; -typedef struct nxt_unit_init_s nxt_unit_init_t; -typedef union nxt_unit_sptr_u nxt_unit_sptr_t; -typedef struct nxt_unit_field_s nxt_unit_field_t; -typedef struct nxt_unit_request_s nxt_unit_request_t; -typedef struct nxt_unit_response_s nxt_unit_response_t; -typedef struct nxt_unit_read_info_s nxt_unit_read_info_t; -typedef struct nxt_unit_websocket_frame_s nxt_unit_websocket_frame_t; +typedef struct nxt_unit_s nxt_unit_t; +typedef struct nxt_unit_ctx_s nxt_unit_ctx_t; +typedef struct nxt_unit_port_id_s nxt_unit_port_id_t; +typedef struct nxt_unit_port_s nxt_unit_port_t; +typedef struct nxt_unit_buf_s nxt_unit_buf_t; +typedef struct nxt_unit_request_info_s nxt_unit_request_info_t; +typedef struct nxt_unit_callbacks_s nxt_unit_callbacks_t; +typedef struct nxt_unit_init_s nxt_unit_init_t; +typedef union nxt_unit_sptr_u nxt_unit_sptr_t; +typedef struct nxt_unit_field_s nxt_unit_field_t; +typedef struct nxt_unit_request_s nxt_unit_request_t; +typedef struct nxt_unit_response_s nxt_unit_response_t; +typedef struct nxt_unit_read_info_s nxt_unit_read_info_t; +typedef struct nxt_unit_websocket_frame_s nxt_unit_websocket_frame_t; #endif /* _NXT_UNIT_TYPEDEFS_H_INCLUDED_ */ diff --git a/src/nxt_unit_websocket.h b/src/nxt_unit_websocket.h index beb2536ef..205f249ee 100644 --- a/src/nxt_unit_websocket.h +++ b/src/nxt_unit_websocket.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,16 +10,15 @@ #include "nxt_unit_typedefs.h" #include "nxt_websocket_header.h" - struct nxt_unit_websocket_frame_s { - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - uint64_t payload_len; - nxt_websocket_header_t *header; - uint8_t *mask; + uint64_t payload_len; + nxt_websocket_header_t *header; + uint8_t *mask; - nxt_unit_buf_t *content_buf; - uint64_t content_length; + nxt_unit_buf_t *content_buf; + uint64_t content_length; }; diff --git a/src/nxt_unix.h b/src/nxt_unix.h index 6bc6be5ee..8284f9a19 100644 --- a/src/nxt_unix.h +++ b/src/nxt_unix.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -24,20 +23,20 @@ #endif #ifndef _GNU_SOURCE -#define _GNU_SOURCE /* pread(), pwrite(), gethostname(). */ +#define _GNU_SOURCE /* pread(), pwrite(), gethostname(). */ #endif -#define _FILE_OFFSET_BITS 64 +#define _FILE_OFFSET_BITS 64 -#include /* malloc_usable_size(). */ -#include /* syscall(SYS_gettid). */ +#include /* malloc_usable_size(). */ +#include /* syscall(SYS_gettid). */ #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4) /* * POSIX semaphores using NPTL atomic/futex operations * were introduced during glibc 2.3 development time. */ -#define NXT_HAVE_SEM_TRYWAIT_FAST 1 +#define NXT_HAVE_SEM_TRYWAIT_FAST 1 #endif #endif /* NXT_LINUX */ @@ -46,12 +45,12 @@ #if (NXT_FREEBSD) #if (NXT_HAVE_MALLOC_USABLE_SIZE) -#include /* malloc_usable_size(). */ +#include /* malloc_usable_size(). */ #endif #if (__FreeBSD_version >= 900007) /* POSIX semaphores using atomic/umtx. */ -#define NXT_HAVE_SEM_TRYWAIT_FAST 1 +#define NXT_HAVE_SEM_TRYWAIT_FAST 1 #endif #endif /* NXT_FREEBSD */ @@ -60,15 +59,15 @@ #if (NXT_SOLARIS) #ifndef _FILE_OFFSET_BITS -#define _FILE_OFFSET_BITS 64 /* Must be before . */ +#define _FILE_OFFSET_BITS 64 /* Must be before . */ #endif -#ifndef _REENTRANT /* May be set by "-mt" options. */ -#define _REENTRANT /* Thread safe errno. */ +#ifndef _REENTRANT /* May be set by "-mt" options. */ +#define _REENTRANT /* Thread safe errno. */ #endif #ifndef _POSIX_PTHREAD_SEMANTICS -#define _POSIX_PTHREAD_SEMANTICS /* 2 arguments in sigwait(). */ +#define _POSIX_PTHREAD_SEMANTICS /* 2 arguments in sigwait(). */ #endif /* @@ -82,7 +81,7 @@ /* Enable X/Open sockets API. */ #define _XOPEN_SOURCE -#define _XOPEN_SOURCE_EXTENDED 1 +#define _XOPEN_SOURCE_EXTENDED 1 /* Enable Solaris extensions disabled by _XOPEN_SOURCE. */ #ifndef __EXTENSIONS__ #define __EXTENSIONS__ @@ -94,25 +93,25 @@ #if (NXT_MACOSX) #ifndef _DARWIN_C_SOURCE -#define _DARWIN_C_SOURCE /* pthread_threadid_np(), mach_port_t. */ +#define _DARWIN_C_SOURCE /* pthread_threadid_np(), mach_port_t. */ #endif -#include /* mach_absolute_time(). */ -#include /* malloc_size(). */ +#include /* mach_absolute_time(). */ +#include /* malloc_size(). */ -#endif /* NXT_MACOSX */ +#endif /* NXT_MACOSX */ #if (NXT_AIX) -#define _THREAD_SAFE /* Must before any include. */ +#define _THREAD_SAFE /* Must before any include. */ -#endif /* NXT_AIX */ +#endif /* NXT_AIX */ #if (NXT_HPUX) -#define _FILE_OFFSET_BITS 64 +#define _FILE_OFFSET_BITS 64 /* * HP-UX provides three sockets API: @@ -158,18 +157,18 @@ #include #include #include -#include /* offsetof() */ +#include /* offsetof() */ #include #include #include #include #include #if (NXT_HAVE_SYS_FILIO_H) -#include /* FIONBIO */ +#include /* FIONBIO */ #endif #include #include -#include /* MAXPATHLEN */ +#include /* MAXPATHLEN */ #include #include #include @@ -191,7 +190,7 @@ /* * Epoll edge-tiggered mode is pretty much useless without EPOLLRDHUP support. */ -#define NXT_HAVE_EPOLL_EDGE 1 +#define NXT_HAVE_EPOLL_EDGE 1 #endif #endif @@ -229,11 +228,11 @@ #endif #if (NXT_HAVE_GETRANDOM) -#include /* getrandom(). */ +#include /* getrandom(). */ #elif (NXT_HAVE_LINUX_SYS_GETRANDOM) -#include /* SYS_getrandom. */ +#include /* SYS_getrandom. */ #elif (NXT_HAVE_GETENTROPY_SYS_RANDOM) -#include /* getentropy(). */ +#include /* getentropy(). */ #endif #if (NXT_HAVE_ISOLATION_ROOTFS) @@ -268,25 +267,22 @@ * * On AIX and HP-UX IOV_MAX is 16. */ -#define NXT_IOBUF_MAX 8 +#define NXT_IOBUF_MAX 8 -typedef struct iovec nxt_iobuf_t; +typedef struct iovec nxt_iobuf_t; -#define nxt_iobuf_data(iob) \ - (iob)->iov_base +#define nxt_iobuf_data(iob) (iob)->iov_base -#define nxt_iobuf_size(iob) \ - (iob)->iov_len +#define nxt_iobuf_size(iob) (iob)->iov_len -#define nxt_iobuf_set(iob, p, size) \ - do { \ - (iob)->iov_base = (void *) p; \ - (iob)->iov_len = size; \ +#define nxt_iobuf_set(iob, p, size) \ + do { \ + (iob)->iov_base = (void *) p; \ + (iob)->iov_len = size; \ } while (0) -#define nxt_iobuf_add(iob, size) \ - (iob)->iov_len += size +#define nxt_iobuf_add(iob, size) (iob)->iov_len += size #endif /* _NXT_UNIX_H_INCLUDED_ */ diff --git a/src/nxt_upstream.c b/src/nxt_upstream.c index c92d4a50b..8b4bbd005 100644 --- a/src/nxt_upstream.c +++ b/src/nxt_upstream.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,23 +8,22 @@ #include -static nxt_http_action_t *nxt_upstream_handler(nxt_task_t *task, - nxt_http_request_t *r, nxt_http_action_t *action); - +static nxt_http_action_t * +nxt_upstream_handler(nxt_task_t *task, nxt_http_request_t *r, + nxt_http_action_t *action); nxt_int_t nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *conf) -{ + nxt_conf_value_t *conf) { size_t size; uint32_t i, n, next; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_int_t ret; nxt_str_t name, *string; - nxt_upstreams_t *upstreams; - nxt_conf_value_t *upstreams_conf, *upcf; + nxt_upstreams_t *upstreams; + nxt_conf_value_t *upstreams_conf, *upcf; - static const nxt_str_t upstreams_name = nxt_string("upstreams"); + static const nxt_str_t upstreams_name = nxt_string("upstreams"); upstreams_conf = nxt_conf_get_object_member(conf, &upstreams_name, NULL); @@ -39,7 +37,7 @@ nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_OK; } - mp = tmcf->router_conf->mem_pool; + mp = tmcf->router_conf->mem_pool; size = sizeof(nxt_upstreams_t) + n * sizeof(nxt_upstream_t); upstreams = nxt_mp_zalloc(mp, size); @@ -48,7 +46,7 @@ nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } upstreams->items = n; - next = 0; + next = 0; for (i = 0; i < n; i++) { upcf = nxt_conf_next_object_member(upstreams_conf, &name, &next); @@ -59,7 +57,7 @@ nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } ret = nxt_upstream_round_robin_create(task, tmcf, upcf, - &upstreams->upstream[i]); + &upstreams->upstream[i]); if (nxt_slow_path(ret != NXT_OK)) { return NXT_ERROR; } @@ -70,25 +68,23 @@ nxt_upstreams_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, return NXT_OK; } - nxt_int_t nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name, - nxt_http_action_t *action) -{ + nxt_http_action_t *action) { uint32_t i, n; - nxt_upstream_t *upstream; + nxt_upstream_t *upstream; if (upstreams == NULL) { return NXT_DECLINED; } upstream = &upstreams->upstream[0]; - n = upstreams->items; + n = upstreams->items; for (i = 0; i < n; i++) { if (nxt_strstr_eq(&upstream[i].name, name)) { action->u.upstream_number = i; - action->handler = nxt_upstream_handler; + action->handler = nxt_upstream_handler; return NXT_OK; } @@ -97,18 +93,16 @@ nxt_upstream_find(nxt_upstreams_t *upstreams, nxt_str_t *name, return NXT_DECLINED; } - nxt_int_t nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf, - nxt_upstream_t ***upstream_joint) -{ + nxt_upstream_t ***upstream_joint) { uint32_t i, n; - nxt_upstream_t *u, **up; - nxt_upstreams_t *upstreams; - nxt_router_conf_t *router_conf; + nxt_upstream_t *u, **up; + nxt_upstreams_t *upstreams; + nxt_router_conf_t *router_conf; router_conf = tmcf->router_conf; - upstreams = router_conf->upstreams; + upstreams = router_conf->upstreams; if (upstreams == NULL) { *upstream_joint = NULL; @@ -136,12 +130,10 @@ nxt_upstreams_joint_create(nxt_router_temp_conf_t *tmcf, return NXT_OK; } - static nxt_http_action_t * nxt_upstream_handler(nxt_task_t *task, nxt_http_request_t *r, - nxt_http_action_t *action) -{ - nxt_upstream_t *u; + nxt_http_action_t *action) { + nxt_upstream_t *u; u = r->conf->upstreams[action->u.upstream_number]; diff --git a/src/nxt_upstream.h b/src/nxt_upstream.h index 0c3b4c8b1..3e8a875c7 100644 --- a/src/nxt_upstream.h +++ b/src/nxt_upstream.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,74 +7,67 @@ #define _NXT_UPSTREAM_H_INCLUDED_ -typedef struct nxt_upstream_proxy_s nxt_upstream_proxy_t; -typedef struct nxt_upstream_round_robin_s nxt_upstream_round_robin_t; +typedef struct nxt_upstream_proxy_s nxt_upstream_proxy_t; +typedef struct nxt_upstream_round_robin_s nxt_upstream_round_robin_t; typedef struct nxt_upstream_round_robin_server_s nxt_upstream_round_robin_server_t; -typedef void (*nxt_upstream_peer_ready_t)(nxt_task_t *task, - nxt_upstream_server_t *us); -typedef void (*nxt_upstream_peer_error_t)(nxt_task_t *task, - nxt_upstream_server_t *us); - +typedef void ( + *nxt_upstream_peer_ready_t)(nxt_task_t *task, nxt_upstream_server_t *us); +typedef void ( + *nxt_upstream_peer_error_t)(nxt_task_t *task, nxt_upstream_server_t *us); typedef struct { - nxt_upstream_peer_ready_t ready; - nxt_upstream_peer_error_t error; + nxt_upstream_peer_ready_t ready; + nxt_upstream_peer_error_t error; } nxt_upstream_peer_state_t; - typedef nxt_upstream_t *(*nxt_upstream_joint_create_t)( nxt_router_temp_conf_t *tmcf, nxt_upstream_t *upstream); -typedef void (*nxt_upstream_server_get_t)(nxt_task_t *task, - nxt_upstream_server_t *us); - +typedef void ( + *nxt_upstream_server_get_t)(nxt_task_t *task, nxt_upstream_server_t *us); typedef struct { - nxt_upstream_joint_create_t joint_create; - nxt_upstream_server_get_t get; + nxt_upstream_joint_create_t joint_create; + nxt_upstream_server_get_t get; } nxt_upstream_server_proto_t; - struct nxt_upstream_s { - const nxt_upstream_server_proto_t *proto; + const nxt_upstream_server_proto_t *proto; union { - nxt_upstream_proxy_t *proxy; - nxt_upstream_round_robin_t *round_robin; + nxt_upstream_proxy_t *proxy; + nxt_upstream_round_robin_t *round_robin; } type; - nxt_str_t name; + nxt_str_t name; }; - struct nxt_upstreams_s { - uint32_t items; - nxt_upstream_t upstream[]; + uint32_t items; + nxt_upstream_t upstream[]; }; - struct nxt_upstream_server_s { - nxt_sockaddr_t *sockaddr; - const nxt_upstream_peer_state_t *state; - nxt_upstream_t *upstream; + nxt_sockaddr_t *sockaddr; + const nxt_upstream_peer_state_t *state; + nxt_upstream_t *upstream; - uint8_t protocol; + uint8_t protocol; union { - nxt_upstream_round_robin_server_t *round_robin; + nxt_upstream_round_robin_server_t *round_robin; } server; union { - nxt_http_peer_t *http; + nxt_http_peer_t *http; } peer; }; - -nxt_int_t nxt_upstream_round_robin_create(nxt_task_t *task, - nxt_router_temp_conf_t *tmcf, nxt_conf_value_t *upstream_conf, - nxt_upstream_t *upstream); +nxt_int_t +nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, + nxt_conf_value_t *upstream_conf, nxt_upstream_t *upstream); #endif /* _NXT_UPSTREAM_H_INCLUDED_ */ diff --git a/src/nxt_upstream_round_robin.c b/src/nxt_upstream_round_robin.c index 96c3e44e1..cf767c7c6 100644 --- a/src/nxt_upstream_round_robin.c +++ b/src/nxt_upstream_round_robin.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,64 +8,61 @@ #include #include - struct nxt_upstream_round_robin_server_s { - nxt_sockaddr_t *sockaddr; + nxt_sockaddr_t *sockaddr; - int32_t current_weight; - int32_t effective_weight; - int32_t weight; + int32_t current_weight; + int32_t effective_weight; + int32_t weight; - uint8_t protocol; + uint8_t protocol; }; - struct nxt_upstream_round_robin_s { - uint32_t items; - nxt_upstream_round_robin_server_t server[]; + uint32_t items; + nxt_upstream_round_robin_server_t server[]; }; - -static nxt_upstream_t *nxt_upstream_round_robin_joint_create( - nxt_router_temp_conf_t *tmcf, nxt_upstream_t *upstream); -static void nxt_upstream_round_robin_server_get(nxt_task_t *task, - nxt_upstream_server_t *us); +static nxt_upstream_t * +nxt_upstream_round_robin_joint_create(nxt_router_temp_conf_t *tmcf, + nxt_upstream_t *upstream); +static void +nxt_upstream_round_robin_server_get(nxt_task_t *task, + nxt_upstream_server_t *us); -static const nxt_upstream_server_proto_t nxt_upstream_round_robin_proto = { +static const nxt_upstream_server_proto_t nxt_upstream_round_robin_proto = { .joint_create = nxt_upstream_round_robin_joint_create, .get = nxt_upstream_round_robin_server_get, }; - nxt_int_t nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, - nxt_conf_value_t *upstream_conf, nxt_upstream_t *upstream) -{ + nxt_conf_value_t *upstream_conf, nxt_upstream_t *upstream) { double total, k, w; size_t size; uint32_t i, n, next, wt; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_str_t name; - nxt_sockaddr_t *sa; - nxt_conf_value_t *servers_conf, *srvcf, *wtcf; - nxt_upstream_round_robin_t *urr; + nxt_sockaddr_t *sa; + nxt_conf_value_t *servers_conf, *srvcf, *wtcf; + nxt_upstream_round_robin_t *urr; - static const nxt_str_t servers = nxt_string("servers"); - static const nxt_str_t weight = nxt_string("weight"); + static const nxt_str_t servers = nxt_string("servers"); + static const nxt_str_t weight = nxt_string("weight"); mp = tmcf->router_conf->mem_pool; servers_conf = nxt_conf_get_object_member(upstream_conf, &servers, NULL); - n = nxt_conf_object_members_count(servers_conf); + n = nxt_conf_object_members_count(servers_conf); total = 0.0; - next = 0; + next = 0; for (i = 0; i < n; i++) { - srvcf = nxt_conf_next_object_member(servers_conf, &name, &next); - wtcf = nxt_conf_get_object_member(srvcf, &weight, NULL); - w = (wtcf != NULL) ? nxt_conf_get_number(wtcf) : 1; + srvcf = nxt_conf_next_object_member(servers_conf, &name, &next); + wtcf = nxt_conf_get_object_member(srvcf, &weight, NULL); + w = (wtcf != NULL) ? nxt_conf_get_number(wtcf) : 1; total += w; } @@ -89,7 +85,7 @@ nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, } urr->items = n; - next = 0; + next = 0; for (i = 0; i < n; i++) { srvcf = nxt_conf_next_object_member(servers_conf, &name, &next); @@ -105,29 +101,27 @@ nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf, urr->server[i].protocol = NXT_HTTP_PROTO_H1; wtcf = nxt_conf_get_object_member(srvcf, &weight, NULL); - w = (wtcf != NULL) ? k * nxt_conf_get_number(wtcf) : k; - wt = (w > 1 || w == 0) ? round(w) : 1; + w = (wtcf != NULL) ? k * nxt_conf_get_number(wtcf) : k; + wt = (w > 1 || w == 0) ? round(w) : 1; - urr->server[i].weight = wt; + urr->server[i].weight = wt; urr->server[i].effective_weight = wt; } - upstream->proto = &nxt_upstream_round_robin_proto; + upstream->proto = &nxt_upstream_round_robin_proto; upstream->type.round_robin = urr; return NXT_OK; } - static nxt_upstream_t * nxt_upstream_round_robin_joint_create(nxt_router_temp_conf_t *tmcf, - nxt_upstream_t *upstream) -{ + nxt_upstream_t *upstream) { size_t size; uint32_t i, n; - nxt_mp_t *mp; - nxt_upstream_t *u; - nxt_upstream_round_robin_t *urr, *urrcf; + nxt_mp_t *mp; + nxt_upstream_t *u; + nxt_upstream_round_robin_t *urr, *urrcf; mp = tmcf->router_conf->mem_pool; @@ -150,7 +144,7 @@ nxt_upstream_round_robin_joint_create(nxt_router_temp_conf_t *tmcf, u->type.round_robin = urr; - n = urrcf->items; + n = urrcf->items; urr->items = n; for (i = 0; i < n; i++) { @@ -160,16 +154,15 @@ nxt_upstream_round_robin_joint_create(nxt_router_temp_conf_t *tmcf, return u; } - static void -nxt_upstream_round_robin_server_get(nxt_task_t *task, nxt_upstream_server_t *us) -{ +nxt_upstream_round_robin_server_get(nxt_task_t *task, + nxt_upstream_server_t *us) { int32_t total; uint32_t i, n; - nxt_upstream_round_robin_t *round_robin; - nxt_upstream_round_robin_server_t *s, *best; + nxt_upstream_round_robin_t *round_robin; + nxt_upstream_round_robin_server_t *s, *best; - best = NULL; + best = NULL; total = 0; round_robin = us->upstream->type.round_robin; @@ -178,9 +171,8 @@ nxt_upstream_round_robin_server_get(nxt_task_t *task, nxt_upstream_server_t *us) n = round_robin->items; for (i = 0; i < n; i++) { - s[i].current_weight += s[i].effective_weight; - total += s[i].effective_weight; + total += s[i].effective_weight; if (s[i].effective_weight < s[i].weight) { s[i].effective_weight++; @@ -196,10 +188,10 @@ nxt_upstream_round_robin_server_get(nxt_task_t *task, nxt_upstream_server_t *us) return; } - best->current_weight -= total; - us->sockaddr = best->sockaddr; - us->protocol = best->protocol; - us->server.round_robin = best; + best->current_weight -= total; + us->sockaddr = best->sockaddr; + us->protocol = best->protocol; + us->server.round_robin = best; us->state->ready(task, us); } diff --git a/src/nxt_utf8.c b/src/nxt_utf8.c index d983be841..0a30fe8b8 100644 --- a/src/nxt_utf8.c +++ b/src/nxt_utf8.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -31,38 +30,36 @@ u_char * -nxt_utf8_encode(u_char *p, uint32_t u) -{ +nxt_utf8_encode(u_char *p, uint32_t u) { if (u < 0x80) { *p++ = (u_char) (u & 0xFF); return p; } if (u < 0x0800) { - *p++ = (u_char) (( u >> 6) | 0xC0); - *p++ = (u_char) (( u & 0x3F) | 0x80); + *p++ = (u_char) ((u >> 6) | 0xC0); + *p++ = (u_char) ((u & 0x3F) | 0x80); return p; } if (u < 0x10000) { - *p++ = (u_char) ( (u >> 12) | 0xE0); - *p++ = (u_char) (((u >> 6) & 0x3F) | 0x80); - *p++ = (u_char) (( u & 0x3F) | 0x80); + *p++ = (u_char) ((u >> 12) | 0xE0); + *p++ = (u_char) (((u >> 6) & 0x3F) | 0x80); + *p++ = (u_char) ((u & 0x3F) | 0x80); return p; } if (u < 0x110000) { - *p++ = (u_char) ( (u >> 18) | 0xF0); + *p++ = (u_char) ((u >> 18) | 0xF0); *p++ = (u_char) (((u >> 12) & 0x3F) | 0x80); - *p++ = (u_char) (((u >> 6) & 0x3F) | 0x80); - *p++ = (u_char) (( u & 0x3F) | 0x80); + *p++ = (u_char) (((u >> 6) & 0x3F) | 0x80); + *p++ = (u_char) ((u & 0x3F) | 0x80); return p; } return NULL; } - /* * nxt_utf8_decode() decodes UTF-8 sequences and returns a valid * character 0x00 - 0x10FFFF, or 0xFFFFFFFF for invalid or overlong @@ -70,9 +67,8 @@ nxt_utf8_encode(u_char *p, uint32_t u) */ uint32_t -nxt_utf8_decode(const u_char **start, const u_char *end) -{ - uint32_t u; +nxt_utf8_decode(const u_char **start, const u_char *end) { + uint32_t u; u = (uint32_t) **start; @@ -84,7 +80,6 @@ nxt_utf8_decode(const u_char **start, const u_char *end) return nxt_utf8_decode2(start, end); } - /* * nxt_utf8_decode2() decodes two and more bytes UTF-8 sequences only * and returns a valid character 0x80 - 0x10FFFF, or 0xFFFFFFFF for @@ -92,20 +87,17 @@ nxt_utf8_decode(const u_char **start, const u_char *end) */ uint32_t -nxt_utf8_decode2(const u_char **start, const u_char *end) -{ +nxt_utf8_decode2(const u_char **start, const u_char *end) { u_char c; size_t n; uint32_t u, overlong; - const u_char *p; + const u_char *p; p = *start; u = (uint32_t) *p; if (u >= 0xE0) { - if (u >= 0xF0) { - if (nxt_slow_path(u > 0xF4)) { /* * The maximum valid Unicode character is 0x10FFFF @@ -114,23 +106,22 @@ nxt_utf8_decode2(const u_char **start, const u_char *end) return 0xFFFFFFFF; } - u &= 0x07; - overlong = 0x00FFFF; - n = 3; + u &= 0x07; + overlong = 0x00FFFF; + n = 3; } else { - u &= 0x0F; - overlong = 0x07FF; - n = 2; + u &= 0x0F; + overlong = 0x07FF; + n = 2; } } else if (u >= 0xC2) { - /* 0x80 is encoded as 0xC2 0x80. */ - u &= 0x1F; - overlong = 0x007F; - n = 1; + u &= 0x1F; + overlong = 0x007F; + n = 1; } else { /* u <= 0xC2 */ @@ -140,7 +131,6 @@ nxt_utf8_decode2(const u_char **start, const u_char *end) p++; if (nxt_fast_path(p + n <= end)) { - do { c = *p++; /* @@ -167,7 +157,6 @@ nxt_utf8_decode2(const u_char **start, const u_char *end) return 0xFFFFFFFF; } - /* * nxt_utf8_casecmp() tests only up to the minimum of given lengths, but * requires lengths of both strings because otherwise nxt_utf8_decode2() @@ -176,17 +165,15 @@ nxt_utf8_decode2(const u_char **start, const u_char *end) nxt_int_t nxt_utf8_casecmp(const u_char *start1, const u_char *start2, size_t len1, - size_t len2) -{ + size_t len2) { int32_t n; uint32_t u1, u2; - const u_char *end1, *end2; + const u_char *end1, *end2; end1 = start1 + len1; end2 = start2 + len2; while (start1 < end1 && start2 < end2) { - u1 = nxt_utf8_lowcase(&start1, end1); u2 = nxt_utf8_lowcase(&start2, end2); @@ -205,12 +192,10 @@ nxt_utf8_casecmp(const u_char *start1, const u_char *start2, size_t len1, return 0; } - uint32_t -nxt_utf8_lowcase(const u_char **start, const u_char *end) -{ +nxt_utf8_lowcase(const u_char **start, const u_char *end) { uint32_t u; - const uint32_t *block; + const uint32_t *block; u = (uint32_t) **start; @@ -233,12 +218,10 @@ nxt_utf8_lowcase(const u_char **start, const u_char *end) return u; } - ssize_t -nxt_utf8_length(const u_char *p, size_t len) -{ +nxt_utf8_length(const u_char *p, size_t len) { ssize_t length; - const u_char *end; + const u_char *end; length = 0; @@ -255,11 +238,9 @@ nxt_utf8_length(const u_char *p, size_t len) return length; } - nxt_bool_t -nxt_utf8_is_valid(const u_char *p, size_t len) -{ - const u_char *end; +nxt_utf8_is_valid(const u_char *p, size_t len) { + const u_char *end; end = p + len; diff --git a/src/nxt_utf8.h b/src/nxt_utf8.h index 13f42e16a..25b5b55d3 100644 --- a/src/nxt_utf8.h +++ b/src/nxt_utf8.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -13,30 +12,34 @@ * difference between Unicode characters is lesser 0x0010FFFF and * 0x0EEE0EEE can be used as value to indicate UTF-8 encoding error. */ -#define NXT_UTF8_SORT_INVALID 0x0EEE0EEE - - -NXT_EXPORT u_char *nxt_utf8_encode(u_char *p, uint32_t u); -NXT_EXPORT uint32_t nxt_utf8_decode(const u_char **start, const u_char *end); -NXT_EXPORT uint32_t nxt_utf8_decode2(const u_char **start, const u_char *end); -NXT_EXPORT nxt_int_t nxt_utf8_casecmp(const u_char *start1, - const u_char *start2, size_t len1, size_t len2); -NXT_EXPORT uint32_t nxt_utf8_lowcase(const u_char **start, const u_char *end); -NXT_EXPORT ssize_t nxt_utf8_length(const u_char *p, size_t len); -NXT_EXPORT nxt_bool_t nxt_utf8_is_valid(const u_char *p, size_t len); - +#define NXT_UTF8_SORT_INVALID 0x0EEE0EEE + + +NXT_EXPORT u_char * +nxt_utf8_encode(u_char *p, uint32_t u); +NXT_EXPORT uint32_t +nxt_utf8_decode(const u_char **start, const u_char *end); +NXT_EXPORT uint32_t +nxt_utf8_decode2(const u_char **start, const u_char *end); +NXT_EXPORT nxt_int_t +nxt_utf8_casecmp(const u_char *start1, const u_char *start2, size_t len1, + size_t len2); +NXT_EXPORT uint32_t +nxt_utf8_lowcase(const u_char **start, const u_char *end); +NXT_EXPORT ssize_t +nxt_utf8_length(const u_char *p, size_t len); +NXT_EXPORT nxt_bool_t +nxt_utf8_is_valid(const u_char *p, size_t len); /* nxt_utf8_next() expects a valid UTF-8 string. */ nxt_inline const u_char * -nxt_utf8_next(const u_char *p, const u_char *end) -{ - u_char c; +nxt_utf8_next(const u_char *p, const u_char *end) { + u_char c; c = *p++; if ((c & 0x80) != 0) { - do { /* * The first UTF-8 byte is either 0xxxxxxx or 11xxxxxx. diff --git a/src/nxt_var.c b/src/nxt_var.c index 16aa1c7ed..71b16cbb8 100644 --- a/src/nxt_var.c +++ b/src/nxt_var.c @@ -1,67 +1,67 @@ - /* * Copyright (C) NGINX, Inc. */ #include - struct nxt_var_s { - size_t length; - nxt_uint_t vars; - u_char data[]; - -/* - nxt_var_sub_t subs[vars]; - u_char raw[length]; -*/ + size_t length; + nxt_uint_t vars; + u_char data[]; + + /* + nxt_var_sub_t subs[vars]; + u_char raw[length]; + */ }; - typedef struct { - uint32_t index; - uint32_t length; - uint32_t position; + uint32_t index; + uint32_t length; + uint32_t position; } nxt_var_sub_t; - struct nxt_var_query_s { - nxt_mp_t *pool; + nxt_mp_t *pool; - nxt_var_cache_t cache; + nxt_var_cache_t cache; - void *ctx; - void *data; + void *ctx; + void *data; }; +#define nxt_var_subs(var) ((nxt_var_sub_t *) (var)->data) -#define nxt_var_subs(var) ((nxt_var_sub_t *) (var)->data) - -#define nxt_var_raw_start(var) \ +#define nxt_var_raw_start(var) \ ((var)->data + (var)->vars * sizeof(nxt_var_sub_t)) -static nxt_int_t nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data); -static nxt_var_decl_t *nxt_var_hash_find(nxt_str_t *name); +static nxt_int_t +nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_var_decl_t * +nxt_var_hash_find(nxt_str_t *name); -static nxt_var_ref_t *nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, - nxt_mp_t *mp); +static nxt_var_ref_t * +nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, nxt_mp_t *mp); -static nxt_int_t nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data); -static nxt_str_t *nxt_var_cache_value(nxt_task_t *task, nxt_tstr_state_t *state, +static nxt_int_t +nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data); +static nxt_str_t * +nxt_var_cache_value(nxt_task_t *task, nxt_tstr_state_t *state, nxt_var_cache_t *cache, nxt_var_ref_t *ref, void *ctx); -static u_char *nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part); +static u_char * +nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part); -static const nxt_lvlhsh_proto_t nxt_var_hash_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t nxt_var_hash_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_var_hash_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; -static const nxt_lvlhsh_proto_t nxt_var_cache_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t nxt_var_cache_proto nxt_aligned(64) = { NXT_LVLHSH_DEFAULT, nxt_var_cache_test, nxt_mp_lvlhsh_alloc, @@ -69,31 +69,27 @@ static const nxt_lvlhsh_proto_t nxt_var_cache_proto nxt_aligned(64) = { }; -static nxt_lvlhsh_t nxt_var_hash; -static uint32_t nxt_var_count; - -static nxt_var_decl_t **nxt_vars; +static nxt_lvlhsh_t nxt_var_hash; +static uint32_t nxt_var_count; +static nxt_var_decl_t **nxt_vars; static nxt_int_t -nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data) -{ - nxt_var_decl_t *decl; +nxt_var_hash_test(nxt_lvlhsh_query_t *lhq, void *data) { + nxt_var_decl_t *decl; decl = data; return nxt_strstr_eq(&lhq->key, &decl->name) ? NXT_OK : NXT_DECLINED; } - static nxt_var_decl_t * -nxt_var_hash_find(nxt_str_t *name) -{ - nxt_lvlhsh_query_t lhq; +nxt_var_hash_find(nxt_str_t *name) { + nxt_lvlhsh_query_t lhq; lhq.key_hash = nxt_djb_hash(name->start, name->length); - lhq.key = *name; - lhq.proto = &nxt_var_hash_proto; + lhq.key = *name; + lhq.proto = &nxt_var_hash_proto; if (nxt_lvlhsh_find(&nxt_var_hash, &lhq) != NXT_OK) { return NULL; @@ -102,19 +98,16 @@ nxt_var_hash_find(nxt_str_t *name) return lhq.value; } - static nxt_var_ref_t * -nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, nxt_mp_t *mp) -{ +nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, nxt_mp_t *mp) { nxt_int_t ret; nxt_uint_t i; - nxt_var_ref_t *ref; - nxt_var_decl_t *decl; + nxt_var_ref_t *ref; + nxt_var_decl_t *decl; ref = state->var_refs->elts; for (i = 0; i < state->var_refs->nelts; i++) { - if (nxt_strstr_eq(ref[i].name, name)) { return &ref[i]; } @@ -140,7 +133,7 @@ nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, nxt_mp_t *mp) decl = nxt_var_hash_find(name); if (decl != NULL) { - ref->handler = decl->handler; + ref->handler = decl->handler; ref->cacheable = (mp == state->pool) ? decl->cacheable : 0; goto done; @@ -161,12 +154,10 @@ nxt_var_ref_get(nxt_tstr_state_t *state, nxt_str_t *name, nxt_mp_t *mp) return ref; } - nxt_var_field_t * -nxt_var_field_new(nxt_mp_t *mp, nxt_str_t *name, uint32_t hash) -{ - nxt_str_t *str; - nxt_var_field_t *field; +nxt_var_field_new(nxt_mp_t *mp, nxt_str_t *name, uint32_t hash) { + nxt_str_t *str; + nxt_var_field_t *field; field = nxt_mp_alloc(mp, sizeof(nxt_var_field_t)); if (nxt_slow_path(field == NULL)) { @@ -183,14 +174,12 @@ nxt_var_field_new(nxt_mp_t *mp, nxt_str_t *name, uint32_t hash) return field; } - nxt_var_field_t * -nxt_var_field_get(nxt_array_t *fields, uint16_t index) -{ +nxt_var_field_get(nxt_array_t *fields, uint16_t index) { nxt_uint_t nfields; - nxt_var_field_t *field; + nxt_var_field_t *field; - field = fields->elts; + field = fields->elts; nfields = fields->nelts; if (nfields > 0 && index <= nfields) { @@ -200,21 +189,17 @@ nxt_var_field_get(nxt_array_t *fields, uint16_t index) return NULL; } - static nxt_int_t -nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_var_cache_test(nxt_lvlhsh_query_t *lhq, void *data) { return NXT_OK; } - static nxt_str_t * nxt_var_cache_value(nxt_task_t *task, nxt_tstr_state_t *state, - nxt_var_cache_t *cache, nxt_var_ref_t *ref, void *ctx) -{ - nxt_int_t ret; - nxt_str_t *value; - nxt_lvlhsh_query_t lhq; + nxt_var_cache_t *cache, nxt_var_ref_t *ref, void *ctx) { + nxt_int_t ret; + nxt_str_t *value; + nxt_lvlhsh_query_t lhq; value = cache->spare; @@ -231,13 +216,13 @@ nxt_var_cache_value(nxt_task_t *task, nxt_tstr_state_t *state, goto not_cached; } - lhq.key_hash = nxt_murmur_hash2_uint32(&ref->index); - lhq.replace = 0; + lhq.key_hash = nxt_murmur_hash2_uint32(&ref->index); + lhq.replace = 0; lhq.key.length = sizeof(uint32_t); - lhq.key.start = (u_char *) &ref->index; - lhq.value = value; - lhq.proto = &nxt_var_cache_proto; - lhq.pool = cache->pool; + lhq.key.start = (u_char *) &ref->index; + lhq.value = value; + lhq.proto = &nxt_var_cache_proto; + lhq.pool = cache->pool; ret = nxt_lvlhsh_insert(&cache->hash, &lhq); if (nxt_slow_path(ret == NXT_ERROR)) { @@ -260,20 +245,18 @@ nxt_var_cache_value(nxt_task_t *task, nxt_tstr_state_t *state, return value; } - nxt_int_t -nxt_var_register(nxt_var_decl_t *decl, size_t n) -{ - nxt_uint_t i; - nxt_lvlhsh_query_t lhq; +nxt_var_register(nxt_var_decl_t *decl, size_t n) { + nxt_uint_t i; + nxt_lvlhsh_query_t lhq; lhq.replace = 0; - lhq.proto = &nxt_var_hash_proto; + lhq.proto = &nxt_var_hash_proto; for (i = 0; i < n; i++) { - lhq.key = decl[i].name; + lhq.key = decl[i].name; lhq.key_hash = nxt_djb_hash(lhq.key.start, lhq.key.length); - lhq.value = &decl[i]; + lhq.value = &decl[i]; if (nxt_slow_path(nxt_lvlhsh_insert(&nxt_var_hash, &lhq) != NXT_OK)) { return NXT_ERROR; @@ -285,13 +268,11 @@ nxt_var_register(nxt_var_decl_t *decl, size_t n) return NXT_OK; } - nxt_int_t -nxt_var_index_init(void) -{ - nxt_uint_t i; - nxt_var_decl_t *decl, **vars; - nxt_lvlhsh_each_t lhe; +nxt_var_index_init(void) { + nxt_uint_t i; + nxt_var_decl_t *decl, **vars; + nxt_lvlhsh_each_t lhe; vars = nxt_memalign(64, nxt_var_count * sizeof(nxt_var_decl_t *)); if (vars == NULL) { @@ -301,7 +282,7 @@ nxt_var_index_init(void) nxt_lvlhsh_each_init(&lhe, &nxt_var_hash_proto); for (i = 0; i < nxt_var_count; i++) { - decl = nxt_lvlhsh_each(&nxt_var_hash, &lhe); + decl = nxt_lvlhsh_each(&nxt_var_hash, &lhe); vars[i] = decl; } @@ -310,21 +291,19 @@ nxt_var_index_init(void) return NXT_OK; } - nxt_var_t * -nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str) -{ - u_char *p, *end, *next, *src; +nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str) { + u_char *p, *end, *next, *src; size_t size; - nxt_var_t *var; + nxt_var_t *var; nxt_str_t part; nxt_uint_t n; - nxt_var_sub_t *subs; - nxt_var_ref_t *ref; + nxt_var_sub_t *subs; + nxt_var_ref_t *ref; n = 0; - p = str->start; + p = str->start; end = p + str->length; while (p < end) { @@ -346,10 +325,10 @@ nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str) } var->length = str->length; - var->vars = n; + var->vars = n; subs = nxt_var_subs(var); - src = nxt_var_raw_start(var); + src = nxt_var_raw_start(var); nxt_memcpy(src, str->start, str->length); @@ -365,8 +344,8 @@ nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str) return NULL; } - subs[n].index = ref->index; - subs[n].length = next - p; + subs[n].index = ref->index; + subs[n].length = next - p; subs[n].position = p - str->start; n++; @@ -378,15 +357,13 @@ nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str) return var; } - nxt_int_t -nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) -{ - u_char *p, *end, *next; +nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) { + u_char *p, *end, *next; nxt_str_t part; - nxt_var_ref_t *ref; + nxt_var_ref_t *ref; - p = str->start; + p = str->start; end = p + str->length; while (p < end) { @@ -394,7 +371,7 @@ nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) if (next == NULL) { nxt_sprintf(error, error + NXT_MAX_ERROR_STR, - "Invalid variable at position %uz%Z", p - str->start); + "Invalid variable at position %uz%Z", p - str->start); return NXT_ERROR; } @@ -404,7 +381,7 @@ nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) if (ref == NULL) { nxt_sprintf(error, error + NXT_MAX_ERROR_STR, - "Unknown variable \"%V\"%Z", &part); + "Unknown variable \"%V\"%Z", &part); return NXT_ERROR; } @@ -416,13 +393,11 @@ nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error) return NXT_OK; } - static u_char * -nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part) -{ - size_t length; - u_char *p, ch, c; - nxt_bool_t bracket; +nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part) { + size_t length; + u_char *p, ch, c; + nxt_bool_t bracket; p = memchr(start, '$', end - start); @@ -447,7 +422,7 @@ nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part) } length = 0; - start = p; + start = p; while (p < end) { ch = *p; @@ -473,7 +448,7 @@ nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part) } part->length = length; - part->start = start; + part->start = start; } else { if (p == NULL) { @@ -486,25 +461,23 @@ nxt_var_next_part(u_char *start, u_char *end, nxt_str_t *part) return p; } - nxt_int_t nxt_var_interpreter(nxt_task_t *task, nxt_tstr_state_t *state, nxt_var_cache_t *cache, nxt_var_t *var, nxt_str_t *str, void *ctx, - nxt_bool_t logging) -{ - u_char *p, *src; + nxt_bool_t logging) { + u_char *p, *src; size_t length, last, next; uint32_t index; - nxt_str_t *value, **part; + nxt_str_t *value, **part; nxt_uint_t i; nxt_array_t parts; - nxt_var_ref_t *ref; - nxt_var_sub_t *subs; + nxt_var_ref_t *ref; + nxt_var_sub_t *subs; nxt_memzero(&parts, sizeof(nxt_array_t)); nxt_array_init(&parts, cache->pool, sizeof(nxt_str_t *)); - ref = state->var_refs->elts; + ref = state->var_refs->elts; subs = nxt_var_subs(var); length = var->length; @@ -536,10 +509,10 @@ nxt_var_interpreter(nxt_task_t *task, nxt_tstr_state_t *state, } str->length = length; - str->start = p; + str->start = p; part = parts.elts; - src = nxt_var_raw_start(var); + src = nxt_var_raw_start(var); last = 0; @@ -566,12 +539,10 @@ nxt_var_interpreter(nxt_task_t *task, nxt_tstr_state_t *state, return NXT_OK; } - nxt_str_t * nxt_var_get(nxt_task_t *task, nxt_tstr_state_t *state, nxt_var_cache_t *cache, - nxt_str_t *name, void *ctx) -{ - nxt_var_ref_t *ref; + nxt_str_t *name, void *ctx) { + nxt_var_ref_t *ref; ref = nxt_var_ref_get(state, name, cache->pool); if (nxt_slow_path(ref == NULL)) { diff --git a/src/nxt_var.h b/src/nxt_var.h index 08a92c08a..4418d473a 100644 --- a/src/nxt_var.h +++ b/src/nxt_var.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,63 +6,65 @@ #define _NXT_VAR_H_INCLUDED_ -typedef struct nxt_var_s nxt_var_t; -typedef struct nxt_var_query_s nxt_var_query_t; +typedef struct nxt_var_s nxt_var_t; +typedef struct nxt_var_query_s nxt_var_query_t; -typedef nxt_int_t (*nxt_var_handler_t)(nxt_task_t *task, - nxt_str_t *str, - void *ctx, void *data); +typedef nxt_int_t (*nxt_var_handler_t)(nxt_task_t *task, nxt_str_t *str, + void *ctx, void *data); typedef int64_t (*nxt_var_field_hash_t)(nxt_mp_t *mp, nxt_str_t *str); typedef struct { - nxt_str_t name; - nxt_var_handler_t handler; - uint8_t cacheable; /* 1 bit */ + nxt_str_t name; + nxt_var_handler_t handler; + uint8_t cacheable; /* 1 bit */ } nxt_var_decl_t; - typedef struct { - nxt_str_t *name; - nxt_var_handler_t handler; - void *data; - uint32_t index; - uint8_t cacheable; /* 1 bit */ + nxt_str_t *name; + nxt_var_handler_t handler; + void *data; + uint32_t index; + uint8_t cacheable; /* 1 bit */ } nxt_var_ref_t; - typedef struct { - nxt_str_t name; - uint16_t hash; + nxt_str_t name; + uint16_t hash; } nxt_var_field_t; - typedef struct { - nxt_mp_t *pool; - nxt_lvlhsh_t hash; - nxt_str_t *spare; + nxt_mp_t *pool; + nxt_lvlhsh_t hash; + nxt_str_t *spare; } nxt_var_cache_t; +nxt_int_t +nxt_var_register(nxt_var_decl_t *decl, size_t n); +nxt_int_t +nxt_var_index_init(void); -nxt_int_t nxt_var_register(nxt_var_decl_t *decl, size_t n); -nxt_int_t nxt_var_index_init(void); - -nxt_var_field_t *nxt_var_field_get(nxt_array_t *fields, uint16_t index); -nxt_var_field_t *nxt_var_field_new(nxt_mp_t *mp, nxt_str_t *name, - uint32_t hash); +nxt_var_field_t * +nxt_var_field_get(nxt_array_t *fields, uint16_t index); +nxt_var_field_t * +nxt_var_field_new(nxt_mp_t *mp, nxt_str_t *name, uint32_t hash); -nxt_var_t *nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str); -nxt_int_t nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error); +nxt_var_t * +nxt_var_compile(nxt_tstr_state_t *state, nxt_str_t *str); +nxt_int_t +nxt_var_test(nxt_tstr_state_t *state, nxt_str_t *str, u_char *error); -nxt_int_t nxt_var_interpreter(nxt_task_t *task, nxt_tstr_state_t *state, +nxt_int_t +nxt_var_interpreter(nxt_task_t *task, nxt_tstr_state_t *state, nxt_var_cache_t *cache, nxt_var_t *var, nxt_str_t *str, void *ctx, nxt_bool_t logging); -nxt_str_t *nxt_var_get(nxt_task_t *task, nxt_tstr_state_t *state, - nxt_var_cache_t *cache, nxt_str_t *name, void *ctx); +nxt_str_t * +nxt_var_get(nxt_task_t *task, nxt_tstr_state_t *state, nxt_var_cache_t *cache, + nxt_str_t *name, void *ctx); -nxt_int_t nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, - nxt_str_t *name); +nxt_int_t +nxt_http_unknown_var_ref(nxt_mp_t *mp, nxt_var_ref_t *ref, nxt_str_t *name); #endif /* _NXT_VAR_H_INCLUDED_ */ diff --git a/src/nxt_websocket.c b/src/nxt_websocket.c index 910022379..b76d3b358 100644 --- a/src/nxt_websocket.c +++ b/src/nxt_websocket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,56 +6,42 @@ #include #include - nxt_inline uint16_t -nxt_ntoh16(const uint8_t *b) -{ +nxt_ntoh16(const uint8_t *b) { return ((uint16_t) b[0]) << 8 | ((uint16_t) b[1]); } - nxt_inline void -nxt_hton16(uint8_t *b, uint16_t v) -{ +nxt_hton16(uint8_t *b, uint16_t v) { b[0] = (v >> 8); b[1] = (v & 0xFFu); } - nxt_inline uint64_t -nxt_ntoh64(const uint8_t *b) -{ - return ((uint64_t) b[0]) << 56 - | ((uint64_t) b[1]) << 48 - | ((uint64_t) b[2]) << 40 - | ((uint64_t) b[3]) << 32 - | ((uint64_t) b[4]) << 24 - | ((uint64_t) b[5]) << 16 - | ((uint64_t) b[6]) << 8 - | ((uint64_t) b[7]); +nxt_ntoh64(const uint8_t *b) { + return ((uint64_t) b[0]) << 56 | ((uint64_t) b[1]) << 48 + | ((uint64_t) b[2]) << 40 | ((uint64_t) b[3]) << 32 + | ((uint64_t) b[4]) << 24 | ((uint64_t) b[5]) << 16 + | ((uint64_t) b[6]) << 8 | ((uint64_t) b[7]); } - nxt_inline void -nxt_hton64(uint8_t *b, uint64_t v) -{ +nxt_hton64(uint8_t *b, uint64_t v) { b[0] = (v >> 56); b[1] = (v >> 48) & 0xFFu; b[2] = (v >> 40) & 0xFFu; b[3] = (v >> 32) & 0xFFu; b[4] = (v >> 24) & 0xFFu; b[5] = (v >> 16) & 0xFFu; - b[6] = (v >> 8) & 0xFFu; - b[7] = v & 0xFFu; + b[6] = (v >> 8) & 0xFFu; + b[7] = v & 0xFFu; } - size_t -nxt_websocket_frame_header_size(const void *data) -{ +nxt_websocket_frame_header_size(const void *data) { size_t res; uint64_t p; - const nxt_websocket_header_t *h; + const nxt_websocket_header_t *h; h = data; p = h->payload_len; @@ -76,12 +61,10 @@ nxt_websocket_frame_header_size(const void *data) return res; } - uint64_t -nxt_websocket_frame_payload_len(const void *data) -{ +nxt_websocket_frame_payload_len(const void *data) { uint64_t p; - const nxt_websocket_header_t *h; + const nxt_websocket_header_t *h; h = data; p = h->payload_len; @@ -95,12 +78,10 @@ nxt_websocket_frame_payload_len(const void *data) return p; } - void * -nxt_websocket_frame_init(void *data, uint64_t payload_len) -{ - uint8_t *p; - nxt_websocket_header_t *h; +nxt_websocket_frame_init(void *data, uint64_t payload_len) { + uint8_t *p; + nxt_websocket_header_t *h; h = data; p = data; diff --git a/src/nxt_websocket.h b/src/nxt_websocket.h index 499a3268c..15762f59b 100644 --- a/src/nxt_websocket.h +++ b/src/nxt_websocket.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,16 +5,18 @@ #ifndef _NXT_WEBSOCKET_H_INCLUDED_ #define _NXT_WEBSOCKET_H_INCLUDED_ - enum { NXT_WEBSOCKET_ACCEPT_SIZE = 28, }; - -NXT_EXPORT size_t nxt_websocket_frame_header_size(const void *data); -NXT_EXPORT uint64_t nxt_websocket_frame_payload_len(const void *data); -NXT_EXPORT void *nxt_websocket_frame_init(void *data, uint64_t payload_len); -NXT_EXPORT void nxt_websocket_accept(u_char *accept, const void *key); +NXT_EXPORT size_t +nxt_websocket_frame_header_size(const void *data); +NXT_EXPORT uint64_t +nxt_websocket_frame_payload_len(const void *data); +NXT_EXPORT void * +nxt_websocket_frame_init(void *data, uint64_t payload_len); +NXT_EXPORT void +nxt_websocket_accept(u_char *accept, const void *key); -#endif /* _NXT_WEBSOCKET_H_INCLUDED_ */ +#endif /* _NXT_WEBSOCKET_H_INCLUDED_ */ diff --git a/src/nxt_websocket_accept.c b/src/nxt_websocket_accept.c index 0e2cef588..6bb1a77c5 100644 --- a/src/nxt_websocket_accept.c +++ b/src/nxt_websocket_accept.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,13 +6,11 @@ #include #include - static void -nxt_websocket_base64_encode(u_char *d, const uint8_t *s, size_t len) -{ - u_char c0, c1, c2; - static const u_char basis[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +nxt_websocket_base64_encode(u_char *d, const uint8_t *s, size_t len) { + u_char c0, c1, c2; + static const u_char basis[] + = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; while (len > 2) { c0 = s[0]; @@ -25,12 +22,12 @@ nxt_websocket_base64_encode(u_char *d, const uint8_t *s, size_t len) *d++ = basis[((c1 & 0x0f) << 2) | (c2 >> 6)]; *d++ = basis[c2 & 0x3f]; - s += 3; + s += 3; len -= 3; } if (len > 0) { - c0 = s[0]; + c0 = s[0]; *d++ = basis[c0 >> 2]; if (len == 1) { @@ -49,13 +46,11 @@ nxt_websocket_base64_encode(u_char *d, const uint8_t *s, size_t len) } } - void -nxt_websocket_accept(u_char *accept, const void *key) -{ - u_char bin_accept[20]; - nxt_sha1_t ctx; - static const char accept_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; +nxt_websocket_accept(u_char *accept, const void *key) { + u_char bin_accept[20]; + nxt_sha1_t ctx; + static const char accept_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; nxt_sha1_init(&ctx); nxt_sha1_update(&ctx, key, 24); @@ -64,5 +59,3 @@ nxt_websocket_accept(u_char *accept, const void *key) nxt_websocket_base64_encode(accept, bin_accept, sizeof(bin_accept)); } - - diff --git a/src/nxt_websocket_header.h b/src/nxt_websocket_header.h index cb7431dd0..557eced62 100644 --- a/src/nxt_websocket_header.h +++ b/src/nxt_websocket_header.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,34 +7,32 @@ #include - typedef struct { #if (NXT_HAVE_BIG_ENDIAN) - uint8_t fin:1; - uint8_t rsv1:1; - uint8_t rsv2:1; - uint8_t rsv3:1; + uint8_t fin :1; + uint8_t rsv1 :1; + uint8_t rsv2 :1; + uint8_t rsv3 :1; uint8_t opcode:4; - uint8_t mask:1; + uint8_t mask :1; uint8_t payload_len:7; #endif #if (NXT_HAVE_LITTLE_ENDIAN) uint8_t opcode:4; - uint8_t rsv3:1; - uint8_t rsv2:1; - uint8_t rsv1:1; - uint8_t fin:1; + uint8_t rsv3 :1; + uint8_t rsv2 :1; + uint8_t rsv1 :1; + uint8_t fin :1; uint8_t payload_len:7; - uint8_t mask:1; + uint8_t mask :1; #endif uint8_t payload_len_[8]; } nxt_websocket_header_t; - enum { NXT_WEBSOCKET_OP_CONT = 0x00, NXT_WEBSOCKET_OP_TEXT = 0x01, @@ -44,24 +41,23 @@ enum { NXT_WEBSOCKET_OP_PING = 0x09, NXT_WEBSOCKET_OP_PONG = 0x0A, - NXT_WEBSOCKET_OP_CTRL = 0x08, + NXT_WEBSOCKET_OP_CTRL = 0x08, }; - enum { - NXT_WEBSOCKET_CR_NORMAL = 1000, - NXT_WEBSOCKET_CR_GOING_AWAY = 1001, - NXT_WEBSOCKET_CR_PROTOCOL_ERROR = 1002, - NXT_WEBSOCKET_CR_UNPROCESSABLE_INPUT = 1003, - NXT_WEBSOCKET_CR_RESERVED = 1004, - NXT_WEBSOCKET_CR_NOT_PROVIDED = 1005, - NXT_WEBSOCKET_CR_ABNORMAL = 1006, - NXT_WEBSOCKET_CR_INVALID_DATA = 1007, - NXT_WEBSOCKET_CR_POLICY_VIOLATION = 1008, - NXT_WEBSOCKET_CR_MESSAGE_TOO_BIG = 1009, - NXT_WEBSOCKET_CR_EXTENSION_REQUIRED = 1010, - NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR = 1011, - NXT_WEBSOCKET_CR_TLS_HANDSHAKE_FAILED = 1015, + NXT_WEBSOCKET_CR_NORMAL = 1000, + NXT_WEBSOCKET_CR_GOING_AWAY = 1001, + NXT_WEBSOCKET_CR_PROTOCOL_ERROR = 1002, + NXT_WEBSOCKET_CR_UNPROCESSABLE_INPUT = 1003, + NXT_WEBSOCKET_CR_RESERVED = 1004, + NXT_WEBSOCKET_CR_NOT_PROVIDED = 1005, + NXT_WEBSOCKET_CR_ABNORMAL = 1006, + NXT_WEBSOCKET_CR_INVALID_DATA = 1007, + NXT_WEBSOCKET_CR_POLICY_VIOLATION = 1008, + NXT_WEBSOCKET_CR_MESSAGE_TOO_BIG = 1009, + NXT_WEBSOCKET_CR_EXTENSION_REQUIRED = 1010, + NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR = 1011, + NXT_WEBSOCKET_CR_TLS_HANDSHAKE_FAILED = 1015, }; diff --git a/src/nxt_work_queue.c b/src/nxt_work_queue.c index f38f700b0..e8d44bed5 100644 --- a/src/nxt_work_queue.c +++ b/src/nxt_work_queue.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -25,23 +24,23 @@ * a new spare chunk is allocated again. */ -static void nxt_work_queue_allocate(nxt_work_queue_cache_t *cache); +static void +nxt_work_queue_allocate(nxt_work_queue_cache_t *cache); /* It should be adjusted with the "work_queue_bucket_items" directive. */ -static nxt_uint_t nxt_work_queue_bucket_items = 409; +static nxt_uint_t nxt_work_queue_bucket_items = 409; #if (NXT_DEBUG) nxt_inline void -nxt_work_queue_thread_assert(nxt_work_queue_t *wq) -{ +nxt_work_queue_thread_assert(nxt_work_queue_t *wq) { nxt_tid_t tid; - nxt_thread_t *thread; + nxt_thread_t *thread; thread = nxt_thread(); - tid = nxt_thread_tid(thread); + tid = nxt_thread_tid(thread); if (nxt_fast_path(wq->tid == tid)) { return; @@ -58,10 +57,9 @@ nxt_work_queue_thread_assert(nxt_work_queue_t *wq) nxt_abort(); } - -void nxt_work_queue_thread_adopt(nxt_work_queue_t *wq) -{ - nxt_thread_t *thread; +void +nxt_work_queue_thread_adopt(nxt_work_queue_t *wq) { + nxt_thread_t *thread; thread = nxt_thread(); @@ -69,10 +67,8 @@ void nxt_work_queue_thread_adopt(nxt_work_queue_t *wq) wq->tid = nxt_thread_tid(thread); } - void -nxt_work_queue_name(nxt_work_queue_t *wq, const char *name) -{ +nxt_work_queue_name(nxt_work_queue_t *wq, const char *name) { nxt_work_queue_thread_assert(wq); wq->name = name; @@ -86,8 +82,7 @@ nxt_work_queue_name(nxt_work_queue_t *wq, const char *name) void -nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, size_t chunk_size) -{ +nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, size_t chunk_size) { nxt_memzero(cache, sizeof(nxt_work_queue_cache_t)); if (chunk_size == 0) { @@ -102,11 +97,9 @@ nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, size_t chunk_size) } } - void -nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache) -{ - nxt_work_queue_chunk_t *chunk, *next; +nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache) { + nxt_work_queue_chunk_t *chunk, *next; for (chunk = cache->chunk; chunk; chunk = next) { next = chunk->next; @@ -114,25 +107,22 @@ nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache) } } - static void -nxt_work_queue_allocate(nxt_work_queue_cache_t *cache) -{ +nxt_work_queue_allocate(nxt_work_queue_cache_t *cache) { size_t size; nxt_uint_t i, n; - nxt_work_t *work; - nxt_work_queue_chunk_t *chunk; + nxt_work_t *work; + nxt_work_queue_chunk_t *chunk; - n = cache->chunk_size; + n = cache->chunk_size; size = sizeof(nxt_work_queue_chunk_t) + n * sizeof(nxt_work_t); chunk = nxt_malloc(size); if (nxt_fast_path(chunk != NULL)) { - - chunk->next = cache->chunk; + chunk->next = cache->chunk; cache->chunk = chunk; - work = &chunk->work; + work = &chunk->work; for (i = 0; i < n; i++) { work[i].next = &work[i + 1]; @@ -142,39 +132,36 @@ nxt_work_queue_allocate(nxt_work_queue_cache_t *cache) work++; } else if (cache->spare != NULL) { - work = NULL; } else { return; } - cache->next = cache->spare; + cache->next = cache->spare; cache->spare = work; } - /* Add a work to a work queue tail. */ void nxt_work_queue_add(nxt_work_queue_t *wq, nxt_work_handler_t handler, - nxt_task_t *task, void *obj, void *data) -{ - nxt_work_t *work; + nxt_task_t *task, void *obj, void *data) { + nxt_work_t *work; nxt_work_queue_thread_assert(wq); - for ( ;; ) { + for (;;) { work = wq->cache->next; if (nxt_fast_path(work != NULL)) { wq->cache->next = work->next; - work->next = NULL; + work->next = NULL; work->handler = handler; - work->task = task; - work->obj = obj; - work->data = data; + work->task = task; + work->obj = obj; + work->data = data; if (wq->tail != NULL) { wq->tail->next = work; @@ -192,12 +179,10 @@ nxt_work_queue_add(nxt_work_queue_t *wq, nxt_work_handler_t handler, } } - nxt_work_handler_t nxt_work_queue_pop(nxt_work_queue_t *wq, nxt_task_t **task, void **obj, - void **data) -{ - nxt_work_t *work; + void **data) { + nxt_work_t *work; nxt_work_queue_thread_assert(wq); @@ -217,18 +202,16 @@ nxt_work_queue_pop(nxt_work_queue_t *wq, nxt_task_t **task, void **obj, *data = work->data; nxt_prefetch(*data); - work->next = wq->cache->next; + work->next = wq->cache->next; wq->cache->next = work; return work->handler; } - /* Add a work to a locked work queue tail. */ void -nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, nxt_work_t *work) -{ +nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, nxt_work_t *work) { nxt_thread_spin_lock(&lwq->lock); if (lwq->tail != NULL) { @@ -243,15 +226,13 @@ nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, nxt_work_t *work) nxt_thread_spin_unlock(&lwq->lock); } - /* Pop a work from a locked work queue head. */ nxt_work_handler_t nxt_locked_work_queue_pop(nxt_locked_work_queue_t *lwq, nxt_task_t **task, - void **obj, void **data) -{ - nxt_work_t *work; - nxt_work_handler_t handler; + void **obj, void **data) { + nxt_work_t *work; + nxt_work_handler_t handler; handler = NULL; @@ -282,14 +263,12 @@ nxt_locked_work_queue_pop(nxt_locked_work_queue_t *lwq, nxt_task_t **task, return handler; } - /* Move all works from a locked work queue to a usual work queue. */ void nxt_locked_work_queue_move(nxt_thread_t *thr, nxt_locked_work_queue_t *lwq, - nxt_work_queue_t *wq) -{ - nxt_work_t *work; + nxt_work_queue_t *wq) { + nxt_work_t *work; nxt_thread_spin_lock(&lwq->lock); @@ -303,8 +282,8 @@ nxt_locked_work_queue_move(nxt_thread_t *thr, nxt_locked_work_queue_t *lwq, while (work != NULL) { work->task->thread = thr; - nxt_work_queue_add(wq, work->handler, work->task, - work->obj, work->data); + nxt_work_queue_add(wq, work->handler, work->task, work->obj, + work->data); work = work->next; } diff --git a/src/nxt_work_queue.h b/src/nxt_work_queue.h index b6aa4d4c8..206f721ae 100644 --- a/src/nxt_work_queue.h +++ b/src/nxt_work_queue.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,22 +7,20 @@ #define _NXT_WORK_QUEUE_H_INCLUDED_ -typedef struct nxt_work_s nxt_work_t; +typedef struct nxt_work_s nxt_work_t; struct nxt_task_s { - nxt_thread_t *thread; - nxt_log_t *log; + nxt_thread_t *thread; + nxt_log_t *log; uint32_t ident; - nxt_work_t *next_work; + nxt_work_t *next_work; /* TODO: exception_handler, prev/next task, subtasks. */ }; - -#define nxt_task_next_ident() \ +#define nxt_task_next_ident() \ ((uint32_t) nxt_atomic_fetch_add(&nxt_task_ident, 1) & 0x3FFFFFFF) - /* * A work handler with just the obj and data arguments instead * of pointer to a possibly large a work struct allows to call @@ -32,80 +29,82 @@ struct nxt_task_s { * source filters, so the data argument has been introduced and * is used where appropriate. */ -//typedef void (*nxt_work_handler_t)(nxt_task_t *task, void *obj, void *data); +// typedef void (*nxt_work_handler_t)(nxt_task_t *task, void *obj, void *data); struct nxt_work_s { - nxt_work_t *next; - nxt_work_handler_t handler; - nxt_task_t *task; - void *obj; - void *data; + nxt_work_t *next; + nxt_work_handler_t handler; + nxt_task_t *task; + void *obj; + void *data; }; -typedef struct nxt_work_queue_chunk_s nxt_work_queue_chunk_t; +typedef struct nxt_work_queue_chunk_s nxt_work_queue_chunk_t; struct nxt_work_queue_chunk_s { - nxt_work_queue_chunk_t *next; - nxt_work_t work; + nxt_work_queue_chunk_t *next; + nxt_work_t work; }; - typedef struct { - nxt_work_t *next; - nxt_work_t *spare; - nxt_work_queue_chunk_t *chunk; - size_t chunk_size; + nxt_work_t *next; + nxt_work_t *spare; + nxt_work_queue_chunk_t *chunk; + size_t chunk_size; } nxt_work_queue_cache_t; -typedef struct nxt_work_queue_s nxt_work_queue_t; +typedef struct nxt_work_queue_s nxt_work_queue_t; struct nxt_work_queue_s { - nxt_work_t *head; - nxt_work_t *tail; - nxt_work_queue_cache_t *cache; + nxt_work_t *head; + nxt_work_t *tail; + nxt_work_queue_cache_t *cache; #if (NXT_DEBUG) - const char *name; - int32_t pid; - nxt_tid_t tid; + const char *name; + int32_t pid; + nxt_tid_t tid; #endif }; - typedef struct { - nxt_thread_spinlock_t lock; - nxt_work_t *head; - nxt_work_t *tail; - nxt_work_queue_cache_t cache; + nxt_thread_spinlock_t lock; + nxt_work_t *head; + nxt_work_t *tail; + nxt_work_queue_cache_t cache; } nxt_locked_work_queue_t; - -NXT_EXPORT void nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, - size_t chunk_size); -NXT_EXPORT void nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache); - -NXT_EXPORT void nxt_work_queue_add(nxt_work_queue_t *wq, - nxt_work_handler_t handler, nxt_task_t *task, void *obj, void *data); -NXT_EXPORT nxt_work_handler_t nxt_work_queue_pop(nxt_work_queue_t *wq, - nxt_task_t **task, void **obj, void **data); - - -#define nxt_work_set(_work, _handler, _task, _obj, _data) \ - do { \ - nxt_work_t *work = _work; \ - \ - work->handler = _handler; \ - work->task = _task; \ - work->obj = _obj; \ - work->data = _data; \ +NXT_EXPORT void +nxt_work_queue_cache_create(nxt_work_queue_cache_t *cache, size_t chunk_size); +NXT_EXPORT void +nxt_work_queue_cache_destroy(nxt_work_queue_cache_t *cache); + +NXT_EXPORT void +nxt_work_queue_add(nxt_work_queue_t *wq, nxt_work_handler_t handler, + nxt_task_t *task, void *obj, void *data); +NXT_EXPORT nxt_work_handler_t +nxt_work_queue_pop(nxt_work_queue_t *wq, nxt_task_t **task, void **obj, + void **data); + + +#define nxt_work_set(_work, _handler, _task, _obj, _data) \ + do { \ + nxt_work_t *work = _work; \ + \ + work->handler = _handler; \ + work->task = _task; \ + work->obj = _obj; \ + work->data = _data; \ } while (0) #if (NXT_DEBUG) -NXT_EXPORT void nxt_work_queue_name(nxt_work_queue_t *wq, const char *name); -NXT_EXPORT void nxt_work_queue_thread_adopt(nxt_work_queue_t *wq); +NXT_EXPORT void +nxt_work_queue_name(nxt_work_queue_t *wq, const char *name); +NXT_EXPORT void +nxt_work_queue_thread_adopt(nxt_work_queue_t *wq); #else @@ -116,12 +115,14 @@ NXT_EXPORT void nxt_work_queue_thread_adopt(nxt_work_queue_t *wq); #endif -NXT_EXPORT void nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, - nxt_work_t *work); -NXT_EXPORT nxt_work_handler_t nxt_locked_work_queue_pop( - nxt_locked_work_queue_t *lwq, nxt_task_t **task, void **obj, void **data); -NXT_EXPORT void nxt_locked_work_queue_move(nxt_thread_t *thr, - nxt_locked_work_queue_t *lwq, nxt_work_queue_t *wq); +NXT_EXPORT void +nxt_locked_work_queue_add(nxt_locked_work_queue_t *lwq, nxt_work_t *work); +NXT_EXPORT nxt_work_handler_t +nxt_locked_work_queue_pop(nxt_locked_work_queue_t *lwq, nxt_task_t **task, + void **obj, void **data); +NXT_EXPORT void +nxt_locked_work_queue_move(nxt_thread_t *thr, nxt_locked_work_queue_t *lwq, + nxt_work_queue_t *wq); #endif /* _NXT_WORK_QUEUE_H_INCLUDED_ */ diff --git a/src/perl/nxt_perl_psgi.c b/src/perl/nxt_perl_psgi.c index 587656cd4..07dbff325 100644 --- a/src/perl/nxt_perl_psgi.c +++ b/src/perl/nxt_perl_psgi.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Alexander Borisov * Copyright (C) NGINX, Inc. @@ -15,27 +14,29 @@ #include #include - typedef struct { - PerlInterpreter *my_perl; + PerlInterpreter *my_perl; nxt_perl_psgi_io_arg_t arg_input; nxt_perl_psgi_io_arg_t arg_error; - SV *app; - CV *cb; - nxt_unit_request_info_t *req; + SV *app; + CV *cb; + nxt_unit_request_info_t *req; pthread_t thread; - nxt_unit_ctx_t *ctx; + nxt_unit_ctx_t *ctx; } nxt_perl_psgi_ctx_t; - -static SSize_t nxt_perl_psgi_io_input_read(PerlInterpreter *my_perl, +static SSize_t +nxt_perl_psgi_io_input_read(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length); -static SSize_t nxt_perl_psgi_io_input_write(PerlInterpreter *my_perl, +static SSize_t +nxt_perl_psgi_io_input_write(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length); -static SSize_t nxt_perl_psgi_io_error_read(PerlInterpreter *my_perl, +static SSize_t +nxt_perl_psgi_io_error_read(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length); -static SSize_t nxt_perl_psgi_io_error_write(PerlInterpreter *my_perl, +static SSize_t +nxt_perl_psgi_io_error_write(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length); /* @@ -45,70 +46,91 @@ static void nxt_perl_psgi_xs_core_global_changes(PerlInterpreter *my_perl, static void nxt_perl_psgi_xs_init(pTHX); -static SV *nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, - SV *env, SV *app, nxt_unit_request_info_t *req); -static SV *nxt_perl_psgi_call_method(PerlInterpreter *my_perl, SV *obj, - const char *method, nxt_unit_request_info_t *req); +static SV * +nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, SV *env, SV *app, + nxt_unit_request_info_t *req); +static SV * +nxt_perl_psgi_call_method(PerlInterpreter *my_perl, SV *obj, const char *method, + nxt_unit_request_info_t *req); /* For currect load XS modules */ -EXTERN_C void boot_DynaLoader(pTHX_ CV *cv); +EXTERN_C void +boot_DynaLoader(pTHX_ CV *cv); -static int nxt_perl_psgi_io_init(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, const char *mode, void *req); +static int +nxt_perl_psgi_io_init(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, + const char *mode, void *req); -static int nxt_perl_psgi_ctx_init(const char *script, - nxt_perl_psgi_ctx_t *pctx); +static int +nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx); -static SV *nxt_perl_psgi_env_create(PerlInterpreter *my_perl, - nxt_unit_request_info_t *req); -nxt_inline int nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env, - const char *name, uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len); -nxt_inline int nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env, - const char *name, uint32_t name_len, const char *str, uint32_t len); -nxt_inline int nxt_perl_psgi_add_value(PerlInterpreter *my_perl, HV *hash_env, +static SV * +nxt_perl_psgi_env_create(PerlInterpreter *my_perl, + nxt_unit_request_info_t *req); +nxt_inline int +nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env, const char *name, + uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len); +nxt_inline int +nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env, const char *name, + uint32_t name_len, const char *str, uint32_t len); +nxt_inline int +nxt_perl_psgi_add_value(PerlInterpreter *my_perl, HV *hash_env, const char *name, uint32_t name_len, void *value); -static char *nxt_perl_psgi_module_create(const char *script); +static char * +nxt_perl_psgi_module_create(const char *script); -static nxt_int_t nxt_perl_psgi_result_status(PerlInterpreter *my_perl, - SV *result); -static int nxt_perl_psgi_result_head(PerlInterpreter *my_perl, - SV *sv_head, nxt_unit_request_info_t *req, uint16_t status); -static int nxt_perl_psgi_result_body(PerlInterpreter *my_perl, - SV *result, nxt_unit_request_info_t *req); -static int nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, - SV *sv_body, nxt_unit_request_info_t *req); -static int nxt_perl_psgi_result_body_fh(PerlInterpreter *my_perl, SV *sv_body, +static nxt_int_t +nxt_perl_psgi_result_status(PerlInterpreter *my_perl, SV *result); +static int +nxt_perl_psgi_result_head(PerlInterpreter *my_perl, SV *sv_head, + nxt_unit_request_info_t *req, uint16_t status); +static int +nxt_perl_psgi_result_body(PerlInterpreter *my_perl, SV *result, + nxt_unit_request_info_t *req); +static int +nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, SV *sv_body, + nxt_unit_request_info_t *req); +static int +nxt_perl_psgi_result_body_fh(PerlInterpreter *my_perl, SV *sv_body, nxt_unit_request_info_t *req); -static ssize_t nxt_perl_psgi_io_read(nxt_unit_read_info_t *read_info, void *dst, - size_t size); -static int nxt_perl_psgi_result_array(PerlInterpreter *my_perl, - SV *result, nxt_unit_request_info_t *req); -static void nxt_perl_psgi_result_cb(PerlInterpreter *my_perl, SV *result, +static ssize_t +nxt_perl_psgi_io_read(nxt_unit_read_info_t *read_info, void *dst, size_t size); +static int +nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, nxt_unit_request_info_t *req); +static void +nxt_perl_psgi_result_cb(PerlInterpreter *my_perl, SV *result, + nxt_unit_request_info_t *req); + +static nxt_int_t +nxt_perl_psgi_start(nxt_task_t *task, nxt_process_data_t *data); +static void +nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req); +static int +nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx); +static void * +nxt_perl_psgi_thread_func(void *main_ctx); +static int +nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c); +static void +nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, nxt_perl_app_conf_t *c); +static void +nxt_perl_psgi_ctx_free(nxt_perl_psgi_ctx_t *pctx); + +static CV *nxt_perl_psgi_write; +static CV *nxt_perl_psgi_close; +static CV *nxt_perl_psgi_cb; +static pthread_attr_t *nxt_perl_psgi_thread_attr; +static nxt_perl_psgi_ctx_t *nxt_perl_psgi_ctxs; -static nxt_int_t nxt_perl_psgi_start(nxt_task_t *task, - nxt_process_data_t *data); -static void nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req); -static int nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx); -static void *nxt_perl_psgi_thread_func(void *main_ctx); -static int nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c); -static void nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, - nxt_perl_app_conf_t *c); -static void nxt_perl_psgi_ctx_free(nxt_perl_psgi_ctx_t *pctx); - -static CV *nxt_perl_psgi_write; -static CV *nxt_perl_psgi_close; -static CV *nxt_perl_psgi_cb; -static pthread_attr_t *nxt_perl_psgi_thread_attr; -static nxt_perl_psgi_ctx_t *nxt_perl_psgi_ctxs; - -static uint32_t nxt_perl_psgi_compat[] = { - NXT_VERNUM, NXT_DEBUG, +static uint32_t nxt_perl_psgi_compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -NXT_EXPORT nxt_app_module_t nxt_app_module = { +NXT_EXPORT nxt_app_module_t nxt_app_module = { sizeof(nxt_perl_psgi_compat), nxt_perl_psgi_compat, nxt_string("perl"), @@ -120,50 +142,41 @@ NXT_EXPORT nxt_app_module_t nxt_app_module = { }; const nxt_perl_psgi_io_tab_t nxt_perl_psgi_io_tab_input = { - .read = nxt_perl_psgi_io_input_read, + .read = nxt_perl_psgi_io_input_read, .write = nxt_perl_psgi_io_input_write, }; const nxt_perl_psgi_io_tab_t nxt_perl_psgi_io_tab_error = { - .read = nxt_perl_psgi_io_error_read, + .read = nxt_perl_psgi_io_error_read, .write = nxt_perl_psgi_io_error_write, }; - static SSize_t nxt_perl_psgi_io_input_read(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length) -{ + nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length) { return nxt_unit_request_read(arg->req, vbuf, length); } - static SSize_t nxt_perl_psgi_io_input_write(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length) -{ + nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length) { return 0; } - static SSize_t nxt_perl_psgi_io_error_read(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length) -{ + nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length) { return 0; } - static SSize_t nxt_perl_psgi_io_error_write(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length) -{ - nxt_unit_req_error(arg->req, "Perl: %s", (const char*) vbuf); + nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length) { + nxt_unit_req_error(arg->req, "Perl: %s", (const char *) vbuf); return (SSize_t) length; } - /* In the future it will be necessary to change some Perl functions. */ /* static void @@ -187,21 +200,20 @@ nxt_perl_psgi_xs_core_global_changes(PerlInterpreter *my_perl, XS(XS_NGINX__Unit__PSGI_exit); -XS(XS_NGINX__Unit__PSGI_exit) -{ + +XS(XS_NGINX__Unit__PSGI_exit) { I32 ax = POPMARK; - Perl_croak(aTHX_ (char *) NULL); + Perl_croak(aTHX_(char *) NULL); XSRETURN_EMPTY; } - XS(XS_NGINX__Unit__Sandbox_write); -XS(XS_NGINX__Unit__Sandbox_write) -{ + +XS(XS_NGINX__Unit__Sandbox_write) { int rc; - char *body; + char *body; size_t len; - nxt_perl_psgi_ctx_t *pctx; + nxt_perl_psgi_ctx_t *pctx; dXSARGS; @@ -225,21 +237,18 @@ XS(XS_NGINX__Unit__Sandbox_write) XSRETURN_IV(len); } - nxt_inline void -nxt_perl_psgi_cb_request_done(nxt_perl_psgi_ctx_t *pctx, int status) -{ +nxt_perl_psgi_cb_request_done(nxt_perl_psgi_ctx_t *pctx, int status) { if (pctx->req != NULL) { nxt_unit_request_done(pctx->req, status); pctx->req = NULL; } } - XS(XS_NGINX__Unit__Sandbox_close); -XS(XS_NGINX__Unit__Sandbox_close) -{ - I32 ax; + +XS(XS_NGINX__Unit__Sandbox_close) { + I32 ax; ax = POPMARK; @@ -248,14 +257,13 @@ XS(XS_NGINX__Unit__Sandbox_close) XSRETURN_NO; } - XS(XS_NGINX__Unit__Sandbox_cb); -XS(XS_NGINX__Unit__Sandbox_cb) -{ - SV *obj; + +XS(XS_NGINX__Unit__Sandbox_cb) { + SV *obj; int rc; long array_len; - nxt_perl_psgi_ctx_t *pctx; + nxt_perl_psgi_ctx_t *pctx; dXSARGS; @@ -271,12 +279,11 @@ XS(XS_NGINX__Unit__Sandbox_cb) if (nxt_slow_path(SvOK(ST(0)) == 0 || SvROK(ST(0)) == 0 || SvTYPE(SvRV(ST(0))) != SVt_PVAV - || pctx->req == NULL)) - { + || pctx->req == NULL)) { nxt_perl_psgi_cb_request_done(CvXSUBANY(cv).any_ptr, NXT_UNIT_ERROR); Perl_croak(aTHX_ "PSGI: An unexpected response was received " - "from Perl Application"); + "from Perl Application"); XSRETURN_EMPTY; } @@ -285,7 +292,7 @@ XS(XS_NGINX__Unit__Sandbox_cb) if (nxt_slow_path(rc != NXT_UNIT_OK)) { nxt_perl_psgi_cb_request_done(CvXSUBANY(cv).any_ptr, NXT_UNIT_ERROR); - Perl_croak(aTHX_ (char *) NULL); + Perl_croak(aTHX_(char *) NULL); XSRETURN_EMPTY; } @@ -293,8 +300,8 @@ XS(XS_NGINX__Unit__Sandbox_cb) array_len = av_len((AV *) SvRV(ST(0))); if (array_len < 2) { - obj = sv_bless(newRV_noinc((SV *) newHV()), - gv_stashpv("NGINX::Unit::Sandbox", GV_ADD)); + obj = sv_bless(newRV_noinc((SV *) newHV()), + gv_stashpv("NGINX::Unit::Sandbox", GV_ADD)); ST(0) = obj; XSRETURN(1); @@ -305,36 +312,32 @@ XS(XS_NGINX__Unit__Sandbox_cb) XSRETURN_EMPTY; } - static void -nxt_perl_psgi_xs_init(pTHX) -{ -/* - nxt_perl_psgi_xs_core_global_changes(my_perl, "CORE::GLOBAL::exit", - "NGINX::Unit::PSGI::exit", - XS_NGINX__Unit__PSGI_exit); -*/ +nxt_perl_psgi_xs_init(pTHX) { + /* + nxt_perl_psgi_xs_core_global_changes(my_perl, "CORE::GLOBAL::exit", + "NGINX::Unit::PSGI::exit", + XS_NGINX__Unit__PSGI_exit); + */ nxt_perl_psgi_layer_stream_init(aTHX); /* DynaLoader for Perl modules who use XS */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); nxt_perl_psgi_write = newXS("NGINX::Unit::Sandbox::write", - XS_NGINX__Unit__Sandbox_write, __FILE__); + XS_NGINX__Unit__Sandbox_write, __FILE__); nxt_perl_psgi_close = newXS("NGINX::Unit::Sandbox::close", - XS_NGINX__Unit__Sandbox_close, __FILE__); + XS_NGINX__Unit__Sandbox_close, __FILE__); nxt_perl_psgi_cb = newXS("NGINX::Unit::Sandbox::cb", - XS_NGINX__Unit__Sandbox_cb, __FILE__); + XS_NGINX__Unit__Sandbox_cb, __FILE__); } - static SV * -nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, - SV *env, SV *app, nxt_unit_request_info_t *req) -{ - SV *result; +nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, SV *env, SV *app, + nxt_unit_request_info_t *req) { + SV *result; dSP; @@ -345,13 +348,13 @@ nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, XPUSHs(env); PUTBACK; - call_sv(app, G_EVAL|G_SCALAR); + call_sv(app, G_EVAL | G_SCALAR); SPAGAIN; if (SvTRUE(ERRSV)) { nxt_unit_req_error(req, "PSGI: Failed to run Perl Application: \n%s", - SvPV_nolen(ERRSV)); + SvPV_nolen(ERRSV)); } result = POPs; @@ -364,12 +367,10 @@ nxt_perl_psgi_call_var_application(PerlInterpreter *my_perl, return result; } - static SV * nxt_perl_psgi_call_method(PerlInterpreter *my_perl, SV *obj, const char *method, - nxt_unit_request_info_t *req) -{ - SV *result; + nxt_unit_request_info_t *req) { + SV *result; dSP; @@ -380,13 +381,13 @@ nxt_perl_psgi_call_method(PerlInterpreter *my_perl, SV *obj, const char *method, XPUSHs(obj); PUTBACK; - call_method(method, G_EVAL|G_SCALAR); + call_method(method, G_EVAL | G_SCALAR); SPAGAIN; if (SvTRUE(ERRSV)) { - nxt_unit_req_error(req, "PSGI: Failed to call method '%s':\n%s", - method, SvPV_nolen(ERRSV)); + nxt_unit_req_error(req, "PSGI: Failed to call method '%s':\n%s", method, + SvPV_nolen(ERRSV)); result = NULL; } else { @@ -400,33 +401,30 @@ nxt_perl_psgi_call_method(PerlInterpreter *my_perl, SV *obj, const char *method, return result; } - static char * -nxt_perl_psgi_module_create(const char *script) -{ - char *buf, *p; - size_t length; +nxt_perl_psgi_module_create(const char *script) { + char *buf, *p; + size_t length; - static const nxt_str_t prefix = nxt_string( - "package NGINX::Unit::Sandbox;" - "{my $app = do \"" - ); + static const nxt_str_t prefix = nxt_string("package NGINX::Unit::Sandbox;" + "{my $app = do \""); - static const nxt_str_t suffix = nxt_string_zero( + static const nxt_str_t suffix = nxt_string_zero( "\";" "unless ($app) {" " if($@ || $1) {die $@ || $1}" " else {die \"File not found or compilation error.\"}" "} " - "return $app}" - ); + "return $app}"); length = strlen(script); buf = nxt_unit_malloc(NULL, prefix.length + length + suffix.length); if (nxt_slow_path(buf == NULL)) { - nxt_unit_alert(NULL, "PSGI: Failed to allocate memory " - "for Perl script file %s", script); + nxt_unit_alert(NULL, + "PSGI: Failed to allocate memory " + "for Perl script file %s", + script); return NULL; } @@ -438,13 +436,11 @@ nxt_perl_psgi_module_create(const char *script) return buf; } - static int -nxt_perl_psgi_io_init(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, const char *mode, void *req) -{ - SV *io; - PerlIO *fp; +nxt_perl_psgi_io_init(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, + const char *mode, void *req) { + SV *io; + PerlIO *fp; if (arg->io == NULL) { fp = nxt_perl_psgi_layer_stream_fp_create(aTHX_ arg->rv, mode); @@ -467,32 +463,31 @@ nxt_perl_psgi_io_init(PerlInterpreter *my_perl, return NXT_UNIT_OK; } - static void -nxt_perl_psgi_io_release(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg) -{ +nxt_perl_psgi_io_release(PerlInterpreter *my_perl, + nxt_perl_psgi_io_arg_t *arg) { if (arg->io != NULL) { SvREFCNT_dec(arg->io); arg->io = NULL; } } - static int -nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx) -{ +nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx) { int status, res; - char *run_module; - PerlInterpreter *my_perl; + char *run_module; + PerlInterpreter *my_perl; - static char argv[] = "\0""-e\0""0"; - static char *embedding[] = { &argv[0], &argv[1], &argv[4] }; + static char argv[] = "\0" + "-e\0" + "0"; + static char *embedding[] = {&argv[0], &argv[1], &argv[4]}; my_perl = perl_alloc(); if (nxt_slow_path(my_perl == NULL)) { nxt_unit_alert(NULL, - "PSGI: Failed to allocate memory for Perl interpreter"); + "PSGI: Failed to allocate memory for Perl interpreter"); return NXT_UNIT_ERROR; } @@ -513,12 +508,12 @@ nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx) CvXSUBANY(nxt_perl_psgi_write).any_ptr = pctx; CvXSUBANY(nxt_perl_psgi_close).any_ptr = pctx; - CvXSUBANY(nxt_perl_psgi_cb).any_ptr = pctx; + CvXSUBANY(nxt_perl_psgi_cb).any_ptr = pctx; pctx->cb = nxt_perl_psgi_cb; PL_exit_flags |= PERL_EXIT_DESTRUCT_END; - PL_origalen = 1; + PL_origalen = 1; status = perl_run(my_perl); @@ -561,8 +556,8 @@ nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx) pctx->app = eval_pv(run_module, FALSE); if (SvTRUE(ERRSV)) { - nxt_unit_alert(NULL, "PSGI: Failed to parse script: %s\n%s", - script, SvPV_nolen(ERRSV)); + nxt_unit_alert(NULL, "PSGI: Failed to parse script: %s\n%s", script, + SvPV_nolen(ERRSV)); goto fail; } @@ -587,17 +582,15 @@ nxt_perl_psgi_ctx_init(const char *script, nxt_perl_psgi_ctx_t *pctx) return NXT_UNIT_ERROR; } - static SV * nxt_perl_psgi_env_create(PerlInterpreter *my_perl, - nxt_unit_request_info_t *req) -{ - HV *hash_env; - AV *array_version; + nxt_unit_request_info_t *req) { + HV *hash_env; + AV *array_version; uint32_t i; - nxt_unit_field_t *f; - nxt_unit_request_t *r; - nxt_perl_psgi_ctx_t *pctx; + nxt_unit_field_t *f; + nxt_unit_request_t *r; + nxt_perl_psgi_ctx_t *pctx; pctx = req->ctx->data; @@ -606,25 +599,25 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl, return NULL; } -#define RC(FNS) \ - do { \ - if (nxt_slow_path((FNS) != NXT_UNIT_OK)) \ - goto fail; \ +#define RC(FNS) \ + do { \ + if (nxt_slow_path((FNS) != NXT_UNIT_OK)) \ + goto fail; \ } while (0) -#define NL(S) (S), sizeof(S)-1 +#define NL(S) (S), sizeof(S) - 1 r = req->request; RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_SOFTWARE"), - (char *) nxt_server.start, nxt_server.length)); + (char *) nxt_server.start, nxt_server.length)); RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("REQUEST_METHOD"), - &r->method, r->method_length)); - RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("REQUEST_URI"), - &r->target, r->target_length)); - RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("PATH_INFO"), - &r->path, r->path_length)); + &r->method, r->method_length)); + RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("REQUEST_URI"), &r->target, + r->target_length)); + RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("PATH_INFO"), &r->path, + r->path_length)); array_version = newAV(); @@ -636,65 +629,63 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl, av_push(array_version, newSViv(1)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.version"), - newRV_noinc((SV *) array_version))); + newRV_noinc((SV *) array_version))); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.url_scheme"), - r->tls ? newSVpv("https", 5) - : newSVpv("http", 4))); + r->tls ? newSVpv("https", 5) : newSVpv("http", 4))); RC(nxt_perl_psgi_io_init(my_perl, &pctx->arg_input, "r", req)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.input"), - SvREFCNT_inc(pctx->arg_input.io))); + SvREFCNT_inc(pctx->arg_input.io))); RC(nxt_perl_psgi_io_init(my_perl, &pctx->arg_error, "w", req)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.errors"), - SvREFCNT_inc(pctx->arg_error.io))); + SvREFCNT_inc(pctx->arg_error.io))); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.multithread"), - nxt_perl_psgi_ctxs != NULL - ? &PL_sv_yes : &PL_sv_no)); + nxt_perl_psgi_ctxs != NULL ? &PL_sv_yes : &PL_sv_no)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.multiprocess"), - &PL_sv_yes)); + &PL_sv_yes)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.run_once"), - &PL_sv_no)); + &PL_sv_no)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.nonblocking"), - &PL_sv_no)); + &PL_sv_no)); RC(nxt_perl_psgi_add_value(my_perl, hash_env, NL("psgi.streaming"), - &PL_sv_yes)); + &PL_sv_yes)); - RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("QUERY_STRING"), - &r->query, r->query_length)); + RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("QUERY_STRING"), &r->query, + r->query_length)); RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_PROTOCOL"), - &r->version, r->version_length)); - RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("REMOTE_ADDR"), - &r->remote, r->remote_length)); + &r->version, r->version_length)); + RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("REMOTE_ADDR"), &r->remote, + r->remote_length)); RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_ADDR"), - &r->local_addr, r->local_addr_length)); + &r->local_addr, r->local_addr_length)); RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("SERVER_NAME"), - &r->server_name, r->server_name_length)); + &r->server_name, r->server_name_length)); RC(nxt_perl_psgi_add_str(my_perl, hash_env, NL("SERVER_PORT"), "80", 2)); for (i = 0; i < r->fields_count; i++) { f = r->fields + i; RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, - nxt_unit_sptr_get(&f->name), f->name_length, - &f->value, f->value_length)); + nxt_unit_sptr_get(&f->name), f->name_length, &f->value, + f->value_length)); } if (r->content_length_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_length_field; RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("CONTENT_LENGTH"), - &f->value, f->value_length)); + &f->value, f->value_length)); } if (r->content_type_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_type_field; RC(nxt_perl_psgi_add_sptr(my_perl, hash_env, NL("CONTENT_TYPE"), - &f->value, f->value_length)); + &f->value, f->value_length)); } #undef NL @@ -709,24 +700,20 @@ nxt_perl_psgi_env_create(PerlInterpreter *my_perl, return NULL; } - nxt_inline int -nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env, - const char *name, uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len) -{ +nxt_perl_psgi_add_sptr(PerlInterpreter *my_perl, HV *hash_env, const char *name, + uint32_t name_len, nxt_unit_sptr_t *sptr, uint32_t len) { return nxt_perl_psgi_add_str(my_perl, hash_env, name, name_len, - nxt_unit_sptr_get(sptr), len); + nxt_unit_sptr_get(sptr), len); } - nxt_inline int -nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env, - const char *name, uint32_t name_len, const char *str, uint32_t len) -{ - SV **ha; +nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env, const char *name, + uint32_t name_len, const char *str, uint32_t len) { + SV **ha; - ha = hv_store(hash_env, name, (I32) name_len, - newSVpv(str, (STRLEN) len), 0); + ha = hv_store(hash_env, name, (I32) name_len, newSVpv(str, (STRLEN) len), + 0); if (nxt_slow_path(ha == NULL)) { return NXT_UNIT_ERROR; } @@ -734,12 +721,10 @@ nxt_perl_psgi_add_str(PerlInterpreter *my_perl, HV *hash_env, return NXT_UNIT_OK; } - nxt_inline int nxt_perl_psgi_add_value(PerlInterpreter *my_perl, HV *hash_env, - const char *name, uint32_t name_len, void *value) -{ - SV **ha; + const char *name, uint32_t name_len, void *value) { + SV **ha; ha = hv_store(hash_env, name, (I32) name_len, value, 0); if (nxt_slow_path(ha == NULL)) { @@ -749,16 +734,14 @@ nxt_perl_psgi_add_value(PerlInterpreter *my_perl, HV *hash_env, return NXT_UNIT_OK; } - static nxt_int_t -nxt_perl_psgi_result_status(PerlInterpreter *my_perl, SV *result) -{ - SV **sv_status; - AV *array; - u_char *space; - nxt_str_t status; +nxt_perl_psgi_result_status(PerlInterpreter *my_perl, SV *result) { + SV **sv_status; + AV *array; + u_char *space; + nxt_str_t status; - array = (AV *) SvRV(result); + array = (AV *) SvRV(result); sv_status = av_fetch(array, 0, 0); status.start = (u_char *) SvPV(*sv_status, status.length); @@ -771,31 +754,27 @@ nxt_perl_psgi_result_status(PerlInterpreter *my_perl, SV *result) return nxt_int_parse(status.start, status.length); } - static int nxt_perl_psgi_result_head(PerlInterpreter *my_perl, SV *sv_head, - nxt_unit_request_info_t *req, uint16_t status) -{ - AV *array_head; - SV **entry; - int rc; - long i, array_len; - char *name, *value; - STRLEN name_len, value_len; - uint32_t fields, size; - - if (nxt_slow_path(SvROK(sv_head) == 0 - || SvTYPE(SvRV(sv_head)) != SVt_PVAV)) - { - nxt_unit_req_error(req, - "PSGI: An unsupported format was received from " - "Perl Application for head part"); + nxt_unit_request_info_t *req, uint16_t status) { + AV *array_head; + SV **entry; + int rc; + long i, array_len; + char *name, *value; + STRLEN name_len, value_len; + uint32_t fields, size; + + if (nxt_slow_path( + SvROK(sv_head) == 0 || SvTYPE(SvRV(sv_head)) != SVt_PVAV)) { + nxt_unit_req_error(req, "PSGI: An unsupported format was received from " + "Perl Application for head part"); return NXT_UNIT_ERROR; } array_head = (AV *) SvRV(sv_head); - array_len = av_len(array_head); + array_len = av_len(array_head); if (array_len < 1) { return nxt_unit_response_init(req, status, 0, 0); @@ -803,26 +782,26 @@ nxt_perl_psgi_result_head(PerlInterpreter *my_perl, SV *sv_head, if (nxt_slow_path((array_len % 2) == 0)) { nxt_unit_req_error(req, "PSGI: Bad format for head from " - "Perl Application"); + "Perl Application"); return NXT_UNIT_ERROR; } fields = 0; - size = 0; + size = 0; for (i = 0; i <= array_len; i++) { entry = av_fetch(array_head, i, 0); if (nxt_fast_path(entry == NULL)) { nxt_unit_req_error(req, "PSGI: Failed to get head entry from " - "Perl Application"); + "Perl Application"); return NXT_UNIT_ERROR; } - value = SvPV(*entry, value_len); - size += value_len; + value = SvPV(*entry, value_len); + size += value_len; if ((i % 2) == 0) { fields++; @@ -836,7 +815,7 @@ nxt_perl_psgi_result_head(PerlInterpreter *my_perl, SV *sv_head, for (i = 0; i <= array_len; i += 2) { entry = av_fetch(array_head, i, 0); - name = SvPV(*entry, name_len); + name = SvPV(*entry, name_len); entry = av_fetch(array_head, i + 1, 0); value = SvPV(*entry, value_len); @@ -850,22 +829,19 @@ nxt_perl_psgi_result_head(PerlInterpreter *my_perl, SV *sv_head, return NXT_UNIT_OK; } - static int nxt_perl_psgi_result_body(PerlInterpreter *my_perl, SV *sv_body, - nxt_unit_request_info_t *req) -{ - SV **entry; - AV *body_array; - int rc; - long i; - nxt_str_t body; - - if (nxt_slow_path(SvROK(sv_body) == 0 - || SvTYPE(SvRV(sv_body)) != SVt_PVAV)) - { + nxt_unit_request_info_t *req) { + SV **entry; + AV *body_array; + int rc; + long i; + nxt_str_t body; + + if (nxt_slow_path( + SvROK(sv_body) == 0 || SvTYPE(SvRV(sv_body)) != SVt_PVAV)) { nxt_unit_req_error(req, "PSGI: An unsupported format was received from " - "Perl Application for a body part"); + "Perl Application for a body part"); return NXT_UNIT_ERROR; } @@ -873,12 +849,11 @@ nxt_perl_psgi_result_body(PerlInterpreter *my_perl, SV *sv_body, body_array = (AV *) SvRV(sv_body); for (i = 0; i <= av_len(body_array); i++) { - entry = av_fetch(body_array, i, 0); if (nxt_fast_path(entry == NULL)) { nxt_unit_req_error(req, "PSGI: Failed to get body entry from " - "Perl Application"); + "Perl Application"); return NXT_UNIT_ERROR; } @@ -893,7 +868,7 @@ nxt_perl_psgi_result_body(PerlInterpreter *my_perl, SV *sv_body, if (nxt_slow_path(rc != NXT_UNIT_OK)) { nxt_unit_req_error(req, "PSGI: Failed to write content from " - "Perl Application"); + "Perl Application"); return rc; } } @@ -901,15 +876,13 @@ nxt_perl_psgi_result_body(PerlInterpreter *my_perl, SV *sv_body, return NXT_UNIT_OK; } - static int nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, SV *sv_body, - nxt_unit_request_info_t *req) -{ - SV *data, *old_rs, *old_perl_rs; + nxt_unit_request_info_t *req) { + SV *data, *old_rs, *old_perl_rs; int rc; size_t len; - const char *body; + const char *body; /* * Servers should set the $/ special variable to the buffer size @@ -917,7 +890,7 @@ nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, SV *sv_body, * This is done by setting $/ with a reference to an integer ($/ = \8192). */ - old_rs = PL_rs; + old_rs = PL_rs; old_perl_rs = get_sv("/", GV_ADD); PL_rs = sv_2mortal(newRV_noinc(newSViv(nxt_unit_buf_min()))); @@ -926,7 +899,7 @@ nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, SV *sv_body, rc = NXT_UNIT_OK; - for ( ;; ) { + for (;;) { data = nxt_perl_psgi_call_method(my_perl, sv_body, "getline", req); if (nxt_slow_path(data == NULL)) { rc = NXT_UNIT_ERROR; @@ -952,31 +925,28 @@ nxt_perl_psgi_result_body_ref(PerlInterpreter *my_perl, SV *sv_body, if (nxt_slow_path(rc != NXT_UNIT_OK)) { nxt_unit_req_error(req, "PSGI: Failed to write content from " - "Perl Application"); + "Perl Application"); break; } }; - PL_rs = old_rs; + PL_rs = old_rs; sv_setsv(get_sv("/", GV_ADD), old_perl_rs); return rc; } - typedef struct { - PerlInterpreter *my_perl; - PerlIO *fp; + PerlInterpreter *my_perl; + PerlIO *fp; } nxt_perl_psgi_io_ctx_t; - static int nxt_perl_psgi_result_body_fh(PerlInterpreter *my_perl, SV *sv_body, - nxt_unit_request_info_t *req) -{ - IO *io; - nxt_unit_read_info_t read_info; - nxt_perl_psgi_io_ctx_t io_ctx; + nxt_unit_request_info_t *req) { + IO *io; + nxt_unit_read_info_t read_info; + nxt_perl_psgi_io_ctx_t io_ctx; io = GvIO(SvRV(sv_body)); @@ -985,22 +955,20 @@ nxt_perl_psgi_result_body_fh(PerlInterpreter *my_perl, SV *sv_body, } io_ctx.my_perl = my_perl; - io_ctx.fp = IoIFP(io); + io_ctx.fp = IoIFP(io); - read_info.read = nxt_perl_psgi_io_read; - read_info.eof = PerlIO_eof(io_ctx.fp); + read_info.read = nxt_perl_psgi_io_read; + read_info.eof = PerlIO_eof(io_ctx.fp); read_info.buf_size = 8192; - read_info.data = &io_ctx; + read_info.data = &io_ctx; return nxt_unit_response_write_cb(req, &read_info); } - static ssize_t -nxt_perl_psgi_io_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) -{ +nxt_perl_psgi_io_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) { ssize_t res; - nxt_perl_psgi_io_ctx_t *ctx; + nxt_perl_psgi_io_ctx_t *ctx; ctx = read_info->data; @@ -1013,23 +981,21 @@ nxt_perl_psgi_io_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) return res; } - static int nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, - nxt_unit_request_info_t *req) -{ - AV *array; - SV **sv_temp; - int rc; - long array_len; - nxt_int_t status; - - array = (AV *) SvRV(result); + nxt_unit_request_info_t *req) { + AV *array; + SV **sv_temp; + int rc; + long array_len; + nxt_int_t status; + + array = (AV *) SvRV(result); array_len = av_len(array); if (nxt_slow_path(array_len < 0)) { nxt_unit_req_error(req, - "PSGI: Invalid result format from Perl Application"); + "PSGI: Invalid result format from Perl Application"); return NXT_UNIT_ERROR; } @@ -1037,9 +1003,8 @@ nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, status = nxt_perl_psgi_result_status(my_perl, result); if (nxt_slow_path(status < 0)) { - nxt_unit_req_error(req, - "PSGI: An unexpected status was received " - "from Perl Application"); + nxt_unit_req_error(req, "PSGI: An unexpected status was received " + "from Perl Application"); return NXT_UNIT_ERROR; } @@ -1049,7 +1014,7 @@ nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, if (nxt_slow_path(sv_temp == NULL)) { nxt_unit_req_error(req, "PSGI: Failed to get head from " - "Perl ARRAY variable"); + "Perl ARRAY variable"); return NXT_UNIT_ERROR; } @@ -1070,9 +1035,8 @@ nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, sv_temp = av_fetch(array, 2, 0); if (nxt_slow_path(sv_temp == NULL || SvROK(*sv_temp) == FALSE)) { - nxt_unit_req_error(req, - "PSGI: Failed to get body from " - "Perl ARRAY variable"); + nxt_unit_req_error(req, "PSGI: Failed to get body from " + "Perl ARRAY variable"); return NXT_UNIT_ERROR; } @@ -1088,12 +1052,10 @@ nxt_perl_psgi_result_array(PerlInterpreter *my_perl, SV *result, return nxt_perl_psgi_result_body_ref(my_perl, *sv_temp, req); } - static void nxt_perl_psgi_result_cb(PerlInterpreter *my_perl, SV *result, - nxt_unit_request_info_t *req) -{ - nxt_perl_psgi_ctx_t *pctx; + nxt_unit_request_info_t *req) { + nxt_perl_psgi_ctx_t *pctx; dSP; @@ -1103,16 +1065,16 @@ nxt_perl_psgi_result_cb(PerlInterpreter *my_perl, SV *result, SAVETMPS; PUSHMARK(sp); - XPUSHs(newRV_noinc((SV*) pctx->cb)); + XPUSHs(newRV_noinc((SV *) pctx->cb)); PUTBACK; - call_sv(result, G_EVAL|G_SCALAR); + call_sv(result, G_EVAL | G_SCALAR); SPAGAIN; if (SvTRUE(ERRSV)) { nxt_unit_error(NULL, "PSGI: Failed to execute result callback: \n%s", - SvPV_nolen(ERRSV)); + SvPV_nolen(ERRSV)); nxt_perl_psgi_cb_request_done(pctx, NXT_UNIT_ERROR); } @@ -1122,24 +1084,22 @@ nxt_perl_psgi_result_cb(PerlInterpreter *my_perl, SV *result, LEAVE; } - static nxt_int_t -nxt_perl_psgi_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_perl_psgi_start(nxt_task_t *task, nxt_process_data_t *data) { int rc, pargc; - char **pargv, **penv; - nxt_unit_ctx_t *unit_ctx; + char **pargv, **penv; + nxt_unit_ctx_t *unit_ctx; nxt_unit_init_t perl_init; nxt_perl_psgi_ctx_t pctx; - nxt_perl_app_conf_t *c; - nxt_common_app_conf_t *common_conf; + nxt_perl_app_conf_t *c; + nxt_common_app_conf_t *common_conf; common_conf = data->app; - c = &common_conf->u.perl; + c = &common_conf->u.perl; pargc = 0; pargv = NULL; - penv = NULL; + penv = NULL; PERL_SYS_INIT3(&pargc, &pargv, &penv); @@ -1161,9 +1121,9 @@ nxt_perl_psgi_start(nxt_task_t *task, nxt_process_data_t *data) nxt_unit_default_init(task, &perl_init, common_conf); perl_init.callbacks.request_handler = nxt_perl_psgi_request_handler; - perl_init.callbacks.ready_handler = nxt_perl_psgi_ready_handler; - perl_init.data = c; - perl_init.ctx_data = &pctx; + perl_init.callbacks.ready_handler = nxt_perl_psgi_ready_handler; + perl_init.data = c; + perl_init.ctx_data = &pctx; unit_ctx = nxt_unit_init(&perl_init); if (nxt_slow_path(unit_ctx == NULL)) { @@ -1195,16 +1155,14 @@ nxt_perl_psgi_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - static void -nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) -{ - SV *env, *result; +nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) { + SV *env, *result; nxt_int_t rc; - PerlInterpreter *my_perl; - nxt_perl_psgi_ctx_t *pctx; + PerlInterpreter *my_perl; + nxt_perl_psgi_ctx_t *pctx; - pctx = req->ctx->data; + pctx = req->ctx->data; my_perl = pctx->my_perl; pctx->req = req; @@ -1217,7 +1175,7 @@ nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) env = nxt_perl_psgi_env_create(my_perl, req); if (nxt_slow_path(env == NULL)) { nxt_unit_req_error(req, - "PSGI: Failed to create 'env' for Perl Application"); + "PSGI: Failed to create 'env' for Perl Application"); nxt_unit_request_done(req, NXT_UNIT_ERROR); pctx->req = NULL; @@ -1225,11 +1183,9 @@ nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) } /* Call perl sub and get result as SV*. */ - result = nxt_perl_psgi_call_var_application(my_perl, env, pctx->app, - req); + result = nxt_perl_psgi_call_var_application(my_perl, env, pctx->app, req); if (nxt_fast_path(SvOK(result) != 0 && SvROK(result) != 0)) { - if (SvTYPE(SvRV(result)) == SVt_PVAV) { rc = nxt_perl_psgi_result_array(my_perl, result, req); nxt_unit_request_done(req, rc); @@ -1245,7 +1201,7 @@ nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) } nxt_unit_req_error(req, "PSGI: An unexpected response was received " - "from Perl Application"); + "from Perl Application"); nxt_unit_request_done(req, NXT_UNIT_ERROR); pctx->req = NULL; @@ -1256,14 +1212,12 @@ nxt_perl_psgi_request_handler(nxt_unit_request_info_t *req) SvREFCNT_dec(env); } - static int -nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx) -{ +nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx) { int res; uint32_t i; - nxt_perl_app_conf_t *c; - nxt_perl_psgi_ctx_t *pctx; + nxt_perl_app_conf_t *c; + nxt_perl_psgi_ctx_t *pctx; c = ctx->unit->data; @@ -1277,14 +1231,14 @@ nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx) pctx->ctx = ctx; res = pthread_create(&pctx->thread, nxt_perl_psgi_thread_attr, - nxt_perl_psgi_thread_func, pctx); + nxt_perl_psgi_thread_func, pctx); if (nxt_fast_path(res == 0)) { nxt_unit_debug(ctx, "thread #%d created", (int) (i + 1)); } else { nxt_unit_alert(ctx, "thread #%d create failed: %s (%d)", - (int) (i + 1), strerror(res), res); + (int) (i + 1), strerror(res), res); return NXT_UNIT_ERROR; } @@ -1293,12 +1247,10 @@ nxt_perl_psgi_ready_handler(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static void * -nxt_perl_psgi_thread_func(void *data) -{ - nxt_unit_ctx_t *ctx; - nxt_perl_psgi_ctx_t *pctx; +nxt_perl_psgi_thread_func(void *data) { + nxt_unit_ctx_t *ctx; + nxt_perl_psgi_ctx_t *pctx; pctx = data; @@ -1322,13 +1274,11 @@ nxt_perl_psgi_thread_func(void *data) return NULL; } - static int -nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) -{ - int rc; - uint32_t i; - static pthread_attr_t attr; +nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) { + int rc; + uint32_t i; + static pthread_attr_t attr; if (c->threads <= 1) { return NXT_UNIT_OK; @@ -1338,7 +1288,7 @@ nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) rc = pthread_attr_init(&attr); if (nxt_slow_path(rc != 0)) { nxt_unit_alert(NULL, "thread attr init failed: %s (%d)", - strerror(rc), rc); + strerror(rc), rc); return NXT_UNIT_ERROR; } @@ -1346,7 +1296,7 @@ nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) rc = pthread_attr_setstacksize(&attr, c->thread_stack_size); if (nxt_slow_path(rc != 0)) { nxt_unit_alert(NULL, "thread attr set stack size failed: %s (%d)", - strerror(rc), rc); + strerror(rc), rc); return NXT_UNIT_ERROR; } @@ -1354,14 +1304,14 @@ nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) nxt_perl_psgi_thread_attr = &attr; } - nxt_perl_psgi_ctxs = nxt_unit_malloc(NULL, sizeof(nxt_perl_psgi_ctx_t) - * (c->threads - 1)); + nxt_perl_psgi_ctxs + = nxt_unit_malloc(NULL, sizeof(nxt_perl_psgi_ctx_t) * (c->threads - 1)); if (nxt_slow_path(nxt_perl_psgi_ctxs == NULL)) { return NXT_UNIT_ERROR; } - memset(nxt_perl_psgi_ctxs, 0, sizeof(nxt_perl_psgi_ctx_t) - * (c->threads - 1)); + memset(nxt_perl_psgi_ctxs, 0, + sizeof(nxt_perl_psgi_ctx_t) * (c->threads - 1)); for (i = 0; i < c->threads - 1; i++) { rc = nxt_perl_psgi_ctx_init(c->script, &nxt_perl_psgi_ctxs[i]); @@ -1374,13 +1324,11 @@ nxt_perl_psgi_init_threads(nxt_perl_app_conf_t *c) return NXT_UNIT_OK; } - static void -nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, nxt_perl_app_conf_t *c) -{ +nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, nxt_perl_app_conf_t *c) { int res; uint32_t i; - nxt_perl_psgi_ctx_t *pctx; + nxt_perl_psgi_ctx_t *pctx; if (nxt_perl_psgi_ctxs == NULL) { return; @@ -1396,7 +1344,7 @@ nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, nxt_perl_app_conf_t *c) } else { nxt_unit_alert(ctx, "thread #%d join failed: %s (%d)", - (int) (i + 1), strerror(res), res); + (int) (i + 1), strerror(res), res); } } @@ -1407,11 +1355,9 @@ nxt_perl_psgi_join_threads(nxt_unit_ctx_t *ctx, nxt_perl_app_conf_t *c) nxt_unit_free(NULL, nxt_perl_psgi_ctxs); } - static void -nxt_perl_psgi_ctx_free(nxt_perl_psgi_ctx_t *pctx) -{ - PerlInterpreter *my_perl; +nxt_perl_psgi_ctx_free(nxt_perl_psgi_ctx_t *pctx) { + PerlInterpreter *my_perl; my_perl = pctx->my_perl; diff --git a/src/perl/nxt_perl_psgi_layer.c b/src/perl/nxt_perl_psgi_layer.c index 303e5f27e..fbaa8909a 100644 --- a/src/perl/nxt_perl_psgi_layer.c +++ b/src/perl/nxt_perl_psgi_layer.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Alexander Borisov * Copyright (C) NGINX, Inc. @@ -6,50 +5,63 @@ #include - typedef struct { - struct _PerlIO base; + struct _PerlIO base; - SV *var; + SV *var; } nxt_perl_psgi_layer_stream_t; +static IV +nxt_perl_psgi_layer_stream_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, + PerlIO_funcs *tab); +static IV +nxt_perl_psgi_layer_stream_popped(pTHX_ PerlIO *f); -static IV nxt_perl_psgi_layer_stream_pushed(pTHX_ PerlIO *f, const char *mode, - SV *arg, PerlIO_funcs *tab); -static IV nxt_perl_psgi_layer_stream_popped(pTHX_ PerlIO *f); - -static PerlIO *nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, - PerlIO_list_t *layers, IV n, - const char *mode, int fd, int imode, int perm, - PerlIO *f, int narg, SV **args); +static PerlIO * +nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, + IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, + SV **args); -static IV nxt_perl_psgi_layer_stream_close(pTHX_ PerlIO *f); +static IV +nxt_perl_psgi_layer_stream_close(pTHX_ PerlIO *f); -static SSize_t nxt_perl_psgi_layer_stream_read(pTHX_ PerlIO *f, - void *vbuf, Size_t count); -static SSize_t nxt_perl_psgi_layer_stream_write(pTHX_ PerlIO *f, - const void *vbuf, Size_t count); +static SSize_t +nxt_perl_psgi_layer_stream_read(pTHX_ PerlIO *f, void *vbuf, Size_t count); +static SSize_t +nxt_perl_psgi_layer_stream_write(pTHX_ PerlIO *f, const void *vbuf, + Size_t count); -static IV nxt_perl_psgi_layer_stream_fileno(pTHX_ PerlIO *f); -static IV nxt_perl_psgi_layer_stream_seek(pTHX_ PerlIO *f, - Off_t offset, int whence); -static Off_t nxt_perl_psgi_layer_stream_tell(pTHX_ PerlIO *f); -static IV nxt_perl_psgi_layer_stream_fill(pTHX_ PerlIO *f); -static IV nxt_perl_psgi_layer_stream_flush(pTHX_ PerlIO *f); +static IV +nxt_perl_psgi_layer_stream_fileno(pTHX_ PerlIO *f); +static IV +nxt_perl_psgi_layer_stream_seek(pTHX_ PerlIO *f, Off_t offset, int whence); +static Off_t +nxt_perl_psgi_layer_stream_tell(pTHX_ PerlIO *f); +static IV +nxt_perl_psgi_layer_stream_fill(pTHX_ PerlIO *f); +static IV +nxt_perl_psgi_layer_stream_flush(pTHX_ PerlIO *f); -static SV *nxt_perl_psgi_layer_stream_arg(pTHX_ PerlIO *f, - CLONE_PARAMS *param, int flags); +static SV * +nxt_perl_psgi_layer_stream_arg(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags); -static PerlIO *nxt_perl_psgi_layer_stream_dup(pTHX_ PerlIO *f, PerlIO *o, - CLONE_PARAMS *param, int flags); -static IV nxt_perl_psgi_layer_stream_eof(pTHX_ PerlIO *f); +static PerlIO * +nxt_perl_psgi_layer_stream_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, + int flags); +static IV +nxt_perl_psgi_layer_stream_eof(pTHX_ PerlIO *f); -static STDCHAR *nxt_perl_psgi_layer_stream_get_base(pTHX_ PerlIO *f); -static STDCHAR *nxt_perl_psgi_layer_stream_get_ptr(pTHX_ PerlIO *f); -static SSize_t nxt_perl_psgi_layer_stream_get_cnt(pTHX_ PerlIO *f); -static Size_t nxt_perl_psgi_layer_stream_buffersize(pTHX_ PerlIO *f); -static void nxt_perl_psgi_layer_stream_set_ptrcnt(pTHX_ PerlIO *f, - STDCHAR *ptr, SSize_t cnt); +static STDCHAR * +nxt_perl_psgi_layer_stream_get_base(pTHX_ PerlIO *f); +static STDCHAR * +nxt_perl_psgi_layer_stream_get_ptr(pTHX_ PerlIO *f); +static SSize_t +nxt_perl_psgi_layer_stream_get_cnt(pTHX_ PerlIO *f); +static Size_t +nxt_perl_psgi_layer_stream_buffersize(pTHX_ PerlIO *f); +static void +nxt_perl_psgi_layer_stream_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR *ptr, + SSize_t cnt); static PERLIO_FUNCS_DECL(PerlIO_NGINX_Unit) = { @@ -83,12 +95,10 @@ static PERLIO_FUNCS_DECL(PerlIO_NGINX_Unit) = { nxt_perl_psgi_layer_stream_set_ptrcnt, }; - static IV nxt_perl_psgi_layer_stream_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, - PerlIO_funcs *tab) -{ - nxt_perl_psgi_layer_stream_t *unit_stream; + PerlIO_funcs *tab) { + nxt_perl_psgi_layer_stream_t *unit_stream; unit_stream = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); @@ -99,12 +109,10 @@ nxt_perl_psgi_layer_stream_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, return PerlIOBase_pushed(aTHX_ f, mode, Nullsv, tab); } - static IV -nxt_perl_psgi_layer_stream_popped(pTHX_ PerlIO *f) -{ - nxt_perl_psgi_io_arg_t *arg; - nxt_perl_psgi_layer_stream_t *unit_stream; +nxt_perl_psgi_layer_stream_popped(pTHX_ PerlIO *f) { + nxt_perl_psgi_io_arg_t *arg; + nxt_perl_psgi_layer_stream_t *unit_stream; unit_stream = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); @@ -121,14 +129,11 @@ nxt_perl_psgi_layer_stream_popped(pTHX_ PerlIO *f) return 0; } - static PerlIO * -nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, - PerlIO_list_t *layers, IV n, - const char *mode, int fd, int imode, int perm, - PerlIO *f, int narg, SV **args) -{ - SV *arg; +nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers, + IV n, const char *mode, int fd, int imode, int perm, PerlIO *f, int narg, + SV **args) { + SV *arg; arg = (narg > 0) ? *args : PerlIOArg; @@ -137,7 +142,6 @@ nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, PERL_UNUSED_ARG(perm); if (SvROK(arg) || SvPOK(arg)) { - if (f == NULL) { f = PerlIO_allocate(aTHX); } @@ -154,32 +158,26 @@ nxt_perl_psgi_layer_stream_open(pTHX_ PerlIO_funcs *self, return NULL; } - static IV -nxt_perl_psgi_layer_stream_close(pTHX_ PerlIO *f) -{ - IV code; +nxt_perl_psgi_layer_stream_close(pTHX_ PerlIO *f) { + IV code; - code = PerlIOBase_close(aTHX_ f); + code = PerlIOBase_close(aTHX_ f); PerlIOBase(f)->flags &= ~(PERLIO_F_RDBUF | PERLIO_F_WRBUF); return code; } - static IV -nxt_perl_psgi_layer_stream_fileno(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_fileno(pTHX_ PerlIO *f) { PERL_UNUSED_ARG(f); return -1; } - static SSize_t -nxt_perl_psgi_layer_stream_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) -{ - nxt_perl_psgi_io_arg_t *arg; - nxt_perl_psgi_layer_stream_t *unit_stream; +nxt_perl_psgi_layer_stream_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) { + nxt_perl_psgi_io_arg_t *arg; + nxt_perl_psgi_layer_stream_t *unit_stream; if (f == NULL) { return 0; @@ -194,22 +192,20 @@ nxt_perl_psgi_layer_stream_read(pTHX_ PerlIO *f, void *vbuf, Size_t count) } unit_stream = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); - arg = (void *) (intptr_t) SvIV(SvRV(unit_stream->var)); + arg = (void *) (intptr_t) SvIV(SvRV(unit_stream->var)); return arg->io_tab->read(PERL_GET_CONTEXT, arg, vbuf, count); } - static SSize_t -nxt_perl_psgi_layer_stream_write(pTHX_ PerlIO *f, - const void *vbuf, Size_t count) -{ - nxt_perl_psgi_io_arg_t *arg; - nxt_perl_psgi_layer_stream_t *unit_stream; +nxt_perl_psgi_layer_stream_write(pTHX_ PerlIO *f, const void *vbuf, + Size_t count) { + nxt_perl_psgi_io_arg_t *arg; + nxt_perl_psgi_layer_stream_t *unit_stream; if (PerlIOBase(f)->flags & PERLIO_F_CANWRITE) { unit_stream = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); - arg = (void *) (intptr_t) SvIV(SvRV(unit_stream->var)); + arg = (void *) (intptr_t) SvIV(SvRV(unit_stream->var)); return arg->io_tab->write(PERL_GET_CONTEXT, arg, vbuf, count); } @@ -217,47 +213,37 @@ nxt_perl_psgi_layer_stream_write(pTHX_ PerlIO *f, return 0; } - static IV -nxt_perl_psgi_layer_stream_seek(pTHX_ PerlIO *f, Off_t offset, int whence) -{ +nxt_perl_psgi_layer_stream_seek(pTHX_ PerlIO *f, Off_t offset, int whence) { PERL_UNUSED_ARG(f); return 0; } - static Off_t -nxt_perl_psgi_layer_stream_tell(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_tell(pTHX_ PerlIO *f) { PERL_UNUSED_ARG(f); return 0; } - static IV -nxt_perl_psgi_layer_stream_fill(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_fill(pTHX_ PerlIO *f) { PERL_UNUSED_ARG(f); return -1; } - static IV -nxt_perl_psgi_layer_stream_flush(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_flush(pTHX_ PerlIO *f) { return 0; } - static SV * -nxt_perl_psgi_layer_stream_arg(pTHX_ PerlIO * f, - CLONE_PARAMS *param, int flags) -{ - SV *var; - nxt_perl_psgi_layer_stream_t *unit_stream; +nxt_perl_psgi_layer_stream_arg(pTHX_ PerlIO *f, CLONE_PARAMS *param, + int flags) { + SV *var; + nxt_perl_psgi_layer_stream_t *unit_stream; unit_stream = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); - var = unit_stream->var; + var = unit_stream->var; if (flags & PERLIO_DUP_CLONE) { var = PerlIO_sv_dup(aTHX_ var, param); @@ -272,95 +258,72 @@ nxt_perl_psgi_layer_stream_arg(pTHX_ PerlIO * f, return var; } - static PerlIO * -nxt_perl_psgi_layer_stream_dup(pTHX_ PerlIO *f, PerlIO *o, - CLONE_PARAMS *param, int flags) -{ - nxt_perl_psgi_layer_stream_t *fs; +nxt_perl_psgi_layer_stream_dup(pTHX_ PerlIO *f, PerlIO *o, CLONE_PARAMS *param, + int flags) { + nxt_perl_psgi_layer_stream_t *fs; f = PerlIOBase_dup(aTHX_ f, o, param, flags); if (f != NULL) { - fs = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); + fs = PerlIOSelf(f, nxt_perl_psgi_layer_stream_t); fs->var = nxt_perl_psgi_layer_stream_arg(aTHX_ o, param, flags); } return f; } - static IV -nxt_perl_psgi_layer_stream_eof(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_eof(pTHX_ PerlIO *f) { return 1; } - static STDCHAR * -nxt_perl_psgi_layer_stream_get_base(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_get_base(pTHX_ PerlIO *f) { return (STDCHAR *) NULL; } - static STDCHAR * -nxt_perl_psgi_layer_stream_get_ptr(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_get_ptr(pTHX_ PerlIO *f) { return (STDCHAR *) NULL; } - static SSize_t -nxt_perl_psgi_layer_stream_get_cnt(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_get_cnt(pTHX_ PerlIO *f) { return 0; } - static Size_t -nxt_perl_psgi_layer_stream_buffersize(pTHX_ PerlIO *f) -{ +nxt_perl_psgi_layer_stream_buffersize(pTHX_ PerlIO *f) { return 0; } - static void -nxt_perl_psgi_layer_stream_set_ptrcnt(pTHX_ PerlIO *f, - STDCHAR *ptr, SSize_t cnt) -{ +nxt_perl_psgi_layer_stream_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR *ptr, + SSize_t cnt) { /* Need some code. */ } - void -nxt_perl_psgi_layer_stream_init(pTHX) -{ +nxt_perl_psgi_layer_stream_init(pTHX) { PerlIO_define_layer(aTHX_ PERLIO_FUNCS_CAST(&PerlIO_NGINX_Unit)); } - PerlIO * -nxt_perl_psgi_layer_stream_fp_create(pTHX_ SV *arg_rv, - const char *mode) -{ - return PerlIO_openn(aTHX_ "NGINX_Unit_PSGI_Layer_Stream", - mode, 0, 0, 0, NULL, 1, &arg_rv); +nxt_perl_psgi_layer_stream_fp_create(pTHX_ SV *arg_rv, const char *mode) { + return PerlIO_openn(aTHX_ "NGINX_Unit_PSGI_Layer_Stream", mode, 0, 0, 0, + NULL, 1, &arg_rv); } - void -nxt_perl_psgi_layer_stream_fp_destroy(pTHX_ PerlIO *io) -{ +nxt_perl_psgi_layer_stream_fp_destroy(pTHX_ PerlIO *io) { PerlIO_close(io); } - SV * -nxt_perl_psgi_layer_stream_io_create(pTHX_ PerlIO *fp) -{ - SV *rvio; - IO *thatio; +nxt_perl_psgi_layer_stream_io_create(pTHX_ PerlIO *fp) { + SV *rvio; + IO *thatio; thatio = newIO(); diff --git a/src/perl/nxt_perl_psgi_layer.h b/src/perl/nxt_perl_psgi_layer.h index 0972d66f1..bea2d177c 100644 --- a/src/perl/nxt_perl_psgi_layer.h +++ b/src/perl/nxt_perl_psgi_layer.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Alexander Borisov * Copyright (C) NGINX, Inc. @@ -17,32 +16,31 @@ typedef struct nxt_perl_psgi_io_tab_s nxt_perl_psgi_io_tab_t; typedef struct nxt_perl_psgi_io_arg_s nxt_perl_psgi_io_arg_t; - struct nxt_perl_psgi_io_tab_s { - SSize_t (*read)(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, void *vbuf, size_t length); - SSize_t (*write)(PerlInterpreter *my_perl, - nxt_perl_psgi_io_arg_t *arg, const void *vbuf, size_t length); + SSize_t (*read)(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, + void *vbuf, size_t length); + SSize_t (*write)(PerlInterpreter *my_perl, nxt_perl_psgi_io_arg_t *arg, + const void *vbuf, size_t length); }; - struct nxt_perl_psgi_io_arg_s { - SV *rv; - SV *io; - PerlIO *fp; + SV *rv; + SV *io; + PerlIO *fp; - const nxt_perl_psgi_io_tab_t *io_tab; + const nxt_perl_psgi_io_tab_t *io_tab; - void *req; + void *req; }; - void nxt_perl_psgi_layer_stream_init(pTHX); -PerlIO *nxt_perl_psgi_layer_stream_fp_create(pTHX_ SV *arg_rv, - const char *mode); -void nxt_perl_psgi_layer_stream_fp_destroy(pTHX_ PerlIO *io); +PerlIO * +nxt_perl_psgi_layer_stream_fp_create(pTHX_ SV *arg_rv, const char *mode); +void +nxt_perl_psgi_layer_stream_fp_destroy(pTHX_ PerlIO *io); -SV *nxt_perl_psgi_layer_stream_io_create(pTHX_ PerlIO *fp); +SV * +nxt_perl_psgi_layer_stream_io_create(pTHX_ PerlIO *fp); #endif /* _NXT_PERL_PSGI_LAYER_H_INCLUDED_ */ diff --git a/src/python/nxt_python.c b/src/python/nxt_python.c index 143d8d5dc..0265d0ef0 100644 --- a/src/python/nxt_python.c +++ b/src/python/nxt_python.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -14,38 +13,46 @@ #include NXT_PYTHON_MOUNTS_H - typedef struct { pthread_t thread; - nxt_unit_ctx_t *ctx; - void *ctx_data; + nxt_unit_ctx_t *ctx; + void *ctx_data; } nxt_py_thread_info_t; #if PY_MAJOR_VERSION == 3 -static nxt_int_t nxt_python3_init_config(nxt_int_t pep405); +static nxt_int_t +nxt_python3_init_config(nxt_int_t pep405); #endif -static nxt_int_t nxt_python_start(nxt_task_t *task, - nxt_process_data_t *data); -static nxt_int_t nxt_python_set_target(nxt_task_t *task, - nxt_python_target_t *target, nxt_conf_value_t *conf); -nxt_inline nxt_int_t nxt_python_set_prefix(nxt_task_t *task, - nxt_python_target_t *target, nxt_conf_value_t *value); -static nxt_int_t nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value); -static int nxt_python_init_threads(nxt_python_app_conf_t *c); -static int nxt_python_ready_handler(nxt_unit_ctx_t *ctx); -static void *nxt_python_thread_func(void *main_ctx); -static void nxt_python_join_threads(nxt_unit_ctx_t *ctx, - nxt_python_app_conf_t *c); -static void nxt_python_atexit(void); - -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static nxt_int_t +nxt_python_start(nxt_task_t *task, nxt_process_data_t *data); +static nxt_int_t +nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, + nxt_conf_value_t *conf); +nxt_inline nxt_int_t +nxt_python_set_prefix(nxt_task_t *task, nxt_python_target_t *target, + nxt_conf_value_t *value); +static nxt_int_t +nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value); +static int +nxt_python_init_threads(nxt_python_app_conf_t *c); +static int +nxt_python_ready_handler(nxt_unit_ctx_t *ctx); +static void * +nxt_python_thread_func(void *main_ctx); +static void +nxt_python_join_threads(nxt_unit_ctx_t *ctx, nxt_python_app_conf_t *c); +static void +nxt_python_atexit(void); + +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -NXT_EXPORT nxt_app_module_t nxt_app_module = { +NXT_EXPORT nxt_app_module_t nxt_app_module = { sizeof(compat), compat, nxt_string("python"), @@ -56,29 +63,28 @@ NXT_EXPORT nxt_app_module_t nxt_app_module = { nxt_python_start, }; -static PyObject *nxt_py_stderr_flush; -nxt_python_targets_t *nxt_py_targets; +static PyObject *nxt_py_stderr_flush; +nxt_python_targets_t *nxt_py_targets; #if PY_MAJOR_VERSION == 3 -static wchar_t *nxt_py_home; +static wchar_t *nxt_py_home; #else -static char *nxt_py_home; +static char *nxt_py_home; #endif -static pthread_attr_t *nxt_py_thread_attr; -static nxt_py_thread_info_t *nxt_py_threads; +static pthread_attr_t *nxt_py_thread_attr; +static nxt_py_thread_info_t *nxt_py_threads; static nxt_python_proto_t nxt_py_proto; #if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 8) static nxt_int_t -nxt_python3_init_config(nxt_int_t pep405) -{ - PyConfig config; - PyStatus status; - nxt_int_t ret; - PyPreConfig preconfig; +nxt_python3_init_config(nxt_int_t pep405) { + PyConfig config; + PyStatus status; + nxt_int_t ret; + PyPreConfig preconfig; ret = NXT_ERROR; @@ -98,8 +104,7 @@ nxt_python3_init_config(nxt_int_t pep405) PyConfig_InitIsolatedConfig(&config); if (pep405) { - status = PyConfig_SetString(&config, &config.program_name, - nxt_py_home); + status = PyConfig_SetString(&config, &config.program_name, nxt_py_home); if (PyStatus_Exception(status)) { goto out_config_clear; } @@ -128,8 +133,7 @@ nxt_python3_init_config(nxt_int_t pep405) #elif PY_MAJOR_VERSION == 3 static nxt_int_t -nxt_python3_init_config(nxt_int_t pep405) -{ +nxt_python3_init_config(nxt_int_t pep405) { if (pep405) { Py_SetProgramName(nxt_py_home); @@ -144,33 +148,32 @@ nxt_python3_init_config(nxt_int_t pep405) static nxt_int_t -nxt_python_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_python_start(nxt_task_t *task, nxt_process_data_t *data) { int rc; size_t len, size; uint32_t next; - PyObject *obj; + PyObject *obj; nxt_str_t proto, probe_proto, name; nxt_int_t ret, n, i; - nxt_unit_ctx_t *unit_ctx; + nxt_unit_ctx_t *unit_ctx; nxt_unit_init_t python_init; - nxt_conf_value_t *cv; - nxt_python_targets_t *targets; - nxt_common_app_conf_t *app_conf; - nxt_python_app_conf_t *c; + nxt_conf_value_t *cv; + nxt_python_targets_t *targets; + nxt_common_app_conf_t *app_conf; + nxt_python_app_conf_t *c; #if PY_MAJOR_VERSION == 3 - char *path; - nxt_int_t pep405; + char *path; + nxt_int_t pep405; - static const char pyvenv[] = "/pyvenv.cfg"; + static const char pyvenv[] = "/pyvenv.cfg"; static const char bin_python[] = "/bin/python"; #endif - static const nxt_str_t wsgi = nxt_string("wsgi"); - static const nxt_str_t asgi = nxt_string("asgi"); + static const nxt_str_t wsgi = nxt_string("wsgi"); + static const nxt_str_t asgi = nxt_string("asgi"); app_conf = data->app; - c = &app_conf->u.python; + c = &app_conf->u.python; if (c->home != NULL) { len = nxt_strlen(c->home); @@ -314,18 +317,19 @@ nxt_python_start(nxt_task_t *task, nxt_process_data_t *data) nxt_unit_default_init(task, &python_init, data->app); - python_init.data = c; + python_init.data = c; python_init.callbacks.ready_handler = nxt_python_ready_handler; proto = c->protocol; if (proto.length == 0) { - proto = nxt_python_asgi_check(targets->target[0].application) - ? asgi : wsgi; + proto = nxt_python_asgi_check(targets->target[0].application) ? asgi + : wsgi; for (i = 1; i < targets->count; i++) { probe_proto = nxt_python_asgi_check(targets->target[i].application) - ? asgi : wsgi; + ? asgi + : wsgi; if (probe_proto.start != proto.start) { nxt_alert(task, "A mix of ASGI & WSGI targets is forbidden, " "specify protocol in config if incorrect"); @@ -395,21 +399,19 @@ nxt_python_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - static nxt_int_t nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, - nxt_conf_value_t *conf) -{ - char *callable, *module_name; - PyObject *module, *obj; + nxt_conf_value_t *conf) { + char *callable, *module_name; + PyObject *module, *obj; nxt_str_t str; nxt_bool_t is_factory = 0; - nxt_conf_value_t *value; + nxt_conf_value_t *value; - static const nxt_str_t module_str = nxt_string("module"); - static const nxt_str_t callable_str = nxt_string("callable"); - static const nxt_str_t prefix_str = nxt_string("prefix"); - static const nxt_str_t factory_flag_str = nxt_string("factory"); + static const nxt_str_t module_str = nxt_string("module"); + static const nxt_str_t callable_str = nxt_string("callable"); + static const nxt_str_t prefix_str = nxt_string("prefix"); + static const nxt_str_t factory_flag_str = nxt_string("factory"); module = obj = NULL; @@ -447,7 +449,7 @@ nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, obj = PyDict_GetItemString(PyModule_GetDict(module), callable); if (nxt_slow_path(obj == NULL)) { nxt_alert(task, "Python failed to get \"%s\" from module \"%s\"", - callable, module_name); + callable, module_name); goto fail; } @@ -459,25 +461,25 @@ nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, if (is_factory) { if (nxt_slow_path(PyCallable_Check(obj) == 0)) { nxt_alert(task, - "factory \"%s\" in module \"%s\" " - "can not be called to fetch callable", - callable, module_name); - Py_INCREF(obj); /* borrowed reference */ + "factory \"%s\" in module \"%s\" " + "can not be called to fetch callable", + callable, module_name); + Py_INCREF(obj); /* borrowed reference */ goto fail; } obj = PyObject_CallObject(obj, NULL); if (nxt_slow_path(PyCallable_Check(obj) == 0)) { nxt_alert(task, - "factory \"%s\" in module \"%s\" " - "did not return callable object", - callable, module_name); + "factory \"%s\" in module \"%s\" " + "did not return callable object", + callable, module_name); goto fail; } } else if (nxt_slow_path(PyCallable_Check(obj) == 0)) { nxt_alert(task, "\"%s\" in module \"%s\" is not a callable object", - callable, module_name); + callable, module_name); goto fail; } @@ -487,7 +489,7 @@ nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, } target->application = obj; - obj = NULL; + obj = NULL; Py_INCREF(target->application); Py_CLEAR(module); @@ -502,13 +504,11 @@ nxt_python_set_target(nxt_task_t *task, nxt_python_target_t *target, return NXT_ERROR; } - nxt_inline nxt_int_t nxt_python_set_prefix(nxt_task_t *task, nxt_python_target_t *target, - nxt_conf_value_t *value) -{ - u_char *prefix; - nxt_str_t str; + nxt_conf_value_t *value) { + u_char *prefix; + nxt_str_t str; if (value == NULL) { return NXT_OK; @@ -524,14 +524,14 @@ nxt_python_set_prefix(nxt_task_t *task, nxt_python_target_t *target, str.length--; } target->prefix.length = str.length; - prefix = nxt_malloc(str.length); + prefix = nxt_malloc(str.length); if (nxt_slow_path(prefix == NULL)) { nxt_alert(task, "Failed to allocate target prefix string"); return NXT_ERROR; } - target->py_prefix = PyString_FromStringAndSize((char *)str.start, - str.length); + target->py_prefix + = PyString_FromStringAndSize((char *) str.start, str.length); if (nxt_slow_path(target->py_prefix == NULL)) { nxt_free(prefix); nxt_alert(task, "Python failed to allocate target prefix " @@ -544,15 +544,13 @@ nxt_python_set_prefix(nxt_task_t *task, nxt_python_target_t *target, return NXT_OK; } - static nxt_int_t -nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) -{ +nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) { int ret; - PyObject *path, *sys; + PyObject *path, *sys; nxt_str_t str; nxt_uint_t n; - nxt_conf_value_t *array; + nxt_conf_value_t *array; if (value == NULL) { return NXT_OK; @@ -567,7 +565,7 @@ nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) /* sys is a Borrowed reference. */ array = value; - n = nxt_conf_array_elements_count_or_1(array); + n = nxt_conf_array_elements_count_or_1(array); while (n != 0) { n--; @@ -585,7 +583,7 @@ nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) path = PyString_FromStringAndSize((char *) str.start, str.length); if (nxt_slow_path(path == NULL)) { nxt_alert(task, "Python failed to create string object \"%V\"", - &str); + &str); return NXT_ERROR; } @@ -595,7 +593,7 @@ nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) if (nxt_slow_path(ret != 0)) { nxt_alert(task, "Python failed to insert \"%V\" into \"sys.path\"", - &str); + &str); return NXT_ERROR; } } @@ -603,14 +601,12 @@ nxt_python_set_path(nxt_task_t *task, nxt_conf_value_t *value) return NXT_OK; } - static int -nxt_python_init_threads(nxt_python_app_conf_t *c) -{ - int res; - uint32_t i; - nxt_py_thread_info_t *ti; - static pthread_attr_t attr; +nxt_python_init_threads(nxt_python_app_conf_t *c) { + int res; + uint32_t i; + nxt_py_thread_info_t *ti; + static pthread_attr_t attr; if (c->threads <= 1) { return NXT_UNIT_OK; @@ -620,7 +616,7 @@ nxt_python_init_threads(nxt_python_app_conf_t *c) res = pthread_attr_init(&attr); if (nxt_slow_path(res != 0)) { nxt_unit_alert(NULL, "thread attr init failed: %s (%d)", - strerror(res), res); + strerror(res), res); return NXT_UNIT_ERROR; } @@ -628,7 +624,7 @@ nxt_python_init_threads(nxt_python_app_conf_t *c) res = pthread_attr_setstacksize(&attr, c->thread_stack_size); if (nxt_slow_path(res != 0)) { nxt_unit_alert(NULL, "thread attr set stack size failed: %s (%d)", - strerror(res), res); + strerror(res), res); return NXT_UNIT_ERROR; } @@ -636,8 +632,8 @@ nxt_python_init_threads(nxt_python_app_conf_t *c) nxt_py_thread_attr = &attr; } - nxt_py_threads = nxt_unit_malloc(NULL, sizeof(nxt_py_thread_info_t) - * (c->threads - 1)); + nxt_py_threads = nxt_unit_malloc(NULL, + sizeof(nxt_py_thread_info_t) * (c->threads - 1)); if (nxt_slow_path(nxt_py_threads == NULL)) { nxt_unit_alert(NULL, "Failed to allocate thread info array"); @@ -658,14 +654,12 @@ nxt_python_init_threads(nxt_python_app_conf_t *c) return NXT_UNIT_OK; } - static int -nxt_python_ready_handler(nxt_unit_ctx_t *ctx) -{ +nxt_python_ready_handler(nxt_unit_ctx_t *ctx) { int res; uint32_t i; - nxt_py_thread_info_t *ti; - nxt_python_app_conf_t *c; + nxt_py_thread_info_t *ti; + nxt_python_app_conf_t *c; c = ctx->unit->data; @@ -679,32 +673,30 @@ nxt_python_ready_handler(nxt_unit_ctx_t *ctx) ti->ctx = ctx; res = pthread_create(&ti->thread, nxt_py_thread_attr, - nxt_python_thread_func, ti); + nxt_python_thread_func, ti); if (nxt_fast_path(res == 0)) { nxt_unit_debug(ctx, "thread #%d created", (int) (i + 1)); } else { nxt_unit_alert(ctx, "thread #%d create failed: %s (%d)", - (int) (i + 1), strerror(res), res); + (int) (i + 1), strerror(res), res); } } return NXT_UNIT_OK; } - static void * -nxt_python_thread_func(void *data) -{ - nxt_unit_ctx_t *ctx; +nxt_python_thread_func(void *data) { + nxt_unit_ctx_t *ctx; PyGILState_STATE gstate; - nxt_py_thread_info_t *ti; + nxt_py_thread_info_t *ti; ti = data; nxt_unit_debug(ti->ctx, "worker thread #%d start", - (int) (ti - nxt_py_threads + 1)); + (int) (ti - nxt_py_threads + 1)); gstate = PyGILState_Ensure(); @@ -728,19 +720,17 @@ nxt_python_thread_func(void *data) PyGILState_Release(gstate); nxt_unit_debug(NULL, "worker thread #%d end", - (int) (ti - nxt_py_threads + 1)); + (int) (ti - nxt_py_threads + 1)); return NULL; } - static void -nxt_python_join_threads(nxt_unit_ctx_t *ctx, nxt_python_app_conf_t *c) -{ +nxt_python_join_threads(nxt_unit_ctx_t *ctx, nxt_python_app_conf_t *c) { int res; uint32_t i; - PyThreadState *thread_state; - nxt_py_thread_info_t *ti; + PyThreadState *thread_state; + nxt_py_thread_info_t *ti; if (nxt_py_threads == NULL) { return; @@ -762,7 +752,7 @@ nxt_python_join_threads(nxt_unit_ctx_t *ctx, nxt_python_app_conf_t *c) } else { nxt_unit_alert(ctx, "thread #%d join failed: %s (%d)", - (int) (i + 1), strerror(res), res); + (int) (i + 1), strerror(res), res); } } @@ -779,15 +769,13 @@ nxt_python_join_threads(nxt_unit_ctx_t *ctx, nxt_python_app_conf_t *c) nxt_unit_free(NULL, nxt_py_threads); } - int -nxt_python_init_strings(nxt_python_string_t *pstr) -{ - PyObject *obj; +nxt_python_init_strings(nxt_python_string_t *pstr) { + PyObject *obj; while (pstr->string.start != NULL) { obj = PyString_FromStringAndSize((char *) pstr->string.start, - pstr->string.length); + pstr->string.length); if (nxt_slow_path(obj == NULL)) { return NXT_UNIT_ERROR; } @@ -802,11 +790,9 @@ nxt_python_init_strings(nxt_python_string_t *pstr) return NXT_UNIT_OK; } - void -nxt_python_done_strings(nxt_python_string_t *pstr) -{ - PyObject *obj; +nxt_python_done_strings(nxt_python_string_t *pstr) { + PyObject *obj; while (pstr->string.start != NULL) { obj = *pstr->object_p; @@ -818,12 +804,10 @@ nxt_python_done_strings(nxt_python_string_t *pstr) } } - static void -nxt_python_atexit(void) -{ +nxt_python_atexit(void) { nxt_int_t i; - nxt_python_target_t *target; + nxt_python_target_t *target; if (nxt_py_proto.done != NULL) { nxt_py_proto.done(); @@ -851,16 +835,14 @@ nxt_python_atexit(void) } } - void -nxt_python_print_exception(void) -{ +nxt_python_print_exception(void) { PyErr_Print(); #if PY_MAJOR_VERSION == 3 /* The backtrace may be buffered in sys.stderr file object. */ { - PyObject *result; + PyObject *result; result = PyObject_CallFunction(nxt_py_stderr_flush, NULL); if (nxt_slow_path(result == NULL)) { diff --git a/src/python/nxt_python.h b/src/python/nxt_python.h index f51545142..9dc9d01e2 100644 --- a/src/python/nxt_python.h +++ b/src/python/nxt_python.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,74 +10,75 @@ #include #include -#define NXT_PYTHON_VER(maj, min) ((maj << 24) | (min << 16)) +#define NXT_PYTHON_VER(maj, min) ((maj << 24) | (min << 16)) #if PY_MAJOR_VERSION == 3 -#define NXT_PYTHON_BYTES_TYPE "bytestring" +#define NXT_PYTHON_BYTES_TYPE "bytestring" -#define PyString_FromStringAndSize(str, size) \ - PyUnicode_DecodeLatin1((str), (size), "strict") -#define PyString_AS_STRING PyUnicode_DATA +#define PyString_FromStringAndSize(str, size) \ + PyUnicode_DecodeLatin1((str), (size), "strict") +#define PyString_AS_STRING PyUnicode_DATA #else -#define NXT_PYTHON_BYTES_TYPE "string" - -#define PyBytes_FromStringAndSize PyString_FromStringAndSize -#define PyBytes_Check PyString_Check -#define PyBytes_GET_SIZE PyString_GET_SIZE -#define PyBytes_AS_STRING PyString_AS_STRING -#define PyUnicode_InternInPlace PyString_InternInPlace -#define PyUnicode_AsUTF8 PyString_AS_STRING -#define PyUnicode_GET_LENGTH PyUnicode_GET_SIZE +#define NXT_PYTHON_BYTES_TYPE "string" + +#define PyBytes_FromStringAndSize PyString_FromStringAndSize +#define PyBytes_Check PyString_Check +#define PyBytes_GET_SIZE PyString_GET_SIZE +#define PyBytes_AS_STRING PyString_AS_STRING +#define PyUnicode_InternInPlace PyString_InternInPlace +#define PyUnicode_AsUTF8 PyString_AS_STRING +#define PyUnicode_GET_LENGTH PyUnicode_GET_SIZE #endif #if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 5) -#define NXT_HAVE_ASGI 1 +#define NXT_HAVE_ASGI 1 #endif typedef struct { - PyObject *application; - PyObject *py_prefix; - nxt_str_t prefix; - nxt_bool_t asgi_legacy; + PyObject *application; + PyObject *py_prefix; + nxt_str_t prefix; + nxt_bool_t asgi_legacy; } nxt_python_target_t; - typedef struct { - nxt_int_t count; - nxt_python_target_t target[]; + nxt_int_t count; + nxt_python_target_t target[]; } nxt_python_targets_t; - -extern nxt_python_targets_t *nxt_py_targets; - +extern nxt_python_targets_t *nxt_py_targets; typedef struct { nxt_str_t string; - PyObject **object_p; + PyObject **object_p; } nxt_python_string_t; - typedef struct { - int (*ctx_data_alloc)(void **pdata, int main); - void (*ctx_data_free)(void *data); - int (*startup)(void *data); - int (*run)(nxt_unit_ctx_t *ctx); - void (*done)(void); + int (*ctx_data_alloc)(void **pdata, int main); + void (*ctx_data_free)(void *data); + int (*startup)(void *data); + int (*run)(nxt_unit_ctx_t *ctx); + void (*done)(void); } nxt_python_proto_t; +int +nxt_python_init_strings(nxt_python_string_t *pstr); +void +nxt_python_done_strings(nxt_python_string_t *pstr); -int nxt_python_init_strings(nxt_python_string_t *pstr); -void nxt_python_done_strings(nxt_python_string_t *pstr); - -void nxt_python_print_exception(void); +void +nxt_python_print_exception(void); -int nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); +int +nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); -int nxt_python_asgi_check(PyObject *obj); -int nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); +int +nxt_python_asgi_check(PyObject *obj); +int +nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto); -#endif /* _NXT_PYTHON_H_INCLUDED_ */ +#endif /* _NXT_PYTHON_H_INCLUDED_ */ diff --git a/src/python/nxt_python_asgi.c b/src/python/nxt_python_asgi.c index 8f300b53a..b9aaf1c07 100644 --- a/src/python/nxt_python_asgi.c +++ b/src/python/nxt_python_asgi.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -16,44 +15,62 @@ #include -static PyObject *nxt_python_asgi_get_func(PyObject *obj); -static PyObject *nxt_python_asgi_get_event_loop(PyObject *asyncio, - const char *event_loop_func); -static int nxt_python_asgi_ctx_data_alloc(void **pdata, int main); -static void nxt_python_asgi_ctx_data_free(void *data); -static int nxt_python_asgi_startup(void *data); -static int nxt_python_asgi_run(nxt_unit_ctx_t *ctx); +static PyObject * +nxt_python_asgi_get_func(PyObject *obj); +static PyObject * +nxt_python_asgi_get_event_loop(PyObject *asyncio, const char *event_loop_func); +static int +nxt_python_asgi_ctx_data_alloc(void **pdata, int main); +static void +nxt_python_asgi_ctx_data_free(void *data); +static int +nxt_python_asgi_startup(void *data); +static int +nxt_python_asgi_run(nxt_unit_ctx_t *ctx); -static void nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port); -static void nxt_py_asgi_request_handler(nxt_unit_request_info_t *req); -static void nxt_py_asgi_close_handler(nxt_unit_request_info_t *req); +static void +nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +static void +nxt_py_asgi_request_handler(nxt_unit_request_info_t *req); +static void +nxt_py_asgi_close_handler(nxt_unit_request_info_t *req); -static PyObject *nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, - nxt_python_target_t *app_target); -static PyObject *nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, +static PyObject * +nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, + nxt_python_target_t *app_target); +static PyObject * +nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port); +static PyObject * +nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port); -static PyObject *nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, - uint8_t len, uint16_t port); -static PyObject *nxt_py_asgi_create_header(nxt_unit_field_t *f); -static PyObject *nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f); - -static int nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); -static int nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); -static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx, +static PyObject * +nxt_py_asgi_create_header(nxt_unit_field_t *f); +static PyObject * +nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f); + +static int +nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +static int +nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); +static void +nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx, nxt_unit_port_t *port); -static void nxt_py_asgi_quit(nxt_unit_ctx_t *ctx); -static void nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx); +static void +nxt_py_asgi_quit(nxt_unit_ctx_t *ctx); +static void +nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx); -static PyObject *nxt_py_asgi_port_read(PyObject *self, PyObject *args); -static void nxt_python_asgi_done(void); +static PyObject * +nxt_py_asgi_port_read(PyObject *self, PyObject *args); +static void +nxt_python_asgi_done(void); -static PyObject *nxt_py_port_read; +static PyObject *nxt_py_port_read; -static PyMethodDef nxt_py_port_read_method = - {"unit_port_read", nxt_py_asgi_port_read, METH_VARARGS, ""}; +static PyMethodDef nxt_py_port_read_method + = {"unit_port_read", nxt_py_asgi_port_read, METH_VARARGS, ""}; -static nxt_python_proto_t nxt_py_asgi_proto = { +static nxt_python_proto_t nxt_py_asgi_proto = { .ctx_data_alloc = nxt_python_asgi_ctx_data_alloc, .ctx_data_free = nxt_python_asgi_ctx_data_free, .startup = nxt_python_asgi_startup, @@ -61,15 +78,13 @@ static nxt_python_proto_t nxt_py_asgi_proto = { .done = nxt_python_asgi_done, }; -#define NXT_UNIT_HASH_WS_PROTOCOL 0xED0A - +#define NXT_UNIT_HASH_WS_PROTOCOL 0xED0A int -nxt_python_asgi_check(PyObject *obj) -{ +nxt_python_asgi_check(PyObject *obj) { int res; - PyObject *func; - PyCodeObject *code; + PyObject *func; + PyCodeObject *code; func = nxt_python_asgi_get_func(obj); @@ -79,10 +94,10 @@ nxt_python_asgi_check(PyObject *obj) code = (PyCodeObject *) PyFunction_GET_CODE(func); - nxt_unit_debug(NULL, "asgi_check: callable is %sa coroutine function with " - "%d argument(s)", - (code->co_flags & CO_COROUTINE) != 0 ? "" : "not ", - code->co_argcount); + nxt_unit_debug(NULL, + "asgi_check: callable is %sa coroutine function with " + "%d argument(s)", + (code->co_flags & CO_COROUTINE) != 0 ? "" : "not ", code->co_argcount); res = (code->co_flags & CO_COROUTINE) != 0 || code->co_argcount == 1; @@ -91,11 +106,9 @@ nxt_python_asgi_check(PyObject *obj) return res; } - static PyObject * -nxt_python_asgi_get_func(PyObject *obj) -{ - PyObject *call; +nxt_python_asgi_get_func(PyObject *obj) { + PyObject *call; if (PyFunction_Check(obj)) { Py_INCREF(obj); @@ -138,13 +151,11 @@ nxt_python_asgi_get_func(PyObject *obj) return obj; } - int -nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) -{ - PyObject *func; +nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) { + PyObject *func; nxt_int_t i; - PyCodeObject *code; + PyCodeObject *code; nxt_unit_debug(NULL, "asgi_init"); @@ -156,7 +167,7 @@ nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) nxt_py_port_read = PyCFunction_New(&nxt_py_port_read_method, NULL); if (nxt_slow_path(nxt_py_port_read == NULL)) { nxt_unit_alert(NULL, - "Python failed to initialize the 'port_read' function"); + "Python failed to initialize the 'port_read' function"); return NXT_UNIT_ERROR; } @@ -171,9 +182,10 @@ nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) for (i = 0; i < nxt_py_targets->count; i++) { func = nxt_python_asgi_get_func(nxt_py_targets->target[i].application); if (nxt_slow_path(func == NULL)) { - nxt_unit_debug(NULL, "asgi: cannot find function for callable, " - "unable to check for legacy mode (#%d)", - (int) i); + nxt_unit_debug(NULL, + "asgi: cannot find function for callable, " + "unable to check for legacy mode (#%d)", + (int) i); continue; } @@ -188,37 +200,35 @@ nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) Py_DECREF(func); } - init->callbacks.request_handler = nxt_py_asgi_request_handler; - init->callbacks.data_handler = nxt_py_asgi_http_data_handler; + init->callbacks.request_handler = nxt_py_asgi_request_handler; + init->callbacks.data_handler = nxt_py_asgi_http_data_handler; init->callbacks.websocket_handler = nxt_py_asgi_websocket_handler; - init->callbacks.close_handler = nxt_py_asgi_close_handler; - init->callbacks.quit = nxt_py_asgi_quit; - init->callbacks.shm_ack_handler = nxt_py_asgi_shm_ack_handler; - init->callbacks.add_port = nxt_py_asgi_add_port; - init->callbacks.remove_port = nxt_py_asgi_remove_port; + init->callbacks.close_handler = nxt_py_asgi_close_handler; + init->callbacks.quit = nxt_py_asgi_quit; + init->callbacks.shm_ack_handler = nxt_py_asgi_shm_ack_handler; + init->callbacks.add_port = nxt_py_asgi_add_port; + init->callbacks.remove_port = nxt_py_asgi_remove_port; *proto = nxt_py_asgi_proto; return NXT_UNIT_OK; } - static PyObject * -nxt_python_asgi_get_event_loop(PyObject *asyncio, const char *event_loop_func) -{ - PyObject *event_loop, *loop; +nxt_python_asgi_get_event_loop(PyObject *asyncio, const char *event_loop_func) { + PyObject *event_loop, *loop; - event_loop = PyDict_GetItemString(PyModule_GetDict(asyncio), - event_loop_func); + event_loop + = PyDict_GetItemString(PyModule_GetDict(asyncio), event_loop_func); if (nxt_slow_path(event_loop == NULL)) { nxt_unit_alert(NULL, "Python failed to get '%s' from module 'asyncio'", - event_loop_func); + event_loop_func); return NULL; } if (nxt_slow_path(PyCallable_Check(event_loop) == 0)) { nxt_unit_alert(NULL, "'asyncio.%s' is not a callable object", - event_loop_func); + event_loop_func); return NULL; } @@ -226,7 +236,7 @@ nxt_python_asgi_get_event_loop(PyObject *asyncio, const char *event_loop_func) if (nxt_slow_path(loop == NULL)) { if (strcmp(event_loop_func, "get_running_loop") != 0) { nxt_unit_alert(NULL, "Python failed to call 'asyncio.%s'", - event_loop_func); + event_loop_func); } return NULL; @@ -235,19 +245,17 @@ nxt_python_asgi_get_event_loop(PyObject *asyncio, const char *event_loop_func) return loop; } - static int -nxt_python_asgi_ctx_data_alloc(void **pdata, int main) -{ +nxt_python_asgi_ctx_data_alloc(void **pdata, int main) { uint32_t i; - PyObject *asyncio, *loop, *obj; - const char *event_loop_func; - nxt_py_asgi_ctx_data_t *ctx_data; + PyObject *asyncio, *loop, *obj; + const char *event_loop_func; + nxt_py_asgi_ctx_data_t *ctx_data; #if PY_VERSION_HEX < NXT_PYTHON_VER(3, 7) - static const char *main_event_loop_func = "get_event_loop"; + static const char *main_event_loop_func = "get_event_loop"; #else - static const char *main_event_loop_func = "get_running_loop"; + static const char *main_event_loop_func = "get_running_loop"; #endif ctx_data = nxt_unit_malloc(NULL, sizeof(nxt_py_asgi_ctx_data_t)); @@ -261,16 +269,16 @@ nxt_python_asgi_ctx_data_alloc(void **pdata, int main) nxt_queue_init(&ctx_data->drain_queue); struct { - const char *key; - PyObject **handler; + const char *key; + PyObject **handler; } handlers[] = { - { "create_task", &ctx_data->loop_create_task }, - { "add_reader", &ctx_data->loop_add_reader }, - { "remove_reader", &ctx_data->loop_remove_reader }, - { "call_soon", &ctx_data->loop_call_soon }, - { "run_until_complete", &ctx_data->loop_run_until_complete }, - { "create_future", &ctx_data->loop_create_future }, + {"create_task", &ctx_data->loop_create_task}, + {"add_reader", &ctx_data->loop_add_reader}, + {"remove_reader", &ctx_data->loop_remove_reader}, + {"call_soon", &ctx_data->loop_call_soon}, + {"run_until_complete", &ctx_data->loop_run_until_complete}, + {"create_future", &ctx_data->loop_create_future}, }; loop = NULL; @@ -306,7 +314,7 @@ nxt_python_asgi_ctx_data_alloc(void **pdata, int main) obj = PyObject_GetAttrString(loop, handlers[i].key); if (nxt_slow_path(obj == NULL)) { nxt_unit_alert(NULL, "Python failed to get 'loop.%s'", - handlers[i].key); + handlers[i].key); goto fail; } @@ -314,7 +322,7 @@ nxt_python_asgi_ctx_data_alloc(void **pdata, int main) if (nxt_slow_path(PyCallable_Check(obj) == 0)) { nxt_unit_alert(NULL, "'loop.%s' is not a callable object", - handlers[i].key); + handlers[i].key); goto fail; } } @@ -358,11 +366,9 @@ nxt_python_asgi_ctx_data_alloc(void **pdata, int main) return NXT_UNIT_ERROR; } - static void -nxt_python_asgi_ctx_data_free(void *data) -{ - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_python_asgi_ctx_data_free(void *data) { + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = data; @@ -378,24 +384,20 @@ nxt_python_asgi_ctx_data_free(void *data) nxt_unit_free(NULL, ctx_data); } - static int -nxt_python_asgi_startup(void *data) -{ +nxt_python_asgi_startup(void *data) { return nxt_py_asgi_lifespan_startup(data); } - static int -nxt_python_asgi_run(nxt_unit_ctx_t *ctx) -{ - PyObject *res; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_python_asgi_run(nxt_unit_ctx_t *ctx) { + PyObject *res; + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = ctx->data; res = PyObject_CallFunctionObjArgs(ctx_data->loop_run_until_complete, - ctx_data->quit_future, NULL); + ctx_data->quit_future, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(ctx, "Python failed to call loop.run_until_complete"); nxt_python_print_exception(); @@ -410,12 +412,10 @@ nxt_python_asgi_run(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static void -nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ - PyObject *res, *fd; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { + PyObject *res, *fd; + nxt_py_asgi_ctx_data_t *ctx_data; if (port == NULL || port->in_fd == -1) { return; @@ -445,15 +445,13 @@ nxt_py_asgi_remove_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) Py_DECREF(fd); } - static void -nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) -{ - PyObject *scope, *res, *task, *receive, *send, *done, *asgi; - PyObject *state, *newstate, *lifespan; - PyObject *stage2; - nxt_python_target_t *target; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) { + PyObject *scope, *res, *task, *receive, *send, *done, *asgi; + PyObject *state, *newstate, *lifespan; + PyObject *stage2; + nxt_python_target_t *target; + nxt_py_asgi_ctx_data_t *ctx_data; if (req->request->websocket_handshake) { asgi = nxt_py_asgi_websocket_create(req); @@ -494,10 +492,10 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) } req->data = asgi; - ctx_data = req->ctx->data; - target = &nxt_py_targets->target[req->request->app_target]; - lifespan = ctx_data->target_lifespans[req->request->app_target]; - state = PyObject_GetAttr(lifespan, nxt_py_state_str); + ctx_data = req->ctx->data; + target = &nxt_py_targets->target[req->request->app_target]; + lifespan = ctx_data->target_lifespans[req->request->app_target]; + state = PyObject_GetAttr(lifespan, nxt_py_state_str); if (nxt_slow_path(state == NULL)) { nxt_unit_req_alert(req, "Python failed to get 'state' attribute"); nxt_unit_request_done(req, NXT_UNIT_ERROR); @@ -521,9 +519,8 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) goto release_done; } - if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_state_str, newstate) - == -1)) - { + if (nxt_slow_path( + PyDict_SetItem(scope, nxt_py_state_str, newstate) == -1)) { Py_DECREF(newstate); goto release_scope; } @@ -532,8 +529,8 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) if (!target->asgi_legacy) { nxt_unit_req_debug(req, "Python call ASGI 3.0 application"); - res = PyObject_CallFunctionObjArgs(target->application, - scope, receive, send, NULL); + res = PyObject_CallFunctionObjArgs(target->application, scope, receive, + send, NULL); } else { nxt_unit_req_debug(req, "Python call legacy application"); @@ -550,7 +547,7 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) if (nxt_slow_path(PyCallable_Check(res) == 0)) { nxt_unit_req_error(req, - "Legacy ASGI application returns not a callable"); + "Legacy ASGI application returns not a callable"); nxt_unit_request_done(req, NXT_UNIT_ERROR); Py_DECREF(res); @@ -597,10 +594,10 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) Py_DECREF(res); res = PyObject_CallMethodObjArgs(task, nxt_py_add_done_callback_str, done, - NULL); + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_error(req, - "Python failed to call 'task.add_done_callback'"); + "Python failed to call 'task.add_done_callback'"); nxt_python_print_exception(); nxt_unit_request_done(req, NXT_UNIT_ERROR); @@ -622,10 +619,8 @@ nxt_py_asgi_request_handler(nxt_unit_request_info_t *req) Py_DECREF(asgi); } - static void -nxt_py_asgi_close_handler(nxt_unit_request_info_t *req) -{ +nxt_py_asgi_close_handler(nxt_unit_request_info_t *req) { if (req->request->websocket_handshake) { nxt_py_asgi_websocket_close_handler(req); @@ -634,41 +629,38 @@ nxt_py_asgi_close_handler(nxt_unit_request_info_t *req) } } - static PyObject * nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, - nxt_python_target_t *app_target) -{ - char *p, *target, *query; + nxt_python_target_t *app_target) { + char *p, *target, *query; uint32_t target_length, i, path_length; - PyObject *scope, *v, *type, *scheme; - PyObject *headers, *header; + PyObject *scope, *v, *type, *scheme; + PyObject *headers, *header; nxt_str_t prefix; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + nxt_unit_field_t *f; + nxt_unit_request_t *r; - static const nxt_str_t ws_protocol = nxt_string("sec-websocket-protocol"); + static const nxt_str_t ws_protocol = nxt_string("sec-websocket-protocol"); -#define SET_ITEM(dict, key, value) \ - if (nxt_slow_path(PyDict_SetItem(dict, nxt_py_ ## key ## _str, value) \ - == -1)) \ - { \ - nxt_unit_req_alert(req, "Python failed to set '" \ - #dict "." #key "' item"); \ +#define SET_ITEM(dict, key, value) \ + if (nxt_slow_path( \ + PyDict_SetItem(dict, nxt_py_##key##_str, value) == -1)) { \ + nxt_unit_req_alert(req, \ + "Python failed to set '" #dict "." #key "' item"); \ goto fail; \ } - v = NULL; + v = NULL; headers = NULL; r = req->request; if (r->websocket_handshake) { - type = nxt_py_websocket_str; + type = nxt_py_websocket_str; scheme = r->tls ? nxt_py_wss_str : nxt_py_ws_str; } else { - type = nxt_py_http_str; + type = nxt_py_http_str; scheme = r->tls ? nxt_py_https_str : nxt_py_http_str; } @@ -677,24 +669,22 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, return NULL; } - prefix = app_target->prefix; + prefix = app_target->prefix; path_length = r->path_length; - p = nxt_unit_sptr_get(&r->path); + p = nxt_unit_sptr_get(&r->path); if (prefix.length > 0 && ((path_length > prefix.length && p[prefix.length] == '/') || path_length == prefix.length) - && memcmp(prefix.start, p, prefix.length) == 0) - { + && memcmp(prefix.start, p, prefix.length) == 0) { SET_ITEM(scope, root_path, app_target->py_prefix); } p = nxt_unit_sptr_get(&r->version); - SET_ITEM(scope, http_version, p[7] == '1' ? nxt_py_1_1_str - : nxt_py_1_0_str) + SET_ITEM(scope, http_version, p[7] == '1' ? nxt_py_1_1_str : nxt_py_1_0_str) SET_ITEM(scope, scheme, scheme) v = PyString_FromStringAndSize(nxt_unit_sptr_get(&r->method), - r->method_length); + r->method_length); if (nxt_slow_path(v == NULL)) { nxt_unit_req_alert(req, "Python failed to create 'method' string"); goto fail; @@ -704,7 +694,7 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, Py_DECREF(v); v = PyUnicode_DecodeUTF8(nxt_unit_sptr_get(&r->path), r->path_length, - "replace"); + "replace"); if (nxt_slow_path(v == NULL)) { nxt_unit_req_alert(req, "Python failed to create 'path' string"); goto fail; @@ -714,7 +704,7 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, Py_DECREF(v); target = nxt_unit_sptr_get(&r->target); - query = nxt_unit_sptr_get(&r->query); + query = nxt_unit_sptr_get(&r->query); if (r->query.offset != 0) { target_length = query - target - 1; @@ -779,10 +769,8 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, PyTuple_SET_ITEM(headers, i, header); if (f->hash == NXT_UNIT_HASH_WS_PROTOCOL - && f->name_length == ws_protocol.length - && f->value_length > 0 - && r->websocket_handshake) - { + && f->name_length == ws_protocol.length && f->value_length > 0 + && r->websocket_handshake) { v = nxt_py_asgi_create_subprotocols(f); if (nxt_slow_path(v == NULL)) { nxt_unit_req_alert(req, "Failed to create subprotocols"); @@ -810,27 +798,24 @@ nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req, #undef SET_ITEM } - static PyObject * -nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port) -{ +nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port) { #if (NXT_HAVE_UNIX_DOMAIN) - size_t prefix_len; - PyObject *pair, *v; - nxt_str_t addr; + size_t prefix_len; + PyObject *pair, *v; + nxt_str_t addr; addr.length = len; - addr.start = nxt_unit_sptr_get(sptr); + addr.start = nxt_unit_sptr_get(sptr); prefix_len = nxt_length("unix:"); if (nxt_str_start(&addr, "unix:", prefix_len)) { - pair = PyTuple_New(2); if (nxt_slow_path(pair == NULL)) { return NULL; } - addr.start += prefix_len; + addr.start += prefix_len; addr.length -= prefix_len; v = PyString_FromStringAndSize((const char *) addr.start, addr.length); @@ -850,12 +835,11 @@ nxt_py_asgi_create_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port) return nxt_py_asgi_create_ip_address(sptr, len, port); } - static PyObject * -nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port) -{ - char *p; - PyObject *pair, *v; +nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, + uint16_t port) { + char *p; + PyObject *pair, *v; pair = PyTuple_New(2); if (nxt_slow_path(pair == NULL)) { @@ -885,13 +869,11 @@ nxt_py_asgi_create_ip_address(nxt_unit_sptr_t *sptr, uint8_t len, uint16_t port) return pair; } - static PyObject * -nxt_py_asgi_create_header(nxt_unit_field_t *f) -{ +nxt_py_asgi_create_header(nxt_unit_field_t *f) { char c, *name; uint8_t pos; - PyObject *header, *v; + PyObject *header, *v; header = PyTuple_New(2); if (nxt_slow_path(header == NULL)) { @@ -917,7 +899,7 @@ nxt_py_asgi_create_header(nxt_unit_field_t *f) PyTuple_SET_ITEM(header, 0, v); v = PyBytes_FromStringAndSize(nxt_unit_sptr_get(&f->value), - f->value_length); + f->value_length); if (nxt_slow_path(v == NULL)) { Py_DECREF(header); @@ -929,13 +911,11 @@ nxt_py_asgi_create_header(nxt_unit_field_t *f) return header; } - static PyObject * -nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f) -{ - char *v; +nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f) { + char *v; uint32_t i, n, start; - PyObject *res, *proto; + PyObject *res, *proto; v = nxt_unit_sptr_get(&f->value); n = 1; @@ -951,10 +931,10 @@ nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f) return NULL; } - n = 0; + n = 0; start = 0; - for (i = 0; i < f->value_length; ) { + for (i = 0; i < f->value_length;) { if (v[i] != ',') { i++; @@ -997,11 +977,9 @@ nxt_py_asgi_create_subprotocols(nxt_unit_field_t *f) return NULL; } - static int -nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ - int nb; +nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { + int nb; if (port->in_fd == -1) { return NXT_UNIT_OK; @@ -1011,7 +989,7 @@ nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) if (nxt_slow_path(ioctl(port->in_fd, FIONBIO, &nb) == -1)) { nxt_unit_alert(ctx, "ioctl(%d, FIONBIO, 0) failed: %s (%d)", - port->in_fd, strerror(errno), errno); + port->in_fd, strerror(errno), errno); return NXT_UNIT_ERROR; } @@ -1021,13 +999,11 @@ nxt_py_asgi_add_port(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return nxt_py_asgi_add_reader(ctx, port); } - static int -nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) -{ +nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) { int rc; - PyObject *res, *fd, *py_ctx, *py_port; - nxt_py_asgi_ctx_data_t *ctx_data; + PyObject *res, *fd, *py_ctx, *py_port; + nxt_py_asgi_ctx_data_t *ctx_data; nxt_unit_debug(ctx, "asgi_add_reader %d %p %p", port->in_fd, ctx, port); @@ -1059,9 +1035,8 @@ nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) goto clean_py_ctx; } - res = PyObject_CallFunctionObjArgs(ctx_data->loop_add_reader, - fd, nxt_py_port_read, - py_ctx, py_port, NULL); + res = PyObject_CallFunctionObjArgs(ctx_data->loop_add_reader, fd, + nxt_py_port_read, py_ctx, py_port, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(ctx, "Python failed to add_reader"); nxt_python_print_exception(); @@ -1085,11 +1060,9 @@ nxt_py_asgi_add_reader(nxt_unit_ctx_t *ctx, nxt_unit_port_t *port) return rc; } - static void nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx, - nxt_unit_port_t *port) -{ + nxt_unit_port_t *port) { if (port->in_fd == -1 || ctx == NULL) { return; } @@ -1099,12 +1072,10 @@ nxt_py_asgi_remove_port(nxt_unit_t *lib, nxt_unit_ctx_t *ctx, nxt_py_asgi_remove_reader(ctx, port); } - static void -nxt_py_asgi_quit(nxt_unit_ctx_t *ctx) -{ - PyObject *res, *p; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_quit(nxt_unit_ctx_t *ctx) { + PyObject *res, *p; + nxt_py_asgi_ctx_data_t *ctx_data; nxt_unit_debug(ctx, "asgi_quit %p", ctx); @@ -1116,8 +1087,8 @@ nxt_py_asgi_quit(nxt_unit_ctx_t *ctx) nxt_python_print_exception(); } else { - res = PyObject_CallFunctionObjArgs(ctx_data->quit_future_set_result, - p, NULL); + res = PyObject_CallFunctionObjArgs(ctx_data->quit_future_set_result, p, + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(ctx, "Python failed to set_result"); nxt_python_print_exception(); @@ -1130,13 +1101,11 @@ nxt_py_asgi_quit(nxt_unit_ctx_t *ctx) } } - static void -nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx) -{ +nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx) { int rc; - nxt_queue_link_t *lnk; - nxt_py_asgi_ctx_data_t *ctx_data; + nxt_queue_link_t *lnk; + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = ctx->data; @@ -1152,23 +1121,20 @@ nxt_py_asgi_shm_ack_handler(nxt_unit_ctx_t *ctx) } } - static PyObject * -nxt_py_asgi_port_read(PyObject *self, PyObject *args) -{ +nxt_py_asgi_port_read(PyObject *self, PyObject *args) { int rc; - PyObject *arg0, *arg1, *res; + PyObject *arg0, *arg1, *res; Py_ssize_t n; - nxt_unit_ctx_t *ctx; - nxt_unit_port_t *port; - nxt_py_asgi_ctx_data_t *ctx_data; + nxt_unit_ctx_t *ctx; + nxt_unit_port_t *port; + nxt_py_asgi_ctx_data_t *ctx_data; n = PyTuple_GET_SIZE(args); if (n != 2) { nxt_unit_alert(NULL, - "nxt_py_asgi_port_read: invalid number of arguments %d", - (int) n); + "nxt_py_asgi_port_read: invalid number of arguments %d", (int) n); return PyErr_Format(PyExc_TypeError, "invalid number of arguments"); } @@ -1176,7 +1142,7 @@ nxt_py_asgi_port_read(PyObject *self, PyObject *args) arg0 = PyTuple_GET_ITEM(args, 0); if (nxt_slow_path(arg0 == NULL || PyLong_Check(arg0) == 0)) { return PyErr_Format(PyExc_TypeError, - "the first argument is not a long"); + "the first argument is not a long"); } ctx = PyLong_AsVoidPtr(arg0); @@ -1184,7 +1150,7 @@ nxt_py_asgi_port_read(PyObject *self, PyObject *args) arg1 = PyTuple_GET_ITEM(args, 1); if (nxt_slow_path(arg1 == NULL || PyLong_Check(arg1) == 0)) { return PyErr_Format(PyExc_TypeError, - "the second argument is not a long"); + "the second argument is not a long"); } port = PyLong_AsVoidPtr(arg1); @@ -1195,15 +1161,14 @@ nxt_py_asgi_port_read(PyObject *self, PyObject *args) if (nxt_slow_path(rc == NXT_UNIT_ERROR)) { return PyErr_Format(PyExc_RuntimeError, - "error processing port %d message", port->id.id); + "error processing port %d message", port->id.id); } if (rc == NXT_UNIT_OK) { ctx_data = ctx->data; res = PyObject_CallFunctionObjArgs(ctx_data->loop_call_soon, - nxt_py_port_read, - arg0, arg1, NULL); + nxt_py_port_read, arg0, arg1, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(ctx, "Python failed to call 'loop.call_soon'"); nxt_python_print_exception(); @@ -1215,13 +1180,11 @@ nxt_py_asgi_port_read(PyObject *self, PyObject *args) Py_RETURN_NONE; } - PyObject * nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, - void *data) -{ + void *data) { int i; - PyObject *iter, *header, *h_iter, *name, *val, *res; + PyObject *iter, *header, *h_iter, *name, *val, *res; iter = PyObject_GetIter(headers); if (nxt_slow_path(iter == NULL)) { @@ -1240,7 +1203,7 @@ nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, Py_DECREF(iter); return PyErr_Format(PyExc_TypeError, - "'headers' item #%d is not an iterable", i); + "'headers' item #%d is not an iterable", i); } name = PyIter_Next(h_iter); @@ -1251,7 +1214,7 @@ nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, Py_DECREF(iter); return PyErr_Format(PyExc_TypeError, - "'headers' item #%d 'name' is not a byte string", i); + "'headers' item #%d 'name' is not a byte string", i); } val = PyIter_Next(h_iter); @@ -1262,7 +1225,7 @@ nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, Py_DECREF(iter); return PyErr_Format(PyExc_TypeError, - "'headers' item #%d 'value' is not a byte string", i); + "'headers' item #%d 'value' is not a byte string", i); } res = cb(data, i, name, val); @@ -1286,11 +1249,9 @@ nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, Py_RETURN_NONE; } - PyObject * -nxt_py_asgi_calc_size(void *data, int i, PyObject *name, PyObject *val) -{ - nxt_py_asgi_calc_size_ctx_t *ctx; +nxt_py_asgi_calc_size(void *data, int i, PyObject *name, PyObject *val) { + nxt_py_asgi_calc_size_ctx_t *ctx; ctx = data; @@ -1300,16 +1261,14 @@ nxt_py_asgi_calc_size(void *data, int i, PyObject *name, PyObject *val) Py_RETURN_NONE; } - PyObject * -nxt_py_asgi_add_field(void *data, int i, PyObject *name, PyObject *val) -{ +nxt_py_asgi_add_field(void *data, int i, PyObject *name, PyObject *val) { int rc; - char *name_str, *val_str; + char *name_str, *val_str; uint32_t name_len, val_len; nxt_off_t content_length; - nxt_unit_request_info_t *req; - nxt_py_asgi_add_field_ctx_t *ctx; + nxt_unit_request_info_t *req; + nxt_py_asgi_add_field_ctx_t *ctx; name_str = PyBytes_AS_STRING(name); name_len = PyBytes_GET_SIZE(name); @@ -1320,22 +1279,22 @@ nxt_py_asgi_add_field(void *data, int i, PyObject *name, PyObject *val) ctx = data; req = ctx->req; - rc = nxt_unit_response_add_field(req, name_str, name_len, - val_str, val_len); + rc = nxt_unit_response_add_field(req, name_str, name_len, val_str, val_len); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - return PyErr_Format(PyExc_RuntimeError, - "failed to add header #%d", i); + return PyErr_Format(PyExc_RuntimeError, "failed to add header #%d", i); } if (req->response->fields[i].hash == NXT_UNIT_HASH_CONTENT_LENGTH) { content_length = nxt_off_t_parse((u_char *) val_str, val_len); if (nxt_slow_path(content_length < 0)) { - nxt_unit_req_error(req, "failed to parse Content-Length " - "value %.*s", (int) val_len, val_str); + nxt_unit_req_error(req, + "failed to parse Content-Length " + "value %.*s", + (int) val_len, val_str); return PyErr_Format(PyExc_ValueError, - "Failed to parse Content-Length: '%.*s'", - (int) val_len, val_str); + "Failed to parse Content-Length: '%.*s'", (int) val_len, + val_str); } ctx->content_length = content_length; @@ -1344,12 +1303,10 @@ nxt_py_asgi_add_field(void *data, int i, PyObject *name, PyObject *val) Py_RETURN_NONE; } - PyObject * nxt_py_asgi_set_result_soon(nxt_unit_request_info_t *req, - nxt_py_asgi_ctx_data_t *ctx_data, PyObject *future, PyObject *result) -{ - PyObject *set_result, *res; + nxt_py_asgi_ctx_data_t *ctx_data, PyObject *future, PyObject *result) { + PyObject *set_result, *res; if (nxt_slow_path(result == NULL)) { Py_DECREF(future); @@ -1375,7 +1332,7 @@ nxt_py_asgi_set_result_soon(nxt_unit_request_info_t *req, } res = PyObject_CallFunctionObjArgs(ctx_data->loop_call_soon, set_result, - result, NULL); + result, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_alert(req, "Python failed to call 'loop.call_soon'"); nxt_python_print_exception(); @@ -1396,11 +1353,9 @@ nxt_py_asgi_set_result_soon(nxt_unit_request_info_t *req, return future; } - PyObject * -nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type) -{ - PyObject *msg; +nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type) { + PyObject *msg; msg = PyDict_New(); if (nxt_slow_path(msg == NULL)) { @@ -1408,7 +1363,7 @@ nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type) nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create message dict"); + "failed to create message dict"); } if (nxt_slow_path(PyDict_SetItem(msg, nxt_py_type_str, type) == -1)) { @@ -1417,18 +1372,16 @@ nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type) Py_DECREF(msg); return PyErr_Format(PyExc_RuntimeError, - "failed to set 'msg.type' item"); + "failed to set 'msg.type' item"); } return msg; } - PyObject * nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, - PyObject *spec_version) -{ - PyObject *scope, *asgi; + PyObject *spec_version) { + PyObject *scope, *asgi; scope = PyDict_New(); if (nxt_slow_path(scope == NULL)) { @@ -1436,7 +1389,7 @@ nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create 'scope' dict"); + "failed to create 'scope' dict"); } if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_type_str, type) == -1)) { @@ -1445,7 +1398,7 @@ nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, Py_DECREF(scope); return PyErr_Format(PyExc_RuntimeError, - "failed to set 'scope.type' item"); + "failed to set 'scope.type' item"); } asgi = PyDict_New(); @@ -1455,8 +1408,7 @@ nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, Py_DECREF(scope); - return PyErr_Format(PyExc_RuntimeError, - "failed to create 'asgi' dict"); + return PyErr_Format(PyExc_RuntimeError, "failed to create 'asgi' dict"); } if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_asgi_str, asgi) == -1)) { @@ -1466,32 +1418,31 @@ nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, Py_DECREF(scope); return PyErr_Format(PyExc_RuntimeError, - "failed to set 'scope.asgi' item"); + "failed to set 'scope.asgi' item"); } - if (nxt_slow_path(PyDict_SetItem(asgi, nxt_py_version_str, - nxt_py_3_0_str) == -1)) - { + if (nxt_slow_path( + PyDict_SetItem(asgi, nxt_py_version_str, nxt_py_3_0_str) == -1)) { nxt_unit_req_alert(req, "Python failed to set 'asgi.version' item"); Py_DECREF(asgi); Py_DECREF(scope); return PyErr_Format(PyExc_RuntimeError, - "failed to set 'asgi.version' item"); + "failed to set 'asgi.version' item"); } - if (nxt_slow_path(PyDict_SetItem(asgi, nxt_py_spec_version_str, - spec_version) == -1)) - { + if (nxt_slow_path( + PyDict_SetItem(asgi, nxt_py_spec_version_str, spec_version) + == -1)) { nxt_unit_req_alert(req, - "Python failed to set 'asgi.spec_version' item"); + "Python failed to set 'asgi.spec_version' item"); Py_DECREF(asgi); Py_DECREF(scope); return PyErr_Format(PyExc_RuntimeError, - "failed to set 'asgi.spec_version' item"); + "failed to set 'asgi.spec_version' item"); } Py_DECREF(asgi); @@ -1499,51 +1450,39 @@ nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, return scope; } - void -nxt_py_asgi_drain_wait(nxt_unit_request_info_t *req, nxt_queue_link_t *link) -{ - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_drain_wait(nxt_unit_request_info_t *req, nxt_queue_link_t *link) { + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = req->ctx->data; nxt_queue_insert_tail(&ctx_data->drain_queue, link); } - void -nxt_py_asgi_dealloc(PyObject *self) -{ +nxt_py_asgi_dealloc(PyObject *self) { PyObject_Del(self); } - PyObject * -nxt_py_asgi_await(PyObject *self) -{ +nxt_py_asgi_await(PyObject *self) { Py_INCREF(self); return self; } - PyObject * -nxt_py_asgi_iter(PyObject *self) -{ +nxt_py_asgi_iter(PyObject *self) { Py_INCREF(self); return self; } - PyObject * -nxt_py_asgi_next(PyObject *self) -{ +nxt_py_asgi_next(PyObject *self) { return NULL; } - static void -nxt_python_asgi_done(void) -{ +nxt_python_asgi_done(void) { nxt_py_asgi_str_done(); Py_XDECREF(nxt_py_port_read); @@ -1553,15 +1492,12 @@ nxt_python_asgi_done(void) int -nxt_python_asgi_check(PyObject *obj) -{ +nxt_python_asgi_check(PyObject *obj) { return 0; } - int -nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) -{ +nxt_python_asgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) { nxt_unit_alert(NULL, "ASGI not implemented"); return NXT_UNIT_ERROR; } diff --git a/src/python/nxt_python_asgi.h b/src/python/nxt_python_asgi.h index 94478a361..4af3ff27b 100644 --- a/src/python/nxt_python_asgi.h +++ b/src/python/nxt_python_asgi.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,65 +6,86 @@ #define _NXT_PYTHON_ASGI_H_INCLUDED_ -typedef PyObject * (*nxt_py_asgi_enum_header_cb)(void *ctx, int i, +typedef PyObject *(*nxt_py_asgi_enum_header_cb)(void *ctx, int i, PyObject *name, PyObject *val); -void nxt_py_asgi_drain_wait(nxt_unit_request_info_t *req, - nxt_queue_link_t *link); +void +nxt_py_asgi_drain_wait(nxt_unit_request_info_t *req, nxt_queue_link_t *link); typedef struct { - uint32_t fields_count; - uint32_t fields_size; + uint32_t fields_count; + uint32_t fields_size; } nxt_py_asgi_calc_size_ctx_t; typedef struct { - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; uint64_t content_length; } nxt_py_asgi_add_field_ctx_t; typedef struct { - nxt_queue_t drain_queue; - PyObject *loop_run_until_complete; - PyObject *loop_create_future; - PyObject *loop_create_task; - PyObject *loop_call_soon; - PyObject *loop_add_reader; - PyObject *loop_remove_reader; - PyObject *quit_future; - PyObject *quit_future_set_result; - PyObject **target_lifespans; + nxt_queue_t drain_queue; + PyObject *loop_run_until_complete; + PyObject *loop_create_future; + PyObject *loop_create_task; + PyObject *loop_call_soon; + PyObject *loop_add_reader; + PyObject *loop_remove_reader; + PyObject *quit_future; + PyObject *quit_future_set_result; + PyObject **target_lifespans; } nxt_py_asgi_ctx_data_t; -PyObject *nxt_py_asgi_enum_headers(PyObject *headers, - nxt_py_asgi_enum_header_cb cb, void *data); +PyObject * +nxt_py_asgi_enum_headers(PyObject *headers, nxt_py_asgi_enum_header_cb cb, + void *data); -PyObject *nxt_py_asgi_calc_size(void *data, int i, PyObject *n, PyObject *v); -PyObject *nxt_py_asgi_add_field(void *data, int i, PyObject *n, PyObject *v); +PyObject * +nxt_py_asgi_calc_size(void *data, int i, PyObject *n, PyObject *v); +PyObject * +nxt_py_asgi_add_field(void *data, int i, PyObject *n, PyObject *v); -PyObject *nxt_py_asgi_set_result_soon(nxt_unit_request_info_t *req, +PyObject * +nxt_py_asgi_set_result_soon(nxt_unit_request_info_t *req, nxt_py_asgi_ctx_data_t *ctx_data, PyObject *future, PyObject *result); -PyObject *nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type); -PyObject *nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, +PyObject * +nxt_py_asgi_new_msg(nxt_unit_request_info_t *req, PyObject *type); +PyObject * +nxt_py_asgi_new_scope(nxt_unit_request_info_t *req, PyObject *type, PyObject *spec_version); -void nxt_py_asgi_dealloc(PyObject *self); -PyObject *nxt_py_asgi_await(PyObject *self); -PyObject *nxt_py_asgi_iter(PyObject *self); -PyObject *nxt_py_asgi_next(PyObject *self); - -int nxt_py_asgi_http_init(void); -PyObject *nxt_py_asgi_http_create(nxt_unit_request_info_t *req); -void nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req); -int nxt_py_asgi_http_drain(nxt_queue_link_t *lnk); -void nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req); - -int nxt_py_asgi_websocket_init(void); -PyObject *nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req); -void nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *ws); -void nxt_py_asgi_websocket_close_handler(nxt_unit_request_info_t *req); - -int nxt_py_asgi_lifespan_startup(nxt_py_asgi_ctx_data_t *ctx_data); -int nxt_py_asgi_lifespan_shutdown(nxt_unit_ctx_t *ctx); - - -#endif /* _NXT_PYTHON_ASGI_H_INCLUDED_ */ +void +nxt_py_asgi_dealloc(PyObject *self); +PyObject * +nxt_py_asgi_await(PyObject *self); +PyObject * +nxt_py_asgi_iter(PyObject *self); +PyObject * +nxt_py_asgi_next(PyObject *self); + +int +nxt_py_asgi_http_init(void); +PyObject * +nxt_py_asgi_http_create(nxt_unit_request_info_t *req); +void +nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req); +int +nxt_py_asgi_http_drain(nxt_queue_link_t *lnk); +void +nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req); + +int +nxt_py_asgi_websocket_init(void); +PyObject * +nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req); +void +nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *ws); +void +nxt_py_asgi_websocket_close_handler(nxt_unit_request_info_t *req); + +int +nxt_py_asgi_lifespan_startup(nxt_py_asgi_ctx_data_t *ctx_data); +int +nxt_py_asgi_lifespan_shutdown(nxt_unit_ctx_t *ctx); + + +#endif /* _NXT_PYTHON_ASGI_H_INCLUDED_ */ diff --git a/src/python/nxt_python_asgi_http.c b/src/python/nxt_python_asgi_http.c index cdd6357e6..93ee76e9a 100644 --- a/src/python/nxt_python_asgi_http.c +++ b/src/python/nxt_python_asgi_http.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -14,42 +13,43 @@ #include #include - typedef struct { - PyObject_HEAD - nxt_unit_request_info_t *req; - nxt_queue_link_t link; - PyObject *receive_future; - PyObject *send_future; - uint64_t content_length; - uint64_t bytes_sent; - PyObject *send_body; - Py_ssize_t send_body_off; - uint8_t complete; - uint8_t closed; - uint8_t empty_body_received; + PyObject_HEAD nxt_unit_request_info_t *req; + nxt_queue_link_t link; + PyObject *receive_future; + PyObject *send_future; + uint64_t content_length; + uint64_t bytes_sent; + PyObject *send_body; + Py_ssize_t send_body_off; + uint8_t complete; + uint8_t closed; + uint8_t empty_body_received; } nxt_py_asgi_http_t; +static PyObject * +nxt_py_asgi_http_receive(PyObject *self, PyObject *none); +static PyObject * +nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http); +static PyObject * +nxt_py_asgi_http_send(PyObject *self, PyObject *dict); +static PyObject * +nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict); +static PyObject * +nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict); +static void +nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http); +static void +nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, PyObject *future, + PyObject *msg); +static PyObject * +nxt_py_asgi_http_done(PyObject *self, PyObject *future); -static PyObject *nxt_py_asgi_http_receive(PyObject *self, PyObject *none); -static PyObject *nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http); -static PyObject *nxt_py_asgi_http_send(PyObject *self, PyObject *dict); -static PyObject *nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, - PyObject *dict); -static PyObject *nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, - PyObject *dict); -static void nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http); -static void nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, - PyObject *future, PyObject *msg); -static PyObject *nxt_py_asgi_http_done(PyObject *self, PyObject *future); - - -static PyMethodDef nxt_py_asgi_http_methods[] = { - { "receive", nxt_py_asgi_http_receive, METH_NOARGS, 0 }, - { "send", nxt_py_asgi_http_send, METH_O, 0 }, - { "_done", nxt_py_asgi_http_done, METH_O, 0 }, - { NULL, NULL, 0, 0 } -}; + +static PyMethodDef nxt_py_asgi_http_methods[] + = {{"receive", nxt_py_asgi_http_receive, METH_NOARGS, 0}, + {"send", nxt_py_asgi_http_send, METH_O, 0}, + {"_done", nxt_py_asgi_http_done, METH_O, 0}, {NULL, NULL, 0, 0}}; static PyAsyncMethods nxt_py_asgi_async_methods = { .am_await = nxt_py_asgi_await, @@ -58,7 +58,8 @@ static PyAsyncMethods nxt_py_asgi_async_methods = { static PyTypeObject nxt_py_asgi_http_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "unit._asgi_http", + .tp_name + = "unit._asgi_http", .tp_basicsize = sizeof(nxt_py_asgi_http_t), .tp_dealloc = nxt_py_asgi_dealloc, .tp_as_async = &nxt_py_asgi_async_methods, @@ -69,60 +70,54 @@ static PyTypeObject nxt_py_asgi_http_type = { .tp_methods = nxt_py_asgi_http_methods, }; -static Py_ssize_t nxt_py_asgi_http_body_buf_size = 32 * 1024 * 1024; - +static Py_ssize_t nxt_py_asgi_http_body_buf_size = 32 * 1024 * 1024; int -nxt_py_asgi_http_init(void) -{ +nxt_py_asgi_http_init(void) { if (nxt_slow_path(PyType_Ready(&nxt_py_asgi_http_type) != 0)) { nxt_unit_alert(NULL, - "Python failed to initialize the 'http' type object"); + "Python failed to initialize the 'http' type object"); return NXT_UNIT_ERROR; } return NXT_UNIT_OK; } - PyObject * -nxt_py_asgi_http_create(nxt_unit_request_info_t *req) -{ - nxt_py_asgi_http_t *http; +nxt_py_asgi_http_create(nxt_unit_request_info_t *req) { + nxt_py_asgi_http_t *http; http = PyObject_New(nxt_py_asgi_http_t, &nxt_py_asgi_http_type); if (nxt_fast_path(http != NULL)) { - http->req = req; - http->receive_future = NULL; - http->send_future = NULL; - http->content_length = -1; - http->bytes_sent = 0; - http->send_body = NULL; - http->send_body_off = 0; - http->complete = 0; - http->closed = 0; + http->req = req; + http->receive_future = NULL; + http->send_future = NULL; + http->content_length = -1; + http->bytes_sent = 0; + http->send_body = NULL; + http->send_body_off = 0; + http->complete = 0; + http->closed = 0; http->empty_body_received = 0; } return (PyObject *) http; } - static PyObject * -nxt_py_asgi_http_receive(PyObject *self, PyObject *none) -{ - PyObject *msg, *future; - nxt_py_asgi_http_t *http; - nxt_py_asgi_ctx_data_t *ctx_data; - nxt_unit_request_info_t *req; +nxt_py_asgi_http_receive(PyObject *self, PyObject *none) { + PyObject *msg, *future; + nxt_py_asgi_http_t *http; + nxt_py_asgi_ctx_data_t *ctx_data; + nxt_unit_request_info_t *req; http = (nxt_py_asgi_http_t *) self; - req = http->req; + req = http->req; nxt_unit_req_debug(req, "asgi_http_receive"); - if (nxt_slow_path(http->closed || http->complete )) { + if (nxt_slow_path(http->closed || http->complete)) { msg = nxt_py_asgi_new_msg(req, nxt_py_http_disconnect_str); } else { @@ -143,7 +138,7 @@ nxt_py_asgi_http_receive(PyObject *self, PyObject *none) Py_DECREF(msg); return PyErr_Format(PyExc_RuntimeError, - "failed to create Future object"); + "failed to create Future object"); } if (msg != Py_None) { @@ -158,15 +153,13 @@ nxt_py_asgi_http_receive(PyObject *self, PyObject *none) return future; } - static PyObject * -nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) -{ - char *body_buf; +nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) { + char *body_buf; ssize_t read_res; - PyObject *msg, *body; + PyObject *msg, *body; Py_ssize_t size; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; req = http->req; @@ -191,7 +184,7 @@ nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create Bytes object"); + "failed to create Bytes object"); } body_buf = PyBytes_AS_STRING(body); @@ -199,7 +192,7 @@ nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) read_res = nxt_unit_request_read(req, body_buf, size); } else { - body = NULL; + body = NULL; read_res = 0; } @@ -211,14 +204,13 @@ nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) return NULL; } -#define SET_ITEM(dict, key, value) \ - if (nxt_slow_path(PyDict_SetItem(dict, nxt_py_ ## key ## _str, value) \ - == -1)) \ - { \ +#define SET_ITEM(dict, key, value) \ + if (nxt_slow_path( \ + PyDict_SetItem(dict, nxt_py_##key##_str, value) == -1)) { \ nxt_unit_req_alert(req, \ - "Python failed to set '" #dict "." #key "' item"); \ + "Python failed to set '" #dict "." #key "' item"); \ PyErr_SetString(PyExc_RuntimeError, \ - "Python failed to set '" #dict "." #key "' item"); \ + "Python failed to set '" #dict "." #key "' item"); \ goto fail; \ } @@ -249,17 +241,15 @@ nxt_py_asgi_http_read_msg(nxt_py_asgi_http_t *http) return NULL; } - static PyObject * -nxt_py_asgi_http_send(PyObject *self, PyObject *dict) -{ - PyObject *type; - const char *type_str; +nxt_py_asgi_http_send(PyObject *self, PyObject *dict) { + PyObject *type; + const char *type_str; Py_ssize_t type_len; - nxt_py_asgi_http_t *http; + nxt_py_asgi_http_t *http; - static const nxt_str_t response_start = nxt_string("http.response.start"); - static const nxt_str_t response_body = nxt_string("http.response.body"); + static const nxt_str_t response_start = nxt_string("http.response.start"); + static const nxt_str_t response_body = nxt_string("http.response.body"); http = (nxt_py_asgi_http_t *) self; @@ -273,7 +263,7 @@ nxt_py_asgi_http_send(PyObject *self, PyObject *dict) type_str = PyUnicode_AsUTF8AndSize(type, &type_len); nxt_unit_req_debug(http->req, "asgi_http_send type is '%.*s'", - (int) type_len, type_str); + (int) type_len, type_str); if (nxt_unit_response_is_init(http->req)) { if (nxt_str_eq(&response_body, type_str, (size_t) type_len)) { @@ -281,8 +271,9 @@ nxt_py_asgi_http_send(PyObject *self, PyObject *dict) } return PyErr_Format(PyExc_RuntimeError, - "Expected ASGI message 'http.response.body', " - "but got '%U'", type); + "Expected ASGI message 'http.response.body', " + "but got '%U'", + type); } if (nxt_str_eq(&response_start, type_str, (size_t) type_len)) { @@ -290,18 +281,17 @@ nxt_py_asgi_http_send(PyObject *self, PyObject *dict) } return PyErr_Format(PyExc_RuntimeError, - "Expected ASGI message 'http.response.start', " - "but got '%U'", type); + "Expected ASGI message 'http.response.start', " + "but got '%U'", + type); } - static PyObject * -nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict) -{ - int rc; - PyObject *status, *headers, *res; - nxt_py_asgi_calc_size_ctx_t calc_size_ctx; - nxt_py_asgi_add_field_ctx_t add_field_ctx; +nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict) { + int rc; + PyObject *status, *headers, *res; + nxt_py_asgi_calc_size_ctx_t calc_size_ctx; + nxt_py_asgi_add_field_ctx_t add_field_ctx; status = PyDict_GetItem(dict, nxt_py_status_str); if (nxt_slow_path(status == NULL || !PyLong_Check(status))) { @@ -310,13 +300,13 @@ nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict) return PyErr_Format(PyExc_TypeError, "'status' is not an integer"); } - calc_size_ctx.fields_size = 0; + calc_size_ctx.fields_size = 0; calc_size_ctx.fields_count = 0; headers = PyDict_GetItem(dict, nxt_py_headers_str); if (headers != NULL) { res = nxt_py_asgi_enum_headers(headers, nxt_py_asgi_calc_size, - &calc_size_ctx); + &calc_size_ctx); if (nxt_slow_path(res == NULL)) { return NULL; } @@ -325,19 +315,18 @@ nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict) } rc = nxt_unit_response_init(http->req, PyLong_AsLong(status), - calc_size_ctx.fields_count, - calc_size_ctx.fields_size); + calc_size_ctx.fields_count, calc_size_ctx.fields_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to allocate response object"); + "failed to allocate response object"); } - add_field_ctx.req = http->req; + add_field_ctx.req = http->req; add_field_ctx.content_length = -1; if (headers != NULL) { res = nxt_py_asgi_enum_headers(headers, nxt_py_asgi_add_field, - &add_field_ctx); + &add_field_ctx); if (nxt_slow_path(res == NULL)) { return NULL; } @@ -351,21 +340,19 @@ nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http, PyObject *dict) return (PyObject *) http; } - static PyObject * -nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) -{ +nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) { int rc; - char *body_str; + char *body_str; ssize_t sent; - PyObject *body, *more_body, *future; + PyObject *body, *more_body, *future; Py_ssize_t body_len, body_off; - nxt_py_asgi_ctx_data_t *ctx_data; + nxt_py_asgi_ctx_data_t *ctx_data; if (nxt_slow_path(http->complete)) { return PyErr_Format(PyExc_RuntimeError, - "Unexpected ASGI message 'http.response.body' " - "sent, after response already completed"); + "Unexpected ASGI message 'http.response.body' " + "sent, after response already completed"); } if (nxt_slow_path(http->send_future != NULL)) { @@ -390,17 +377,15 @@ nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) } else { return PyErr_Format(PyExc_TypeError, - "'body' is not a byte string or bytearray"); + "'body' is not a byte string or bytearray"); } nxt_unit_req_debug(http->req, "asgi_http_response_body: %d, %d", - (int) body_len, (more_body == Py_True) ); + (int) body_len, (more_body == Py_True)); - if (nxt_slow_path(http->bytes_sent + body_len - > http->content_length)) - { + if (nxt_slow_path(http->bytes_sent + body_len > http->content_length)) { return PyErr_Format(PyExc_RuntimeError, - "Response content longer than Content-Length"); + "Response content longer than Content-Length"); } body_off = 0; @@ -414,19 +399,20 @@ nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) } if (nxt_slow_path(sent == 0)) { - nxt_unit_req_debug(http->req, "asgi_http_response_body: " - "out of shared memory, %d", - (int) body_len); + nxt_unit_req_debug(http->req, + "asgi_http_response_body: " + "out of shared memory, %d", + (int) body_len); - future = PyObject_CallObject(ctx_data->loop_create_future, - NULL); + future + = PyObject_CallObject(ctx_data->loop_create_future, NULL); if (nxt_slow_path(future == NULL)) { nxt_unit_req_alert(http->req, - "Python failed to create Future object"); + "Python failed to create Future object"); nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create Future object"); + "failed to create Future object"); } http->send_body = body; @@ -441,21 +427,21 @@ nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) return future; } - body_str += sent; - body_len -= sent; - body_off += sent; + body_str += sent; + body_len -= sent; + body_off += sent; http->bytes_sent += sent; } } else { nxt_unit_req_debug(http->req, "asgi_http_response_body: 0, %d", - (more_body == Py_True) ); + (more_body == Py_True)); if (!nxt_unit_response_is_sent(http->req)) { rc = nxt_unit_response_send(http->req); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to send response"); + "failed to send response"); } } } @@ -470,11 +456,9 @@ nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict) return (PyObject *) http; } - static void -nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http) -{ - PyObject *msg, *future; +nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http) { + PyObject *msg, *future; if (http->receive_future == NULL) { return; @@ -490,7 +474,7 @@ nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http) return; } - future = http->receive_future; + future = http->receive_future; http->receive_future = NULL; nxt_py_asgi_http_set_result(http, future, msg); @@ -498,12 +482,10 @@ nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http) Py_DECREF(msg); } - static void nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, PyObject *future, - PyObject *msg) -{ - PyObject *res; + PyObject *msg) { + PyObject *res; res = PyObject_CallMethodObjArgs(future, nxt_py_done_str, NULL); if (nxt_slow_path(res == NULL)) { @@ -513,7 +495,7 @@ nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, PyObject *future, if (nxt_fast_path(res == Py_False)) { res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, - NULL); + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_alert(http->req, "'set_result' call failed"); nxt_python_print_exception(); @@ -527,12 +509,10 @@ nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, PyObject *future, Py_DECREF(future); } - void -nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req) -{ - PyObject *msg, *future; - nxt_py_asgi_http_t *http; +nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req) { + PyObject *msg, *future; + nxt_py_asgi_http_t *http; http = req->data; @@ -552,7 +532,7 @@ nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req) return; } - future = http->receive_future; + future = http->receive_future; http->receive_future = NULL; nxt_py_asgi_http_set_result(http, future, msg); @@ -560,15 +540,13 @@ nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req) Py_DECREF(msg); } - int -nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) -{ - char *body_str; +nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) { + char *body_str; ssize_t sent; - PyObject *future, *exc, *res; + PyObject *future, *exc, *res; Py_ssize_t body_len; - nxt_py_asgi_http_t *http; + nxt_py_asgi_http_t *http; http = nxt_container_of(lnk, nxt_py_asgi_http_t, link); @@ -591,12 +569,12 @@ nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) body_len -= sent; http->send_body_off += sent; - http->bytes_sent += sent; + http->bytes_sent += sent; } Py_CLEAR(http->send_body); - future = http->send_future; + future = http->send_future; http->send_future = NULL; nxt_py_asgi_http_set_result(http, future, Py_None); @@ -606,8 +584,7 @@ nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) fail: exc = PyObject_CallFunctionObjArgs(PyExc_RuntimeError, - nxt_py_failed_to_send_body_str, - NULL); + nxt_py_failed_to_send_body_str, NULL); if (nxt_slow_path(exc == NULL)) { nxt_unit_req_alert(http->req, "RuntimeError create failed"); nxt_python_print_exception(); @@ -616,11 +593,11 @@ nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) Py_INCREF(exc); } - future = http->send_future; + future = http->send_future; http->send_future = NULL; res = PyObject_CallMethodObjArgs(future, nxt_py_set_exception_str, exc, - NULL); + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_alert(http->req, "'set_exception' call failed"); nxt_python_print_exception(); @@ -633,11 +610,9 @@ nxt_py_asgi_http_drain(nxt_queue_link_t *lnk) return NXT_UNIT_ERROR; } - void -nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req) -{ - nxt_py_asgi_http_t *http; +nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req) { + nxt_py_asgi_http_t *http; http = req->data; @@ -650,13 +625,11 @@ nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req) } } - static PyObject * -nxt_py_asgi_http_done(PyObject *self, PyObject *future) -{ +nxt_py_asgi_http_done(PyObject *self, PyObject *future) { int rc; - PyObject *res; - nxt_py_asgi_http_t *http; + PyObject *res; + nxt_py_asgi_http_t *http; http = (nxt_py_asgi_http_t *) self; @@ -669,7 +642,7 @@ nxt_py_asgi_http_done(PyObject *self, PyObject *future) res = PyObject_CallMethodObjArgs(future, nxt_py_result_str, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_error(http->req, - "Python failed to call 'future.result()'"); + "Python failed to call 'future.result()'"); nxt_python_print_exception(); rc = NXT_UNIT_ERROR; diff --git a/src/python/nxt_python_asgi_lifespan.c b/src/python/nxt_python_asgi_lifespan.c index 041cca212..115d953af 100644 --- a/src/python/nxt_python_asgi_lifespan.c +++ b/src/python/nxt_python_asgi_lifespan.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -14,65 +13,69 @@ #include - -typedef struct { - PyObject_HEAD - nxt_py_asgi_ctx_data_t *ctx_data; - int disabled; - int startup_received; - int startup_sent; - int shutdown_received; - int shutdown_sent; - int shutdown_called; - PyObject *startup_future; - PyObject *shutdown_future; - PyObject *receive_future; - PyObject *state; +typedef struct { + PyObject_HEAD nxt_py_asgi_ctx_data_t *ctx_data; + int disabled; + int startup_received; + int startup_sent; + int shutdown_received; + int shutdown_sent; + int shutdown_called; + PyObject *startup_future; + PyObject *shutdown_future; + PyObject *receive_future; + PyObject *state; } nxt_py_asgi_lifespan_t; -static PyObject *nxt_py_asgi_lifespan_target_startup( - nxt_py_asgi_ctx_data_t *ctx_data, nxt_python_target_t *target); -static int nxt_py_asgi_lifespan_target_shutdown( - nxt_py_asgi_lifespan_t *lifespan); -static PyObject *nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none); -static PyObject *nxt_py_asgi_lifespan_send(PyObject *self, PyObject *dict); -static PyObject *nxt_py_asgi_lifespan_send_startup( - nxt_py_asgi_lifespan_t *lifespan, int v, PyObject *dict); -static PyObject *nxt_py_asgi_lifespan_send_(nxt_py_asgi_lifespan_t *lifespan, - int v, int *sent, PyObject **future); -static PyObject *nxt_py_asgi_lifespan_send_shutdown( - nxt_py_asgi_lifespan_t *lifespan, int v, PyObject *dict); -static PyObject *nxt_py_asgi_lifespan_disable(nxt_py_asgi_lifespan_t *lifespan); -static PyObject *nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future); -static void nxt_py_asgi_lifespan_dealloc(PyObject *self); - - -static PyMethodDef nxt_py_asgi_lifespan_methods[] = { - { "receive", nxt_py_asgi_lifespan_receive, METH_NOARGS, 0 }, - { "send", nxt_py_asgi_lifespan_send, METH_O, 0 }, - { "_done", nxt_py_asgi_lifespan_done, METH_O, 0 }, - { NULL, NULL, 0, 0 } -}; +static PyObject * +nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, + nxt_python_target_t *target); +static int +nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan); +static PyObject * +nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none); +static PyObject * +nxt_py_asgi_lifespan_send(PyObject *self, PyObject *dict); +static PyObject * +nxt_py_asgi_lifespan_send_startup(nxt_py_asgi_lifespan_t *lifespan, int v, + PyObject *dict); +static PyObject * +nxt_py_asgi_lifespan_send_(nxt_py_asgi_lifespan_t *lifespan, int v, int *sent, + PyObject **future); +static PyObject * +nxt_py_asgi_lifespan_send_shutdown(nxt_py_asgi_lifespan_t *lifespan, int v, + PyObject *dict); +static PyObject * +nxt_py_asgi_lifespan_disable(nxt_py_asgi_lifespan_t *lifespan); +static PyObject * +nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future); +static void +nxt_py_asgi_lifespan_dealloc(PyObject *self); + -static PyMemberDef nxt_py_asgi_lifespan_members[] = { - { +static PyMethodDef nxt_py_asgi_lifespan_methods[] + = {{"receive", nxt_py_asgi_lifespan_receive, METH_NOARGS, 0}, + {"send", nxt_py_asgi_lifespan_send, METH_O, 0}, + {"_done", nxt_py_asgi_lifespan_done, METH_O, 0}, {NULL, NULL, 0, 0}}; + +static PyMemberDef nxt_py_asgi_lifespan_members[] + = {{ #if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 7) - .name = "state", + .name = "state", #else - .name = (char *)"state", + .name = (char *) "state", #endif - .type = T_OBJECT_EX, - .offset = offsetof(nxt_py_asgi_lifespan_t, state), - .flags = READONLY, + .type = T_OBJECT_EX, + .offset = offsetof(nxt_py_asgi_lifespan_t, state), + .flags = READONLY, #if PY_VERSION_HEX >= NXT_PYTHON_VER(3, 7) - .doc = PyDoc_STR("lifespan.state") + .doc = PyDoc_STR("lifespan.state") #else - .doc = (char *)PyDoc_STR("lifespan.state") + .doc = (char *) PyDoc_STR("lifespan.state") #endif - }, + }, - { NULL, 0, 0, 0, NULL } -}; + {NULL, 0, 0, 0, NULL}}; static PyAsyncMethods nxt_py_asgi_async_methods = { .am_await = nxt_py_asgi_await, @@ -81,7 +84,8 @@ static PyAsyncMethods nxt_py_asgi_async_methods = { static PyTypeObject nxt_py_asgi_lifespan_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "unit._asgi_lifespan", + .tp_name + = "unit._asgi_lifespan", .tp_basicsize = sizeof(nxt_py_asgi_lifespan_t), .tp_dealloc = nxt_py_asgi_lifespan_dealloc, .tp_as_async = &nxt_py_asgi_async_methods, @@ -93,17 +97,15 @@ static PyTypeObject nxt_py_asgi_lifespan_type = { .tp_members = nxt_py_asgi_lifespan_members, }; - int -nxt_py_asgi_lifespan_startup(nxt_py_asgi_ctx_data_t *ctx_data) -{ +nxt_py_asgi_lifespan_startup(nxt_py_asgi_ctx_data_t *ctx_data) { size_t size; - PyObject *lifespan; - PyObject **target_lifespans; + PyObject *lifespan; + PyObject **target_lifespans; nxt_int_t i; - nxt_python_target_t *target; + nxt_python_target_t *target; - size = nxt_py_targets->count * sizeof(PyObject*); + size = nxt_py_targets->count * sizeof(PyObject *); target_lifespans = nxt_unit_malloc(NULL, size); if (nxt_slow_path(target_lifespans == NULL)) { @@ -129,18 +131,16 @@ nxt_py_asgi_lifespan_startup(nxt_py_asgi_ctx_data_t *ctx_data) return NXT_UNIT_OK; } - static PyObject * nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, - nxt_python_target_t *target) -{ - PyObject *scope, *res, *py_task, *receive, *send, *done; - PyObject *stage2; - nxt_py_asgi_lifespan_t *lifespan, *ret; + nxt_python_target_t *target) { + PyObject *scope, *res, *py_task, *receive, *send, *done; + PyObject *stage2; + nxt_py_asgi_lifespan_t *lifespan, *ret; if (nxt_slow_path(PyType_Ready(&nxt_py_asgi_lifespan_type) != 0)) { nxt_unit_alert(NULL, - "Python failed to initialize the 'asgi_lifespan' type object"); + "Python failed to initialize the 'asgi_lifespan' type object"); return NULL; } @@ -170,8 +170,8 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, goto release_send; } - lifespan->startup_future = PyObject_CallObject(ctx_data->loop_create_future, - NULL); + lifespan->startup_future + = PyObject_CallObject(ctx_data->loop_create_future, NULL); if (nxt_slow_path(lifespan->startup_future == NULL)) { nxt_unit_alert(NULL, "Python failed to create Future object"); nxt_python_print_exception(); @@ -179,16 +179,16 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, goto release_done; } - lifespan->ctx_data = ctx_data; - lifespan->disabled = 0; - lifespan->startup_received = 0; - lifespan->startup_sent = 0; + lifespan->ctx_data = ctx_data; + lifespan->disabled = 0; + lifespan->startup_received = 0; + lifespan->startup_sent = 0; lifespan->shutdown_received = 0; - lifespan->shutdown_sent = 0; - lifespan->shutdown_called = 0; - lifespan->shutdown_future = NULL; - lifespan->receive_future = NULL; - lifespan->state = NULL; + lifespan->shutdown_sent = 0; + lifespan->shutdown_called = 0; + lifespan->shutdown_future = NULL; + lifespan->receive_future = NULL; + lifespan->state = NULL; scope = nxt_py_asgi_new_scope(NULL, nxt_py_lifespan_str, nxt_py_2_0_str); if (nxt_slow_path(scope == NULL)) { @@ -197,16 +197,13 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, lifespan->state = PyDict_New(); if (nxt_slow_path(lifespan->state == NULL)) { - nxt_unit_req_error(NULL, - "Python failed to create 'state' dict"); + nxt_unit_req_error(NULL, "Python failed to create 'state' dict"); goto release_future; } - if (nxt_slow_path(PyDict_SetItem(scope, nxt_py_state_str, - lifespan->state) == -1)) - { - nxt_unit_req_error(NULL, - "Python failed to set 'scope.state' item"); + if (nxt_slow_path( + PyDict_SetItem(scope, nxt_py_state_str, lifespan->state) == -1)) { + nxt_unit_req_error(NULL, "Python failed to set 'scope.state' item"); Py_CLEAR(lifespan->state); goto release_future; } @@ -214,8 +211,8 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, if (!target->asgi_legacy) { nxt_unit_req_debug(NULL, "Python call ASGI 3.0 application"); - res = PyObject_CallFunctionObjArgs(target->application, - scope, receive, send, NULL); + res = PyObject_CallFunctionObjArgs(target->application, scope, receive, + send, NULL); } else { nxt_unit_req_debug(NULL, "Python call legacy application"); @@ -223,7 +220,7 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, res = PyObject_CallFunctionObjArgs(target->application, scope, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_log(NULL, NXT_UNIT_LOG_INFO, - "ASGI Lifespan processing exception"); + "ASGI Lifespan processing exception"); nxt_python_print_exception(); lifespan->disabled = 1; @@ -236,7 +233,7 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, if (nxt_slow_path(PyCallable_Check(res) == 0)) { nxt_unit_req_error(NULL, - "Legacy ASGI application returns not a callable"); + "Legacy ASGI application returns not a callable"); Py_DECREF(res); @@ -262,8 +259,8 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, goto release_scope; } - py_task = PyObject_CallFunctionObjArgs(ctx_data->loop_create_task, res, - NULL); + py_task + = PyObject_CallFunctionObjArgs(ctx_data->loop_create_task, res, NULL); if (nxt_slow_path(py_task == NULL)) { nxt_unit_alert(NULL, "Python failed to call the create_task"); nxt_python_print_exception(); @@ -274,7 +271,7 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, Py_DECREF(res); res = PyObject_CallMethodObjArgs(py_task, nxt_py_add_done_callback_str, - done, NULL); + done, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Python failed to call 'task.add_done_callback'"); nxt_python_print_exception(); @@ -284,7 +281,7 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, Py_DECREF(res); res = PyObject_CallFunctionObjArgs(ctx_data->loop_run_until_complete, - lifespan->startup_future, NULL); + lifespan->startup_future, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Python failed to call loop.run_until_complete"); nxt_python_print_exception(); @@ -317,18 +314,16 @@ nxt_py_asgi_lifespan_target_startup(nxt_py_asgi_ctx_data_t *ctx_data, return (PyObject *) ret; } - int -nxt_py_asgi_lifespan_shutdown(nxt_unit_ctx_t *ctx) -{ +nxt_py_asgi_lifespan_shutdown(nxt_unit_ctx_t *ctx) { nxt_int_t i, ret; - nxt_py_asgi_lifespan_t *lifespan; - nxt_py_asgi_ctx_data_t *ctx_data; + nxt_py_asgi_lifespan_t *lifespan; + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = ctx->data; for (i = 0; i < nxt_py_targets->count; i++) { - lifespan = (nxt_py_asgi_lifespan_t *)ctx_data->target_lifespans[i]; + lifespan = (nxt_py_asgi_lifespan_t *) ctx_data->target_lifespans[i]; ret = nxt_py_asgi_lifespan_target_shutdown(lifespan); if (nxt_slow_path(ret != NXT_UNIT_OK)) { @@ -341,12 +336,10 @@ nxt_py_asgi_lifespan_shutdown(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static int -nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) -{ - PyObject *msg, *future, *res; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) { + PyObject *msg, *future, *res; + nxt_py_asgi_ctx_data_t *ctx_data; ctx_data = lifespan->ctx_data; @@ -357,14 +350,14 @@ nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) lifespan->shutdown_called = 1; if (lifespan->receive_future != NULL) { - future = lifespan->receive_future; + future = lifespan->receive_future; lifespan->receive_future = NULL; msg = nxt_py_asgi_new_msg(NULL, nxt_py_lifespan_shutdown_str); if (nxt_fast_path(msg != NULL)) { - res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, - msg, NULL); + res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, + NULL); Py_XDECREF(res); Py_DECREF(msg); } @@ -376,8 +369,8 @@ nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) return NXT_UNIT_OK; } - lifespan->shutdown_future = PyObject_CallObject(ctx_data->loop_create_future, - NULL); + lifespan->shutdown_future + = PyObject_CallObject(ctx_data->loop_create_future, NULL); if (nxt_slow_path(lifespan->shutdown_future == NULL)) { nxt_unit_alert(NULL, "Python failed to create Future object"); nxt_python_print_exception(); @@ -385,7 +378,7 @@ nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) } res = PyObject_CallFunctionObjArgs(ctx_data->loop_run_until_complete, - lifespan->shutdown_future, NULL); + lifespan->shutdown_future, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Python failed to call loop.run_until_complete"); nxt_python_print_exception(); @@ -398,13 +391,11 @@ nxt_py_asgi_lifespan_target_shutdown(nxt_py_asgi_lifespan_t *lifespan) return NXT_UNIT_OK; } - static PyObject * -nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none) -{ - PyObject *msg, *future; - nxt_py_asgi_lifespan_t *lifespan; - nxt_py_asgi_ctx_data_t *ctx_data; +nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none) { + PyObject *msg, *future; + nxt_py_asgi_lifespan_t *lifespan; + nxt_py_asgi_ctx_data_t *ctx_data; lifespan = (nxt_py_asgi_lifespan_t *) self; ctx_data = lifespan->ctx_data; @@ -417,7 +408,7 @@ nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none) nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create Future object"); + "failed to create Future object"); } if (!lifespan->startup_received) { @@ -442,61 +433,54 @@ nxt_py_asgi_lifespan_receive(PyObject *self, PyObject *none) return future; } - static PyObject * -nxt_py_asgi_lifespan_send(PyObject *self, PyObject *dict) -{ - PyObject *type, *msg; - const char *type_str; +nxt_py_asgi_lifespan_send(PyObject *self, PyObject *dict) { + PyObject *type, *msg; + const char *type_str; Py_ssize_t type_len; - nxt_py_asgi_lifespan_t *lifespan; + nxt_py_asgi_lifespan_t *lifespan; - static const nxt_str_t startup_complete - = nxt_string("lifespan.startup.complete"); - static const nxt_str_t startup_failed - = nxt_string("lifespan.startup.failed"); - static const nxt_str_t shutdown_complete - = nxt_string("lifespan.shutdown.complete"); - static const nxt_str_t shutdown_failed - = nxt_string("lifespan.shutdown.failed"); + static const nxt_str_t startup_complete + = nxt_string("lifespan.startup.complete"); + static const nxt_str_t startup_failed + = nxt_string("lifespan.startup.failed"); + static const nxt_str_t shutdown_complete + = nxt_string("lifespan.shutdown.complete"); + static const nxt_str_t shutdown_failed + = nxt_string("lifespan.shutdown.failed"); lifespan = (nxt_py_asgi_lifespan_t *) self; type = PyDict_GetItem(dict, nxt_py_type_str); if (nxt_slow_path(type == NULL || !PyUnicode_Check(type))) { nxt_unit_error(NULL, - "asgi_lifespan_send: 'type' is not a unicode string"); - return PyErr_Format(PyExc_TypeError, - "'type' is not a unicode string"); + "asgi_lifespan_send: 'type' is not a unicode string"); + return PyErr_Format(PyExc_TypeError, "'type' is not a unicode string"); } type_str = PyUnicode_AsUTF8AndSize(type, &type_len); - nxt_unit_debug(NULL, "asgi_lifespan_send type is '%.*s'", - (int) type_len, type_str); + nxt_unit_debug(NULL, "asgi_lifespan_send type is '%.*s'", (int) type_len, + type_str); if (type_len == (Py_ssize_t) startup_complete.length - && memcmp(type_str, startup_complete.start, type_len) == 0) - { + && memcmp(type_str, startup_complete.start, type_len) == 0) { return nxt_py_asgi_lifespan_send_startup(lifespan, 0, NULL); } if (type_len == (Py_ssize_t) startup_failed.length - && memcmp(type_str, startup_failed.start, type_len) == 0) - { + && memcmp(type_str, startup_failed.start, type_len) == 0) { msg = PyDict_GetItem(dict, nxt_py_message_str); return nxt_py_asgi_lifespan_send_startup(lifespan, 1, msg); } if (type_len == (Py_ssize_t) shutdown_complete.length - && memcmp(type_str, shutdown_complete.start, type_len) == 0) - { + && memcmp(type_str, shutdown_complete.start, type_len) == 0) { return nxt_py_asgi_lifespan_send_shutdown(lifespan, 0, NULL); } if (type_len == (Py_ssize_t) shutdown_failed.length - && memcmp(type_str, shutdown_failed.start, type_len) == 0) - { + && memcmp(type_str, shutdown_failed.start, type_len) == 0) { msg = PyDict_GetItem(dict, nxt_py_message_str); return nxt_py_asgi_lifespan_send_shutdown(lifespan, 1, msg); } @@ -504,12 +488,10 @@ nxt_py_asgi_lifespan_send(PyObject *self, PyObject *dict) return nxt_py_asgi_lifespan_disable(lifespan); } - static PyObject * nxt_py_asgi_lifespan_send_startup(nxt_py_asgi_lifespan_t *lifespan, int v, - PyObject *message) -{ - const char *message_str; + PyObject *message) { + const char *message_str; Py_ssize_t message_len; if (nxt_slow_path(v != 0)) { @@ -522,17 +504,14 @@ nxt_py_asgi_lifespan_send_startup(nxt_py_asgi_lifespan_t *lifespan, int v, } } - return nxt_py_asgi_lifespan_send_(lifespan, v, - &lifespan->startup_sent, - &lifespan->startup_future); + return nxt_py_asgi_lifespan_send_(lifespan, v, &lifespan->startup_sent, + &lifespan->startup_future); } - static PyObject * nxt_py_asgi_lifespan_send_(nxt_py_asgi_lifespan_t *lifespan, int v, int *sent, - PyObject **pfuture) -{ - PyObject *future, *res; + PyObject **pfuture) { + PyObject *future, *res; if (*sent) { return nxt_py_asgi_lifespan_disable(lifespan); @@ -541,11 +520,11 @@ nxt_py_asgi_lifespan_send_(nxt_py_asgi_lifespan_t *lifespan, int v, int *sent, *sent = 1 + v; if (*pfuture != NULL) { - future = *pfuture; + future = *pfuture; *pfuture = NULL; - res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, - Py_None, NULL); + res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, Py_None, + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Failed to call 'future.set_result'"); nxt_python_print_exception(); @@ -562,34 +541,27 @@ nxt_py_asgi_lifespan_send_(nxt_py_asgi_lifespan_t *lifespan, int v, int *sent, return (PyObject *) lifespan; } - static PyObject * -nxt_py_asgi_lifespan_disable(nxt_py_asgi_lifespan_t *lifespan) -{ +nxt_py_asgi_lifespan_disable(nxt_py_asgi_lifespan_t *lifespan) { nxt_unit_warn(NULL, "Got invalid state transition on lifespan protocol"); lifespan->disabled = 1; return PyErr_Format(PyExc_AssertionError, - "Got invalid state transition on lifespan protocol"); + "Got invalid state transition on lifespan protocol"); } - static PyObject * nxt_py_asgi_lifespan_send_shutdown(nxt_py_asgi_lifespan_t *lifespan, int v, - PyObject *message) -{ - return nxt_py_asgi_lifespan_send_(lifespan, v, - &lifespan->shutdown_sent, - &lifespan->shutdown_future); + PyObject *message) { + return nxt_py_asgi_lifespan_send_(lifespan, v, &lifespan->shutdown_sent, + &lifespan->shutdown_future); } - static PyObject * -nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future) -{ - PyObject *res; - nxt_py_asgi_lifespan_t *lifespan; +nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future) { + PyObject *res; + nxt_py_asgi_lifespan_t *lifespan; nxt_unit_debug(NULL, "asgi_lifespan_done"); @@ -606,18 +578,18 @@ nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future) res = PyObject_CallMethodObjArgs(future, nxt_py_result_str, NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_log(NULL, NXT_UNIT_LOG_INFO, - "ASGI Lifespan processing exception"); + "ASGI Lifespan processing exception"); nxt_python_print_exception(); } Py_XDECREF(res); if (lifespan->startup_future != NULL) { - future = lifespan->startup_future; + future = lifespan->startup_future; lifespan->startup_future = NULL; - res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, - Py_None, NULL); + res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, Py_None, + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Failed to call 'future.set_result'"); nxt_python_print_exception(); @@ -628,11 +600,11 @@ nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future) } if (lifespan->shutdown_future != NULL) { - future = lifespan->shutdown_future; + future = lifespan->shutdown_future; lifespan->shutdown_future = NULL; - res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, - Py_None, NULL); + res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, Py_None, + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_alert(NULL, "Failed to call 'future.set_result'"); nxt_python_print_exception(); @@ -645,11 +617,9 @@ nxt_py_asgi_lifespan_done(PyObject *self, PyObject *future) Py_RETURN_NONE; } - static void -nxt_py_asgi_lifespan_dealloc(PyObject *self) -{ - nxt_py_asgi_lifespan_t *lifespan = (nxt_py_asgi_lifespan_t *)self; +nxt_py_asgi_lifespan_dealloc(PyObject *self) { + nxt_py_asgi_lifespan_t *lifespan = (nxt_py_asgi_lifespan_t *) self; Py_CLEAR(lifespan->state); PyObject_Del(self); diff --git a/src/python/nxt_python_asgi_str.c b/src/python/nxt_python_asgi_str.c index 3bea87d5f..0cde62828 100644 --- a/src/python/nxt_python_asgi_str.c +++ b/src/python/nxt_python_asgi_str.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,130 +11,126 @@ #include -PyObject *nxt_py_1_0_str; -PyObject *nxt_py_1_1_str; -PyObject *nxt_py_2_0_str; -PyObject *nxt_py_2_1_str; -PyObject *nxt_py_3_0_str; -PyObject *nxt_py_add_done_callback_str; -PyObject *nxt_py_asgi_str; -PyObject *nxt_py_bad_state_str; -PyObject *nxt_py_body_str; -PyObject *nxt_py_bytes_str; -PyObject *nxt_py_client_str; -PyObject *nxt_py_code_str; -PyObject *nxt_py_done_str; -PyObject *nxt_py_exception_str; -PyObject *nxt_py_failed_to_send_body_str; -PyObject *nxt_py_headers_str; -PyObject *nxt_py_http_str; -PyObject *nxt_py_http_disconnect_str; -PyObject *nxt_py_http_request_str; -PyObject *nxt_py_http_version_str; -PyObject *nxt_py_https_str; -PyObject *nxt_py_lifespan_str; -PyObject *nxt_py_lifespan_shutdown_str; -PyObject *nxt_py_lifespan_startup_str; -PyObject *nxt_py_method_str; -PyObject *nxt_py_message_str; -PyObject *nxt_py_message_too_big_str; -PyObject *nxt_py_more_body_str; -PyObject *nxt_py_path_str; -PyObject *nxt_py_query_string_str; -PyObject *nxt_py_raw_path_str; -PyObject *nxt_py_result_str; -PyObject *nxt_py_root_path_str; -PyObject *nxt_py_scheme_str; -PyObject *nxt_py_server_str; -PyObject *nxt_py_set_exception_str; -PyObject *nxt_py_set_result_str; -PyObject *nxt_py_spec_version_str; -PyObject *nxt_py_status_str; -PyObject *nxt_py_subprotocol_str; -PyObject *nxt_py_subprotocols_str; -PyObject *nxt_py_text_str; -PyObject *nxt_py_type_str; -PyObject *nxt_py_state_str; -PyObject *nxt_py_version_str; -PyObject *nxt_py_websocket_str; -PyObject *nxt_py_websocket_accept_str; -PyObject *nxt_py_websocket_close_str; -PyObject *nxt_py_websocket_connect_str; -PyObject *nxt_py_websocket_disconnect_str; -PyObject *nxt_py_websocket_receive_str; -PyObject *nxt_py_websocket_send_str; -PyObject *nxt_py_ws_str; -PyObject *nxt_py_wss_str; +PyObject *nxt_py_1_0_str; +PyObject *nxt_py_1_1_str; +PyObject *nxt_py_2_0_str; +PyObject *nxt_py_2_1_str; +PyObject *nxt_py_3_0_str; +PyObject *nxt_py_add_done_callback_str; +PyObject *nxt_py_asgi_str; +PyObject *nxt_py_bad_state_str; +PyObject *nxt_py_body_str; +PyObject *nxt_py_bytes_str; +PyObject *nxt_py_client_str; +PyObject *nxt_py_code_str; +PyObject *nxt_py_done_str; +PyObject *nxt_py_exception_str; +PyObject *nxt_py_failed_to_send_body_str; +PyObject *nxt_py_headers_str; +PyObject *nxt_py_http_str; +PyObject *nxt_py_http_disconnect_str; +PyObject *nxt_py_http_request_str; +PyObject *nxt_py_http_version_str; +PyObject *nxt_py_https_str; +PyObject *nxt_py_lifespan_str; +PyObject *nxt_py_lifespan_shutdown_str; +PyObject *nxt_py_lifespan_startup_str; +PyObject *nxt_py_method_str; +PyObject *nxt_py_message_str; +PyObject *nxt_py_message_too_big_str; +PyObject *nxt_py_more_body_str; +PyObject *nxt_py_path_str; +PyObject *nxt_py_query_string_str; +PyObject *nxt_py_raw_path_str; +PyObject *nxt_py_result_str; +PyObject *nxt_py_root_path_str; +PyObject *nxt_py_scheme_str; +PyObject *nxt_py_server_str; +PyObject *nxt_py_set_exception_str; +PyObject *nxt_py_set_result_str; +PyObject *nxt_py_spec_version_str; +PyObject *nxt_py_status_str; +PyObject *nxt_py_subprotocol_str; +PyObject *nxt_py_subprotocols_str; +PyObject *nxt_py_text_str; +PyObject *nxt_py_type_str; +PyObject *nxt_py_state_str; +PyObject *nxt_py_version_str; +PyObject *nxt_py_websocket_str; +PyObject *nxt_py_websocket_accept_str; +PyObject *nxt_py_websocket_close_str; +PyObject *nxt_py_websocket_connect_str; +PyObject *nxt_py_websocket_disconnect_str; +PyObject *nxt_py_websocket_receive_str; +PyObject *nxt_py_websocket_send_str; +PyObject *nxt_py_ws_str; +PyObject *nxt_py_wss_str; static nxt_python_string_t nxt_py_asgi_strings[] = { - { nxt_string("1.0"), &nxt_py_1_0_str }, - { nxt_string("1.1"), &nxt_py_1_1_str }, - { nxt_string("2.0"), &nxt_py_2_0_str }, - { nxt_string("2.1"), &nxt_py_2_1_str }, - { nxt_string("3.0"), &nxt_py_3_0_str }, - { nxt_string("add_done_callback"), &nxt_py_add_done_callback_str }, - { nxt_string("asgi"), &nxt_py_asgi_str }, - { nxt_string("bad state"), &nxt_py_bad_state_str }, - { nxt_string("body"), &nxt_py_body_str }, - { nxt_string("bytes"), &nxt_py_bytes_str }, - { nxt_string("client"), &nxt_py_client_str }, - { nxt_string("code"), &nxt_py_code_str }, - { nxt_string("done"), &nxt_py_done_str }, - { nxt_string("exception"), &nxt_py_exception_str }, - { nxt_string("failed to send body"), &nxt_py_failed_to_send_body_str }, - { nxt_string("headers"), &nxt_py_headers_str }, - { nxt_string("http"), &nxt_py_http_str }, - { nxt_string("http.disconnect"), &nxt_py_http_disconnect_str }, - { nxt_string("http.request"), &nxt_py_http_request_str }, - { nxt_string("http_version"), &nxt_py_http_version_str }, - { nxt_string("https"), &nxt_py_https_str }, - { nxt_string("lifespan"), &nxt_py_lifespan_str }, - { nxt_string("lifespan.shutdown"), &nxt_py_lifespan_shutdown_str }, - { nxt_string("lifespan.startup"), &nxt_py_lifespan_startup_str }, - { nxt_string("message"), &nxt_py_message_str }, - { nxt_string("message too big"), &nxt_py_message_too_big_str }, - { nxt_string("method"), &nxt_py_method_str }, - { nxt_string("more_body"), &nxt_py_more_body_str }, - { nxt_string("path"), &nxt_py_path_str }, - { nxt_string("query_string"), &nxt_py_query_string_str }, - { nxt_string("raw_path"), &nxt_py_raw_path_str }, - { nxt_string("result"), &nxt_py_result_str }, - { nxt_string("root_path"), &nxt_py_root_path_str }, - { nxt_string("scheme"), &nxt_py_scheme_str }, - { nxt_string("server"), &nxt_py_server_str }, - { nxt_string("set_exception"), &nxt_py_set_exception_str }, - { nxt_string("set_result"), &nxt_py_set_result_str }, - { nxt_string("spec_version"), &nxt_py_spec_version_str }, - { nxt_string("status"), &nxt_py_status_str }, - { nxt_string("subprotocol"), &nxt_py_subprotocol_str }, - { nxt_string("subprotocols"), &nxt_py_subprotocols_str }, - { nxt_string("text"), &nxt_py_text_str }, - { nxt_string("type"), &nxt_py_type_str }, - { nxt_string("state"), &nxt_py_state_str }, - { nxt_string("version"), &nxt_py_version_str }, - { nxt_string("websocket"), &nxt_py_websocket_str }, - { nxt_string("websocket.accept"), &nxt_py_websocket_accept_str }, - { nxt_string("websocket.close"), &nxt_py_websocket_close_str }, - { nxt_string("websocket.connect"), &nxt_py_websocket_connect_str }, - { nxt_string("websocket.disconnect"), &nxt_py_websocket_disconnect_str }, - { nxt_string("websocket.receive"), &nxt_py_websocket_receive_str }, - { nxt_string("websocket.send"), &nxt_py_websocket_send_str }, - { nxt_string("ws"), &nxt_py_ws_str }, - { nxt_string("wss"), &nxt_py_wss_str }, - { nxt_null_string, NULL }, + {nxt_string("1.0"), &nxt_py_1_0_str}, + {nxt_string("1.1"), &nxt_py_1_1_str}, + {nxt_string("2.0"), &nxt_py_2_0_str}, + {nxt_string("2.1"), &nxt_py_2_1_str}, + {nxt_string("3.0"), &nxt_py_3_0_str}, + {nxt_string("add_done_callback"), &nxt_py_add_done_callback_str}, + {nxt_string("asgi"), &nxt_py_asgi_str}, + {nxt_string("bad state"), &nxt_py_bad_state_str}, + {nxt_string("body"), &nxt_py_body_str}, + {nxt_string("bytes"), &nxt_py_bytes_str}, + {nxt_string("client"), &nxt_py_client_str}, + {nxt_string("code"), &nxt_py_code_str}, + {nxt_string("done"), &nxt_py_done_str}, + {nxt_string("exception"), &nxt_py_exception_str}, + {nxt_string("failed to send body"), &nxt_py_failed_to_send_body_str}, + {nxt_string("headers"), &nxt_py_headers_str}, + {nxt_string("http"), &nxt_py_http_str}, + {nxt_string("http.disconnect"), &nxt_py_http_disconnect_str}, + {nxt_string("http.request"), &nxt_py_http_request_str}, + {nxt_string("http_version"), &nxt_py_http_version_str}, + {nxt_string("https"), &nxt_py_https_str}, + {nxt_string("lifespan"), &nxt_py_lifespan_str}, + {nxt_string("lifespan.shutdown"), &nxt_py_lifespan_shutdown_str}, + {nxt_string("lifespan.startup"), &nxt_py_lifespan_startup_str}, + {nxt_string("message"), &nxt_py_message_str}, + {nxt_string("message too big"), &nxt_py_message_too_big_str}, + {nxt_string("method"), &nxt_py_method_str}, + {nxt_string("more_body"), &nxt_py_more_body_str}, + {nxt_string("path"), &nxt_py_path_str}, + {nxt_string("query_string"), &nxt_py_query_string_str}, + {nxt_string("raw_path"), &nxt_py_raw_path_str}, + {nxt_string("result"), &nxt_py_result_str}, + {nxt_string("root_path"), &nxt_py_root_path_str}, + {nxt_string("scheme"), &nxt_py_scheme_str}, + {nxt_string("server"), &nxt_py_server_str}, + {nxt_string("set_exception"), &nxt_py_set_exception_str}, + {nxt_string("set_result"), &nxt_py_set_result_str}, + {nxt_string("spec_version"), &nxt_py_spec_version_str}, + {nxt_string("status"), &nxt_py_status_str}, + {nxt_string("subprotocol"), &nxt_py_subprotocol_str}, + {nxt_string("subprotocols"), &nxt_py_subprotocols_str}, + {nxt_string("text"), &nxt_py_text_str}, + {nxt_string("type"), &nxt_py_type_str}, + {nxt_string("state"), &nxt_py_state_str}, + {nxt_string("version"), &nxt_py_version_str}, + {nxt_string("websocket"), &nxt_py_websocket_str}, + {nxt_string("websocket.accept"), &nxt_py_websocket_accept_str}, + {nxt_string("websocket.close"), &nxt_py_websocket_close_str}, + {nxt_string("websocket.connect"), &nxt_py_websocket_connect_str}, + {nxt_string("websocket.disconnect"), &nxt_py_websocket_disconnect_str}, + {nxt_string("websocket.receive"), &nxt_py_websocket_receive_str}, + {nxt_string("websocket.send"), &nxt_py_websocket_send_str}, + {nxt_string("ws"), &nxt_py_ws_str}, + {nxt_string("wss"), &nxt_py_wss_str}, + {nxt_null_string, NULL}, }; - int -nxt_py_asgi_str_init(void) -{ +nxt_py_asgi_str_init(void) { return nxt_python_init_strings(nxt_py_asgi_strings); } - void -nxt_py_asgi_str_done(void) -{ +nxt_py_asgi_str_done(void) { nxt_python_done_strings(nxt_py_asgi_strings); } diff --git a/src/python/nxt_python_asgi_str.h b/src/python/nxt_python_asgi_str.h index 3c7a3ed92..51ce3bd36 100644 --- a/src/python/nxt_python_asgi_str.h +++ b/src/python/nxt_python_asgi_str.h @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -7,64 +6,66 @@ #define _NXT_PYTHON_ASGI_STR_H_INCLUDED_ -extern PyObject *nxt_py_1_0_str; -extern PyObject *nxt_py_1_1_str; -extern PyObject *nxt_py_2_0_str; -extern PyObject *nxt_py_2_1_str; -extern PyObject *nxt_py_3_0_str; -extern PyObject *nxt_py_add_done_callback_str; -extern PyObject *nxt_py_asgi_str; -extern PyObject *nxt_py_bad_state_str; -extern PyObject *nxt_py_body_str; -extern PyObject *nxt_py_bytes_str; -extern PyObject *nxt_py_client_str; -extern PyObject *nxt_py_code_str; -extern PyObject *nxt_py_done_str; -extern PyObject *nxt_py_exception_str; -extern PyObject *nxt_py_failed_to_send_body_str; -extern PyObject *nxt_py_headers_str; -extern PyObject *nxt_py_http_str; -extern PyObject *nxt_py_http_disconnect_str; -extern PyObject *nxt_py_http_request_str; -extern PyObject *nxt_py_http_version_str; -extern PyObject *nxt_py_https_str; -extern PyObject *nxt_py_lifespan_str; -extern PyObject *nxt_py_lifespan_shutdown_str; -extern PyObject *nxt_py_lifespan_startup_str; -extern PyObject *nxt_py_method_str; -extern PyObject *nxt_py_message_str; -extern PyObject *nxt_py_message_too_big_str; -extern PyObject *nxt_py_more_body_str; -extern PyObject *nxt_py_path_str; -extern PyObject *nxt_py_query_string_str; -extern PyObject *nxt_py_result_str; -extern PyObject *nxt_py_raw_path_str; -extern PyObject *nxt_py_root_path_str; -extern PyObject *nxt_py_scheme_str; -extern PyObject *nxt_py_server_str; -extern PyObject *nxt_py_set_exception_str; -extern PyObject *nxt_py_set_result_str; -extern PyObject *nxt_py_spec_version_str; -extern PyObject *nxt_py_status_str; -extern PyObject *nxt_py_subprotocol_str; -extern PyObject *nxt_py_subprotocols_str; -extern PyObject *nxt_py_text_str; -extern PyObject *nxt_py_type_str; -extern PyObject *nxt_py_state_str; -extern PyObject *nxt_py_version_str; -extern PyObject *nxt_py_websocket_str; -extern PyObject *nxt_py_websocket_accept_str; -extern PyObject *nxt_py_websocket_close_str; -extern PyObject *nxt_py_websocket_connect_str; -extern PyObject *nxt_py_websocket_disconnect_str; -extern PyObject *nxt_py_websocket_receive_str; -extern PyObject *nxt_py_websocket_send_str; -extern PyObject *nxt_py_ws_str; -extern PyObject *nxt_py_wss_str; +extern PyObject *nxt_py_1_0_str; +extern PyObject *nxt_py_1_1_str; +extern PyObject *nxt_py_2_0_str; +extern PyObject *nxt_py_2_1_str; +extern PyObject *nxt_py_3_0_str; +extern PyObject *nxt_py_add_done_callback_str; +extern PyObject *nxt_py_asgi_str; +extern PyObject *nxt_py_bad_state_str; +extern PyObject *nxt_py_body_str; +extern PyObject *nxt_py_bytes_str; +extern PyObject *nxt_py_client_str; +extern PyObject *nxt_py_code_str; +extern PyObject *nxt_py_done_str; +extern PyObject *nxt_py_exception_str; +extern PyObject *nxt_py_failed_to_send_body_str; +extern PyObject *nxt_py_headers_str; +extern PyObject *nxt_py_http_str; +extern PyObject *nxt_py_http_disconnect_str; +extern PyObject *nxt_py_http_request_str; +extern PyObject *nxt_py_http_version_str; +extern PyObject *nxt_py_https_str; +extern PyObject *nxt_py_lifespan_str; +extern PyObject *nxt_py_lifespan_shutdown_str; +extern PyObject *nxt_py_lifespan_startup_str; +extern PyObject *nxt_py_method_str; +extern PyObject *nxt_py_message_str; +extern PyObject *nxt_py_message_too_big_str; +extern PyObject *nxt_py_more_body_str; +extern PyObject *nxt_py_path_str; +extern PyObject *nxt_py_query_string_str; +extern PyObject *nxt_py_result_str; +extern PyObject *nxt_py_raw_path_str; +extern PyObject *nxt_py_root_path_str; +extern PyObject *nxt_py_scheme_str; +extern PyObject *nxt_py_server_str; +extern PyObject *nxt_py_set_exception_str; +extern PyObject *nxt_py_set_result_str; +extern PyObject *nxt_py_spec_version_str; +extern PyObject *nxt_py_status_str; +extern PyObject *nxt_py_subprotocol_str; +extern PyObject *nxt_py_subprotocols_str; +extern PyObject *nxt_py_text_str; +extern PyObject *nxt_py_type_str; +extern PyObject *nxt_py_state_str; +extern PyObject *nxt_py_version_str; +extern PyObject *nxt_py_websocket_str; +extern PyObject *nxt_py_websocket_accept_str; +extern PyObject *nxt_py_websocket_close_str; +extern PyObject *nxt_py_websocket_connect_str; +extern PyObject *nxt_py_websocket_disconnect_str; +extern PyObject *nxt_py_websocket_receive_str; +extern PyObject *nxt_py_websocket_send_str; +extern PyObject *nxt_py_ws_str; +extern PyObject *nxt_py_wss_str; -int nxt_py_asgi_str_init(void); -void nxt_py_asgi_str_done(void); +int +nxt_py_asgi_str_init(void); +void +nxt_py_asgi_str_done(void); -#endif /* _NXT_PYTHON_ASGI_STR_H_INCLUDED_ */ +#endif /* _NXT_PYTHON_ASGI_STR_H_INCLUDED_ */ diff --git a/src/python/nxt_python_asgi_websocket.c b/src/python/nxt_python_asgi_websocket.c index ab1d0324e..7a205ec00 100644 --- a/src/python/nxt_python_asgi_websocket.c +++ b/src/python/nxt_python_asgi_websocket.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -16,7 +15,6 @@ #include #include - enum { NXT_WS_INIT, NXT_WS_CONNECT, @@ -25,56 +23,55 @@ enum { NXT_WS_CLOSED, }; - typedef struct { nxt_queue_link_t link; - nxt_unit_websocket_frame_t *frame; + nxt_unit_websocket_frame_t *frame; } nxt_py_asgi_penging_frame_t; - typedef struct { - PyObject_HEAD - nxt_unit_request_info_t *req; - PyObject *receive_future; - PyObject *receive_exc_str; - int state; - nxt_queue_t pending_frames; - uint64_t pending_payload_len; - uint64_t pending_frame_len; - int pending_fins; + PyObject_HEAD nxt_unit_request_info_t *req; + PyObject *receive_future; + PyObject *receive_exc_str; + int state; + nxt_queue_t pending_frames; + uint64_t pending_payload_len; + uint64_t pending_frame_len; + int pending_fins; } nxt_py_asgi_websocket_t; +static PyObject * +nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none); +static PyObject * +nxt_py_asgi_websocket_send(PyObject *self, PyObject *dict); +static PyObject * +nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict); +static PyObject * +nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, PyObject *dict); +static PyObject * +nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict); +static void +nxt_py_asgi_websocket_receive_done(nxt_py_asgi_websocket_t *ws, PyObject *msg); +static void +nxt_py_asgi_websocket_receive_fail(nxt_py_asgi_websocket_t *ws, PyObject *exc); +static void +nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *f); +static PyObject * +nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, + nxt_unit_websocket_frame_t *frame); +static uint64_t +nxt_py_asgi_websocket_pending_len(nxt_py_asgi_websocket_t *ws); +static nxt_unit_websocket_frame_t * +nxt_py_asgi_websocket_pop_frame(nxt_py_asgi_websocket_t *ws); +static PyObject * +nxt_py_asgi_websocket_disconnect_msg(nxt_py_asgi_websocket_t *ws); +static PyObject * +nxt_py_asgi_websocket_done(PyObject *self, PyObject *future); -static PyObject *nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none); -static PyObject *nxt_py_asgi_websocket_send(PyObject *self, PyObject *dict); -static PyObject *nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, - PyObject *dict); -static PyObject *nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, - PyObject *dict); -static PyObject *nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, - PyObject *dict); -static void nxt_py_asgi_websocket_receive_done(nxt_py_asgi_websocket_t *ws, - PyObject *msg); -static void nxt_py_asgi_websocket_receive_fail(nxt_py_asgi_websocket_t *ws, - PyObject *exc); -static void nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *f); -static PyObject *nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, - nxt_unit_websocket_frame_t *frame); -static uint64_t nxt_py_asgi_websocket_pending_len( - nxt_py_asgi_websocket_t *ws); -static nxt_unit_websocket_frame_t *nxt_py_asgi_websocket_pop_frame( - nxt_py_asgi_websocket_t *ws); -static PyObject *nxt_py_asgi_websocket_disconnect_msg( - nxt_py_asgi_websocket_t *ws); -static PyObject *nxt_py_asgi_websocket_done(PyObject *self, PyObject *future); - - -static PyMethodDef nxt_py_asgi_websocket_methods[] = { - { "receive", nxt_py_asgi_websocket_receive, METH_NOARGS, 0 }, - { "send", nxt_py_asgi_websocket_send, METH_O, 0 }, - { "_done", nxt_py_asgi_websocket_done, METH_O, 0 }, - { NULL, NULL, 0, 0 } -}; + +static PyMethodDef nxt_py_asgi_websocket_methods[] + = {{"receive", nxt_py_asgi_websocket_receive, METH_NOARGS, 0}, + {"send", nxt_py_asgi_websocket_send, METH_O, 0}, + {"_done", nxt_py_asgi_websocket_done, METH_O, 0}, {NULL, NULL, 0, 0}}; static PyAsyncMethods nxt_py_asgi_async_methods = { .am_await = nxt_py_asgi_await, @@ -83,7 +80,8 @@ static PyAsyncMethods nxt_py_asgi_async_methods = { static PyTypeObject nxt_py_asgi_websocket_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "unit._asgi_websocket", + .tp_name + = "unit._asgi_websocket", .tp_basicsize = sizeof(nxt_py_asgi_websocket_t), .tp_dealloc = nxt_py_asgi_dealloc, .tp_as_async = &nxt_py_asgi_async_methods, @@ -94,51 +92,45 @@ static PyTypeObject nxt_py_asgi_websocket_type = { .tp_methods = nxt_py_asgi_websocket_methods, }; -static uint64_t nxt_py_asgi_ws_max_frame_size = 1024 * 1024; -static uint64_t nxt_py_asgi_ws_max_buffer_size = 10 * 1024 * 1024; - +static uint64_t nxt_py_asgi_ws_max_frame_size = 1024 * 1024; +static uint64_t nxt_py_asgi_ws_max_buffer_size = 10 * 1024 * 1024; int -nxt_py_asgi_websocket_init(void) -{ +nxt_py_asgi_websocket_init(void) { if (nxt_slow_path(PyType_Ready(&nxt_py_asgi_websocket_type) != 0)) { nxt_unit_alert(NULL, - "Python failed to initialize the \"asgi_websocket\" type object"); + "Python failed to initialize the \"asgi_websocket\" type object"); return NXT_UNIT_ERROR; } return NXT_UNIT_OK; } - PyObject * -nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req) -{ - nxt_py_asgi_websocket_t *ws; +nxt_py_asgi_websocket_create(nxt_unit_request_info_t *req) { + nxt_py_asgi_websocket_t *ws; ws = PyObject_New(nxt_py_asgi_websocket_t, &nxt_py_asgi_websocket_type); if (nxt_fast_path(ws != NULL)) { - ws->req = req; - ws->receive_future = NULL; + ws->req = req; + ws->receive_future = NULL; ws->receive_exc_str = NULL; - ws->state = NXT_WS_INIT; + ws->state = NXT_WS_INIT; nxt_queue_init(&ws->pending_frames); ws->pending_payload_len = 0; - ws->pending_frame_len = 0; - ws->pending_fins = 0; + ws->pending_frame_len = 0; + ws->pending_fins = 0; } return (PyObject *) ws; } - static PyObject * -nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none) -{ - PyObject *future, *msg; - nxt_py_asgi_ctx_data_t *ctx_data; - nxt_py_asgi_websocket_t *ws; +nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none) { + PyObject *future, *msg; + nxt_py_asgi_ctx_data_t *ctx_data; + nxt_py_asgi_websocket_t *ws; ws = (nxt_py_asgi_websocket_t *) self; @@ -154,11 +146,9 @@ nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none) } if (nxt_slow_path(ws->state == NXT_WS_CLOSED)) { - nxt_unit_req_error(ws->req, - "receive() called for closed WebSocket"); + nxt_unit_req_error(ws->req, "receive() called for closed WebSocket"); - return PyErr_Format(PyExc_RuntimeError, - "WebSocket already closed"); + return PyErr_Format(PyExc_RuntimeError, "WebSocket already closed"); } ctx_data = ws->req->ctx->data; @@ -169,7 +159,7 @@ nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none) nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "failed to create Future object"); + "failed to create Future object"); } if (nxt_slow_path(ws->state == NXT_WS_INIT)) { @@ -198,74 +188,68 @@ nxt_py_asgi_websocket_receive(PyObject *self, PyObject *none) return future; } - static PyObject * -nxt_py_asgi_websocket_send(PyObject *self, PyObject *dict) -{ - PyObject *type; - const char *type_str; +nxt_py_asgi_websocket_send(PyObject *self, PyObject *dict) { + PyObject *type; + const char *type_str; Py_ssize_t type_len; - nxt_py_asgi_websocket_t *ws; + nxt_py_asgi_websocket_t *ws; - static const nxt_str_t websocket_accept = nxt_string("websocket.accept"); - static const nxt_str_t websocket_close = nxt_string("websocket.close"); - static const nxt_str_t websocket_send = nxt_string("websocket.send"); + static const nxt_str_t websocket_accept = nxt_string("websocket.accept"); + static const nxt_str_t websocket_close = nxt_string("websocket.close"); + static const nxt_str_t websocket_send = nxt_string("websocket.send"); ws = (nxt_py_asgi_websocket_t *) self; type = PyDict_GetItem(dict, nxt_py_type_str); if (nxt_slow_path(type == NULL || !PyUnicode_Check(type))) { nxt_unit_req_error(ws->req, "asgi_websocket_send: " - "'type' is not a unicode string"); - return PyErr_Format(PyExc_TypeError, - "'type' is not a unicode string"); + "'type' is not a unicode string"); + return PyErr_Format(PyExc_TypeError, "'type' is not a unicode string"); } type_str = PyUnicode_AsUTF8AndSize(type, &type_len); nxt_unit_req_debug(ws->req, "asgi_websocket_send type is '%.*s'", - (int) type_len, type_str); + (int) type_len, type_str); if (type_len == (Py_ssize_t) websocket_accept.length - && memcmp(type_str, websocket_accept.start, type_len) == 0) - { + && memcmp(type_str, websocket_accept.start, type_len) == 0) { return nxt_py_asgi_websocket_accept(ws, dict); } if (type_len == (Py_ssize_t) websocket_close.length - && memcmp(type_str, websocket_close.start, type_len) == 0) - { + && memcmp(type_str, websocket_close.start, type_len) == 0) { return nxt_py_asgi_websocket_close(ws, dict); } if (type_len == (Py_ssize_t) websocket_send.length - && memcmp(type_str, websocket_send.start, type_len) == 0) - { + && memcmp(type_str, websocket_send.start, type_len) == 0) { return nxt_py_asgi_websocket_send_frame(ws, dict); } - nxt_unit_req_error(ws->req, "asgi_websocket_send: " - "unexpected 'type': '%.*s'", (int) type_len, type_str); + nxt_unit_req_error(ws->req, + "asgi_websocket_send: " + "unexpected 'type': '%.*s'", + (int) type_len, type_str); return PyErr_Format(PyExc_AssertionError, "unexpected 'type': '%U'", type); } - static PyObject * -nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) -{ - int rc; - char *subprotocol_str; - PyObject *res, *headers, *subprotocol; - Py_ssize_t subprotocol_len; - nxt_py_asgi_calc_size_ctx_t calc_size_ctx; - nxt_py_asgi_add_field_ctx_t add_field_ctx; +nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) { + int rc; + char *subprotocol_str; + PyObject *res, *headers, *subprotocol; + Py_ssize_t subprotocol_len; + nxt_py_asgi_calc_size_ctx_t calc_size_ctx; + nxt_py_asgi_add_field_ctx_t add_field_ctx; - static const nxt_str_t ws_protocol = nxt_string("sec-websocket-protocol"); + static const nxt_str_t ws_protocol = nxt_string("sec-websocket-protocol"); - switch(ws->state) { + switch (ws->state) { case NXT_WS_INIT: return PyErr_Format(PyExc_RuntimeError, - "WebSocket connect not received"); + "WebSocket connect not received"); case NXT_WS_CONNECT: break; @@ -287,13 +271,13 @@ nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) return PyErr_Format(PyExc_RuntimeError, "response already sent"); } - calc_size_ctx.fields_size = 0; + calc_size_ctx.fields_size = 0; calc_size_ctx.fields_count = 0; headers = PyDict_GetItem(dict, nxt_py_headers_str); if (headers != NULL) { res = nxt_py_asgi_enum_headers(headers, nxt_py_asgi_calc_size, - &calc_size_ctx); + &calc_size_ctx); if (nxt_slow_path(res == NULL)) { return NULL; } @@ -312,20 +296,19 @@ nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) subprotocol_len = 0; } - rc = nxt_unit_response_init(ws->req, 101, - calc_size_ctx.fields_count, - calc_size_ctx.fields_size); + rc = nxt_unit_response_init(ws->req, 101, calc_size_ctx.fields_count, + calc_size_ctx.fields_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to allocate response object"); + "failed to allocate response object"); } - add_field_ctx.req = ws->req; + add_field_ctx.req = ws->req; add_field_ctx.content_length = -1; if (headers != NULL) { res = nxt_py_asgi_enum_headers(headers, nxt_py_asgi_add_field, - &add_field_ctx); + &add_field_ctx); if (nxt_slow_path(res == NULL)) { return NULL; } @@ -333,12 +316,10 @@ nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) if (subprotocol_len > 0) { rc = nxt_unit_response_add_field(ws->req, - (const char *) ws_protocol.start, - ws_protocol.length, - subprotocol_str, subprotocol_len); + (const char *) ws_protocol.start, ws_protocol.length, + subprotocol_str, subprotocol_len); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - return PyErr_Format(PyExc_RuntimeError, - "failed to add header"); + return PyErr_Format(PyExc_RuntimeError, "failed to add header"); } } @@ -354,17 +335,15 @@ nxt_py_asgi_websocket_accept(nxt_py_asgi_websocket_t *ws, PyObject *dict) return (PyObject *) ws; } - static PyObject * -nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, PyObject *dict) -{ +nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, PyObject *dict) { int rc; uint16_t status_code; - PyObject *code; + PyObject *code; if (nxt_slow_path(ws->state == NXT_WS_INIT)) { return PyErr_Format(PyExc_RuntimeError, - "WebSocket connect not received"); + "WebSocket connect not received"); } if (nxt_slow_path(ws->state == NXT_WS_DISCONNECTED)) { @@ -384,24 +363,23 @@ nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, PyObject *dict) status_code = (code != NULL) ? htons(PyLong_AsLong(code)) : htons(NXT_WEBSOCKET_CR_NORMAL); - rc = nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, - 1, &status_code, 2); + rc = nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, 1, + &status_code, 2); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to send close frame"); + "failed to send close frame"); } } else { rc = nxt_unit_response_init(ws->req, 403, 0, 0); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to allocate response object"); + "failed to allocate response object"); } rc = nxt_unit_response_send(ws->req); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - return PyErr_Format(PyExc_RuntimeError, - "failed to send response"); + return PyErr_Format(PyExc_RuntimeError, "failed to send response"); } } @@ -412,24 +390,21 @@ nxt_py_asgi_websocket_close(nxt_py_asgi_websocket_t *ws, PyObject *dict) return (PyObject *) ws; } - static PyObject * -nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict) -{ +nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict) { int rc; uint8_t opcode; - PyObject *bytes, *text; - const void *buf; + PyObject *bytes, *text; + const void *buf; Py_ssize_t buf_size; if (nxt_slow_path(ws->state == NXT_WS_INIT)) { return PyErr_Format(PyExc_RuntimeError, - "WebSocket connect not received"); + "WebSocket connect not received"); } if (nxt_slow_path(ws->state == NXT_WS_CONNECT)) { - return PyErr_Format(PyExc_RuntimeError, - "WebSocket not accepted yet"); + return PyErr_Format(PyExc_RuntimeError, "WebSocket not accepted yet"); } if (nxt_slow_path(ws->state == NXT_WS_DISCONNECTED)) { @@ -446,8 +421,7 @@ nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict) } if (nxt_slow_path(bytes != NULL && !PyBytes_Check(bytes))) { - return PyErr_Format(PyExc_TypeError, - "'bytes' is not a byte string"); + return PyErr_Format(PyExc_TypeError, "'bytes' is not a byte string"); } text = PyDict_GetItem(dict, nxt_py_text_str); @@ -456,22 +430,21 @@ nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict) } if (nxt_slow_path(text != NULL && !PyUnicode_Check(text))) { - return PyErr_Format(PyExc_TypeError, - "'text' is not a unicode string"); + return PyErr_Format(PyExc_TypeError, "'text' is not a unicode string"); } if (nxt_slow_path(((bytes != NULL) ^ (text != NULL)) == 0)) { return PyErr_Format(PyExc_ValueError, - "Exactly one of 'bytes' or 'text' must be non-None"); + "Exactly one of 'bytes' or 'text' must be non-None"); } if (bytes != NULL) { - buf = PyBytes_AS_STRING(bytes); + buf = PyBytes_AS_STRING(bytes); buf_size = PyBytes_GET_SIZE(bytes); - opcode = NXT_WEBSOCKET_OP_BINARY; + opcode = NXT_WEBSOCKET_OP_BINARY; } else { - buf = PyUnicode_AsUTF8AndSize(text, &buf_size); + buf = PyUnicode_AsUTF8AndSize(text, &buf_size); opcode = NXT_WEBSOCKET_OP_TEXT; } @@ -484,15 +457,13 @@ nxt_py_asgi_websocket_send_frame(nxt_py_asgi_websocket_t *ws, PyObject *dict) return (PyObject *) ws; } - void -nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) -{ +nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) { uint8_t opcode; uint16_t status_code; uint64_t rest; - PyObject *msg, *exc; - nxt_py_asgi_websocket_t *ws; + PyObject *msg, *exc; + nxt_py_asgi_websocket_t *ws; ws = frame->req->data; @@ -502,13 +473,11 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) if (nxt_slow_path(opcode != NXT_WEBSOCKET_OP_CONT && opcode != NXT_WEBSOCKET_OP_TEXT && opcode != NXT_WEBSOCKET_OP_BINARY - && opcode != NXT_WEBSOCKET_OP_CLOSE)) - { + && opcode != NXT_WEBSOCKET_OP_CLOSE)) { nxt_unit_websocket_done(frame); nxt_unit_req_debug(ws->req, - "asgi_websocket_handler: ignore frame with opcode %d", - opcode); + "asgi_websocket_handler: ignore frame with opcode %d", opcode); return; } @@ -543,15 +512,15 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) if (!nxt_queue_is_empty(&ws->pending_frames)) { if (nxt_slow_path(opcode == NXT_WEBSOCKET_OP_TEXT - || opcode == NXT_WEBSOCKET_OP_BINARY)) - { + || opcode == NXT_WEBSOCKET_OP_BINARY)) { nxt_unit_req_alert(ws->req, - "Invalid state: pending frames with active receiver. " - "CONT frame expected. (%d)", opcode); + "Invalid state: pending frames with active receiver. " + "CONT frame expected. (%d)", + opcode); PyErr_SetString(PyExc_AssertionError, - "Invalid state: pending frames with active receiver. " - "CONT frame expected."); + "Invalid state: pending frames with active receiver. " + "CONT frame expected."); nxt_unit_websocket_done(frame); @@ -579,9 +548,8 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) return; } - exc = PyObject_CallFunctionObjArgs(PyExc_RuntimeError, - nxt_py_bad_state_str, - NULL); + exc = PyObject_CallFunctionObjArgs(PyExc_RuntimeError, nxt_py_bad_state_str, + NULL); if (nxt_slow_path(exc == NULL)) { nxt_unit_req_alert(ws->req, "RuntimeError create failed"); nxt_python_print_exception(); @@ -596,8 +564,8 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) status_code = htons(NXT_WEBSOCKET_CR_MESSAGE_TOO_BIG); - (void) nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, - 1, &status_code, 2); + (void) nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, 1, + &status_code, 2); ws->state = NXT_WS_CLOSED; @@ -608,8 +576,7 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) } exc = PyObject_CallFunctionObjArgs(PyExc_RuntimeError, - nxt_py_message_too_big_str, - NULL); + nxt_py_message_too_big_str, NULL); if (nxt_slow_path(exc == NULL)) { nxt_unit_req_alert(ws->req, "RuntimeError create failed"); nxt_python_print_exception(); @@ -623,13 +590,11 @@ nxt_py_asgi_websocket_handler(nxt_unit_websocket_frame_t *frame) nxt_py_asgi_websocket_receive_fail(ws, exc); } - static void -nxt_py_asgi_websocket_receive_done(nxt_py_asgi_websocket_t *ws, PyObject *msg) -{ - PyObject *future, *res; +nxt_py_asgi_websocket_receive_done(nxt_py_asgi_websocket_t *ws, PyObject *msg) { + PyObject *future, *res; - future = ws->receive_future; + future = ws->receive_future; ws->receive_future = NULL; res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, NULL); @@ -644,17 +609,15 @@ nxt_py_asgi_websocket_receive_done(nxt_py_asgi_websocket_t *ws, PyObject *msg) Py_DECREF(msg); } - static void -nxt_py_asgi_websocket_receive_fail(nxt_py_asgi_websocket_t *ws, PyObject *exc) -{ - PyObject *future, *res; +nxt_py_asgi_websocket_receive_fail(nxt_py_asgi_websocket_t *ws, PyObject *exc) { + PyObject *future, *res; - future = ws->receive_future; + future = ws->receive_future; ws->receive_future = NULL; res = PyObject_CallMethodObjArgs(future, nxt_py_set_exception_str, exc, - NULL); + NULL); if (nxt_slow_path(res == NULL)) { nxt_unit_req_alert(ws->req, "'set_exception' call failed"); nxt_python_print_exception(); @@ -666,18 +629,16 @@ nxt_py_asgi_websocket_receive_fail(nxt_py_asgi_websocket_t *ws, PyObject *exc) Py_DECREF(exc); } - static void -nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) -{ +nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) { int rc; - nxt_py_asgi_websocket_t *ws; - nxt_py_asgi_penging_frame_t *p; + nxt_py_asgi_websocket_t *ws; + nxt_py_asgi_penging_frame_t *p; - nxt_unit_req_debug(frame->req, "asgi_websocket_suspend_frame: " - "%d, %"PRIu64", %d", - frame->header->opcode, frame->payload_len, - frame->header->fin); + nxt_unit_req_debug(frame->req, + "asgi_websocket_suspend_frame: " + "%d, %" PRIu64 ", %d", + frame->header->opcode, frame->payload_len, frame->header->fin); ws = frame->req->data; @@ -688,7 +649,7 @@ nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) nxt_unit_websocket_done(frame); PyErr_SetString(PyExc_RuntimeError, - "Failed to retain frame for suspension."); + "Failed to retain frame for suspension."); return; } @@ -696,12 +657,12 @@ nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) p = nxt_unit_malloc(frame->req->ctx, sizeof(nxt_py_asgi_penging_frame_t)); if (nxt_slow_path(p == NULL)) { nxt_unit_req_alert(ws->req, - "Failed to allocate buffer to suspend frame."); + "Failed to allocate buffer to suspend frame."); nxt_unit_websocket_done(frame); PyErr_SetString(PyExc_RuntimeError, - "Failed to allocate buffer to suspend frame."); + "Failed to allocate buffer to suspend frame."); return; } @@ -710,7 +671,7 @@ nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) nxt_queue_insert_tail(&ws->pending_frames, &p->link); ws->pending_payload_len += frame->payload_len; - ws->pending_fins += frame->header->fin; + ws->pending_fins += frame->header->fin; if (frame->header->fin) { ws->pending_frame_len = 0; @@ -725,33 +686,29 @@ nxt_py_asgi_websocket_suspend_frame(nxt_unit_websocket_frame_t *frame) } } - static PyObject * nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, - nxt_unit_websocket_frame_t *frame) -{ + nxt_unit_websocket_frame_t *frame) { int fin; - char *buf; + char *buf; uint8_t code_buf[2], opcode; uint16_t code; - PyObject *msg, *data, *type, *data_key; + PyObject *msg, *data, *type, *data_key; uint64_t payload_len; - nxt_unit_websocket_frame_t *fin_frame; + nxt_unit_websocket_frame_t *fin_frame; nxt_unit_req_debug(ws->req, "asgi_websocket_pop_msg"); fin_frame = NULL; if (nxt_queue_is_empty(&ws->pending_frames) - || (frame != NULL - && frame->header->opcode == NXT_WEBSOCKET_OP_CLOSE)) - { + || (frame != NULL && frame->header->opcode == NXT_WEBSOCKET_OP_CLOSE)) { payload_len = frame->payload_len; } else { if (frame != NULL) { payload_len = ws->pending_payload_len + frame->payload_len; - fin_frame = frame; + fin_frame = frame; } else { payload_len = nxt_py_asgi_websocket_pending_len(ws); @@ -764,12 +721,12 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, if (nxt_slow_path(opcode == NXT_WEBSOCKET_OP_CONT)) { nxt_unit_req_alert(ws->req, - "Invalid state: attempt to process CONT frame."); + "Invalid state: attempt to process CONT frame."); nxt_unit_websocket_done(frame); return PyErr_Format(PyExc_AssertionError, - "Invalid state: attempt to process CONT frame."); + "Invalid state: attempt to process CONT frame."); } type = nxt_py_websocket_receive_str; @@ -779,17 +736,17 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, buf = nxt_unit_malloc(frame->req->ctx, payload_len); if (nxt_slow_path(buf == NULL)) { nxt_unit_req_alert(ws->req, - "Failed to allocate buffer for payload (%d).", - (int) payload_len); + "Failed to allocate buffer for payload (%d).", + (int) payload_len); nxt_unit_websocket_done(frame); return PyErr_Format(PyExc_RuntimeError, - "Failed to allocate buffer for payload (%d).", - (int) payload_len); + "Failed to allocate buffer for payload (%d).", + (int) payload_len); } - data = NULL; + data = NULL; data_key = nxt_py_text_str; break; @@ -798,17 +755,16 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, data = PyBytes_FromStringAndSize(NULL, payload_len); if (nxt_slow_path(data == NULL)) { nxt_unit_req_alert(ws->req, - "Failed to create Bytes for payload (%d).", - (int) payload_len); + "Failed to create Bytes for payload (%d).", (int) payload_len); nxt_python_print_exception(); nxt_unit_websocket_done(frame); return PyErr_Format(PyExc_RuntimeError, - "Failed to create Bytes for payload."); + "Failed to create Bytes for payload."); } - buf = (char *) PyBytes_AS_STRING(data); + buf = (char *) PyBytes_AS_STRING(data); data_key = nxt_py_bytes_str; break; @@ -826,18 +782,16 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, data = PyLong_FromLong(code); if (nxt_slow_path(data == NULL)) { - nxt_unit_req_alert(ws->req, - "Failed to create Long from code %d.", - (int) code); + nxt_unit_req_alert(ws->req, "Failed to create Long from code %d.", + (int) code); nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "Failed to create Long from code %d.", - (int) code); + "Failed to create Long from code %d.", (int) code); } - buf = NULL; - type = nxt_py_websocket_disconnect_str; + buf = NULL; + type = nxt_py_websocket_disconnect_str; data_key = nxt_py_code_str; break; @@ -848,11 +802,11 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, nxt_unit_websocket_done(frame); return PyErr_Format(PyExc_AssertionError, "Unexpected opcode %d", - opcode); + opcode); } if (buf != NULL) { - fin = frame->header->fin; + fin = frame->header->fin; buf += nxt_unit_websocket_read(frame, buf, frame->payload_len); nxt_unit_websocket_done(frame); @@ -860,7 +814,7 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, if (!fin) { while (!nxt_queue_is_empty(&ws->pending_frames)) { frame = nxt_py_asgi_websocket_pop_frame(ws); - fin = frame->header->fin; + fin = frame->header->fin; buf += nxt_unit_websocket_read(frame, buf, frame->payload_len); @@ -873,7 +827,7 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, if (fin_frame != NULL) { buf += nxt_unit_websocket_read(fin_frame, buf, - fin_frame->payload_len); + fin_frame->payload_len); nxt_unit_websocket_done(fin_frame); } } @@ -887,12 +841,12 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, if (nxt_slow_path(data == NULL)) { nxt_unit_req_alert(ws->req, - "Failed to create Unicode for payload (%d).", - (int) payload_len); + "Failed to create Unicode for payload (%d).", + (int) payload_len); nxt_python_print_exception(); return PyErr_Format(PyExc_RuntimeError, - "Failed to create Unicode."); + "Failed to create Unicode."); } } } @@ -910,7 +864,7 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, Py_DECREF(data); return PyErr_Format(PyExc_RuntimeError, - "Python failed to set 'msg.data' item"); + "Python failed to set 'msg.data' item"); } Py_DECREF(data); @@ -918,12 +872,10 @@ nxt_py_asgi_websocket_pop_msg(nxt_py_asgi_websocket_t *ws, return msg; } - static uint64_t -nxt_py_asgi_websocket_pending_len(nxt_py_asgi_websocket_t *ws) -{ +nxt_py_asgi_websocket_pending_len(nxt_py_asgi_websocket_t *ws) { uint64_t res; - nxt_py_asgi_penging_frame_t *p; + nxt_py_asgi_penging_frame_t *p; res = 0; @@ -932,49 +884,46 @@ nxt_py_asgi_websocket_pending_len(nxt_py_asgi_websocket_t *ws) if (p->frame->header->fin) { nxt_unit_req_debug(ws->req, "asgi_websocket_pending_len: %d", - (int) res); + (int) res); return res; } - } nxt_queue_loop; + } + nxt_queue_loop; nxt_unit_req_debug(ws->req, "asgi_websocket_pending_len: %d (all)", - (int) res); + (int) res); return res; } - static nxt_unit_websocket_frame_t * -nxt_py_asgi_websocket_pop_frame(nxt_py_asgi_websocket_t *ws) -{ - nxt_queue_link_t *lnk; - nxt_unit_websocket_frame_t *frame; - nxt_py_asgi_penging_frame_t *p; +nxt_py_asgi_websocket_pop_frame(nxt_py_asgi_websocket_t *ws) { + nxt_queue_link_t *lnk; + nxt_unit_websocket_frame_t *frame; + nxt_py_asgi_penging_frame_t *p; lnk = nxt_queue_first(&ws->pending_frames); nxt_queue_remove(lnk); p = nxt_queue_link_data(lnk, nxt_py_asgi_penging_frame_t, link); - frame = p->frame; + frame = p->frame; ws->pending_payload_len -= frame->payload_len; - ws->pending_fins -= frame->header->fin; + ws->pending_fins -= frame->header->fin; nxt_unit_free(frame->req->ctx, p); - nxt_unit_req_debug(frame->req, "asgi_websocket_pop_frame: " - "%d, %"PRIu64", %d", - frame->header->opcode, frame->payload_len, - frame->header->fin); + nxt_unit_req_debug(frame->req, + "asgi_websocket_pop_frame: " + "%d, %" PRIu64 ", %d", + frame->header->opcode, frame->payload_len, frame->header->fin); return frame; } - void -nxt_py_asgi_websocket_close_handler(nxt_unit_request_info_t *req) -{ - PyObject *msg, *exc; - nxt_py_asgi_websocket_t *ws; +nxt_py_asgi_websocket_close_handler(nxt_unit_request_info_t *req) { + PyObject *msg, *exc; + nxt_py_asgi_websocket_t *ws; ws = req->data; @@ -1002,11 +951,9 @@ nxt_py_asgi_websocket_close_handler(nxt_unit_request_info_t *req) } } - static PyObject * -nxt_py_asgi_websocket_disconnect_msg(nxt_py_asgi_websocket_t *ws) -{ - PyObject *msg, *code; +nxt_py_asgi_websocket_disconnect_msg(nxt_py_asgi_websocket_t *ws) { + PyObject *msg, *code; msg = nxt_py_asgi_new_msg(ws->req, nxt_py_websocket_disconnect_str); if (nxt_slow_path(msg == NULL)) { @@ -1030,7 +977,7 @@ nxt_py_asgi_websocket_disconnect_msg(nxt_py_asgi_websocket_t *ws) Py_DECREF(code); return PyErr_Format(PyExc_RuntimeError, - "Python failed to set 'msg.code' item"); + "Python failed to set 'msg.code' item"); } Py_DECREF(code); @@ -1038,14 +985,12 @@ nxt_py_asgi_websocket_disconnect_msg(nxt_py_asgi_websocket_t *ws) return msg; } - static PyObject * -nxt_py_asgi_websocket_done(PyObject *self, PyObject *future) -{ +nxt_py_asgi_websocket_done(PyObject *self, PyObject *future) { int rc; uint16_t status_code; - PyObject *res; - nxt_py_asgi_websocket_t *ws; + PyObject *res; + nxt_py_asgi_websocket_t *ws; ws = (nxt_py_asgi_websocket_t *) self; @@ -1057,8 +1002,7 @@ nxt_py_asgi_websocket_done(PyObject *self, PyObject *future) */ res = PyObject_CallMethodObjArgs(future, nxt_py_result_str, NULL); if (nxt_slow_path(res == NULL)) { - nxt_unit_req_error(ws->req, - "Python failed to call 'future.result()'"); + nxt_unit_req_error(ws->req, "Python failed to call 'future.result()'"); nxt_python_print_exception(); rc = NXT_UNIT_ERROR; @@ -1071,11 +1015,11 @@ nxt_py_asgi_websocket_done(PyObject *self, PyObject *future) if (ws->state == NXT_WS_ACCEPTED) { status_code = (rc == NXT_UNIT_OK) - ? htons(NXT_WEBSOCKET_CR_NORMAL) - : htons(NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR); + ? htons(NXT_WEBSOCKET_CR_NORMAL) + : htons(NXT_WEBSOCKET_CR_INTERNAL_SERVER_ERROR); - rc = nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, - 1, &status_code, 2); + rc = nxt_unit_websocket_send(ws->req, NXT_WEBSOCKET_OP_CLOSE, 1, + &status_code, 2); } while (!nxt_queue_is_empty(&ws->pending_frames)) { diff --git a/src/python/nxt_python_wsgi.c b/src/python/nxt_python_wsgi.c index c621097ea..e007e811e 100644 --- a/src/python/nxt_python_wsgi.c +++ b/src/python/nxt_python_wsgi.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Max Romanov * Copyright (C) Valentin V. Bartenev @@ -41,84 +40,102 @@ typedef struct { PyObject_HEAD - uint64_t content_length; + uint64_t content_length; uint64_t bytes_sent; - PyObject *environ; - PyObject *start_resp; - PyObject *write; - nxt_unit_request_info_t *req; - PyThreadState *thread_state; -} nxt_python_ctx_t; - - -static int nxt_python_wsgi_ctx_data_alloc(void **pdata, int main); -static void nxt_python_wsgi_ctx_data_free(void *data); -static int nxt_python_wsgi_run(nxt_unit_ctx_t *ctx); -static void nxt_python_wsgi_done(void); - -static void nxt_python_request_handler(nxt_unit_request_info_t *req); - -static PyObject *nxt_python_create_environ(nxt_python_app_conf_t *c); -static PyObject *nxt_python_copy_environ(nxt_unit_request_info_t *req); -static PyObject *nxt_python_get_environ(nxt_python_ctx_t *pctx, - nxt_python_target_t *app_target); -static int nxt_python_add_sptr(nxt_python_ctx_t *pctx, PyObject *name, + PyObject *environ; + PyObject *start_resp; + PyObject *write; + nxt_unit_request_info_t *req; + PyThreadState *thread_state; +} nxt_python_ctx_t; + +static int +nxt_python_wsgi_ctx_data_alloc(void **pdata, int main); +static void +nxt_python_wsgi_ctx_data_free(void *data); +static int +nxt_python_wsgi_run(nxt_unit_ctx_t *ctx); +static void +nxt_python_wsgi_done(void); + +static void +nxt_python_request_handler(nxt_unit_request_info_t *req); + +static PyObject * +nxt_python_create_environ(nxt_python_app_conf_t *c); +static PyObject * +nxt_python_copy_environ(nxt_unit_request_info_t *req); +static PyObject * +nxt_python_get_environ(nxt_python_ctx_t *pctx, nxt_python_target_t *app_target); +static int +nxt_python_add_sptr(nxt_python_ctx_t *pctx, PyObject *name, nxt_unit_sptr_t *sptr, uint32_t size); -static int nxt_python_add_char(nxt_python_ctx_t *pctx, PyObject *name, - char *src, uint32_t size); -static int nxt_python_add_py_string(nxt_python_ctx_t *pctx, PyObject *name, +static int +nxt_python_add_char(nxt_python_ctx_t *pctx, PyObject *name, char *src, + uint32_t size); +static int +nxt_python_add_py_string(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value); -static int nxt_python_add_field(nxt_python_ctx_t *pctx, - nxt_unit_field_t *field, int n, uint32_t vl); -static PyObject *nxt_python_field_name(const char *name, uint8_t len); -static PyObject *nxt_python_field_value(nxt_unit_field_t *f, int n, +static int +nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, uint32_t vl); -static int nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, - PyObject *value); +static PyObject * +nxt_python_field_name(const char *name, uint8_t len); +static PyObject * +nxt_python_field_value(nxt_unit_field_t *f, int n, uint32_t vl); +static int +nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value); -static PyObject *nxt_py_start_resp(PyObject *self, PyObject *args); -static int nxt_python_response_add_field(nxt_python_ctx_t *pctx, - PyObject *name, PyObject *value, int i); -static int nxt_python_str_buf(PyObject *str, char **buf, uint32_t *len, - PyObject **bytes); -static PyObject *nxt_py_write(PyObject *self, PyObject *args); +static PyObject * +nxt_py_start_resp(PyObject *self, PyObject *args); +static int +nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, + PyObject *value, int i); +static int +nxt_python_str_buf(PyObject *str, char **buf, uint32_t *len, PyObject **bytes); +static PyObject * +nxt_py_write(PyObject *self, PyObject *args); -static void nxt_py_input_dealloc(nxt_python_ctx_t *pctx); -static PyObject *nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args); -static PyObject *nxt_py_input_readline(nxt_python_ctx_t *pctx, - PyObject *args); -static PyObject *nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size); -static PyObject *nxt_py_input_readlines(nxt_python_ctx_t *self, - PyObject *args); +static void +nxt_py_input_dealloc(nxt_python_ctx_t *pctx); +static PyObject * +nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args); +static PyObject * +nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args); +static PyObject * +nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size); +static PyObject * +nxt_py_input_readlines(nxt_python_ctx_t *self, PyObject *args); -static PyObject *nxt_py_input_iter(PyObject *pctx); -static PyObject *nxt_py_input_next(PyObject *pctx); +static PyObject * +nxt_py_input_iter(PyObject *pctx); +static PyObject * +nxt_py_input_next(PyObject *pctx); -static int nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes); +static int +nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes); -static PyMethodDef nxt_py_start_resp_method[] = { - {"unit_start_response", nxt_py_start_resp, METH_VARARGS, ""} -}; +static PyMethodDef nxt_py_start_resp_method[] + = {{"unit_start_response", nxt_py_start_resp, METH_VARARGS, ""}}; -static PyMethodDef nxt_py_write_method[] = { - {"unit_write", nxt_py_write, METH_O, ""} -}; +static PyMethodDef nxt_py_write_method[] + = {{"unit_write", nxt_py_write, METH_O, ""}}; -static PyMethodDef nxt_py_input_methods[] = { - { "read", (PyCFunction) nxt_py_input_read, METH_VARARGS, 0 }, - { "readline", (PyCFunction) nxt_py_input_readline, METH_VARARGS, 0 }, - { "readlines", (PyCFunction) nxt_py_input_readlines, METH_VARARGS, 0 }, - { NULL, NULL, 0, 0 } -}; +static PyMethodDef nxt_py_input_methods[] + = {{"read", (PyCFunction) nxt_py_input_read, METH_VARARGS, 0}, + {"readline", (PyCFunction) nxt_py_input_readline, METH_VARARGS, 0}, + {"readlines", (PyCFunction) nxt_py_input_readlines, METH_VARARGS, 0}, + {NULL, NULL, 0, 0}}; static PyTypeObject nxt_py_input_type = { PyVarObject_HEAD_INIT(NULL, 0) - .tp_name = "unit._input", + .tp_name + = "unit._input", .tp_basicsize = sizeof(nxt_python_ctx_t), .tp_dealloc = (destructor) nxt_py_input_dealloc, .tp_flags = Py_TPFLAGS_DEFAULT, @@ -129,67 +146,64 @@ static PyTypeObject nxt_py_input_type = { }; -static PyObject *nxt_py_environ_ptyp; - -static PyObject *nxt_py_80_str; -static PyObject *nxt_py_close_str; -static PyObject *nxt_py_content_length_str; -static PyObject *nxt_py_content_type_str; -static PyObject *nxt_py_http_str; -static PyObject *nxt_py_https_str; -static PyObject *nxt_py_path_info_str; -static PyObject *nxt_py_query_string_str; -static PyObject *nxt_py_remote_addr_str; -static PyObject *nxt_py_request_method_str; -static PyObject *nxt_py_request_uri_str; -static PyObject *nxt_py_script_name_str; -static PyObject *nxt_py_server_addr_str; -static PyObject *nxt_py_server_name_str; -static PyObject *nxt_py_server_port_str; -static PyObject *nxt_py_server_protocol_str; -static PyObject *nxt_py_wsgi_input_str; -static PyObject *nxt_py_wsgi_uri_scheme_str; +static PyObject *nxt_py_environ_ptyp; + +static PyObject *nxt_py_80_str; +static PyObject *nxt_py_close_str; +static PyObject *nxt_py_content_length_str; +static PyObject *nxt_py_content_type_str; +static PyObject *nxt_py_http_str; +static PyObject *nxt_py_https_str; +static PyObject *nxt_py_path_info_str; +static PyObject *nxt_py_query_string_str; +static PyObject *nxt_py_remote_addr_str; +static PyObject *nxt_py_request_method_str; +static PyObject *nxt_py_request_uri_str; +static PyObject *nxt_py_script_name_str; +static PyObject *nxt_py_server_addr_str; +static PyObject *nxt_py_server_name_str; +static PyObject *nxt_py_server_port_str; +static PyObject *nxt_py_server_protocol_str; +static PyObject *nxt_py_wsgi_input_str; +static PyObject *nxt_py_wsgi_uri_scheme_str; static nxt_python_string_t nxt_python_strings[] = { - { nxt_string("80"), &nxt_py_80_str }, - { nxt_string("close"), &nxt_py_close_str }, - { nxt_string("CONTENT_LENGTH"), &nxt_py_content_length_str }, - { nxt_string("CONTENT_TYPE"), &nxt_py_content_type_str }, - { nxt_string("http"), &nxt_py_http_str }, - { nxt_string("https"), &nxt_py_https_str }, - { nxt_string("PATH_INFO"), &nxt_py_path_info_str }, - { nxt_string("QUERY_STRING"), &nxt_py_query_string_str }, - { nxt_string("REMOTE_ADDR"), &nxt_py_remote_addr_str }, - { nxt_string("REQUEST_METHOD"), &nxt_py_request_method_str }, - { nxt_string("REQUEST_URI"), &nxt_py_request_uri_str }, - { nxt_string("SCRIPT_NAME"), &nxt_py_script_name_str }, - { nxt_string("SERVER_ADDR"), &nxt_py_server_addr_str }, - { nxt_string("SERVER_NAME"), &nxt_py_server_name_str }, - { nxt_string("SERVER_PORT"), &nxt_py_server_port_str }, - { nxt_string("SERVER_PROTOCOL"), &nxt_py_server_protocol_str }, - { nxt_string("wsgi.input"), &nxt_py_wsgi_input_str }, - { nxt_string("wsgi.url_scheme"), &nxt_py_wsgi_uri_scheme_str }, - { nxt_null_string, NULL }, + {nxt_string("80"), &nxt_py_80_str}, + {nxt_string("close"), &nxt_py_close_str}, + {nxt_string("CONTENT_LENGTH"), &nxt_py_content_length_str}, + {nxt_string("CONTENT_TYPE"), &nxt_py_content_type_str}, + {nxt_string("http"), &nxt_py_http_str}, + {nxt_string("https"), &nxt_py_https_str}, + {nxt_string("PATH_INFO"), &nxt_py_path_info_str}, + {nxt_string("QUERY_STRING"), &nxt_py_query_string_str}, + {nxt_string("REMOTE_ADDR"), &nxt_py_remote_addr_str}, + {nxt_string("REQUEST_METHOD"), &nxt_py_request_method_str}, + {nxt_string("REQUEST_URI"), &nxt_py_request_uri_str}, + {nxt_string("SCRIPT_NAME"), &nxt_py_script_name_str}, + {nxt_string("SERVER_ADDR"), &nxt_py_server_addr_str}, + {nxt_string("SERVER_NAME"), &nxt_py_server_name_str}, + {nxt_string("SERVER_PORT"), &nxt_py_server_port_str}, + {nxt_string("SERVER_PROTOCOL"), &nxt_py_server_protocol_str}, + {nxt_string("wsgi.input"), &nxt_py_wsgi_input_str}, + {nxt_string("wsgi.url_scheme"), &nxt_py_wsgi_uri_scheme_str}, + {nxt_null_string, NULL}, }; -static nxt_python_proto_t nxt_py_wsgi_proto = { +static nxt_python_proto_t nxt_py_wsgi_proto = { .ctx_data_alloc = nxt_python_wsgi_ctx_data_alloc, .ctx_data_free = nxt_python_wsgi_ctx_data_free, .run = nxt_python_wsgi_run, .done = nxt_python_wsgi_done, }; - int -nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) -{ - PyObject *obj; +nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) { + PyObject *obj; obj = NULL; - if (nxt_slow_path(nxt_python_init_strings(nxt_python_strings) - != NXT_UNIT_OK)) - { + if (nxt_slow_path( + nxt_python_init_strings(nxt_python_strings) != NXT_UNIT_OK)) { nxt_unit_alert(NULL, "Python failed to init string objects"); goto fail; } @@ -200,7 +214,7 @@ nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) } nxt_py_environ_ptyp = obj; - obj = NULL; + obj = NULL; init->callbacks.request_handler = nxt_python_request_handler; @@ -215,34 +229,32 @@ nxt_python_wsgi_init(nxt_unit_init_t *init, nxt_python_proto_t *proto) return NXT_UNIT_ERROR; } - static int -nxt_python_wsgi_ctx_data_alloc(void **pdata, int main) -{ - nxt_python_ctx_t *pctx; +nxt_python_wsgi_ctx_data_alloc(void **pdata, int main) { + nxt_python_ctx_t *pctx; pctx = PyObject_New(nxt_python_ctx_t, &nxt_py_input_type); if (nxt_slow_path(pctx == NULL)) { nxt_unit_alert(NULL, - "Python failed to create the \"wsgi.input\" object"); + "Python failed to create the \"wsgi.input\" object"); return NXT_UNIT_ERROR; } - pctx->write = NULL; + pctx->write = NULL; pctx->environ = NULL; - pctx->start_resp = PyCFunction_New(nxt_py_start_resp_method, - (PyObject *) pctx); + pctx->start_resp + = PyCFunction_New(nxt_py_start_resp_method, (PyObject *) pctx); if (nxt_slow_path(pctx->start_resp == NULL)) { nxt_unit_alert(NULL, - "Python failed to initialize the \"start_response\" function"); + "Python failed to initialize the \"start_response\" function"); goto fail; } pctx->write = PyCFunction_New(nxt_py_write_method, (PyObject *) pctx); if (nxt_slow_path(pctx->write == NULL)) { nxt_unit_alert(NULL, - "Python failed to initialize the \"write\" function"); + "Python failed to initialize the \"write\" function"); goto fail; } @@ -262,11 +274,9 @@ nxt_python_wsgi_ctx_data_alloc(void **pdata, int main) return NXT_UNIT_ERROR; } - static void -nxt_python_wsgi_ctx_data_free(void *data) -{ - nxt_python_ctx_t *pctx; +nxt_python_wsgi_ctx_data_free(void *data) { + nxt_python_ctx_t *pctx; pctx = data; @@ -276,12 +286,10 @@ nxt_python_wsgi_ctx_data_free(void *data) Py_XDECREF(pctx); } - static int -nxt_python_wsgi_run(nxt_unit_ctx_t *ctx) -{ +nxt_python_wsgi_run(nxt_unit_ctx_t *ctx) { int rc; - nxt_python_ctx_t *pctx; + nxt_python_ctx_t *pctx; pctx = ctx->data; @@ -294,31 +302,27 @@ nxt_python_wsgi_run(nxt_unit_ctx_t *ctx) return rc; } - static void -nxt_python_wsgi_done(void) -{ +nxt_python_wsgi_done(void) { nxt_python_done_strings(nxt_python_strings); Py_XDECREF(nxt_py_environ_ptyp); } - static void -nxt_python_request_handler(nxt_unit_request_info_t *req) -{ +nxt_python_request_handler(nxt_unit_request_info_t *req) { int rc; - PyObject *environ, *args, *response, *iterator, *item; - PyObject *close, *result; + PyObject *environ, *args, *response, *iterator, *item; + PyObject *close, *result; nxt_bool_t prepare_environ; - nxt_python_ctx_t *pctx; - nxt_python_target_t *target; + nxt_python_ctx_t *pctx; + nxt_python_target_t *target; pctx = req->ctx->data; pctx->content_length = -1; - pctx->bytes_sent = 0; - pctx->req = req; + pctx->bytes_sent = 0; + pctx->req = req; PyEval_RestoreThread(pctx->thread_state); @@ -385,8 +389,9 @@ nxt_python_request_handler(nxt_unit_request_info_t *req) if (item == NULL) { if (nxt_slow_path(PyErr_Occurred() != NULL)) { - nxt_unit_req_error(req, "Python failed to iterate over " - "the application response object"); + nxt_unit_req_error(req, + "Python failed to iterate over " + "the application response object"); nxt_python_print_exception(); rc = NXT_UNIT_ERROR; @@ -415,7 +420,7 @@ nxt_python_request_handler(nxt_unit_request_info_t *req) } else { nxt_unit_req_error(req, - "the application returned not an iterable object"); + "the application returned not an iterable object"); nxt_python_print_exception(); rc = NXT_UNIT_ERROR; @@ -460,33 +465,30 @@ nxt_python_request_handler(nxt_unit_request_info_t *req) } } - static PyObject * -nxt_python_create_environ(nxt_python_app_conf_t *c) -{ - PyObject *obj, *err, *environ; +nxt_python_create_environ(nxt_python_app_conf_t *c) { + PyObject *obj, *err, *environ; environ = PyDict_New(); if (nxt_slow_path(environ == NULL)) { nxt_unit_alert(NULL, - "Python failed to create the \"environ\" dictionary"); + "Python failed to create the \"environ\" dictionary"); return NULL; } obj = PyString_FromStringAndSize((char *) nxt_server.start, - nxt_server.length); + nxt_server.length); if (nxt_slow_path(obj == NULL)) { nxt_unit_alert(NULL, - "Python failed to create the \"SERVER_SOFTWARE\" environ value"); + "Python failed to create the \"SERVER_SOFTWARE\" environ value"); goto fail; } - if (nxt_slow_path(PyDict_SetItemString(environ, "SERVER_SOFTWARE", obj) - != 0)) - { + if (nxt_slow_path( + PyDict_SetItemString(environ, "SERVER_SOFTWARE", obj) != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"SERVER_SOFTWARE\" environ value"); + "Python failed to set the \"SERVER_SOFTWARE\" environ value"); goto fail; } @@ -496,14 +498,14 @@ nxt_python_create_environ(nxt_python_app_conf_t *c) if (nxt_slow_path(obj == NULL)) { nxt_unit_alert(NULL, - "Python failed to build the \"wsgi.version\" environ value"); + "Python failed to build the \"wsgi.version\" environ value"); goto fail; } - if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.version", obj) != 0)) - { + if (nxt_slow_path( + PyDict_SetItemString(environ, "wsgi.version", obj) != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"wsgi.version\" environ value"); + "Python failed to set the \"wsgi.version\" environ value"); goto fail; } @@ -512,36 +514,31 @@ nxt_python_create_environ(nxt_python_app_conf_t *c) if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.multithread", - c->threads > 1 ? Py_True : Py_False) - != 0)) - { + c->threads > 1 ? Py_True : Py_False) + != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"wsgi.multithread\" environ value"); + "Python failed to set the \"wsgi.multithread\" environ value"); goto fail; } - if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.multiprocess", - Py_True) - != 0)) - { + if (nxt_slow_path( + PyDict_SetItemString(environ, "wsgi.multiprocess", Py_True) != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"wsgi.multiprocess\" environ value"); + "Python failed to set the \"wsgi.multiprocess\" environ value"); goto fail; } - if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.run_once", - Py_False) - != 0)) - { + if (nxt_slow_path( + PyDict_SetItemString(environ, "wsgi.run_once", Py_False) != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"wsgi.run_once\" environ value"); + "Python failed to set the \"wsgi.run_once\" environ value"); goto fail; } if (nxt_slow_path(PyType_Ready(&nxt_py_input_type) != 0)) { nxt_unit_alert(NULL, - "Python failed to initialize the \"wsgi.input\" type object"); + "Python failed to initialize the \"wsgi.input\" type object"); goto fail; } @@ -553,10 +550,9 @@ nxt_python_create_environ(nxt_python_app_conf_t *c) goto fail; } - if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.errors", err) != 0)) - { + if (nxt_slow_path(PyDict_SetItemString(environ, "wsgi.errors", err) != 0)) { nxt_unit_alert(NULL, - "Python failed to set the \"wsgi.errors\" environ value"); + "Python failed to set the \"wsgi.errors\" environ value"); goto fail; } @@ -570,102 +566,97 @@ nxt_python_create_environ(nxt_python_app_conf_t *c) return NULL; } - static PyObject * -nxt_python_copy_environ(nxt_unit_request_info_t *req) -{ - PyObject *environ; +nxt_python_copy_environ(nxt_unit_request_info_t *req) { + PyObject *environ; environ = PyDict_Copy(nxt_py_environ_ptyp); if (nxt_slow_path(environ == NULL)) { nxt_unit_req_alert(req, - "Python failed to copy the \"environ\" dictionary"); + "Python failed to copy the \"environ\" dictionary"); nxt_python_print_exception(); } return environ; } - static PyObject * nxt_python_get_environ(nxt_python_ctx_t *pctx, - nxt_python_target_t *app_target) -{ + nxt_python_target_t *app_target) { int rc; - char *path; + char *path; uint32_t i, j, vl, path_length; - PyObject *environ; + PyObject *environ; nxt_str_t prefix; - nxt_unit_field_t *f, *f2; - nxt_unit_request_t *r; + nxt_unit_field_t *f, *f2; + nxt_unit_request_t *r; r = pctx->req->request; -#define RC(S) \ - do { \ - rc = (S); \ - if (nxt_slow_path(rc != NXT_UNIT_OK)) { \ - goto fail; \ - } \ - } while(0) +#define RC(S) \ + do { \ + rc = (S); \ + if (nxt_slow_path(rc != NXT_UNIT_OK)) { \ + goto fail; \ + } \ + } while (0) RC(nxt_python_add_sptr(pctx, nxt_py_request_method_str, &r->method, - r->method_length)); + r->method_length)); RC(nxt_python_add_sptr(pctx, nxt_py_request_uri_str, &r->target, - r->target_length)); + r->target_length)); RC(nxt_python_add_sptr(pctx, nxt_py_query_string_str, &r->query, - r->query_length)); + r->query_length)); - prefix = app_target->prefix; + prefix = app_target->prefix; path_length = r->path_length; - path = nxt_unit_sptr_get(&r->path); + path = nxt_unit_sptr_get(&r->path); if (prefix.length > 0 && ((path_length > prefix.length && path[prefix.length] == '/') || path_length == prefix.length) - && memcmp(prefix.start, path, prefix.length) == 0) - { + && memcmp(prefix.start, path, prefix.length) == 0) { RC(nxt_python_add_py_string(pctx, nxt_py_script_name_str, - app_target->py_prefix)); + app_target->py_prefix)); - path += prefix.length; + path += prefix.length; path_length -= prefix.length; } RC(nxt_python_add_char(pctx, nxt_py_path_info_str, path, path_length)); RC(nxt_python_add_sptr(pctx, nxt_py_remote_addr_str, &r->remote, - r->remote_length)); + r->remote_length)); RC(nxt_python_add_sptr(pctx, nxt_py_server_addr_str, &r->local_addr, - r->local_addr_length)); + r->local_addr_length)); if (r->tls) { RC(nxt_python_add_obj(pctx, nxt_py_wsgi_uri_scheme_str, - nxt_py_https_str)); + nxt_py_https_str)); } else { RC(nxt_python_add_obj(pctx, nxt_py_wsgi_uri_scheme_str, - nxt_py_http_str)); + nxt_py_http_str)); } RC(nxt_python_add_sptr(pctx, nxt_py_server_protocol_str, &r->version, - r->version_length)); + r->version_length)); RC(nxt_python_add_sptr(pctx, nxt_py_server_name_str, &r->server_name, - r->server_name_length)); + r->server_name_length)); RC(nxt_python_add_obj(pctx, nxt_py_server_port_str, nxt_py_80_str)); nxt_unit_request_group_dup_fields(pctx->req); for (i = 0; i < r->fields_count;) { - f = r->fields + i; + f = r->fields + i; vl = f->value_length; for (j = i + 1; j < r->fields_count; j++) { f2 = r->fields + j; if (f2->hash != f->hash - || nxt_unit_sptr_get(&f2->name) != nxt_unit_sptr_get(&f->name)) - { + || nxt_unit_sptr_get(&f2->name) + != nxt_unit_sptr_get(&f->name)) { break; } @@ -681,27 +672,27 @@ nxt_python_get_environ(nxt_python_ctx_t *pctx, f = r->fields + r->content_length_field; RC(nxt_python_add_sptr(pctx, nxt_py_content_length_str, &f->value, - f->value_length)); + f->value_length)); } if (r->content_type_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_type_field; RC(nxt_python_add_sptr(pctx, nxt_py_content_type_str, &f->value, - f->value_length)); + f->value_length)); } #undef RC if (nxt_slow_path(PyDict_SetItem(pctx->environ, nxt_py_wsgi_input_str, - (PyObject *) pctx) != 0)) - { + (PyObject *) pctx) + != 0)) { nxt_unit_req_error(pctx->req, - "Python failed to set the \"wsgi.input\" environ value"); + "Python failed to set the \"wsgi.input\" environ value"); goto fail; } - environ = pctx->environ; + environ = pctx->environ; pctx->environ = NULL; return environ; @@ -714,27 +705,22 @@ nxt_python_get_environ(nxt_python_ctx_t *pctx, return NULL; } - static int nxt_python_add_sptr(nxt_python_ctx_t *pctx, PyObject *name, - nxt_unit_sptr_t *sptr, uint32_t size) -{ + nxt_unit_sptr_t *sptr, uint32_t size) { return nxt_python_add_char(pctx, name, nxt_unit_sptr_get(sptr), size); } - static int -nxt_python_add_char(nxt_python_ctx_t *pctx, PyObject *name, - char *src, uint32_t size) -{ +nxt_python_add_char(nxt_python_ctx_t *pctx, PyObject *name, char *src, + uint32_t size) { int res; - PyObject *value; + PyObject *value; value = PyString_FromStringAndSize(src, size); if (nxt_slow_path(value == NULL)) { nxt_unit_req_error(pctx->req, - "Python failed to create value string \"%.*s\"", - (int) size, src); + "Python failed to create value string \"%.*s\"", (int) size, src); nxt_python_print_exception(); return NXT_UNIT_ERROR; @@ -747,14 +733,13 @@ nxt_python_add_char(nxt_python_ctx_t *pctx, PyObject *name, return res; } - -static int nxt_python_add_py_string(nxt_python_ctx_t *pctx, PyObject *name, - PyObject *value) -{ +static int +nxt_python_add_py_string(nxt_python_ctx_t *pctx, PyObject *name, + PyObject *value) { if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { nxt_unit_req_error(pctx->req, - "Python failed to set the \"%s\" environ value", - PyUnicode_AsUTF8(name)); + "Python failed to set the \"%s\" environ value", + PyUnicode_AsUTF8(name)); return NXT_UNIT_ERROR; } @@ -762,21 +747,19 @@ static int nxt_python_add_py_string(nxt_python_ctx_t *pctx, PyObject *name, return NXT_UNIT_OK; } - static int nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, - uint32_t vl) -{ - char *src; - PyObject *name, *value; + uint32_t vl) { + char *src; + PyObject *name, *value; src = nxt_unit_sptr_get(&field->name); name = nxt_python_field_name(src, field->name_length); if (nxt_slow_path(name == NULL)) { nxt_unit_req_error(pctx->req, - "Python failed to create name string \"%.*s\"", - (int) field->name_length, src); + "Python failed to create name string \"%.*s\"", + (int) field->name_length, src); nxt_python_print_exception(); return NXT_UNIT_ERROR; @@ -786,9 +769,9 @@ nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, if (nxt_slow_path(value == NULL)) { nxt_unit_req_error(pctx->req, - "Python failed to create value string \"%.*s\"", - (int) field->value_length, - (char *) nxt_unit_sptr_get(&field->value)); + "Python failed to create value string \"%.*s\"", + (int) field->value_length, + (char *) nxt_unit_sptr_get(&field->value)); nxt_python_print_exception(); goto fail; @@ -796,8 +779,8 @@ nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { nxt_unit_req_error(pctx->req, - "Python failed to set the \"%s\" environ value", - PyUnicode_AsUTF8(name)); + "Python failed to set the \"%s\" environ value", + PyUnicode_AsUTF8(name)); goto fail; } @@ -814,13 +797,11 @@ nxt_python_add_field(nxt_python_ctx_t *pctx, nxt_unit_field_t *field, int n, return NXT_UNIT_ERROR; } - static PyObject * -nxt_python_field_name(const char *name, uint8_t len) -{ - char *p, c; +nxt_python_field_name(const char *name, uint8_t len) { + char *p, c; uint8_t i; - PyObject *res; + PyObject *res; #if PY_MAJOR_VERSION == 3 res = PyUnicode_New(len + 5, 255); @@ -855,19 +836,17 @@ nxt_python_field_name(const char *name, uint8_t len) return res; } - static PyObject * -nxt_python_field_value(nxt_unit_field_t *f, int n, uint32_t vl) -{ +nxt_python_field_value(nxt_unit_field_t *f, int n, uint32_t vl) { int i; - char *p, *src; - PyObject *res; + char *p, *src; + PyObject *res; src = nxt_unit_sptr_get(&f->value); #if PY_MAJOR_VERSION == 3 if (nxt_slow_path(n > 1)) { - char *ptr; + char *ptr; p = nxt_unit_malloc(NULL, vl + 1); if (nxt_slow_path(p == NULL)) { @@ -875,13 +854,13 @@ nxt_python_field_value(nxt_unit_field_t *f, int n, uint32_t vl) } ptr = p; - p = nxt_cpymem(p, src, f->value_length); + p = nxt_cpymem(p, src, f->value_length); for (i = 1; i < n; i++) { p = nxt_cpymem(p, ", ", 2); src = nxt_unit_sptr_get(&f[i].value); - p = nxt_cpymem(p, src, f[i].value_length); + p = nxt_cpymem(p, src, f[i].value_length); } *p = '\0'; @@ -908,21 +887,19 @@ nxt_python_field_value(nxt_unit_field_t *f, int n, uint32_t vl) p = nxt_cpymem(p, ", ", 2); src = nxt_unit_sptr_get(&f[i].value); - p = nxt_cpymem(p, src, f[i].value_length); + p = nxt_cpymem(p, src, f[i].value_length); } #endif return res; } - static int -nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value) -{ +nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value) { if (nxt_slow_path(PyDict_SetItem(pctx->environ, name, value) != 0)) { nxt_unit_req_error(pctx->req, - "Python failed to set the \"%s\" environ value", - PyUnicode_AsUTF8(name)); + "Python failed to set the \"%s\" environ value", + PyUnicode_AsUTF8(name)); return NXT_UNIT_ERROR; } @@ -930,22 +907,20 @@ nxt_python_add_obj(nxt_python_ctx_t *pctx, PyObject *name, PyObject *value) return NXT_UNIT_OK; } - static PyObject * -nxt_py_start_resp(PyObject *self, PyObject *args) -{ +nxt_py_start_resp(PyObject *self, PyObject *args) { int rc, status; - char *status_str, *space_ptr; + char *status_str, *space_ptr; uint32_t status_len; - PyObject *headers, *tuple, *string, *status_bytes; + PyObject *headers, *tuple, *string, *status_bytes; Py_ssize_t i, n, fields_size, fields_count; - nxt_python_ctx_t *pctx; + nxt_python_ctx_t *pctx; pctx = (nxt_python_ctx_t *) self; if (nxt_slow_path(pctx->req == NULL)) { return PyErr_Format(PyExc_RuntimeError, - "start_response() is called " - "outside of WSGI request processing"); + "start_response() is called " + "outside of WSGI request processing"); } n = PyTuple_GET_SIZE(args); @@ -957,16 +932,16 @@ nxt_py_start_resp(PyObject *self, PyObject *args) string = PyTuple_GET_ITEM(args, 0); if (!PyBytes_Check(string) && !PyUnicode_Check(string)) { return PyErr_Format(PyExc_TypeError, - "failed to write first argument (not a string?)"); + "failed to write first argument (not a string?)"); } headers = PyTuple_GET_ITEM(args, 1); if (!PyList_Check(headers)) { return PyErr_Format(PyExc_TypeError, - "the second argument is not a response headers list"); + "the second argument is not a response headers list"); } - fields_size = 0; + fields_size = 0; fields_count = PyList_GET_SIZE(headers); for (i = 0; i < fields_count; i++) { @@ -974,12 +949,12 @@ nxt_py_start_resp(PyObject *self, PyObject *args) if (!PyTuple_Check(tuple)) { return PyErr_Format(PyExc_TypeError, - "the response headers must be a list of tuples"); + "the response headers must be a list of tuples"); } if (PyTuple_GET_SIZE(tuple) != 2) { return PyErr_Format(PyExc_TypeError, - "each header must be a tuple of two items"); + "each header must be a tuple of two items"); } string = PyTuple_GET_ITEM(tuple, 0); @@ -991,7 +966,7 @@ nxt_py_start_resp(PyObject *self, PyObject *args) } else { return PyErr_Format(PyExc_TypeError, - "header #%d name is not a string", (int) i); + "header #%d name is not a string", (int) i); } string = PyTuple_GET_ITEM(tuple, 1); @@ -1003,7 +978,7 @@ nxt_py_start_resp(PyObject *self, PyObject *args) } else { return PyErr_Format(PyExc_TypeError, - "header #%d value is not a string", (int) i); + "header #%d value is not a string", (int) i); } } @@ -1036,17 +1011,17 @@ nxt_py_start_resp(PyObject *self, PyObject *args) rc = nxt_unit_response_init(pctx->req, status, fields_count, fields_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to allocate response object"); + "failed to allocate response object"); } for (i = 0; i < fields_count; i++) { tuple = PyList_GET_ITEM(headers, i); rc = nxt_python_response_add_field(pctx, PyTuple_GET_ITEM(tuple, 0), - PyTuple_GET_ITEM(tuple, 1), i); + PyTuple_GET_ITEM(tuple, 1), i); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - return PyErr_Format(PyExc_RuntimeError, - "failed to add header #%d", (int) i); + return PyErr_Format(PyExc_RuntimeError, "failed to add header #%d", + (int) i); } } @@ -1067,7 +1042,7 @@ nxt_py_start_resp(PyObject *self, PyObject *args) rc = nxt_unit_response_send(pctx->req); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to send response headers"); + "failed to send response headers"); } } @@ -1075,18 +1050,16 @@ nxt_py_start_resp(PyObject *self, PyObject *args) return pctx->write; } - static int nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, - PyObject *value, int i) -{ - int rc; - char *name_str, *value_str; - uint32_t name_length, value_length; - PyObject *name_bytes, *value_bytes; - nxt_off_t content_length; - - name_bytes = NULL; + PyObject *value, int i) { + int rc; + char *name_str, *value_str; + uint32_t name_length, value_length; + PyObject *name_bytes, *value_bytes; + nxt_off_t content_length; + + name_bytes = NULL; value_bytes = NULL; rc = nxt_python_str_buf(name, &name_str, &name_length, &name_bytes); @@ -1100,7 +1073,7 @@ nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, } rc = nxt_unit_response_add_field(pctx->req, name_str, name_length, - value_str, value_length); + value_str, value_length); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto fail; } @@ -1108,8 +1081,10 @@ nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, if (pctx->req->response->fields[i].hash == NXT_UNIT_HASH_CONTENT_LENGTH) { content_length = nxt_off_t_parse((u_char *) value_str, value_length); if (nxt_slow_path(content_length < 0)) { - nxt_unit_req_error(pctx->req, "failed to parse Content-Length " - "value %.*s", (int) value_length, value_str); + nxt_unit_req_error(pctx->req, + "failed to parse Content-Length " + "value %.*s", + (int) value_length, value_str); } else { pctx->content_length = content_length; @@ -1124,13 +1099,11 @@ nxt_python_response_add_field(nxt_python_ctx_t *pctx, PyObject *name, return rc; } - static int -nxt_python_str_buf(PyObject *str, char **buf, uint32_t *len, PyObject **bytes) -{ +nxt_python_str_buf(PyObject *str, char **buf, uint32_t *len, PyObject **bytes) { if (PyBytes_Check(str)) { - *buf = PyBytes_AS_STRING(str); - *len = PyBytes_GET_SIZE(str); + *buf = PyBytes_AS_STRING(str); + *len = PyBytes_GET_SIZE(str); *bytes = NULL; } else { @@ -1146,45 +1119,39 @@ nxt_python_str_buf(PyObject *str, char **buf, uint32_t *len, PyObject **bytes) return NXT_UNIT_OK; } - static PyObject * -nxt_py_write(PyObject *self, PyObject *str) -{ - int rc; +nxt_py_write(PyObject *self, PyObject *str) { + int rc; if (nxt_fast_path(!PyBytes_Check(str))) { return PyErr_Format(PyExc_TypeError, "the argument is not a %s", - NXT_PYTHON_BYTES_TYPE); + NXT_PYTHON_BYTES_TYPE); } rc = nxt_python_write((nxt_python_ctx_t *) self, str); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return PyErr_Format(PyExc_RuntimeError, - "failed to write response value"); + "failed to write response value"); } Py_RETURN_NONE; } - static void -nxt_py_input_dealloc(nxt_python_ctx_t *self) -{ +nxt_py_input_dealloc(nxt_python_ctx_t *self) { PyObject_Del(self); } - static PyObject * -nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args) -{ - char *buf; - PyObject *content, *obj; - Py_ssize_t size, n; +nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args) { + char *buf; + PyObject *content, *obj; + Py_ssize_t size, n; if (nxt_slow_path(pctx->req == NULL)) { return PyErr_Format(PyExc_RuntimeError, - "wsgi.input.read() is called " - "outside of WSGI request processing"); + "wsgi.input.read() is called " + "outside of WSGI request processing"); } size = pctx->req->content_length; @@ -1207,7 +1174,7 @@ nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args) if (size != -1) { return PyErr_Format(PyExc_ValueError, - "the read body size cannot be zero or less"); + "the read body size cannot be zero or less"); } } @@ -1228,18 +1195,16 @@ nxt_py_input_read(nxt_python_ctx_t *pctx, PyObject *args) return content; } - static PyObject * -nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args) -{ - ssize_t ssize; - PyObject *obj; - Py_ssize_t n; +nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args) { + ssize_t ssize; + PyObject *obj; + Py_ssize_t n; if (nxt_slow_path(pctx->req == NULL)) { return PyErr_Format(PyExc_RuntimeError, - "wsgi.input.readline() is called " - "outside of WSGI request processing"); + "wsgi.input.readline() is called " + "outside of WSGI request processing"); } n = PyTuple_GET_SIZE(args); @@ -1263,7 +1228,7 @@ nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args) if (ssize != -1) { return PyErr_Format(PyExc_ValueError, - "the read line size cannot be zero or less"); + "the read line size cannot be zero or less"); } if (PyErr_Occurred()) { @@ -1274,13 +1239,11 @@ nxt_py_input_readline(nxt_python_ctx_t *pctx, PyObject *args) return nxt_py_input_getline(pctx, SSIZE_MAX); } - static PyObject * -nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size) -{ - void *buf; +nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size) { + void *buf; ssize_t res; - PyObject *content; + PyObject *content; res = nxt_unit_request_readline_size(pctx->req, size); if (nxt_slow_path(res < 0)) { @@ -1303,16 +1266,14 @@ nxt_py_input_getline(nxt_python_ctx_t *pctx, size_t size) return content; } - static PyObject * -nxt_py_input_readlines(nxt_python_ctx_t *pctx, PyObject *args) -{ - PyObject *res; +nxt_py_input_readlines(nxt_python_ctx_t *pctx, PyObject *args) { + PyObject *res; if (nxt_slow_path(pctx->req == NULL)) { return PyErr_Format(PyExc_RuntimeError, - "wsgi.input.readlines() is called " - "outside of WSGI request processing"); + "wsgi.input.readlines() is called " + "outside of WSGI request processing"); } res = PyList_New(0); @@ -1320,7 +1281,7 @@ nxt_py_input_readlines(nxt_python_ctx_t *pctx, PyObject *args) return NULL; } - for ( ;; ) { + for (;;) { PyObject *line = nxt_py_input_getline(pctx, SSIZE_MAX); if (nxt_slow_path(line == NULL)) { Py_DECREF(res); @@ -1332,33 +1293,29 @@ nxt_py_input_readlines(nxt_python_ctx_t *pctx, PyObject *args) return res; } - PyList_Append(res, line); + PyList_Append(res, line); Py_DECREF(line); } return res; } - static PyObject * -nxt_py_input_iter(PyObject *self) -{ +nxt_py_input_iter(PyObject *self) { Py_INCREF(self); return self; } - static PyObject * -nxt_py_input_next(PyObject *self) -{ - PyObject *line; - nxt_python_ctx_t *pctx; +nxt_py_input_next(PyObject *self) { + PyObject *line; + nxt_python_ctx_t *pctx; pctx = (nxt_python_ctx_t *) self; if (nxt_slow_path(pctx->req == NULL)) { return PyErr_Format(PyExc_RuntimeError, - "wsgi.input.next() is called " - "outside of WSGI request processing"); + "wsgi.input.next() is called " + "outside of WSGI request processing"); } line = nxt_py_input_getline(pctx, SSIZE_MAX); @@ -1375,15 +1332,13 @@ nxt_py_input_next(PyObject *self) return line; } - static int -nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes) -{ - int rc; - char *str_buf; - uint32_t str_length; +nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes) { + int rc; + char *str_buf; + uint32_t str_length; - str_buf = PyBytes_AS_STRING(bytes); + str_buf = PyBytes_AS_STRING(bytes); str_length = PyBytes_GET_SIZE(bytes); if (nxt_slow_path(str_length == 0)) { @@ -1399,8 +1354,8 @@ nxt_python_write(nxt_python_ctx_t *pctx, PyObject *bytes) * an error if the application tries to write() past that point. */ if (nxt_slow_path(str_length > pctx->content_length - pctx->bytes_sent)) { - nxt_unit_req_error(pctx->req, "content length %"PRIu64" exceeded", - pctx->content_length); + nxt_unit_req_error(pctx->req, "content length %" PRIu64 " exceeded", + pctx->content_length); return NXT_UNIT_ERROR; } diff --git a/src/ruby/nxt_ruby.c b/src/ruby/nxt_ruby.c index 27b868fed..79b76ac6f 100644 --- a/src/ruby/nxt_ruby.c +++ b/src/ruby/nxt_ruby.c @@ -15,82 +15,113 @@ #include -#define NXT_RUBY_RACK_API_VERSION_MAJOR 1 -#define NXT_RUBY_RACK_API_VERSION_MINOR 3 - +#define NXT_RUBY_RACK_API_VERSION_MAJOR 1 +#define NXT_RUBY_RACK_API_VERSION_MINOR 3 typedef struct { - nxt_task_t *task; - nxt_str_t *script; - nxt_ruby_ctx_t *rctx; + nxt_task_t *task; + nxt_str_t *script; + nxt_ruby_ctx_t *rctx; } nxt_ruby_rack_init_t; +static nxt_int_t +nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data); +static VALUE +nxt_ruby_init_basic(VALUE arg); + +static VALUE +nxt_ruby_hook_procs_load(VALUE path); +static VALUE +nxt_ruby_hook_register(VALUE arg); +static VALUE +nxt_ruby_hook_call(VALUE name); + +static VALUE +nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init); + +static VALUE +nxt_ruby_require_rubygems(VALUE arg); +static VALUE +nxt_ruby_bundler_setup(VALUE arg); +static VALUE +nxt_ruby_require_rack(VALUE arg); +static VALUE +nxt_ruby_rack_parse_script(VALUE ctx); +static VALUE +nxt_ruby_rack_env_create(VALUE arg); +static int +nxt_ruby_init_io(nxt_ruby_ctx_t *rctx); +static void +nxt_ruby_request_handler(nxt_unit_request_info_t *req); +static void * +nxt_ruby_request_handler_gvl(void *req); +static int +nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx); +static void * +nxt_ruby_thread_create_gvl(void *rctx); +static VALUE +nxt_ruby_thread_func(VALUE arg); +static void * +nxt_ruby_unit_run(void *ctx); +static void +nxt_ruby_ubf(void *ctx); +static int +nxt_ruby_init_threads(nxt_ruby_app_conf_t *c); +static void +nxt_ruby_join_threads(nxt_unit_ctx_t *ctx, nxt_ruby_app_conf_t *c); + +static VALUE +nxt_ruby_rack_app_run(VALUE arg); +static int +nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env); +nxt_inline void +nxt_ruby_add_sptr(VALUE hash_env, VALUE name, nxt_unit_sptr_t *sptr, + uint32_t len); +static nxt_int_t +nxt_ruby_rack_result_status(nxt_unit_request_info_t *req, VALUE result); +static int +nxt_ruby_rack_result_headers(nxt_unit_request_info_t *req, VALUE result, + nxt_int_t status); +static int +nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg); +static int +nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg); +static int +nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, VALUE result); +static int +nxt_ruby_rack_result_body_file_write(nxt_unit_request_info_t *req, + VALUE filepath); +static void * +nxt_ruby_response_write_cb(void *read_info); +static VALUE +nxt_ruby_rack_result_body_each(VALUE body, VALUE arg, int argc, + const VALUE *argv, VALUE blockarg); +static void * +nxt_ruby_response_write(void *body); + +static void +nxt_ruby_exception_log(nxt_unit_request_info_t *req, uint32_t level, + const char *desc); + +static void +nxt_ruby_ctx_done(nxt_ruby_ctx_t *rctx); +static void +nxt_ruby_atexit(void); + -static nxt_int_t nxt_ruby_start(nxt_task_t *task, - nxt_process_data_t *data); -static VALUE nxt_ruby_init_basic(VALUE arg); - -static VALUE nxt_ruby_hook_procs_load(VALUE path); -static VALUE nxt_ruby_hook_register(VALUE arg); -static VALUE nxt_ruby_hook_call(VALUE name); - -static VALUE nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init); - -static VALUE nxt_ruby_require_rubygems(VALUE arg); -static VALUE nxt_ruby_bundler_setup(VALUE arg); -static VALUE nxt_ruby_require_rack(VALUE arg); -static VALUE nxt_ruby_rack_parse_script(VALUE ctx); -static VALUE nxt_ruby_rack_env_create(VALUE arg); -static int nxt_ruby_init_io(nxt_ruby_ctx_t *rctx); -static void nxt_ruby_request_handler(nxt_unit_request_info_t *req); -static void *nxt_ruby_request_handler_gvl(void *req); -static int nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx); -static void *nxt_ruby_thread_create_gvl(void *rctx); -static VALUE nxt_ruby_thread_func(VALUE arg); -static void *nxt_ruby_unit_run(void *ctx); -static void nxt_ruby_ubf(void *ctx); -static int nxt_ruby_init_threads(nxt_ruby_app_conf_t *c); -static void nxt_ruby_join_threads(nxt_unit_ctx_t *ctx, - nxt_ruby_app_conf_t *c); - -static VALUE nxt_ruby_rack_app_run(VALUE arg); -static int nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env); -nxt_inline void nxt_ruby_add_sptr(VALUE hash_env, VALUE name, - nxt_unit_sptr_t *sptr, uint32_t len); -static nxt_int_t nxt_ruby_rack_result_status(nxt_unit_request_info_t *req, - VALUE result); -static int nxt_ruby_rack_result_headers(nxt_unit_request_info_t *req, - VALUE result, nxt_int_t status); -static int nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg); -static int nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg); -static int nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, - VALUE result); -static int nxt_ruby_rack_result_body_file_write(nxt_unit_request_info_t *req, - VALUE filepath); -static void *nxt_ruby_response_write_cb(void *read_info); -static VALUE nxt_ruby_rack_result_body_each(VALUE body, VALUE arg, - int argc, const VALUE *argv, VALUE blockarg); -static void *nxt_ruby_response_write(void *body); - -static void nxt_ruby_exception_log(nxt_unit_request_info_t *req, - uint32_t level, const char *desc); - -static void nxt_ruby_ctx_done(nxt_ruby_ctx_t *rctx); -static void nxt_ruby_atexit(void); - - -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -static VALUE nxt_ruby_hook_procs; -static VALUE nxt_ruby_rackup; -static VALUE nxt_ruby_call; +static VALUE nxt_ruby_hook_procs; +static VALUE nxt_ruby_rackup; +static VALUE nxt_ruby_call; static uint32_t nxt_ruby_threads; -static nxt_ruby_ctx_t *nxt_ruby_ctxs; +static nxt_ruby_ctx_t *nxt_ruby_ctxs; -NXT_EXPORT nxt_app_module_t nxt_app_module = { +NXT_EXPORT nxt_app_module_t nxt_app_module = { sizeof(compat), compat, nxt_string("ruby"), @@ -102,59 +133,57 @@ NXT_EXPORT nxt_app_module_t nxt_app_module = { }; typedef struct { - nxt_str_t string; - VALUE *v; + nxt_str_t string; + VALUE *v; } nxt_ruby_string_t; -static VALUE nxt_rb_80_str; -static VALUE nxt_rb_content_length_str; -static VALUE nxt_rb_content_type_str; -static VALUE nxt_rb_http_str; -static VALUE nxt_rb_https_str; -static VALUE nxt_rb_path_info_str; -static VALUE nxt_rb_query_string_str; -static VALUE nxt_rb_rack_url_scheme_str; -static VALUE nxt_rb_remote_addr_str; -static VALUE nxt_rb_request_method_str; -static VALUE nxt_rb_request_uri_str; -static VALUE nxt_rb_server_addr_str; -static VALUE nxt_rb_server_name_str; -static VALUE nxt_rb_server_port_str; -static VALUE nxt_rb_server_protocol_str; -static VALUE nxt_rb_on_worker_boot; -static VALUE nxt_rb_on_worker_shutdown; -static VALUE nxt_rb_on_thread_boot; -static VALUE nxt_rb_on_thread_shutdown; +static VALUE nxt_rb_80_str; +static VALUE nxt_rb_content_length_str; +static VALUE nxt_rb_content_type_str; +static VALUE nxt_rb_http_str; +static VALUE nxt_rb_https_str; +static VALUE nxt_rb_path_info_str; +static VALUE nxt_rb_query_string_str; +static VALUE nxt_rb_rack_url_scheme_str; +static VALUE nxt_rb_remote_addr_str; +static VALUE nxt_rb_request_method_str; +static VALUE nxt_rb_request_uri_str; +static VALUE nxt_rb_server_addr_str; +static VALUE nxt_rb_server_name_str; +static VALUE nxt_rb_server_port_str; +static VALUE nxt_rb_server_protocol_str; +static VALUE nxt_rb_on_worker_boot; +static VALUE nxt_rb_on_worker_shutdown; +static VALUE nxt_rb_on_thread_boot; +static VALUE nxt_rb_on_thread_shutdown; static nxt_ruby_string_t nxt_rb_strings[] = { - { nxt_string("80"), &nxt_rb_80_str }, - { nxt_string("CONTENT_LENGTH"), &nxt_rb_content_length_str }, - { nxt_string("CONTENT_TYPE"), &nxt_rb_content_type_str }, - { nxt_string("http"), &nxt_rb_http_str }, - { nxt_string("https"), &nxt_rb_https_str }, - { nxt_string("PATH_INFO"), &nxt_rb_path_info_str }, - { nxt_string("QUERY_STRING"), &nxt_rb_query_string_str }, - { nxt_string("rack.url_scheme"), &nxt_rb_rack_url_scheme_str }, - { nxt_string("REMOTE_ADDR"), &nxt_rb_remote_addr_str }, - { nxt_string("REQUEST_METHOD"), &nxt_rb_request_method_str }, - { nxt_string("REQUEST_URI"), &nxt_rb_request_uri_str }, - { nxt_string("SERVER_ADDR"), &nxt_rb_server_addr_str }, - { nxt_string("SERVER_NAME"), &nxt_rb_server_name_str }, - { nxt_string("SERVER_PORT"), &nxt_rb_server_port_str }, - { nxt_string("SERVER_PROTOCOL"), &nxt_rb_server_protocol_str }, - { nxt_string("on_worker_boot"), &nxt_rb_on_worker_boot }, - { nxt_string("on_worker_shutdown"), &nxt_rb_on_worker_shutdown }, - { nxt_string("on_thread_boot"), &nxt_rb_on_thread_boot }, - { nxt_string("on_thread_shutdown"), &nxt_rb_on_thread_shutdown }, - { nxt_null_string, NULL }, + {nxt_string("80"), &nxt_rb_80_str}, + {nxt_string("CONTENT_LENGTH"), &nxt_rb_content_length_str}, + {nxt_string("CONTENT_TYPE"), &nxt_rb_content_type_str}, + {nxt_string("http"), &nxt_rb_http_str}, + {nxt_string("https"), &nxt_rb_https_str}, + {nxt_string("PATH_INFO"), &nxt_rb_path_info_str}, + {nxt_string("QUERY_STRING"), &nxt_rb_query_string_str}, + {nxt_string("rack.url_scheme"), &nxt_rb_rack_url_scheme_str}, + {nxt_string("REMOTE_ADDR"), &nxt_rb_remote_addr_str}, + {nxt_string("REQUEST_METHOD"), &nxt_rb_request_method_str}, + {nxt_string("REQUEST_URI"), &nxt_rb_request_uri_str}, + {nxt_string("SERVER_ADDR"), &nxt_rb_server_addr_str}, + {nxt_string("SERVER_NAME"), &nxt_rb_server_name_str}, + {nxt_string("SERVER_PORT"), &nxt_rb_server_port_str}, + {nxt_string("SERVER_PROTOCOL"), &nxt_rb_server_protocol_str}, + {nxt_string("on_worker_boot"), &nxt_rb_on_worker_boot}, + {nxt_string("on_worker_shutdown"), &nxt_rb_on_worker_shutdown}, + {nxt_string("on_thread_boot"), &nxt_rb_on_thread_boot}, + {nxt_string("on_thread_shutdown"), &nxt_rb_on_thread_shutdown}, + {nxt_null_string, NULL}, }; - static int -nxt_ruby_init_strings(void) -{ +nxt_ruby_init_strings(void) { VALUE v; - nxt_ruby_string_t *pstr; + nxt_ruby_string_t *pstr; pstr = nxt_rb_strings; @@ -163,8 +192,7 @@ nxt_ruby_init_strings(void) if (nxt_slow_path(v == Qnil)) { nxt_unit_alert(NULL, "Ruby: failed to create const string '%.*s'", - (int) pstr->string.length, - (char *) pstr->string.start); + (int) pstr->string.length, (char *) pstr->string.start); return NXT_UNIT_ERROR; } @@ -179,11 +207,9 @@ nxt_ruby_init_strings(void) return NXT_UNIT_OK; } - static void -nxt_ruby_done_strings(void) -{ - nxt_ruby_string_t *pstr; +nxt_ruby_done_strings(void) { + nxt_ruby_string_t *pstr; pstr = nxt_rb_strings; @@ -196,11 +222,9 @@ nxt_ruby_done_strings(void) } } - static VALUE -nxt_ruby_hook_procs_load(VALUE path) -{ - VALUE module, file, file_obj; +nxt_ruby_hook_procs_load(VALUE path) { + VALUE module, file, file_obj; module = rb_define_module("Unit"); @@ -208,32 +232,30 @@ nxt_ruby_hook_procs_load(VALUE path) rb_gc_register_address(&nxt_ruby_hook_procs); - rb_define_module_function(module, "on_worker_boot", - &nxt_ruby_hook_register, 0); + rb_define_module_function(module, "on_worker_boot", &nxt_ruby_hook_register, + 0); rb_define_module_function(module, "on_worker_shutdown", - &nxt_ruby_hook_register, 0); - rb_define_module_function(module, "on_thread_boot", - &nxt_ruby_hook_register, 0); + &nxt_ruby_hook_register, 0); + rb_define_module_function(module, "on_thread_boot", &nxt_ruby_hook_register, + 0); rb_define_module_function(module, "on_thread_shutdown", - &nxt_ruby_hook_register, 0); + &nxt_ruby_hook_register, 0); - file = rb_const_get(rb_cObject, rb_intern("File")); + file = rb_const_get(rb_cObject, rb_intern("File")); file_obj = rb_funcall(file, rb_intern("read"), 1, path); return rb_funcall(module, rb_intern("module_eval"), 3, file_obj, path, - INT2NUM(1)); + INT2NUM(1)); } - static VALUE -nxt_ruby_hook_register(VALUE arg) -{ - VALUE kernel, callee, callee_str; +nxt_ruby_hook_register(VALUE arg) { + VALUE kernel, callee, callee_str; rb_need_block(); - kernel = rb_const_get(rb_cObject, rb_intern("Kernel")); - callee = rb_funcall(kernel, rb_intern("__callee__"), 0); + kernel = rb_const_get(rb_cObject, rb_intern("Kernel")); + callee = rb_funcall(kernel, rb_intern("__callee__"), 0); callee_str = rb_funcall(callee, rb_intern("to_s"), 0); rb_hash_aset(nxt_ruby_hook_procs, callee_str, rb_block_proc()); @@ -241,11 +263,9 @@ nxt_ruby_hook_register(VALUE arg) return Qnil; } - static VALUE -nxt_ruby_hook_call(VALUE name) -{ - VALUE proc; +nxt_ruby_hook_call(VALUE name) { + VALUE proc; proc = rb_hash_lookup(nxt_ruby_hook_procs, name); if (proc == Qnil) { @@ -255,25 +275,23 @@ nxt_ruby_hook_call(VALUE name) return rb_funcall(proc, rb_intern("call"), 0); } - static nxt_int_t -nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) { int state, rc; VALUE res, path; nxt_ruby_ctx_t ruby_ctx; - nxt_unit_ctx_t *unit_ctx; + nxt_unit_ctx_t *unit_ctx; nxt_unit_init_t ruby_unit_init; - nxt_ruby_app_conf_t *c; + nxt_ruby_app_conf_t *c; nxt_ruby_rack_init_t rack_init; - nxt_common_app_conf_t *conf; + nxt_common_app_conf_t *conf; - static char *argv[2] = { (char *) "NGINX_Unit", (char *) "-e0" }; + static char *argv[2] = {(char *) "NGINX_Unit", (char *) "-e0"}; signal(SIGINT, SIG_IGN); conf = data->app; - c = &conf->u.ruby; + c = &conf->u.ruby; nxt_ruby_threads = c->threads; @@ -284,39 +302,39 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) ruby_options(2, argv); ruby_script("NGINX_Unit"); - ruby_ctx.env = Qnil; + ruby_ctx.env = Qnil; ruby_ctx.io_input = Qnil; ruby_ctx.io_error = Qnil; - ruby_ctx.thread = Qnil; - ruby_ctx.ctx = NULL; - ruby_ctx.req = NULL; + ruby_ctx.thread = Qnil; + ruby_ctx.ctx = NULL; + ruby_ctx.req = NULL; - rack_init.task = task; + rack_init.task = task; rack_init.script = &c->script; - rack_init.rctx = &ruby_ctx; + rack_init.rctx = &ruby_ctx; nxt_ruby_init_strings(); - res = rb_protect(nxt_ruby_init_basic, - (VALUE) (uintptr_t) &rack_init, &state); + res = rb_protect(nxt_ruby_init_basic, (VALUE) (uintptr_t) &rack_init, + &state); if (nxt_slow_path(res == Qnil || state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to init basic variables"); + "Failed to init basic variables"); return NXT_ERROR; } - nxt_ruby_call = Qnil; + nxt_ruby_call = Qnil; nxt_ruby_hook_procs = Qnil; if (c->hooks.start != NULL) { - path = rb_str_new((const char *) c->hooks.start, - (long) c->hooks.length); + path + = rb_str_new((const char *) c->hooks.start, (long) c->hooks.length); rb_protect(nxt_ruby_hook_procs_load, path, &state); rb_str_free(path); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to setup hooks"); + "Failed to setup hooks"); return NXT_ERROR; } } @@ -325,7 +343,7 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) rb_protect(nxt_ruby_hook_call, nxt_rb_on_worker_boot, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_worker_boot()"); + "Failed to call on_worker_boot()"); return NXT_ERROR; } } @@ -347,10 +365,10 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) rb_gc_register_address(&nxt_ruby_call); ruby_ctx.env = rb_protect(nxt_ruby_rack_env_create, - (VALUE) (uintptr_t) &ruby_ctx, &state); + (VALUE) (uintptr_t) &ruby_ctx, &state); if (nxt_slow_path(ruby_ctx.env == Qnil || state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to create 'environ' variable"); + "Failed to create 'environ' variable"); goto fail; } @@ -362,9 +380,9 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) nxt_unit_default_init(task, &ruby_unit_init, conf); ruby_unit_init.callbacks.request_handler = nxt_ruby_request_handler; - ruby_unit_init.callbacks.ready_handler = nxt_ruby_ready_handler; - ruby_unit_init.data = c; - ruby_unit_init.ctx_data = &ruby_ctx; + ruby_unit_init.callbacks.ready_handler = nxt_ruby_ready_handler; + ruby_unit_init.data = c; + ruby_unit_init.ctx_data = &ruby_ctx; unit_ctx = nxt_unit_init(&ruby_unit_init); if (nxt_slow_path(unit_ctx == NULL)) { @@ -375,18 +393,18 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) rb_protect(nxt_ruby_hook_call, nxt_rb_on_thread_boot, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_thread_boot()"); + "Failed to call on_thread_boot()"); } } rc = (intptr_t) rb_thread_call_without_gvl2(nxt_ruby_unit_run, unit_ctx, - nxt_ruby_ubf, unit_ctx); + nxt_ruby_ubf, unit_ctx); if (nxt_ruby_hook_procs != Qnil) { rb_protect(nxt_ruby_hook_call, nxt_rb_on_thread_shutdown, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_thread_shutdown()"); + "Failed to call on_thread_shutdown()"); } } @@ -396,7 +414,7 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) rb_protect(nxt_ruby_hook_call, nxt_rb_on_worker_shutdown, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_worker_shutdown()"); + "Failed to call on_worker_shutdown()"); } } @@ -421,40 +439,36 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) return NXT_ERROR; } - static VALUE -nxt_ruby_init_basic(VALUE arg) -{ +nxt_ruby_init_basic(VALUE arg) { int state; - nxt_ruby_rack_init_t *rack_init; + nxt_ruby_rack_init_t *rack_init; rack_init = (nxt_ruby_rack_init_t *) (uintptr_t) arg; state = rb_enc_find_index("encdb"); if (nxt_slow_path(state == 0)) { nxt_alert(rack_init->task, - "Ruby: Failed to find encoding index 'encdb'"); + "Ruby: Failed to find encoding index 'encdb'"); return Qnil; } rb_funcall(rb_cObject, rb_intern("require"), 1, - rb_str_new2("enc/trans/transdb")); + rb_str_new2("enc/trans/transdb")); return arg; } - static VALUE -nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init) -{ - int state; - VALUE rackup, err; +nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init) { + int state; + VALUE rackup, err; rb_protect(nxt_ruby_require_rubygems, Qnil, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to require 'rubygems' package"); + "Failed to require 'rubygems' package"); return Qnil; } @@ -464,7 +478,7 @@ nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init) if (rb_obj_is_kind_of(err, rb_eLoadError) == Qfalse) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to require 'bundler/setup' package"); + "Failed to require 'bundler/setup' package"); return Qnil; } @@ -474,16 +488,16 @@ nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init) rb_protect(nxt_ruby_require_rack, Qnil, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to require 'rack' package"); + "Failed to require 'rack' package"); return Qnil; } rackup = rb_protect(nxt_ruby_rack_parse_script, - (VALUE) (uintptr_t) rack_init, &state); + (VALUE) (uintptr_t) rack_init, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to parse rack script"); + "Failed to parse rack script"); return Qnil; } @@ -499,43 +513,35 @@ nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init) return RARRAY_PTR(rackup)[0]; } - static VALUE -nxt_ruby_require_rubygems(VALUE arg) -{ +nxt_ruby_require_rubygems(VALUE arg) { return rb_funcall(rb_cObject, rb_intern("require"), 1, - rb_str_new2("rubygems")); + rb_str_new2("rubygems")); } - static VALUE -nxt_ruby_bundler_setup(VALUE arg) -{ +nxt_ruby_bundler_setup(VALUE arg) { return rb_funcall(rb_cObject, rb_intern("require"), 1, - rb_str_new2("bundler/setup")); + rb_str_new2("bundler/setup")); } - static VALUE -nxt_ruby_require_rack(VALUE arg) -{ +nxt_ruby_require_rack(VALUE arg) { return rb_funcall(rb_cObject, rb_intern("require"), 1, rb_str_new2("rack")); } - static VALUE -nxt_ruby_rack_parse_script(VALUE ctx) -{ +nxt_ruby_rack_parse_script(VALUE ctx) { VALUE script, res, rack, builder; - nxt_ruby_rack_init_t *rack_init; + nxt_ruby_rack_init_t *rack_init; rack_init = (nxt_ruby_rack_init_t *) (uintptr_t) ctx; - rack = rb_const_get(rb_cObject, rb_intern("Rack")); + rack = rb_const_get(rb_cObject, rb_intern("Rack")); builder = rb_const_get(rack, rb_intern("Builder")); script = rb_str_new((const char *) rack_init->script->start, - (long) rack_init->script->length); + (long) rack_init->script->length); res = rb_funcall(builder, rb_intern("parse_file"), 1, script); @@ -544,13 +550,11 @@ nxt_ruby_rack_parse_script(VALUE ctx) return res; } - static VALUE -nxt_ruby_rack_env_create(VALUE arg) -{ +nxt_ruby_rack_env_create(VALUE arg) { int rc; VALUE hash_env, version; - nxt_ruby_ctx_t *rctx; + nxt_ruby_ctx_t *rctx; rctx = (nxt_ruby_ctx_t *) (uintptr_t) arg; @@ -562,8 +566,7 @@ nxt_ruby_rack_env_create(VALUE arg) hash_env = rb_hash_new(); rb_hash_aset(hash_env, rb_str_new2("SERVER_SOFTWARE"), - rb_str_new((const char *) nxt_server.start, - (long) nxt_server.length)); + rb_str_new((const char *) nxt_server.start, (long) nxt_server.length)); version = rb_ary_new(); @@ -575,7 +578,7 @@ nxt_ruby_rack_env_create(VALUE arg) rb_hash_aset(hash_env, rb_str_new2("rack.input"), rctx->io_input); rb_hash_aset(hash_env, rb_str_new2("rack.errors"), rctx->io_error); rb_hash_aset(hash_env, rb_str_new2("rack.multithread"), - nxt_ruby_threads > 1 ? Qtrue : Qfalse); + nxt_ruby_threads > 1 ? Qtrue : Qfalse); rb_hash_aset(hash_env, rb_str_new2("rack.multiprocess"), Qtrue); rb_hash_aset(hash_env, rb_str_new2("rack.run_once"), Qfalse); rb_hash_aset(hash_env, rb_str_new2("rack.hijack?"), Qfalse); @@ -589,19 +592,17 @@ nxt_ruby_rack_env_create(VALUE arg) return hash_env; } - static int -nxt_ruby_init_io(nxt_ruby_ctx_t *rctx) -{ - VALUE io_input, io_error; +nxt_ruby_init_io(nxt_ruby_ctx_t *rctx) { + VALUE io_input, io_error; io_input = nxt_ruby_stream_io_input_init(); - rctx->io_input = rb_funcall(io_input, rb_intern("new"), 1, - (VALUE) (uintptr_t) rctx); + rctx->io_input + = rb_funcall(io_input, rb_intern("new"), 1, (VALUE) (uintptr_t) rctx); if (nxt_slow_path(rctx->io_input == Qnil)) { nxt_unit_alert(NULL, - "Ruby: Failed to create environment 'rack.input' var"); + "Ruby: Failed to create environment 'rack.input' var"); return NXT_UNIT_ERROR; } @@ -610,11 +611,11 @@ nxt_ruby_init_io(nxt_ruby_ctx_t *rctx) io_error = nxt_ruby_stream_io_error_init(); - rctx->io_error = rb_funcall(io_error, rb_intern("new"), 1, - (VALUE) (uintptr_t) rctx); + rctx->io_error + = rb_funcall(io_error, rb_intern("new"), 1, (VALUE) (uintptr_t) rctx); if (nxt_slow_path(rctx->io_error == Qnil)) { nxt_unit_alert(NULL, - "Ruby: Failed to create environment 'rack.error' var"); + "Ruby: Failed to create environment 'rack.error' var"); return NXT_UNIT_ERROR; } @@ -624,31 +625,26 @@ nxt_ruby_init_io(nxt_ruby_ctx_t *rctx) return NXT_UNIT_OK; } - static void -nxt_ruby_request_handler(nxt_unit_request_info_t *req) -{ +nxt_ruby_request_handler(nxt_unit_request_info_t *req) { (void) rb_thread_call_with_gvl(nxt_ruby_request_handler_gvl, req); } - static void * -nxt_ruby_request_handler_gvl(void *data) -{ +nxt_ruby_request_handler_gvl(void *data) { int state; VALUE res; - nxt_ruby_ctx_t *rctx; - nxt_unit_request_info_t *req; + nxt_ruby_ctx_t *rctx; + nxt_unit_request_info_t *req; req = data; - rctx = req->ctx->data; + rctx = req->ctx->data; rctx->req = req; res = rb_protect(nxt_ruby_rack_app_run, (VALUE) (uintptr_t) req, &state); if (nxt_slow_path(res == Qnil || state != 0)) { - nxt_ruby_exception_log(req, NXT_LOG_ERR, - "Failed to run ruby script"); + nxt_ruby_exception_log(req, NXT_LOG_ERR, "Failed to run ruby script"); nxt_unit_request_done(req, NXT_UNIT_ERROR); @@ -661,15 +657,13 @@ nxt_ruby_request_handler_gvl(void *data) return NULL; } - static VALUE -nxt_ruby_rack_app_run(VALUE arg) -{ +nxt_ruby_rack_app_run(VALUE arg) { int rc; VALUE env, result; nxt_int_t status; - nxt_ruby_ctx_t *rctx; - nxt_unit_request_info_t *req; + nxt_ruby_ctx_t *rctx; + nxt_unit_request_info_t *req; req = (nxt_unit_request_info_t *) arg; @@ -679,8 +673,7 @@ nxt_ruby_rack_app_run(VALUE arg) rc = nxt_ruby_read_request(req, env); if (nxt_slow_path(rc != NXT_UNIT_OK)) { - nxt_unit_req_alert(req, - "Ruby: Failed to process incoming request"); + nxt_unit_req_alert(req, "Ruby: Failed to process incoming request"); goto fail; } @@ -688,15 +681,15 @@ nxt_ruby_rack_app_run(VALUE arg) result = rb_funcall(nxt_ruby_rackup, nxt_ruby_call, 1, env); if (nxt_slow_path(TYPE(result) != T_ARRAY)) { nxt_unit_req_error(req, - "Ruby: Invalid response format from application"); + "Ruby: Invalid response format from application"); goto fail; } if (nxt_slow_path(RARRAY_LEN(result) != 3)) { nxt_unit_req_error(req, - "Ruby: Invalid response format from application. " - "Need 3 entries [Status, Headers, Body]"); + "Ruby: Invalid response format from application. " + "Need 3 entries [Status, Headers, Body]"); goto fail; } @@ -704,7 +697,7 @@ nxt_ruby_rack_app_run(VALUE arg) status = nxt_ruby_rack_result_status(req, result); if (nxt_slow_path(status < 0)) { nxt_unit_req_error(req, - "Ruby: Invalid response status from application."); + "Ruby: Invalid response status from application."); goto fail; } @@ -730,37 +723,35 @@ nxt_ruby_rack_app_run(VALUE arg) return Qnil; } - static int -nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env) -{ +nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env) { VALUE name; uint32_t i; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + nxt_unit_field_t *f; + nxt_unit_request_t *r; r = req->request; nxt_ruby_add_sptr(hash_env, nxt_rb_request_method_str, &r->method, - r->method_length); + r->method_length); nxt_ruby_add_sptr(hash_env, nxt_rb_request_uri_str, &r->target, - r->target_length); + r->target_length); nxt_ruby_add_sptr(hash_env, nxt_rb_path_info_str, &r->path, r->path_length); nxt_ruby_add_sptr(hash_env, nxt_rb_query_string_str, &r->query, - r->query_length); + r->query_length); nxt_ruby_add_sptr(hash_env, nxt_rb_server_protocol_str, &r->version, - r->version_length); + r->version_length); nxt_ruby_add_sptr(hash_env, nxt_rb_remote_addr_str, &r->remote, - r->remote_length); + r->remote_length); nxt_ruby_add_sptr(hash_env, nxt_rb_server_addr_str, &r->local_addr, - r->local_addr_length); + r->local_addr_length); nxt_ruby_add_sptr(hash_env, nxt_rb_server_name_str, &r->server_name, - r->server_name_length); + r->server_name_length); rb_hash_aset(hash_env, nxt_rb_server_port_str, nxt_rb_80_str); rb_hash_aset(hash_env, nxt_rb_rack_url_scheme_str, - r->tls ? nxt_rb_https_str : nxt_rb_http_str); + r->tls ? nxt_rb_https_str : nxt_rb_http_str); for (i = 0; i < r->fields_count; i++) { f = r->fields + i; @@ -773,37 +764,33 @@ nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env) if (r->content_length_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_length_field; - nxt_ruby_add_sptr(hash_env, nxt_rb_content_length_str, - &f->value, f->value_length); + nxt_ruby_add_sptr(hash_env, nxt_rb_content_length_str, &f->value, + f->value_length); } if (r->content_type_field != NXT_UNIT_NONE_FIELD) { f = r->fields + r->content_type_field; - nxt_ruby_add_sptr(hash_env, nxt_rb_content_type_str, - &f->value, f->value_length); + nxt_ruby_add_sptr(hash_env, nxt_rb_content_type_str, &f->value, + f->value_length); } return NXT_UNIT_OK; } - nxt_inline void -nxt_ruby_add_sptr(VALUE hash_env, VALUE name, - nxt_unit_sptr_t *sptr, uint32_t len) -{ - char *str; +nxt_ruby_add_sptr(VALUE hash_env, VALUE name, nxt_unit_sptr_t *sptr, + uint32_t len) { + char *str; str = nxt_unit_sptr_get(sptr); rb_hash_aset(hash_env, name, rb_str_new(str, len)); } - static nxt_int_t -nxt_ruby_rack_result_status(nxt_unit_request_info_t *req, VALUE result) -{ - VALUE status; +nxt_ruby_rack_result_status(nxt_unit_request_info_t *req, VALUE result) { + VALUE status; status = rb_ary_entry(result, 0); @@ -813,105 +800,99 @@ nxt_ruby_rack_result_status(nxt_unit_request_info_t *req, VALUE result) if (TYPE(status) == T_STRING) { return nxt_int_parse((u_char *) RSTRING_PTR(status), - RSTRING_LEN(status)); + RSTRING_LEN(status)); } nxt_unit_req_error(req, "Ruby: Invalid response 'status' " - "format from application"); + "format from application"); return -2; } - typedef struct { int rc; uint32_t fields; uint32_t size; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; } nxt_ruby_headers_info_t; - static int nxt_ruby_rack_result_headers(nxt_unit_request_info_t *req, VALUE result, - nxt_int_t status) -{ - int rc; - VALUE headers; - nxt_ruby_headers_info_t headers_info; + nxt_int_t status) { + int rc; + VALUE headers; + nxt_ruby_headers_info_t headers_info; headers = rb_ary_entry(result, 1); if (nxt_slow_path(TYPE(headers) != T_HASH)) { - nxt_unit_req_error(req, - "Ruby: Invalid response 'headers' format from " - "application"); + nxt_unit_req_error(req, "Ruby: Invalid response 'headers' format from " + "application"); return NXT_UNIT_ERROR; } rc = NXT_UNIT_OK; - headers_info.rc = NXT_UNIT_OK; + headers_info.rc = NXT_UNIT_OK; headers_info.fields = 0; - headers_info.size = 0; - headers_info.req = req; + headers_info.size = 0; + headers_info.req = req; rb_hash_foreach(headers, nxt_ruby_hash_info, - (VALUE) (uintptr_t) &headers_info); + (VALUE) (uintptr_t) &headers_info); if (nxt_slow_path(headers_info.rc != NXT_UNIT_OK)) { return headers_info.rc; } - rc = nxt_unit_response_init(req, status, - headers_info.fields, headers_info.size); + rc = nxt_unit_response_init(req, status, headers_info.fields, + headers_info.size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } rb_hash_foreach(headers, nxt_ruby_hash_add, - (VALUE) (uintptr_t) &headers_info); + (VALUE) (uintptr_t) &headers_info); return rc; } - static int -nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg) -{ - const char *value, *value_end, *pos; - nxt_ruby_headers_info_t *headers_info; +nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg) { + const char *value, *value_end, *pos; + nxt_ruby_headers_info_t *headers_info; headers_info = (void *) (uintptr_t) arg; if (nxt_slow_path(TYPE(r_key) != T_STRING)) { nxt_unit_req_error(headers_info->req, - "Ruby: Wrong header entry 'key' from application"); + "Ruby: Wrong header entry 'key' from application"); goto fail; } if (nxt_slow_path(TYPE(r_value) != T_STRING && TYPE(r_value) != T_ARRAY)) { nxt_unit_req_error(headers_info->req, - "Ruby: Wrong header entry 'value' from application"); + "Ruby: Wrong header entry 'value' from application"); goto fail; } if (TYPE(r_value) == T_ARRAY) { - int i; - int arr_len = RARRAY_LEN(r_value); - VALUE item; - size_t len = 0; + int i; + int arr_len = RARRAY_LEN(r_value); + VALUE item; + size_t len = 0; for (i = 0; i < arr_len; i++) { item = rb_ary_entry(r_value, i); if (TYPE(item) != T_STRING) { nxt_unit_req_error(headers_info->req, - "Ruby: Wrong header entry in 'value' array " - "from application"); + "Ruby: Wrong header entry in 'value' array " + "from application"); goto fail; } - len += RSTRING_LEN(item) + 2; /* +2 for '; ' */ + len += RSTRING_LEN(item) + 2; /* +2 for '; ' */ } if (arr_len > 0) { @@ -924,12 +905,12 @@ nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg) return ST_CONTINUE; } - value = RSTRING_PTR(r_value); + value = RSTRING_PTR(r_value); value_end = value + RSTRING_LEN(r_value); pos = value; - for ( ;; ) { + for (;;) { pos = strchr(pos, '\n'); if (pos == NULL) { @@ -957,31 +938,29 @@ nxt_ruby_hash_info(VALUE r_key, VALUE r_value, VALUE arg) return ST_STOP; } - static int -nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) -{ - int *rc; +nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) { + int *rc; uint32_t key_len; - const char *value, *value_end, *pos; - nxt_ruby_headers_info_t *headers_info; + const char *value, *value_end, *pos; + nxt_ruby_headers_info_t *headers_info; headers_info = (void *) (uintptr_t) arg; - rc = &headers_info->rc; + rc = &headers_info->rc; key_len = RSTRING_LEN(r_key); if (TYPE(r_value) == T_ARRAY) { - int i; - int arr_len = RARRAY_LEN(r_value); - char *field, *p; - VALUE item; - size_t len = 0; + int i; + int arr_len = RARRAY_LEN(r_value); + char *field, *p; + VALUE item; + size_t len = 0; for (i = 0; i < arr_len; i++) { item = rb_ary_entry(r_value, i); - len += RSTRING_LEN(item) + 2; /* +2 for '; ' */ + len += RSTRING_LEN(item) + 2; /* +2 for '; ' */ } field = nxt_unit_malloc(NULL, len); @@ -1002,9 +981,8 @@ nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) len -= 2; } - *rc = nxt_unit_response_add_field(headers_info->req, - RSTRING_PTR(r_key), key_len, - field, len); + *rc = nxt_unit_response_add_field(headers_info->req, RSTRING_PTR(r_key), + key_len, field, len); nxt_unit_free(NULL, field); if (nxt_slow_path(*rc != NXT_UNIT_OK)) { @@ -1014,21 +992,20 @@ nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) return ST_CONTINUE; } - value = RSTRING_PTR(r_value); + value = RSTRING_PTR(r_value); value_end = value + RSTRING_LEN(r_value); pos = value; - for ( ;; ) { + for (;;) { pos = strchr(pos, '\n'); if (pos == NULL) { break; } - *rc = nxt_unit_response_add_field(headers_info->req, - RSTRING_PTR(r_key), key_len, - value, pos - value); + *rc = nxt_unit_response_add_field(headers_info->req, RSTRING_PTR(r_key), + key_len, value, pos - value); if (nxt_slow_path(*rc != NXT_UNIT_OK)) { goto fail; } @@ -1038,9 +1015,8 @@ nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) } if (value <= value_end) { - *rc = nxt_unit_response_add_field(headers_info->req, - RSTRING_PTR(r_key), key_len, - value, value_end - value); + *rc = nxt_unit_response_add_field(headers_info->req, RSTRING_PTR(r_key), + key_len, value, value_end - value); if (nxt_slow_path(*rc != NXT_UNIT_OK)) { goto fail; } @@ -1055,22 +1031,18 @@ nxt_ruby_hash_add(VALUE r_key, VALUE r_value, VALUE arg) return ST_STOP; } - static int -nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, VALUE result) -{ - int rc; - VALUE fn, body; +nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, VALUE result) { + int rc; + VALUE fn, body; body = rb_ary_entry(result, 2); if (rb_respond_to(body, rb_intern("to_path"))) { - fn = rb_funcall(body, rb_intern("to_path"), 0); if (nxt_slow_path(TYPE(fn) != T_STRING)) { - nxt_unit_req_error(req, - "Ruby: Failed to get 'body' file path from " - "application"); + nxt_unit_req_error(req, "Ruby: Failed to get 'body' file path from " + "application"); return NXT_UNIT_ERROR; } @@ -1082,12 +1054,11 @@ nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, VALUE result) } else if (rb_respond_to(body, rb_intern("each"))) { rb_block_call(body, rb_intern("each"), 0, 0, - nxt_ruby_rack_result_body_each, (VALUE) (uintptr_t) req); + nxt_ruby_rack_result_body_each, (VALUE) (uintptr_t) req); } else { - nxt_unit_req_error(req, - "Ruby: Invalid response 'body' format " - "from application"); + nxt_unit_req_error(req, "Ruby: Invalid response 'body' format " + "from application"); return NXT_UNIT_ERROR; } @@ -1099,19 +1070,17 @@ nxt_ruby_rack_result_body(nxt_unit_request_info_t *req, VALUE result) return NXT_UNIT_OK; } - typedef struct { - int fd; - off_t pos; - off_t rest; + int fd; + off_t pos; + off_t rest; } nxt_ruby_rack_file_t; - static ssize_t -nxt_ruby_rack_file_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) -{ +nxt_ruby_rack_file_read(nxt_unit_read_info_t *read_info, void *dst, + size_t size) { ssize_t res; - nxt_ruby_rack_file_t *file; + nxt_ruby_rack_file_t *file; file = read_info->data; @@ -1120,7 +1089,7 @@ nxt_ruby_rack_file_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) res = pread(file->fd, dst, size, file->pos); if (res >= 0) { - file->pos += res; + file->pos += res; file->rest -= res; if (size > (size_t) res) { @@ -1133,27 +1102,24 @@ nxt_ruby_rack_file_read(nxt_unit_read_info_t *read_info, void *dst, size_t size) return res; } - typedef struct { nxt_unit_read_info_t read_info; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; } nxt_ruby_read_info_t; - static int nxt_ruby_rack_result_body_file_write(nxt_unit_request_info_t *req, - VALUE filepath) -{ - int fd, rc; - struct stat finfo; - nxt_ruby_rack_file_t ruby_file; - nxt_ruby_read_info_t ri; + VALUE filepath) { + int fd, rc; + struct stat finfo; + nxt_ruby_rack_file_t ruby_file; + nxt_ruby_read_info_t ri; fd = open(RSTRING_PTR(filepath), O_RDONLY, 0); if (nxt_slow_path(fd == -1)) { nxt_unit_req_error(req, - "Ruby: Failed to open content file \"%s\": %s (%d)", - RSTRING_PTR(filepath), strerror(errno), errno); + "Ruby: Failed to open content file \"%s\": %s (%d)", + RSTRING_PTR(filepath), strerror(errno), errno); return NXT_UNIT_ERROR; } @@ -1161,40 +1127,36 @@ nxt_ruby_rack_result_body_file_write(nxt_unit_request_info_t *req, rc = fstat(fd, &finfo); if (nxt_slow_path(rc == -1)) { nxt_unit_req_error(req, - "Ruby: Content file fstat(\"%s\") failed: %s (%d)", - RSTRING_PTR(filepath), strerror(errno), errno); + "Ruby: Content file fstat(\"%s\") failed: %s (%d)", + RSTRING_PTR(filepath), strerror(errno), errno); close(fd); return NXT_UNIT_ERROR; } - ruby_file.fd = fd; - ruby_file.pos = 0; + ruby_file.fd = fd; + ruby_file.pos = 0; ruby_file.rest = finfo.st_size; - ri.read_info.read = nxt_ruby_rack_file_read; - ri.read_info.eof = ruby_file.rest == 0; + ri.read_info.read = nxt_ruby_rack_file_read; + ri.read_info.eof = ruby_file.rest == 0; ri.read_info.buf_size = ruby_file.rest; - ri.read_info.data = &ruby_file; - ri.req = req; + ri.read_info.data = &ruby_file; + ri.req = req; - rc = (intptr_t) rb_thread_call_without_gvl(nxt_ruby_response_write_cb, - &ri, - nxt_ruby_ubf, - req->ctx); + rc = (intptr_t) rb_thread_call_without_gvl(nxt_ruby_response_write_cb, &ri, + nxt_ruby_ubf, req->ctx); close(fd); return rc; } - static void * -nxt_ruby_response_write_cb(void *data) -{ +nxt_ruby_response_write_cb(void *data) { int rc; - nxt_ruby_read_info_t *ri; + nxt_ruby_read_info_t *ri; ri = data; @@ -1206,59 +1168,51 @@ nxt_ruby_response_write_cb(void *data) return (void *) (intptr_t) rc; } - typedef struct { VALUE body; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; } nxt_ruby_write_info_t; - static VALUE nxt_ruby_rack_result_body_each(VALUE body, VALUE arg, int argc, - const VALUE *argv, VALUE blockarg) -{ - nxt_ruby_write_info_t wi; + const VALUE *argv, VALUE blockarg) { + nxt_ruby_write_info_t wi; if (TYPE(body) != T_STRING) { return Qnil; } wi.body = body; - wi.req = (void *) (uintptr_t) arg; + wi.req = (void *) (uintptr_t) arg; (void) rb_thread_call_without_gvl(nxt_ruby_response_write, - (void *) (uintptr_t) &wi, - nxt_ruby_ubf, wi.req->ctx); + (void *) (uintptr_t) &wi, nxt_ruby_ubf, wi.req->ctx); return Qnil; } - static void * -nxt_ruby_response_write(void *data) -{ +nxt_ruby_response_write(void *data) { int rc; - nxt_ruby_write_info_t *wi; + nxt_ruby_write_info_t *wi; wi = data; rc = nxt_unit_response_write(wi->req, RSTRING_PTR(wi->body), - RSTRING_LEN(wi->body)); + RSTRING_LEN(wi->body)); if (nxt_slow_path(rc != NXT_UNIT_OK)) { nxt_unit_req_error(wi->req, - "Ruby: Failed to write 'body' from application"); + "Ruby: Failed to write 'body' from application"); } return (void *) (intptr_t) rc; } - static void nxt_ruby_exception_log(nxt_unit_request_info_t *req, uint32_t level, - const char *desc) -{ - int i; - VALUE err, ary, eclass, msg; + const char *desc) { + int i; + VALUE err, ary, eclass, msg; nxt_unit_req_log(req, level, "Ruby: %s", desc); @@ -1274,25 +1228,22 @@ nxt_ruby_exception_log(nxt_unit_request_info_t *req, uint32_t level, if (RARRAY_LEN(ary) == 0) { nxt_unit_req_log(req, level, "Ruby: %s (%s)", RSTRING_PTR(msg), - RSTRING_PTR(eclass)); + RSTRING_PTR(eclass)); return; } nxt_unit_req_log(req, level, "Ruby: %s: %s (%s)", - RSTRING_PTR(RARRAY_PTR(ary)[0]), - RSTRING_PTR(msg), RSTRING_PTR(eclass)); + RSTRING_PTR(RARRAY_PTR(ary)[0]), RSTRING_PTR(msg), RSTRING_PTR(eclass)); for (i = 1; i < RARRAY_LEN(ary); i++) { nxt_unit_req_log(req, level, "from %s", - RSTRING_PTR(RARRAY_PTR(ary)[i])); + RSTRING_PTR(RARRAY_PTR(ary)[i])); } } - static void -nxt_ruby_ctx_done(nxt_ruby_ctx_t *rctx) -{ +nxt_ruby_ctx_done(nxt_ruby_ctx_t *rctx) { if (rctx->io_input != Qnil) { rb_gc_unregister_address(&rctx->io_input); } @@ -1306,10 +1257,8 @@ nxt_ruby_ctx_done(nxt_ruby_ctx_t *rctx) } } - static void -nxt_ruby_atexit(void) -{ +nxt_ruby_atexit(void) { if (nxt_ruby_rackup != Qnil) { rb_gc_unregister_address(&nxt_ruby_rackup); } @@ -1327,14 +1276,12 @@ nxt_ruby_atexit(void) ruby_cleanup(0); } - static int -nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx) -{ +nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx) { VALUE res; uint32_t i; - nxt_ruby_ctx_t *rctx; - nxt_ruby_app_conf_t *c; + nxt_ruby_ctx_t *rctx; + nxt_ruby_app_conf_t *c; c = ctx->unit->data; @@ -1364,24 +1311,20 @@ nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static void * -nxt_ruby_thread_create_gvl(void *rctx) -{ - VALUE res; +nxt_ruby_thread_create_gvl(void *rctx) { + VALUE res; res = rb_thread_create(RUBY_METHOD_FUNC(nxt_ruby_thread_func), rctx); return (void *) (uintptr_t) res; } - static VALUE -nxt_ruby_thread_func(VALUE arg) -{ +nxt_ruby_thread_func(VALUE arg) { int state; - nxt_unit_ctx_t *ctx; - nxt_ruby_ctx_t *rctx; + nxt_unit_ctx_t *ctx; + nxt_ruby_ctx_t *rctx; rctx = (nxt_ruby_ctx_t *) (uintptr_t) arg; @@ -1396,18 +1339,18 @@ nxt_ruby_thread_func(VALUE arg) rb_protect(nxt_ruby_hook_call, nxt_rb_on_thread_boot, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_thread_boot()"); + "Failed to call on_thread_boot()"); } } - (void) rb_thread_call_without_gvl(nxt_ruby_unit_run, ctx, - nxt_ruby_ubf, ctx); + (void) rb_thread_call_without_gvl(nxt_ruby_unit_run, ctx, nxt_ruby_ubf, + ctx); if (nxt_ruby_hook_procs != Qnil) { rb_protect(nxt_ruby_hook_call, nxt_rb_on_thread_shutdown, &state); if (nxt_slow_path(state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ERR, - "Failed to call on_thread_shutdown()"); + "Failed to call on_thread_shutdown()"); } } @@ -1420,34 +1363,28 @@ nxt_ruby_thread_func(VALUE arg) return Qnil; } - static void * -nxt_ruby_unit_run(void *ctx) -{ +nxt_ruby_unit_run(void *ctx) { return (void *) (intptr_t) nxt_unit_run(ctx); } - static void -nxt_ruby_ubf(void *ctx) -{ +nxt_ruby_ubf(void *ctx) { nxt_unit_warn(ctx, "Ruby: UBF"); } - static int -nxt_ruby_init_threads(nxt_ruby_app_conf_t *c) -{ +nxt_ruby_init_threads(nxt_ruby_app_conf_t *c) { int state; uint32_t i; - nxt_ruby_ctx_t *rctx; + nxt_ruby_ctx_t *rctx; if (c->threads <= 1) { return NXT_UNIT_OK; } - nxt_ruby_ctxs = nxt_unit_malloc(NULL, sizeof(nxt_ruby_ctx_t) - * (c->threads - 1)); + nxt_ruby_ctxs + = nxt_unit_malloc(NULL, sizeof(nxt_ruby_ctx_t) * (c->threads - 1)); if (nxt_slow_path(nxt_ruby_ctxs == NULL)) { nxt_unit_alert(NULL, "Failed to allocate run contexts array"); @@ -1457,20 +1394,20 @@ nxt_ruby_init_threads(nxt_ruby_app_conf_t *c) for (i = 0; i < c->threads - 1; i++) { rctx = &nxt_ruby_ctxs[i]; - rctx->env = Qnil; + rctx->env = Qnil; rctx->io_input = Qnil; rctx->io_error = Qnil; - rctx->thread = Qnil; + rctx->thread = Qnil; } for (i = 0; i < c->threads - 1; i++) { rctx = &nxt_ruby_ctxs[i]; rctx->env = rb_protect(nxt_ruby_rack_env_create, - (VALUE) (uintptr_t) rctx, &state); + (VALUE) (uintptr_t) rctx, &state); if (nxt_slow_path(rctx->env == Qnil || state != 0)) { nxt_ruby_exception_log(NULL, NXT_LOG_ALERT, - "Failed to create 'environ' variable"); + "Failed to create 'environ' variable"); return NXT_UNIT_ERROR; } } @@ -1478,12 +1415,10 @@ nxt_ruby_init_threads(nxt_ruby_app_conf_t *c) return NXT_UNIT_OK; } - static void -nxt_ruby_join_threads(nxt_unit_ctx_t *ctx, nxt_ruby_app_conf_t *c) -{ +nxt_ruby_join_threads(nxt_unit_ctx_t *ctx, nxt_ruby_app_conf_t *c) { uint32_t i; - nxt_ruby_ctx_t *rctx; + nxt_ruby_ctx_t *rctx; if (nxt_ruby_ctxs == NULL) { return; diff --git a/src/ruby/nxt_ruby.h b/src/ruby/nxt_ruby.h index 264300213..9a11dfc63 100644 --- a/src/ruby/nxt_ruby.h +++ b/src/ruby/nxt_ruby.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Alexander Borisov * Copyright (C) NGINX, Inc. @@ -19,18 +18,18 @@ #include #include - typedef struct { VALUE env; VALUE io_input; VALUE io_error; VALUE thread; - nxt_unit_ctx_t *ctx; - nxt_unit_request_info_t *req; + nxt_unit_ctx_t *ctx; + nxt_unit_request_info_t *req; } nxt_ruby_ctx_t; - -VALUE nxt_ruby_stream_io_input_init(void); -VALUE nxt_ruby_stream_io_error_init(void); +VALUE +nxt_ruby_stream_io_input_init(void); +VALUE +nxt_ruby_stream_io_error_init(void); #endif /* _NXT_RUBY_H_INCLUDED_ */ diff --git a/src/ruby/nxt_ruby_stream_io.c b/src/ruby/nxt_ruby_stream_io.c index 4ef69cee1..dedfdc5b6 100644 --- a/src/ruby/nxt_ruby_stream_io.c +++ b/src/ruby/nxt_ruby_stream_io.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Alexander Borisov * Copyright (C) NGINX, Inc. @@ -8,23 +7,32 @@ #include -static VALUE nxt_ruby_stream_io_new(VALUE class, VALUE arg); -static VALUE nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self); -static VALUE nxt_ruby_stream_io_gets(VALUE obj); -static VALUE nxt_ruby_stream_io_each(VALUE obj); -static VALUE nxt_ruby_stream_io_read(VALUE obj, VALUE args); -static VALUE nxt_ruby_stream_io_rewind(VALUE obj); -static VALUE nxt_ruby_stream_io_puts(VALUE obj, VALUE args); -static VALUE nxt_ruby_stream_io_write(VALUE obj, VALUE args); -nxt_inline long nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val); -static VALUE nxt_ruby_stream_io_flush(VALUE obj); -static VALUE nxt_ruby_stream_io_close(VALUE obj); - +static VALUE +nxt_ruby_stream_io_new(VALUE class, VALUE arg); +static VALUE +nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self); +static VALUE +nxt_ruby_stream_io_gets(VALUE obj); +static VALUE +nxt_ruby_stream_io_each(VALUE obj); +static VALUE +nxt_ruby_stream_io_read(VALUE obj, VALUE args); +static VALUE +nxt_ruby_stream_io_rewind(VALUE obj); +static VALUE +nxt_ruby_stream_io_puts(VALUE obj, VALUE args); +static VALUE +nxt_ruby_stream_io_write(VALUE obj, VALUE args); +nxt_inline long +nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val); +static VALUE +nxt_ruby_stream_io_flush(VALUE obj); +static VALUE +nxt_ruby_stream_io_close(VALUE obj); VALUE -nxt_ruby_stream_io_input_init(void) -{ - VALUE stream_io; +nxt_ruby_stream_io_input_init(void) { + VALUE stream_io; stream_io = rb_define_class("NGINX_Unit_Stream_IO_Read", rb_cObject); @@ -33,8 +41,8 @@ nxt_ruby_stream_io_input_init(void) rb_gc_register_address(&stream_io); rb_define_singleton_method(stream_io, "new", nxt_ruby_stream_io_new, 1); - rb_define_method(stream_io, "initialize", - nxt_ruby_stream_io_initialize, -1); + rb_define_method(stream_io, "initialize", nxt_ruby_stream_io_initialize, + -1); rb_define_method(stream_io, "gets", nxt_ruby_stream_io_gets, 0); rb_define_method(stream_io, "each", nxt_ruby_stream_io_each, 0); rb_define_method(stream_io, "read", nxt_ruby_stream_io_read, -2); @@ -44,11 +52,9 @@ nxt_ruby_stream_io_input_init(void) return stream_io; } - VALUE -nxt_ruby_stream_io_error_init(void) -{ - VALUE stream_io; +nxt_ruby_stream_io_error_init(void) { + VALUE stream_io; stream_io = rb_define_class("NGINX_Unit_Stream_IO_Error", rb_cObject); @@ -57,8 +63,8 @@ nxt_ruby_stream_io_error_init(void) rb_gc_register_address(&stream_io); rb_define_singleton_method(stream_io, "new", nxt_ruby_stream_io_new, 1); - rb_define_method(stream_io, "initialize", - nxt_ruby_stream_io_initialize, -1); + rb_define_method(stream_io, "initialize", nxt_ruby_stream_io_initialize, + -1); rb_define_method(stream_io, "puts", nxt_ruby_stream_io_puts, -2); rb_define_method(stream_io, "write", nxt_ruby_stream_io_write, -2); rb_define_method(stream_io, "flush", nxt_ruby_stream_io_flush, 0); @@ -67,11 +73,9 @@ nxt_ruby_stream_io_error_init(void) return stream_io; } - static VALUE -nxt_ruby_stream_io_new(VALUE class, VALUE arg) -{ - VALUE self; +nxt_ruby_stream_io_new(VALUE class, VALUE arg) { + VALUE self; self = Data_Wrap_Struct(class, 0, 0, (void *) (uintptr_t) arg); @@ -80,21 +84,17 @@ nxt_ruby_stream_io_new(VALUE class, VALUE arg) return self; } - static VALUE -nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self) -{ +nxt_ruby_stream_io_initialize(int argc, VALUE *argv, VALUE self) { return self; } - static VALUE -nxt_ruby_stream_io_gets(VALUE obj) -{ +nxt_ruby_stream_io_gets(VALUE obj) { VALUE buf; ssize_t res; - nxt_ruby_ctx_t *rctx; - nxt_unit_request_info_t *req; + nxt_ruby_ctx_t *rctx; + nxt_unit_request_info_t *req; Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx); req = rctx->req; @@ -121,17 +121,15 @@ nxt_ruby_stream_io_gets(VALUE obj) return buf; } - static VALUE -nxt_ruby_stream_io_each(VALUE obj) -{ - VALUE chunk; +nxt_ruby_stream_io_each(VALUE obj) { + VALUE chunk; if (rb_block_given_p() == 0) { rb_raise(rb_eArgError, "Expected block on rack.input 'each' method"); } - for ( ;; ) { + for (;;) { chunk = nxt_ruby_stream_io_gets(obj); if (chunk == Qnil) { @@ -144,13 +142,11 @@ nxt_ruby_stream_io_each(VALUE obj) return Qnil; } - static VALUE -nxt_ruby_stream_io_read(VALUE obj, VALUE args) -{ +nxt_ruby_stream_io_read(VALUE obj, VALUE args) { VALUE buf; long copy_size, u_size; - nxt_ruby_ctx_t *rctx; + nxt_ruby_ctx_t *rctx; Data_Get_Struct(obj, nxt_ruby_ctx_t, rctx); @@ -181,7 +177,6 @@ nxt_ruby_stream_io_read(VALUE obj, VALUE args) copy_size = nxt_unit_request_read(rctx->req, RSTRING_PTR(buf), copy_size); if (RARRAY_LEN(args) > 1 && TYPE(RARRAY_PTR(args)[1]) == T_STRING) { - rb_str_set_len(RARRAY_PTR(args)[1], 0); rb_str_cat(RARRAY_PTR(args)[1], RSTRING_PTR(buf), copy_size); } @@ -191,18 +186,14 @@ nxt_ruby_stream_io_read(VALUE obj, VALUE args) return buf; } - static VALUE -nxt_ruby_stream_io_rewind(VALUE obj) -{ +nxt_ruby_stream_io_rewind(VALUE obj) { return Qnil; } - static VALUE -nxt_ruby_stream_io_puts(VALUE obj, VALUE args) -{ - nxt_ruby_ctx_t *rctx; +nxt_ruby_stream_io_puts(VALUE obj, VALUE args) { + nxt_ruby_ctx_t *rctx; if (RARRAY_LEN(args) != 1) { return Qnil; @@ -215,12 +206,10 @@ nxt_ruby_stream_io_puts(VALUE obj, VALUE args) return Qnil; } - static VALUE -nxt_ruby_stream_io_write(VALUE obj, VALUE args) -{ +nxt_ruby_stream_io_write(VALUE obj, VALUE args) { long len; - nxt_ruby_ctx_t *rctx; + nxt_ruby_ctx_t *rctx; if (RARRAY_LEN(args) != 1) { return Qnil; @@ -233,10 +222,8 @@ nxt_ruby_stream_io_write(VALUE obj, VALUE args) return LONG2FIX(len); } - nxt_inline long -nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val) -{ +nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val) { if (nxt_slow_path(val == Qnil)) { return 0; } @@ -254,16 +241,12 @@ nxt_ruby_stream_io_s_write(nxt_ruby_ctx_t *rctx, VALUE val) return RSTRING_LEN(val); } - static VALUE -nxt_ruby_stream_io_flush(VALUE obj) -{ +nxt_ruby_stream_io_flush(VALUE obj) { return Qnil; } - static VALUE -nxt_ruby_stream_io_close(VALUE obj) -{ +nxt_ruby_stream_io_close(VALUE obj) { return Qnil; } diff --git a/src/test/nxt_base64_test.c b/src/test/nxt_base64_test.c index 13a772b69..8714603c1 100644 --- a/src/test/nxt_base64_test.c +++ b/src/test/nxt_base64_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -6,66 +5,49 @@ #include #include "nxt_tests.h" - nxt_int_t -nxt_base64_test(nxt_thread_t *thr) -{ - ssize_t ret; - nxt_uint_t i; +nxt_base64_test(nxt_thread_t *thr) { + ssize_t ret; + nxt_uint_t i; static struct { - nxt_str_t enc; - nxt_str_t dec; + nxt_str_t enc; + nxt_str_t dec; } tests[] = { - { nxt_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+//+9876543210" - "zyxwvutsrqponmlkjihgfedcba" - "ZYXWVUTSRQPONMLKJIHGFEDCBA"), - nxt_string("\x00\x10\x83\x10\x51\x87\x20\x92\x8b\x30\xd3\x8f" - "\x41\x14\x93\x51\x55\x97\x61\x96\x9b\x71\xd7\x9f" - "\x82\x18\xa3\x92\x59\xa7\xa2\x9a\xab\xb2\xdb\xaf" - "\xc3\x1c\xb3\xd3\x5d\xb7\xe3\x9e\xbb\xf3\xdf\xbf" - "\xff\xef\x7c\xef\xae\x78\xdf\x6d\x74\xcf\x2c\x70" - "\xbe\xeb\x6c\xae\xaa\x68\x9e\x69\x64\x8e\x28\x60" - "\x7d\xe7\x5c\x6d\xa6\x58\x5d\x65\x54\x4d\x24\x50" - "\x3c\xe3\x4c\x2c\xa2\x48\x1c\x61\x44\x0c\x20\x40") }, - - { nxt_string("Aa=="), - nxt_string("\x01") }, - { nxt_string("0Z"), - nxt_string("\xd1") }, - { nxt_string("0aA="), - nxt_string("\xd1\xa0") }, - { nxt_string("z/+"), - nxt_string("\xcf\xff") }, - { nxt_string("z9+Npe=="), - nxt_string("\xcf\xdf\x8d\xa5") }, - { nxt_string("/+98765"), - nxt_string("\xff\xef\x7c\xef\xae") }, - - { nxt_string("aBc_"), - nxt_null_string }, - { nxt_string("5"), - nxt_null_string }, - { nxt_string("M==="), - nxt_null_string }, - { nxt_string("===="), - nxt_null_string }, - { nxt_string("Ab="), - nxt_null_string }, - { nxt_string("00=0"), - nxt_null_string }, - { nxt_string("\0"), - nxt_null_string }, - { nxt_string("\r\naaaa"), - nxt_null_string }, - { nxt_string("=0000"), - nxt_null_string }, + {nxt_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+//+9876543210" + "zyxwvutsrqponmlkjihgfedcba" + "ZYXWVUTSRQPONMLKJIHGFEDCBA"), + nxt_string("\x00\x10\x83\x10\x51\x87\x20\x92\x8b\x30\xd3\x8f" + "\x41\x14\x93\x51\x55\x97\x61\x96\x9b\x71\xd7\x9f" + "\x82\x18\xa3\x92\x59\xa7\xa2\x9a\xab\xb2\xdb\xaf" + "\xc3\x1c\xb3\xd3\x5d\xb7\xe3\x9e\xbb\xf3\xdf\xbf" + "\xff\xef\x7c\xef\xae\x78\xdf\x6d\x74\xcf\x2c\x70" + "\xbe\xeb\x6c\xae\xaa\x68\x9e\x69\x64\x8e\x28\x60" + "\x7d\xe7\x5c\x6d\xa6\x58\x5d\x65\x54\x4d\x24\x50" + "\x3c\xe3\x4c\x2c\xa2\x48\x1c\x61\x44\x0c\x20\x40")}, + + {nxt_string("Aa=="), nxt_string("\x01")}, + {nxt_string("0Z"), nxt_string("\xd1")}, + {nxt_string("0aA="), nxt_string("\xd1\xa0")}, + {nxt_string("z/+"), nxt_string("\xcf\xff")}, + {nxt_string("z9+Npe=="), nxt_string("\xcf\xdf\x8d\xa5")}, + {nxt_string("/+98765"), nxt_string("\xff\xef\x7c\xef\xae")}, + + {nxt_string("aBc_"), nxt_null_string}, + {nxt_string("5"), nxt_null_string}, + {nxt_string("M==="), nxt_null_string}, + {nxt_string("===="), nxt_null_string}, + {nxt_string("Ab="), nxt_null_string}, + {nxt_string("00=0"), nxt_null_string}, + {nxt_string("\0"), nxt_null_string}, + {nxt_string("\r\naaaa"), nxt_null_string}, + {nxt_string("=0000"), nxt_null_string}, }; - u_char buf[96]; + u_char buf[96]; nxt_thread_time_update(thr); @@ -78,9 +60,9 @@ nxt_base64_test(nxt_thread_t *thr) if ((size_t) ret != tests[i].dec.length) { nxt_log_alert(thr->log, - "nxt_base64_decode() test \"%V\" failed: incorrect " - "length of decoded string %z, expected %uz", - &tests[i].enc, ret, tests[i].dec.length); + "nxt_base64_decode() test \"%V\" failed: incorrect " + "length of decoded string %z, expected %uz", + &tests[i].enc, ret, tests[i].dec.length); return NXT_ERROR; } diff --git a/src/test/nxt_clone_test.c b/src/test/nxt_clone_test.c index 1a864f0e0..550d7092e 100644 --- a/src/test/nxt_clone_test.c +++ b/src/test/nxt_clone_test.c @@ -11,31 +11,33 @@ #define UIDMAP 1 #define GIDMAP 2 - typedef struct { - nxt_int_t map_type; - nxt_str_t map_data; - nxt_int_t setid; - nxt_credential_t creds; - nxt_uid_t unit_euid; - nxt_gid_t unit_egid; - nxt_int_t result; - nxt_str_t errmsg; + nxt_int_t map_type; + nxt_str_t map_data; + nxt_int_t setid; + nxt_credential_t creds; + nxt_uid_t unit_euid; + nxt_gid_t unit_egid; + nxt_int_t result; + nxt_str_t errmsg; } nxt_clone_creds_testcase_t; typedef struct { - nxt_clone_creds_testcase_t *tc; + nxt_clone_creds_testcase_t *tc; } nxt_clone_creds_ctx_t; - -nxt_int_t nxt_clone_test_mappings(nxt_task_t *task, nxt_mp_t *mp, +nxt_int_t +nxt_clone_test_mappings(nxt_task_t *task, nxt_mp_t *mp, nxt_clone_creds_ctx_t *ctx, nxt_clone_creds_testcase_t *tc); -void nxt_cdecl nxt_clone_test_log_handler(nxt_uint_t level, nxt_log_t *log, - const char *fmt, ...); -nxt_int_t nxt_clone_test_map_assert(nxt_task_t *task, - nxt_clone_creds_testcase_t *tc, nxt_clone_credential_map_t *map); -static nxt_int_t nxt_clone_test_parse_map(nxt_task_t *task, - nxt_str_t *map_str, nxt_clone_credential_map_t *map); +void nxt_cdecl +nxt_clone_test_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, + ...); +nxt_int_t +nxt_clone_test_map_assert(nxt_task_t *task, nxt_clone_creds_testcase_t *tc, + nxt_clone_credential_map_t *map); +static nxt_int_t +nxt_clone_test_parse_map(nxt_task_t *task, nxt_str_t *map_str, + nxt_clone_credential_map_t *map); nxt_log_t *test_log; @@ -43,285 +45,139 @@ nxt_log_t *test_log; static nxt_gid_t gids[] = {1000, 10000, 60000}; static nxt_clone_creds_testcase_t testcases[] = { - { - /* - * Unprivileged unit - * - * if no uid mapping and app creds and unit creds are the same, - * then we automatically add a map for the creds->uid. - * Then, child process can safely setuid(creds->uid) in - * the new namespace. - */ - UIDMAP, - nxt_string(""), - 0, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, - nxt_string(""), - 0, - {"johndoe", 10000, 10000, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, - nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 1}]"), - 0, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, - nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), - 0, - {"root", 0, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, - nxt_string("[{\"container\": 65534, \"host\": 1000, \"size\": 1}]"), - 0, - {"nobody", 65534, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {/* + * Unprivileged unit + * + * if no uid mapping and app creds and unit creds are the same, + * then we automatically add a map for the creds->uid. + * Then, child process can safely setuid(creds->uid) in + * the new namespace. + */ + UIDMAP, nxt_string(""), 0, {"nobody", 65534, 65534, 0, NULL}, 1000, + 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string(""), 0, {"johndoe", 10000, 10000, 0, NULL}, 1000, 1000, + NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 1}]"), + 0, {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, NXT_OK, + nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), 0, + {"root", 0, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, + nxt_string("[{\"container\": 65534, \"host\": 1000, \"size\": 1}]"), 0, + {"nobody", 65534, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}," " {\"container\": 1000, \"host\": 2000, \"size\": 1}]"), - 0, - {"root", 0, 0, 0, NULL}, - 1000, 1000, - NXT_ERROR, + 0, {"root", 0, 0, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"uidmap\" field has 2 entries but unprivileged unit has " - "a maximum of 1 map.") - }, - { - UIDMAP, + "a maximum of 1 map.")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}," " {\"container\": 1000, \"host\": 2000, \"size\": 1}]"), 1, /* privileged */ - {"root", 0, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {"root", 0, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1000}," " {\"container\": 1000, \"host\": 2000, \"size\": 1000}]"), 1, /* privileged */ - {"johndoe", 500, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {"johndoe", 500, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1000}," " {\"container\": 1000, \"host\": 2000, \"size\": 1000}]"), 1, /* privileged */ - {"johndoe", 1000, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {"johndoe", 1000, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1000}," " {\"container\": 1000, \"host\": 2000, \"size\": 1000}]"), 1, /* privileged */ - {"johndoe", 1500, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {"johndoe", 1500, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1000}," " {\"container\": 1000, \"host\": 2000, \"size\": 1000}]"), 1, /* privileged */ - {"johndoe", 1999, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - UIDMAP, + {"johndoe", 1999, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {UIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1000}," " {\"container\": 1000, \"host\": 2000, \"size\": 1000}]"), 1, /* privileged */ - {"johndoe", 2000, 0, 0, NULL}, - 1000, 1000, - NXT_ERROR, + {"johndoe", 2000, 0, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"uidmap\" field has no \"container\" entry for user " - "\"johndoe\" (uid 2000)") - }, - { - /* - * Unprivileged unit - * - * if no gid mapping and app creds and unit creds are the same, - * then we automatically add a map for the creds->base_gid. - * Then, child process can safely setgid(creds->base_gid) in - * the new namespace. - */ + "\"johndoe\" (uid 2000)")}, + {/* + * Unprivileged unit + * + * if no gid mapping and app creds and unit creds are the same, + * then we automatically add a map for the creds->base_gid. + * Then, child process can safely setgid(creds->base_gid) in + * the new namespace. + */ + GIDMAP, nxt_string("[]"), 0, {"nobody", 65534, 65534, 0, NULL}, 1000, + 1000, NXT_OK, nxt_string("")}, + {/* + * Unprivileged unit + * + * Inside the new namespace, we can have any gid but it + * should map to parent gid (in this case 1000) in parent + * namespace. + */ + GIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), + 0, {"root", 0, 0, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {GIDMAP, + nxt_string("[{\"container\": 65534, \"host\": 1000, \"size\": 1}]"), 0, + {"nobody", 65534, 65534, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {/* + * Unprivileged unit + * + * There's no mapping for "johndoe" (gid 1000) inside the namespace. + */ GIDMAP, - nxt_string("[]"), - 0, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - /* - * Unprivileged unit - * - * Inside the new namespace, we can have any gid but it - * should map to parent gid (in this case 1000) in parent - * namespace. - */ - GIDMAP, - nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), - 0, - {"root", 0, 0, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - GIDMAP, - nxt_string("[{\"container\": 65534, \"host\": 1000, \"size\": 1}]"), - 0, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - /* - * Unprivileged unit - * - * There's no mapping for "johndoe" (gid 1000) inside the namespace. - */ - GIDMAP, - nxt_string("[{\"container\": 65535, \"host\": 1000, \"size\": 1}]"), - 0, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_ERROR, + nxt_string("[{\"container\": 65535, \"host\": 1000, \"size\": 1}]"), 0, + {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"gidmap\" field has no \"container\" entry for " - "gid 1000.") - }, - { - GIDMAP, - nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 2}]"), - 0, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_ERROR, + "gid 1000.")}, + {GIDMAP, nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 2}]"), + 0, {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"gidmap\" field has an entry with \"size\": 2, but " - "for unprivileged unit it must be 1.") - }, - { - GIDMAP, - nxt_string("[{\"container\": 1000, \"host\": 1001, \"size\": 1}]"), - 0, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_ERROR, + "for unprivileged unit it must be 1.")}, + {GIDMAP, nxt_string("[{\"container\": 1000, \"host\": 1001, \"size\": 1}]"), + 0, {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"gidmap\" field has an entry for host gid 1001 but " - "unprivileged unit can only map itself (gid 1000) " - "into child namespaces.") - }, - { - GIDMAP, - nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 1}]"), - 0, - {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, - NXT_ERROR, + "unprivileged unit can only map itself (gid 1000) " + "into child namespaces.")}, + {GIDMAP, nxt_string("[{\"container\": 1000, \"host\": 1000, \"size\": 1}]"), + 0, {"johndoe", 1000, 1000, 3, gids}, 1000, 1000, NXT_ERROR, nxt_string("unprivileged unit disallow supplementary groups for " - "new namespace (user \"johndoe\" has 3 groups).") - }, + "new namespace (user \"johndoe\" has 3 groups).")}, /* privileged unit */ /* not root with capabilities */ - { - GIDMAP, - nxt_string("[]"), - 1, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - GIDMAP, - nxt_string(""), - 1, - {"johndoe", 1000, 1000, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - /* missing gid of {"user": "nobody"} */ - GIDMAP, - nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), - 1, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_ERROR, + {GIDMAP, nxt_string("[]"), 1, {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, + NXT_OK, nxt_string("")}, + {GIDMAP, nxt_string(""), 1, {"johndoe", 1000, 1000, 0, NULL}, 1000, 1000, + NXT_OK, nxt_string("")}, + {/* missing gid of {"user": "nobody"} */ + GIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}]"), + 1, {"nobody", 65534, 65534, 0, NULL}, 1000, 1000, NXT_ERROR, nxt_string("\"gidmap\" field has no \"container\" entry for " - "gid 65534.") - }, - { - /* solves the previous by mapping 65534 gids */ + "gid 65534.")}, + {/* solves the previous by mapping 65534 gids */ GIDMAP, - nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 65535}]"), - 1, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - /* solves by adding a separate mapping */ + nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 65535}]"), 1, + {"nobody", 65534, 65534, 0, NULL}, 1000, 1000, NXT_OK, nxt_string("")}, + {/* solves by adding a separate mapping */ GIDMAP, nxt_string("[{\"container\": 0, \"host\": 1000, \"size\": 1}," " {\"container\": 65534, \"host\": 1000, \"size\": 1}]"), - 1, - {"nobody", 65534, 65534, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, - { - /* - * Map a big range - */ + 1, {"nobody", 65534, 65534, 0, NULL}, 1000, 1000, NXT_OK, + nxt_string("")}, + {/* + * Map a big range + */ GIDMAP, - nxt_string("[{\"container\": 0, \"host\": 0, \"size\": 200000}]"), - 1, - {"johndoe", 100000, 100000, 0, NULL}, - 1000, 1000, - NXT_OK, - nxt_string("") - }, + nxt_string("[{\"container\": 0, \"host\": 0, \"size\": 200000}]"), 1, + {"johndoe", 100000, 100000, 0, NULL}, 1000, 1000, NXT_OK, + nxt_string("")}, { /* * Validate if supplementary groups are mapped @@ -330,7 +186,8 @@ static nxt_clone_creds_testcase_t testcases[] = { nxt_string("[]"), 1, {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, + 1000, + 1000, NXT_ERROR, nxt_string("\"gidmap\" field has no entries but user \"johndoe\" " "has 3 suplementary groups."), @@ -340,7 +197,8 @@ static nxt_clone_creds_testcase_t testcases[] = { nxt_string("[{\"container\": 0, \"host\": 0, \"size\": 1}]"), 1, {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, + 1000, + 1000, NXT_ERROR, nxt_string("\"gidmap\" field has no \"container\" entry for " "gid 1000."), @@ -350,7 +208,8 @@ static nxt_clone_creds_testcase_t testcases[] = { nxt_string("[{\"container\": 1000, \"host\": 0, \"size\": 1}]"), 1, {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, + 1000, + 1000, NXT_ERROR, nxt_string("\"gidmap\" field has missing suplementary gid mappings " "(found 1 out of 3)."), @@ -361,7 +220,8 @@ static nxt_clone_creds_testcase_t testcases[] = { " {\"container\": 10000, \"host\": 10000, \"size\": 1}]"), 1, {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, + 1000, + 1000, NXT_ERROR, nxt_string("\"gidmap\" field has missing suplementary gid mappings " "(found 2 out of 3)."), @@ -376,28 +236,27 @@ static nxt_clone_creds_testcase_t testcases[] = { " {\"container\": 60000, \"host\": 60000, \"size\": 1}]"), 1, {"johndoe", 1000, 1000, 3, gids}, - 1000, 1000, + 1000, + 1000, NXT_OK, nxt_string(""), }, }; - void nxt_cdecl -nxt_clone_test_log_handler(nxt_uint_t level, nxt_log_t *log, - const char *fmt, ...) -{ - u_char *p, *end; +nxt_clone_test_log_handler(nxt_uint_t level, nxt_log_t *log, const char *fmt, + ...) { + u_char *p, *end; va_list args; - nxt_clone_creds_ctx_t *ctx; - nxt_clone_creds_testcase_t *tc; + nxt_clone_creds_ctx_t *ctx; + nxt_clone_creds_testcase_t *tc; u_char msg[NXT_MAX_ERROR_STR]; - p = msg; + p = msg; end = msg + NXT_MAX_ERROR_STR; ctx = log->ctx; - tc = ctx->tc; + tc = ctx->tc; va_start(args, fmt); p = nxt_vsprintf(p, end, fmt, args); @@ -416,30 +275,22 @@ nxt_clone_test_log_handler(nxt_uint_t level, nxt_log_t *log, if (!nxt_str_eq(&tc->errmsg, msg, (nxt_uint_t) (p - msg - 1))) { nxt_log_error(NXT_LOG_ERR, &nxt_main_log, - "error log mismatch: got [%s] but wants [%V]", - msg, &tc->errmsg); + "error log mismatch: got [%s] but wants [%V]", msg, &tc->errmsg); return; } } - nxt_int_t -nxt_clone_creds_test(nxt_thread_t *thr) -{ - nxt_mp_t *mp; - nxt_int_t ret; - nxt_uint_t count, i; - nxt_task_t *task; - nxt_runtime_t rt; - nxt_clone_creds_ctx_t ctx; - - nxt_log_t nxt_clone_creds_log = { - NXT_LOG_INFO, - 0, - nxt_clone_test_log_handler, - NULL, - &ctx - }; +nxt_clone_creds_test(nxt_thread_t *thr) { + nxt_mp_t *mp; + nxt_int_t ret; + nxt_uint_t count, i; + nxt_task_t *task; + nxt_runtime_t rt; + nxt_clone_creds_ctx_t ctx; + + nxt_log_t nxt_clone_creds_log + = {NXT_LOG_INFO, 0, nxt_clone_test_log_handler, NULL, &ctx}; nxt_thread_time_update(thr); @@ -454,11 +305,11 @@ nxt_clone_creds_test(nxt_thread_t *thr) rt.mem_pool = mp; - test_log = task->log; - task->log = &nxt_clone_creds_log; + test_log = task->log; + task->log = &nxt_clone_creds_log; task->thread = thr; - count = sizeof(testcases)/sizeof(nxt_clone_creds_testcase_t); + count = sizeof(testcases) / sizeof(nxt_clone_creds_testcase_t); for (i = 0; i < count; i++) { ret = nxt_clone_test_mappings(task, mp, &ctx, &testcases[i]); @@ -479,14 +330,12 @@ nxt_clone_creds_test(nxt_thread_t *thr) return ret; } - nxt_int_t nxt_clone_test_mappings(nxt_task_t *task, nxt_mp_t *mp, - nxt_clone_creds_ctx_t *ctx, nxt_clone_creds_testcase_t *tc) -{ - nxt_int_t ret; - nxt_runtime_t *rt; - nxt_clone_credential_map_t map; + nxt_clone_creds_ctx_t *ctx, nxt_clone_creds_testcase_t *tc) { + nxt_int_t ret; + nxt_runtime_t *rt; + nxt_clone_credential_map_t map; rt = task->thread->runtime; @@ -527,11 +376,9 @@ nxt_clone_test_mappings(nxt_task_t *task, nxt_mp_t *mp, return NXT_OK; } - nxt_int_t nxt_clone_test_map_assert(nxt_task_t *task, nxt_clone_creds_testcase_t *tc, - nxt_clone_credential_map_t *map) -{ + nxt_clone_credential_map_t *map) { nxt_int_t ret; if (tc->map_type == UIDMAP) { @@ -542,8 +389,8 @@ nxt_clone_test_map_assert(nxt_task_t *task, nxt_clone_creds_testcase_t *tc, if (ret != tc->result) { nxt_log_error(NXT_LOG_ERR, &nxt_main_log, - "return %d instead of %d (map: %V)", ret, tc->result, - &tc->map_data); + "return %d instead of %d (map: %V)", ret, tc->result, + &tc->map_data); return NXT_ERROR; } @@ -551,18 +398,16 @@ nxt_clone_test_map_assert(nxt_task_t *task, nxt_clone_creds_testcase_t *tc, return NXT_OK; } - static nxt_int_t nxt_clone_test_parse_map(nxt_task_t *task, nxt_str_t *map_str, - nxt_clone_credential_map_t *map) -{ + nxt_clone_credential_map_t *map) { nxt_uint_t i; - nxt_runtime_t *rt; - nxt_conf_value_t *array, *obj, *value; + nxt_runtime_t *rt; + nxt_conf_value_t *array, *obj, *value; - static const nxt_str_t host_name = nxt_string("host"); - static const nxt_str_t cont_name = nxt_string("container"); - static const nxt_str_t size_name = nxt_string("size"); + static const nxt_str_t host_name = nxt_string("host"); + static const nxt_str_t cont_name = nxt_string("container"); + static const nxt_str_t size_name = nxt_string("size"); rt = task->thread->runtime; @@ -577,8 +422,8 @@ nxt_clone_test_parse_map(nxt_task_t *task, nxt_str_t *map_str, return NXT_OK; } - map->map = nxt_mp_alloc(rt->mem_pool, - map->size * sizeof(nxt_clone_map_entry_t)); + map->map + = nxt_mp_alloc(rt->mem_pool, map->size * sizeof(nxt_clone_map_entry_t)); if (map->map == NULL) { return NXT_ERROR; @@ -587,13 +432,13 @@ nxt_clone_test_parse_map(nxt_task_t *task, nxt_str_t *map_str, for (i = 0; i < map->size; i++) { obj = nxt_conf_get_array_element(array, i); - value = nxt_conf_get_object_member(obj, &host_name, NULL); + value = nxt_conf_get_object_member(obj, &host_name, NULL); map->map[i].host = nxt_conf_get_number(value); value = nxt_conf_get_object_member(obj, &cont_name, NULL); map->map[i].container = nxt_conf_get_number(value); - value = nxt_conf_get_object_member(obj, &size_name, NULL); + value = nxt_conf_get_object_member(obj, &size_name, NULL); map->map[i].size = nxt_conf_get_number(value); } diff --git a/src/test/nxt_cq_test.c b/src/test/nxt_cq_test.c index ae69505a3..b72830836 100644 --- a/src/test/nxt_cq_test.c +++ b/src/test/nxt_cq_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -8,10 +7,10 @@ #include #ifndef NXT_NCQ_TEST -#define NXT_NCQ_TEST 1 +#define NXT_NCQ_TEST 1 #endif -#define NXT_QTEST_USE_THREAD 0 +#define NXT_QTEST_USE_THREAD 0 #if NXT_NCQ_TEST #include @@ -20,11 +19,11 @@ #endif -#define MAX_ITER 20 -#define STAT_ITER 5 -#define MIN_COV 0.02 +#define MAX_ITER 20 +#define STAT_ITER 5 +#define MIN_COV 0.02 -extern char **environ; +extern char **environ; static uintptr_t nops = 10000000; static uintptr_t nprocs_enq = 0; @@ -37,23 +36,19 @@ static uintptr_t nprocs_faa = 0; static uintptr_t nprocs = 1; - static size_t -elapsed_time(size_t us) -{ - struct timeval t; +elapsed_time(size_t us) { + struct timeval t; - gettimeofday(&t, NULL); + gettimeofday(&t, NULL); - return t.tv_sec * 1000000 + t.tv_usec - us; + return t.tv_sec * 1000000 + t.tv_usec - us; } - static double -mean(const double *times, int n) -{ - int i; - double sum; +mean(const double *times, int n) { + int i; + double sum; sum = 0; @@ -64,12 +59,10 @@ mean(const double *times, int n) return sum / n; } - static double -cov(const double *times, double mean, int n) -{ - int i; - double variance; +cov(const double *times, double mean, int n) { + int i; + double variance; variance = 0; @@ -84,61 +77,58 @@ cov(const double *times, double mean, int n) typedef struct { #if NXT_NCQ_TEST - nxt_nncq_t free_queue; - nxt_nncq_t active_queue; + nxt_nncq_t free_queue; + nxt_nncq_t active_queue; #else - nxt_nvbcq_t free_queue; - nxt_nvbcq_t active_queue; + nxt_nvbcq_t free_queue; + nxt_nvbcq_t active_queue; #endif - uint32_t counter; + uint32_t counter; } nxt_cq_t; - -static nxt_cq_t *pgq; +static nxt_cq_t *pgq; #if NXT_NCQ_TEST -#define nxt_cq_enqueue nxt_nncq_enqueue -#define nxt_cq_dequeue nxt_nncq_dequeue -#define nxt_cq_empty nxt_nncq_empty -#define nxt_cq_init nxt_nncq_init -#define NXT_CQ_SIZE NXT_NNCQ_SIZE +#define nxt_cq_enqueue nxt_nncq_enqueue +#define nxt_cq_dequeue nxt_nncq_dequeue +#define nxt_cq_empty nxt_nncq_empty +#define nxt_cq_init nxt_nncq_init +#define NXT_CQ_SIZE NXT_NNCQ_SIZE #else -#define nxt_cq_enqueue nxt_nvbcq_enqueue -#define nxt_cq_dequeue nxt_nvbcq_dequeue -#define nxt_cq_empty nxt_nvbcq_empty -#define nxt_cq_init nxt_nvbcq_init -#define NXT_CQ_SIZE NXT_NVBCQ_SIZE +#define nxt_cq_enqueue nxt_nvbcq_enqueue +#define nxt_cq_dequeue nxt_nvbcq_dequeue +#define nxt_cq_empty nxt_nvbcq_empty +#define nxt_cq_init nxt_nvbcq_init +#define NXT_CQ_SIZE NXT_NVBCQ_SIZE #endif typedef struct { - int id; - uint64_t enq; - uint64_t deq; - uint64_t wait_enq; - uint64_t wait_deq; - uint64_t own_res; - uint64_t cas; - uint64_t faa; + int id; + uint64_t enq; + uint64_t deq; + uint64_t wait_enq; + uint64_t wait_deq; + uint64_t own_res; + uint64_t cas; + uint64_t faa; #if NXT_QTEST_USE_THREAD - nxt_thread_handle_t handle; + nxt_thread_handle_t handle; #else - nxt_pid_t pid; - int status; + nxt_pid_t pid; + int status; #endif } nxt_worker_info_t; - static void -cas_worker(void *p) -{ - nxt_cq_t *q; +cas_worker(void *p) { + nxt_cq_t *q; uint32_t c; uintptr_t i; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_cas; i++) { @@ -150,15 +140,13 @@ cas_worker(void *p) } } - static void -faa_worker(void *p) -{ - nxt_cq_t *q; +faa_worker(void *p) { + nxt_cq_t *q; uintptr_t i; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_faa; i++) { @@ -167,15 +155,13 @@ faa_worker(void *p) } } - static void -enq_deq_worker(void *p) -{ - nxt_cq_t *q; +enq_deq_worker(void *p) { + nxt_cq_t *q; uintptr_t i, v; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_enq_deq; i++) { @@ -199,15 +185,13 @@ enq_deq_worker(void *p) } } - static void -enq_worker(void *p) -{ - nxt_cq_t *q; +enq_worker(void *p) { + nxt_cq_t *q; uintptr_t i, v; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_enq; i++) { @@ -220,15 +204,13 @@ enq_worker(void *p) } } - static void -deq_worker(void *p) -{ - nxt_cq_t *q; +deq_worker(void *p) { + nxt_cq_t *q; uintptr_t i, v; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_deq; i++) { @@ -241,19 +223,16 @@ deq_worker(void *p) } } - static void -wenq_worker(void *p) -{ - nxt_cq_t *q; +wenq_worker(void *p) { + nxt_cq_t *q; uintptr_t i, v; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_wenq; i++) { - do { wi->wait_enq++; v = nxt_cq_dequeue(&q->free_queue); @@ -266,19 +245,16 @@ wenq_worker(void *p) } } - static void -wdeq_worker(void *p) -{ - nxt_cq_t *q; +wdeq_worker(void *p) { + nxt_cq_t *q; uintptr_t i, v; - nxt_worker_info_t *wi; + nxt_worker_info_t *wi; - q = pgq; + q = pgq; wi = p; for (i = 0; i < nops / nprocs_wdeq; i++) { - do { wi->wait_deq++; v = nxt_cq_dequeue(&q->active_queue); @@ -291,18 +267,16 @@ wdeq_worker(void *p) } } - static nxt_int_t -worker_create(nxt_worker_info_t *wi, int id, nxt_thread_start_t start) -{ +worker_create(nxt_worker_info_t *wi, int id, nxt_thread_start_t start) { wi->id = id; #if NXT_QTEST_USE_THREAD - nxt_thread_link_t *link; + nxt_thread_link_t *link; link = nxt_zalloc(sizeof(nxt_thread_link_t)); - link->start = start; + link->start = start; link->work.data = wi; return nxt_thread_create(&wi->handle, link); @@ -322,10 +296,8 @@ worker_create(nxt_worker_info_t *wi, int id, nxt_thread_start_t start) #endif } - static void -worker_wait(nxt_worker_info_t *wi) -{ +worker_wait(nxt_worker_info_t *wi) { #if NXT_QTEST_USE_THREAD pthread_join(wi->handle, NULL); @@ -334,22 +306,20 @@ worker_wait(nxt_worker_info_t *wi) #endif } - int nxt_cdecl -main(int argc, char **argv) -{ +main(int argc, char **argv) { int i, k, id, verbose, objective, rk; - char *a; + char *a; size_t start, elapsed; - double *stats, m, c; + double *stats, m, c; uint64_t total_ops; uintptr_t j; nxt_task_t task; - nxt_thread_t *thr; - nxt_worker_info_t *wi; + nxt_thread_t *thr; + nxt_worker_info_t *wi; double times[MAX_ITER], mopsec[MAX_ITER]; - verbose = 0; + verbose = 0; objective = 0; for (i = 1; i < argc; i++) { @@ -422,13 +392,13 @@ main(int argc, char **argv) } nxt_main_log.level = NXT_LOG_INFO; - task.log = &nxt_main_log; + task.log = &nxt_main_log; - thr = nxt_thread(); + thr = nxt_thread(); thr->task = &task; pgq = mmap(NULL, sizeof(nxt_cq_t), PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, -1, 0); + MAP_ANON | MAP_SHARED, -1, 0); if (pgq == MAP_FAILED) { return 2; } @@ -436,7 +406,7 @@ main(int argc, char **argv) nxt_cq_init(&pgq->free_queue); nxt_cq_init(&pgq->active_queue); - for(i = 0; i < NXT_CQ_SIZE; i++) { + for (i = 0; i < NXT_CQ_SIZE; i++) { nxt_cq_enqueue(&pgq->free_queue, i); } @@ -446,7 +416,7 @@ main(int argc, char **argv) } wi = mmap(NULL, nprocs * sizeof(nxt_worker_info_t), PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, -1, 0); + MAP_ANON | MAP_SHARED, -1, 0); if (wi == MAP_FAILED) { return 3; } @@ -457,7 +427,7 @@ main(int argc, char **argv) nxt_cq_init(&pgq->free_queue); nxt_cq_init(&pgq->active_queue); - for(i = 0; i < NXT_CQ_SIZE; i++) { + for (i = 0; i < NXT_CQ_SIZE; i++) { nxt_cq_enqueue(&pgq->free_queue, i); } @@ -500,13 +470,13 @@ main(int argc, char **argv) elapsed = elapsed_time(start); for (j = 1; j < nprocs; j++) { - wi[0].enq += wi[j].enq; - wi[0].deq += wi[j].deq; + wi[0].enq += wi[j].enq; + wi[0].deq += wi[j].deq; wi[0].wait_enq += wi[j].wait_enq; wi[0].wait_deq += wi[j].wait_deq; - wi[0].own_res += wi[j].own_res; - wi[0].cas += wi[j].cas; - wi[0].faa += wi[j].faa; + wi[0].own_res += wi[j].own_res; + wi[0].cas += wi[j].cas; + wi[0].faa += wi[j].faa; } total_ops = wi[0].enq + wi[0].deq + wi[0].cas + wi[0].faa; @@ -515,22 +485,23 @@ main(int argc, char **argv) total_ops = nops; } - times[k] = elapsed / 1000.0; + times[k] = elapsed / 1000.0; mopsec[k] = (double) total_ops / elapsed; if (verbose >= 2) { - printf("enq %10"PRIu64"\n", wi[0].enq); - printf("deq %10"PRIu64"\n", wi[0].deq); - printf("wait_enq %10"PRIu64"\n", wi[0].wait_enq); - printf("wait_deq %10"PRIu64"\n", wi[0].wait_deq); - printf("own_res %10"PRIu64"\n", wi[0].own_res); - printf("cas %10"PRIu64"\n", wi[0].cas); - printf("faa %10"PRIu64"\n", wi[0].faa); - printf("total ops %10"PRIu64"\n", total_ops); + printf("enq %10" PRIu64 "\n", wi[0].enq); + printf("deq %10" PRIu64 "\n", wi[0].deq); + printf("wait_enq %10" PRIu64 "\n", wi[0].wait_enq); + printf("wait_deq %10" PRIu64 "\n", wi[0].wait_deq); + printf("own_res %10" PRIu64 "\n", wi[0].own_res); + printf("cas %10" PRIu64 "\n", wi[0].cas); + printf("faa %10" PRIu64 "\n", wi[0].faa); + printf("total ops %10" PRIu64 "\n", total_ops); printf("Mops/sec %13.2f\n", mopsec[k]); printf("elapsed %10d us\n", (int) elapsed); - printf("per op %10d ns\n", (int) ((1000 * elapsed) / total_ops)); + printf("per op %10d ns\n", + (int) ((1000 * elapsed) / total_ops)); } if (k >= STAT_ITER) { @@ -543,12 +514,12 @@ main(int argc, char **argv) if (objective == 0) { printf(" #%02d elapsed time: %.2f ms; Mops/sec %.2f; " "mean time %.2f ms; cov %.4f\n", - (int) k + 1, times[k], mopsec[k], m, c); + (int) k + 1, times[k], mopsec[k], m, c); } else { printf(" #%02d elapsed time: %.2f ms; Mops/sec %.2f; " "mean Mop/sec %.2f; cov %.4f\n", - (int) k + 1, times[k], mopsec[k], m, c); + (int) k + 1, times[k], mopsec[k], m, c); } } @@ -569,7 +540,7 @@ main(int argc, char **argv) } else { if (verbose >= 1) { printf(" #%02d elapsed time: %.2f ms; Mops/sec %.2f\n", - (int) k + 1, times[k], mopsec[k]); + (int) k + 1, times[k], mopsec[k]); } } } diff --git a/src/test/nxt_gmtime_test.c b/src/test/nxt_gmtime_test.c index cec81ab45..70808d46a 100644 --- a/src/test/nxt_gmtime_test.c +++ b/src/test/nxt_gmtime_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,42 +10,35 @@ #if (NXT_TIME_T_SIZE == 4) /* A 86400-fold number below 2^31. */ -#define NXT_GMTIME_MAX 2147472000 +#define NXT_GMTIME_MAX 2147472000 #else /* * March 19, 29398 is maximum valid data if nxt_uint_t * is 4 bytes size whilst nxt_time_t is 8 bytes size. */ -#define NXT_GMTIME_MAX 865550793600 +#define NXT_GMTIME_MAX 865550793600 #endif nxt_int_t -nxt_gmtime_test(nxt_thread_t *thr) -{ - struct tm tm0, *tm1; - nxt_time_t s; - nxt_nsec_t start, end; +nxt_gmtime_test(nxt_thread_t *thr) { + struct tm tm0, *tm1; + nxt_time_t s; + nxt_nsec_t start, end; nxt_thread_time_update(thr); nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime test started"); for (s = 0; s < NXT_GMTIME_MAX; s += 86400) { - nxt_gmtime(s, &tm0); tm1 = gmtime(&s); - if (tm0.tm_mday != tm1->tm_mday - || tm0.tm_mon != tm1->tm_mon - || tm0.tm_year != tm1->tm_year - || tm0.tm_yday != tm1->tm_yday - || tm0.tm_wday != tm1->tm_wday) - { - nxt_log_alert(thr->log, - "gmtime test failed: %T @ %02d.%02d.%d", - s, tm1->tm_mday, tm1->tm_mon + 1, - tm1->tm_year + 1900); + if (tm0.tm_mday != tm1->tm_mday || tm0.tm_mon != tm1->tm_mon + || tm0.tm_year != tm1->tm_year || tm0.tm_yday != tm1->tm_yday + || tm0.tm_wday != tm1->tm_wday) { + nxt_log_alert(thr->log, "gmtime test failed: %T @ %02d.%02d.%d", s, + tm1->tm_mday, tm1->tm_mon + 1, tm1->tm_year + 1900); return NXT_ERROR; } } @@ -63,7 +55,7 @@ nxt_gmtime_test(nxt_thread_t *thr) end = nxt_thread_monotonic_time(thr); nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_gmtime(): %0.1fns", - (end - start) / 10000000.0); + (end - start) / 10000000.0); nxt_thread_time_update(thr); @@ -77,7 +69,7 @@ nxt_gmtime_test(nxt_thread_t *thr) end = nxt_thread_monotonic_time(thr); nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime(): %0.1fns", - (end - start) / 10000000.0); + (end - start) / 10000000.0); nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime test passed"); return NXT_OK; diff --git a/src/test/nxt_http_parse_test.c b/src/test/nxt_http_parse_test.c index 474b3f8d2..97545b5e9 100644 --- a/src/test/nxt_http_parse_test.c +++ b/src/test/nxt_http_parse_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Valentin V. Bartenev @@ -7,539 +6,322 @@ #include #include "nxt_tests.h" - typedef struct { - nxt_str_t method; - nxt_str_t target; - nxt_str_t args; - u_char version[8]; + nxt_str_t method; + nxt_str_t target; + nxt_str_t args; + u_char version[8]; /* target with "/." */ - unsigned complex_target:1; + unsigned complex_target :1; /* target with "%" */ - unsigned quoted_target:1; + unsigned quoted_target :1; /* target with " " */ - unsigned space_in_target:1; + unsigned space_in_target:1; } nxt_http_parse_test_request_line_t; - typedef struct { - nxt_int_t result; - unsigned discard_unsafe_fields:1; + nxt_int_t result; + unsigned discard_unsafe_fields:1; } nxt_http_parse_test_fields_t; - typedef union { - void *pointer; - nxt_http_parse_test_fields_t fields; - nxt_http_parse_test_request_line_t request_line; + void *pointer; + nxt_http_parse_test_fields_t fields; + nxt_http_parse_test_request_line_t request_line; } nxt_http_parse_test_data_t; - typedef struct { - nxt_str_t request; - nxt_int_t result; - nxt_int_t (*handler)(nxt_http_request_parse_t *rp, - nxt_http_parse_test_data_t *data, - nxt_str_t *request, nxt_log_t *log); + nxt_str_t request; + nxt_int_t result; + nxt_int_t (*handler)(nxt_http_request_parse_t *rp, + nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log); - nxt_http_parse_test_data_t data; + nxt_http_parse_test_data_t data; } nxt_http_parse_test_case_t; - -static nxt_int_t nxt_http_parse_test_run(nxt_http_request_parse_t *rp, - nxt_str_t *request); -static nxt_int_t nxt_http_parse_test_bench(nxt_thread_t *thr, - nxt_str_t *request, nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n); -static nxt_int_t nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp, - nxt_http_parse_test_data_t *data, - nxt_str_t *request, nxt_log_t *log); -static nxt_int_t nxt_http_parse_test_fields(nxt_http_request_parse_t *rp, +static nxt_int_t +nxt_http_parse_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request); +static nxt_int_t +nxt_http_parse_test_bench(nxt_thread_t *thr, nxt_str_t *request, + nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n); +static nxt_int_t +nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp, + nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log); +static nxt_int_t +nxt_http_parse_test_fields(nxt_http_request_parse_t *rp, nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log); -static nxt_int_t nxt_http_test_header_return(void *ctx, nxt_http_field_t *field, - uintptr_t data); +static nxt_int_t +nxt_http_test_header_return(void *ctx, nxt_http_field_t *field, uintptr_t data); -static nxt_http_parse_test_case_t nxt_http_test_cases[] = { - { - nxt_string("GET / HTTP/1.0\r\n\r\n"), - NXT_DONE, - &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/"), - nxt_null_string, - "HTTP/1.0", - 0, 0, 0 - }} - }, - { - nxt_string("XXX-METHOD /d.ir/fi+le.ext?key=val HTTP/1.2\n\n"), - NXT_DONE, +static nxt_http_parse_test_case_t nxt_http_test_cases[] = { + {nxt_string("GET / HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("XXX-METHOD"), - nxt_string("/d.ir/fi+le.ext?key=val"), - nxt_string("key=val"), - "HTTP/1.2", - 0, 0, 0 - }} - }, - { - nxt_string("GET /di.r/? HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/"), nxt_null_string, + "HTTP/1.0", 0, 0, 0}}}, + {nxt_string("XXX-METHOD /d.ir/fi+le.ext?key=val HTTP/1.2\n\n"), + NXT_DONE, &nxt_http_parse_test_request_line, + {.request_line + = {nxt_string("XXX-METHOD"), nxt_string("/d.ir/fi+le.ext?key=val"), + nxt_string("key=val"), "HTTP/1.2", 0, 0, 0}}}, + {nxt_string("GET /di.r/? HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/di.r/?"), - nxt_string(""), - "HTTP/1.0", - 0, 0, 0 - }} - }, - { - nxt_string("GEt / HTTP/1.0\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET /\0 HTTP/1.0\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET /\r HTTP/1.0\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET /\n HTTP/1.0\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.0\r\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/2.0\r\n"), - NXT_HTTP_PARSE_UNSUPPORTED_VERSION, - NULL, { NULL } - }, - { - nxt_string("GET /. HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/di.r/?"), + nxt_string(""), "HTTP/1.0", 0, 0, 0}}}, + {nxt_string("GEt / HTTP/1.0\r\n\r\n"), NXT_HTTP_PARSE_INVALID, NULL, + {NULL}}, + {nxt_string("GET /\0 HTTP/1.0\r\n\r\n"), NXT_HTTP_PARSE_INVALID, NULL, + {NULL}}, + {nxt_string("GET /\r HTTP/1.0\r\n\r\n"), NXT_HTTP_PARSE_INVALID, NULL, + {NULL}}, + {nxt_string("GET /\n HTTP/1.0\r\n\r\n"), NXT_HTTP_PARSE_INVALID, NULL, + {NULL}}, + {nxt_string("GET / HTTP/1.0\r\r\n"), NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/2.0\r\n"), NXT_HTTP_PARSE_UNSUPPORTED_VERSION, NULL, + {NULL}}, + {nxt_string("GET /. HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/."), - nxt_null_string, - "HTTP/1.0", - 1, 0, 0 - }} - }, - { - nxt_string("GET /# HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/."), nxt_null_string, + "HTTP/1.0", 1, 0, 0}}}, + {nxt_string("GET /# HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/#"), - nxt_null_string, - "HTTP/1.0", - 1, 0, 0 - }} - }, - { - nxt_string("GET /?# HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/#"), nxt_null_string, + "HTTP/1.0", 1, 0, 0}}}, + {nxt_string("GET /?# HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/?#"), - nxt_string(""), - "HTTP/1.0", - 1, 0, 0 - }} - }, - { - nxt_string("GET // HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/?#"), nxt_string(""), + "HTTP/1.0", 1, 0, 0}}}, + {nxt_string("GET // HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("//"), - nxt_null_string, - "HTTP/1.0", - 1, 0, 0 - }} - }, - { - nxt_string("GET /%20 HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("//"), nxt_null_string, + "HTTP/1.0", 1, 0, 0}}}, + {nxt_string("GET /%20 HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/%20"), - nxt_null_string, - "HTTP/1.0", - 0, 1, 0 - }} - }, - { - nxt_string("GET / a HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/%20"), + nxt_null_string, "HTTP/1.0", 0, 1, 0}}}, + {nxt_string("GET / a HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/ a"), - nxt_null_string, - "HTTP/1.0", - 0, 0, 1 - }} - }, - { - nxt_string("GET /na %20me.ext?args HTTP/1.0\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/ a"), nxt_null_string, + "HTTP/1.0", 0, 0, 1}}}, + {nxt_string("GET /na %20me.ext?args HTTP/1.0\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/na %20me.ext?args"), - nxt_string("args"), - "HTTP/1.0", - 0, 1, 1 - }} - }, - { - nxt_string("GET / HTTP/1.0 HTTP/1.1\r\n\r\n"), - NXT_DONE, + {.request_line = {nxt_string("GET"), nxt_string("/na %20me.ext?args"), + nxt_string("args"), "HTTP/1.0", 0, 1, 1}}}, + {nxt_string("GET / HTTP/1.0 HTTP/1.1\r\n\r\n"), NXT_DONE, &nxt_http_parse_test_request_line, - { .request_line = { - nxt_string("GET"), - nxt_string("/ HTTP/1.0"), - nxt_null_string, - "HTTP/1.1", - 0, 0, 1 - }} - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: example.com\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host:example.com \r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host:\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - ":Host: example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Ho_st: example.com\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Ho\0st: example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Ho\rst: example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Ho\nst: example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host : example.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: exa\0mple.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: exa\rmple.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: exa\bmple.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: пример.испытание\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: xn--e1afmkfd.xn--80akhbyknj4f\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: exa\nmple.com\r\n\r\n"), - NXT_HTTP_PARSE_INVALID, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "Host: exa\tmple.com\r\n\r\n"), - NXT_DONE, - NULL, { NULL } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "X-Unknown-Header: value\r\n" - "X-Good-Header: value\r\n" - "!#$%&'*+.^_`|~: skipped\r\n\r\n"), - NXT_DONE, - &nxt_http_parse_test_fields, - { .fields = { NXT_OK, 1 } } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "X-Good-Header: value\r\n" - "X-Unknown-Header: value\r\n" - "X-Bad-Header: value\r\n\r\n"), - NXT_DONE, - &nxt_http_parse_test_fields, - { .fields = { NXT_ERROR, 1 } } - }, - { - nxt_string("GET / HTTP/1.1\r\n" - "!#$%&'*+.^_`|~: allowed\r\n\r\n"), - NXT_DONE, - &nxt_http_parse_test_fields, - { .fields = { NXT_ERROR, 0 } } - }, + {.request_line = {nxt_string("GET"), nxt_string("/ HTTP/1.0"), + nxt_null_string, "HTTP/1.1", 0, 0, 1}}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: example.com\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host:example.com \r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host:\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + ":Host: example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Ho_st: example.com\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Ho\0st: example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Ho\rst: example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Ho\nst: example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host : example.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: exa\0mple.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: exa\rmple.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: exa\bmple.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: пример.испытание\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: xn--e1afmkfd.xn--80akhbyknj4f\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: exa\nmple.com\r\n\r\n"), + NXT_HTTP_PARSE_INVALID, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "Host: exa\tmple.com\r\n\r\n"), + NXT_DONE, NULL, {NULL}}, + {nxt_string("GET / HTTP/1.1\r\n" + "X-Unknown-Header: value\r\n" + "X-Good-Header: value\r\n" + "!#$%&'*+.^_`|~: skipped\r\n\r\n"), + NXT_DONE, &nxt_http_parse_test_fields, {.fields = {NXT_OK, 1}}}, + {nxt_string("GET / HTTP/1.1\r\n" + "X-Good-Header: value\r\n" + "X-Unknown-Header: value\r\n" + "X-Bad-Header: value\r\n\r\n"), + NXT_DONE, &nxt_http_parse_test_fields, {.fields = {NXT_ERROR, 1}}}, + {nxt_string("GET / HTTP/1.1\r\n" + "!#$%&'*+.^_`|~: allowed\r\n\r\n"), + NXT_DONE, &nxt_http_parse_test_fields, {.fields = {NXT_ERROR, 0}}}, }; -static nxt_http_field_proc_t nxt_http_test_fields[] = { - { nxt_string("X-Bad-Header"), - &nxt_http_test_header_return, - NXT_ERROR }, +static nxt_http_field_proc_t nxt_http_test_fields[] = { + {nxt_string("X-Bad-Header"), &nxt_http_test_header_return, NXT_ERROR}, - { nxt_string("X-Good-Header"), - &nxt_http_test_header_return, - NXT_OK }, + {nxt_string("X-Good-Header"), &nxt_http_test_header_return, NXT_OK}, - { nxt_string("!#$%&'*+.^_`|~"), - &nxt_http_test_header_return, - NXT_ERROR }, + {nxt_string("!#$%&'*+.^_`|~"), &nxt_http_test_header_return, NXT_ERROR}, }; -static nxt_lvlhsh_t nxt_http_test_fields_hash; - - -static nxt_http_field_proc_t nxt_http_test_bench_fields[] = { - { nxt_string("Host"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("User-Agent"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Accept"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Accept-Encoding"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Accept-Language"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Connection"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Content-Length"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Content-Range"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Content-Type"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Cookie"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Range"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("If-Range"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Transfer-Encoding"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Expect"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Via"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("If-Modified-Since"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("If-Unmodified-Since"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("If-Match"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("If-None-Match"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Referer"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Date"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Upgrade"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Authorization"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Keep-Alive"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Forwarded-For"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Forwarded-Host"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Forwarded-Proto"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Http-Method-Override"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Real-IP"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("X-Request-ID"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("TE"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Pragma"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Cache-Control"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Origin"), - &nxt_http_test_header_return, NXT_OK }, - { nxt_string("Upgrade-Insecure-Requests"), - &nxt_http_test_header_return, NXT_OK }, +static nxt_lvlhsh_t nxt_http_test_fields_hash; + + +static nxt_http_field_proc_t nxt_http_test_bench_fields[] = { + {nxt_string("Host"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("User-Agent"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Accept"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Accept-Encoding"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Accept-Language"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Connection"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Content-Length"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Content-Range"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Content-Type"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Cookie"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Range"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("If-Range"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Transfer-Encoding"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Expect"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Via"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("If-Modified-Since"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("If-Unmodified-Since"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("If-Match"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("If-None-Match"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Referer"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Date"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Upgrade"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Authorization"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Keep-Alive"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("X-Forwarded-For"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("X-Forwarded-Host"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("X-Forwarded-Proto"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("X-Http-Method-Override"), &nxt_http_test_header_return, + NXT_OK}, + {nxt_string("X-Real-IP"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("X-Request-ID"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("TE"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Pragma"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Cache-Control"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Origin"), &nxt_http_test_header_return, NXT_OK}, + {nxt_string("Upgrade-Insecure-Requests"), &nxt_http_test_header_return, + NXT_OK}, }; -static nxt_str_t nxt_http_test_simple_request = nxt_string( - "GET /page HTTP/1.1\r\n" - "Host: example.com\r\n\r\n" -); +static nxt_str_t nxt_http_test_simple_request + = nxt_string("GET /page HTTP/1.1\r\n" + "Host: example.com\r\n\r\n"); static nxt_str_t nxt_http_test_big_request = nxt_string( "POST /path/to/very/interesting/article/on.this.site?arg1=value&arg2=value" - "2&very_big_arg=even_bigger_value HTTP/1.1\r\n" + "2&very_big_arg=even_bigger_value HTTP/1.1\r\n" "Host: www.example.com\r\n" "User-Agent: Mozilla/5.0 (X11; Gentoo Linux x86_64; rv:42.0) Firefox/42.0" - "\r\n" + "\r\n" "Accept: text/html,application/json,application/xml;q=0.9,*/*;q=0.8\r\n" "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n" "Accept-Encoding: gzip, deflate, br\r\n" "If-Modified-Since: Wed, 31 Dec 1986 16:00:00 GMT\r\n" "Referer: https://example.org/path/to/not-interesting/article.html\r\n" "Cookie: name=value; name2=value2; some_big_cookie=iVBORw0KGgoAAAANSUhEUgA" - "AAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAABmelRY" - "dFJhdyBwcm9maWxlIHR5cGUgZXhpZgAAeNptitsJgEAMBP9ThSWsZy6PcvKhcB1YvjEni" - "ODAwjAs7ec4aCmkEXc1cREk7OwtUgyTFRA3BU+vFPjS7gUI/p46Q0u2fP/1B7oA1Scbwk" - "nkf9gAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACfUExURQwMDDw8PFBQUAICAhQUFAcHBxs" - "bGxEREQkJCTk5OTU1NSAgIFRUVB8fH0xMTCUlJVtbW0pKSikpKS8vL0BAQEZGRjMzM2Bg" - "YL6+vsDAwLS0tF1dXXJycrGxsWVlZWhoaKenp29vb6urq8TExHp6epSUlLu7u66urqOjo" - "5ycnH9/f4CAgJOTk5qamo6OjoWFhYiIiHd3d8nJyc/Pz9LS0ojXP1QAAAihSURBVFjDZV" - "eHdqM6EBUYEEh0EM3gCu41+/7/294dCSfZsxOHeM8yV3f6iGVGYohNEtJPGEjPiSLpMTz" - "zokg8DmGOCOm/P0I6MTPaBGDPCGEYV3kEzchjzPOSPIkk8BzuM8fSCOFfALER+6MdpnaV" - "55FMoOP7UliioK8QzpiT0Qv0Fl4lDJvFPwChETuHFjhw7vhRVcGAXDqcfhhnRaZUWeJTW" - "pYVCBEYAJihtCsUpIhyq6win3ueDCoRBIknJRwACtz3AJhDYBhESsmyEjhaKv0MRJIIFR" - "d4XyYqC1RWwQFeBF2CcApCmEFI2KwHTRIrsMq8UnYcRUkehKtlaGeq8BjowKHEQf7oEgH" - "JcKRWpSeZpTIrs5dKlGX9fF7GfrtdWqDAuce1IyOtLbWyRKRYIIIPBo63gswO07q20/p6" - "2txvj+flvUZUZeQ4IODBGDoYivoReREzugaAJKuX637dP0/DbnMGwuWyTTNlBYX0ItL3E" - "q2ptUmYZi9+ANLt9r2+nrqmORKD1/W9Xi3hirisEumQOz+qRv5hUL/H1bg7tG0znKbHCy" - "Zs16u6TgmiQH5rLW2Ltslhf6kjO1bjOJ4PTfu1PwDgeR0BsF6BBCBQIThee+P78QvAQNS" - "X17mD/tfXYaMBejAAhWWahqoiB5q8dmYQ9rc+AF7Trmn2BLC7vy4XQ0ADpHZmJRQPznVO" - "0YcABJRnBwBg+Tofm3a//2q7zYREIAAyAQRQQKqAJ/ksH4CPC4wJy9uma2eA2+syjtsVn" - "LicKzDTRYaqMgi/AQyHQNSPY0uyb7vdHVEcezDQBhAHJXLPqLOZxN8+CLJVehmapoUX2u" - "54okzsIXACucAOYyunov62AUDiN0IQd69+dyAf7PfdsLlRGAGwXekowIgySRzoMzZzcAj" - "gpxIs9Ti+TsTghLMvV1Lfbvt+vbTR9ZAJtlWoXxSIwaxuohCUt8Pp3LTd+XHt01KF9XZL" - "iRhXkSwKCzYg7X2NwGYYJsRvCHU6nndNO3SH4TauV9v3OK7rUKHnUJaiTxRl4XODwD8mC" - "Gptn0Q8j1e4oOmmfi0iZY/naRuWaIyiNI1bxDljs/7M4Hcxlta9fzTd/qubrrdYpNZ2GL" - "ZxgJboFkmFVhGLLPE/6ubPp5nNTphOAGj/QHavtZ292t3KLouiQocqbXhRKOlr+/9hoA0" - "og/d+dzi0/+2b7nTr60vXbtZhJkQZx2GaLsNMxZ8ozk5gphN/M4i79nBo/uwHdJPn1Db7" - "c40aUgoDRVdTmhn3awbsXxOs4PZfc2i+vrrTNCEe+/0JnTmkoZOiJcT2co4i5z9hnHu6Z" - "bxoT7sWAM3mfp9O7Vd7rnUV6E8ap2lk/MdmJzD2eyRohKrf4+DmON2ej6HZ31epnnqpLg" - "ZV8dmFMw6fB0vww0Gs903ToJaviOifdnrXS6SxhgjjxNEF9BH6VlUVMKqf+STqPTLpeHr" - "0l2HYHaYeHohVZiOIYUYjhjHfx0cLAHI96Qrzi4BXeYxiRi94PjeH4/k8xshgO8u0HYoI" - "EIDvQgzEPOJIaGAlSSQQye54nzbH3Wb3wFSJ9SJAi0XAZ33NwXUXC5dJFIRHvZo7n0Z3J" - "oDNaYef0zVd2bFZJjDzEmhByWfQ8bi/gDDpuz7NCa4RidhivT90w7B51tfXpV+F2CVEqd" - "eamC+gj5cYznSYawCYwSPvEIbP3ArqXXdeXze3MUUNBJbSAGHgGuOZ7maazAfAoXnnaP8" - "yN9kdj8fhjPY8TNt6FWchDTbsVB4s196jANI3XwNQPPXM9LSLmZ/Ae0f8nuGC2lhPK5md" - "++zbh76B8V0Wmaz0aOB7epHy5XA4b3ZIgt1puvYYrCkaQZyhCrjZ1ehw+B//An2skMYLh" - "GDCXB3b43Q6dhSL+7NHQ0YZYW3yyVfgyUwoOI1WABje3IkkBRMHRPmmPWxupyM4nF/jek" - "mrp8pSSSqap++aSADA1ZuTtsLTewPgKmfadx2q8YwNZVwhDzJVZnbGfEcDOB8A/Y1wDAV" - "iRxtHVLF321EiTJf3u0b+osLgglyTximcUQr6NJ2ZvwDAxwa9ejg8l7wcDsOAZLptwzgr" - "LUXLdOC5nF5yPi6giFAYsbTwbwQHcRCejFCHA/lwwoZFZRBjvZlbGJ4mGylj8E27giJDo" - "SQCsvJyR702xwGz8X5dp7qSMuy7lGcmhBrB13XxC8Asw7zIueBJ/brvEINHvzRLeSmS3C" - "SfTgHDwaXKIOd5c4/RoYzrRHiOtbpOm8391dNuhXW3rECBzwC+qWQS+IAZABSBE+VoJzV" - "6P+e5Wl9u9wlZRJtNjEXTLq1INwHdhvxZH9GkcFI8HFqAsWDLhYw5k0W8Hl8Y0fUSFxBs" - "9CquLGFKQBfcDODPrQGPnPpRlADAiZEMCVb1/r0lAkjD0kq9xSJnmj/7NoEiYUxAElOOA" - "SMoFgwAUhbKpnmANhTTFSXD+x6jEjJm+CaUXIdfJhFuN3RLy3GbcBcqYjJPKH8QwGWdod" - "nbEgqOMQD6xpXQJ/fjelXlgKU9vghk4S0KwZIC15YSvXjZ15awslAHzP00008iUEE7oC4" - "r7nKHerJAl18gGRGPAMwzez2GVpmFFhEAAKOe5CN6ZL6v0znPpVcluBMyj2ZDHhWLhciT" - "Ctq4UKb9uIIfV3ChqzvJpxvpWBIeAOheSXQ8ZEEig2DhyjyqSqVoJ9j2W0y2knLW16dCd" - "6EjyQ0a/E23IDDwowJ5IFJsMzJaRAEoxOFy1S+tXDAAcMdlxoP4w7UtnABQe0nhUa1HES" - "5kVennooC/WWEpANRLK4mYjplkcy/ViU+n627I8gjXIJ9L5APiCDYiqFD7IIYLWKoKySj" - "lUXleNM9TzcSfdxRGqlKijGALtTVJA7bgi0RVRaByyhjqP1S73BxPyjoeM47LPRqvVInU" - "cvGoCit3GRpZ5VC0XZ1zpg6pb1AqLAhDD8L/AcHH1p8sEFAHAAAAAElFTkSuQmCC\r\n" + "AAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAABmelRY" + "dFJhdyBwcm9maWxlIHR5cGUgZXhpZgAAeNptitsJgEAMBP9ThSWsZy6PcvKhcB1YvjEni" + "ODAwjAs7ec4aCmkEXc1cREk7OwtUgyTFRA3BU+vFPjS7gUI/p46Q0u2fP/1B7oA1Scbwk" + "nkf9gAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACfUExURQwMDDw8PFBQUAICAhQUFAcHBxs" + "bGxEREQkJCTk5OTU1NSAgIFRUVB8fH0xMTCUlJVtbW0pKSikpKS8vL0BAQEZGRjMzM2Bg" + "YL6+vsDAwLS0tF1dXXJycrGxsWVlZWhoaKenp29vb6urq8TExHp6epSUlLu7u66urqOjo" + "5ycnH9/f4CAgJOTk5qamo6OjoWFhYiIiHd3d8nJyc/Pz9LS0ojXP1QAAAihSURBVFjDZV" + "eHdqM6EBUYEEh0EM3gCu41+/7/294dCSfZsxOHeM8yV3f6iGVGYohNEtJPGEjPiSLpMTz" + "zokg8DmGOCOm/P0I6MTPaBGDPCGEYV3kEzchjzPOSPIkk8BzuM8fSCOFfALER+6MdpnaV" + "55FMoOP7UliioK8QzpiT0Qv0Fl4lDJvFPwChETuHFjhw7vhRVcGAXDqcfhhnRaZUWeJTW" + "pYVCBEYAJihtCsUpIhyq6win3ueDCoRBIknJRwACtz3AJhDYBhESsmyEjhaKv0MRJIIFR" + "d4XyYqC1RWwQFeBF2CcApCmEFI2KwHTRIrsMq8UnYcRUkehKtlaGeq8BjowKHEQf7oEgH" + "JcKRWpSeZpTIrs5dKlGX9fF7GfrtdWqDAuce1IyOtLbWyRKRYIIIPBo63gswO07q20/p6" + "2txvj+flvUZUZeQ4IODBGDoYivoReREzugaAJKuX637dP0/DbnMGwuWyTTNlBYX0ItL3E" + "q2ptUmYZi9+ANLt9r2+nrqmORKD1/W9Xi3hirisEumQOz+qRv5hUL/H1bg7tG0znKbHCy" + "Zs16u6TgmiQH5rLW2Ltslhf6kjO1bjOJ4PTfu1PwDgeR0BsF6BBCBQIThee+P78QvAQNS" + "X17mD/tfXYaMBejAAhWWahqoiB5q8dmYQ9rc+AF7Trmn2BLC7vy4XQ0ADpHZmJRQPznVO" + "0YcABJRnBwBg+Tofm3a//2q7zYREIAAyAQRQQKqAJ/ksH4CPC4wJy9uma2eA2+syjtsVn" + "LicKzDTRYaqMgi/AQyHQNSPY0uyb7vdHVEcezDQBhAHJXLPqLOZxN8+CLJVehmapoUX2u" + "54okzsIXACucAOYyunov62AUDiN0IQd69+dyAf7PfdsLlRGAGwXekowIgySRzoMzZzcAj" + "gpxIs9Ti+TsTghLMvV1Lfbvt+vbTR9ZAJtlWoXxSIwaxuohCUt8Pp3LTd+XHt01KF9XZL" + "iRhXkSwKCzYg7X2NwGYYJsRvCHU6nndNO3SH4TauV9v3OK7rUKHnUJaiTxRl4XODwD8mC" + "Gptn0Q8j1e4oOmmfi0iZY/naRuWaIyiNI1bxDljs/7M4Hcxlta9fzTd/qubrrdYpNZ2GL" + "ZxgJboFkmFVhGLLPE/6ubPp5nNTphOAGj/QHavtZ292t3KLouiQocqbXhRKOlr+/9hoA0" + "og/d+dzi0/+2b7nTr60vXbtZhJkQZx2GaLsNMxZ8ozk5gphN/M4i79nBo/uwHdJPn1Db7" + "c40aUgoDRVdTmhn3awbsXxOs4PZfc2i+vrrTNCEe+/0JnTmkoZOiJcT2co4i5z9hnHu6Z" + "bxoT7sWAM3mfp9O7Vd7rnUV6E8ap2lk/MdmJzD2eyRohKrf4+DmON2ej6HZ31epnnqpLg" + "ZV8dmFMw6fB0vww0Gs903ToJaviOifdnrXS6SxhgjjxNEF9BH6VlUVMKqf+STqPTLpeHr" + "0l2HYHaYeHohVZiOIYUYjhjHfx0cLAHI96Qrzi4BXeYxiRi94PjeH4/k8xshgO8u0HYoI" + "EIDvQgzEPOJIaGAlSSQQye54nzbH3Wb3wFSJ9SJAi0XAZ33NwXUXC5dJFIRHvZo7n0Z3J" + "oDNaYef0zVd2bFZJjDzEmhByWfQ8bi/gDDpuz7NCa4RidhivT90w7B51tfXpV+F2CVEqd" + "eamC+gj5cYznSYawCYwSPvEIbP3ArqXXdeXze3MUUNBJbSAGHgGuOZ7maazAfAoXnnaP8" + "yN9kdj8fhjPY8TNt6FWchDTbsVB4s196jANI3XwNQPPXM9LSLmZ/Ae0f8nuGC2lhPK5md" + "++zbh76B8V0Wmaz0aOB7epHy5XA4b3ZIgt1puvYYrCkaQZyhCrjZ1ehw+B//An2skMYLh" + "GDCXB3b43Q6dhSL+7NHQ0YZYW3yyVfgyUwoOI1WABje3IkkBRMHRPmmPWxupyM4nF/jek" + "mrp8pSSSqap++aSADA1ZuTtsLTewPgKmfadx2q8YwNZVwhDzJVZnbGfEcDOB8A/Y1wDAV" + "iRxtHVLF321EiTJf3u0b+osLgglyTximcUQr6NJ2ZvwDAxwa9ejg8l7wcDsOAZLptwzgr" + "LUXLdOC5nF5yPi6giFAYsbTwbwQHcRCejFCHA/lwwoZFZRBjvZlbGJ4mGylj8E27giJDo" + "SQCsvJyR702xwGz8X5dp7qSMuy7lGcmhBrB13XxC8Asw7zIueBJ/brvEINHvzRLeSmS3C" + "SfTgHDwaXKIOd5c4/RoYzrRHiOtbpOm8391dNuhXW3rECBzwC+qWQS+IAZABSBE+VoJzV" + "6P+e5Wl9u9wlZRJtNjEXTLq1INwHdhvxZH9GkcFI8HFqAsWDLhYw5k0W8Hl8Y0fUSFxBs" + "9CquLGFKQBfcDODPrQGPnPpRlADAiZEMCVb1/r0lAkjD0kq9xSJnmj/7NoEiYUxAElOOA" + "SMoFgwAUhbKpnmANhTTFSXD+x6jEjJm+CaUXIdfJhFuN3RLy3GbcBcqYjJPKH8QwGWdod" + "nbEgqOMQD6xpXQJ/fjelXlgKU9vghk4S0KwZIC15YSvXjZ15awslAHzP00008iUEE7oC4" + "r7nKHerJAl18gGRGPAMwzez2GVpmFFhEAAKOe5CN6ZL6v0znPpVcluBMyj2ZDHhWLhciT" + "Ctq4UKb9uIIfV3ChqzvJpxvpWBIeAOheSXQ8ZEEig2DhyjyqSqVoJ9j2W0y2knLW16dCd" + "6EjyQ0a/E23IDDwowJ5IFJsMzJaRAEoxOFy1S+tXDAAcMdlxoP4w7UtnABQe0nhUa1HES" + "5kVennooC/WWEpANRLK4mYjplkcy/ViU+n627I8gjXIJ9L5APiCDYiqFD7IIYLWKoKySj" + "lUXleNM9TzcSfdxRGqlKijGALtTVJA7bgi0RVRaByyhjqP1S73BxPyjoeM47LPRqvVInU" + "cvGoCit3GRpZ5VC0XZ1zpg6pb1AqLAhDD8L/AcHH1p8sEFAHAAAAAElFTkSuQmCC\r\n" "Connection: keep-alive\r\n" "Content-Length: 0\r\n" "Upgrade-Insecure-Requests: 1\r\n" "Pragma: no-cache\r\n" "Cache-Control: no-cache\r\n" "X-Forwarded-For: 192.0.2.0, 198.51.100.0, 203.0.113.0\r\n" - "\r\n" -); - + "\r\n"); nxt_int_t -nxt_http_parse_test(nxt_thread_t *thr) -{ - nxt_mp_t *mp_temp; +nxt_http_parse_test(nxt_thread_t *thr) { + nxt_mp_t *mp_temp; nxt_int_t rc; nxt_uint_t i, colls, lvl_colls; nxt_lvlhsh_t hash; nxt_http_request_parse_t rp; - nxt_http_parse_test_case_t *test; + nxt_http_parse_test_case_t *test; nxt_thread_time_update(thr); - rc = nxt_http_fields_hash(&nxt_http_test_fields_hash, - nxt_http_test_fields, - nxt_nitems(nxt_http_test_fields)); + rc = nxt_http_fields_hash(&nxt_http_test_fields_hash, nxt_http_test_fields, + nxt_nitems(nxt_http_test_fields)); if (rc != NXT_OK) { return NXT_ERROR; } @@ -565,17 +347,17 @@ nxt_http_parse_test(nxt_thread_t *thr) rc = nxt_http_parse_test_run(&rp, &test->request); if (rc != test->result) { - nxt_log_alert(thr->log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - result: %i (expected: %i)", - &test->request, rc, test->result); + nxt_log_alert(thr->log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - result: %i (expected: %i)", + &test->request, rc, test->result); return NXT_ERROR; } if (test->handler != NULL && test->handler(&rp, &test->data, &test->request, thr->log) - != NXT_OK) - { + != NXT_OK) { return NXT_ERROR; } @@ -586,58 +368,50 @@ nxt_http_parse_test(nxt_thread_t *thr) nxt_memzero(&hash, sizeof(nxt_lvlhsh_t)); - colls = nxt_http_fields_hash_collisions(&hash, - nxt_http_test_bench_fields, - nxt_nitems(nxt_http_test_bench_fields), - 0); + colls = nxt_http_fields_hash_collisions(&hash, nxt_http_test_bench_fields, + nxt_nitems(nxt_http_test_bench_fields), 0); nxt_memzero(&hash, sizeof(nxt_lvlhsh_t)); lvl_colls = nxt_http_fields_hash_collisions(&hash, - nxt_http_test_bench_fields, - nxt_nitems(nxt_http_test_bench_fields), - 1); + nxt_http_test_bench_fields, nxt_nitems(nxt_http_test_bench_fields), 1); nxt_log_error(NXT_LOG_NOTICE, thr->log, - "http parse test hash collisions %ui out of %uz, level: %ui", - colls, nxt_nitems(nxt_http_test_bench_fields), lvl_colls); + "http parse test hash collisions %ui out of %uz, level: %ui", colls, + nxt_nitems(nxt_http_test_bench_fields), lvl_colls); nxt_memzero(&hash, sizeof(nxt_lvlhsh_t)); rc = nxt_http_fields_hash(&hash, nxt_http_test_bench_fields, - nxt_nitems(nxt_http_test_bench_fields)); + nxt_nitems(nxt_http_test_bench_fields)); if (rc != NXT_OK) { return NXT_ERROR; } - if (nxt_http_parse_test_bench(thr, &nxt_http_test_simple_request, - &hash, "simple", 1000000) - != NXT_OK) - { + if (nxt_http_parse_test_bench(thr, &nxt_http_test_simple_request, &hash, + "simple", 1000000) + != NXT_OK) { return NXT_ERROR; } - if (nxt_http_parse_test_bench(thr, &nxt_http_test_big_request, - &hash, "big", 100000) - != NXT_OK) - { + if (nxt_http_parse_test_bench(thr, &nxt_http_test_big_request, &hash, "big", + 100000) + != NXT_OK) { return NXT_ERROR; } return NXT_OK; } - static nxt_int_t -nxt_http_parse_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request) -{ - nxt_int_t rc; - nxt_buf_mem_t buf; +nxt_http_parse_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request) { + nxt_int_t rc; + nxt_buf_mem_t buf; buf.start = request->start; - buf.end = request->start + request->length; + buf.end = request->start + request->length; - buf.pos = buf.start; + buf.pos = buf.start; buf.free = buf.pos + 1; do { @@ -648,23 +422,21 @@ nxt_http_parse_test_run(nxt_http_request_parse_t *rp, nxt_str_t *request) return rc; } - static nxt_int_t nxt_http_parse_test_bench(nxt_thread_t *thr, nxt_str_t *request, - nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n) -{ - nxt_mp_t *mp; - nxt_nsec_t start, end; - nxt_uint_t i; - nxt_buf_mem_t buf; - nxt_http_request_parse_t rp; + nxt_lvlhsh_t *hash, const char *name, nxt_uint_t n) { + nxt_mp_t *mp; + nxt_nsec_t start, end; + nxt_uint_t i; + nxt_buf_mem_t buf; + nxt_http_request_parse_t rp; nxt_log_error(NXT_LOG_NOTICE, thr->log, - "http parse %s request bench started: %uz bytes, %ui runs", - name, request->length, n); + "http parse %s request bench started: %uz bytes, %ui runs", name, + request->length, n); buf.start = request->start; - buf.end = request->start + request->length; + buf.end = request->start + request->length; nxt_thread_time_update(thr); start = nxt_thread_monotonic_time(thr); @@ -681,20 +453,23 @@ nxt_http_parse_test_bench(nxt_thread_t *thr, nxt_str_t *request, return NXT_ERROR; } - buf.pos = buf.start; + buf.pos = buf.start; buf.free = buf.end; if (nxt_slow_path(nxt_http_parse_request(&rp, &buf) != NXT_DONE)) { - nxt_log_alert(thr->log, "http parse %s request bench failed " - "while parsing", name); + nxt_log_alert(thr->log, + "http parse %s request bench failed " + "while parsing", + name); return NXT_ERROR; } - if (nxt_slow_path(nxt_http_fields_process(rp.fields, hash, NULL) - != NXT_OK)) - { - nxt_log_alert(thr->log, "http parse %s request bench failed " - "while fields processing", name); + if (nxt_slow_path( + nxt_http_fields_process(rp.fields, hash, NULL) != NXT_OK)) { + nxt_log_alert(thr->log, + "http parse %s request bench failed " + "while fields processing", + name); return NXT_ERROR; } @@ -705,76 +480,76 @@ nxt_http_parse_test_bench(nxt_thread_t *thr, nxt_str_t *request, end = nxt_thread_monotonic_time(thr); nxt_log_error(NXT_LOG_NOTICE, thr->log, - "http parse %s request bench: %0.3fs", - name, (end - start) / 1000000000.0); + "http parse %s request bench: %0.3fs", name, + (end - start) / 1000000000.0); return NXT_OK; } - static nxt_int_t nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp, - nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log) -{ - nxt_str_t str; + nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log) { + nxt_str_t str; - nxt_http_parse_test_request_line_t *test = &data->request_line; + nxt_http_parse_test_request_line_t *test = &data->request_line; if (rp->method.start != test->method.start - && !nxt_strstr_eq(&rp->method, &test->method)) - { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - method: \"%V\" (expected: \"%V\")", - request, &rp->method, &test->method); + && !nxt_strstr_eq(&rp->method, &test->method)) { + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - method: \"%V\" (expected: \"%V\")", + request, &rp->method, &test->method); return NXT_ERROR; } str.length = rp->target_end - rp->target_start; - str.start = rp->target_start; + str.start = rp->target_start; if (str.start != test->target.start - && !nxt_strstr_eq(&str, &test->target)) - { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - target: \"%V\" (expected: \"%V\")", - request, &str, &test->target); + && !nxt_strstr_eq(&str, &test->target)) { + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - target: \"%V\" (expected: \"%V\")", + request, &str, &test->target); return NXT_ERROR; } if (rp->args.start != test->args.start - && !nxt_strstr_eq(&rp->args, &test->args)) - { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - args: \"%V\" (expected: \"%V\")", - request, &rp->args, &test->args); + && !nxt_strstr_eq(&rp->args, &test->args)) { + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - args: \"%V\" (expected: \"%V\")", + request, &rp->args, &test->args); return NXT_ERROR; } if (memcmp(rp->version.str, test->version, 8) != 0) { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - version: \"%*s\" (expected: \"%*s\")", request, - (size_t) 8, rp->version.str, - (size_t) 8, test->version); + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - version: \"%*s\" (expected: \"%*s\")", + request, (size_t) 8, rp->version.str, (size_t) 8, test->version); return NXT_ERROR; } if (rp->complex_target != test->complex_target) { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - complex_target: %d (expected: %d)", - request, rp->complex_target, test->complex_target); + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - complex_target: %d (expected: %d)", + request, rp->complex_target, test->complex_target); return NXT_ERROR; } if (rp->quoted_target != test->quoted_target) { - nxt_log_alert(log, "http parse test case failed:\n" - " - request:\n\"%V\"\n" - " - quoted_target: %d (expected: %d)", - request, rp->quoted_target, test->quoted_target); + nxt_log_alert(log, + "http parse test case failed:\n" + " - request:\n\"%V\"\n" + " - quoted_target: %d (expected: %d)", + request, rp->quoted_target, test->quoted_target); return NXT_ERROR; } @@ -791,29 +566,27 @@ nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp, return NXT_OK; } - static nxt_int_t nxt_http_parse_test_fields(nxt_http_request_parse_t *rp, - nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log) -{ - nxt_int_t rc; + nxt_http_parse_test_data_t *data, nxt_str_t *request, nxt_log_t *log) { + nxt_int_t rc; rc = nxt_http_fields_process(rp->fields, &nxt_http_test_fields_hash, NULL); if (rc != data->fields.result) { - nxt_log_alert(log, "http parse test hash failed:\n" - " - request:\n\"%V\"\n" - " - result: %i (expected: %i)", - request, rc, data->fields.result); + nxt_log_alert(log, + "http parse test hash failed:\n" + " - request:\n\"%V\"\n" + " - result: %i (expected: %i)", + request, rc, data->fields.result); return NXT_ERROR; } return NXT_OK; } - static nxt_int_t -nxt_http_test_header_return(void *ctx, nxt_http_field_t *field, uintptr_t data) -{ +nxt_http_test_header_return(void *ctx, nxt_http_field_t *field, + uintptr_t data) { return data; } diff --git a/src/test/nxt_lvlhsh_test.c b/src/test/nxt_lvlhsh_test.c index baa6d0e1e..de351cd62 100644 --- a/src/test/nxt_lvlhsh_test.c +++ b/src/test/nxt_lvlhsh_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,10 +6,8 @@ #include #include "nxt_tests.h" - static nxt_int_t -nxt_lvlhsh_test_key_test(nxt_lvlhsh_query_t *lhq, void *data) -{ +nxt_lvlhsh_test_key_test(nxt_lvlhsh_query_t *lhq, void *data) { if (*(uintptr_t *) lhq->key.start == (uintptr_t) data) { return NXT_OK; } @@ -18,137 +15,129 @@ nxt_lvlhsh_test_key_test(nxt_lvlhsh_query_t *lhq, void *data) return NXT_DECLINED; } - -static const nxt_lvlhsh_proto_t malloc_proto nxt_aligned(64) = { - //NXT_LVLHSH_LARGE_MEMALIGN, +static const nxt_lvlhsh_proto_t malloc_proto nxt_aligned(64) = { + // NXT_LVLHSH_LARGE_MEMALIGN, NXT_LVLHSH_DEFAULT, nxt_lvlhsh_test_key_test, nxt_lvlhsh_alloc, nxt_lvlhsh_free, }; -static const nxt_lvlhsh_proto_t pool_proto nxt_aligned(64) = { +static const nxt_lvlhsh_proto_t pool_proto nxt_aligned(64) = { NXT_LVLHSH_LARGE_SLAB, nxt_lvlhsh_test_key_test, nxt_mp_lvlhsh_alloc, nxt_mp_lvlhsh_free, }; - static nxt_int_t nxt_lvlhsh_test_add(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, - void *pool, uintptr_t key) -{ - nxt_lvlhsh_query_t lhq; + void *pool, uintptr_t key) { + nxt_lvlhsh_query_t lhq; - lhq.key_hash = key; - lhq.replace = 0; + lhq.key_hash = key; + lhq.replace = 0; lhq.key.length = sizeof(uintptr_t); - lhq.key.start = (u_char *) &key; - lhq.value = (void *) key; - lhq.proto = proto; - lhq.pool = pool; + lhq.key.start = (u_char *) &key; + lhq.value = (void *) key; + lhq.proto = proto; + lhq.pool = pool; switch (nxt_lvlhsh_insert(lh, &lhq)) { - case NXT_OK: return NXT_OK; case NXT_DECLINED: nxt_thread_log_alert("lvlhsh test failed: " - "key %p is already in hash", key); + "key %p is already in hash", + key); /* Fall through. */ default: return NXT_ERROR; } } - static nxt_int_t nxt_lvlhsh_test_get(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, - uintptr_t key) -{ - nxt_lvlhsh_query_t lhq; + uintptr_t key) { + nxt_lvlhsh_query_t lhq; - lhq.key_hash = key; + lhq.key_hash = key; lhq.key.length = sizeof(uintptr_t); - lhq.key.start = (u_char *) &key; - lhq.proto = proto; + lhq.key.start = (u_char *) &key; + lhq.proto = proto; if (nxt_lvlhsh_find(lh, &lhq) == NXT_OK) { - if (key == (uintptr_t) lhq.value) { return NXT_OK; } } nxt_thread_log_alert("lvlhsh test failed: " - "key %p not found in hash", key); + "key %p not found in hash", + key); return NXT_ERROR; } - static nxt_int_t nxt_lvlhsh_test_delete(nxt_lvlhsh_t *lh, const nxt_lvlhsh_proto_t *proto, - void *pool, uintptr_t key) -{ - nxt_int_t ret; - nxt_lvlhsh_query_t lhq; + void *pool, uintptr_t key) { + nxt_int_t ret; + nxt_lvlhsh_query_t lhq; - lhq.key_hash = key; + lhq.key_hash = key; lhq.key.length = sizeof(uintptr_t); - lhq.key.start = (u_char *) &key; - lhq.proto = proto; - lhq.pool = pool; + lhq.key.start = (u_char *) &key; + lhq.proto = proto; + lhq.pool = pool; ret = nxt_lvlhsh_delete(lh, &lhq); if (ret != NXT_OK) { nxt_thread_log_alert("lvlhsh test failed: " - "key %p not found in hash", key); + "key %p not found in hash", + key); } return ret; } - nxt_int_t -nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) -{ - void *value; +nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) { + void *value; uint32_t key; - nxt_mp_t *mp; + nxt_mp_t *mp; nxt_nsec_t start, end; nxt_uint_t i; nxt_lvlhsh_t lh; nxt_lvlhsh_each_t lhe; - const nxt_lvlhsh_proto_t *proto; + const nxt_lvlhsh_proto_t *proto; - const size_t min_chunk_size = 32; - const size_t page_size = 1024; - const size_t page_alignment = 128; - const size_t cluster_size = 4096; + const size_t min_chunk_size = 32; + const size_t page_size = 1024; + const size_t page_alignment = 128; + const size_t cluster_size = 4096; nxt_thread_time_update(thr); start = nxt_thread_monotonic_time(thr); if (use_pool) { mp = nxt_mp_create(cluster_size, page_alignment, page_size, - min_chunk_size); + min_chunk_size); if (mp == NULL) { return NXT_ERROR; } - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh test started: %uD pool", n); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "lvlhsh test started: %uD pool", + n); proto = &pool_proto; } else { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh test started: %uD malloc", n); + "lvlhsh test started: %uD malloc", n); proto = &malloc_proto; - mp = NULL; + mp = NULL; } nxt_memzero(&lh, sizeof(nxt_lvlhsh_t)); @@ -159,7 +148,7 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) if (nxt_lvlhsh_test_add(&lh, proto, mp, key) != NXT_OK) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh add test failed at %ui", i); + "lvlhsh add test failed at %ui", i); return NXT_ERROR; } } @@ -183,7 +172,7 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) if (i != n) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh each test failed at %ui of %ui", i, n); + "lvlhsh each test failed at %ui of %ui", i, n); return NXT_ERROR; } @@ -203,13 +192,13 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) if (i != n) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh peek test failed at %ui of %ui", i, n); + "lvlhsh peek test failed at %ui of %ui", i, n); return NXT_ERROR; } if (!nxt_lvlhsh_is_empty(&lh)) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh is not empty after deletion"); + "lvlhsh is not empty after deletion"); return NXT_ERROR; } @@ -219,7 +208,7 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) if (nxt_lvlhsh_test_add(&lh, proto, mp, key) != NXT_OK) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh add test failed at %ui", i); + "lvlhsh add test failed at %ui", i); return NXT_ERROR; } } @@ -234,13 +223,13 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) if (i != n) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh retrieve test failed at %ui of %ui", i, n); + "lvlhsh retrieve test failed at %ui of %ui", i, n); return NXT_ERROR; } if (!nxt_lvlhsh_is_empty(&lh)) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "lvlhsh is not empty after retrieving"); + "lvlhsh is not empty after retrieving"); return NXT_ERROR; } @@ -257,7 +246,7 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool) end = nxt_thread_monotonic_time(thr); nxt_log_error(NXT_LOG_NOTICE, thr->log, "lvlhsh test passed: %0.3fs", - (end - start) / 1000000000.0); + (end - start) / 1000000000.0); return NXT_OK; } diff --git a/src/test/nxt_malloc_test.c b/src/test/nxt_malloc_test.c index daa609b14..877da2396 100644 --- a/src/test/nxt_malloc_test.c +++ b/src/test/nxt_malloc_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,30 +7,26 @@ #include "nxt_tests.h" -#define TIMES 1000 - +#define TIMES 1000 typedef struct { - size_t size; - size_t alignment; - nxt_bool_t tight; + size_t size; + size_t alignment; + nxt_bool_t tight; } nxt_malloc_size_t; - static nxt_malloc_size_t * nxt_malloc_run_test(nxt_thread_t *thr, nxt_malloc_size_t *last, size_t size, - nxt_uint_t times) -{ + nxt_uint_t times) { size_t a, s, alignment; uintptr_t n; nxt_uint_t i, tight; - static u_char *p[TIMES + 1]; + static u_char *p[TIMES + 1]; alignment = (size_t) -1; - tight = 0; + tight = 0; for (i = 1; i < times; i++) { - p[i] = nxt_malloc(size); if (p[i] == NULL) { return NULL; @@ -73,20 +68,18 @@ nxt_malloc_run_test(nxt_thread_t *thr, nxt_malloc_size_t *last, size_t size, last++; - last->size = size; + last->size = size; last->alignment = alignment; - last->tight = times * 9 / 10 < tight; + last->tight = times * 9 / 10 < tight; return last; } - nxt_int_t -nxt_malloc_test(nxt_thread_t *thr) -{ - size_t size; - nxt_malloc_size_t *last, *s; - static nxt_malloc_size_t sizes[100]; +nxt_malloc_test(nxt_thread_t *thr) { + size_t size; + nxt_malloc_size_t *last, *s; + static nxt_malloc_size_t sizes[100]; nxt_log_error(NXT_LOG_NOTICE, thr->log, "malloc test started"); @@ -115,8 +108,8 @@ nxt_malloc_test(nxt_thread_t *thr) for (s = &sizes[1]; s <= last; s++) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "malloc sizes: %uz-%uz alignment:%uz tight:%ui", - s[-1].size + 1, s->size, s->alignment, s->tight); + "malloc sizes: %uz-%uz alignment:%uz tight:%ui", s[-1].size + 1, + s->size, s->alignment, s->tight); } return NXT_OK; diff --git a/src/test/nxt_mem_zone_test.c b/src/test/nxt_mem_zone_test.c index faf0feee6..4842399cb 100644 --- a/src/test/nxt_mem_zone_test.c +++ b/src/test/nxt_mem_zone_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,21 +6,19 @@ #include #include "nxt_tests.h" - nxt_int_t nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, - size_t max_size) -{ - void *start, **blocks; + size_t max_size) { + void *start, **blocks; size_t total, zone_size; uint32_t size; nxt_uint_t i, n; - nxt_mem_zone_t *zone; + nxt_mem_zone_t *zone; const size_t page_size = 4096; nxt_thread_time_update(thr); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "mem zone test started, max:%uz", max_size); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem zone test started, max:%uz", + max_size); zone_size = (max_size + 1) * nblocks; @@ -43,18 +40,17 @@ nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, size = 0; for (i = 0; i < runs; i++) { - total = 0; for (n = 0; n < nblocks; n++) { size = nxt_murmur_hash2(&size, sizeof(uint32_t)); - total += size & max_size; - blocks[n] = nxt_mem_zone_alloc(zone, size & max_size); + total += size & max_size; + blocks[n] = nxt_mem_zone_alloc(zone, size & max_size); if (blocks[n] == NULL) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "mem zone test failed: %uz", total); + "mem zone test failed: %uz", total); return NXT_ERROR; } } diff --git a/src/test/nxt_mp_test.c b/src/test/nxt_mp_test.c index 39aa939da..e32fa07f6 100644 --- a/src/test/nxt_mp_test.c +++ b/src/test/nxt_mp_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,26 +6,24 @@ #include #include "nxt_tests.h" - nxt_int_t nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, - size_t max_size) -{ - void **blocks; - size_t total; - uint32_t value, size; - nxt_mp_t *mp; - nxt_bool_t valid; - nxt_uint_t i, n; - - const size_t min_chunk_size = 16; - const size_t page_size = 128; - const size_t page_alignment = 128; - const size_t cluster_size = page_size * 8; + size_t max_size) { + void **blocks; + size_t total; + uint32_t value, size; + nxt_mp_t *mp; + nxt_bool_t valid; + nxt_uint_t i, n; + + const size_t min_chunk_size = 16; + const size_t page_size = 128; + const size_t page_alignment = 128; + const size_t cluster_size = page_size * 8; nxt_thread_time_update(thr); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "mem pool test started, max:%uz", max_size); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "mem pool test started, max:%uz", + max_size); blocks = nxt_malloc(nblocks * sizeof(void *)); if (blocks == NULL) { @@ -34,7 +31,7 @@ nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, } valid = nxt_mp_test_sizes(cluster_size, page_alignment, page_size, - min_chunk_size); + min_chunk_size); if (!valid) { return NXT_ERROR; } @@ -47,7 +44,6 @@ nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, value = 0; for (i = 0; i < runs; i++) { - total = 0; for (n = 0; n < nblocks; n++) { @@ -59,12 +55,12 @@ nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, size++; } - total += size; - blocks[n] = nxt_mp_alloc(mp, size); + total += size; + blocks[n] = nxt_mp_alloc(mp, size); if (blocks[n] == NULL) { nxt_log_error(NXT_LOG_NOTICE, thr->log, - "mem pool test failed: %uz", total); + "mem pool test failed: %uz", total); return NXT_ERROR; } } diff --git a/src/test/nxt_msec_diff_test.c b/src/test/nxt_msec_diff_test.c index 0059b2464..cc34ab0a0 100644 --- a/src/test/nxt_msec_diff_test.c +++ b/src/test/nxt_msec_diff_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,35 +7,39 @@ #include "nxt_tests.h" -static const nxt_msec_t pairs[] = { +static const nxt_msec_t pairs[] = { - 0x00000000, 0x00000001, - 0x00000000, 0x7FFFFFFF, + 0x00000000, + 0x00000001, + 0x00000000, + 0x7FFFFFFF, - 0x7FFFFFFF, 0x80000000, - 0x7FFFFFFF, 0x80000001, + 0x7FFFFFFF, + 0x80000000, + 0x7FFFFFFF, + 0x80000001, - 0x80000000, 0x80000001, - 0x80000000, 0xFFFFFFFF, + 0x80000000, + 0x80000001, + 0x80000000, + 0xFFFFFFFF, - 0xFFFFFFFF, 0x00000000, - 0xFFFFFFFF, 0x00000001, + 0xFFFFFFFF, + 0x00000000, + 0xFFFFFFFF, + 0x00000001, }; - nxt_int_t -nxt_msec_diff_test(nxt_thread_t *thr, nxt_msec_less_t less) -{ - nxt_uint_t i; +nxt_msec_diff_test(nxt_thread_t *thr, nxt_msec_less_t less) { + nxt_uint_t i; nxt_thread_time_update(thr); for (i = 0; i < nxt_nitems(pairs); i += 2) { - if (!less(pairs[i], pairs[i + 1])) { - nxt_log_alert(thr->log, - "msec diff test failed: 0x%08XM 0x%08XM", - pairs[i], pairs[i + 1]); + nxt_log_alert(thr->log, "msec diff test failed: 0x%08XM 0x%08XM", + pairs[i], pairs[i + 1]); return NXT_ERROR; } } diff --git a/src/test/nxt_rbtree1.c b/src/test/nxt_rbtree1.c index ec0248586..60af76adf 100644 --- a/src/test/nxt_rbtree1.c +++ b/src/test/nxt_rbtree1.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -15,26 +14,26 @@ */ -nxt_inline void nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, - nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node); -nxt_inline void nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, +nxt_inline void +nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, nxt_rbtree1_node_t *sentinel, + nxt_rbtree1_node_t *node); +nxt_inline void +nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node); - void -nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) -{ - nxt_rbtree1_node_t **root, *temp, *sentinel; +nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) { + nxt_rbtree1_node_t **root, *temp, *sentinel; /* a binary tree insert */ - root = (nxt_rbtree1_node_t **) &tree->root; + root = (nxt_rbtree1_node_t **) &tree->root; sentinel = tree->sentinel; if (*root == sentinel) { node->parent = NULL; - node->left = sentinel; - node->right = sentinel; + node->left = sentinel; + node->right = sentinel; nxt_rbtree1_black(node); *root = node; @@ -46,7 +45,6 @@ nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) /* re-balance tree */ while (node != *root && nxt_rbtree1_is_red(node->parent)) { - if (node->parent == node->parent->parent->left) { temp = node->parent->parent->right; @@ -92,15 +90,12 @@ nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) nxt_rbtree1_black(*root); } - void nxt_rbtree1_insert_value(nxt_rbtree1_node_t *temp, nxt_rbtree1_node_t *node, - nxt_rbtree1_node_t *sentinel) -{ - nxt_rbtree1_node_t **p; - - for ( ;; ) { + nxt_rbtree1_node_t *sentinel) { + nxt_rbtree1_node_t **p; + for (;;) { p = (node->key < temp->key) ? &temp->left : &temp->right; if (*p == sentinel) { @@ -110,22 +105,19 @@ nxt_rbtree1_insert_value(nxt_rbtree1_node_t *temp, nxt_rbtree1_node_t *node, temp = *p; } - *p = node; + *p = node; node->parent = temp; - node->left = sentinel; - node->right = sentinel; + node->left = sentinel; + node->right = sentinel; nxt_rbtree1_red(node); } - void nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *temp, - nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) -{ - nxt_rbtree1_node_t **p; - - for ( ;; ) { + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) { + nxt_rbtree1_node_t **p; + for (;;) { /* * Timer values * 1) are spread in small range, usually several minutes, @@ -136,7 +128,8 @@ nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *temp, /* node->key < temp->key */ p = ((nxt_rbtree1_key_int_t) (node->key - temp->key) < 0) - ? &temp->left : &temp->right; + ? &temp->left + : &temp->right; if (*p == sentinel) { break; @@ -145,31 +138,29 @@ nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *temp, temp = *p; } - *p = node; + *p = node; node->parent = temp; - node->left = sentinel; - node->right = sentinel; + node->left = sentinel; + node->right = sentinel; nxt_rbtree1_red(node); } - void -nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) -{ - nxt_uint_t red; - nxt_rbtree1_node_t **root, *sentinel, *subst, *temp, *w; +nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) { + nxt_uint_t red; + nxt_rbtree1_node_t **root, *sentinel, *subst, *temp, *w; /* a binary tree delete */ - root = (nxt_rbtree1_node_t **) &tree->root; + root = (nxt_rbtree1_node_t **) &tree->root; sentinel = tree->sentinel; if (node->left == sentinel) { - temp = node->right; + temp = node->right; subst = node; } else if (node->right == sentinel) { - temp = node->left; + temp = node->left; subst = node; } else { @@ -187,10 +178,10 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) nxt_rbtree1_black(temp); /* DEBUG stuff */ - node->left = NULL; - node->right = NULL; + node->left = NULL; + node->right = NULL; node->parent = NULL; - node->key = 0; + node->key = 0; return; } @@ -205,11 +196,9 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) } if (subst == node) { - temp->parent = subst->parent; } else { - if (subst->parent == node) { temp->parent = subst; @@ -217,8 +206,8 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) temp->parent = subst->parent; } - subst->left = node->left; - subst->right = node->right; + subst->left = node->left; + subst->right = node->right; subst->parent = node->parent; nxt_rbtree1_copy_color(subst, node); @@ -243,10 +232,10 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) } /* DEBUG stuff */ - node->left = NULL; - node->right = NULL; + node->left = NULL; + node->right = NULL; node->parent = NULL; - node->key = 0; + node->key = 0; if (red) { return; @@ -255,7 +244,6 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) /* a delete fixup */ while (temp != *root && nxt_rbtree1_is_black(temp)) { - if (temp == temp->parent->left) { w = temp->parent->right; @@ -266,8 +254,8 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) w = temp->parent->right; } - if (nxt_rbtree1_is_black(w->left) && nxt_rbtree1_is_black(w->right)) - { + if (nxt_rbtree1_is_black(w->left) + && nxt_rbtree1_is_black(w->right)) { nxt_rbtree1_red(w); temp = temp->parent; @@ -296,8 +284,8 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) w = temp->parent->left; } - if (nxt_rbtree1_is_black(w->left) && nxt_rbtree1_is_black(w->right)) - { + if (nxt_rbtree1_is_black(w->left) + && nxt_rbtree1_is_black(w->right)) { nxt_rbtree1_red(w); temp = temp->parent; @@ -321,14 +309,12 @@ nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) nxt_rbtree1_black(temp); } - nxt_inline void nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, nxt_rbtree1_node_t *sentinel, - nxt_rbtree1_node_t *node) -{ - nxt_rbtree1_node_t *temp; + nxt_rbtree1_node_t *node) { + nxt_rbtree1_node_t *temp; - temp = node->right; + temp = node->right; node->right = temp->left; if (temp->left != sentinel) { @@ -347,18 +333,16 @@ nxt_rbtree1_left_rotate(nxt_rbtree1_node_t **root, nxt_rbtree1_node_t *sentinel, node->parent->right = temp; } - temp->left = node; + temp->left = node; node->parent = temp; } - nxt_inline void nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, - nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node) -{ - nxt_rbtree1_node_t *temp; + nxt_rbtree1_node_t *sentinel, nxt_rbtree1_node_t *node) { + nxt_rbtree1_node_t *temp; - temp = node->left; + temp = node->left; node->left = temp->right; if (temp->right != sentinel) { @@ -377,6 +361,6 @@ nxt_rbtree1_right_rotate(nxt_rbtree1_node_t **root, node->parent->left = temp; } - temp->right = node; + temp->right = node; node->parent = temp; } diff --git a/src/test/nxt_rbtree1.h b/src/test/nxt_rbtree1.h index d6230ab0c..5535d4535 100644 --- a/src/test/nxt_rbtree1.h +++ b/src/test/nxt_rbtree1.h @@ -1,70 +1,68 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. */ -typedef nxt_uint_t nxt_rbtree1_key_t; -typedef nxt_int_t nxt_rbtree1_key_int_t; +typedef nxt_uint_t nxt_rbtree1_key_t; +typedef nxt_int_t nxt_rbtree1_key_int_t; -typedef struct nxt_rbtree1_node_s nxt_rbtree1_node_t; +typedef struct nxt_rbtree1_node_s nxt_rbtree1_node_t; struct nxt_rbtree1_node_s { - nxt_rbtree1_key_t key; - nxt_rbtree1_node_t *left; - nxt_rbtree1_node_t *right; - nxt_rbtree1_node_t *parent; - u_char color; - u_char data; + nxt_rbtree1_key_t key; + nxt_rbtree1_node_t *left; + nxt_rbtree1_node_t *right; + nxt_rbtree1_node_t *parent; + u_char color; + u_char data; }; -typedef struct nxt_rbtree1_s nxt_rbtree1_t; +typedef struct nxt_rbtree1_s nxt_rbtree1_t; -typedef void (*nxt_rbtree1_insert_pt) (nxt_rbtree1_node_t *root, +typedef void (*nxt_rbtree1_insert_pt)(nxt_rbtree1_node_t *root, nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); struct nxt_rbtree1_s { - nxt_rbtree1_node_t *root; - nxt_rbtree1_node_t *sentinel; - nxt_rbtree1_insert_pt insert; + nxt_rbtree1_node_t *root; + nxt_rbtree1_node_t *sentinel; + nxt_rbtree1_insert_pt insert; }; - -#define nxt_rbtree1_init(tree, s, i) \ - nxt_rbtree1_sentinel_init(s); \ - (tree)->root = s; \ - (tree)->sentinel = s; \ - (tree)->insert = i - - -NXT_EXPORT void nxt_rbtree1_insert(nxt_rbtree1_t *tree, - nxt_rbtree1_node_t *node); -NXT_EXPORT void nxt_rbtree1_delete(nxt_rbtree1_t *tree, - nxt_rbtree1_node_t *node); -NXT_EXPORT void nxt_rbtree1_insert_value(nxt_rbtree1_node_t *root, - nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); -NXT_EXPORT void nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *root, +#define nxt_rbtree1_init(tree, s, i) \ + nxt_rbtree1_sentinel_init(s); \ + (tree)->root = s; \ + (tree)->sentinel = s; \ + (tree)->insert = i + + +NXT_EXPORT void +nxt_rbtree1_insert(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node); +NXT_EXPORT void +nxt_rbtree1_delete(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node); +NXT_EXPORT void +nxt_rbtree1_insert_value(nxt_rbtree1_node_t *root, nxt_rbtree1_node_t *node, + nxt_rbtree1_node_t *sentinel); +NXT_EXPORT void +nxt_rbtree1_insert_timer_value(nxt_rbtree1_node_t *root, nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); -#define nxt_rbtree1_red(node) ((node)->color = 1) -#define nxt_rbtree1_black(node) ((node)->color = 0) -#define nxt_rbtree1_is_red(node) ((node)->color) -#define nxt_rbtree1_is_black(node) (!nxt_rbtree1_is_red(node)) -#define nxt_rbtree1_copy_color(n1, n2) (n1->color = n2->color) +#define nxt_rbtree1_red(node) ((node)->color = 1) +#define nxt_rbtree1_black(node) ((node)->color = 0) +#define nxt_rbtree1_is_red(node) ((node)->color) +#define nxt_rbtree1_is_black(node) (!nxt_rbtree1_is_red(node)) +#define nxt_rbtree1_copy_color(n1, n2) (n1->color = n2->color) /* a sentinel must be black */ -#define nxt_rbtree1_sentinel_init(node) nxt_rbtree1_black(node) - +#define nxt_rbtree1_sentinel_init(node) nxt_rbtree1_black(node) nxt_inline nxt_rbtree1_node_t * -nxt_rbtree1_min(nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) -{ +nxt_rbtree1_min(nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) { while (node->left != sentinel) { node = node->left; } diff --git a/src/test/nxt_rbtree1_test.c b/src/test/nxt_rbtree1_test.c index 1f23998c8..6694e8f6d 100644 --- a/src/test/nxt_rbtree1_test.c +++ b/src/test/nxt_rbtree1_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -9,24 +8,20 @@ #include "nxt_rbtree1.h" -#define nxt_rbtree1_is_empty(tree) \ - (((tree)->root) == (tree)->sentinel) - +#define nxt_rbtree1_is_empty(tree) (((tree)->root) == (tree)->sentinel) -#define nxt_rbtree1_is_there_successor(tree, node) \ - ((node) != (tree)->sentinel) +#define nxt_rbtree1_is_there_successor(tree, node) ((node) != (tree)->sentinel) nxt_inline nxt_rbtree1_node_t * -nxt_rbtree1_node_successor(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) -{ - nxt_rbtree1_node_t *parent; +nxt_rbtree1_node_successor(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) { + nxt_rbtree1_node_t *parent; if (node->right != tree->sentinel) { return nxt_rbtree1_min(node->right, tree->sentinel); } - for ( ;; ) { + for (;;) { parent = node->parent; if (parent == NULL) { @@ -41,25 +36,23 @@ nxt_rbtree1_node_successor(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) } } - -static void nxt_rbtree1_test_insert_value(nxt_rbtree1_node_t *temp, +static void +nxt_rbtree1_test_insert_value(nxt_rbtree1_node_t *temp, nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel); -static nxt_int_t nxt_rbtree1_test_compare(nxt_rbtree1_node_t *node1, - nxt_rbtree1_node_t *node2); -static int nxt_cdecl nxt_rbtree1_test_sort_cmp(const void *one, - const void *two); -static nxt_rbtree1_node_t *nxt_rbtree1_test_find(nxt_rbtree1_t *tree, - nxt_rbtree1_node_t *node); - +static nxt_int_t +nxt_rbtree1_test_compare(nxt_rbtree1_node_t *node1, nxt_rbtree1_node_t *node2); +static int nxt_cdecl +nxt_rbtree1_test_sort_cmp(const void *one, const void *two); +static nxt_rbtree1_node_t * +nxt_rbtree1_test_find(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node); nxt_int_t -nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) -{ +nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) { uint32_t key, *keys; nxt_uint_t i; nxt_nsec_t start, end; nxt_rbtree1_t tree; - nxt_rbtree1_node_t *node, *nodes, sentinel; + nxt_rbtree1_node_t *node, *nodes, sentinel; nxt_thread_time_update(thr); @@ -83,7 +76,7 @@ nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) for (i = 0; i < n; i++) { key = nxt_murmur_hash2(&key, sizeof(uint32_t)); - keys[i] = key; + keys[i] = key; nodes[i].key = key; } @@ -99,19 +92,18 @@ nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) for (i = 0; i < n; i++) { if (nxt_rbtree1_test_find(&tree, &nodes[i]) != &nodes[i]) { nxt_log_alert(thr->log, "rbtree1 test failed: %08XD not found", - nodes[i].key); + nodes[i].key); goto fail; } } - i = 0; + i = 0; node = nxt_rbtree1_min(tree.root, tree.sentinel); while (nxt_rbtree1_is_there_successor(&tree, node)) { - if (keys[i] != node->key) { - nxt_log_alert(thr->log, "rbtree1 test failed: %i: %08XD %08XD", - i, keys[i], node->key); + nxt_log_alert(thr->log, "rbtree1 test failed: %i: %08XD %08XD", i, + keys[i], node->key); goto fail; } @@ -141,7 +133,7 @@ nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) nxt_free(nodes); nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree1 test passed %0.3fs", - (end - start) / 1000000000.0); + (end - start) / 1000000000.0); return NXT_OK; @@ -153,14 +145,12 @@ nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n) return NXT_ERROR; } - static void nxt_rbtree1_test_insert_value(nxt_rbtree1_node_t *temp, - nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) -{ - nxt_rbtree1_node_t **p; + nxt_rbtree1_node_t *node, nxt_rbtree1_node_t *sentinel) { + nxt_rbtree1_node_t **p; - for ( ;; ) { + for (;;) { nxt_prefetch(temp->left); nxt_prefetch(temp->right); @@ -173,14 +163,13 @@ nxt_rbtree1_test_insert_value(nxt_rbtree1_node_t *temp, temp = *p; } - *p = node; + *p = node; node->parent = temp; - node->left = sentinel; - node->right = sentinel; + node->left = sentinel; + node->right = sentinel; nxt_rbtree1_red(node); } - /* * Subtraction cannot be used in these comparison functions because the key * values are spread uniform in whole 0 .. 2^32 range but are not grouped @@ -188,8 +177,7 @@ nxt_rbtree1_test_insert_value(nxt_rbtree1_node_t *temp, */ nxt_inline nxt_int_t -nxt_rbtree1_test_compare(nxt_rbtree1_node_t *node1, nxt_rbtree1_node_t *node2) -{ +nxt_rbtree1_test_compare(nxt_rbtree1_node_t *node1, nxt_rbtree1_node_t *node2) { if (node1->key < node2->key) { return -1; } @@ -201,13 +189,11 @@ nxt_rbtree1_test_compare(nxt_rbtree1_node_t *node1, nxt_rbtree1_node_t *node2) return 1; } - static int nxt_cdecl -nxt_rbtree1_test_sort_cmp(const void *one, const void *two) -{ - const uint32_t *first, *second; +nxt_rbtree1_test_sort_cmp(const void *one, const void *two) { + const uint32_t *first, *second; - first = one; + first = one; second = two; if (*first < *second) { @@ -221,14 +207,12 @@ nxt_rbtree1_test_sort_cmp(const void *one, const void *two) return 1; } - static nxt_rbtree1_node_t * -nxt_rbtree1_test_find(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) -{ +nxt_rbtree1_test_find(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) { nxt_int_t n; - nxt_rbtree1_node_t *next, *sentinel; + nxt_rbtree1_node_t *next, *sentinel; - next = tree->root; + next = tree->root; sentinel = tree->sentinel; while (next != sentinel) { @@ -254,19 +238,17 @@ nxt_rbtree1_test_find(nxt_rbtree1_t *tree, nxt_rbtree1_node_t *node) #if (NXT_TEST_RTDTSC) -#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree1_node_t)) +#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree1_node_t)) static nxt_rbtree1_t mb_tree; static nxt_rbtree1_node_t mb_sentinel; -static nxt_rbtree1_node_t *mb_nodes; - +static nxt_rbtree1_node_t *mb_nodes; nxt_int_t -nxt_rbtree1_mb_start(nxt_thread_t *thr) -{ - uint32_t key; - uint64_t start, end; - nxt_uint_t i, n; +nxt_rbtree1_mb_start(nxt_thread_t *thr) { + uint32_t key; + uint64_t start, end; + nxt_uint_t i, n; n = NXT_RBT_STEP; @@ -280,7 +262,7 @@ nxt_rbtree1_mb_start(nxt_thread_t *thr) key = 0; for (i = 0; i < NXT_RBT_NODES; i++) { - key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); mb_nodes[n * i].key = key; } @@ -295,45 +277,41 @@ nxt_rbtree1_mb_start(nxt_thread_t *thr) end = nxt_rdtsc(); nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree1 mb cached insert: %L cycles", end - start); + "rbtree1 mb cached insert: %L cycles", end - start); return NXT_OK; } - void -nxt_rbtree1_mb_insert(nxt_thread_t *thr) -{ - uint64_t start, end; - nxt_uint_t n; +nxt_rbtree1_mb_insert(nxt_thread_t *thr) { + uint64_t start, end; + nxt_uint_t n; - n = NXT_RBT_STEP; + n = NXT_RBT_STEP; n *= (NXT_RBT_NODES - 1); start = nxt_rdtsc(); nxt_rbtree1_insert(&mb_tree, &mb_nodes[n]); end = nxt_rdtsc(); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree1 mb insert: %L cycles", end - start); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree1 mb insert: %L cycles", + end - start); } - void -nxt_rbtree1_mb_delete(nxt_thread_t *thr) -{ - uint64_t start, end; - nxt_uint_t n; +nxt_rbtree1_mb_delete(nxt_thread_t *thr) { + uint64_t start, end; + nxt_uint_t n; - n = NXT_RBT_STEP; + n = NXT_RBT_STEP; n *= (NXT_RBT_NODES / 4 + 1); start = nxt_rdtsc(); nxt_rbtree1_delete(&mb_tree, &mb_nodes[n]); end = nxt_rdtsc(); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree1 mb delete: %L cycles", end - start); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree1 mb delete: %L cycles", + end - start); nxt_free(mb_nodes); } diff --git a/src/test/nxt_rbtree_test.c b/src/test/nxt_rbtree_test.c index 41375d920..4dab94bb4 100644 --- a/src/test/nxt_rbtree_test.c +++ b/src/test/nxt_rbtree_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,29 +6,27 @@ #include #include "nxt_tests.h" - typedef struct { - NXT_RBTREE_NODE (node); - uint32_t key; + NXT_RBTREE_NODE(node); + uint32_t key; } nxt_rbtree_test_t; - -static intptr_t nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2); -static nxt_int_t nxt_rbtree_test_compare(uint32_t key1, uint32_t key2); -static int nxt_cdecl nxt_rbtree_test_sort_cmp(const void *one, const void *two); - +static intptr_t +nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2); +static nxt_int_t +nxt_rbtree_test_compare(uint32_t key1, uint32_t key2); +static int nxt_cdecl +nxt_rbtree_test_sort_cmp(const void *one, const void *two); nxt_int_t -nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) -{ - void *mark; +nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) { + void *mark; uint32_t key, *keys; nxt_uint_t i; nxt_nsec_t start, end; nxt_rbtree_t tree; - nxt_rbtree_node_t *node; - nxt_rbtree_test_t *items, *item; + nxt_rbtree_node_t *node; + nxt_rbtree_test_t *items, *item; nxt_thread_time_update(thr); @@ -55,7 +52,7 @@ nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) for (i = 0; i < n; i++) { key = nxt_murmur_hash2(&key, sizeof(uint32_t)); - keys[i] = key; + keys[i] = key; items[i].key = key; } @@ -73,21 +70,20 @@ nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) if (node != (nxt_rbtree_node_t *) &items[i].node) { nxt_log_alert(thr->log, "rbtree test failed: %08XD not found", - items[i].key); + items[i].key); goto fail; } } - i = 0; + i = 0; node = nxt_rbtree_min(&tree); while (nxt_rbtree_is_there_successor(&tree, node)) { - item = (nxt_rbtree_test_t *) node; if (keys[i] != item->key) { - nxt_log_alert(thr->log, "rbtree test failed: %i: %08XD %08XD", - i, keys[i], item->key); + nxt_log_alert(thr->log, "rbtree test failed: %i: %08XD %08XD", i, + keys[i], item->key); goto fail; } @@ -124,7 +120,7 @@ nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) nxt_free(items); nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree test passed %0.3fs", - (end - start) / 1000000000.0); + (end - start) / 1000000000.0); return NXT_OK; @@ -136,12 +132,9 @@ nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n) return NXT_ERROR; } - static intptr_t -nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, - nxt_rbtree_node_t *node2) -{ - nxt_rbtree_test_t *item1, *item2; +nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2) { + nxt_rbtree_test_t *item1, *item2; item1 = (nxt_rbtree_test_t *) node1; item2 = (nxt_rbtree_test_t *) node2; @@ -149,7 +142,6 @@ nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, return nxt_rbtree_test_compare(item1->key, item2->key); } - /* * Subtraction cannot be used in these comparison functions because * the key values are spread uniform in whole 0 .. 2^32 range but are @@ -157,8 +149,7 @@ nxt_rbtree_test_comparison(nxt_rbtree_node_t *node1, */ static nxt_int_t -nxt_rbtree_test_compare(uint32_t key1, uint32_t key2) -{ +nxt_rbtree_test_compare(uint32_t key1, uint32_t key2) { if (key1 < key2) { return -1; } @@ -170,13 +161,11 @@ nxt_rbtree_test_compare(uint32_t key1, uint32_t key2) return 1; } - static int nxt_cdecl -nxt_rbtree_test_sort_cmp(const void *one, const void *two) -{ - const uint32_t *first, *second; +nxt_rbtree_test_sort_cmp(const void *one, const void *two) { + const uint32_t *first, *second; - first = one; + first = one; second = two; if (*first < *second) { @@ -193,18 +182,16 @@ nxt_rbtree_test_sort_cmp(const void *one, const void *two) #if (NXT_TEST_RTDTSC) -#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree_test_t)) +#define NXT_RBT_STEP (21 * nxt_pagesize / 10 / sizeof(nxt_rbtree_test_t)) static nxt_rbtree_t mb_tree; -static nxt_rbtree_test_t *mb_nodes; - +static nxt_rbtree_test_t *mb_nodes; nxt_int_t -nxt_rbtree_mb_start(nxt_thread_t *thr) -{ - uint32_t key; - uint64_t start, end; - nxt_uint_t i, n; +nxt_rbtree_mb_start(nxt_thread_t *thr) { + uint32_t key; + uint64_t start, end; + nxt_uint_t i, n; n = NXT_RBT_STEP; @@ -218,7 +205,7 @@ nxt_rbtree_mb_start(nxt_thread_t *thr) key = 0; for (i = 0; i < NXT_RBT_NODES; i++) { - key = nxt_murmur_hash2(&key, sizeof(uint32_t)); + key = nxt_murmur_hash2(&key, sizeof(uint32_t)); mb_nodes[n * i].key = key; } @@ -233,45 +220,41 @@ nxt_rbtree_mb_start(nxt_thread_t *thr) end = nxt_rdtsc(); nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree mb cached insert: %L cycles", end - start); + "rbtree mb cached insert: %L cycles", end - start); return NXT_OK; } - void -nxt_rbtree_mb_insert(nxt_thread_t *thr) -{ - uint64_t start, end; - nxt_uint_t n; +nxt_rbtree_mb_insert(nxt_thread_t *thr) { + uint64_t start, end; + nxt_uint_t n; - n = NXT_RBT_STEP; + n = NXT_RBT_STEP; n *= (NXT_RBT_NODES - 1); start = nxt_rdtsc(); nxt_rbtree_insert(&mb_tree, &mb_nodes[n].node); end = nxt_rdtsc(); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree mb insert: %L cycles", end - start); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree mb insert: %L cycles", + end - start); } - void -nxt_rbtree_mb_delete(nxt_thread_t *thr) -{ - uint64_t start, end; - nxt_uint_t n; +nxt_rbtree_mb_delete(nxt_thread_t *thr) { + uint64_t start, end; + nxt_uint_t n; - n = NXT_RBT_STEP; + n = NXT_RBT_STEP; n *= (NXT_RBT_NODES / 4 + 1); start = nxt_rdtsc(); nxt_rbtree_delete(&mb_tree, &mb_nodes[n].node); end = nxt_rdtsc(); - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "rbtree mb delete: %L cycles", end - start); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "rbtree mb delete: %L cycles", + end - start); nxt_free(mb_nodes); } diff --git a/src/test/nxt_sprintf_test.c b/src/test/nxt_sprintf_test.c index 7c6e20195..56936e7b7 100644 --- a/src/test/nxt_sprintf_test.c +++ b/src/test/nxt_sprintf_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,61 +6,52 @@ #include #include "nxt_tests.h" - typedef struct { - const char *format; - const char *test; - double number; + const char *format; + const char *test; + double number; } nxt_sprintf_double_test_t; - -static const nxt_sprintf_double_test_t double_test[] = -{ - { "%3.5f", "1234.56700", 1234.567 }, - { "%3.0f", "1234", 1234.567 }, - { "%f", "1234.567", 1234.567 }, - { "%f", "0.1", 0.1 }, - { "%f", "0.000001", 0.000001 }, - { "%f", "4503599627370495", 4503599627370495.0 }, +static const nxt_sprintf_double_test_t double_test[] = { + {"%3.5f", "1234.56700", 1234.567}, + {"%3.0f", "1234", 1234.567}, + {"%f", "1234.567", 1234.567}, + {"%f", "0.1", 0.1}, + {"%f", "0.000001", 0.000001}, + {"%f", "4503599627370495", 4503599627370495.0}, }; - static nxt_int_t nxt_sprintf_test_double(u_char *buf, u_char *end, const char *fmt, - const char *test, double n) -{ - u_char *p; + const char *test, double n) { + u_char *p; - p = nxt_sprintf(buf, end, fmt, n); + p = nxt_sprintf(buf, end, fmt, n); *p = '\0'; return nxt_strcmp(buf, test); } - nxt_int_t -nxt_sprintf_test(nxt_thread_t *thr) -{ - nxt_int_t ret; - nxt_uint_t i; - u_char *end, buf[64]; +nxt_sprintf_test(nxt_thread_t *thr) { + nxt_int_t ret; + nxt_uint_t i; + u_char *end, buf[64]; nxt_thread_time_update(thr); end = buf + 64; for (i = 0; i < nxt_nitems(double_test); i++) { - ret = nxt_sprintf_test_double(buf, end, double_test[i].format, - double_test[i].test, - double_test[i].number); + double_test[i].test, double_test[i].number); if (ret == NXT_OK) { continue; } nxt_log_alert(thr->log, "nxt_sprintf(\"%s\") failed: \"%s\" vs \"%s\"", - double_test[i].format, double_test[i].test, buf); + double_test[i].format, double_test[i].test, buf); return NXT_ERROR; } diff --git a/src/test/nxt_strverscmp_test.c b/src/test/nxt_strverscmp_test.c index 40adbfb26..4c65f1548 100644 --- a/src/test/nxt_strverscmp_test.c +++ b/src/test/nxt_strverscmp_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. * Copyright (C) Valentin V. Bartenev @@ -7,57 +6,52 @@ #include #include "nxt_tests.h" - typedef struct { - const char *v1; + const char *v1; const char res; - const char *v2; + const char *v2; } nxt_strverscmp_test_t; - nxt_int_t -nxt_strverscmp_test(nxt_thread_t *thr) -{ - nxt_int_t ret; - nxt_uint_t i; - - static const nxt_strverscmp_test_t tests[] = { - { "word", '=', "word" }, - { "42", '=', "42" }, - { "000", '=', "000" }, - { "2", '>', "1" }, - { "2", '<', "10" }, - { "rc2", '>', "rc" }, - { "rc2", '<', "rc3" }, - { "1.13.8", '>', "1.1.9" }, - { "1.9", '<', "1.13.8" }, - { "9.9", '<', "10.0" }, - { "1", '>', "007" }, - { "2b01", '<', "2b013" }, - { "011", '>', "01" }, - { "011", '>', "01.1" }, - { "011", '>', "01+1" }, - { "011", '<', "01:1" }, - { "011", '<', "01b" }, - { "020", '>', "01b" }, - { "a0", '>', "a01" }, - { "b00", '<', "b01" }, - { "c000", '<', "c01" }, - { "000", '<', "00" }, - { "000", '<', "00a" }, - { "00.", '>', "000" }, - { "a.0", '<', "a0" }, - { "b11", '>', "b0" }, +nxt_strverscmp_test(nxt_thread_t *thr) { + nxt_int_t ret; + nxt_uint_t i; + + static const nxt_strverscmp_test_t tests[] = { + {"word", '=', "word"}, + {"42", '=', "42"}, + {"000", '=', "000"}, + {"2", '>', "1"}, + {"2", '<', "10"}, + {"rc2", '>', "rc"}, + {"rc2", '<', "rc3"}, + {"1.13.8", '>', "1.1.9"}, + {"1.9", '<', "1.13.8"}, + {"9.9", '<', "10.0"}, + {"1", '>', "007"}, + {"2b01", '<', "2b013"}, + {"011", '>', "01"}, + {"011", '>', "01.1"}, + {"011", '>', "01+1"}, + {"011", '<', "01:1"}, + {"011", '<', "01b"}, + {"020", '>', "01b"}, + {"a0", '>', "a01"}, + {"b00", '<', "b01"}, + {"c000", '<', "c01"}, + {"000", '<', "00"}, + {"000", '<', "00a"}, + {"00.", '>', "000"}, + {"a.0", '<', "a0"}, + {"b11", '>', "b0"}, }; nxt_thread_time_update(thr); for (i = 0; i < nxt_nitems(tests); i++) { - ret = nxt_strverscmp((u_char *) tests[i].v1, (u_char *) tests[i].v2); switch (tests[i].res) { - case '<': if (ret < 0) { continue; @@ -81,14 +75,13 @@ nxt_strverscmp_test(nxt_thread_t *thr) } nxt_log_alert(thr->log, - "nxt_strverscmp() test \"%s\" %c \"%s\" failed: %i", - tests[i].v1, tests[i].res, tests[i].v2, ret); + "nxt_strverscmp() test \"%s\" %c \"%s\" failed: %i", tests[i].v1, + tests[i].res, tests[i].v2, ret); return NXT_ERROR; } - nxt_log_error(NXT_LOG_NOTICE, thr->log, - "nxt_strverscmp() test passed"); + nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_strverscmp() test passed"); return NXT_OK; } diff --git a/src/test/nxt_term_parse_test.c b/src/test/nxt_term_parse_test.c index ceae3fe5b..208e7144d 100644 --- a/src/test/nxt_term_parse_test.c +++ b/src/test/nxt_term_parse_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,51 +6,45 @@ #include #include "nxt_tests.h" - typedef struct { - nxt_str_t string; - nxt_bool_t is_sec; - nxt_int_t value; + nxt_str_t string; + nxt_bool_t is_sec; + nxt_int_t value; } nxt_term_parse_test_t; - -static const nxt_term_parse_test_t terms[] = { - { nxt_string("1y"), 1, 365 * 24 * 60 * 60 }, - { nxt_string("1w"), 1, 7 * 24 * 60 * 60 }, - { nxt_string("1w"), 0, 7 * 24 * 60 * 60 * 1000 }, - { nxt_string("1w 1d"), 0, 8 * 24 * 60 * 60 * 1000 }, - { nxt_string("1w d"), 0, -1 }, - { nxt_string("w"), 0, -1 }, - { nxt_string("1d 1w"), 0, -1 }, - { nxt_string("1 "), 1, 1 }, - { nxt_string("25d"), 0, -2 }, - { nxt_string("300"), 1, 300 }, - { nxt_string("300"), 0, 300000 }, - { nxt_string("300s"), 1, 300 }, - { nxt_string("300ms"), 0, 300 }, - { nxt_string("1y 1M 1w1d1h1m1s"), 1, - (((((365 + 30 + 7 + 1) * 24 + 1) * 60) + 1) * 60) + 1 }, +static const nxt_term_parse_test_t terms[] = { + {nxt_string("1y"), 1, 365 * 24 * 60 * 60}, + {nxt_string("1w"), 1, 7 * 24 * 60 * 60}, + {nxt_string("1w"), 0, 7 * 24 * 60 * 60 * 1000}, + {nxt_string("1w 1d"), 0, 8 * 24 * 60 * 60 * 1000}, + {nxt_string("1w d"), 0, -1}, + {nxt_string("w"), 0, -1}, + {nxt_string("1d 1w"), 0, -1}, + {nxt_string("1 "), 1, 1}, + {nxt_string("25d"), 0, -2}, + {nxt_string("300"), 1, 300}, + {nxt_string("300"), 0, 300000}, + {nxt_string("300s"), 1, 300}, + {nxt_string("300ms"), 0, 300}, + {nxt_string("1y 1M 1w1d1h1m1s"), 1, + (((((365 + 30 + 7 + 1) * 24 + 1) * 60) + 1) * 60) + 1}, }; - nxt_int_t -nxt_term_parse_test(nxt_thread_t *thr) -{ +nxt_term_parse_test(nxt_thread_t *thr) { nxt_int_t val; nxt_uint_t i; - const nxt_str_t *s; + const nxt_str_t *s; nxt_thread_time_update(thr); for (i = 0; i < nxt_nitems(terms); i++) { - - s = &terms[i].string; + s = &terms[i].string; val = nxt_term_parse(s->start, s->length, terms[i].is_sec); if (val != terms[i].value) { - nxt_log_alert(thr->log, - "term parse test failed: \"%V\": %i %i", - s, terms[i].value, val); + nxt_log_alert(thr->log, "term parse test failed: \"%V\": %i %i", s, + terms[i].value, val); return NXT_ERROR; } } diff --git a/src/test/nxt_tests.c b/src/test/nxt_tests.c index 03a2a1df4..cc6fb5f8d 100644 --- a/src/test/nxt_tests.c +++ b/src/test/nxt_tests.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,41 +7,36 @@ #include "nxt_tests.h" -extern char **environ; - -nxt_module_init_t nxt_init_modules[1]; -nxt_uint_t nxt_init_modules_n; +extern char **environ; +nxt_module_init_t nxt_init_modules[1]; +nxt_uint_t nxt_init_modules_n; /* The function is defined here to prevent inline optimizations. */ static nxt_bool_t -nxt_msec_less(nxt_msec_t first, nxt_msec_t second) -{ +nxt_msec_less(nxt_msec_t first, nxt_msec_t second) { return (nxt_msec_diff(first, second) < 0); } - int nxt_cdecl -main(int argc, char **argv) -{ +main(int argc, char **argv) { nxt_task_t task; - nxt_thread_t *thr; + nxt_thread_t *thr; if (nxt_lib_start("tests", argv, &environ) != NXT_OK) { return 1; } nxt_main_log.level = NXT_LOG_INFO; - task.log = &nxt_main_log; + task.log = &nxt_main_log; - thr = nxt_thread(); + thr = nxt_thread(); thr->task = &task; #if (NXT_TEST_RTDTSC) if (nxt_process_argv[1] != NULL - && memcmp(nxt_process_argv[1], "rbm", 3) == 0) - { + && memcmp(nxt_process_argv[1], "rbm", 3) == 0) { if (nxt_rbtree1_mb_start(thr) != NXT_OK) { return 1; } diff --git a/src/test/nxt_tests.h b/src/test/nxt_tests.h index 463dc8519..38d3efcb6 100644 --- a/src/test/nxt_tests.h +++ b/src/test/nxt_tests.h @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -11,20 +10,19 @@ typedef nxt_bool_t (*nxt_msec_less_t)(nxt_msec_t first, nxt_msec_t second); -#define NXT_RBT_NODES 1500 +#define NXT_RBT_NODES 1500 #if (__i386__ || __i386 || __amd64__ || __amd64) #if (NXT_GCC || NXT_CLANG) -#define NXT_TEST_RTDTSC 1 +#define NXT_TEST_RTDTSC 1 nxt_inline uint64_t -nxt_rdtsc(void) -{ - uint32_t eax, edx; +nxt_rdtsc(void) { + uint32_t eax, edx; - __asm__ volatile ("rdtsc" : "=a" (eax), "=d" (edx)); + __asm__ volatile("rdtsc" : "=a"(eax), "=d"(edx)); return ((uint64_t) edx << 32) | eax; } @@ -33,39 +31,59 @@ nxt_rdtsc(void) #endif -nxt_int_t nxt_term_parse_test(nxt_thread_t *thr); -nxt_int_t nxt_msec_diff_test(nxt_thread_t *thr, nxt_msec_less_t); +nxt_int_t +nxt_term_parse_test(nxt_thread_t *thr); +nxt_int_t +nxt_msec_diff_test(nxt_thread_t *thr, nxt_msec_less_t); -nxt_int_t nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n); -nxt_int_t nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n); +nxt_int_t +nxt_rbtree_test(nxt_thread_t *thr, nxt_uint_t n); +nxt_int_t +nxt_rbtree1_test(nxt_thread_t *thr, nxt_uint_t n); #if (NXT_TEST_RTDTSC) -nxt_int_t nxt_rbtree_mb_start(nxt_thread_t *thr); -void nxt_rbtree_mb_insert(nxt_thread_t *thr); -void nxt_rbtree_mb_delete(nxt_thread_t *thr); +nxt_int_t +nxt_rbtree_mb_start(nxt_thread_t *thr); +void +nxt_rbtree_mb_insert(nxt_thread_t *thr); +void +nxt_rbtree_mb_delete(nxt_thread_t *thr); -nxt_int_t nxt_rbtree1_mb_start(nxt_thread_t *thr); -void nxt_rbtree1_mb_insert(nxt_thread_t *thr); -void nxt_rbtree1_mb_delete(nxt_thread_t *thr); +nxt_int_t +nxt_rbtree1_mb_start(nxt_thread_t *thr); +void +nxt_rbtree1_mb_insert(nxt_thread_t *thr); +void +nxt_rbtree1_mb_delete(nxt_thread_t *thr); #endif -nxt_int_t nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, +nxt_int_t +nxt_mp_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, + size_t max_size); +nxt_int_t +nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, nxt_uint_t nblocks, size_t max_size); -nxt_int_t nxt_mem_zone_test(nxt_thread_t *thr, nxt_uint_t runs, - nxt_uint_t nblocks, size_t max_size); -nxt_int_t nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, - nxt_bool_t use_pool); - -nxt_int_t nxt_gmtime_test(nxt_thread_t *thr); -nxt_int_t nxt_sprintf_test(nxt_thread_t *thr); -nxt_int_t nxt_malloc_test(nxt_thread_t *thr); -nxt_int_t nxt_utf8_test(nxt_thread_t *thr); -nxt_int_t nxt_http_parse_test(nxt_thread_t *thr); -nxt_int_t nxt_strverscmp_test(nxt_thread_t *thr); -nxt_int_t nxt_base64_test(nxt_thread_t *thr); -nxt_int_t nxt_clone_creds_test(nxt_thread_t *thr); +nxt_int_t +nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool); + +nxt_int_t +nxt_gmtime_test(nxt_thread_t *thr); +nxt_int_t +nxt_sprintf_test(nxt_thread_t *thr); +nxt_int_t +nxt_malloc_test(nxt_thread_t *thr); +nxt_int_t +nxt_utf8_test(nxt_thread_t *thr); +nxt_int_t +nxt_http_parse_test(nxt_thread_t *thr); +nxt_int_t +nxt_strverscmp_test(nxt_thread_t *thr); +nxt_int_t +nxt_base64_test(nxt_thread_t *thr); +nxt_int_t +nxt_clone_creds_test(nxt_thread_t *thr); #endif /* _NXT_TESTS_H_INCLUDED_ */ diff --git a/src/test/nxt_unit_app_test.c b/src/test/nxt_unit_app_test.c index 5dcebe187..4baad5994 100644 --- a/src/test/nxt_unit_app_test.c +++ b/src/test/nxt_unit_app_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -11,42 +10,44 @@ #include -#define CONTENT_TYPE "Content-Type" -#define TEXT_PLAIN "text/plain" -#define HELLO_WORLD "Hello world!\n" +#define CONTENT_TYPE "Content-Type" +#define TEXT_PLAIN "text/plain" +#define HELLO_WORLD "Hello world!\n" -#define NEW_LINE "\n" +#define NEW_LINE "\n" -#define REQUEST_DATA "Request data:\n" -#define METHOD " Method: " -#define PROTOCOL " Protocol: " -#define REMOTE_ADDR " Remote addr: " -#define LOCAL_ADDR " Local addr: " -#define TARGET " Target: " -#define PATH " Path: " -#define QUERY " Query: " -#define FIELDS " Fields:\n" -#define FIELD_PAD " " -#define FIELD_SEP ": " -#define BODY " Body:\n" +#define REQUEST_DATA "Request data:\n" +#define METHOD " Method: " +#define PROTOCOL " Protocol: " +#define REMOTE_ADDR " Remote addr: " +#define LOCAL_ADDR " Local addr: " +#define TARGET " Target: " +#define PATH " Path: " +#define QUERY " Query: " +#define FIELDS " Fields:\n" +#define FIELD_PAD " " +#define FIELD_SEP ": " +#define BODY " Body:\n" -static int ready_handler(nxt_unit_ctx_t *ctx); -static void *worker(void *main_ctx); -static void greeting_app_request_handler(nxt_unit_request_info_t *req); -static inline char *copy(char *p, const void *src, uint32_t len); +static int +ready_handler(nxt_unit_ctx_t *ctx); +static void * +worker(void *main_ctx); +static void +greeting_app_request_handler(nxt_unit_request_info_t *req); +static inline char * +copy(char *p, const void *src, uint32_t len); static int thread_count; -static pthread_t *threads; - +static pthread_t *threads; int -main(int argc, char **argv) -{ - int i, err; - nxt_unit_ctx_t *ctx; - nxt_unit_init_t init; +main(int argc, char **argv) { + int i, err; + nxt_unit_ctx_t *ctx; + nxt_unit_init_t init; if (argc == 3 && strcmp(argv[1], "-t") == 0) { thread_count = atoi(argv[2]); @@ -55,7 +56,7 @@ main(int argc, char **argv) memset(&init, 0, sizeof(nxt_unit_init_t)); init.callbacks.request_handler = greeting_app_request_handler; - init.callbacks.ready_handler = ready_handler; + init.callbacks.ready_handler = ready_handler; ctx = nxt_unit_init(&init); if (ctx == NULL) { @@ -74,8 +75,8 @@ main(int argc, char **argv) nxt_unit_debug(ctx, "join thread #%d", i); } else { - nxt_unit_alert(ctx, "pthread_join(#%d) failed: %s (%d)", - i, strerror(err), err); + nxt_unit_alert(ctx, "pthread_join(#%d) failed: %s (%d)", i, + strerror(err), err); } } @@ -89,11 +90,9 @@ main(int argc, char **argv) return 0; } - static int -ready_handler(nxt_unit_ctx_t *ctx) -{ - int i, err; +ready_handler(nxt_unit_ctx_t *ctx) { + int i, err; nxt_unit_debug(ctx, "ready"); @@ -116,12 +115,10 @@ ready_handler(nxt_unit_ctx_t *ctx) return NXT_UNIT_OK; } - static void * -worker(void *main_ctx) -{ +worker(void *main_ctx) { int rc; - nxt_unit_ctx_t *ctx; + nxt_unit_ctx_t *ctx; ctx = nxt_unit_ctx_alloc(main_ctx, NULL); if (ctx == NULL) { @@ -139,36 +136,32 @@ worker(void *main_ctx) return (void *) (intptr_t) rc; } - static void -greeting_app_request_handler(nxt_unit_request_info_t *req) -{ +greeting_app_request_handler(nxt_unit_request_info_t *req) { int rc; - char *p; + char *p; ssize_t res; uint32_t i; - nxt_unit_buf_t *buf; - nxt_unit_field_t *f; - nxt_unit_request_t *r; + nxt_unit_buf_t *buf; + nxt_unit_field_t *f; + nxt_unit_request_t *r; rc = nxt_unit_response_init(req, 200 /* Status code. */, - 1 /* Number of response headers. */, - nxt_length(CONTENT_TYPE) - + nxt_length(TEXT_PLAIN) - + nxt_length(HELLO_WORLD)); + 1 /* Number of response headers. */, + nxt_length(CONTENT_TYPE) + nxt_length(TEXT_PLAIN) + + nxt_length(HELLO_WORLD)); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto fail; } - rc = nxt_unit_response_add_field(req, - CONTENT_TYPE, nxt_length(CONTENT_TYPE), - TEXT_PLAIN, nxt_length(TEXT_PLAIN)); + rc = nxt_unit_response_add_field(req, CONTENT_TYPE, + nxt_length(CONTENT_TYPE), TEXT_PLAIN, nxt_length(TEXT_PLAIN)); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto fail; } rc = nxt_unit_response_add_content(req, HELLO_WORLD, - nxt_length(HELLO_WORLD)); + nxt_length(HELLO_WORLD)); if (nxt_slow_path(rc != NXT_UNIT_OK)) { goto fail; } @@ -180,28 +173,16 @@ greeting_app_request_handler(nxt_unit_request_info_t *req) r = req->request; - buf = nxt_unit_response_buf_alloc(req, (req->request_buf->end - - req->request_buf->start) - + nxt_length(REQUEST_DATA) - + nxt_length(METHOD) - + nxt_length(NEW_LINE) - + nxt_length(PROTOCOL) - + nxt_length(NEW_LINE) - + nxt_length(REMOTE_ADDR) - + nxt_length(NEW_LINE) - + nxt_length(LOCAL_ADDR) - + nxt_length(NEW_LINE) - + nxt_length(TARGET) - + nxt_length(NEW_LINE) - + nxt_length(PATH) - + nxt_length(NEW_LINE) - + nxt_length(QUERY) - + nxt_length(NEW_LINE) - + nxt_length(FIELDS) - + r->fields_count * ( - nxt_length(FIELD_PAD) - + nxt_length(FIELD_SEP)) - + nxt_length(BODY)); + buf = nxt_unit_response_buf_alloc(req, + (req->request_buf->end - req->request_buf->start) + + nxt_length(REQUEST_DATA) + nxt_length(METHOD) + + nxt_length(NEW_LINE) + nxt_length(PROTOCOL) + nxt_length(NEW_LINE) + + nxt_length(REMOTE_ADDR) + nxt_length(NEW_LINE) + + nxt_length(LOCAL_ADDR) + nxt_length(NEW_LINE) + nxt_length(TARGET) + + nxt_length(NEW_LINE) + nxt_length(PATH) + nxt_length(NEW_LINE) + + nxt_length(QUERY) + nxt_length(NEW_LINE) + nxt_length(FIELDS) + + r->fields_count * (nxt_length(FIELD_PAD) + nxt_length(FIELD_SEP)) + + nxt_length(BODY)); if (nxt_slow_path(buf == NULL)) { rc = NXT_UNIT_ERROR; @@ -212,33 +193,33 @@ greeting_app_request_handler(nxt_unit_request_info_t *req) p = copy(p, REQUEST_DATA, nxt_length(REQUEST_DATA)); - p = copy(p, METHOD, nxt_length(METHOD)); - p = copy(p, nxt_unit_sptr_get(&r->method), r->method_length); + p = copy(p, METHOD, nxt_length(METHOD)); + p = copy(p, nxt_unit_sptr_get(&r->method), r->method_length); *p++ = '\n'; - p = copy(p, PROTOCOL, nxt_length(PROTOCOL)); - p = copy(p, nxt_unit_sptr_get(&r->version), r->version_length); + p = copy(p, PROTOCOL, nxt_length(PROTOCOL)); + p = copy(p, nxt_unit_sptr_get(&r->version), r->version_length); *p++ = '\n'; - p = copy(p, REMOTE_ADDR, nxt_length(REMOTE_ADDR)); - p = copy(p, nxt_unit_sptr_get(&r->remote), r->remote_length); + p = copy(p, REMOTE_ADDR, nxt_length(REMOTE_ADDR)); + p = copy(p, nxt_unit_sptr_get(&r->remote), r->remote_length); *p++ = '\n'; - p = copy(p, LOCAL_ADDR, nxt_length(LOCAL_ADDR)); - p = copy(p, nxt_unit_sptr_get(&r->local_addr), r->local_addr_length); + p = copy(p, LOCAL_ADDR, nxt_length(LOCAL_ADDR)); + p = copy(p, nxt_unit_sptr_get(&r->local_addr), r->local_addr_length); *p++ = '\n'; - p = copy(p, TARGET, nxt_length(TARGET)); - p = copy(p, nxt_unit_sptr_get(&r->target), r->target_length); + p = copy(p, TARGET, nxt_length(TARGET)); + p = copy(p, nxt_unit_sptr_get(&r->target), r->target_length); *p++ = '\n'; - p = copy(p, PATH, nxt_length(PATH)); - p = copy(p, nxt_unit_sptr_get(&r->path), r->path_length); + p = copy(p, PATH, nxt_length(PATH)); + p = copy(p, nxt_unit_sptr_get(&r->path), r->path_length); *p++ = '\n'; if (r->query.offset) { - p = copy(p, QUERY, nxt_length(QUERY)); - p = copy(p, nxt_unit_sptr_get(&r->query), r->query_length); + p = copy(p, QUERY, nxt_length(QUERY)); + p = copy(p, nxt_unit_sptr_get(&r->query), r->query_length); *p++ = '\n'; } @@ -247,19 +228,18 @@ greeting_app_request_handler(nxt_unit_request_info_t *req) for (i = 0; i < r->fields_count; i++) { f = r->fields + i; - p = copy(p, FIELD_PAD, nxt_length(FIELD_PAD)); - p = copy(p, nxt_unit_sptr_get(&f->name), f->name_length); - p = copy(p, FIELD_SEP, nxt_length(FIELD_SEP)); - p = copy(p, nxt_unit_sptr_get(&f->value), f->value_length); + p = copy(p, FIELD_PAD, nxt_length(FIELD_PAD)); + p = copy(p, nxt_unit_sptr_get(&f->name), f->name_length); + p = copy(p, FIELD_SEP, nxt_length(FIELD_SEP)); + p = copy(p, nxt_unit_sptr_get(&f->value), f->value_length); *p++ = '\n'; } if (r->content_length > 0) { p = copy(p, BODY, nxt_length(BODY)); - res = nxt_unit_request_read(req, p, buf->end - p); - p += res; - + res = nxt_unit_request_read(req, p, buf->end - p); + p += res; } buf->free = p; @@ -271,10 +251,8 @@ greeting_app_request_handler(nxt_unit_request_info_t *req) nxt_unit_request_done(req, rc); } - static inline char * -copy(char *p, const void *src, uint32_t len) -{ +copy(char *p, const void *src, uint32_t len) { memcpy(p, src, len); return p + len; diff --git a/src/test/nxt_unit_websocket_chat.c b/src/test/nxt_unit_websocket_chat.c index ec7c2cc33..23077deb6 100644 --- a/src/test/nxt_unit_websocket_chat.c +++ b/src/test/nxt_unit_websocket_chat.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -19,42 +18,41 @@ #include -#define CONTENT_TYPE "Content-Type" -#define CONTENT_LENGTH "Content-Length" -#define TEXT_HTML "text/html" +#define CONTENT_TYPE "Content-Type" +#define CONTENT_LENGTH "Content-Length" +#define TEXT_HTML "text/html" typedef struct { - nxt_queue_link_t link; - int id; + nxt_queue_link_t link; + int id; } ws_chat_request_data_t; +static int +ws_chat_root(nxt_unit_request_info_t *req); +static void +ws_chat_broadcast(const char *buf, size_t size); -static int ws_chat_root(nxt_unit_request_info_t *req); -static void ws_chat_broadcast(const char *buf, size_t size); - - -static const char ws_chat_index_html[]; -static const int ws_chat_index_html_size; -static char ws_chat_index_content_length[34]; -static int ws_chat_index_content_length_size; +static const char ws_chat_index_html[]; +static const int ws_chat_index_html_size; -static nxt_queue_t ws_chat_sessions; -static int ws_chat_next_id = 0; +static char ws_chat_index_content_length[34]; +static int ws_chat_index_content_length_size; +static nxt_queue_t ws_chat_sessions; +static int ws_chat_next_id = 0; static void -ws_chat_request_handler(nxt_unit_request_info_t *req) -{ +ws_chat_request_handler(nxt_unit_request_info_t *req) { static char buf[1024]; int buf_size; int rc = NXT_UNIT_OK; - nxt_unit_request_t *r; - ws_chat_request_data_t *data; + nxt_unit_request_t *r; + ws_chat_request_data_t *data; r = req->request; - const char* target = nxt_unit_sptr_get(&r->target); + const char *target = nxt_unit_sptr_get(&r->target); if (strcmp(target, "/") == 0) { rc = ws_chat_root(req); @@ -80,7 +78,8 @@ ws_chat_request_handler(nxt_unit_request_info_t *req) nxt_unit_response_send(req); - buf_size = snprintf(buf, sizeof(buf), "Guest #%d has joined.", data->id); + buf_size + = snprintf(buf, sizeof(buf), "Guest #%d has joined.", data->id); ws_chat_broadcast(buf, buf_size); @@ -96,40 +95,34 @@ ws_chat_request_handler(nxt_unit_request_info_t *req) nxt_unit_request_done(req, rc); } - static int -ws_chat_root(nxt_unit_request_info_t *req) -{ +ws_chat_root(nxt_unit_request_info_t *req) { int rc; rc = nxt_unit_response_init(req, 200 /* Status code. */, - 2 /* Number of response headers. */, - nxt_length(CONTENT_TYPE) - + nxt_length(TEXT_HTML) - + nxt_length(CONTENT_LENGTH) - + ws_chat_index_content_length_size - + ws_chat_index_html_size); + 2 /* Number of response headers. */, + nxt_length(CONTENT_TYPE) + nxt_length(TEXT_HTML) + + nxt_length(CONTENT_LENGTH) + ws_chat_index_content_length_size + + ws_chat_index_html_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } - rc = nxt_unit_response_add_field(req, - CONTENT_TYPE, nxt_length(CONTENT_TYPE), - TEXT_HTML, nxt_length(TEXT_HTML)); + rc = nxt_unit_response_add_field(req, CONTENT_TYPE, + nxt_length(CONTENT_TYPE), TEXT_HTML, nxt_length(TEXT_HTML)); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } - rc = nxt_unit_response_add_field(req, - CONTENT_LENGTH, nxt_length(CONTENT_LENGTH), - ws_chat_index_content_length, - ws_chat_index_content_length_size); + rc = nxt_unit_response_add_field(req, CONTENT_LENGTH, + nxt_length(CONTENT_LENGTH), ws_chat_index_content_length, + ws_chat_index_content_length_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } rc = nxt_unit_response_add_content(req, ws_chat_index_html, - ws_chat_index_html_size); + ws_chat_index_html_size); if (nxt_slow_path(rc != NXT_UNIT_OK)) { return rc; } @@ -137,32 +130,28 @@ ws_chat_root(nxt_unit_request_info_t *req) return nxt_unit_response_send(req); } - static void -ws_chat_broadcast(const char *buf, size_t size) -{ - ws_chat_request_data_t *data; - nxt_unit_request_info_t *req; +ws_chat_broadcast(const char *buf, size_t size) { + ws_chat_request_data_t *data; + nxt_unit_request_info_t *req; nxt_unit_debug(NULL, "broadcast: %*.s", (int) size, buf); nxt_queue_each(data, &ws_chat_sessions, ws_chat_request_data_t, link) { - req = nxt_unit_get_request_info_from_data(data); nxt_unit_req_debug(req, "send: %*.s", (int) size, buf); nxt_unit_websocket_send(req, NXT_WEBSOCKET_OP_TEXT, 1, buf, size); - } nxt_queue_loop; + } + nxt_queue_loop; } - static void -ws_chat_websocket_handler(nxt_unit_websocket_frame_t *ws) -{ +ws_chat_websocket_handler(nxt_unit_websocket_frame_t *ws) { int buf_size; static char buf[1024]; - ws_chat_request_data_t *data; + ws_chat_request_data_t *data; if (ws->header->opcode != NXT_WEBSOCKET_OP_TEXT) { return; @@ -173,25 +162,22 @@ ws_chat_websocket_handler(nxt_unit_websocket_frame_t *ws) buf_size = snprintf(buf, sizeof(buf), "Guest #%d: ", data->id); buf_size += nxt_unit_websocket_read(ws, buf + buf_size, - nxt_min(sizeof(buf), - ws->content_length)); + nxt_min(sizeof(buf), ws->content_length)); ws_chat_broadcast(buf, buf_size); nxt_unit_websocket_done(ws); } - static void -ws_chat_close_handler(nxt_unit_request_info_t *req) -{ +ws_chat_close_handler(nxt_unit_request_info_t *req) { int buf_size; static char buf[1024]; - ws_chat_request_data_t *data; + ws_chat_request_data_t *data; data = req->data; - buf_size = snprintf(buf, sizeof(buf), "Guest #%d has disconnected.", - data->id); + buf_size + = snprintf(buf, sizeof(buf), "Guest #%d has disconnected.", data->id); nxt_queue_remove(&data->link); nxt_unit_request_done(req, NXT_UNIT_OK); @@ -199,25 +185,21 @@ ws_chat_close_handler(nxt_unit_request_info_t *req) ws_chat_broadcast(buf, buf_size); } - int -main(void) -{ - nxt_unit_ctx_t *ctx; - nxt_unit_init_t init; +main(void) { + nxt_unit_ctx_t *ctx; + nxt_unit_init_t init; - ws_chat_index_content_length_size = - snprintf(ws_chat_index_content_length, - sizeof(ws_chat_index_content_length), "%d", - ws_chat_index_html_size); + ws_chat_index_content_length_size = snprintf(ws_chat_index_content_length, + sizeof(ws_chat_index_content_length), "%d", ws_chat_index_html_size); nxt_queue_init(&ws_chat_sessions); memset(&init, 0, sizeof(nxt_unit_init_t)); - init.callbacks.request_handler = ws_chat_request_handler; + init.callbacks.request_handler = ws_chat_request_handler; init.callbacks.websocket_handler = ws_chat_websocket_handler; - init.callbacks.close_handler = ws_chat_close_handler; + init.callbacks.close_handler = ws_chat_close_handler; init.request_data_size = sizeof(ws_chat_request_data_t); @@ -233,116 +215,119 @@ main(void) return 0; } - -static const char ws_chat_index_html[] = -"\n" -"\n" -" WebSocket Chat Examples\n" -" \n" -" \n" -"\n" -"\n" -"

Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable\n" -" Javascript and reload this page!

\n" -"
\n" -"

\n" -"
\n" -"
\n" -"
\n" -"
\n" -"\n" -"\n" -; - -static const int ws_chat_index_html_size = nxt_length(ws_chat_index_html); +static const char ws_chat_index_html[] + = "\n" + "\n" + " WebSocket Chat Examples\n" + " \n" + " \n" + "\n" + "\n" + "\n" + "
\n" + "

\n" + "
\n" + "
\n" + "
\n" + "
\n" + "\n" + "\n"; + +static const int ws_chat_index_html_size = nxt_length(ws_chat_index_html); diff --git a/src/test/nxt_unit_websocket_echo.c b/src/test/nxt_unit_websocket_echo.c index eab2e45fe..fa0408c6f 100644 --- a/src/test/nxt_unit_websocket_echo.c +++ b/src/test/nxt_unit_websocket_echo.c @@ -1,4 +1,3 @@ - /* * Copyright (C) NGINX, Inc. */ @@ -12,14 +11,12 @@ #include #include - static void -ws_echo_request_handler(nxt_unit_request_info_t *req) -{ +ws_echo_request_handler(nxt_unit_request_info_t *req) { int rc; - const char *target; + const char *target; - rc = NXT_UNIT_OK; + rc = NXT_UNIT_OK; target = nxt_unit_sptr_get(&req->request->target); if (strcmp(target, "/") == 0) { @@ -47,23 +44,21 @@ ws_echo_request_handler(nxt_unit_request_info_t *req) nxt_unit_request_done(req, rc); } - static void -ws_echo_websocket_handler(nxt_unit_websocket_frame_t *ws) -{ +ws_echo_websocket_handler(nxt_unit_websocket_frame_t *ws) { uint8_t opcode; ssize_t size; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - static size_t buf_size = 0; - static uint8_t *buf = NULL; + static size_t buf_size = 0; + static uint8_t *buf = NULL; if (buf_size < ws->content_length) { - buf = realloc(buf, ws->content_length); + buf = realloc(buf, ws->content_length); buf_size = ws->content_length; } - req = ws->req; + req = ws->req; opcode = ws->header->opcode; if (opcode == NXT_WEBSOCKET_OP_PONG) { @@ -81,16 +76,14 @@ ws_echo_websocket_handler(nxt_unit_websocket_frame_t *ws) } } - int -main(void) -{ - nxt_unit_ctx_t *ctx; - nxt_unit_init_t init; +main(void) { + nxt_unit_ctx_t *ctx; + nxt_unit_init_t init; memset(&init, 0, sizeof(nxt_unit_init_t)); - init.callbacks.request_handler = ws_echo_request_handler; + init.callbacks.request_handler = ws_echo_request_handler; init.callbacks.websocket_handler = ws_echo_websocket_handler; ctx = nxt_unit_init(&init); diff --git a/src/test/nxt_utf8_file_name_test.c b/src/test/nxt_utf8_file_name_test.c index 5723e19bb..0fbc2b6f0 100644 --- a/src/test/nxt_utf8_file_name_test.c +++ b/src/test/nxt_utf8_file_name_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -7,19 +6,18 @@ #include -extern char **environ; - -static nxt_int_t nxt_utf8_file_name_test(nxt_thread_t *thr); +extern char **environ; +static nxt_int_t +nxt_utf8_file_name_test(nxt_thread_t *thr); -nxt_module_init_t nxt_init_modules[1]; -nxt_uint_t nxt_init_modules_n; +nxt_module_init_t nxt_init_modules[1]; +nxt_uint_t nxt_init_modules_n; int nxt_cdecl -main(int argc, char **argv) -{ - nxt_thread_t *thr; +main(int argc, char **argv) { + nxt_thread_t *thr; if (nxt_lib_start("utf8_file_name_test", argv, &environ) != NXT_OK) { return 1; @@ -36,19 +34,17 @@ main(int argc, char **argv) return 0; } - static nxt_int_t -nxt_utf8_file_name_test(nxt_thread_t *thr) -{ - u_char *p, test[4], buf[32]; - ssize_t n; - uint32_t uc, lc; - nxt_int_t ret; - nxt_task_t task; - nxt_file_t uc_file, lc_file; - const u_char *pp; - nxt_file_name_t uc_name[10], lc_name[10]; - static const u_char utf8[4] = "UTF8"; +nxt_utf8_file_name_test(nxt_thread_t *thr) { + u_char *p, test[4], buf[32]; + ssize_t n; + uint32_t uc, lc; + nxt_int_t ret; + nxt_task_t task; + nxt_file_t uc_file, lc_file; + const u_char *pp; + nxt_file_name_t uc_name[10], lc_name[10]; + static const u_char utf8[4] = "UTF8"; nxt_thread_time_update(thr); @@ -66,7 +62,7 @@ nxt_utf8_file_name_test(nxt_thread_t *thr) nxt_memzero(&uc_file, sizeof(nxt_file_t)); - uc_file.name = uc_name; + uc_file.name = uc_name; uc_file.log_level = NXT_LOG_ALERT; nxt_memzero(&lc_file, sizeof(nxt_file_t)); @@ -74,10 +70,9 @@ nxt_utf8_file_name_test(nxt_thread_t *thr) lc_file.name = lc_name; task.thread = thr; - task.log = thr->log; + task.log = thr->log; for (uc = 0x41; uc < 0x110000; uc++) { - p = nxt_utf8_encode(&uc_name[5], uc); if (p == NULL) { @@ -92,7 +87,7 @@ nxt_utf8_file_name_test(nxt_thread_t *thr) if (lc == 0xFFFFFFFF) { nxt_log_alert(thr->log, "nxt_utf8_lowcase(%05uxD) failed: %05uxD", - uc, lc); + uc, lc); return NXT_ERROR; } @@ -110,7 +105,7 @@ nxt_utf8_file_name_test(nxt_thread_t *thr) *p = '\0'; ret = nxt_file_open(&task, &uc_file, NXT_FILE_WRONLY, NXT_FILE_TRUNCATE, - NXT_FILE_DEFAULT_ACCESS); + NXT_FILE_DEFAULT_ACCESS); if (ret != NXT_OK) { return NXT_ERROR; } @@ -122,7 +117,7 @@ nxt_utf8_file_name_test(nxt_thread_t *thr) nxt_file_close(&task, &uc_file); ret = nxt_file_open(&task, &lc_file, NXT_FILE_RDONLY, NXT_FILE_OPEN, - NXT_FILE_DEFAULT_ACCESS); + NXT_FILE_DEFAULT_ACCESS); if (ret == NXT_OK) { n = nxt_file_read(&lc_file, test, 4, 0); diff --git a/src/test/nxt_utf8_test.c b/src/test/nxt_utf8_test.c index 31e5bff94..46a918ff8 100644 --- a/src/test/nxt_utf8_test.c +++ b/src/test/nxt_utf8_test.c @@ -1,4 +1,3 @@ - /* * Copyright (C) Igor Sysoev * Copyright (C) NGINX, Inc. @@ -8,45 +7,103 @@ #include "nxt_tests.h" -#define NXT_UTF8_START_TEST 0xC2 -//#define NXT_UTF8_START_TEST 0 +#define NXT_UTF8_START_TEST 0xC2 +// #define NXT_UTF8_START_TEST 0 -static u_char invalid[] = { +static u_char invalid[] = { /* Invalid first byte less than 0xC2. */ - 1, 0x80, 0x00, 0x00, 0x00, - 1, 0xC0, 0x00, 0x00, 0x00, - 2, 0xC0, 0x00, 0x00, 0x00, - 3, 0xC0, 0x00, 0x00, 0x00, - 4, 0xC0, 0x00, 0x00, 0x00, + 1, + 0x80, + 0x00, + 0x00, + 0x00, + 1, + 0xC0, + 0x00, + 0x00, + 0x00, + 2, + 0xC0, + 0x00, + 0x00, + 0x00, + 3, + 0xC0, + 0x00, + 0x00, + 0x00, + 4, + 0xC0, + 0x00, + 0x00, + 0x00, /* Invalid 0x110000 value. */ - 4, 0xF4, 0x90, 0x80, 0x80, + 4, + 0xF4, + 0x90, + 0x80, + 0x80, /* Incomplete length. */ - 2, 0xE0, 0xAF, 0xB5, 0x00, + 2, + 0xE0, + 0xAF, + 0xB5, + 0x00, /* Overlong values. */ - 2, 0xC0, 0x80, 0x00, 0x00, - 2, 0xC1, 0xB3, 0x00, 0x00, - 3, 0xE0, 0x80, 0x80, 0x00, - 3, 0xE0, 0x81, 0xB3, 0x00, - 3, 0xE0, 0x90, 0x9A, 0x00, - 4, 0xF0, 0x80, 0x8A, 0x80, - 4, 0xF0, 0x80, 0x81, 0xB3, - 4, 0xF0, 0x80, 0xAF, 0xB5, + 2, + 0xC0, + 0x80, + 0x00, + 0x00, + 2, + 0xC1, + 0xB3, + 0x00, + 0x00, + 3, + 0xE0, + 0x80, + 0x80, + 0x00, + 3, + 0xE0, + 0x81, + 0xB3, + 0x00, + 3, + 0xE0, + 0x90, + 0x9A, + 0x00, + 4, + 0xF0, + 0x80, + 0x8A, + 0x80, + 4, + 0xF0, + 0x80, + 0x81, + 0xB3, + 4, + 0xF0, + 0x80, + 0xAF, + 0xB5, }; - static nxt_int_t -nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len) -{ - u_char *p, utf8[4]; +nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len) { + u_char *p, utf8[4]; size_t size; uint32_t u, d; nxt_uint_t i; - const u_char *pp; + const u_char *pp; pp = overlong; @@ -60,15 +117,14 @@ nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len) size = (p != NULL) ? p - utf8 : 0; if (len != size || memcmp(overlong, utf8, size) != 0) { - u = 0; for (i = 0; i < len; i++) { u = (u << 8) + overlong[i]; } nxt_log_alert(thr->log, - "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD, %uz", - u, len, d, size); + "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD, %uz", u, len, d, + size); return NXT_ERROR; } @@ -77,16 +133,14 @@ nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len) return NXT_OK; } - nxt_int_t -nxt_utf8_test(nxt_thread_t *thr) -{ - u_char *p, utf8[4]; +nxt_utf8_test(nxt_thread_t *thr) { + u_char *p, utf8[4]; size_t len; int32_t n; uint32_t u, d; nxt_uint_t i, k, l, m; - const u_char *pp; + const u_char *pp; nxt_thread_time_update(thr); @@ -95,7 +149,6 @@ nxt_utf8_test(nxt_thread_t *thr) /* Test valid UTF-8. */ for (u = 0; u < 0x110000; u++) { - p = nxt_utf8_encode(utf8, u); if (p == NULL) { @@ -108,8 +161,8 @@ nxt_utf8_test(nxt_thread_t *thr) d = nxt_utf8_decode(&pp, p); if (u != d) { - nxt_log_alert(thr->log, "nxt_utf8_decode(%05uxD) failed: %05uxD", - u, d); + nxt_log_alert(thr->log, "nxt_utf8_decode(%05uxD) failed: %05uxD", u, + d); return NXT_ERROR; } } @@ -117,8 +170,7 @@ nxt_utf8_test(nxt_thread_t *thr) /* Test some invalid UTF-8. */ for (i = 0; i < sizeof(invalid); i += 5) { - - len = invalid[i]; + len = invalid[i]; utf8[0] = invalid[i + 1]; utf8[1] = invalid[i + 2]; utf8[2] = invalid[i + 3]; @@ -129,15 +181,13 @@ nxt_utf8_test(nxt_thread_t *thr) d = nxt_utf8_decode(&pp, utf8 + len); if (d != 0xFFFFFFFF) { - u = 0; for (i = 0; i < len; i++) { u = (u << 8) + utf8[i]; } nxt_log_alert(thr->log, - "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD", - u, len, d); + "nxt_utf8_decode(%05uxD, %uz) failed: %05uxD", u, len, d); return NXT_ERROR; } } @@ -176,10 +226,8 @@ nxt_utf8_test(nxt_thread_t *thr) } } - n = nxt_utf8_casecmp((u_char *) "ABC АБВ ΑΒΓ", - (u_char *) "abc абв αβγ", - nxt_length("ABC АБВ ΑΒΓ"), - nxt_length("abc абв αβγ")); + n = nxt_utf8_casecmp((u_char *) "ABC АБВ ΑΒΓ", (u_char *) "abc абв αβγ", + nxt_length("ABC АБВ ΑΒΓ"), nxt_length("abc абв αβγ")); if (n != 0) { nxt_log_alert(thr->log, "nxt_utf8_casecmp() failed"); diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c index bf0b0a0f9..2652fe595 100644 --- a/src/wasm/nxt_rt_wasmtime.c +++ b/src/wasm/nxt_rt_wasmtime.c @@ -14,26 +14,24 @@ #include "nxt_wasm.h" -typedef struct nxt_wasmtime_ctx_s nxt_wasmtime_ctx_t; +typedef struct nxt_wasmtime_ctx_s nxt_wasmtime_ctx_t; struct nxt_wasmtime_ctx_s { - wasm_engine_t *engine; - wasmtime_store_t *store; + wasm_engine_t *engine; + wasmtime_store_t *store; wasmtime_memory_t memory; - wasmtime_module_t *module; - wasmtime_linker_t *linker; - wasmtime_context_t *ctx; + wasmtime_module_t *module; + wasmtime_linker_t *linker; + wasmtime_context_t *ctx; }; -static nxt_wasmtime_ctx_t nxt_wasmtime_ctx; - +static nxt_wasmtime_ctx_t nxt_wasmtime_ctx; static void nxt_wasmtime_err_msg(wasmtime_error_t *error, wasm_trap_t *trap, - const char *fmt, ...) -{ - va_list args; - wasm_byte_vec_t error_message; + const char *fmt, ...) { + va_list args; + wasm_byte_vec_t error_message; fprintf(stderr, "WASMTIME ERROR: "); va_start(args, fmt); @@ -52,77 +50,65 @@ nxt_wasmtime_err_msg(wasmtime_error_t *error, wasm_trap_t *trap, wasm_trap_message(trap, &error_message); wasm_trap_delete(trap); } - fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data); + fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); wasm_byte_vec_delete(&error_message); } - static wasm_trap_t * nxt_wasm_get_init_mem_size(void *env, wasmtime_caller_t *caller, - const wasmtime_val_t *args, size_t nargs, - wasmtime_val_t *results, size_t nresults) -{ + const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, + size_t nresults) { results[0].of.i32 = NXT_WASM_MEM_SIZE; return NULL; } - static wasm_trap_t * nxt_wasm_response_end(void *env, wasmtime_caller_t *caller, - const wasmtime_val_t *args, size_t nargs, - wasmtime_val_t *results, size_t nresults) -{ + const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, + size_t nresults) { nxt_wasm_do_response_end(env); return NULL; } - static wasm_trap_t * nxt_wasm_send_response(void *env, wasmtime_caller_t *caller, - const wasmtime_val_t *args, size_t nargs, - wasmtime_val_t *results, size_t nresults) -{ + const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, + size_t nresults) { nxt_wasm_do_send_response(env, args[0].of.i32); return NULL; } - static wasm_trap_t * nxt_wasm_send_headers(void *env, wasmtime_caller_t *caller, - const wasmtime_val_t *args, size_t nargs, - wasmtime_val_t *results, size_t nresults) -{ + const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, + size_t nresults) { nxt_wasm_do_send_headers(env, args[0].of.i32); return NULL; } - static wasm_trap_t * nxt_wasm_set_resp_status(void *env, wasmtime_caller_t *caller, - const wasmtime_val_t *args, size_t nargs, - wasmtime_val_t *results, size_t nresults) -{ - nxt_wasm_ctx_t *ctx = env; + const wasmtime_val_t *args, size_t nargs, wasmtime_val_t *results, + size_t nresults) { + nxt_wasm_ctx_t *ctx = env; ctx->status = args[0].of.i32; return NULL; } - static void -nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) -{ - const char *name = ctx->fh[hook].func_name; - wasm_trap_t *trap = NULL; - wasmtime_error_t *error; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; - const nxt_wasm_func_t *func = &ctx->fh[hook].func; +nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) { + const char *name = ctx->fh[hook].func_name; + wasm_trap_t *trap = NULL; + wasmtime_error_t *error; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + const nxt_wasm_func_t *func = &ctx->fh[hook].func; if (name == NULL) { return; @@ -131,86 +117,74 @@ nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook) error = wasmtime_func_call(rt_ctx->ctx, func, NULL, 0, NULL, 0, &trap); if (error != NULL || trap != NULL) { nxt_wasmtime_err_msg(error, trap, "failed to call hook function [%s]", - name); + name); } } - static int -nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) -{ - int i = 0; - wasm_trap_t *trap = NULL; - wasmtime_val_t args[1] = { }; - wasmtime_val_t results[1] = { }; - wasmtime_error_t *error; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; - const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_REQUEST].func; - - args[i].kind = WASMTIME_I32; +nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx) { + int i = 0; + wasm_trap_t *trap = NULL; + wasmtime_val_t args[1] = {}; + wasmtime_val_t results[1] = {}; + wasmtime_error_t *error; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_REQUEST].func; + + args[i].kind = WASMTIME_I32; args[i++].of.i32 = ctx->baddr_off; error = wasmtime_func_call(rt_ctx->ctx, func, args, i, results, 1, &trap); if (error != NULL || trap != NULL) { nxt_wasmtime_err_msg(error, trap, - "failed to call function [->wasm_request_handler]" - ); + "failed to call function [->wasm_request_handler]"); return -1; } return results[0].of.i32; } - static void -nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx) -{ - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; +nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx) { + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; static const struct { - const char *func_name; + const char *func_name; - wasmtime_func_callback_t func; - wasm_valkind_t params[1]; - wasm_valkind_t results[1]; + wasmtime_func_callback_t func; + wasm_valkind_t params[1]; + wasm_valkind_t results[1]; enum { NXT_WASM_FT_0_0, NXT_WASM_FT_1_0, NXT_WASM_FT_0_1, - } ft; - } import_functions[] = { - { - .func_name = "nxt_wasm_get_init_mem_size", - .func = nxt_wasm_get_init_mem_size, - .results = { WASM_I32 }, - .ft = NXT_WASM_FT_0_1 - }, { - .func_name = "nxt_wasm_response_end", - .func = nxt_wasm_response_end, - .ft = NXT_WASM_FT_0_0 - }, { - .func_name = "nxt_wasm_send_response", - .func = nxt_wasm_send_response, - .params = { WASM_I32 }, - .ft = NXT_WASM_FT_1_0 - }, { - .func_name = "nxt_wasm_send_headers", - .func = nxt_wasm_send_headers, - .params = { WASM_I32 }, - .ft = NXT_WASM_FT_1_0 - }, { - .func_name = "nxt_wasm_set_resp_status", - .func = nxt_wasm_set_resp_status, - .params = { WASM_I32 }, - .ft = NXT_WASM_FT_1_0 - }, - - { } - }, *imf; + } ft; + } import_functions[] = {{.func_name = "nxt_wasm_get_init_mem_size", + .func = nxt_wasm_get_init_mem_size, + .results = {WASM_I32}, + .ft = NXT_WASM_FT_0_1}, + {.func_name = "nxt_wasm_response_end", + .func = nxt_wasm_response_end, + .ft = NXT_WASM_FT_0_0}, + {.func_name = "nxt_wasm_send_response", + .func = nxt_wasm_send_response, + .params = {WASM_I32}, + .ft = NXT_WASM_FT_1_0}, + {.func_name = "nxt_wasm_send_headers", + .func = nxt_wasm_send_headers, + .params = {WASM_I32}, + .ft = NXT_WASM_FT_1_0}, + {.func_name = "nxt_wasm_set_resp_status", + .func = nxt_wasm_set_resp_status, + .params = {WASM_I32}, + .ft = NXT_WASM_FT_1_0}, + + {}}, + *imf; for (imf = import_functions; imf->func_name != NULL; imf++) { - wasm_functype_t *func_ty; + wasm_functype_t *func_ty; switch (imf->ft) { case NXT_WASM_FT_0_0: @@ -227,35 +201,30 @@ nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx) func_ty = NULL; } - wasmtime_linker_define_func(rt_ctx->linker, "env", 3, - imf->func_name, strlen(imf->func_name), - func_ty, imf->func, ctx, NULL); + wasmtime_linker_define_func(rt_ctx->linker, "env", 3, imf->func_name, + strlen(imf->func_name), func_ty, imf->func, ctx, NULL); wasm_functype_delete(func_ty); } } - static int -nxt_wasmtime_get_function_exports(nxt_wasm_ctx_t *ctx) -{ +nxt_wasmtime_get_function_exports(nxt_wasm_ctx_t *ctx) { int i; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; for (i = 0; i < NXT_WASM_FH_NR; i++) { - bool ok; - wasmtime_extern_t item; + bool ok; + wasmtime_extern_t item; if (ctx->fh[i].func_name == NULL) { continue; } ok = wasmtime_linker_get(rt_ctx->linker, rt_ctx->ctx, "", 0, - ctx->fh[i].func_name, - strlen(ctx->fh[i].func_name), &item); + ctx->fh[i].func_name, strlen(ctx->fh[i].func_name), &item); if (!ok) { nxt_wasmtime_err_msg(NULL, NULL, - "couldn't get (%s) export from module", - ctx->fh[i].func_name); + "couldn't get (%s) export from module", ctx->fh[i].func_name); return -1; } ctx->fh[i].func = item.of.func; @@ -264,14 +233,12 @@ nxt_wasmtime_get_function_exports(nxt_wasm_ctx_t *ctx) return 0; } - static int -nxt_wasmtime_wasi_init(const nxt_wasm_ctx_t *ctx) -{ - char **dir; - wasi_config_t *wasi_config; - wasmtime_error_t *error; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; +nxt_wasmtime_wasi_init(const nxt_wasm_ctx_t *ctx) { + char **dir; + wasi_config_t *wasi_config; + wasmtime_error_t *error; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; wasi_config = wasi_config_new(); @@ -293,33 +260,30 @@ nxt_wasmtime_wasi_init(const nxt_wasm_ctx_t *ctx) return 0; } - static int -nxt_wasmtime_init_memory(nxt_wasm_ctx_t *ctx) -{ +nxt_wasmtime_init_memory(nxt_wasm_ctx_t *ctx) { int i = 0; bool ok; - wasm_trap_t *trap = NULL; - wasmtime_val_t args[1] = { }; - wasmtime_val_t results[1] = { }; - wasmtime_error_t *error; + wasm_trap_t *trap = NULL; + wasmtime_val_t args[1] = {}; + wasmtime_val_t results[1] = {}; + wasmtime_error_t *error; wasmtime_extern_t item; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; - const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_MALLOC].func; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_MALLOC].func; - args[i].kind = WASMTIME_I32; + args[i].kind = WASMTIME_I32; args[i++].of.i32 = NXT_WASM_MEM_SIZE + NXT_WASM_PAGE_SIZE; error = wasmtime_func_call(rt_ctx->ctx, func, args, i, results, 1, &trap); if (error != NULL || trap != NULL) { nxt_wasmtime_err_msg(error, trap, - "failed to call function [->wasm_malloc_handler]" - ); + "failed to call function [->wasm_malloc_handler]"); return -1; } ok = wasmtime_linker_get(rt_ctx->linker, rt_ctx->ctx, "", 0, "memory", - strlen("memory"), &item); + strlen("memory"), &item); if (!ok) { nxt_wasmtime_err_msg(NULL, NULL, "couldn't get 'memory' from module\n"); return -1; @@ -327,30 +291,28 @@ nxt_wasmtime_init_memory(nxt_wasm_ctx_t *ctx) rt_ctx->memory = item.of.memory; ctx->baddr_off = results[0].of.i32; - ctx->baddr = wasmtime_memory_data(rt_ctx->ctx, &rt_ctx->memory); + ctx->baddr = wasmtime_memory_data(rt_ctx->ctx, &rt_ctx->memory); ctx->baddr += ctx->baddr_off; return 0; } - static int -nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) -{ +nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) { int err; - FILE *fp; + FILE *fp; size_t file_size; wasm_byte_vec_t wasm; - wasmtime_error_t *error; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + wasmtime_error_t *error; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; rt_ctx->engine = wasm_engine_new(); - rt_ctx->store = wasmtime_store_new(rt_ctx->engine, NULL, NULL); - rt_ctx->ctx = wasmtime_store_context(rt_ctx->store); + rt_ctx->store = wasmtime_store_new(rt_ctx->engine, NULL, NULL); + rt_ctx->ctx = wasmtime_store_context(rt_ctx->store); rt_ctx->linker = wasmtime_linker_new(rt_ctx->engine); - error = wasmtime_linker_define_wasi(rt_ctx->linker); + error = wasmtime_linker_define_wasi(rt_ctx->linker); if (error != NULL) { nxt_wasmtime_err_msg(error, NULL, "failed to link wasi"); return -1; @@ -358,8 +320,8 @@ nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) fp = fopen(ctx->module_path, "r"); if (!fp) { - nxt_wasmtime_err_msg(NULL, NULL, - "error opening file (%s)", ctx->module_path); + nxt_wasmtime_err_msg(NULL, NULL, "error opening file (%s)", + ctx->module_path); return -1; } fseek(fp, 0L, SEEK_END); @@ -373,8 +335,8 @@ nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) } fclose(fp); - error = wasmtime_module_new(rt_ctx->engine, (uint8_t *)wasm.data, wasm.size, - &rt_ctx->module); + error = wasmtime_module_new(rt_ctx->engine, (uint8_t *) wasm.data, + wasm.size, &rt_ctx->module); if (!rt_ctx->module) { nxt_wasmtime_err_msg(error, NULL, "failed to compile module"); return -1; @@ -386,10 +348,10 @@ nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) nxt_wasmtime_wasi_init(ctx); error = wasmtime_linker_module(rt_ctx->linker, rt_ctx->ctx, "", 0, - rt_ctx->module); + rt_ctx->module); if (error != NULL) { - nxt_wasmtime_err_msg(error, NULL, "failed to instantiate"); - return -1; + nxt_wasmtime_err_msg(error, NULL, "failed to instantiate"); + return -1; } err = nxt_wasmtime_get_function_exports(ctx); @@ -405,16 +367,14 @@ nxt_wasmtime_init(nxt_wasm_ctx_t *ctx) return 0; } - static void -nxt_wasmtime_destroy(const nxt_wasm_ctx_t *ctx) -{ - int i = 0; - wasmtime_val_t args[1] = { }; - nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; - const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_FREE].func; +nxt_wasmtime_destroy(const nxt_wasm_ctx_t *ctx) { + int i = 0; + wasmtime_val_t args[1] = {}; + nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx; + const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_FREE].func; - args[i].kind = WASMTIME_I32; + args[i].kind = WASMTIME_I32; args[i++].of.i32 = ctx->baddr_off; wasmtime_func_call(rt_ctx->ctx, func, args, i, NULL, 0, NULL); @@ -424,10 +384,9 @@ nxt_wasmtime_destroy(const nxt_wasm_ctx_t *ctx) wasm_engine_delete(rt_ctx->engine); } - -const nxt_wasm_operations_t nxt_wasm_ops = { - .init = nxt_wasmtime_init, - .destroy = nxt_wasmtime_destroy, - .exec_request = nxt_wasmtime_execute_request, - .exec_hook = nxt_wasmtime_execute_hook, +const nxt_wasm_operations_t nxt_wasm_ops = { + .init = nxt_wasmtime_init, + .destroy = nxt_wasmtime_destroy, + .exec_request = nxt_wasmtime_execute_request, + .exec_hook = nxt_wasmtime_execute_hook, }; diff --git a/src/wasm/nxt_wasm.c b/src/wasm/nxt_wasm.c index db79d6aee..b98636302 100644 --- a/src/wasm/nxt_wasm.c +++ b/src/wasm/nxt_wasm.c @@ -11,42 +11,39 @@ #include "nxt_wasm.h" -#define NXT_WASM_VERSION "0.1" +#define NXT_WASM_VERSION "0.1" -#define NXT_WASM_DO_HOOK(hook) nxt_wops->exec_hook(&nxt_wasm_ctx, hook); +#define NXT_WASM_DO_HOOK(hook) nxt_wops->exec_hook(&nxt_wasm_ctx, hook); -static uint32_t compat[] = { - NXT_VERNUM, NXT_DEBUG, +static uint32_t compat[] = { + NXT_VERNUM, + NXT_DEBUG, }; -static nxt_wasm_ctx_t nxt_wasm_ctx; +static nxt_wasm_ctx_t nxt_wasm_ctx; -static const nxt_wasm_operations_t *nxt_wops; +static const nxt_wasm_operations_t *nxt_wops; enum { - NXT_WASM_HTTP_OK = 200, - NXT_WASM_HTTP_ERROR = 500 + NXT_WASM_HTTP_OK = 200, + NXT_WASM_HTTP_ERROR = 500 }; - void -nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx) -{ +nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx) { nxt_unit_request_done(ctx->req, NXT_UNIT_OK); NXT_WASM_DO_HOOK(NXT_WASM_FH_RESPONSE_END); } - void -nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset) -{ +nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset) { size_t fields_len; unsigned int i; - nxt_wasm_response_fields_t *rh; + nxt_wasm_response_fields_t *rh; - rh = (nxt_wasm_response_fields_t *)(ctx->baddr + offset); + rh = (nxt_wasm_response_fields_t *) (ctx->baddr + offset); fields_len = 0; for (i = 0; i < rh->nfields; i++) { @@ -56,61 +53,57 @@ nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset) nxt_unit_response_init(ctx->req, ctx->status, rh->nfields, fields_len); for (i = 0; i < rh->nfields; i++) { - const char *name; - const char *val; + const char *name; + const char *val; - name = (const char *)rh + rh->fields[i].name_off; - val = (const char *)rh + rh->fields[i].value_off; + name = (const char *) rh + rh->fields[i].name_off; + val = (const char *) rh + rh->fields[i].value_off; - nxt_unit_response_add_field(ctx->req, name, rh->fields[i].name_len, - val, rh->fields[i].value_len); + nxt_unit_response_add_field(ctx->req, name, rh->fields[i].name_len, val, + rh->fields[i].value_len); } nxt_unit_response_send(ctx->req); } - void -nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset) -{ - nxt_wasm_response_t *resp; - nxt_unit_request_info_t *req = ctx->req; +nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset) { + nxt_wasm_response_t *resp; + nxt_unit_request_info_t *req = ctx->req; if (!nxt_unit_response_is_init(req)) { nxt_unit_response_init(req, ctx->status, 0, 0); } - resp = (nxt_wasm_response_t *)(nxt_wasm_ctx.baddr + offset); + resp = (nxt_wasm_response_t *) (nxt_wasm_ctx.baddr + offset); - nxt_unit_response_write(req, (const char *)resp->data, resp->size); + nxt_unit_response_write(req, (const char *) resp->data, resp->size); } - static void -nxt_wasm_request_handler(nxt_unit_request_info_t *req) -{ +nxt_wasm_request_handler(nxt_unit_request_info_t *req) { int err; size_t offset, read_bytes, content_sent, content_len; ssize_t bytes_read; - nxt_unit_field_t *sf, *sf_end; - nxt_unit_request_t *r; - nxt_wasm_request_t *wr; - nxt_wasm_http_field_t *df; + nxt_unit_field_t *sf, *sf_end; + nxt_unit_request_t *r; + nxt_wasm_request_t *wr; + nxt_wasm_http_field_t *df; NXT_WASM_DO_HOOK(NXT_WASM_FH_REQUEST_INIT); - wr = (nxt_wasm_request_t *)nxt_wasm_ctx.baddr; + wr = (nxt_wasm_request_t *) nxt_wasm_ctx.baddr; -#define SET_REQ_MEMBER(dmember, smember) \ - do { \ - const char *str = nxt_unit_sptr_get(&r->smember); \ - wr->dmember##_off = offset; \ - wr->dmember##_len = strlen(str); \ - memcpy((uint8_t *)wr + offset, str, wr->dmember##_len + 1); \ - offset += wr->dmember##_len + 1; \ +#define SET_REQ_MEMBER(dmember, smember) \ + do { \ + const char *str = nxt_unit_sptr_get(&r->smember); \ + wr->dmember##_off = offset; \ + wr->dmember##_len = strlen(str); \ + memcpy((uint8_t *) wr + offset, str, wr->dmember##_len + 1); \ + offset += wr->dmember##_len + 1; \ } while (0) - r = req->request; + r = req->request; offset = sizeof(nxt_wasm_request_t) + (r->fields_count * sizeof(nxt_wasm_http_field_t)); @@ -124,40 +117,41 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) SET_REQ_MEMBER(server_name, server_name); #undef SET_REQ_MEMBER - df = wr->fields; + df = wr->fields; sf_end = r->fields + r->fields_count; for (sf = r->fields; sf < sf_end; sf++) { - const char *name = nxt_unit_sptr_get(&sf->name); - const char *value = nxt_unit_sptr_get(&sf->value); + const char *name = nxt_unit_sptr_get(&sf->name); + const char *value = nxt_unit_sptr_get(&sf->value); df->name_off = offset; df->name_len = strlen(name); - memcpy((uint8_t *)wr + offset, name, df->name_len + 1); + memcpy((uint8_t *) wr + offset, name, df->name_len + 1); offset += df->name_len + 1; df->value_off = offset; df->value_len = strlen(value); - memcpy((uint8_t *)wr + offset, value, df->value_len + 1); + memcpy((uint8_t *) wr + offset, value, df->value_len + 1); offset += df->value_len + 1; df++; } - wr->tls = r->tls; - wr->nfields = r->fields_count; + wr->tls = r->tls; + wr->nfields = r->fields_count; wr->content_off = offset; wr->content_len = content_len = r->content_length; read_bytes = nxt_min(wr->content_len, NXT_WASM_MEM_SIZE - offset); - bytes_read = nxt_unit_request_read(req, (uint8_t *)wr + offset, read_bytes); + bytes_read + = nxt_unit_request_read(req, (uint8_t *) wr + offset, read_bytes); wr->content_sent = wr->total_content_sent = content_sent = bytes_read; wr->request_size = offset + bytes_read; nxt_wasm_ctx.status = NXT_WASM_HTTP_OK; - nxt_wasm_ctx.req = req; - err = nxt_wops->exec_request(&nxt_wasm_ctx); + nxt_wasm_ctx.req = req; + err = nxt_wops->exec_request(&nxt_wasm_ctx); if (err) { goto out_err_500; } @@ -168,15 +162,15 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) offset = sizeof(nxt_wasm_request_t); do { - read_bytes = nxt_min(content_len - content_sent, - NXT_WASM_MEM_SIZE - offset); - bytes_read = nxt_unit_request_read(req, (uint8_t *)wr + offset, - read_bytes); + read_bytes + = nxt_min(content_len - content_sent, NXT_WASM_MEM_SIZE - offset); + bytes_read + = nxt_unit_request_read(req, (uint8_t *) wr + offset, read_bytes); - content_sent += bytes_read; + content_sent += bytes_read; wr->request_size = wr->content_sent = bytes_read; - wr->total_content_sent = content_sent; - wr->content_off = offset; + wr->total_content_sent = content_sent; + wr->content_off = offset; err = nxt_wops->exec_request(&nxt_wasm_ctx); if (err) { @@ -194,14 +188,12 @@ nxt_wasm_request_handler(nxt_unit_request_info_t *req) NXT_WASM_DO_HOOK(NXT_WASM_FH_REQUEST_END); } - static nxt_int_t -nxt_wasm_start(nxt_task_t *task, nxt_process_data_t *data) -{ +nxt_wasm_start(nxt_task_t *task, nxt_process_data_t *data) { nxt_int_t ret; - nxt_unit_ctx_t *unit_ctx; + nxt_unit_ctx_t *unit_ctx; nxt_unit_init_t wasm_init; - nxt_common_app_conf_t *conf; + nxt_common_app_conf_t *conf; conf = data->app; @@ -224,7 +216,7 @@ nxt_wasm_start(nxt_task_t *task, nxt_process_data_t *data) NXT_WASM_DO_HOOK(NXT_WASM_FH_MODULE_END); if (nxt_wasm_ctx.dirs != NULL) { - char **p; + char **p; for (p = nxt_wasm_ctx.dirs; *p != NULL; p++) { nxt_free(*p); @@ -237,15 +229,13 @@ nxt_wasm_start(nxt_task_t *task, nxt_process_data_t *data) exit(EXIT_SUCCESS); } - static nxt_int_t nxt_wasm_setup(nxt_task_t *task, nxt_process_t *process, - nxt_common_app_conf_t *conf) -{ + nxt_common_app_conf_t *conf) { int n, i, err; - nxt_conf_value_t *dirs = NULL; - nxt_wasm_app_conf_t *c; - nxt_wasm_func_handler_t *fh; + nxt_conf_value_t *dirs = NULL; + nxt_wasm_app_conf_t *c; + nxt_wasm_func_handler_t *fh; static const nxt_str_t filesystem_str = nxt_string("filesystem"); c = &conf->u.wasm; @@ -257,14 +247,14 @@ nxt_wasm_setup(nxt_task_t *task, nxt_process_t *process, fh = nxt_wasm_ctx.fh; fh[NXT_WASM_FH_REQUEST].func_name = c->request_handler; - fh[NXT_WASM_FH_MALLOC].func_name = c->malloc_handler; - fh[NXT_WASM_FH_FREE].func_name = c->free_handler; + fh[NXT_WASM_FH_MALLOC].func_name = c->malloc_handler; + fh[NXT_WASM_FH_FREE].func_name = c->free_handler; /* Optional function handlers (hooks) */ - fh[NXT_WASM_FH_MODULE_INIT].func_name = c->module_init_handler; - fh[NXT_WASM_FH_MODULE_END].func_name = c->module_end_handler; + fh[NXT_WASM_FH_MODULE_INIT].func_name = c->module_init_handler; + fh[NXT_WASM_FH_MODULE_END].func_name = c->module_end_handler; fh[NXT_WASM_FH_REQUEST_INIT].func_name = c->request_init_handler; - fh[NXT_WASM_FH_REQUEST_END].func_name = c->request_end_handler; + fh[NXT_WASM_FH_REQUEST_END].func_name = c->request_end_handler; fh[NXT_WASM_FH_RESPONSE_END].func_name = c->response_end_handler; /* Get any directories to pass through to the WASM module */ @@ -284,7 +274,7 @@ nxt_wasm_setup(nxt_task_t *task, nxt_process_t *process, for (i = 0; i < n; i++) { nxt_str_t str; - nxt_conf_value_t *value; + nxt_conf_value_t *value; value = nxt_conf_get_array_element(dirs, i); nxt_conf_get_string(value, &str); @@ -302,14 +292,13 @@ nxt_wasm_setup(nxt_task_t *task, nxt_process_t *process, return NXT_OK; } - -NXT_EXPORT nxt_app_module_t nxt_app_module = { - .compat_length = sizeof(compat), - .compat = compat, - .type = nxt_string("wasm"), - .version = NXT_WASM_VERSION, - .mounts = NULL, - .nmounts = 0, - .setup = nxt_wasm_setup, - .start = nxt_wasm_start, +NXT_EXPORT nxt_app_module_t nxt_app_module = { + .compat_length = sizeof(compat), + .compat = compat, + .type = nxt_string("wasm"), + .version = NXT_WASM_VERSION, + .mounts = NULL, + .nmounts = 0, + .setup = nxt_wasm_setup, + .start = nxt_wasm_start, }; diff --git a/src/wasm/nxt_wasm.h b/src/wasm/nxt_wasm.h index 6bc3ae35a..aae0632c7 100644 --- a/src/wasm/nxt_wasm.h +++ b/src/wasm/nxt_wasm.h @@ -17,74 +17,74 @@ #endif -#define NXT_WASM_PAGE_SIZE (64 * 1024) -#define NXT_WASM_MEM_SIZE (32UL * 1024 * 1024) +#define NXT_WASM_PAGE_SIZE (64 * 1024) +#define NXT_WASM_MEM_SIZE (32UL * 1024 * 1024) #if defined(NXT_HAVE_WASM_WASMTIME) -typedef wasmtime_func_t nxt_wasm_func_t; +typedef wasmtime_func_t nxt_wasm_func_t; #endif -typedef struct nxt_wasm_http_field_s nxt_wasm_http_field_t; -typedef struct nxt_wasm_request_s nxt_wasm_request_t; -typedef struct nxt_wasm_response_s nxt_wasm_response_t; -typedef struct nxt_wasm_response_fields_s nxt_wasm_response_fields_t; -typedef enum nxt_wasm_fh_e nxt_wasm_fh_t; -typedef struct nxt_wasm_func_handler_s nxt_wasm_func_handler_t; -typedef struct nxt_wasm_ctx_s nxt_wasm_ctx_t; -typedef struct nxt_wasm_operations_s nxt_wasm_operations_t; +typedef struct nxt_wasm_http_field_s nxt_wasm_http_field_t; +typedef struct nxt_wasm_request_s nxt_wasm_request_t; +typedef struct nxt_wasm_response_s nxt_wasm_response_t; +typedef struct nxt_wasm_response_fields_s nxt_wasm_response_fields_t; +typedef enum nxt_wasm_fh_e nxt_wasm_fh_t; +typedef struct nxt_wasm_func_handler_s nxt_wasm_func_handler_t; +typedef struct nxt_wasm_ctx_s nxt_wasm_ctx_t; +typedef struct nxt_wasm_operations_s nxt_wasm_operations_t; struct nxt_wasm_http_field_s { - uint32_t name_off; - uint32_t name_len; - uint32_t value_off; - uint32_t value_len; + uint32_t name_off; + uint32_t name_len; + uint32_t value_off; + uint32_t value_len; }; struct nxt_wasm_request_s { - uint32_t method_off; - uint32_t method_len; - uint32_t version_off; - uint32_t version_len; - uint32_t path_off; - uint32_t path_len; - uint32_t query_off; - uint32_t query_len; - uint32_t remote_off; - uint32_t remote_len; - uint32_t local_addr_off; - uint32_t local_addr_len; - uint32_t local_port_off; - uint32_t local_port_len; - uint32_t server_name_off; - uint32_t server_name_len; - - uint64_t content_len; - uint64_t total_content_sent; - uint32_t content_sent; - uint32_t content_off; - - uint32_t request_size; - - uint32_t nfields; - - uint32_t tls; - - char __pad[4]; - - nxt_wasm_http_field_t fields[]; + uint32_t method_off; + uint32_t method_len; + uint32_t version_off; + uint32_t version_len; + uint32_t path_off; + uint32_t path_len; + uint32_t query_off; + uint32_t query_len; + uint32_t remote_off; + uint32_t remote_len; + uint32_t local_addr_off; + uint32_t local_addr_len; + uint32_t local_port_off; + uint32_t local_port_len; + uint32_t server_name_off; + uint32_t server_name_len; + + uint64_t content_len; + uint64_t total_content_sent; + uint32_t content_sent; + uint32_t content_off; + + uint32_t request_size; + + uint32_t nfields; + + uint32_t tls; + + char __pad[4]; + + nxt_wasm_http_field_t fields[]; }; struct nxt_wasm_response_s { - uint32_t size; + uint32_t size; - uint8_t data[]; + uint8_t data[]; }; struct nxt_wasm_response_fields_s { - uint32_t nfields; + uint32_t nfields; - nxt_wasm_http_field_t fields[]; + nxt_wasm_http_field_t fields[]; }; enum nxt_wasm_fh_e { @@ -103,40 +103,43 @@ enum nxt_wasm_fh_e { }; struct nxt_wasm_func_handler_s { - const char *func_name; - nxt_wasm_func_t func; + const char *func_name; + nxt_wasm_func_t func; }; struct nxt_wasm_ctx_s { - const char *module_path; + const char *module_path; - nxt_wasm_func_handler_t fh[NXT_WASM_FH_NR]; + nxt_wasm_func_handler_t fh[NXT_WASM_FH_NR]; - char **dirs; + char **dirs; - nxt_unit_request_info_t *req; + nxt_unit_request_info_t *req; - uint8_t *baddr; - size_t baddr_off; + uint8_t *baddr; + size_t baddr_off; - size_t response_off; + size_t response_off; - uint16_t status; + uint16_t status; }; struct nxt_wasm_operations_s { - int (*init)(nxt_wasm_ctx_t *ctx); - void (*destroy)(const nxt_wasm_ctx_t *ctx); - int (*exec_request)(const nxt_wasm_ctx_t *ctx); - void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook); + int (*init)(nxt_wasm_ctx_t *ctx); + void (*destroy)(const nxt_wasm_ctx_t *ctx); + int (*exec_request)(const nxt_wasm_ctx_t *ctx); + void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook); }; -extern const nxt_wasm_operations_t nxt_wasm_ops; +extern const nxt_wasm_operations_t nxt_wasm_ops; /* Exported to the WASM module */ -extern void nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx); -extern void nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset); -extern void nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset); - -#endif /* _NXT_WASM_H_INCLUDED_ */ +extern void +nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx); +extern void +nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset); +extern void +nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset); + +#endif /* _NXT_WASM_H_INCLUDED_ */