From 7397ab2cf13555a9f0aa3ccc50f9e76fc54fdcec Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 17 Jul 2012 11:40:13 -0700 Subject: [PATCH] uv: Upgrade to a9f6f06 --- deps/uv/config-unix.mk | 4 ++ deps/uv/include/uv-private/uv-unix.h | 13 +++- deps/uv/src/unix/darwin.c | 27 -------- deps/uv/src/unix/freebsd.c | 16 ++++- deps/uv/src/unix/linux/linux-core.c | 72 ---------------------- deps/uv/src/unix/openbsd.c | 30 --------- deps/uv/src/unix/proctitle.c | 92 ++++++++++++++++++++++++++++ deps/uv/src/unix/sunos.c | 18 ------ deps/uv/src/unix/thread.c | 40 ++++++++++++ deps/uv/test/test-process-title.c | 13 +++- deps/uv/uv.gyp | 3 + 11 files changed, 176 insertions(+), 152 deletions(-) create mode 100644 deps/uv/src/unix/proctitle.c diff --git a/deps/uv/config-unix.mk b/deps/uv/config-unix.mk index 0581b5114c..231d884434 100644 --- a/deps/uv/config-unix.mk +++ b/deps/uv/config-unix.mk @@ -128,6 +128,10 @@ else RUNNER_LINKFLAGS += -pthread endif +ifneq (FreeBSD,$(uname_S)) +OBJS += src/unix/proctitle.o +endif + RUNNER_LIBS= RUNNER_SRC=test/runner-unix.c diff --git a/deps/uv/include/uv-private/uv-unix.h b/deps/uv/include/uv-private/uv-unix.h index da185e29da..7b86c0ea6d 100644 --- a/deps/uv/include/uv-private/uv-unix.h +++ b/deps/uv/include/uv-private/uv-unix.h @@ -40,10 +40,17 @@ #include #include -#include #include #include +#if defined(__APPLE__) && defined(__MACH__) +# include +# include +# include +#else +# include +#endif + #if __sun # include # include @@ -67,7 +74,11 @@ typedef pthread_once_t uv_once_t; typedef pthread_t uv_thread_t; typedef pthread_mutex_t uv_mutex_t; typedef pthread_rwlock_t uv_rwlock_t; +#if defined(__APPLE__) && defined(__MACH__) +typedef semaphore_t uv_sem_t; +#else typedef sem_t uv_sem_t; +#endif /* Platform-specific definitions for uv_spawn support. */ typedef gid_t uv_gid_t; diff --git a/deps/uv/src/unix/darwin.c b/deps/uv/src/unix/darwin.c index e6deb3017b..b3cd01221b 100644 --- a/deps/uv/src/unix/darwin.c +++ b/deps/uv/src/unix/darwin.c @@ -41,8 +41,6 @@ #include #include /* sysconf */ -static char *process_title; - #if TARGET_OS_IPHONE /* see: http://developer.apple.com/library/mac/#qa/qa1398/_index.html */ uint64_t uv_hrtime() { @@ -138,31 +136,6 @@ void uv_loadavg(double avg[3]) { } -char** uv_setup_args(int argc, char** argv) { - process_title = argc ? strdup(argv[0]) : NULL; - return argv; -} - - -uv_err_t uv_set_process_title(const char* title) { - /* TODO implement me */ - return uv__new_artificial_error(UV_ENOSYS); -} - - -uv_err_t uv_get_process_title(char* buffer, size_t size) { - if (process_title) { - strncpy(buffer, process_title, size); - } else { - if (size > 0) { - buffer[0] = '\0'; - } - } - - return uv_ok_; -} - - uv_err_t uv_resident_set_memory(size_t* rss) { struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; diff --git a/deps/uv/src/unix/freebsd.c b/deps/uv/src/unix/freebsd.c index f6f441ff16..be8006c5a8 100644 --- a/deps/uv/src/unix/freebsd.c +++ b/deps/uv/src/unix/freebsd.c @@ -139,9 +139,23 @@ char** uv_setup_args(int argc, char** argv) { uv_err_t uv_set_process_title(const char* title) { + int oid[4]; + if (process_title) free(process_title); process_title = strdup(title); - setproctitle(title); + + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + + sysctl(oid, + ARRAY_SIZE(oid), + NULL, + NULL, + process_title, + strlen(process_title) + 1); + return uv_ok_; } diff --git a/deps/uv/src/unix/linux/linux-core.c b/deps/uv/src/unix/linux/linux-core.c index 34f48a94b9..9cde6a1e14 100644 --- a/deps/uv/src/unix/linux/linux-core.c +++ b/deps/uv/src/unix/linux/linux-core.c @@ -58,11 +58,6 @@ static char buf[MAXPATHLEN + 1]; -static struct { - char *str; - size_t len; -} process_title; - /* * There's probably some way to get time from Linux than gettimeofday(). What @@ -112,73 +107,6 @@ uint64_t uv_get_total_memory(void) { } -char** uv_setup_args(int argc, char** argv) { - char **new_argv; - char **new_env; - size_t size; - int envc; - char *s; - int i; - - for (envc = 0; environ[envc]; envc++); - - s = envc ? environ[envc - 1] : argv[argc - 1]; - - process_title.str = argv[0]; - process_title.len = s + strlen(s) + 1 - argv[0]; - - size = process_title.len; - size += (argc + 1) * sizeof(char **); - size += (envc + 1) * sizeof(char **); - - if ((s = (char *) malloc(size)) == NULL) { - process_title.str = NULL; - process_title.len = 0; - return argv; - } - - new_argv = (char **) s; - new_env = new_argv + argc + 1; - s = (char *) (new_env + envc + 1); - memcpy(s, process_title.str, process_title.len); - - for (i = 0; i < argc; i++) - new_argv[i] = s + (argv[i] - argv[0]); - new_argv[argc] = NULL; - - s += environ[0] - argv[0]; - - for (i = 0; i < envc; i++) - new_env[i] = s + (environ[i] - environ[0]); - new_env[envc] = NULL; - - environ = new_env; - return new_argv; -} - - -uv_err_t uv_set_process_title(const char* title) { - /* No need to terminate, last char is always '\0'. */ - if (process_title.len) - strncpy(process_title.str, title, process_title.len - 1); - - return uv_ok_; -} - - -uv_err_t uv_get_process_title(char* buffer, size_t size) { - if (process_title.str) { - strncpy(buffer, process_title.str, size); - } else { - if (size > 0) { - buffer[0] = '\0'; - } - } - - return uv_ok_; -} - - uv_err_t uv_resident_set_memory(size_t* rss) { FILE* f; int itmp; diff --git a/deps/uv/src/unix/openbsd.c b/deps/uv/src/unix/openbsd.c index 865f8e9e6a..0c5d81628a 100644 --- a/deps/uv/src/unix/openbsd.c +++ b/deps/uv/src/unix/openbsd.c @@ -40,9 +40,6 @@ #define NANOSEC ((uint64_t) 1e9) -static char *process_title; - - uint64_t uv_hrtime(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -137,33 +134,6 @@ uint64_t uv_get_total_memory(void) { } -char** uv_setup_args(int argc, char** argv) { - process_title = argc ? strdup(argv[0]) : NULL; - return argv; -} - - -uv_err_t uv_set_process_title(const char* title) { - if (process_title) free(process_title); - process_title = strdup(title); - setproctitle(title); - return uv_ok_; -} - - -uv_err_t uv_get_process_title(char* buffer, size_t size) { - if (process_title) { - strncpy(buffer, process_title, size); - } else { - if (size > 0) { - buffer[0] = '\0'; - } - } - - return uv_ok_; -} - - uv_err_t uv_resident_set_memory(size_t* rss) { kvm_t *kd = NULL; struct kinfo_proc *kinfo = NULL; diff --git a/deps/uv/src/unix/proctitle.c b/deps/uv/src/unix/proctitle.c new file mode 100644 index 0000000000..2909971022 --- /dev/null +++ b/deps/uv/src/unix/proctitle.c @@ -0,0 +1,92 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "internal.h" + +#include +#include +#include + +/* NOTE: FreeBSD is using it's own implementation of those functions */ + +static struct { + char *str; + size_t len; +} process_title; + +extern char** environ; + + +char** uv_setup_args(int argc, char** argv) { + char **new_argv; + char **new_env; + size_t size; + int envc; + char *s; + int i; + + for (envc = 0; environ[envc]; envc++); + + s = envc ? environ[envc - 1] : argv[argc - 1]; + + process_title.str = argv[0]; + process_title.len = s + strlen(s) + 1 - argv[0]; + + size = process_title.len; + size += (argc + 1) * sizeof(char **); + size += (envc + 1) * sizeof(char **); + + if ((s = (char *) malloc(size)) == NULL) { + process_title.str = NULL; + process_title.len = 0; + return argv; + } + + new_argv = (char **) s; + new_env = new_argv + argc + 1; + s = (char *) (new_env + envc + 1); + memcpy(s, process_title.str, process_title.len); + + for (i = 0; i < argc; i++) + new_argv[i] = s + (argv[i] - argv[0]); + new_argv[argc] = NULL; + + s += environ[0] - argv[0]; + + for (i = 0; i < envc; i++) + new_env[i] = s + (environ[i] - environ[0]); + new_env[envc] = NULL; + + environ = new_env; + return new_argv; +} + + +uv_err_t uv_set_process_title(const char* title) { + uv_strlcpy(process_title.str, title, process_title.len); + return uv_ok_; +} + + +uv_err_t uv_get_process_title(char* buffer, size_t size) { + uv_strlcpy(buffer, process_title.str ? process_title.str : "", size); + return uv_ok_; +} diff --git a/deps/uv/src/unix/sunos.c b/deps/uv/src/unix/sunos.c index b95a89b456..1a439d94f4 100644 --- a/deps/uv/src/unix/sunos.c +++ b/deps/uv/src/unix/sunos.c @@ -238,24 +238,6 @@ void uv__fs_event_close(uv_fs_event_t* handle) { #endif /* HAVE_PORTS_FS */ -char** uv_setup_args(int argc, char** argv) { - return argv; -} - - -uv_err_t uv_set_process_title(const char* title) { - return uv_ok_; -} - - -uv_err_t uv_get_process_title(char* buffer, size_t size) { - if (size > 0) { - buffer[0] = '\0'; - } - return uv_ok_; -} - - uv_err_t uv_resident_set_memory(size_t* rss) { psinfo_t psinfo; uv_err_t err; diff --git a/deps/uv/src/unix/thread.c b/deps/uv/src/unix/thread.c index cd4e3333b5..a267d337d7 100644 --- a/deps/uv/src/unix/thread.c +++ b/deps/uv/src/unix/thread.c @@ -167,6 +167,44 @@ void uv_once(uv_once_t* guard, void (*callback)(void)) { abort(); } +#if defined(__APPLE__) && defined(__MACH__) + +int uv_sem_init(uv_sem_t* sem, unsigned int value) { + return semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, value); +} + + +void uv_sem_destroy(uv_sem_t* sem) { + if (semaphore_destroy(mach_task_self(), *sem)) + abort(); +} + + +void uv_sem_post(uv_sem_t* sem) { + if (semaphore_signal(*sem)) + abort(); +} + + +void uv_sem_wait(uv_sem_t* sem) { + if (semaphore_wait(*sem)) + abort(); +} + + +int uv_sem_trywait(uv_sem_t* sem) { + mach_timespec_t interval; + + interval.tv_sec = 0; + interval.tv_nsec = 0; + + if (semaphore_timedwait(*sem, interval) == KERN_SUCCESS) + return 0; + else + return -1; +} + +#else /* !(defined(__APPLE__) && defined(__MACH__)) */ int uv_sem_init(uv_sem_t* sem, unsigned int value) { return sem_init(sem, 0, value); @@ -209,3 +247,5 @@ int uv_sem_trywait(uv_sem_t* sem) { return r; } + +#endif /* defined(__APPLE__) && defined(__MACH__) */ diff --git a/deps/uv/test/test-process-title.c b/deps/uv/test/test-process-title.c index 59fceda31b..13d9dddfc4 100644 --- a/deps/uv/test/test-process-title.c +++ b/deps/uv/test/test-process-title.c @@ -23,20 +23,27 @@ #include "task.h" #include -TEST_IMPL(process_title) { + +static void set_title(const char* title) { char buffer[512]; uv_err_t err; err = uv_get_process_title(buffer, sizeof(buffer)); ASSERT(UV_OK == err.code); - err = uv_set_process_title("new title"); + err = uv_set_process_title(title); ASSERT(UV_OK == err.code); err = uv_get_process_title(buffer, sizeof(buffer)); ASSERT(UV_OK == err.code); - ASSERT(strcmp(buffer, "new title") == 0); + ASSERT(strcmp(buffer, title) == 0); +} + +TEST_IMPL(process_title) { + /* Check for format string vulnerabilities. */ + set_title("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"); + set_title("new title"); return 0; } diff --git a/deps/uv/uv.gyp b/deps/uv/uv.gyp index 96c6b724c6..1ed3b6bc4b 100644 --- a/deps/uv/uv.gyp +++ b/deps/uv/uv.gyp @@ -287,6 +287,9 @@ [ 'OS=="mac" or OS=="freebsd" or OS=="openbsd" or OS=="netbsd"', { 'sources': [ 'src/unix/kqueue.c' ], }], + [ 'OS!="win" and OS!="freebsd"', { + 'sources': [ 'src/unix/proctitle.c' ], + }], ] },