Nginx: fix crash when server configuration item [`kernel_network_stack`] is on and uses `proxy_pass`.

When nginx is configured like this:
```
server {
    listen       8000;
    kernel_network_stack on;
    location / {
        proxy_pass http://127.0.0.1:8080/;
    }
}
```
nginx will crash, becasue kernel network stack is handled in a single thread, but we have hijacked all the socket apis, it causes that all apis enter to f-stack's path which is in main thread.
This commit is contained in:
logwang 2018-01-11 18:03:24 +08:00
parent e9cbb9895c
commit e340d433ea
3 changed files with 29 additions and 24 deletions

View File

@ -10,6 +10,12 @@
#include <ngx_event.h> #include <ngx_event.h>
#if (NGX_HAVE_FSTACK)
extern int fstack_territory(int domain, int type, int protocol);
extern int is_fstack_fd(int sockfd);
#endif
ngx_os_io_t ngx_io; ngx_os_io_t ngx_io;
@ -376,11 +382,6 @@ ngx_set_inherited_sockets(ngx_cycle_t *cycle)
return NGX_OK; return NGX_OK;
} }
#if (NGX_HAVE_FSTACK)
extern int
fstack_territory(int domain, int type, int protocol);
#endif
ngx_int_t ngx_int_t
ngx_open_listening_sockets(ngx_cycle_t *cycle) ngx_open_listening_sockets(ngx_cycle_t *cycle)
{ {
@ -1166,6 +1167,10 @@ ngx_get_connection(ngx_socket_t s, ngx_log_t *log)
wev->write = 1; wev->write = 1;
#if (NGX_HAVE_FSTACK)
rev->belong_to_host = wev->belong_to_host = is_fstack_fd(s) ? 0 : 1;
#endif
return c; return c;
} }

View File

@ -119,7 +119,7 @@ static int (*real_ioctl)(int, int, void *);
static int (*real_gettimeofday)(struct timeval *tv, struct timezone *tz); static int (*real_gettimeofday)(struct timeval *tv, struct timezone *tz);
static int inited; static __thread int inited;
#define SYSCALL(func) \ #define SYSCALL(func) \
({ \ ({ \
@ -151,7 +151,7 @@ static inline int restore_fstack_fd(int sockfd) {
} }
/* Tell whether a 'sockfd' belongs to fstack. */ /* Tell whether a 'sockfd' belongs to fstack. */
static inline int is_fstack_fd(int sockfd) { int is_fstack_fd(int sockfd) {
if (unlikely(inited == 0)) { if (unlikely(inited == 0)) {
return 0; return 0;
} }

View File

@ -783,7 +783,7 @@ ngx_event_process_init(ngx_cycle_t *cycle)
#if (NGX_HAVE_FSTACK) #if (NGX_HAVE_FSTACK)
/* Note when nginx running on fstack, /* Note when nginx running on fstack,
make sure that add the right fd to kqueue !! */ make sure that add the right fd to kqueue !! */
c->read->belong_to_host = c->write->belong_to_host = ls[i].belong_to_host; c->read->belong_to_host = c->write->belong_to_host = ls[i].belong_to_host;
#endif #endif
#if (NGX_HAVE_DEFERRED_ACCEPT) #if (NGX_HAVE_DEFERRED_ACCEPT)