From ed093f1314807f55a472838eb82bb532768b0e79 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 22 Aug 2012 00:54:15 +0200 Subject: [PATCH] uv: upgrade to 564e7c7 --- deps/uv/include/uv-private/uv-darwin.h | 3 + deps/uv/include/uv-private/uv-unix.h | 5 + deps/uv/include/uv-private/uv-win.h | 1 + deps/uv/include/uv.h | 7 + deps/uv/src/unix/core.c | 12 +- deps/uv/src/unix/internal.h | 8 +- deps/uv/src/unix/linux/syscalls.c | 4 +- deps/uv/src/unix/linux/syscalls.h | 2 +- deps/uv/src/unix/process.c | 18 ++ deps/uv/src/unix/stream.c | 217 +++++++++++++++++++++++++ deps/uv/src/win/core.c | 3 + deps/uv/src/win/signal.c | 8 + deps/uv/src/win/tty.c | 41 ++++- 13 files changed, 317 insertions(+), 12 deletions(-) diff --git a/deps/uv/include/uv-private/uv-darwin.h b/deps/uv/include/uv-private/uv-darwin.h index 93f2ca4e78..7f1b928a64 100644 --- a/deps/uv/include/uv-private/uv-darwin.h +++ b/deps/uv/include/uv-private/uv-darwin.h @@ -34,4 +34,7 @@ int fflags; \ int fd; \ +#define UV_STREAM_PRIVATE_PLATFORM_FIELDS \ + void* select; \ + #endif /* UV_DARWIN_H */ diff --git a/deps/uv/include/uv-private/uv-unix.h b/deps/uv/include/uv-private/uv-unix.h index 560889e177..6cdea67245 100644 --- a/deps/uv/include/uv-private/uv-unix.h +++ b/deps/uv/include/uv-private/uv-unix.h @@ -79,6 +79,10 @@ struct uv__io_s { # define UV_PLATFORM_FS_EVENT_FIELDS /* empty */ #endif +#ifndef UV_STREAM_PRIVATE_PLATFORM_FIELDS +# define UV_STREAM_PRIVATE_PLATFORM_FIELDS /* empty */ +#endif + /* Note: May be cast to struct iovec. See writev(2). */ typedef struct { char* base; @@ -178,6 +182,7 @@ typedef struct { int delayed_error; \ int accepted_fd; \ int fd; \ + UV_STREAM_PRIVATE_PLATFORM_FIELDS \ #define UV_TCP_PRIVATE_FIELDS \ uv_idle_t* idle_handle; /* for UV_TCP_SINGLE_ACCEPT handles */ \ diff --git a/deps/uv/include/uv-private/uv-win.h b/deps/uv/include/uv-private/uv-win.h index dbbecedc9d..ad27ee5187 100644 --- a/deps/uv/include/uv-private/uv-win.h +++ b/deps/uv/include/uv-private/uv-win.h @@ -65,6 +65,7 @@ typedef intptr_t ssize_t; */ #define SIGHUP 1 #define SIGKILL 9 +#define SIGWINCH 28 /* * Guids and typedefs for winsock extension functions diff --git a/deps/uv/include/uv.h b/deps/uv/include/uv.h index 66ee41cb95..0e90a392e8 100644 --- a/deps/uv/include/uv.h +++ b/deps/uv/include/uv.h @@ -1608,6 +1608,13 @@ UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle); * program is given approximately 10 seconds to perform cleanup. After that * Windows will unconditionally terminate it. * + * SIGWINCH is raised whenever libuv detects that the console has been + * resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t + * handle to write to the console. SIGWINCH may not always be delivered in a + * timely manner; libuv will only detect size changes when the cursor is + * being moved. When a readable uv_tty_handle is used in raw mode, resizing + * the console buffer will also trigger a SIGWINCH signal. + * * Watchers for other signals can be successfully created, but these signals * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV, * SIGTERM and SIGKILL. diff --git a/deps/uv/src/unix/core.c b/deps/uv/src/unix/core.c index 7b2617223c..85d5f16449 100644 --- a/deps/uv/src/unix/core.c +++ b/deps/uv/src/unix/core.c @@ -49,10 +49,14 @@ #ifdef __APPLE__ # include /* _NSGetExecutablePath */ +# include +# include #endif #ifdef __FreeBSD__ # include +# include +# include # include #endif @@ -459,7 +463,7 @@ int uv__accept(int sockfd) { while (1) { #if __linux__ - static int no_accept4; + static __read_mostly int no_accept4; if (no_accept4) goto skip; @@ -503,7 +507,7 @@ skip: } -#if __linux__ +#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) int uv__nonblock(int fd, int set) { int r; @@ -526,7 +530,7 @@ int uv__cloexec(int fd, int set) { return r; } -#else /* !__linux__ */ +#else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */ int uv__nonblock(int fd, int set) { int flags; @@ -575,7 +579,7 @@ int uv__cloexec(int fd, int set) { return r; } -#endif /* __linux__ */ +#endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */ /* This function is not execve-safe, there is a race window diff --git a/deps/uv/src/unix/internal.h b/deps/uv/src/unix/internal.h index 6e16b2c65d..e2668109db 100644 --- a/deps/uv/src/unix/internal.h +++ b/deps/uv/src/unix/internal.h @@ -28,7 +28,13 @@ #include #include /* abort */ -#if __STRICT_ANSI__ +#if defined(__GNUC__) +# define __read_mostly __attribute__((__section__(".data.read_mostly"))) +#else +# define __read_mostly +#endif + +#if defined(__STRICT_ANSI__) # define inline __inline #endif diff --git a/deps/uv/src/unix/linux/syscalls.c b/deps/uv/src/unix/linux/syscalls.c index a99a6da883..6536215b18 100644 --- a/deps/uv/src/unix/linux/syscalls.c +++ b/deps/uv/src/unix/linux/syscalls.c @@ -247,9 +247,9 @@ int uv__eventfd2(unsigned int count, int flags) { } -int uv__epoll_create(void) { +int uv__epoll_create(int size) { #if __NR_epoll_create - return syscall(__NR_epoll_create); + return syscall(__NR_epoll_create, size); #else return errno = ENOSYS, -1; #endif diff --git a/deps/uv/src/unix/linux/syscalls.h b/deps/uv/src/unix/linux/syscalls.h index 527d8c5724..71edde9c34 100644 --- a/deps/uv/src/unix/linux/syscalls.h +++ b/deps/uv/src/unix/linux/syscalls.h @@ -89,7 +89,7 @@ struct uv__mmsghdr { int uv__accept4(int fd, struct sockaddr* addr, socklen_t* addrlen, int flags); int uv__eventfd(unsigned int count); -int uv__epoll_create(void); +int uv__epoll_create(int size); int uv__epoll_create1(int flags); int uv__epoll_ctl(int epfd, int op, int fd, struct uv__epoll_event *ev); int uv__epoll_wait(int epfd, diff --git a/deps/uv/src/unix/process.c b/deps/uv/src/unix/process.c index 67f2d7316a..829f79aafd 100644 --- a/deps/uv/src/unix/process.c +++ b/deps/uv/src/unix/process.c @@ -117,6 +117,11 @@ static void uv__chld(uv_signal_t* handle, int signum) { int uv__make_socketpair(int fds[2], int flags) { #if __linux__ + static __read_mostly int no_cloexec; + + if (no_cloexec) + goto skip; + if (socketpair(AF_UNIX, SOCK_STREAM | UV__SOCK_CLOEXEC | flags, 0, fds) == 0) return 0; @@ -125,6 +130,10 @@ int uv__make_socketpair(int fds[2], int flags) { */ if (errno != EINVAL) return -1; + + no_cloexec = 1; + +skip: #endif if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) @@ -144,11 +153,20 @@ int uv__make_socketpair(int fds[2], int flags) { int uv__make_pipe(int fds[2], int flags) { #if __linux__ + static __read_mostly int no_pipe2; + + if (no_pipe2) + goto skip; + if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0) return 0; if (errno != ENOSYS) return -1; + + no_pipe2 = 1; + +skip: #endif if (pipe(fds)) diff --git a/deps/uv/src/unix/stream.c b/deps/uv/src/unix/stream.c index 0ab86f3a3f..f9fc7195d1 100644 --- a/deps/uv/src/unix/stream.c +++ b/deps/uv/src/unix/stream.c @@ -34,6 +34,27 @@ #include #include +#if defined(__APPLE__) +# include +# include +# include + +/* ev.h is overwriting EV_ERROR from sys/event.h */ +#define EV_ERROR_ORIG 0x4000 + +/* Forward declaration */ +typedef struct uv__stream_select_s uv__stream_select_t; + +struct uv__stream_select_s { + uv_stream_t* stream; + uv_thread_t thread; + uv_sem_t sem; + uv_mutex_t mutex; + uv_async_t async; + int events; + int fake_fd; +}; +#endif /* defined(__APPLE__) */ static void uv__stream_connect(uv_stream_t*); static void uv__write(uv_stream_t* stream); @@ -69,11 +90,182 @@ void uv__stream_init(uv_loop_t* loop, ngx_queue_init(&stream->write_completed_queue); stream->write_queue_size = 0; +#if defined(__APPLE__) + stream->select = NULL; +#endif /* defined(__APPLE_) */ + uv__io_init(&stream->read_watcher, uv__stream_io, -1, 0); uv__io_init(&stream->write_watcher, uv__stream_io, -1, 0); } +#if defined(__APPLE__) +void uv__stream_osx_select(void* arg) { + uv_stream_t* stream; + uv__stream_select_t* s; + fd_set read; + fd_set write; + fd_set error; + struct timeval timeout; + int events; + int fd; + int r; + + stream = arg; + s = stream->select; + fd = stream->fd; + + while (1) { + /* Terminate on semaphore */ + if (uv_sem_trywait(&s->sem) == 0) break; + + /* Watch fd using select(2) */ + FD_ZERO(&read); + FD_ZERO(&write); + FD_ZERO(&error); + FD_SET(fd, &read); + FD_SET(fd, &write); + FD_SET(fd, &error); + + timeout.tv_sec = 0; + timeout.tv_usec = 250000; /* 250 ms timeout */ + r = select(fd + 1, &read, &write, &error, &timeout); + if (r == -1) { + if (errno == EINTR) continue; + /* XXX: Possible?! */ + abort(); + } + + /* Ignore timeouts */ + if (r == 0) continue; + + /* Handle events */ + events = 0; + if (FD_ISSET(fd, &read)) events |= UV__IO_READ; + if (FD_ISSET(fd, &write)) events |= UV__IO_WRITE; + if (FD_ISSET(fd, &error)) events |= UV__IO_ERROR; + + uv_mutex_lock(&s->mutex); + s->events |= events; + uv_mutex_unlock(&s->mutex); + + if (events != 0) uv_async_send(&s->async); + } +} + + +void uv__stream_osx_select_cb(uv_async_t* handle, int status) { + uv_stream_t* stream; + uv__stream_select_t* s; + int events; + + s = container_of(handle, uv__stream_select_t, async); + stream = s->stream; + + /* Get and reset stream's events */ + uv_mutex_lock(&s->mutex); + events = s->events; + s->events = 0; + uv_mutex_unlock(&s->mutex); + + /* Invoke callback on event-loop */ + if ((events & UV__IO_READ) && uv__io_active(&stream->read_watcher)) { + uv__stream_io(stream->loop, &stream->read_watcher, UV__IO_READ); + } + if ((events & UV__IO_WRITE) && uv__io_active(&stream->write_watcher)) { + uv__stream_io(stream->loop, &stream->write_watcher, UV__IO_WRITE); + } + if (events & UV__IO_ERROR) { + /* XXX: Handle it! */ + uv__stream_io(stream->loop, NULL, UV__IO_ERROR); + } +} + + +void uv__stream_osx_cb_close(uv_handle_t* async) { + /* Free container */ + free(container_of(async, uv__stream_select_t, async)); +} + + +int uv__stream_try_select(uv_stream_t* stream, int fd) { + /* + * kqueue doesn't work with some files from /dev mount on osx. + * select(2) in separate thread for those fds + */ + + int kq; + int ret; + struct kevent filter[1]; + struct kevent events[1]; + struct timespec timeout; + uv__stream_select_t* s; + + kq = kqueue(); + if (kq < 0) { + fprintf(stderr, "(libuv) Failed to create kqueue (%d)\n", errno); + abort(); + } + + EV_SET(&filter[0], fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0); + + /* Use small timeout, because we only want to capture EINVALs */ + timeout.tv_sec = 0; + timeout.tv_nsec = 1; + + ret = kevent(kq, filter, 1, events, 1, &timeout); + close(kq); + if (ret < 1) return -1; + if ((events[0].flags & EV_ERROR_ORIG) == 0 || events[0].data != EINVAL) { + return -1; + } + + /* At this point we definitely know that this fd won't work with kqueue */ + s = malloc(sizeof(*s)); + if (s == NULL) { + /* TODO: Return error */ + abort(); + } + + if (uv_async_init(stream->loop, + &s->async, + uv__stream_osx_select_cb)) { + return -1; + } + s->async.flags |= UV__HANDLE_INTERNAL; + uv__handle_unref((uv_handle_t*) &s->async); + + if (uv_sem_init(&s->sem, 0)) goto fatal1; + if (uv_mutex_init(&s->mutex)) goto fatal2; + + /* Create fake fd for io watcher */ + s->fake_fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (s->fake_fd == -1) goto fatal3; + + if (uv_thread_create(&s->thread, uv__stream_osx_select, stream)) { + goto fatal4; + } + + s->stream = stream; + stream->select = s; + + return 0; + +fatal4: + close(s->fake_fd); +fatal3: + uv_mutex_destroy(&s->mutex); +fatal2: + uv_sem_destroy(&s->sem); +fatal1: + uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close); + + free(s); + return -1; +} +#endif /* defined(__APPLE__) */ + + int uv__stream_open(uv_stream_t* stream, int fd, int flags) { socklen_t yes; @@ -102,6 +294,13 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) { } } +#if defined(__APPLE__) + if (uv__stream_try_select(stream, fd) == 0) { + /* Use fake fd */ + fd = ((uv__stream_select_t*) stream->select)->fake_fd; + } +#endif /* defined(__APPLE__) */ + /* Associate the fd with each watcher. */ uv__io_set(&stream->read_watcher, uv__stream_io, fd, UV__IO_READ); uv__io_set(&stream->write_watcher, uv__stream_io, fd, UV__IO_WRITE); @@ -980,6 +1179,24 @@ int uv_is_writable(const uv_stream_t* stream) { void uv__stream_close(uv_stream_t* handle) { +#if defined(__APPLE__) + /* Terminate select loop first */ + if (handle->select != NULL) { + uv__stream_select_t* s; + + s = handle->select; + + uv_sem_post(&s->sem); + uv_thread_join(&s->thread); + uv_sem_destroy(&s->sem); + uv_mutex_destroy(&s->mutex); + close(s->fake_fd); + uv_close((uv_handle_t*) &s->async, uv__stream_osx_cb_close); + + handle->select = NULL; + } +#endif /* defined(__APPLE__) */ + uv_read_stop(handle); uv__io_stop(handle->loop, &handle->write_watcher); diff --git a/deps/uv/src/win/core.c b/deps/uv/src/win/core.c index 2c76b10009..dde1348ab7 100644 --- a/deps/uv/src/win/core.c +++ b/deps/uv/src/win/core.c @@ -73,6 +73,9 @@ static void uv_loop_init(uv_loop_t* loop) { uv_fatal_error(GetLastError(), "CreateIoCompletionPort"); } + /* To prevent uninitialized memory access, loop->time must be intialized */ + /* to zero before calling uv_update_time for the first time. */ + loop->time = 0; uv_update_time(loop); ngx_queue_init(&loop->handle_queue); diff --git a/deps/uv/src/win/signal.c b/deps/uv/src/win/signal.c index 3407c6c68f..e35e1ff959 100644 --- a/deps/uv/src/win/signal.c +++ b/deps/uv/src/win/signal.c @@ -169,6 +169,10 @@ static uv_err_t uv__signal_register(int signum) { case SIGHUP: return uv__signal_register_control_handler(); + case SIGWINCH: + /* SIGWINCH is generated in tty.c. No need to register anything. */ + return uv_ok_; + case SIGILL: case SIGABRT_COMPAT: case SIGFPE: @@ -193,6 +197,10 @@ static void uv__signal_unregister(int signum) { uv__signal_unregister_control_handler(); return; + case SIGWINCH: + /* SIGWINCH is generated in tty.c. No need to unregister anything. */ + return; + case SIGILL: case SIGABRT_COMPAT: case SIGFPE: diff --git a/deps/uv/src/win/tty.c b/deps/uv/src/win/tty.c index 8575f4f7e2..af828cf8f1 100644 --- a/deps/uv/src/win/tty.c +++ b/deps/uv/src/win/tty.c @@ -82,6 +82,8 @@ static int uv_tty_virtual_width = -1; static CRITICAL_SECTION uv_tty_output_lock; +static HANDLE uv_tty_output_handle = INVALID_HANDLE_VALUE; + void uv_console_init() { InitializeCriticalSection(&uv_tty_output_lock); @@ -113,10 +115,17 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) { return -1; } - /* Update the virtual window. We must hold the tty_output_lock because the */ - /* virtual window state is shared between all uv_tty handles. */ + /* Obtain the the tty_output_lock because the virtual window state is */ + /* shared between all uv_tty_t handles. */ EnterCriticalSection(&uv_tty_output_lock); + + /* Store the global tty output handle. This handle is used by TTY read */ + /* streams to update the virtual window when a CONSOLE_BUFFER_SIZE_EVENT */ + /* is received. */ + uv_tty_output_handle = handle; + uv_tty_update_virtual_window(&screen_buffer_info); + LeaveCriticalSection(&uv_tty_output_lock); } @@ -513,7 +522,20 @@ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle, } records_left--; - /* Ignore events that are not keyboard events */ + /* If the window was resized, recompute the virtual window size. This */ + /* will trigger a SIGWINCH signal if the window size changed in an */ + /* way that matters to libuv. */ + if (handle->last_input_record.EventType == WINDOW_BUFFER_SIZE_EVENT) { + CONSOLE_SCREEN_BUFFER_INFO info; + if (uv_tty_output_handle == INVALID_HANDLE_VALUE) + continue; + if (!GetConsoleScreenBufferInfo(uv_tty_output_handle, &info)) + continue; + uv_tty_update_virtual_window(&info); + continue; + } + + /* Ignore other events that are not key or resize events. */ if (handle->last_input_record.EventType != KEY_EVENT) { continue; } @@ -835,8 +857,11 @@ int uv_tty_read_stop(uv_tty_t* handle) { static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) { - uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1; + int old_virtual_width = uv_tty_virtual_width; + int old_virtual_height = uv_tty_virtual_height; + uv_tty_virtual_width = info->dwSize.X; + uv_tty_virtual_height = info->srWindow.Bottom - info->srWindow.Top + 1; /* Recompute virtual window offset row. */ if (uv_tty_virtual_offset == -1) { @@ -854,6 +879,14 @@ static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info) { if (uv_tty_virtual_offset < 0) { uv_tty_virtual_offset = 0; } + + /* If the virtual window size changed, emit a SIGWINCH signal. Don't emit */ + /* if this was the first time the virtual window size was computed. */ + if (old_virtual_width != -1 && old_virtual_height != -1 && + (uv_tty_virtual_width != old_virtual_width || + uv_tty_virtual_height != old_virtual_height)) { + uv__signal_dispatch(SIGWINCH); + } }