├── test ├── fixtures │ ├── empty_file │ └── load_error.node ├── test-pass-always.c ├── test-fail-always.c ├── test-get-loadavg.c ├── runner-unix.h ├── test-loop-configure.c ├── test-get-memory.c ├── runner-win.h ├── test-run-nowait.c ├── test-run-once.c ├── test-tcp-flags.c ├── test-cwd-and-chdir.c ├── test-hrtime.c ├── test-thread-equal.c ├── test-strscpy.c ├── test-pipe-pending-instances.c ├── test-dlerror.c ├── benchmark-thread.c ├── test-default-loop-close.c ├── run-benchmarks.c ├── test-gethostname.c ├── test-ip4-addr.c ├── test-loop-time.c ├── test-tmpdir.c ├── test-loop-alive.c ├── test-poll-close.c ├── test-connect-unspecified.c ├── test-homedir.c ├── test-tcp-connect6-error.c ├── test-tcp-connect-error.c ├── test-threadpool.c ├── test-async-null-cb.c ├── test-loop-stop.c ├── test-callback-order.c ├── test-process-title.c ├── test-walk-handles.c ├── test-tcp-write-after-connect.c ├── test-loop-close.c └── test-get-passwd.c ├── img └── banner.png ├── docs ├── src │ ├── static │ │ ├── diagrams.key │ │ │ ├── Metadata │ │ │ │ ├── DocumentIdentifier │ │ │ │ ├── Properties.plist │ │ │ │ └── BuildVersionHistory.plist │ │ │ ├── Index.zip │ │ │ ├── preview.jpg │ │ │ ├── preview-web.jpg │ │ │ ├── Data │ │ │ │ ├── st0-311.jpg │ │ │ │ └── st1-475.jpg │ │ │ └── preview-micro.jpg │ │ ├── logo.png │ │ ├── favicon.ico │ │ ├── architecture.png │ │ └── loop_iteration.png │ ├── upgrading.rst │ ├── api.rst │ ├── guide.rst │ ├── guide │ │ ├── about.rst │ │ └── eventloops.rst │ ├── check.rst │ ├── prepare.rst │ ├── dll.rst │ ├── sphinx-plugins │ │ └── manpage.py │ ├── idle.rst │ ├── index.rst │ ├── version.rst │ ├── async.rst │ └── threadpool.rst └── code │ ├── plugin │ ├── hello.c │ ├── plugin.h │ └── main.c │ ├── proc-streams │ ├── test.c │ └── main.c │ ├── cgi │ ├── tick.c │ └── main.c │ ├── helloworld │ └── main.c │ ├── idle-basic │ └── main.c │ ├── multi-echo-server │ └── hammer.js │ ├── ref-timer │ └── main.c │ ├── uvstop │ └── main.c │ ├── detach │ └── main.c │ ├── tty │ └── main.c │ ├── thread-create │ └── main.c │ ├── spawn │ └── main.c │ ├── interfaces │ └── main.c │ ├── queue-work │ └── main.c │ ├── idle-compute │ └── main.c │ ├── progress │ └── main.c │ ├── tty-gravity │ └── main.c │ ├── onchange │ └── main.c │ ├── queue-cancel │ └── main.c │ ├── locks │ └── main.c │ ├── signal │ └── main.c │ ├── uvcat │ └── main.c │ ├── uvtee │ └── main.c │ ├── dns │ └── main.c │ ├── tcp-echo-server │ └── main.c │ └── pipe-echo-server │ └── main.c ├── m4 ├── .gitignore └── as_case.m4 ├── libuv.pc.in ├── src ├── strscpy.c ├── strscpy.h ├── win │ ├── detect-wakeup.c │ ├── snprintf.c │ ├── stream-inl.h │ └── atomicops-inl.h ├── idna.h ├── unix │ ├── posix-hrtime.c │ ├── sysinfo-loadavg.c │ ├── no-proctitle.c │ ├── sysinfo-memory.c │ ├── procfs-exepath.c │ ├── no-fsevents.c │ ├── cygwin.c │ ├── spinlock.h │ ├── pthread-fixes.c │ └── dl.c ├── version.c └── uv-data-getter-setters.c ├── .github └── ISSUE_TEMPLATE.md ├── android-configure-x86 ├── android-configure-arm ├── android-configure-arm64 ├── appveyor.yml ├── android-configure-x86_64 ├── samples ├── .gitignore └── socks5-proxy │ ├── .gitignore │ ├── build.gyp │ ├── Makefile │ └── util.c ├── .gitignore ├── tools └── vswhere_usability_wrapper.cmd ├── include └── uv │ ├── threadpool.h │ ├── os390.h │ ├── posix.h │ ├── aix.h │ ├── bsd.h │ ├── linux.h │ ├── version.h │ ├── android-ifaddrs.h │ └── sunos.h ├── autogen.sh ├── MAINTAINERS.md ├── gyp_uv.py └── .mailmap /test/fixtures/empty_file: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/fixtures/load_error.node: -------------------------------------------------------------------------------- 1 | foobar 2 | -------------------------------------------------------------------------------- /img/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/img/banner.png -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Metadata/DocumentIdentifier: -------------------------------------------------------------------------------- 1 | F69E9CD9-EEF1-4223-9DA4-A1EA7FE112BA -------------------------------------------------------------------------------- /docs/src/static/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/logo.png -------------------------------------------------------------------------------- /docs/src/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/favicon.ico -------------------------------------------------------------------------------- /m4/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore libtoolize-generated files. 2 | *.m4 3 | !as_case.m4 4 | !libuv-check-flags.m4 5 | -------------------------------------------------------------------------------- /docs/src/static/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/architecture.png -------------------------------------------------------------------------------- /docs/src/static/loop_iteration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/loop_iteration.png -------------------------------------------------------------------------------- /docs/code/plugin/hello.c: -------------------------------------------------------------------------------- 1 | #include "plugin.h" 2 | 3 | void initialize() { 4 | mfp_register("Hello World!"); 5 | } 6 | -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Index.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/Index.zip -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/preview.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/preview.jpg -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/preview-web.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/preview-web.jpg -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Data/st0-311.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/Data/st0-311.jpg -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Data/st1-475.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/Data/st1-475.jpg -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/preview-micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/preview-micro.jpg -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Metadata/Properties.plist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kriszyp/libuv/v1.x/docs/src/static/diagrams.key/Metadata/Properties.plist -------------------------------------------------------------------------------- /docs/code/proc-streams/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | fprintf(stderr, "This is stderr\n"); 6 | printf("This is stdout\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /docs/code/plugin/plugin.h: -------------------------------------------------------------------------------- 1 | #ifndef UVBOOK_PLUGIN_SYSTEM 2 | #define UVBOOK_PLUGIN_SYSTEM 3 | 4 | // Plugin authors should use this to register their plugins with mfp. 5 | void mfp_register(const char *name); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /docs/src/upgrading.rst: -------------------------------------------------------------------------------- 1 | .. _upgrading: 2 | 3 | Upgrading 4 | ========= 5 | 6 | Migration guides for different libuv versions, starting with 1.0. 7 | 8 | .. toctree:: 9 | :maxdepth: 1 10 | 11 | migration_010_100 12 | -------------------------------------------------------------------------------- /docs/code/cgi/tick.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i; 6 | for (i = 0; i < 10; i++) { 7 | printf("tick\n"); 8 | fflush(stdout); 9 | sleep(1); 10 | } 11 | printf("BOOM!\n"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /libuv.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=${prefix} 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libuv 7 | Version: @PACKAGE_VERSION@ 8 | Description: multi-platform support library with a focus on asynchronous I/O. 9 | URL: http://libuv.org/ 10 | 11 | Libs: -L${libdir} -luv @LIBS@ 12 | Cflags: -I${includedir} 13 | -------------------------------------------------------------------------------- /docs/code/helloworld/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | uv_loop_t *loop = malloc(sizeof(uv_loop_t)); 7 | uv_loop_init(loop); 8 | 9 | printf("Now quitting.\n"); 10 | uv_run(loop, UV_RUN_DEFAULT); 11 | 12 | uv_loop_close(loop); 13 | free(loop); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /docs/src/static/diagrams.key/Metadata/BuildVersionHistory.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Template: White (2014-02-28 09:41) 6 | M6.2.2-1878-1 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/strscpy.c: -------------------------------------------------------------------------------- 1 | #include "strscpy.h" 2 | #include /* SSIZE_MAX */ 3 | 4 | ssize_t uv__strscpy(char* d, const char* s, size_t n) { 5 | size_t i; 6 | 7 | for (i = 0; i < n; i++) 8 | if ('\0' == (d[i] = s[i])) 9 | return i > SSIZE_MAX ? UV_E2BIG : (ssize_t) i; 10 | 11 | if (i == 0) 12 | return 0; 13 | 14 | d[--i] = '\0'; 15 | 16 | return UV_E2BIG; 17 | } 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 9 | * **Version**: 10 | * **Platform**: 11 | -------------------------------------------------------------------------------- /docs/src/api.rst: -------------------------------------------------------------------------------- 1 | .. _api: 2 | 3 | API documentation 4 | ================= 5 | 6 | .. toctree:: 7 | :maxdepth: 1 8 | 9 | errors 10 | version 11 | loop 12 | handle 13 | request 14 | timer 15 | prepare 16 | check 17 | idle 18 | async 19 | poll 20 | signal 21 | process 22 | stream 23 | tcp 24 | pipe 25 | tty 26 | udp 27 | fs_event 28 | fs_poll 29 | fs 30 | threadpool 31 | dns 32 | dll 33 | threading 34 | misc 35 | 36 | -------------------------------------------------------------------------------- /docs/code/idle-basic/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int64_t counter = 0; 5 | 6 | void wait_for_a_while(uv_idle_t* handle) { 7 | counter++; 8 | 9 | if (counter >= 10e6) 10 | uv_idle_stop(handle); 11 | } 12 | 13 | int main() { 14 | uv_idle_t idler; 15 | 16 | uv_idle_init(uv_default_loop(), &idler); 17 | uv_idle_start(&idler, wait_for_a_while); 18 | 19 | printf("Idling...\n"); 20 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 21 | 22 | uv_loop_close(uv_default_loop()); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /docs/src/guide.rst: -------------------------------------------------------------------------------- 1 | .. _guide: 2 | 3 | User guide 4 | ========== 5 | 6 | .. warning:: 7 | The contents of this guide have been recently incorporated into the libuv documentation 8 | and it hasn't gone through thorough review yet. If you spot a mistake please file an 9 | issue, or better yet, open a pull request! 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | guide/introduction 15 | guide/basics 16 | guide/filesystem 17 | guide/networking 18 | guide/threads 19 | guide/processes 20 | guide/eventloops 21 | guide/utilities 22 | guide/about 23 | -------------------------------------------------------------------------------- /m4/as_case.m4: -------------------------------------------------------------------------------- 1 | # AS_CASE(WORD, [PATTERN1], [IF-MATCHED1]...[DEFAULT]) 2 | # ---------------------------------------------------- 3 | # Expand into 4 | # | case WORD in 5 | # | PATTERN1) IF-MATCHED1 ;; 6 | # | ... 7 | # | *) DEFAULT ;; 8 | # | esac 9 | m4_define([_AS_CASE], 10 | [m4_if([$#], 0, [m4_fatal([$0: too few arguments: $#])], 11 | [$#], 1, [ *) $1 ;;], 12 | [$#], 2, [ $1) m4_default([$2], [:]) ;;], 13 | [ $1) m4_default([$2], [:]) ;; 14 | $0(m4_shiftn(2, $@))])dnl 15 | ]) 16 | m4_defun([AS_CASE], 17 | [m4_ifval([$2$3], 18 | [case $1 in 19 | _AS_CASE(m4_shift($@)) 20 | esac])]) 21 | 22 | -------------------------------------------------------------------------------- /docs/code/multi-echo-server/hammer.js: -------------------------------------------------------------------------------- 1 | var net = require('net'); 2 | 3 | var PHRASE = "hello world"; 4 | var write = function(socket) { 5 | socket.write(PHRASE, 'utf8'); 6 | } 7 | 8 | for (var i = 0; i < 1000; i++) { 9 | (function() { 10 | var socket = net.connect(7000, 'localhost', function() { 11 | socket.on('data', function(reply) { 12 | if (reply.toString().indexOf(PHRASE) != 0) 13 | console.error("Problem! '" + reply + "'" + " '" + PHRASE + "'"); 14 | else 15 | write(socket); 16 | }); 17 | write(socket); 18 | }); 19 | })(); 20 | } 21 | -------------------------------------------------------------------------------- /src/strscpy.h: -------------------------------------------------------------------------------- 1 | #ifndef UV_STRSCPY_H_ 2 | #define UV_STRSCPY_H_ 3 | 4 | /* Include uv.h for its definitions of size_t and ssize_t. 5 | * size_t can be obtained directly from but ssize_t requires 6 | * some hoop jumping on Windows that I didn't want to duplicate here. 7 | */ 8 | #include "uv.h" 9 | 10 | /* Copies up to |n-1| bytes from |d| to |s| and always zero-terminates 11 | * the result, except when |n==0|. Returns the number of bytes copied 12 | * or UV_E2BIG if |d| is too small. 13 | * 14 | * See https://www.kernel.org/doc/htmldocs/kernel-api/API-strscpy.html 15 | */ 16 | ssize_t uv__strscpy(char* d, const char* s, size_t n); 17 | 18 | #endif /* UV_STRSCPY_H_ */ 19 | -------------------------------------------------------------------------------- /android-configure-x86: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TOOLCHAIN=$PWD/android-toolchain-x86 4 | mkdir -p $TOOLCHAIN 5 | API=${3:-24} 6 | $1/build/tools/make-standalone-toolchain.sh \ 7 | --toolchain=x86-4.9 \ 8 | --arch=x86 \ 9 | --install-dir=$TOOLCHAIN \ 10 | --platform=android-$API \ 11 | --force 12 | export PATH=$TOOLCHAIN/bin:$PATH 13 | export AR=i686-linux-android-ar 14 | export CC=i686-linux-android-gcc 15 | export CXX=i686-linux-android-g++ 16 | export LINK=i686-linux-android-g++ 17 | export PLATFORM=android 18 | export CFLAGS="-D__ANDROID_API__=$API" 19 | 20 | if [[ $2 == 'gyp' ]] 21 | then 22 | ./gyp_uv.py -Dtarget_arch=x86 -DOS=android -f make-android 23 | fi 24 | -------------------------------------------------------------------------------- /android-configure-arm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TOOLCHAIN=$PWD/android-toolchain-arm 4 | mkdir -p $TOOLCHAIN 5 | API=${3:-24} 6 | $1/build/tools/make-standalone-toolchain.sh \ 7 | --toolchain=arm-linux-androideabi-4.9 \ 8 | --arch=arm \ 9 | --install-dir=$TOOLCHAIN \ 10 | --platform=android-$API \ 11 | --force 12 | export PATH=$TOOLCHAIN/bin:$PATH 13 | export AR=arm-linux-androideabi-ar 14 | export CC=arm-linux-androideabi-gcc 15 | export CXX=arm-linux-androideabi-g++ 16 | export LINK=arm-linux-androideabi-g++ 17 | export PLATFORM=android 18 | export CFLAGS="-D__ANDROID_API__=$API" 19 | 20 | if [[ $2 == 'gyp' ]] 21 | then 22 | ./gyp_uv.py -Dtarget_arch=arm -DOS=android -f make-android 23 | fi 24 | -------------------------------------------------------------------------------- /android-configure-arm64: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TOOLCHAIN=$PWD/android-toolchain-arm64 4 | mkdir -p $TOOLCHAIN 5 | API=${3:-24} 6 | $1/build/tools/make-standalone-toolchain.sh \ 7 | --toolchain=aarch64-linux-android-4.9 \ 8 | --arch=arm64 \ 9 | --install-dir=$TOOLCHAIN \ 10 | --platform=android-$API \ 11 | --force 12 | export PATH=$TOOLCHAIN/bin:$PATH 13 | export AR=aarch64-linux-android-ar 14 | export CC=aarch64-linux-android-gcc 15 | export CXX=aarch64-linux-android-g++ 16 | export LINK=aarch64-linux-android-g++ 17 | export PLATFORM=android 18 | export CFLAGS="-D__ANDROID_API__=$API" 19 | 20 | if [[ $2 == 'gyp' ]] 21 | then 22 | ./gyp_uv.py -Dtarget_arch=arm64 -DOS=android -f make-android 23 | fi 24 | -------------------------------------------------------------------------------- /docs/code/ref-timer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | uv_loop_t *loop; 6 | uv_timer_t gc_req; 7 | uv_timer_t fake_job_req; 8 | 9 | void gc(uv_timer_t *handle) { 10 | fprintf(stderr, "Freeing unused objects\n"); 11 | } 12 | 13 | void fake_job(uv_timer_t *handle) { 14 | fprintf(stdout, "Fake job done\n"); 15 | } 16 | 17 | int main() { 18 | loop = uv_default_loop(); 19 | 20 | uv_timer_init(loop, &gc_req); 21 | uv_unref((uv_handle_t*) &gc_req); 22 | 23 | uv_timer_start(&gc_req, gc, 0, 2000); 24 | 25 | // could actually be a TCP download or something 26 | uv_timer_init(loop, &fake_job_req); 27 | uv_timer_start(&fake_job_req, fake_job, 9000, 0); 28 | return uv_run(loop, UV_RUN_DEFAULT); 29 | } 30 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: v1.18.0.build{build} 2 | 3 | init: 4 | - git config --global core.autocrlf true 5 | 6 | install: 7 | - cinst -y nsis 8 | 9 | matrix: 10 | fast_finish: true 11 | allow_failures: 12 | - platform: x86 13 | configuration: Release 14 | - platform: x64 15 | configuration: Release 16 | 17 | platform: 18 | - x86 19 | - x64 20 | 21 | configuration: 22 | - Release 23 | 24 | build_script: 25 | # Fixed tag version number if using a tag. 26 | - cmd: if "%APPVEYOR_REPO_TAG%" == "true" set APPVEYOR_BUILD_VERSION=%APPVEYOR_REPO_TAG_NAME% 27 | # vcbuild overwrites the platform variable. 28 | - cmd: set ARCH=%platform% 29 | - cmd: vcbuild.bat release %ARCH% shared 30 | 31 | cache: 32 | - C:\projects\libuv\build\gyp 33 | -------------------------------------------------------------------------------- /docs/code/uvstop/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int64_t counter = 0; 5 | 6 | void idle_cb(uv_idle_t *handle) { 7 | printf("Idle callback\n"); 8 | counter++; 9 | 10 | if (counter >= 5) { 11 | uv_stop(uv_default_loop()); 12 | printf("uv_stop() called\n"); 13 | } 14 | } 15 | 16 | void prep_cb(uv_prepare_t *handle) { 17 | printf("Prep callback\n"); 18 | } 19 | 20 | int main() { 21 | uv_idle_t idler; 22 | uv_prepare_t prep; 23 | 24 | uv_idle_init(uv_default_loop(), &idler); 25 | uv_idle_start(&idler, idle_cb); 26 | 27 | uv_prepare_init(uv_default_loop(), &prep); 28 | uv_prepare_start(&prep, prep_cb); 29 | 30 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /android-configure-x86_64: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export TOOLCHAIN=$PWD/android-toolchain-x86_64 4 | mkdir -p $TOOLCHAIN 5 | API=${3:-24} 6 | $1/build/tools/make-standalone-toolchain.sh \ 7 | --toolchain=x86_64-4.9 \ 8 | --arch=x86_64 \ 9 | --install-dir=$TOOLCHAIN \ 10 | --platform=android-$API \ 11 | --force 12 | export PATH=$TOOLCHAIN/bin:$PATH 13 | export AR=x86_64-linux-android-ar 14 | export CC=x86_64-linux-android-gcc 15 | export CXX=x86_64-linux-android-g++ 16 | export LINK=x86_64-linux-android-g++ 17 | export PLATFORM=android 18 | export CFLAGS="-D__ANDROID_API__=$API -fPIC" 19 | export CXXFLAGS="-D__ANDROID_API__=$API -fPIC" 20 | export LDFLAGS="-fPIC" 21 | 22 | if [[ $2 == 'gyp' ]] 23 | then 24 | ./gyp_uv.py -Dtarget_arch=x86_64 -DOS=android -f make-android 25 | fi 26 | -------------------------------------------------------------------------------- /docs/code/detach/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | uv_loop_t *loop; 6 | uv_process_t child_req; 7 | uv_process_options_t options; 8 | 9 | int main() { 10 | loop = uv_default_loop(); 11 | 12 | char* args[3]; 13 | args[0] = "sleep"; 14 | args[1] = "100"; 15 | args[2] = NULL; 16 | 17 | options.exit_cb = NULL; 18 | options.file = "sleep"; 19 | options.args = args; 20 | options.flags = UV_PROCESS_DETACHED; 21 | 22 | int r; 23 | if ((r = uv_spawn(loop, &child_req, &options))) { 24 | fprintf(stderr, "%s\n", uv_strerror(r)); 25 | return 1; 26 | } 27 | fprintf(stderr, "Launched sleep with PID %d\n", child_req.pid); 28 | uv_unref((uv_handle_t*) &child_req); 29 | 30 | return uv_run(loop, UV_RUN_DEFAULT); 31 | } 32 | -------------------------------------------------------------------------------- /docs/code/tty/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | uv_loop_t *loop; 7 | uv_tty_t tty; 8 | int main() { 9 | loop = uv_default_loop(); 10 | 11 | uv_tty_init(loop, &tty, STDOUT_FILENO, 0); 12 | uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL); 13 | 14 | if (uv_guess_handle(1) == UV_TTY) { 15 | uv_write_t req; 16 | uv_buf_t buf; 17 | buf.base = "\033[41;37m"; 18 | buf.len = strlen(buf.base); 19 | uv_write(&req, (uv_stream_t*) &tty, &buf, 1, NULL); 20 | } 21 | 22 | uv_write_t req; 23 | uv_buf_t buf; 24 | buf.base = "Hello TTY\n"; 25 | buf.len = strlen(buf.base); 26 | uv_write(&req, (uv_stream_t*) &tty, &buf, 1, NULL); 27 | uv_tty_reset_mode(); 28 | return uv_run(loop, UV_RUN_DEFAULT); 29 | } 30 | -------------------------------------------------------------------------------- /docs/code/thread-create/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | void hare(void *arg) { 7 | int tracklen = *((int *) arg); 8 | while (tracklen) { 9 | tracklen--; 10 | sleep(1); 11 | fprintf(stderr, "Hare ran another step\n"); 12 | } 13 | fprintf(stderr, "Hare done running!\n"); 14 | } 15 | 16 | void tortoise(void *arg) { 17 | int tracklen = *((int *) arg); 18 | while (tracklen) { 19 | tracklen--; 20 | fprintf(stderr, "Tortoise ran another step\n"); 21 | sleep(3); 22 | } 23 | fprintf(stderr, "Tortoise done running!\n"); 24 | } 25 | 26 | int main() { 27 | int tracklen = 10; 28 | uv_thread_t hare_id; 29 | uv_thread_t tortoise_id; 30 | uv_thread_create(&hare_id, hare, &tracklen); 31 | uv_thread_create(&tortoise_id, tortoise, &tracklen); 32 | 33 | uv_thread_join(&hare_id); 34 | uv_thread_join(&tortoise_id); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /docs/code/spawn/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | uv_loop_t *loop; 7 | uv_process_t child_req; 8 | uv_process_options_t options; 9 | 10 | void on_exit(uv_process_t *req, int64_t exit_status, int term_signal) { 11 | fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal); 12 | uv_close((uv_handle_t*) req, NULL); 13 | } 14 | 15 | int main() { 16 | loop = uv_default_loop(); 17 | 18 | char* args[3]; 19 | args[0] = "mkdir"; 20 | args[1] = "test-dir"; 21 | args[2] = NULL; 22 | 23 | options.exit_cb = on_exit; 24 | options.file = "mkdir"; 25 | options.args = args; 26 | 27 | int r; 28 | if ((r = uv_spawn(loop, &child_req, &options))) { 29 | fprintf(stderr, "%s\n", uv_strerror(r)); 30 | return 1; 31 | } else { 32 | fprintf(stderr, "Launched process with ID %d\n", child_req.pid); 33 | } 34 | 35 | return uv_run(loop, UV_RUN_DEFAULT); 36 | } 37 | -------------------------------------------------------------------------------- /docs/src/guide/about.rst: -------------------------------------------------------------------------------- 1 | About 2 | ===== 3 | 4 | `Nikhil Marathe `_ started writing this book one 5 | afternoon (June 16, 2012) when he didn't feel like programming. He had recently 6 | been stung by the lack of good documentation on libuv while working on 7 | `node-taglib `_. Although reference 8 | documentation was present, there were no comprehensive tutorials. This book is 9 | the output of that need and tries to be accurate. That said, the book may have 10 | mistakes. Pull requests are encouraged. 11 | 12 | Nikhil is indebted to Marc Lehmann's comprehensive `man page 13 | `_ about libev which 14 | describes much of the semantics of the two libraries. 15 | 16 | This book was made using `Sphinx `_ and `vim 17 | `_. 18 | 19 | .. note:: 20 | In 2017 the libuv project incorporated the Nikhil's work into the official 21 | documentation and it's maintained there henceforth. 22 | 23 | -------------------------------------------------------------------------------- /docs/code/interfaces/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char buf[512]; 6 | uv_interface_address_t *info; 7 | int count, i; 8 | 9 | uv_interface_addresses(&info, &count); 10 | i = count; 11 | 12 | printf("Number of interfaces: %d\n", count); 13 | while (i--) { 14 | uv_interface_address_t interface = info[i]; 15 | 16 | printf("Name: %s\n", interface.name); 17 | printf("Internal? %s\n", interface.is_internal ? "Yes" : "No"); 18 | 19 | if (interface.address.address4.sin_family == AF_INET) { 20 | uv_ip4_name(&interface.address.address4, buf, sizeof(buf)); 21 | printf("IPv4 address: %s\n", buf); 22 | } 23 | else if (interface.address.address4.sin_family == AF_INET6) { 24 | uv_ip6_name(&interface.address.address6, buf, sizeof(buf)); 25 | printf("IPv6 address: %s\n", buf); 26 | } 27 | 28 | printf("\n"); 29 | } 30 | 31 | uv_free_interface_addresses(info, count); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /docs/code/queue-work/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #define FIB_UNTIL 25 8 | uv_loop_t *loop; 9 | 10 | long fib_(long t) { 11 | if (t == 0 || t == 1) 12 | return 1; 13 | else 14 | return fib_(t-1) + fib_(t-2); 15 | } 16 | 17 | void fib(uv_work_t *req) { 18 | int n = *(int *) req->data; 19 | if (random() % 2) 20 | sleep(1); 21 | else 22 | sleep(3); 23 | long fib = fib_(n); 24 | fprintf(stderr, "%dth fibonacci is %lu\n", n, fib); 25 | } 26 | 27 | void after_fib(uv_work_t *req, int status) { 28 | fprintf(stderr, "Done calculating %dth fibonacci\n", *(int *) req->data); 29 | } 30 | 31 | int main() { 32 | loop = uv_default_loop(); 33 | 34 | int data[FIB_UNTIL]; 35 | uv_work_t req[FIB_UNTIL]; 36 | int i; 37 | for (i = 0; i < FIB_UNTIL; i++) { 38 | data[i] = i; 39 | req[i].data = (void *) &data[i]; 40 | uv_queue_work(loop, &req[i], fib, after_fib); 41 | } 42 | 43 | return uv_run(loop, UV_RUN_DEFAULT); 44 | } 45 | -------------------------------------------------------------------------------- /docs/code/plugin/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include "plugin.h" 8 | 9 | typedef void (*init_plugin_function)(); 10 | 11 | void mfp_register(const char *name) { 12 | fprintf(stderr, "Registered plugin \"%s\"\n", name); 13 | } 14 | 15 | int main(int argc, char **argv) { 16 | if (argc == 1) { 17 | fprintf(stderr, "Usage: %s [plugin1] [plugin2] ...\n", argv[0]); 18 | return 0; 19 | } 20 | 21 | uv_lib_t *lib = (uv_lib_t*) malloc(sizeof(uv_lib_t)); 22 | while (--argc) { 23 | fprintf(stderr, "Loading %s\n", argv[argc]); 24 | if (uv_dlopen(argv[argc], lib)) { 25 | fprintf(stderr, "Error: %s\n", uv_dlerror(lib)); 26 | continue; 27 | } 28 | 29 | init_plugin_function init_plugin; 30 | if (uv_dlsym(lib, "initialize", (void **) &init_plugin)) { 31 | fprintf(stderr, "dlsym error: %s\n", uv_dlerror(lib)); 32 | continue; 33 | } 34 | 35 | init_plugin(); 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /docs/src/check.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _check: 3 | 4 | :c:type:`uv_check_t` --- Check handle 5 | ===================================== 6 | 7 | Check handles will run the given callback once per loop iteration, right 8 | after polling for i/o. 9 | 10 | 11 | Data types 12 | ---------- 13 | 14 | .. c:type:: uv_check_t 15 | 16 | Check handle type. 17 | 18 | .. c:type:: void (*uv_check_cb)(uv_check_t* handle) 19 | 20 | Type definition for callback passed to :c:func:`uv_check_start`. 21 | 22 | 23 | Public members 24 | ^^^^^^^^^^^^^^ 25 | 26 | N/A 27 | 28 | .. seealso:: The :c:type:`uv_handle_t` members also apply. 29 | 30 | 31 | API 32 | --- 33 | 34 | .. c:function:: int uv_check_init(uv_loop_t* loop, uv_check_t* check) 35 | 36 | Initialize the handle. 37 | 38 | .. c:function:: int uv_check_start(uv_check_t* check, uv_check_cb cb) 39 | 40 | Start the handle with the given callback. 41 | 42 | .. c:function:: int uv_check_stop(uv_check_t* check) 43 | 44 | Stop the handle, the callback will no longer be called. 45 | 46 | .. seealso:: The :c:type:`uv_handle_t` API functions also apply. 47 | -------------------------------------------------------------------------------- /docs/src/prepare.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _prepare: 3 | 4 | :c:type:`uv_prepare_t` --- Prepare handle 5 | ========================================= 6 | 7 | Prepare handles will run the given callback once per loop iteration, right 8 | before polling for i/o. 9 | 10 | 11 | Data types 12 | ---------- 13 | 14 | .. c:type:: uv_prepare_t 15 | 16 | Prepare handle type. 17 | 18 | .. c:type:: void (*uv_prepare_cb)(uv_prepare_t* handle) 19 | 20 | Type definition for callback passed to :c:func:`uv_prepare_start`. 21 | 22 | 23 | Public members 24 | ^^^^^^^^^^^^^^ 25 | 26 | N/A 27 | 28 | .. seealso:: The :c:type:`uv_handle_t` members also apply. 29 | 30 | 31 | API 32 | --- 33 | 34 | .. c:function:: int uv_prepare_init(uv_loop_t* loop, uv_prepare_t* prepare) 35 | 36 | Initialize the handle. 37 | 38 | .. c:function:: int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb) 39 | 40 | Start the handle with the given callback. 41 | 42 | .. c:function:: int uv_prepare_stop(uv_prepare_t* prepare) 43 | 44 | Stop the handle, the callback will no longer be called. 45 | 46 | .. seealso:: The :c:type:`uv_handle_t` API functions also apply. 47 | -------------------------------------------------------------------------------- /docs/src/dll.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _dll: 3 | 4 | Shared library handling 5 | ======================= 6 | 7 | libuv provides cross platform utilities for loading shared libraries and 8 | retrieving symbols from them, using the following API. 9 | 10 | 11 | Data types 12 | ---------- 13 | 14 | .. c:type:: uv_lib_t 15 | 16 | Shared library data type. 17 | 18 | 19 | Public members 20 | ^^^^^^^^^^^^^^ 21 | 22 | N/A 23 | 24 | 25 | API 26 | --- 27 | 28 | .. c:function:: int uv_dlopen(const char* filename, uv_lib_t* lib) 29 | 30 | Opens a shared library. The filename is in utf-8. Returns 0 on success and 31 | -1 on error. Call :c:func:`uv_dlerror` to get the error message. 32 | 33 | .. c:function:: void uv_dlclose(uv_lib_t* lib) 34 | 35 | Close the shared library. 36 | 37 | .. c:function:: int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) 38 | 39 | Retrieves a data pointer from a dynamic library. It is legal for a symbol 40 | to map to NULL. Returns 0 on success and -1 if the symbol was not found. 41 | 42 | .. c:function:: const char* uv_dlerror(const uv_lib_t* lib) 43 | 44 | Returns the last uv_dlopen() or uv_dlsym() error message. 45 | -------------------------------------------------------------------------------- /samples/.gitignore: -------------------------------------------------------------------------------- 1 | # Copyright StrongLoop, Inc. All rights reserved. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to 5 | # deal in the Software without restriction, including without limitation the 6 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | # sell copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in 11 | # all copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | # IN THE SOFTWARE. 20 | 21 | *.mk 22 | *.Makefile 23 | -------------------------------------------------------------------------------- /samples/socks5-proxy/.gitignore: -------------------------------------------------------------------------------- 1 | # Copyright StrongLoop, Inc. All rights reserved. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to 5 | # deal in the Software without restriction, including without limitation the 6 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | # sell copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in 11 | # all copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | # IN THE SOFTWARE. 20 | 21 | /build/ 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.[oa] 3 | *.l[oa] 4 | *.opensdf 5 | *.orig 6 | *.pyc 7 | *.sdf 8 | *.suo 9 | .vs/ 10 | *.VC.db 11 | *.VC.opendb 12 | core 13 | vgcore.* 14 | .buildstamp 15 | .dirstamp 16 | .deps/ 17 | /.libs/ 18 | /aclocal.m4 19 | /ar-lib 20 | /autom4te.cache/ 21 | /compile 22 | /config.guess 23 | /config.log 24 | /config.status 25 | /config.sub 26 | /configure 27 | /depcomp 28 | /install-sh 29 | /libtool 30 | /libuv.a 31 | /libuv.dylib 32 | /libuv.pc 33 | /libuv.so 34 | /ltmain.sh 35 | /missing 36 | /test-driver 37 | Makefile 38 | Makefile.in 39 | 40 | # Generated by gyp for android 41 | *.target.mk 42 | /android-toolchain 43 | 44 | /out/ 45 | /build/ 46 | 47 | /test/.libs/ 48 | /test/run-tests 49 | /test/run-tests.exe 50 | /test/run-tests.dSYM 51 | /test/run-benchmarks 52 | /test/run-benchmarks.exe 53 | /test/run-benchmarks.dSYM 54 | 55 | *.sln 56 | *.sln.cache 57 | *.ncb 58 | *.vcproj 59 | *.vcproj*.user 60 | *.vcxproj 61 | *.vcxproj.filters 62 | *.vcxproj.user 63 | _UpgradeReport_Files/ 64 | UpgradeLog*.XML 65 | Debug 66 | Release 67 | ipch 68 | 69 | # sphinx generated files 70 | /docs/build/ 71 | 72 | # Clion / IntelliJ project files 73 | /.idea/ 74 | 75 | *.xcodeproj 76 | *.xcworkspace 77 | 78 | # make dist output 79 | libuv-*.tar.* 80 | -------------------------------------------------------------------------------- /src/win/detect-wakeup.c: -------------------------------------------------------------------------------- 1 | #include "uv.h" 2 | #include "internal.h" 3 | #include "winapi.h" 4 | 5 | static void uv__register_system_resume_callback(void); 6 | 7 | void uv__init_detect_system_wakeup(void) { 8 | /* Try registering system power event callback. This is the cleanest 9 | * method, but it will only work on Win8 and above. 10 | */ 11 | uv__register_system_resume_callback(); 12 | } 13 | 14 | static ULONG CALLBACK uv__system_resume_callback(PVOID Context, 15 | ULONG Type, 16 | PVOID Setting) { 17 | if (Type == PBT_APMRESUMESUSPEND || Type == PBT_APMRESUMEAUTOMATIC) 18 | uv__wake_all_loops(); 19 | 20 | return 0; 21 | } 22 | 23 | static void uv__register_system_resume_callback(void) { 24 | _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS recipient; 25 | _HPOWERNOTIFY registration_handle; 26 | 27 | if (pPowerRegisterSuspendResumeNotification == NULL) 28 | return; 29 | 30 | recipient.Callback = uv__system_resume_callback; 31 | recipient.Context = NULL; 32 | (*pPowerRegisterSuspendResumeNotification)(DEVICE_NOTIFY_CALLBACK, 33 | &recipient, 34 | ®istration_handle); 35 | } 36 | -------------------------------------------------------------------------------- /docs/code/idle-compute/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | uv_loop_t *loop; 6 | uv_fs_t stdin_watcher; 7 | uv_idle_t idler; 8 | char buffer[1024]; 9 | 10 | void crunch_away(uv_idle_t* handle) { 11 | // Compute extra-terrestrial life 12 | // fold proteins 13 | // computer another digit of PI 14 | // or similar 15 | fprintf(stderr, "Computing PI...\n"); 16 | // just to avoid overwhelming your terminal emulator 17 | uv_idle_stop(handle); 18 | } 19 | 20 | void on_type(uv_fs_t *req) { 21 | if (stdin_watcher.result > 0) { 22 | buffer[stdin_watcher.result] = '\0'; 23 | printf("Typed %s\n", buffer); 24 | 25 | uv_buf_t buf = uv_buf_init(buffer, 1024); 26 | uv_fs_read(loop, &stdin_watcher, 0, &buf, 1, -1, on_type); 27 | uv_idle_start(&idler, crunch_away); 28 | } 29 | else if (stdin_watcher.result < 0) { 30 | fprintf(stderr, "error opening file: %s\n", uv_strerror(req->result)); 31 | } 32 | } 33 | 34 | int main() { 35 | loop = uv_default_loop(); 36 | 37 | uv_idle_init(loop, &idler); 38 | 39 | uv_buf_t buf = uv_buf_init(buffer, 1024); 40 | uv_fs_read(loop, &stdin_watcher, 0, &buf, 1, -1, on_type); 41 | uv_idle_start(&idler, crunch_away); 42 | return uv_run(loop, UV_RUN_DEFAULT); 43 | } 44 | -------------------------------------------------------------------------------- /docs/code/progress/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | uv_loop_t *loop; 8 | uv_async_t async; 9 | 10 | double percentage; 11 | 12 | void fake_download(uv_work_t *req) { 13 | int size = *((int*) req->data); 14 | int downloaded = 0; 15 | while (downloaded < size) { 16 | percentage = downloaded*100.0/size; 17 | async.data = (void*) &percentage; 18 | uv_async_send(&async); 19 | 20 | sleep(1); 21 | downloaded += (200+random())%1000; // can only download max 1000bytes/sec, 22 | // but at least a 200; 23 | } 24 | } 25 | 26 | void after(uv_work_t *req, int status) { 27 | fprintf(stderr, "Download complete\n"); 28 | uv_close((uv_handle_t*) &async, NULL); 29 | } 30 | 31 | void print_progress(uv_async_t *handle) { 32 | double percentage = *((double*) handle->data); 33 | fprintf(stderr, "Downloaded %.2f%%\n", percentage); 34 | } 35 | 36 | int main() { 37 | loop = uv_default_loop(); 38 | 39 | uv_work_t req; 40 | int size = 10240; 41 | req.data = (void*) &size; 42 | 43 | uv_async_init(loop, &async, print_progress); 44 | uv_queue_work(loop, &req, fake_download, after); 45 | 46 | return uv_run(loop, UV_RUN_DEFAULT); 47 | } 48 | -------------------------------------------------------------------------------- /docs/code/proc-streams/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | uv_loop_t *loop; 8 | uv_process_t child_req; 9 | uv_process_options_t options; 10 | 11 | void on_exit(uv_process_t *req, int64_t exit_status, int term_signal) { 12 | fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal); 13 | uv_close((uv_handle_t*) req, NULL); 14 | } 15 | 16 | int main() { 17 | loop = uv_default_loop(); 18 | 19 | size_t size = 500; 20 | char path[size]; 21 | uv_exepath(path, &size); 22 | strcpy(path + (strlen(path) - strlen("proc-streams")), "test"); 23 | 24 | char* args[2]; 25 | args[0] = path; 26 | args[1] = NULL; 27 | 28 | /* ... */ 29 | 30 | options.stdio_count = 3; 31 | uv_stdio_container_t child_stdio[3]; 32 | child_stdio[0].flags = UV_IGNORE; 33 | child_stdio[1].flags = UV_IGNORE; 34 | child_stdio[2].flags = UV_INHERIT_FD; 35 | child_stdio[2].data.fd = 2; 36 | options.stdio = child_stdio; 37 | 38 | options.exit_cb = on_exit; 39 | options.file = args[0]; 40 | options.args = args; 41 | 42 | int r; 43 | if ((r = uv_spawn(loop, &child_req, &options))) { 44 | fprintf(stderr, "%s\n", uv_strerror(r)); 45 | return 1; 46 | } 47 | 48 | return uv_run(loop, UV_RUN_DEFAULT); 49 | } 50 | -------------------------------------------------------------------------------- /docs/code/tty-gravity/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | uv_loop_t *loop; 7 | uv_tty_t tty; 8 | uv_timer_t tick; 9 | uv_write_t write_req; 10 | int width, height; 11 | int pos = 0; 12 | char *message = " Hello TTY "; 13 | 14 | void update(uv_timer_t *req) { 15 | char data[500]; 16 | 17 | uv_buf_t buf; 18 | buf.base = data; 19 | buf.len = sprintf(data, "\033[2J\033[H\033[%dB\033[%luC\033[42;37m%s", 20 | pos, 21 | (unsigned long) (width-strlen(message))/2, 22 | message); 23 | uv_write(&write_req, (uv_stream_t*) &tty, &buf, 1, NULL); 24 | 25 | pos++; 26 | if (pos > height) { 27 | uv_tty_reset_mode(); 28 | uv_timer_stop(&tick); 29 | } 30 | } 31 | 32 | int main() { 33 | loop = uv_default_loop(); 34 | 35 | uv_tty_init(loop, &tty, STDOUT_FILENO, 0); 36 | uv_tty_set_mode(&tty, 0); 37 | 38 | if (uv_tty_get_winsize(&tty, &width, &height)) { 39 | fprintf(stderr, "Could not get TTY information\n"); 40 | uv_tty_reset_mode(); 41 | return 1; 42 | } 43 | 44 | fprintf(stderr, "Width %d, height %d\n", width, height); 45 | uv_timer_init(loop, &tick); 46 | uv_timer_start(&tick, update, 200, 200); 47 | return uv_run(loop, UV_RUN_DEFAULT); 48 | } 49 | -------------------------------------------------------------------------------- /test/test-pass-always.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "task.h" 23 | 24 | 25 | TEST_IMPL(pass_always) { 26 | /* This test always passes. It is used to test the test runner. */ 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /docs/src/sphinx-plugins/manpage.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | # 4 | # Copyright (c) 2013 Dariusz Dwornikowski. All rights reserved. 5 | # 6 | # Adapted from https://github.com/tdi/sphinxcontrib-manpage 7 | # License: Apache 2 8 | # 9 | 10 | 11 | import re 12 | 13 | from docutils import nodes, utils 14 | from docutils.parsers.rst.roles import set_classes 15 | from string import Template 16 | 17 | 18 | def make_link_node(rawtext, app, name, manpage_num, options): 19 | ref = app.config.man_url_regex 20 | if not ref: 21 | ref = "http://linux.die.net/man/%s/%s" % (manpage_num, name) 22 | else: 23 | s = Template(ref) 24 | ref = s.substitute(num=manpage_num, topic=name) 25 | set_classes(options) 26 | node = nodes.reference(rawtext, "%s(%s)" % (name, manpage_num), refuri=ref, **options) 27 | return node 28 | 29 | 30 | def man_role(name, rawtext, text, lineno, inliner, options={}, content=[]): 31 | app = inliner.document.settings.env.app 32 | p = re.compile("([a-zA-Z0-9_\.-_]+)\((\d)\)") 33 | m = p.match(text) 34 | 35 | manpage_num = m.group(2) 36 | name = m.group(1) 37 | node = make_link_node(rawtext, app, name, manpage_num, options) 38 | return [node], [] 39 | 40 | 41 | def setup(app): 42 | app.info('Initializing manpage plugin') 43 | app.add_role('man', man_role) 44 | app.add_config_value('man_url_regex', None, 'env') 45 | return 46 | 47 | -------------------------------------------------------------------------------- /docs/code/onchange/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | uv_loop_t *loop; 7 | const char *command; 8 | 9 | void run_command(uv_fs_event_t *handle, const char *filename, int events, int status) { 10 | char path[1024]; 11 | size_t size = 1023; 12 | // Does not handle error if path is longer than 1023. 13 | uv_fs_event_getpath(handle, path, &size); 14 | path[size] = '\0'; 15 | 16 | fprintf(stderr, "Change detected in %s: ", path); 17 | if (events & UV_RENAME) 18 | fprintf(stderr, "renamed"); 19 | if (events & UV_CHANGE) 20 | fprintf(stderr, "changed"); 21 | 22 | fprintf(stderr, " %s\n", filename ? filename : ""); 23 | system(command); 24 | } 25 | 26 | int main(int argc, char **argv) { 27 | if (argc <= 2) { 28 | fprintf(stderr, "Usage: %s [file2 ...]\n", argv[0]); 29 | return 1; 30 | } 31 | 32 | loop = uv_default_loop(); 33 | command = argv[1]; 34 | 35 | while (argc-- > 2) { 36 | fprintf(stderr, "Adding watch on %s\n", argv[argc]); 37 | uv_fs_event_t *fs_event_req = malloc(sizeof(uv_fs_event_t)); 38 | uv_fs_event_init(loop, fs_event_req); 39 | // The recursive flag watches subdirectories too. 40 | uv_fs_event_start(fs_event_req, run_command, argv[argc], UV_FS_EVENT_RECURSIVE); 41 | } 42 | 43 | return uv_run(loop, UV_RUN_DEFAULT); 44 | } 45 | -------------------------------------------------------------------------------- /test/test-fail-always.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "task.h" 23 | 24 | 25 | TEST_IMPL(fail_always) { 26 | /* This test always fails. It is used to test the test runner. */ 27 | FATAL("Yes, it always fails"); 28 | return 2; 29 | } 30 | -------------------------------------------------------------------------------- /docs/src/idle.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _idle: 3 | 4 | :c:type:`uv_idle_t` --- Idle handle 5 | =================================== 6 | 7 | Idle handles will run the given callback once per loop iteration, right 8 | before the :c:type:`uv_prepare_t` handles. 9 | 10 | .. note:: 11 | The notable difference with prepare handles is that when there are active idle handles, 12 | the loop will perform a zero timeout poll instead of blocking for i/o. 13 | 14 | .. warning:: 15 | Despite the name, idle handles will get their callbacks called on every loop iteration, 16 | not when the loop is actually "idle". 17 | 18 | 19 | Data types 20 | ---------- 21 | 22 | .. c:type:: uv_idle_t 23 | 24 | Idle handle type. 25 | 26 | .. c:type:: void (*uv_idle_cb)(uv_idle_t* handle) 27 | 28 | Type definition for callback passed to :c:func:`uv_idle_start`. 29 | 30 | 31 | Public members 32 | ^^^^^^^^^^^^^^ 33 | 34 | N/A 35 | 36 | .. seealso:: The :c:type:`uv_handle_t` members also apply. 37 | 38 | 39 | API 40 | --- 41 | 42 | .. c:function:: int uv_idle_init(uv_loop_t* loop, uv_idle_t* idle) 43 | 44 | Initialize the handle. 45 | 46 | .. c:function:: int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb) 47 | 48 | Start the handle with the given callback. 49 | 50 | .. c:function:: int uv_idle_stop(uv_idle_t* idle) 51 | 52 | Stop the handle, the callback will no longer be called. 53 | 54 | .. seealso:: The :c:type:`uv_handle_t` API functions also apply. 55 | -------------------------------------------------------------------------------- /tools/vswhere_usability_wrapper.cmd: -------------------------------------------------------------------------------- 1 | :: Copyright 2017 - Refael Ackermann 2 | :: Distributed under MIT style license or the libuv license 3 | :: See accompanying file LICENSE at https://github.com/node4good/windows-autoconf 4 | :: or libuv LICENSE file at https://github.com/libuv/libuv 5 | :: version: 2.0.0 6 | 7 | @if not defined DEBUG_HELPER @ECHO OFF 8 | setlocal 9 | if "%~1"=="prerelease" set VSWHERE_WITH_PRERELEASE=1 10 | set "InstallerPath=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer" 11 | if not exist "%InstallerPath%" set "InstallerPath=%ProgramFiles%\Microsoft Visual Studio\Installer" 12 | if not exist "%InstallerPath%" goto :no-vswhere 13 | :: Manipulate %Path% for easier " handeling 14 | set "Path=%Path%;%InstallerPath%" 15 | where vswhere 2> nul > nul 16 | if errorlevel 1 goto :no-vswhere 17 | set VSWHERE_REQ=-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 18 | set VSWHERE_PRP=-property installationPath 19 | set VSWHERE_LMT=-version "[15.0,16.0)" 20 | vswhere -prerelease > nul 21 | if not errorlevel 1 if "%VSWHERE_WITH_PRERELEASE%"=="1" set "VSWHERE_LMT=%VSWHERE_LMT% -prerelease" 22 | SET VSWHERE_ARGS=-latest -products * %VSWHERE_REQ% %VSWHERE_PRP% %VSWHERE_LMT% 23 | for /f "usebackq tokens=*" %%i in (`vswhere %VSWHERE_ARGS%`) do ( 24 | endlocal 25 | set "VCINSTALLDIR=%%i\VC\" 26 | set "VS150COMNTOOLS=%%i\Common7\Tools\" 27 | exit /B 0 28 | ) 29 | 30 | :no-vswhere 31 | endlocal 32 | echo could not find "vswhere" 33 | exit /B 1 -------------------------------------------------------------------------------- /test/test-get-loadavg.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | TEST_IMPL(get_loadavg) { 26 | 27 | double avg[3] = {-1, -1, -1}; 28 | uv_loadavg(avg); 29 | 30 | ASSERT(avg[0] >= 0); 31 | ASSERT(avg[1] >= 0); 32 | ASSERT(avg[2] >= 0); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /docs/code/queue-cancel/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #define FIB_UNTIL 25 8 | uv_loop_t *loop; 9 | uv_work_t fib_reqs[FIB_UNTIL]; 10 | 11 | long fib_(long t) { 12 | if (t == 0 || t == 1) 13 | return 1; 14 | else 15 | return fib_(t-1) + fib_(t-2); 16 | } 17 | 18 | void fib(uv_work_t *req) { 19 | int n = *(int *) req->data; 20 | if (random() % 2) 21 | sleep(1); 22 | else 23 | sleep(3); 24 | long fib = fib_(n); 25 | fprintf(stderr, "%dth fibonacci is %lu\n", n, fib); 26 | } 27 | 28 | void after_fib(uv_work_t *req, int status) { 29 | if (status == UV_ECANCELED) 30 | fprintf(stderr, "Calculation of %d cancelled.\n", *(int *) req->data); 31 | } 32 | 33 | void signal_handler(uv_signal_t *req, int signum) 34 | { 35 | printf("Signal received!\n"); 36 | int i; 37 | for (i = 0; i < FIB_UNTIL; i++) { 38 | uv_cancel((uv_req_t*) &fib_reqs[i]); 39 | } 40 | uv_signal_stop(req); 41 | } 42 | 43 | int main() { 44 | loop = uv_default_loop(); 45 | 46 | int data[FIB_UNTIL]; 47 | int i; 48 | for (i = 0; i < FIB_UNTIL; i++) { 49 | data[i] = i; 50 | fib_reqs[i].data = (void *) &data[i]; 51 | uv_queue_work(loop, &fib_reqs[i], fib, after_fib); 52 | } 53 | 54 | uv_signal_t sig; 55 | uv_signal_init(loop, &sig); 56 | uv_signal_start(&sig, signal_handler, SIGINT); 57 | 58 | return uv_run(loop, UV_RUN_DEFAULT); 59 | } 60 | -------------------------------------------------------------------------------- /test/runner-unix.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef TEST_RUNNER_UNIX_H 23 | #define TEST_RUNNER_UNIX_H 24 | 25 | #include 26 | #include /* FILE */ 27 | 28 | typedef struct { 29 | FILE* stdout_file; 30 | pid_t pid; 31 | char* name; 32 | int status; 33 | int terminated; 34 | } process_info_t; 35 | 36 | #endif /* TEST_RUNNER_UNIX_H */ 37 | -------------------------------------------------------------------------------- /src/idna.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, 2018 Ben Noordhuis 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef UV_SRC_IDNA_H_ 17 | #define UV_SRC_IDNA_H_ 18 | 19 | /* Decode a single codepoint. Returns the codepoint or UINT32_MAX on error. 20 | * |p| is updated on success _and_ error, i.e., bad multi-byte sequences are 21 | * skipped in their entirety, not just the first bad byte. 22 | */ 23 | unsigned uv__utf8_decode1(const char** p, const char* pe); 24 | 25 | /* Convert a UTF-8 domain name to IDNA 2008 / Punycode. A return value >= 0 26 | * is the number of bytes written to |d|, including the trailing nul byte. 27 | * A return value < 0 is a libuv error code. |s| and |d| can not overlap. 28 | */ 29 | long uv__idna_toascii(const char* s, const char* se, char* d, char* de); 30 | 31 | #endif /* UV_SRC_IDNA_H_ */ 32 | -------------------------------------------------------------------------------- /src/unix/posix-hrtime.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | #undef NANOSEC 29 | #define NANOSEC ((uint64_t) 1e9) 30 | 31 | uint64_t uv__hrtime(uv_clocktype_t type) { 32 | struct timespec ts; 33 | clock_gettime(CLOCK_MONOTONIC, &ts); 34 | return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec); 35 | } 36 | -------------------------------------------------------------------------------- /test/test-loop-configure.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014, Ben Noordhuis 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #include "uv.h" 17 | #include "task.h" 18 | 19 | static void timer_cb(uv_timer_t* handle) { 20 | uv_close((uv_handle_t*) handle, NULL); 21 | } 22 | 23 | 24 | TEST_IMPL(loop_configure) { 25 | uv_timer_t timer_handle; 26 | uv_loop_t loop; 27 | ASSERT(0 == uv_loop_init(&loop)); 28 | #ifdef _WIN32 29 | ASSERT(UV_ENOSYS == uv_loop_configure(&loop, UV_LOOP_BLOCK_SIGNAL, 0)); 30 | #else 31 | ASSERT(0 == uv_loop_configure(&loop, UV_LOOP_BLOCK_SIGNAL, SIGPROF)); 32 | #endif 33 | ASSERT(0 == uv_timer_init(&loop, &timer_handle)); 34 | ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 10, 0)); 35 | ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT)); 36 | ASSERT(0 == uv_loop_close(&loop)); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /docs/code/locks/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uv_barrier_t blocker; 5 | uv_rwlock_t numlock; 6 | int shared_num; 7 | 8 | void reader(void *n) 9 | { 10 | int num = *(int *)n; 11 | int i; 12 | for (i = 0; i < 20; i++) { 13 | uv_rwlock_rdlock(&numlock); 14 | printf("Reader %d: acquired lock\n", num); 15 | printf("Reader %d: shared num = %d\n", num, shared_num); 16 | uv_rwlock_rdunlock(&numlock); 17 | printf("Reader %d: released lock\n", num); 18 | } 19 | uv_barrier_wait(&blocker); 20 | } 21 | 22 | void writer(void *n) 23 | { 24 | int num = *(int *)n; 25 | int i; 26 | for (i = 0; i < 20; i++) { 27 | uv_rwlock_wrlock(&numlock); 28 | printf("Writer %d: acquired lock\n", num); 29 | shared_num++; 30 | printf("Writer %d: incremented shared num = %d\n", num, shared_num); 31 | uv_rwlock_wrunlock(&numlock); 32 | printf("Writer %d: released lock\n", num); 33 | } 34 | uv_barrier_wait(&blocker); 35 | } 36 | 37 | int main() 38 | { 39 | uv_barrier_init(&blocker, 4); 40 | 41 | shared_num = 0; 42 | uv_rwlock_init(&numlock); 43 | 44 | uv_thread_t threads[3]; 45 | 46 | int thread_nums[] = {1, 2, 1}; 47 | uv_thread_create(&threads[0], reader, &thread_nums[0]); 48 | uv_thread_create(&threads[1], reader, &thread_nums[1]); 49 | 50 | uv_thread_create(&threads[2], writer, &thread_nums[2]); 51 | 52 | uv_barrier_wait(&blocker); 53 | uv_barrier_destroy(&blocker); 54 | 55 | uv_rwlock_destroy(&numlock); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/unix/sysinfo-loadavg.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | void uv_loadavg(double avg[3]) { 29 | struct sysinfo info; 30 | 31 | if (sysinfo(&info) < 0) return; 32 | 33 | avg[0] = (double) info.loads[0] / 65536.0; 34 | avg[1] = (double) info.loads[1] / 65536.0; 35 | avg[2] = (double) info.loads[2] / 65536.0; 36 | } 37 | -------------------------------------------------------------------------------- /include/uv/threadpool.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | /* 23 | * This file is private to libuv. It provides common functionality to both 24 | * Windows and Unix backends. 25 | */ 26 | 27 | #ifndef UV_THREADPOOL_H_ 28 | #define UV_THREADPOOL_H_ 29 | 30 | struct uv__work { 31 | void (*work)(struct uv__work *w); 32 | void (*done)(struct uv__work *w, int status); 33 | struct uv_loop_s* loop; 34 | void* wq[2]; 35 | }; 36 | 37 | #endif /* UV_THREADPOOL_H_ */ 38 | -------------------------------------------------------------------------------- /src/unix/no-proctitle.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | char** uv_setup_args(int argc, char** argv) { 29 | return argv; 30 | } 31 | 32 | int uv_set_process_title(const char* title) { 33 | return 0; 34 | } 35 | 36 | int uv_get_process_title(char* buffer, size_t size) { 37 | if (buffer == NULL || size == 0) 38 | return UV_EINVAL; 39 | 40 | buffer[0] = '\0'; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /include/uv/os390.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_MVS_H 23 | #define UV_MVS_H 24 | 25 | #define UV_PLATFORM_SEM_T long 26 | 27 | #define UV_PLATFORM_LOOP_FIELDS \ 28 | void* ep; \ 29 | 30 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 31 | char rfis_rftok[8]; \ 32 | 33 | #endif /* UV_MVS_H */ 34 | -------------------------------------------------------------------------------- /docs/src/index.rst: -------------------------------------------------------------------------------- 1 | 2 | Welcome to the libuv documentation 3 | ================================== 4 | 5 | Overview 6 | -------- 7 | 8 | libuv is a multi-platform support library with a focus on asynchronous I/O. It 9 | was primarily developed for use by `Node.js`_, but it's also used by `Luvit`_, 10 | `Julia`_, `pyuv`_, and `others`_. 11 | 12 | .. note:: 13 | In case you find errors in this documentation you can help by sending 14 | `pull requests `_! 15 | 16 | .. _Node.js: http://nodejs.org 17 | .. _Luvit: http://luvit.io 18 | .. _Julia: http://julialang.org 19 | .. _pyuv: https://github.com/saghul/pyuv 20 | .. _others: https://github.com/libuv/libuv/wiki/Projects-that-use-libuv 21 | 22 | 23 | Features 24 | -------- 25 | 26 | * Full-featured event loop backed by epoll, kqueue, IOCP, event ports. 27 | * Asynchronous TCP and UDP sockets 28 | * Asynchronous DNS resolution 29 | * Asynchronous file and file system operations 30 | * File system events 31 | * ANSI escape code controlled TTY 32 | * IPC with socket sharing, using Unix domain sockets or named pipes (Windows) 33 | * Child processes 34 | * Thread pool 35 | * Signal handling 36 | * High resolution clock 37 | * Threading and synchronization primitives 38 | 39 | 40 | Documentation 41 | ------------- 42 | 43 | .. toctree:: 44 | :maxdepth: 1 45 | 46 | design 47 | api 48 | guide 49 | upgrading 50 | 51 | 52 | Downloads 53 | --------- 54 | 55 | libuv can be downloaded from `here `_. 56 | 57 | 58 | Installation 59 | ------------ 60 | 61 | Installation instructions can be found in `the README `_. 62 | 63 | -------------------------------------------------------------------------------- /test/test-get-memory.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | TEST_IMPL(get_memory) { 26 | uint64_t free_mem = uv_get_free_memory(); 27 | uint64_t total_mem = uv_get_total_memory(); 28 | 29 | printf("free_mem=%llu, total_mem=%llu\n", 30 | (unsigned long long) free_mem, 31 | (unsigned long long) total_mem); 32 | 33 | ASSERT(free_mem > 0); 34 | ASSERT(total_mem > 0); 35 | ASSERT(total_mem > free_mem); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /src/unix/sysinfo-memory.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | uint64_t uv_get_free_memory(void) { 29 | struct sysinfo info; 30 | 31 | if (sysinfo(&info) == 0) 32 | return (uint64_t) info.freeram * info.mem_unit; 33 | return 0; 34 | } 35 | 36 | uint64_t uv_get_total_memory(void) { 37 | struct sysinfo info; 38 | 39 | if (sysinfo(&info) == 0) 40 | return (uint64_t) info.totalram * info.mem_unit; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /src/unix/procfs-exepath.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | int uv_exepath(char* buffer, size_t* size) { 29 | ssize_t n; 30 | 31 | if (buffer == NULL || size == NULL || *size == 0) 32 | return UV_EINVAL; 33 | 34 | n = *size - 1; 35 | if (n > 0) 36 | n = readlink("/proc/self/exe", buffer, n); 37 | 38 | if (n == -1) 39 | return UV__ERR(errno); 40 | 41 | buffer[n] = '\0'; 42 | *size = n; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /src/win/snprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright the libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #if defined(_MSC_VER) && _MSC_VER < 1900 23 | 24 | #include 25 | #include 26 | 27 | /* Emulate snprintf() on MSVC<2015, _snprintf() doesn't zero-terminate the buffer 28 | * on overflow... 29 | */ 30 | int snprintf(char* buf, size_t len, const char* fmt, ...) { 31 | int n; 32 | va_list ap; 33 | va_start(ap, fmt); 34 | 35 | n = _vscprintf(fmt, ap); 36 | vsnprintf_s(buf, len, _TRUNCATE, fmt, ap); 37 | 38 | va_end(ap); 39 | return n; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /include/uv/posix.h: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_POSIX_H 23 | #define UV_POSIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | struct pollfd* poll_fds; \ 27 | size_t poll_fds_used; \ 28 | size_t poll_fds_size; \ 29 | unsigned char poll_fds_iterating; \ 30 | 31 | #endif /* UV_POSIX_H */ 32 | -------------------------------------------------------------------------------- /docs/code/signal/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | uv_loop_t* create_loop() 7 | { 8 | uv_loop_t *loop = malloc(sizeof(uv_loop_t)); 9 | if (loop) { 10 | uv_loop_init(loop); 11 | } 12 | return loop; 13 | } 14 | 15 | void signal_handler(uv_signal_t *handle, int signum) 16 | { 17 | printf("Signal received: %d\n", signum); 18 | uv_signal_stop(handle); 19 | } 20 | 21 | // two signal handlers in one loop 22 | void thread1_worker(void *userp) 23 | { 24 | uv_loop_t *loop1 = create_loop(); 25 | 26 | uv_signal_t sig1a, sig1b; 27 | uv_signal_init(loop1, &sig1a); 28 | uv_signal_start(&sig1a, signal_handler, SIGUSR1); 29 | 30 | uv_signal_init(loop1, &sig1b); 31 | uv_signal_start(&sig1b, signal_handler, SIGUSR1); 32 | 33 | uv_run(loop1, UV_RUN_DEFAULT); 34 | } 35 | 36 | // two signal handlers, each in its own loop 37 | void thread2_worker(void *userp) 38 | { 39 | uv_loop_t *loop2 = create_loop(); 40 | uv_loop_t *loop3 = create_loop(); 41 | 42 | uv_signal_t sig2; 43 | uv_signal_init(loop2, &sig2); 44 | uv_signal_start(&sig2, signal_handler, SIGUSR1); 45 | 46 | uv_signal_t sig3; 47 | uv_signal_init(loop3, &sig3); 48 | uv_signal_start(&sig3, signal_handler, SIGUSR1); 49 | 50 | while (uv_run(loop2, UV_RUN_NOWAIT) || uv_run(loop3, UV_RUN_NOWAIT)) { 51 | } 52 | } 53 | 54 | int main() 55 | { 56 | printf("PID %d\n", getpid()); 57 | 58 | uv_thread_t thread1, thread2; 59 | 60 | uv_thread_create(&thread1, thread1_worker, 0); 61 | uv_thread_create(&thread2, thread2_worker, 0); 62 | 63 | uv_thread_join(&thread1); 64 | uv_thread_join(&thread2); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /include/uv/aix.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_AIX_H 23 | #define UV_AIX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | int fs_fd; \ 27 | 28 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 29 | uv__io_t event_watcher; \ 30 | char *dir_filename; \ 31 | 32 | #endif /* UV_AIX_H */ 33 | -------------------------------------------------------------------------------- /test/runner-win.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | /* Don't complain about write(), fileno() etc. being deprecated. */ 23 | #ifdef _MSC_VER 24 | #pragma warning(disable : 4996) 25 | #endif 26 | 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900 33 | extern int snprintf(char*, size_t, const char*, ...); 34 | #endif 35 | 36 | typedef struct { 37 | HANDLE process; 38 | HANDLE stdio_in; 39 | HANDLE stdio_out; 40 | char *name; 41 | } process_info_t; 42 | -------------------------------------------------------------------------------- /src/unix/no-fsevents.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | 27 | int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle) { 28 | return UV_ENOSYS; 29 | } 30 | 31 | int uv_fs_event_start(uv_fs_event_t* handle, uv_fs_event_cb cb, 32 | const char* filename, unsigned int flags) { 33 | return UV_ENOSYS; 34 | } 35 | 36 | int uv_fs_event_stop(uv_fs_event_t* handle) { 37 | return UV_ENOSYS; 38 | } 39 | 40 | void uv__fs_event_close(uv_fs_event_t* handle) { 41 | UNREACHABLE(); 42 | } 43 | -------------------------------------------------------------------------------- /docs/src/version.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _version: 3 | 4 | Version-checking macros and functions 5 | ===================================== 6 | 7 | Starting with version 1.0.0 libuv follows the `semantic versioning`_ 8 | scheme. This means that new APIs can be introduced throughout the lifetime of 9 | a major release. In this section you'll find all macros and functions that 10 | will allow you to write or compile code conditionally, in order to work with 11 | multiple libuv versions. 12 | 13 | .. _semantic versioning: http://semver.org 14 | 15 | 16 | Macros 17 | ------ 18 | 19 | .. c:macro:: UV_VERSION_MAJOR 20 | 21 | libuv version's major number. 22 | 23 | .. c:macro:: UV_VERSION_MINOR 24 | 25 | libuv version's minor number. 26 | 27 | .. c:macro:: UV_VERSION_PATCH 28 | 29 | libuv version's patch number. 30 | 31 | .. c:macro:: UV_VERSION_IS_RELEASE 32 | 33 | Set to 1 to indicate a release version of libuv, 0 for a development 34 | snapshot. 35 | 36 | .. c:macro:: UV_VERSION_SUFFIX 37 | 38 | libuv version suffix. Certain development releases such as Release Candidates 39 | might have a suffix such as "rc". 40 | 41 | .. c:macro:: UV_VERSION_HEX 42 | 43 | Returns the libuv version packed into a single integer. 8 bits are used for 44 | each component, with the patch number stored in the 8 least significant 45 | bits. E.g. for libuv 1.2.3 this would be 0x010203. 46 | 47 | .. versionadded:: 1.7.0 48 | 49 | 50 | Functions 51 | --------- 52 | 53 | .. c:function:: unsigned int uv_version(void) 54 | 55 | Returns :c:macro:`UV_VERSION_HEX`. 56 | 57 | .. c:function:: const char* uv_version_string(void) 58 | 59 | Returns the libuv version number as a string. For non-release versions the 60 | version suffix is included. 61 | -------------------------------------------------------------------------------- /include/uv/bsd.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_BSD_H 23 | #define UV_BSD_H 24 | 25 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 26 | uv__io_t event_watcher; \ 27 | 28 | #define UV_IO_PRIVATE_PLATFORM_FIELDS \ 29 | int rcount; \ 30 | int wcount; \ 31 | 32 | #define UV_HAVE_KQUEUE 1 33 | 34 | #endif /* UV_BSD_H */ 35 | -------------------------------------------------------------------------------- /test/test-run-nowait.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static uv_timer_t timer_handle; 26 | static int timer_called = 0; 27 | 28 | 29 | static void timer_cb(uv_timer_t* handle) { 30 | ASSERT(handle == &timer_handle); 31 | timer_called = 1; 32 | } 33 | 34 | 35 | TEST_IMPL(run_nowait) { 36 | int r; 37 | uv_timer_init(uv_default_loop(), &timer_handle); 38 | uv_timer_start(&timer_handle, timer_cb, 100, 100); 39 | 40 | r = uv_run(uv_default_loop(), UV_RUN_NOWAIT); 41 | ASSERT(r != 0); 42 | ASSERT(timer_called == 0); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /samples/socks5-proxy/build.gyp: -------------------------------------------------------------------------------- 1 | # Copyright StrongLoop, Inc. All rights reserved. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to 5 | # deal in the Software without restriction, including without limitation the 6 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | # sell copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in 11 | # all copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | # IN THE SOFTWARE. 20 | 21 | { 22 | 'targets': [ 23 | { 24 | 'dependencies': ['../../uv.gyp:libuv'], 25 | 'target_name': 's5-proxy', 26 | 'type': 'executable', 27 | 'sources': [ 28 | 'client.c', 29 | 'defs.h', 30 | 'main.c', 31 | 's5.c', 32 | 's5.h', 33 | 'server.c', 34 | 'util.c', 35 | ], 36 | 'conditions': [ 37 | ['OS=="win"', { 38 | 'defines': ['HAVE_UNISTD_H=0'], 39 | 'sources': ['getopt.c'] 40 | }, { 41 | 'defines': ['HAVE_UNISTD_H=1'] 42 | }] 43 | ] 44 | } 45 | ] 46 | } 47 | -------------------------------------------------------------------------------- /samples/socks5-proxy/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright StrongLoop, Inc. All rights reserved. 2 | # 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to 5 | # deal in the Software without restriction, including without limitation the 6 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | # sell copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | # 10 | # The above copyright notice and this permission notice shall be included in 11 | # all copies or substantial portions of the Software. 12 | # 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | # IN THE SOFTWARE. 20 | 21 | BUILDTYPE ?= Debug 22 | BUILDDIR ?= build 23 | GYP ?= gyp 24 | V ?= 25 | 26 | SOURCES := client.c defs.h getopt.c main.c s5.c s5.h server.c util.c 27 | 28 | .PHONY: all clean 29 | 30 | all: $(BUILDDIR)/$(BUILDTYPE)/s5-proxy 31 | 32 | clean: 33 | $(RM) $(BUILDDIR) 34 | 35 | $(BUILDDIR)/$(BUILDTYPE)/s5-proxy: $(BUILDDIR)/Makefile $(SOURCES) 36 | $(MAKE) -C $(BUILDDIR) V=$(V) 37 | 38 | $(BUILDDIR)/Makefile: ../../common.gypi build.gyp 39 | $(GYP) \ 40 | -Duv_library=static_library \ 41 | -Goutput_dir=. \ 42 | -I../../common.gypi \ 43 | -f make \ 44 | --depth=. \ 45 | --generator-output=$(BUILDDIR) \ 46 | build.gyp 47 | -------------------------------------------------------------------------------- /test/test-run-once.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #define NUM_TICKS 64 26 | 27 | static uv_idle_t idle_handle; 28 | static int idle_counter; 29 | 30 | 31 | static void idle_cb(uv_idle_t* handle) { 32 | ASSERT(handle == &idle_handle); 33 | 34 | if (++idle_counter == NUM_TICKS) 35 | uv_idle_stop(handle); 36 | } 37 | 38 | 39 | TEST_IMPL(run_once) { 40 | uv_idle_init(uv_default_loop(), &idle_handle); 41 | uv_idle_start(&idle_handle, idle_cb); 42 | 43 | while (uv_run(uv_default_loop(), UV_RUN_ONCE)); 44 | ASSERT(idle_counter == NUM_TICKS); 45 | 46 | MAKE_VALGRIND_HAPPY(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /test/test-tcp-flags.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #include 26 | #include 27 | 28 | 29 | TEST_IMPL(tcp_flags) { 30 | uv_loop_t* loop; 31 | uv_tcp_t handle; 32 | int r; 33 | 34 | loop = uv_default_loop(); 35 | 36 | r = uv_tcp_init(loop, &handle); 37 | ASSERT(r == 0); 38 | 39 | r = uv_tcp_nodelay(&handle, 1); 40 | ASSERT(r == 0); 41 | 42 | r = uv_tcp_keepalive(&handle, 1, 60); 43 | ASSERT(r == 0); 44 | 45 | uv_close((uv_handle_t*)&handle, NULL); 46 | 47 | r = uv_run(loop, UV_RUN_DEFAULT); 48 | ASSERT(r == 0); 49 | 50 | MAKE_VALGRIND_HAPPY(); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /docs/src/async.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _async: 3 | 4 | :c:type:`uv_async_t` --- Async handle 5 | ===================================== 6 | 7 | Async handles allow the user to "wakeup" the event loop and get a callback 8 | called from another thread. 9 | 10 | 11 | Data types 12 | ---------- 13 | 14 | .. c:type:: uv_async_t 15 | 16 | Async handle type. 17 | 18 | .. c:type:: void (*uv_async_cb)(uv_async_t* handle) 19 | 20 | Type definition for callback passed to :c:func:`uv_async_init`. 21 | 22 | 23 | Public members 24 | ^^^^^^^^^^^^^^ 25 | 26 | N/A 27 | 28 | .. seealso:: The :c:type:`uv_handle_t` members also apply. 29 | 30 | 31 | API 32 | --- 33 | 34 | .. c:function:: int uv_async_init(uv_loop_t* loop, uv_async_t* async, uv_async_cb async_cb) 35 | 36 | Initialize the handle. A NULL callback is allowed. 37 | 38 | :returns: 0 on success, or an error code < 0 on failure. 39 | 40 | .. note:: 41 | Unlike other handle initialization functions, it immediately starts the handle. 42 | 43 | .. c:function:: int uv_async_send(uv_async_t* async) 44 | 45 | Wake up the event loop and call the async handle's callback. 46 | 47 | :returns: 0 on success, or an error code < 0 on failure. 48 | 49 | .. note:: 50 | It's safe to call this function from any thread. The callback will be called on the 51 | loop thread. 52 | 53 | .. warning:: 54 | libuv will coalesce calls to :c:func:`uv_async_send`, that is, not every call to it will 55 | yield an execution of the callback. For example: if :c:func:`uv_async_send` is called 5 56 | times in a row before the callback is called, the callback will only be called once. If 57 | :c:func:`uv_async_send` is called again after the callback was called, it will be called 58 | again. 59 | 60 | .. seealso:: 61 | The :c:type:`uv_handle_t` API functions also apply. 62 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Copyright (c) 2013, Ben Noordhuis 4 | # 5 | # Permission to use, copy, modify, and/or distribute this software for any 6 | # purpose with or without fee is hereby granted, provided that the above 7 | # copyright notice and this permission notice appear in all copies. 8 | # 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | 17 | cd `dirname "$0"` 18 | 19 | if [ "$LIBTOOLIZE" = "" ] && [ "`uname`" = "Darwin" ]; then 20 | LIBTOOLIZE=glibtoolize 21 | fi 22 | 23 | ACLOCAL=${ACLOCAL:-aclocal} 24 | AUTOCONF=${AUTOCONF:-autoconf} 25 | AUTOMAKE=${AUTOMAKE:-automake} 26 | LIBTOOLIZE=${LIBTOOLIZE:-libtoolize} 27 | 28 | automake_version=`"$AUTOMAKE" --version | head -n 1 | sed 's/[^.0-9]//g'` 29 | automake_version_major=`echo "$automake_version" | cut -d. -f1` 30 | automake_version_minor=`echo "$automake_version" | cut -d. -f2` 31 | 32 | UV_EXTRA_AUTOMAKE_FLAGS= 33 | if test "$automake_version_major" -gt 1 || \ 34 | test "$automake_version_major" -eq 1 && \ 35 | test "$automake_version_minor" -gt 11; then 36 | # serial-tests is available in v1.12 and newer. 37 | UV_EXTRA_AUTOMAKE_FLAGS="$UV_EXTRA_AUTOMAKE_FLAGS serial-tests" 38 | fi 39 | echo "m4_define([UV_EXTRA_AUTOMAKE_FLAGS], [$UV_EXTRA_AUTOMAKE_FLAGS])" \ 40 | > m4/libuv-extra-automake-flags.m4 41 | 42 | set -ex 43 | "$LIBTOOLIZE" --copy 44 | "$ACLOCAL" -I m4 45 | "$AUTOCONF" 46 | "$AUTOMAKE" --add-missing --copy 47 | -------------------------------------------------------------------------------- /include/uv/linux.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_LINUX_H 23 | #define UV_LINUX_H 24 | 25 | #define UV_PLATFORM_LOOP_FIELDS \ 26 | uv__io_t inotify_read_watcher; \ 27 | void* inotify_watchers; \ 28 | int inotify_fd; \ 29 | 30 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 31 | void* watchers[2]; \ 32 | int wd; \ 33 | 34 | #endif /* UV_LINUX_H */ 35 | -------------------------------------------------------------------------------- /test/test-cwd-and-chdir.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | #define PATHMAX 1024 27 | extern char executable_path[]; 28 | 29 | TEST_IMPL(cwd_and_chdir) { 30 | char buffer_orig[PATHMAX]; 31 | char buffer_new[PATHMAX]; 32 | size_t size1; 33 | size_t size2; 34 | int err; 35 | 36 | size1 = sizeof buffer_orig; 37 | err = uv_cwd(buffer_orig, &size1); 38 | ASSERT(err == 0); 39 | 40 | err = uv_chdir(buffer_orig); 41 | ASSERT(err == 0); 42 | 43 | size2 = sizeof buffer_new; 44 | err = uv_cwd(buffer_new, &size2); 45 | ASSERT(err == 0); 46 | 47 | ASSERT(size1 == size2); 48 | ASSERT(strcmp(buffer_orig, buffer_new) == 0); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /src/version.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | 24 | #define UV_STRINGIFY(v) UV_STRINGIFY_HELPER(v) 25 | #define UV_STRINGIFY_HELPER(v) #v 26 | 27 | #define UV_VERSION_STRING_BASE UV_STRINGIFY(UV_VERSION_MAJOR) "." \ 28 | UV_STRINGIFY(UV_VERSION_MINOR) "." \ 29 | UV_STRINGIFY(UV_VERSION_PATCH) 30 | 31 | #if UV_VERSION_IS_RELEASE 32 | # define UV_VERSION_STRING UV_VERSION_STRING_BASE 33 | #else 34 | # define UV_VERSION_STRING UV_VERSION_STRING_BASE "-" UV_VERSION_SUFFIX 35 | #endif 36 | 37 | 38 | unsigned int uv_version(void) { 39 | return UV_VERSION_HEX; 40 | } 41 | 42 | 43 | const char* uv_version_string(void) { 44 | return UV_VERSION_STRING; 45 | } 46 | -------------------------------------------------------------------------------- /docs/code/uvcat/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void on_read(uv_fs_t *req); 8 | 9 | uv_fs_t open_req; 10 | uv_fs_t read_req; 11 | uv_fs_t write_req; 12 | 13 | static char buffer[1024]; 14 | 15 | static uv_buf_t iov; 16 | 17 | void on_write(uv_fs_t *req) { 18 | if (req->result < 0) { 19 | fprintf(stderr, "Write error: %s\n", uv_strerror((int)req->result)); 20 | } 21 | else { 22 | uv_fs_read(uv_default_loop(), &read_req, open_req.result, &iov, 1, -1, on_read); 23 | } 24 | } 25 | 26 | void on_read(uv_fs_t *req) { 27 | if (req->result < 0) { 28 | fprintf(stderr, "Read error: %s\n", uv_strerror(req->result)); 29 | } 30 | else if (req->result == 0) { 31 | uv_fs_t close_req; 32 | // synchronous 33 | uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL); 34 | } 35 | else if (req->result > 0) { 36 | iov.len = req->result; 37 | uv_fs_write(uv_default_loop(), &write_req, 1, &iov, 1, -1, on_write); 38 | } 39 | } 40 | 41 | void on_open(uv_fs_t *req) { 42 | // The request passed to the callback is the same as the one the call setup 43 | // function was passed. 44 | assert(req == &open_req); 45 | if (req->result >= 0) { 46 | iov = uv_buf_init(buffer, sizeof(buffer)); 47 | uv_fs_read(uv_default_loop(), &read_req, req->result, 48 | &iov, 1, -1, on_read); 49 | } 50 | else { 51 | fprintf(stderr, "error opening file: %s\n", uv_strerror((int)req->result)); 52 | } 53 | } 54 | 55 | int main(int argc, char **argv) { 56 | uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open); 57 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 58 | 59 | uv_fs_req_cleanup(&open_req); 60 | uv_fs_req_cleanup(&read_req); 61 | uv_fs_req_cleanup(&write_req); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /src/unix/cygwin.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | 28 | int uv_uptime(double* uptime) { 29 | struct sysinfo info; 30 | 31 | if (sysinfo(&info) < 0) 32 | return UV__ERR(errno); 33 | 34 | *uptime = info.uptime; 35 | return 0; 36 | } 37 | 38 | int uv_resident_set_memory(size_t* rss) { 39 | /* FIXME: read /proc/meminfo? */ 40 | *rss = 0; 41 | return 0; 42 | } 43 | 44 | int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { 45 | /* FIXME: read /proc/stat? */ 46 | *cpu_infos = NULL; 47 | *count = 0; 48 | return UV_ENOSYS; 49 | } 50 | 51 | void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) { 52 | (void)cpu_infos; 53 | (void)count; 54 | } 55 | -------------------------------------------------------------------------------- /include/uv/version.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_VERSION_H 23 | #define UV_VERSION_H 24 | 25 | /* 26 | * Versions with the same major number are ABI stable. API is allowed to 27 | * evolve between minor releases, but only in a backwards compatible way. 28 | * Make sure you update the -soname directives in configure.ac 29 | * and uv.gyp whenever you bump UV_VERSION_MAJOR or UV_VERSION_MINOR (but 30 | * not UV_VERSION_PATCH.) 31 | */ 32 | 33 | #define UV_VERSION_MAJOR 1 34 | #define UV_VERSION_MINOR 24 35 | #define UV_VERSION_PATCH 2 36 | #define UV_VERSION_IS_RELEASE 0 37 | #define UV_VERSION_SUFFIX "dev" 38 | 39 | #define UV_VERSION_HEX ((UV_VERSION_MAJOR << 16) | \ 40 | (UV_VERSION_MINOR << 8) | \ 41 | (UV_VERSION_PATCH)) 42 | 43 | #endif /* UV_VERSION_H */ 44 | -------------------------------------------------------------------------------- /test/test-hrtime.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #ifndef MILLISEC 26 | # define MILLISEC 1000 27 | #endif 28 | 29 | #ifndef NANOSEC 30 | # define NANOSEC ((uint64_t) 1e9) 31 | #endif 32 | 33 | 34 | TEST_IMPL(hrtime) { 35 | uint64_t a, b, diff; 36 | int i = 75; 37 | while (i > 0) { 38 | a = uv_hrtime(); 39 | uv_sleep(45); 40 | b = uv_hrtime(); 41 | 42 | diff = b - a; 43 | 44 | /* printf("i= %d diff = %llu\n", i, (unsigned long long int) diff); */ 45 | 46 | /* The windows Sleep() function has only a resolution of 10-20 ms. Check 47 | * that the difference between the two hrtime values is somewhat in the 48 | * range we expect it to be. */ 49 | ASSERT(diff > (uint64_t) 25 * NANOSEC / MILLISEC); 50 | ASSERT(diff < (uint64_t) 80 * NANOSEC / MILLISEC); 51 | --i; 52 | } 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /include/uv/android-ifaddrs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 1995, 1999 3 | * Berkeley Software Design, Inc. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 12 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 14 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 15 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 16 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 17 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 18 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 19 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 20 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 21 | * SUCH DAMAGE. 22 | * 23 | * BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp 24 | */ 25 | 26 | #ifndef _IFADDRS_H_ 27 | #define _IFADDRS_H_ 28 | 29 | struct ifaddrs { 30 | struct ifaddrs *ifa_next; 31 | char *ifa_name; 32 | unsigned int ifa_flags; 33 | struct sockaddr *ifa_addr; 34 | struct sockaddr *ifa_netmask; 35 | struct sockaddr *ifa_dstaddr; 36 | void *ifa_data; 37 | }; 38 | 39 | /* 40 | * This may have been defined in . Note that if is 41 | * to be included it must be included before this header file. 42 | */ 43 | #ifndef ifa_broadaddr 44 | #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 45 | #endif 46 | 47 | #include 48 | 49 | __BEGIN_DECLS 50 | extern int getifaddrs(struct ifaddrs **ifap); 51 | extern void freeifaddrs(struct ifaddrs *ifa); 52 | __END_DECLS 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /test/test-thread-equal.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | uv_thread_t main_thread_id; 26 | uv_thread_t subthreads[2]; 27 | 28 | static void check_thread(void* arg) { 29 | uv_thread_t *thread_id = arg; 30 | uv_thread_t self_id = uv_thread_self(); 31 | ASSERT(uv_thread_equal(&main_thread_id, &self_id) == 0); 32 | *thread_id = uv_thread_self(); 33 | } 34 | 35 | TEST_IMPL(thread_equal) { 36 | uv_thread_t threads[2]; 37 | main_thread_id = uv_thread_self(); 38 | ASSERT(0 != uv_thread_equal(&main_thread_id, &main_thread_id)); 39 | ASSERT(0 == uv_thread_create(threads + 0, check_thread, subthreads + 0)); 40 | ASSERT(0 == uv_thread_create(threads + 1, check_thread, subthreads + 1)); 41 | ASSERT(0 == uv_thread_join(threads + 0)); 42 | ASSERT(0 == uv_thread_join(threads + 1)); 43 | ASSERT(0 == uv_thread_equal(subthreads + 0, subthreads + 1)); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /test/test-strscpy.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | #include "../src/strscpy.h" 27 | #include "../src/strscpy.c" 28 | 29 | TEST_IMPL(strscpy) { 30 | char d[4]; 31 | 32 | ASSERT(0 == uv__strscpy(d, "", 0)); 33 | ASSERT(0 == uv__strscpy(d, "x", 0)); 34 | 35 | memset(d, 0, sizeof(d)); 36 | ASSERT(1 == uv__strscpy(d, "x", sizeof(d))); 37 | ASSERT(0 == memcmp(d, "x\0\0", sizeof(d))); 38 | 39 | memset(d, 0, sizeof(d)); 40 | ASSERT(2 == uv__strscpy(d, "xy", sizeof(d))); 41 | ASSERT(0 == memcmp(d, "xy\0", sizeof(d))); 42 | 43 | ASSERT(3 == uv__strscpy(d, "xyz", sizeof(d))); 44 | ASSERT(0 == memcmp(d, "xyz", sizeof(d))); 45 | 46 | ASSERT(UV_E2BIG == uv__strscpy(d, "xyzz", sizeof(d))); 47 | ASSERT(0 == memcmp(d, "xyz", sizeof(d))); 48 | 49 | ASSERT(UV_E2BIG == uv__strscpy(d, "xyzzy", sizeof(d))); 50 | ASSERT(0 == memcmp(d, "xyz", sizeof(d))); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /test/test-pipe-pending-instances.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2015 Saúl Ibarra Corretgé . 2 | * All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | */ 22 | 23 | #include "uv.h" 24 | #include "task.h" 25 | 26 | 27 | static void connection_cb(uv_stream_t* server, int status) { 28 | ASSERT(0 && "this will never be called"); 29 | } 30 | 31 | 32 | TEST_IMPL(pipe_pending_instances) { 33 | int r; 34 | uv_pipe_t pipe_handle; 35 | uv_loop_t* loop; 36 | 37 | loop = uv_default_loop(); 38 | 39 | r = uv_pipe_init(loop, &pipe_handle, 0); 40 | ASSERT(r == 0); 41 | 42 | uv_pipe_pending_instances(&pipe_handle, 8); 43 | 44 | r = uv_pipe_bind(&pipe_handle, TEST_PIPENAME); 45 | ASSERT(r == 0); 46 | 47 | uv_pipe_pending_instances(&pipe_handle, 16); 48 | 49 | r = uv_listen((uv_stream_t*)&pipe_handle, 128, connection_cb); 50 | ASSERT(r == 0); 51 | 52 | uv_close((uv_handle_t*)&pipe_handle, NULL); 53 | 54 | r = uv_run(loop, UV_RUN_DEFAULT); 55 | ASSERT(r == 0); 56 | 57 | MAKE_VALGRIND_HAPPY(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /include/uv/sunos.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_SUNOS_H 23 | #define UV_SUNOS_H 24 | 25 | #include 26 | #include 27 | 28 | /* For the sake of convenience and reduced #ifdef-ery in src/unix/sunos.c, 29 | * add the fs_event fields even when this version of SunOS doesn't support 30 | * file watching. 31 | */ 32 | #define UV_PLATFORM_LOOP_FIELDS \ 33 | uv__io_t fs_event_watcher; \ 34 | int fs_fd; \ 35 | 36 | #if defined(PORT_SOURCE_FILE) 37 | 38 | # define UV_PLATFORM_FS_EVENT_FIELDS \ 39 | file_obj_t fo; \ 40 | int fd; \ 41 | 42 | #endif /* defined(PORT_SOURCE_FILE) */ 43 | 44 | #endif /* UV_SUNOS_H */ 45 | -------------------------------------------------------------------------------- /test/test-dlerror.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | 27 | TEST_IMPL(dlerror) { 28 | const char* path = "test/fixtures/load_error.node"; 29 | const char* dlerror_no_error = "no error"; 30 | const char* msg; 31 | uv_lib_t lib; 32 | int r; 33 | 34 | lib.errmsg = NULL; 35 | lib.handle = NULL; 36 | msg = uv_dlerror(&lib); 37 | ASSERT(msg != NULL); 38 | ASSERT(strstr(msg, dlerror_no_error) != NULL); 39 | 40 | r = uv_dlopen(path, &lib); 41 | ASSERT(r == -1); 42 | 43 | msg = uv_dlerror(&lib); 44 | ASSERT(msg != NULL); 45 | ASSERT(strstr(msg, path) != NULL); 46 | ASSERT(strstr(msg, dlerror_no_error) == NULL); 47 | 48 | /* Should return the same error twice in a row. */ 49 | msg = uv_dlerror(&lib); 50 | ASSERT(msg != NULL); 51 | ASSERT(strstr(msg, path) != NULL); 52 | ASSERT(strstr(msg, dlerror_no_error) == NULL); 53 | 54 | uv_dlclose(&lib); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/unix/spinlock.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013, Ben Noordhuis 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef UV_SPINLOCK_H_ 17 | #define UV_SPINLOCK_H_ 18 | 19 | #include "internal.h" /* ACCESS_ONCE, UV_UNUSED */ 20 | #include "atomic-ops.h" 21 | 22 | #define UV_SPINLOCK_INITIALIZER { 0 } 23 | 24 | typedef struct { 25 | int lock; 26 | } uv_spinlock_t; 27 | 28 | UV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock)); 29 | UV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock)); 30 | UV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock)); 31 | UV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock)); 32 | 33 | UV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock)) { 34 | ACCESS_ONCE(int, spinlock->lock) = 0; 35 | } 36 | 37 | UV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock)) { 38 | while (!uv_spinlock_trylock(spinlock)) cpu_relax(); 39 | } 40 | 41 | UV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock)) { 42 | ACCESS_ONCE(int, spinlock->lock) = 0; 43 | } 44 | 45 | UV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock)) { 46 | /* TODO(bnoordhuis) Maybe change to a ticket lock to guarantee fair queueing. 47 | * Not really critical until we have locks that are (frequently) contended 48 | * for by several threads. 49 | */ 50 | return 0 == cmpxchgi(&spinlock->lock, 0, 1); 51 | } 52 | 53 | #endif /* UV_SPINLOCK_H_ */ 54 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | 2 | # Project Maintainers 3 | 4 | libuv is currently managed by the following individuals: 5 | 6 | * **Anna Henningsen** ([@addaleax](https://github.com/addaleax)) 7 | * **Bartosz Sosnowski** ([@bzoz](https://github.com/bzoz)) 8 | * **Ben Noordhuis** ([@bnoordhuis](https://github.com/bnoordhuis)) 9 | - GPG key: D77B 1E34 243F BAF0 5F8E 9CC3 4F55 C8C8 46AB 89B9 (pubkey-bnoordhuis) 10 | * **Bert Belder** ([@piscisaureus](https://github.com/piscisaureus)) 11 | * **Colin Ihrig** ([@cjihrig](https://github.com/cjihrig)) 12 | - GPG key: 94AE 3667 5C46 4D64 BAFA 68DD 7434 390B DBE9 B9C5 (pubkey-cjihrig) 13 | - GPG key: 5735 3E0D BDAA A7E8 39B6 6A1A FF47 D5E4 AD8B 4FDC (pubkey-cjihrig-kb) 14 | * **Fedor Indutny** ([@indutny](https://github.com/indutny)) 15 | - GPG key: AF2E EA41 EC34 47BF DD86 FED9 D706 3CCE 19B7 E890 (pubkey-indutny) 16 | * **Imran Iqbal** ([@imran-iq](https://github.com/imran-iq)) 17 | - GPG key: 9DFE AA5F 481B BF77 2D90 03CE D592 4925 2F8E C41A (pubkey-iwuzhere) 18 | * **Jameson Nash** ([@vtjnash](https://github.com/vtjnash)) 19 | * **John Barboza** ([@jbarz](https://github.com/jbarz)) 20 | * **Santiago Gimeno** ([@santigimeno](https://github.com/santigimeno)) 21 | - GPG key: 612F 0EAD 9401 6223 79DF 4402 F28C 3C8D A33C 03BE (pubkey-santigimeno) 22 | * **Saúl Ibarra Corretgé** ([@saghul](https://github.com/saghul)) 23 | - GPG key: FDF5 1936 4458 319F A823 3DC9 410E 5553 AE9B C059 (pubkey-saghul) 24 | 25 | ## Storing a maintainer key in Git 26 | 27 | It's quite handy to store a maintainer's signature as a git blob, and have 28 | that object tagged and signed with such key. 29 | 30 | Export your public key: 31 | 32 | $ gpg --armor --export saghul@gmail.com > saghul.asc 33 | 34 | Store it as a blob on the repo: 35 | 36 | $ git hash-object -w saghul.asc 37 | 38 | The previous command returns a hash, copy it. For the sake of this explanation, 39 | we'll assume it's 'abcd1234'. Storing the blob in git is not enough, it could 40 | be garbage collected since nothing references it, so we'll create a tag for it: 41 | 42 | $ git tag -s pubkey-saghul abcd1234 43 | 44 | Commit the changes and push: 45 | 46 | $ git push origin pubkey-saghul 47 | -------------------------------------------------------------------------------- /test/benchmark-thread.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #include 26 | #include 27 | 28 | #define NUM_THREADS (20 * 1000) 29 | 30 | static volatile int num_threads; 31 | 32 | 33 | static void thread_entry(void* arg) { 34 | ASSERT(arg == (void *) 42); 35 | num_threads++; 36 | /* FIXME write barrier? */ 37 | } 38 | 39 | 40 | BENCHMARK_IMPL(thread_create) { 41 | uint64_t start_time; 42 | double duration; 43 | uv_thread_t tid; 44 | int i, r; 45 | 46 | start_time = uv_hrtime(); 47 | 48 | for (i = 0; i < NUM_THREADS; i++) { 49 | r = uv_thread_create(&tid, thread_entry, (void *) 42); 50 | ASSERT(r == 0); 51 | 52 | r = uv_thread_join(&tid); 53 | ASSERT(r == 0); 54 | } 55 | 56 | duration = (uv_hrtime() - start_time) / 1e9; 57 | 58 | ASSERT(num_threads == NUM_THREADS); 59 | 60 | printf("%d threads created in %.2f seconds (%.0f/s)\n", 61 | NUM_THREADS, duration, NUM_THREADS / duration); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /src/win/stream-inl.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_WIN_STREAM_INL_H_ 23 | #define UV_WIN_STREAM_INL_H_ 24 | 25 | #include 26 | 27 | #include "uv.h" 28 | #include "internal.h" 29 | #include "handle-inl.h" 30 | #include "req-inl.h" 31 | 32 | 33 | INLINE static void uv_stream_init(uv_loop_t* loop, 34 | uv_stream_t* handle, 35 | uv_handle_type type) { 36 | uv__handle_init(loop, (uv_handle_t*) handle, type); 37 | handle->write_queue_size = 0; 38 | handle->activecnt = 0; 39 | handle->stream.conn.shutdown_req = NULL; 40 | handle->stream.conn.write_reqs_pending = 0; 41 | 42 | UV_REQ_INIT(&handle->read_req, UV_READ); 43 | handle->read_req.event_handle = NULL; 44 | handle->read_req.wait_handle = INVALID_HANDLE_VALUE; 45 | handle->read_req.data = handle; 46 | } 47 | 48 | 49 | INLINE static void uv_connection_init(uv_stream_t* handle) { 50 | handle->flags |= UV_HANDLE_CONNECTION; 51 | } 52 | 53 | 54 | #endif /* UV_WIN_STREAM_INL_H_ */ 55 | -------------------------------------------------------------------------------- /test/test-default-loop-close.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | 26 | static int timer_cb_called; 27 | 28 | 29 | static void timer_cb(uv_timer_t* timer) { 30 | timer_cb_called++; 31 | uv_close((uv_handle_t*) timer, NULL); 32 | } 33 | 34 | 35 | TEST_IMPL(default_loop_close) { 36 | uv_loop_t* loop; 37 | uv_timer_t timer_handle; 38 | 39 | loop = uv_default_loop(); 40 | ASSERT(loop != NULL); 41 | 42 | ASSERT(0 == uv_timer_init(loop, &timer_handle)); 43 | ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 1, 0)); 44 | ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); 45 | ASSERT(1 == timer_cb_called); 46 | ASSERT(0 == uv_loop_close(loop)); 47 | 48 | loop = uv_default_loop(); 49 | ASSERT(loop != NULL); 50 | 51 | ASSERT(0 == uv_timer_init(loop, &timer_handle)); 52 | ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 1, 0)); 53 | ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT)); 54 | ASSERT(2 == timer_cb_called); 55 | ASSERT(0 == uv_loop_close(loop)); 56 | 57 | MAKE_VALGRIND_HAPPY(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /docs/src/guide/eventloops.rst: -------------------------------------------------------------------------------- 1 | Advanced event loops 2 | ==================== 3 | 4 | libuv provides considerable user control over event loops, and you can achieve 5 | interesting results by juggling multiple loops. You can also embed libuv's 6 | event loop into another event loop based library -- imagine a Qt based UI, and 7 | Qt's event loop driving a libuv backend which does intensive system level 8 | tasks. 9 | 10 | Stopping an event loop 11 | ~~~~~~~~~~~~~~~~~~~~~~ 12 | 13 | ``uv_stop()`` can be used to stop an event loop. The earliest the loop will 14 | stop running is *on the next iteration*, possibly later. This means that events 15 | that are ready to be processed in this iteration of the loop will still be 16 | processed, so ``uv_stop()`` can't be used as a kill switch. When ``uv_stop()`` 17 | is called, the loop **won't** block for i/o on this iteration. The semantics of 18 | these things can be a bit difficult to understand, so let's look at 19 | ``uv_run()`` where all the control flow occurs. 20 | 21 | .. rubric:: src/unix/core.c - uv_run 22 | .. literalinclude:: ../../../src/unix/core.c 23 | :linenos: 24 | :lines: 304-324 25 | :emphasize-lines: 10,19,21 26 | 27 | ``stop_flag`` is set by ``uv_stop()``. Now all libuv callbacks are invoked 28 | within the event loop, which is why invoking ``uv_stop()`` in them will still 29 | lead to this iteration of the loop occurring. First libuv updates timers, then 30 | runs pending timer, idle and prepare callbacks, and invokes any pending I/O 31 | callbacks. If you were to call ``uv_stop()`` in any of them, ``stop_flag`` 32 | would be set. This causes ``uv_backend_timeout()`` to return ``0``, which is 33 | why the loop does not block on I/O. If on the other hand, you called 34 | ``uv_stop()`` in one of the check handlers, I/O has already finished and is not 35 | affected. 36 | 37 | ``uv_stop()`` is useful to shutdown a loop when a result has been computed or 38 | there is an error, without having to ensure that all handlers are stopped one 39 | by one. 40 | 41 | Here is a simple example that stops the loop and demonstrates how the current 42 | iteration of the loop still takes places. 43 | 44 | .. rubric:: uvstop/main.c 45 | .. literalinclude:: ../../code/uvstop/main.c 46 | :linenos: 47 | :emphasize-lines: 11 48 | 49 | -------------------------------------------------------------------------------- /test/run-benchmarks.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | #include "runner.h" 26 | #include "task.h" 27 | 28 | /* Actual benchmarks and helpers are defined in benchmark-list.h */ 29 | #include "benchmark-list.h" 30 | 31 | 32 | static int maybe_run_test(int argc, char **argv); 33 | 34 | 35 | int main(int argc, char **argv) { 36 | if (platform_init(argc, argv)) 37 | return EXIT_FAILURE; 38 | 39 | switch (argc) { 40 | case 1: return run_tests(1); 41 | case 2: return maybe_run_test(argc, argv); 42 | case 3: return run_test_part(argv[1], argv[2]); 43 | default: 44 | fprintf(stderr, "Too many arguments.\n"); 45 | fflush(stderr); 46 | return EXIT_FAILURE; 47 | } 48 | 49 | return EXIT_SUCCESS; 50 | } 51 | 52 | 53 | static int maybe_run_test(int argc, char **argv) { 54 | if (strcmp(argv[1], "--list") == 0) { 55 | print_tests(stdout); 56 | return 0; 57 | } 58 | 59 | if (strcmp(argv[1], "spawn_helper") == 0) { 60 | printf("hello world\n"); 61 | return 42; 62 | } 63 | 64 | return run_test(argv[1], 1, 1); 65 | } 66 | -------------------------------------------------------------------------------- /gyp_uv.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import platform 5 | import sys 6 | 7 | try: 8 | import multiprocessing.synchronize 9 | gyp_parallel_support = True 10 | except ImportError: 11 | gyp_parallel_support = False 12 | 13 | 14 | CC = os.environ.get('CC', 'cc') 15 | script_dir = os.path.dirname(__file__) 16 | uv_root = os.path.normpath(script_dir) 17 | output_dir = os.path.join(os.path.abspath(uv_root), 'out') 18 | 19 | sys.path.insert(0, os.path.join(uv_root, 'build', 'gyp', 'pylib')) 20 | try: 21 | import gyp 22 | except ImportError: 23 | print('You need to install gyp in build/gyp first. See the README.') 24 | sys.exit(42) 25 | 26 | 27 | def host_arch(): 28 | machine = platform.machine() 29 | if machine == 'i386': return 'ia32' 30 | if machine == 'AMD64': return 'x64' 31 | if machine == 'x86_64': return 'x64' 32 | if machine.startswith('arm'): return 'arm' 33 | if machine.startswith('mips'): return 'mips' 34 | return machine # Return as-is and hope for the best. 35 | 36 | 37 | def run_gyp(args): 38 | rc = gyp.main(args) 39 | if rc != 0: 40 | print('Error running GYP') 41 | sys.exit(rc) 42 | 43 | 44 | if __name__ == '__main__': 45 | args = sys.argv[1:] 46 | args.extend('-I common.gypi test/test.gyp'.split(' ')) 47 | args.append('--depth=' + uv_root) 48 | 49 | # There's a bug with windows which doesn't allow this feature. 50 | if sys.platform != 'win32': 51 | if '-f' not in args: 52 | args.extend('-f make'.split()) 53 | if 'eclipse' not in args and 'ninja' not in args: 54 | args.extend(['-Goutput_dir=' + output_dir]) 55 | args.extend(['--generator-output', output_dir]) 56 | 57 | if not any(a.startswith('-Dhost_arch=') for a in args): 58 | args.append('-Dhost_arch=%s' % host_arch()) 59 | 60 | if not any(a.startswith('-Dtarget_arch=') for a in args): 61 | args.append('-Dtarget_arch=%s' % host_arch()) 62 | 63 | if not any(a.startswith('-Duv_library=') for a in args): 64 | args.append('-Duv_library=static_library') 65 | 66 | # Some platforms (OpenBSD for example) don't have multiprocessing.synchronize 67 | # so gyp must be run with --no-parallel 68 | if not gyp_parallel_support: 69 | args.append('--no-parallel') 70 | 71 | gyp_args = list(args) 72 | print(gyp_args) 73 | run_gyp(gyp_args) 74 | -------------------------------------------------------------------------------- /test/test-gethostname.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | #ifndef MAXHOSTNAMELEN 27 | # define MAXHOSTNAMELEN 256 28 | #endif 29 | 30 | TEST_IMPL(gethostname) { 31 | char buf[MAXHOSTNAMELEN + 1]; 32 | size_t size; 33 | size_t enobufs_size; 34 | int r; 35 | 36 | /* Reject invalid inputs */ 37 | size = 1; 38 | r = uv_os_gethostname(NULL, &size); 39 | ASSERT(r == UV_EINVAL); 40 | r = uv_os_gethostname(buf, NULL); 41 | ASSERT(r == UV_EINVAL); 42 | size = 0; 43 | r = uv_os_gethostname(buf, &size); 44 | ASSERT(r == UV_EINVAL); 45 | 46 | /* Return UV_ENOBUFS if the buffer cannot hold the hostname */ 47 | enobufs_size = 1; 48 | buf[0] = '\0'; 49 | r = uv_os_gethostname(buf, &enobufs_size); 50 | ASSERT(r == UV_ENOBUFS); 51 | ASSERT(buf[0] == '\0'); 52 | ASSERT(enobufs_size > 1); 53 | 54 | /* Successfully get the hostname */ 55 | size = MAXHOSTNAMELEN + 1; 56 | r = uv_os_gethostname(buf, &size); 57 | ASSERT(r == 0); 58 | ASSERT(size > 1 && size == strlen(buf)); 59 | ASSERT(size + 1 == enobufs_size); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /test/test-ip4-addr.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #include 26 | #include 27 | 28 | 29 | TEST_IMPL(ip4_addr) { 30 | struct sockaddr_in addr; 31 | char dst[16]; 32 | 33 | ASSERT(0 == uv_inet_ntop(AF_INET, "\xFF\xFF\xFF\xFF", dst, sizeof(dst))); 34 | ASSERT(0 == strcmp(dst, "255.255.255.255")); 35 | ASSERT(UV_ENOSPC == uv_inet_ntop(AF_INET, "\xFF\xFF\xFF\xFF", 36 | dst, sizeof(dst) - 1)); 37 | 38 | ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); 39 | ASSERT(0 == uv_ip4_addr("255.255.255.255", TEST_PORT, &addr)); 40 | ASSERT(UV_EINVAL == uv_ip4_addr("255.255.255*000", TEST_PORT, &addr)); 41 | ASSERT(UV_EINVAL == uv_ip4_addr("255.255.255.256", TEST_PORT, &addr)); 42 | ASSERT(UV_EINVAL == uv_ip4_addr("2555.0.0.0", TEST_PORT, &addr)); 43 | ASSERT(UV_EINVAL == uv_ip4_addr("255", TEST_PORT, &addr)); 44 | 45 | /* for broken address family */ 46 | ASSERT(UV_EAFNOSUPPORT == uv_inet_pton(42, "127.0.0.1", 47 | &addr.sin_addr.s_addr)); 48 | 49 | MAKE_VALGRIND_HAPPY(); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /test/test-loop-time.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | 26 | TEST_IMPL(loop_update_time) { 27 | uint64_t start; 28 | 29 | start = uv_now(uv_default_loop()); 30 | while (uv_now(uv_default_loop()) - start < 1000) 31 | ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_NOWAIT)); 32 | 33 | MAKE_VALGRIND_HAPPY(); 34 | return 0; 35 | } 36 | 37 | static void cb(uv_timer_t* timer) { 38 | uv_close((uv_handle_t*)timer, NULL); 39 | } 40 | 41 | TEST_IMPL(loop_backend_timeout) { 42 | uv_loop_t *loop = uv_default_loop(); 43 | uv_timer_t timer; 44 | int r; 45 | 46 | r = uv_timer_init(loop, &timer); 47 | ASSERT(r == 0); 48 | 49 | ASSERT(!uv_loop_alive(loop)); 50 | ASSERT(uv_backend_timeout(loop) == 0); 51 | 52 | r = uv_timer_start(&timer, cb, 1000, 0); /* 1 sec */ 53 | ASSERT(r == 0); 54 | ASSERT(uv_backend_timeout(loop) > 100); /* 0.1 sec */ 55 | ASSERT(uv_backend_timeout(loop) <= 1000); /* 1 sec */ 56 | 57 | r = uv_run(loop, UV_RUN_DEFAULT); 58 | ASSERT(r == 0); 59 | ASSERT(uv_backend_timeout(loop) == 0); 60 | 61 | MAKE_VALGRIND_HAPPY(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /src/unix/pthread-fixes.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013, Sony Mobile Communications AB 2 | * Copyright (c) 2012, Google Inc. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are 7 | met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following disclaimer 13 | in the documentation and/or other materials provided with the 14 | distribution. 15 | * Neither the name of Google Inc. nor the names of its 16 | contributors may be used to endorse or promote products derived from 17 | this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | /* Android versions < 4.1 have a broken pthread_sigmask. */ 33 | #include 34 | #include 35 | #include 36 | 37 | int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset) { 38 | static int workaround; 39 | int err; 40 | 41 | if (workaround) { 42 | return sigprocmask(how, set, oset); 43 | } else { 44 | err = pthread_sigmask(how, set, oset); 45 | if (err) { 46 | if (err == EINVAL && sigprocmask(how, set, oset) == 0) { 47 | workaround = 1; 48 | return 0; 49 | } else { 50 | return -1; 51 | } 52 | } 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /test/test-tmpdir.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | #define PATHMAX 1024 27 | #define SMALLPATH 1 28 | 29 | TEST_IMPL(tmpdir) { 30 | char tmpdir[PATHMAX]; 31 | size_t len; 32 | char last; 33 | int r; 34 | 35 | /* Test the normal case */ 36 | len = sizeof tmpdir; 37 | tmpdir[0] = '\0'; 38 | 39 | ASSERT(strlen(tmpdir) == 0); 40 | r = uv_os_tmpdir(tmpdir, &len); 41 | ASSERT(r == 0); 42 | ASSERT(strlen(tmpdir) == len); 43 | ASSERT(len > 0); 44 | ASSERT(tmpdir[len] == '\0'); 45 | 46 | if (len > 1) { 47 | last = tmpdir[len - 1]; 48 | #ifdef _WIN32 49 | ASSERT(last != '\\'); 50 | #else 51 | ASSERT(last != '/'); 52 | #endif 53 | } 54 | 55 | /* Test the case where the buffer is too small */ 56 | len = SMALLPATH; 57 | r = uv_os_tmpdir(tmpdir, &len); 58 | ASSERT(r == UV_ENOBUFS); 59 | ASSERT(len > SMALLPATH); 60 | 61 | /* Test invalid inputs */ 62 | r = uv_os_tmpdir(NULL, &len); 63 | ASSERT(r == UV_EINVAL); 64 | r = uv_os_tmpdir(tmpdir, NULL); 65 | ASSERT(r == UV_EINVAL); 66 | len = 0; 67 | r = uv_os_tmpdir(tmpdir, &len); 68 | ASSERT(r == UV_EINVAL); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /src/win/atomicops-inl.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_WIN_ATOMICOPS_INL_H_ 23 | #define UV_WIN_ATOMICOPS_INL_H_ 24 | 25 | #include "uv.h" 26 | #include "internal.h" 27 | 28 | 29 | /* Atomic set operation on char */ 30 | #ifdef _MSC_VER /* MSVC */ 31 | 32 | /* _InterlockedOr8 is supported by MSVC on x32 and x64. It is slightly less 33 | * efficient than InterlockedExchange, but InterlockedExchange8 does not exist, 34 | * and interlocked operations on larger targets might require the target to be 35 | * aligned. */ 36 | #pragma intrinsic(_InterlockedOr8) 37 | 38 | static char INLINE uv__atomic_exchange_set(char volatile* target) { 39 | return _InterlockedOr8(target, 1); 40 | } 41 | 42 | #else /* GCC */ 43 | 44 | /* Mingw-32 version, hopefully this works for 64-bit gcc as well. */ 45 | static inline char uv__atomic_exchange_set(char volatile* target) { 46 | const char one = 1; 47 | char old_value; 48 | __asm__ __volatile__ ("lock xchgb %0, %1\n\t" 49 | : "=r"(old_value), "=m"(*target) 50 | : "0"(one), "m"(*target) 51 | : "memory"); 52 | return old_value; 53 | } 54 | 55 | #endif 56 | 57 | #endif /* UV_WIN_ATOMICOPS_INL_H_ */ 58 | -------------------------------------------------------------------------------- /test/test-loop-alive.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static uv_timer_t timer_handle; 26 | 27 | static void timer_cb(uv_timer_t* handle) { 28 | ASSERT(handle); 29 | } 30 | 31 | 32 | static uv_work_t work_req; 33 | 34 | static void work_cb(uv_work_t* req) { 35 | ASSERT(req); 36 | } 37 | 38 | static void after_work_cb(uv_work_t* req, int status) { 39 | ASSERT(req); 40 | ASSERT(status == 0); 41 | } 42 | 43 | 44 | TEST_IMPL(loop_alive) { 45 | int r; 46 | ASSERT(!uv_loop_alive(uv_default_loop())); 47 | 48 | /* loops with handles are alive */ 49 | uv_timer_init(uv_default_loop(), &timer_handle); 50 | uv_timer_start(&timer_handle, timer_cb, 100, 0); 51 | ASSERT(uv_loop_alive(uv_default_loop())); 52 | 53 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); 54 | ASSERT(r == 0); 55 | ASSERT(!uv_loop_alive(uv_default_loop())); 56 | 57 | /* loops with requests are alive */ 58 | r = uv_queue_work(uv_default_loop(), &work_req, work_cb, after_work_cb); 59 | ASSERT(r == 0); 60 | ASSERT(uv_loop_alive(uv_default_loop())); 61 | 62 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); 63 | ASSERT(r == 0); 64 | ASSERT(!uv_loop_alive(uv_default_loop())); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /src/uv-data-getter-setters.c: -------------------------------------------------------------------------------- 1 | #include "uv.h" 2 | 3 | const char* uv_handle_type_name(uv_handle_type type) { 4 | switch (type) { 5 | #define XX(uc,lc) case UV_##uc: return #lc; 6 | UV_HANDLE_TYPE_MAP(XX) 7 | #undef XX 8 | case UV_FILE: return "file"; 9 | case UV_HANDLE_TYPE_MAX: 10 | case UV_UNKNOWN_HANDLE: return NULL; 11 | } 12 | return NULL; 13 | } 14 | 15 | uv_handle_type uv_handle_get_type(const uv_handle_t* handle) { 16 | return handle->type; 17 | } 18 | 19 | void* uv_handle_get_data(const uv_handle_t* handle) { 20 | return handle->data; 21 | } 22 | 23 | uv_loop_t* uv_handle_get_loop(const uv_handle_t* handle) { 24 | return handle->loop; 25 | } 26 | 27 | void uv_handle_set_data(uv_handle_t* handle, void* data) { 28 | handle->data = data; 29 | } 30 | 31 | const char* uv_req_type_name(uv_req_type type) { 32 | switch (type) { 33 | #define XX(uc,lc) case UV_##uc: return #lc; 34 | UV_REQ_TYPE_MAP(XX) 35 | #undef XX 36 | case UV_REQ_TYPE_MAX: 37 | case UV_UNKNOWN_REQ: 38 | default: /* UV_REQ_TYPE_PRIVATE */ 39 | return NULL; 40 | } 41 | return NULL; 42 | } 43 | 44 | uv_req_type uv_req_get_type(const uv_req_t* req) { 45 | return req->type; 46 | } 47 | 48 | void* uv_req_get_data(const uv_req_t* req) { 49 | return req->data; 50 | } 51 | 52 | void uv_req_set_data(uv_req_t* req, void* data) { 53 | req->data = data; 54 | } 55 | 56 | size_t uv_stream_get_write_queue_size(const uv_stream_t* stream) { 57 | return stream->write_queue_size; 58 | } 59 | 60 | size_t uv_udp_get_send_queue_size(const uv_udp_t* handle) { 61 | return handle->send_queue_size; 62 | } 63 | 64 | size_t uv_udp_get_send_queue_count(const uv_udp_t* handle) { 65 | return handle->send_queue_count; 66 | } 67 | 68 | uv_pid_t uv_process_get_pid(const uv_process_t* proc) { 69 | return proc->pid; 70 | } 71 | 72 | uv_fs_type uv_fs_get_type(const uv_fs_t* req) { 73 | return req->fs_type; 74 | } 75 | 76 | ssize_t uv_fs_get_result(const uv_fs_t* req) { 77 | return req->result; 78 | } 79 | 80 | void* uv_fs_get_ptr(const uv_fs_t* req) { 81 | return req->ptr; 82 | } 83 | 84 | const char* uv_fs_get_path(const uv_fs_t* req) { 85 | return req->path; 86 | } 87 | 88 | uv_stat_t* uv_fs_get_statbuf(uv_fs_t* req) { 89 | return &req->statbuf; 90 | } 91 | 92 | void* uv_loop_get_data(const uv_loop_t* loop) { 93 | return loop->data; 94 | } 95 | 96 | void uv_loop_set_data(uv_loop_t* loop, void* data) { 97 | loop->data = data; 98 | } 99 | -------------------------------------------------------------------------------- /docs/src/threadpool.rst: -------------------------------------------------------------------------------- 1 | 2 | .. _threadpool: 3 | 4 | Thread pool work scheduling 5 | =========================== 6 | 7 | libuv provides a threadpool which can be used to run user code and get notified 8 | in the loop thread. This thread pool is internally used to run all file system 9 | operations, as well as getaddrinfo and getnameinfo requests. 10 | 11 | Its default size is 4, but it can be changed at startup time by setting the 12 | ``UV_THREADPOOL_SIZE`` environment variable to any value (the absolute maximum 13 | is 128). 14 | 15 | The threadpool is global and shared across all event loops. When a particular 16 | function makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`) 17 | libuv preallocates and initializes the maximum number of threads allowed by 18 | ``UV_THREADPOOL_SIZE``. This causes a relatively minor memory overhead 19 | (~1MB for 128 threads) but increases the performance of threading at runtime. 20 | 21 | .. note:: 22 | Note that even though a global thread pool which is shared across all events 23 | loops is used, the functions are not thread safe. 24 | 25 | 26 | Data types 27 | ---------- 28 | 29 | .. c:type:: uv_work_t 30 | 31 | Work request type. 32 | 33 | .. c:type:: void (*uv_work_cb)(uv_work_t* req) 34 | 35 | Callback passed to :c:func:`uv_queue_work` which will be run on the thread 36 | pool. 37 | 38 | .. c:type:: void (*uv_after_work_cb)(uv_work_t* req, int status) 39 | 40 | Callback passed to :c:func:`uv_queue_work` which will be called on the loop 41 | thread after the work on the threadpool has been completed. If the work 42 | was cancelled using :c:func:`uv_cancel` `status` will be ``UV_ECANCELED``. 43 | 44 | 45 | Public members 46 | ^^^^^^^^^^^^^^ 47 | 48 | .. c:member:: uv_loop_t* uv_work_t.loop 49 | 50 | Loop that started this request and where completion will be reported. 51 | Readonly. 52 | 53 | .. seealso:: The :c:type:`uv_req_t` members also apply. 54 | 55 | 56 | API 57 | --- 58 | 59 | .. c:function:: int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb) 60 | 61 | Initializes a work request which will run the given `work_cb` in a thread 62 | from the threadpool. Once `work_cb` is completed, `after_work_cb` will be 63 | called on the loop thread. 64 | 65 | This request can be cancelled with :c:func:`uv_cancel`. 66 | 67 | .. seealso:: The :c:type:`uv_req_t` API functions also apply. 68 | -------------------------------------------------------------------------------- /test/test-poll-close.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include 23 | 24 | #ifndef _WIN32 25 | # include 26 | # include 27 | # include 28 | #endif 29 | 30 | #include "uv.h" 31 | #include "task.h" 32 | 33 | #define NUM_SOCKETS 64 34 | 35 | 36 | static int close_cb_called = 0; 37 | 38 | 39 | static void close_cb(uv_handle_t* handle) { 40 | close_cb_called++; 41 | } 42 | 43 | 44 | TEST_IMPL(poll_close) { 45 | uv_os_sock_t sockets[NUM_SOCKETS]; 46 | uv_poll_t poll_handles[NUM_SOCKETS]; 47 | int i; 48 | 49 | #ifdef _WIN32 50 | { 51 | struct WSAData wsa_data; 52 | int r = WSAStartup(MAKEWORD(2, 2), &wsa_data); 53 | ASSERT(r == 0); 54 | } 55 | #endif 56 | 57 | for (i = 0; i < NUM_SOCKETS; i++) { 58 | sockets[i] = socket(AF_INET, SOCK_STREAM, 0); 59 | uv_poll_init_socket(uv_default_loop(), &poll_handles[i], sockets[i]); 60 | uv_poll_start(&poll_handles[i], UV_READABLE | UV_WRITABLE, NULL); 61 | } 62 | 63 | for (i = 0; i < NUM_SOCKETS; i++) { 64 | uv_close((uv_handle_t*) &poll_handles[i], close_cb); 65 | } 66 | 67 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 68 | 69 | ASSERT(close_cb_called == NUM_SOCKETS); 70 | 71 | MAKE_VALGRIND_HAPPY(); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /samples/socks5-proxy/util.c: -------------------------------------------------------------------------------- 1 | /* Copyright StrongLoop, Inc. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "defs.h" 23 | #include 24 | #include 25 | #include 26 | 27 | static void pr_do(FILE *stream, 28 | const char *label, 29 | const char *fmt, 30 | va_list ap); 31 | 32 | void *xmalloc(size_t size) { 33 | void *ptr; 34 | 35 | ptr = malloc(size); 36 | if (ptr == NULL) { 37 | pr_err("out of memory, need %lu bytes", (unsigned long) size); 38 | exit(1); 39 | } 40 | 41 | return ptr; 42 | } 43 | 44 | void pr_info(const char *fmt, ...) { 45 | va_list ap; 46 | va_start(ap, fmt); 47 | pr_do(stdout, "info", fmt, ap); 48 | va_end(ap); 49 | } 50 | 51 | void pr_warn(const char *fmt, ...) { 52 | va_list ap; 53 | va_start(ap, fmt); 54 | pr_do(stderr, "warn", fmt, ap); 55 | va_end(ap); 56 | } 57 | 58 | void pr_err(const char *fmt, ...) { 59 | va_list ap; 60 | va_start(ap, fmt); 61 | pr_do(stderr, "error", fmt, ap); 62 | va_end(ap); 63 | } 64 | 65 | static void pr_do(FILE *stream, 66 | const char *label, 67 | const char *fmt, 68 | va_list ap) { 69 | char fmtbuf[1024]; 70 | vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, ap); 71 | fprintf(stream, "%s:%s: %s\n", _getprogname(), label, fmtbuf); 72 | } 73 | -------------------------------------------------------------------------------- /test/test-connect-unspecified.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static void connect_4(uv_connect_t* req, int status) { 26 | ASSERT(status != UV_EADDRNOTAVAIL); 27 | } 28 | 29 | static void connect_6(uv_connect_t* req, int status) { 30 | ASSERT(status != UV_EADDRNOTAVAIL); 31 | } 32 | 33 | TEST_IMPL(connect_unspecified) { 34 | uv_loop_t* loop; 35 | uv_tcp_t socket4; 36 | struct sockaddr_in addr4; 37 | uv_connect_t connect4; 38 | uv_tcp_t socket6; 39 | struct sockaddr_in6 addr6; 40 | uv_connect_t connect6; 41 | 42 | loop = uv_default_loop(); 43 | 44 | ASSERT(uv_tcp_init(loop, &socket4) == 0); 45 | ASSERT(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr4) == 0); 46 | ASSERT(uv_tcp_connect(&connect4, 47 | &socket4, 48 | (const struct sockaddr*) &addr4, 49 | connect_4) == 0); 50 | 51 | if (can_ipv6()) { 52 | ASSERT(uv_tcp_init(loop, &socket6) == 0); 53 | ASSERT(uv_ip6_addr("::", TEST_PORT, &addr6) == 0); 54 | ASSERT(uv_tcp_connect(&connect6, 55 | &socket6, 56 | (const struct sockaddr*) &addr6, 57 | connect_6) == 0); 58 | } 59 | 60 | ASSERT(uv_run(loop, UV_RUN_DEFAULT) == 0); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /test/test-homedir.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv project contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | #define PATHMAX 1024 27 | #define SMALLPATH 1 28 | 29 | TEST_IMPL(homedir) { 30 | char homedir[PATHMAX]; 31 | size_t len; 32 | int r; 33 | 34 | /* Test the normal case */ 35 | len = sizeof homedir; 36 | homedir[0] = '\0'; 37 | ASSERT(strlen(homedir) == 0); 38 | r = uv_os_homedir(homedir, &len); 39 | ASSERT(r == 0); 40 | ASSERT(strlen(homedir) == len); 41 | ASSERT(len > 0); 42 | ASSERT(homedir[len] == '\0'); 43 | 44 | #ifdef _WIN32 45 | if (len == 3 && homedir[1] == ':') 46 | ASSERT(homedir[2] == '\\'); 47 | else 48 | ASSERT(homedir[len - 1] != '\\'); 49 | #else 50 | if (len == 1) 51 | ASSERT(homedir[0] == '/'); 52 | else 53 | ASSERT(homedir[len - 1] != '/'); 54 | #endif 55 | 56 | /* Test the case where the buffer is too small */ 57 | len = SMALLPATH; 58 | r = uv_os_homedir(homedir, &len); 59 | ASSERT(r == UV_ENOBUFS); 60 | ASSERT(len > SMALLPATH); 61 | 62 | /* Test invalid inputs */ 63 | r = uv_os_homedir(NULL, &len); 64 | ASSERT(r == UV_EINVAL); 65 | r = uv_os_homedir(homedir, NULL); 66 | ASSERT(r == UV_EINVAL); 67 | len = 0; 68 | r = uv_os_homedir(homedir, &len); 69 | ASSERT(r == UV_EINVAL); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /docs/code/uvtee/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | typedef struct { 10 | uv_write_t req; 11 | uv_buf_t buf; 12 | } write_req_t; 13 | 14 | uv_loop_t *loop; 15 | uv_pipe_t stdin_pipe; 16 | uv_pipe_t stdout_pipe; 17 | uv_pipe_t file_pipe; 18 | 19 | void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 20 | *buf = uv_buf_init((char*) malloc(suggested_size), suggested_size); 21 | } 22 | 23 | void free_write_req(uv_write_t *req) { 24 | write_req_t *wr = (write_req_t*) req; 25 | free(wr->buf.base); 26 | free(wr); 27 | } 28 | 29 | void on_stdout_write(uv_write_t *req, int status) { 30 | free_write_req(req); 31 | } 32 | 33 | void on_file_write(uv_write_t *req, int status) { 34 | free_write_req(req); 35 | } 36 | 37 | void write_data(uv_stream_t *dest, size_t size, uv_buf_t buf, uv_write_cb cb) { 38 | write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t)); 39 | req->buf = uv_buf_init((char*) malloc(size), size); 40 | memcpy(req->buf.base, buf.base, size); 41 | uv_write((uv_write_t*) req, (uv_stream_t*)dest, &req->buf, 1, cb); 42 | } 43 | 44 | void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { 45 | if (nread < 0){ 46 | if (nread == UV_EOF){ 47 | // end of file 48 | uv_close((uv_handle_t *)&stdin_pipe, NULL); 49 | uv_close((uv_handle_t *)&stdout_pipe, NULL); 50 | uv_close((uv_handle_t *)&file_pipe, NULL); 51 | } 52 | } else if (nread > 0) { 53 | write_data((uv_stream_t *)&stdout_pipe, nread, *buf, on_stdout_write); 54 | write_data((uv_stream_t *)&file_pipe, nread, *buf, on_file_write); 55 | } 56 | 57 | // OK to free buffer as write_data copies it. 58 | if (buf->base) 59 | free(buf->base); 60 | } 61 | 62 | int main(int argc, char **argv) { 63 | loop = uv_default_loop(); 64 | 65 | uv_pipe_init(loop, &stdin_pipe, 0); 66 | uv_pipe_open(&stdin_pipe, 0); 67 | 68 | uv_pipe_init(loop, &stdout_pipe, 0); 69 | uv_pipe_open(&stdout_pipe, 1); 70 | 71 | uv_fs_t file_req; 72 | int fd = uv_fs_open(loop, &file_req, argv[1], O_CREAT | O_RDWR, 0644, NULL); 73 | uv_pipe_init(loop, &file_pipe, 0); 74 | uv_pipe_open(&file_pipe, fd); 75 | 76 | uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, read_stdin); 77 | 78 | uv_run(loop, UV_RUN_DEFAULT); 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /docs/code/dns/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | uv_loop_t *loop; 7 | 8 | void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 9 | buf->base = malloc(suggested_size); 10 | buf->len = suggested_size; 11 | } 12 | 13 | void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { 14 | if (nread < 0) { 15 | if (nread != UV_EOF) 16 | fprintf(stderr, "Read error %s\n", uv_err_name(nread)); 17 | uv_close((uv_handle_t*) client, NULL); 18 | free(buf->base); 19 | free(client); 20 | return; 21 | } 22 | 23 | char *data = (char*) malloc(sizeof(char) * (nread+1)); 24 | data[nread] = '\0'; 25 | strncpy(data, buf->base, nread); 26 | 27 | fprintf(stderr, "%s", data); 28 | free(data); 29 | free(buf->base); 30 | } 31 | 32 | void on_connect(uv_connect_t *req, int status) { 33 | if (status < 0) { 34 | fprintf(stderr, "connect failed error %s\n", uv_err_name(status)); 35 | free(req); 36 | return; 37 | } 38 | 39 | uv_read_start((uv_stream_t*) req->handle, alloc_buffer, on_read); 40 | free(req); 41 | } 42 | 43 | void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res) { 44 | if (status < 0) { 45 | fprintf(stderr, "getaddrinfo callback error %s\n", uv_err_name(status)); 46 | return; 47 | } 48 | 49 | char addr[17] = {'\0'}; 50 | uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16); 51 | fprintf(stderr, "%s\n", addr); 52 | 53 | uv_connect_t *connect_req = (uv_connect_t*) malloc(sizeof(uv_connect_t)); 54 | uv_tcp_t *socket = (uv_tcp_t*) malloc(sizeof(uv_tcp_t)); 55 | uv_tcp_init(loop, socket); 56 | 57 | uv_tcp_connect(connect_req, socket, (const struct sockaddr*) res->ai_addr, on_connect); 58 | 59 | uv_freeaddrinfo(res); 60 | } 61 | 62 | int main() { 63 | loop = uv_default_loop(); 64 | 65 | struct addrinfo hints; 66 | hints.ai_family = PF_INET; 67 | hints.ai_socktype = SOCK_STREAM; 68 | hints.ai_protocol = IPPROTO_TCP; 69 | hints.ai_flags = 0; 70 | 71 | uv_getaddrinfo_t resolver; 72 | fprintf(stderr, "irc.freenode.net is... "); 73 | int r = uv_getaddrinfo(loop, &resolver, on_resolved, "irc.freenode.net", "6667", &hints); 74 | 75 | if (r) { 76 | fprintf(stderr, "getaddrinfo call error %s\n", uv_err_name(r)); 77 | return 1; 78 | } 79 | return uv_run(loop, UV_RUN_DEFAULT); 80 | } 81 | -------------------------------------------------------------------------------- /test/test-tcp-connect6-error.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | #include 26 | 27 | 28 | static int connect_cb_called = 0; 29 | static int close_cb_called = 0; 30 | 31 | 32 | static void connect_cb(uv_connect_t* handle, int status) { 33 | ASSERT(handle != NULL); 34 | connect_cb_called++; 35 | } 36 | 37 | 38 | static void close_cb(uv_handle_t* handle) { 39 | ASSERT(handle != NULL); 40 | close_cb_called++; 41 | } 42 | 43 | 44 | TEST_IMPL(tcp_connect6_error_fault) { 45 | const char garbage[] = 46 | "blah blah blah blah blah blah blah blah blah blah blah blah"; 47 | const struct sockaddr_in6* garbage_addr; 48 | uv_tcp_t server; 49 | int r; 50 | uv_connect_t req; 51 | 52 | garbage_addr = (const struct sockaddr_in6*) &garbage; 53 | 54 | r = uv_tcp_init(uv_default_loop(), &server); 55 | ASSERT(r == 0); 56 | r = uv_tcp_connect(&req, 57 | &server, 58 | (const struct sockaddr*) garbage_addr, 59 | connect_cb); 60 | ASSERT(r == UV_EINVAL); 61 | 62 | uv_close((uv_handle_t*)&server, close_cb); 63 | 64 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 65 | 66 | ASSERT(connect_cb_called == 0); 67 | ASSERT(close_cb_called == 1); 68 | 69 | MAKE_VALGRIND_HAPPY(); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /test/test-tcp-connect-error.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | #include 26 | 27 | 28 | static int connect_cb_called = 0; 29 | static int close_cb_called = 0; 30 | 31 | 32 | 33 | static void connect_cb(uv_connect_t* handle, int status) { 34 | ASSERT(handle != NULL); 35 | connect_cb_called++; 36 | } 37 | 38 | 39 | 40 | static void close_cb(uv_handle_t* handle) { 41 | ASSERT(handle != NULL); 42 | close_cb_called++; 43 | } 44 | 45 | 46 | TEST_IMPL(tcp_connect_error_fault) { 47 | const char garbage[] = 48 | "blah blah blah blah blah blah blah blah blah blah blah blah"; 49 | const struct sockaddr_in* garbage_addr; 50 | uv_tcp_t server; 51 | int r; 52 | uv_connect_t req; 53 | 54 | garbage_addr = (const struct sockaddr_in*) &garbage; 55 | 56 | r = uv_tcp_init(uv_default_loop(), &server); 57 | ASSERT(r == 0); 58 | r = uv_tcp_connect(&req, 59 | &server, 60 | (const struct sockaddr*) garbage_addr, 61 | connect_cb); 62 | ASSERT(r == UV_EINVAL); 63 | 64 | uv_close((uv_handle_t*)&server, close_cb); 65 | 66 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 67 | 68 | ASSERT(connect_cb_called == 0); 69 | ASSERT(close_cb_called == 1); 70 | 71 | MAKE_VALGRIND_HAPPY(); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /docs/code/cgi/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | uv_loop_t *loop; 8 | uv_process_t child_req; 9 | uv_process_options_t options; 10 | 11 | void cleanup_handles(uv_process_t *req, int64_t exit_status, int term_signal) { 12 | fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal); 13 | uv_close((uv_handle_t*) req->data, NULL); 14 | uv_close((uv_handle_t*) req, NULL); 15 | } 16 | 17 | void invoke_cgi_script(uv_tcp_t *client) { 18 | size_t size = 500; 19 | char path[size]; 20 | uv_exepath(path, &size); 21 | strcpy(path + (strlen(path) - strlen("cgi")), "tick"); 22 | 23 | char* args[2]; 24 | args[0] = path; 25 | args[1] = NULL; 26 | 27 | /* ... finding the executable path and setting up arguments ... */ 28 | 29 | options.stdio_count = 3; 30 | uv_stdio_container_t child_stdio[3]; 31 | child_stdio[0].flags = UV_IGNORE; 32 | child_stdio[1].flags = UV_INHERIT_STREAM; 33 | child_stdio[1].data.stream = (uv_stream_t*) client; 34 | child_stdio[2].flags = UV_IGNORE; 35 | options.stdio = child_stdio; 36 | 37 | options.exit_cb = cleanup_handles; 38 | options.file = args[0]; 39 | options.args = args; 40 | 41 | // Set this so we can close the socket after the child process exits. 42 | child_req.data = (void*) client; 43 | int r; 44 | if ((r = uv_spawn(loop, &child_req, &options))) { 45 | fprintf(stderr, "%s\n", uv_strerror(r)); 46 | return; 47 | } 48 | } 49 | 50 | void on_new_connection(uv_stream_t *server, int status) { 51 | if (status == -1) { 52 | // error! 53 | return; 54 | } 55 | 56 | uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t)); 57 | uv_tcp_init(loop, client); 58 | if (uv_accept(server, (uv_stream_t*) client) == 0) { 59 | invoke_cgi_script(client); 60 | } 61 | else { 62 | uv_close((uv_handle_t*) client, NULL); 63 | } 64 | } 65 | 66 | int main() { 67 | loop = uv_default_loop(); 68 | 69 | uv_tcp_t server; 70 | uv_tcp_init(loop, &server); 71 | 72 | struct sockaddr_in bind_addr; 73 | uv_ip4_addr("0.0.0.0", 7000, &bind_addr); 74 | uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0); 75 | int r = uv_listen((uv_stream_t*) &server, 128, on_new_connection); 76 | if (r) { 77 | fprintf(stderr, "Listen error %s\n", uv_err_name(r)); 78 | return 1; 79 | } 80 | return uv_run(loop, UV_RUN_DEFAULT); 81 | } 82 | -------------------------------------------------------------------------------- /test/test-threadpool.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static int work_cb_count; 26 | static int after_work_cb_count; 27 | static uv_work_t work_req; 28 | static char data; 29 | 30 | 31 | static void work_cb(uv_work_t* req) { 32 | ASSERT(req == &work_req); 33 | ASSERT(req->data == &data); 34 | work_cb_count++; 35 | } 36 | 37 | 38 | static void after_work_cb(uv_work_t* req, int status) { 39 | ASSERT(status == 0); 40 | ASSERT(req == &work_req); 41 | ASSERT(req->data == &data); 42 | after_work_cb_count++; 43 | } 44 | 45 | 46 | TEST_IMPL(threadpool_queue_work_simple) { 47 | int r; 48 | 49 | work_req.data = &data; 50 | r = uv_queue_work(uv_default_loop(), &work_req, work_cb, after_work_cb); 51 | ASSERT(r == 0); 52 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 53 | 54 | ASSERT(work_cb_count == 1); 55 | ASSERT(after_work_cb_count == 1); 56 | 57 | MAKE_VALGRIND_HAPPY(); 58 | return 0; 59 | } 60 | 61 | 62 | TEST_IMPL(threadpool_queue_work_einval) { 63 | int r; 64 | 65 | work_req.data = &data; 66 | r = uv_queue_work(uv_default_loop(), &work_req, NULL, after_work_cb); 67 | ASSERT(r == UV_EINVAL); 68 | 69 | uv_run(uv_default_loop(), UV_RUN_DEFAULT); 70 | 71 | ASSERT(work_cb_count == 0); 72 | ASSERT(after_work_cb_count == 0); 73 | 74 | MAKE_VALGRIND_HAPPY(); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /docs/code/tcp-echo-server/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define DEFAULT_PORT 7000 7 | #define DEFAULT_BACKLOG 128 8 | 9 | uv_loop_t *loop; 10 | struct sockaddr_in addr; 11 | 12 | typedef struct { 13 | uv_write_t req; 14 | uv_buf_t buf; 15 | } write_req_t; 16 | 17 | void free_write_req(uv_write_t *req) { 18 | write_req_t *wr = (write_req_t*) req; 19 | free(wr->buf.base); 20 | free(wr); 21 | } 22 | 23 | void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 24 | buf->base = (char*) malloc(suggested_size); 25 | buf->len = suggested_size; 26 | } 27 | 28 | void on_close(uv_handle_t* handle) { 29 | free(handle); 30 | } 31 | 32 | void echo_write(uv_write_t *req, int status) { 33 | if (status) { 34 | fprintf(stderr, "Write error %s\n", uv_strerror(status)); 35 | } 36 | free_write_req(req); 37 | } 38 | 39 | void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { 40 | if (nread > 0) { 41 | write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t)); 42 | req->buf = uv_buf_init(buf->base, nread); 43 | uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write); 44 | return; 45 | } 46 | if (nread < 0) { 47 | if (nread != UV_EOF) 48 | fprintf(stderr, "Read error %s\n", uv_err_name(nread)); 49 | uv_close((uv_handle_t*) client, on_close); 50 | } 51 | 52 | free(buf->base); 53 | } 54 | 55 | void on_new_connection(uv_stream_t *server, int status) { 56 | if (status < 0) { 57 | fprintf(stderr, "New connection error %s\n", uv_strerror(status)); 58 | // error! 59 | return; 60 | } 61 | 62 | uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t)); 63 | uv_tcp_init(loop, client); 64 | if (uv_accept(server, (uv_stream_t*) client) == 0) { 65 | uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read); 66 | } 67 | else { 68 | uv_close((uv_handle_t*) client, on_close); 69 | } 70 | } 71 | 72 | int main() { 73 | loop = uv_default_loop(); 74 | 75 | uv_tcp_t server; 76 | uv_tcp_init(loop, &server); 77 | 78 | uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr); 79 | 80 | uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0); 81 | int r = uv_listen((uv_stream_t*) &server, DEFAULT_BACKLOG, on_new_connection); 82 | if (r) { 83 | fprintf(stderr, "Listen error %s\n", uv_strerror(r)); 84 | return 1; 85 | } 86 | return uv_run(loop, UV_RUN_DEFAULT); 87 | } 88 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | A. Hauptmann 2 | Aaron Bieber 3 | Alan Gutierrez 4 | Andrius Bentkus 5 | Bert Belder 6 | Bert Belder 7 | Bert Belder 8 | Brandon Philips 9 | Brian White 10 | Brian White 11 | Caleb James DeLisle 12 | Christoph Iserlohn 13 | Devchandra Meetei Leishangthem 14 | Fedor Indutny 15 | Frank Denis 16 | Imran Iqbal 17 | Isaac Z. Schlueter 18 | Jason Williams 19 | Jesse Gorzinski 20 | Justin Venus 21 | Keno Fischer 22 | Keno Fischer 23 | Leith Bade 24 | Leonard Hecker 25 | Maciej Małecki 26 | Marc Schlaich 27 | Michael 28 | Michael Neumann 29 | Nicholas Vavilov 30 | Nick Logan 31 | Rasmus Christian Pedersen 32 | Rasmus Christian Pedersen 33 | Robert Mustacchi 34 | Ryan Dahl 35 | Ryan Emery 36 | Sakthipriyan Vairamani 37 | Sam Roberts 38 | San-Tai Hsu 39 | Santiago Gimeno 40 | Saúl Ibarra Corretgé 41 | Shigeki Ohtsu 42 | Timothy J. Fontaine 43 | Yasuhiro Matsumoto 44 | Yazhong Liu 45 | Yuki Okumura 46 | jBarz 47 | jBarz 48 | -------------------------------------------------------------------------------- /src/unix/dl.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | static int uv__dlerror(uv_lib_t* lib); 31 | 32 | 33 | int uv_dlopen(const char* filename, uv_lib_t* lib) { 34 | dlerror(); /* Reset error status. */ 35 | lib->errmsg = NULL; 36 | lib->handle = dlopen(filename, RTLD_LAZY); 37 | return lib->handle ? 0 : uv__dlerror(lib); 38 | } 39 | 40 | 41 | void uv_dlclose(uv_lib_t* lib) { 42 | uv__free(lib->errmsg); 43 | lib->errmsg = NULL; 44 | 45 | if (lib->handle) { 46 | /* Ignore errors. No good way to signal them without leaking memory. */ 47 | dlclose(lib->handle); 48 | lib->handle = NULL; 49 | } 50 | } 51 | 52 | 53 | int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) { 54 | dlerror(); /* Reset error status. */ 55 | *ptr = dlsym(lib->handle, name); 56 | return uv__dlerror(lib); 57 | } 58 | 59 | 60 | const char* uv_dlerror(const uv_lib_t* lib) { 61 | return lib->errmsg ? lib->errmsg : "no error"; 62 | } 63 | 64 | 65 | static int uv__dlerror(uv_lib_t* lib) { 66 | const char* errmsg; 67 | 68 | uv__free(lib->errmsg); 69 | 70 | errmsg = dlerror(); 71 | 72 | if (errmsg) { 73 | lib->errmsg = uv__strdup(errmsg); 74 | return -1; 75 | } 76 | else { 77 | lib->errmsg = NULL; 78 | return 0; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /test/test-async-null-cb.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | static uv_async_t async_handle; 27 | static uv_check_t check_handle; 28 | static int check_cb_called; 29 | static uv_thread_t thread; 30 | 31 | 32 | static void thread_cb(void* dummy) { 33 | (void) &dummy; 34 | uv_async_send(&async_handle); 35 | } 36 | 37 | 38 | static void check_cb(uv_check_t* handle) { 39 | ASSERT(check_cb_called == 0); 40 | uv_close((uv_handle_t*) &async_handle, NULL); 41 | uv_close((uv_handle_t*) &check_handle, NULL); 42 | check_cb_called++; 43 | } 44 | 45 | 46 | TEST_IMPL(async_null_cb) { 47 | /* 48 | * Fill async_handle with garbage values. 49 | * uv_async_init() should properly initialize struct fields regardless of 50 | * initial values. 51 | * This is added to verify paddings between fields do not affect behavior. 52 | */ 53 | memset(&async_handle, 0xff, sizeof(async_handle)); 54 | 55 | ASSERT(0 == uv_async_init(uv_default_loop(), &async_handle, NULL)); 56 | ASSERT(0 == uv_check_init(uv_default_loop(), &check_handle)); 57 | ASSERT(0 == uv_check_start(&check_handle, check_cb)); 58 | ASSERT(0 == uv_thread_create(&thread, thread_cb, NULL)); 59 | ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); 60 | ASSERT(0 == uv_thread_join(&thread)); 61 | ASSERT(1 == check_cb_called); 62 | MAKE_VALGRIND_HAPPY(); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /test/test-loop-stop.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static uv_prepare_t prepare_handle; 26 | static uv_timer_t timer_handle; 27 | static int prepare_called = 0; 28 | static int timer_called = 0; 29 | static int num_ticks = 10; 30 | 31 | 32 | static void prepare_cb(uv_prepare_t* handle) { 33 | ASSERT(handle == &prepare_handle); 34 | prepare_called++; 35 | if (prepare_called == num_ticks) 36 | uv_prepare_stop(handle); 37 | } 38 | 39 | 40 | static void timer_cb(uv_timer_t* handle) { 41 | ASSERT(handle == &timer_handle); 42 | timer_called++; 43 | if (timer_called == 1) 44 | uv_stop(uv_default_loop()); 45 | else if (timer_called == num_ticks) 46 | uv_timer_stop(handle); 47 | } 48 | 49 | 50 | TEST_IMPL(loop_stop) { 51 | int r; 52 | uv_prepare_init(uv_default_loop(), &prepare_handle); 53 | uv_prepare_start(&prepare_handle, prepare_cb); 54 | uv_timer_init(uv_default_loop(), &timer_handle); 55 | uv_timer_start(&timer_handle, timer_cb, 100, 100); 56 | 57 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); 58 | ASSERT(r != 0); 59 | ASSERT(timer_called == 1); 60 | 61 | r = uv_run(uv_default_loop(), UV_RUN_NOWAIT); 62 | ASSERT(r != 0); 63 | ASSERT(prepare_called > 1); 64 | 65 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); 66 | ASSERT(r == 0); 67 | ASSERT(timer_called == 10); 68 | ASSERT(prepare_called == 10); 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /test/test-callback-order.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static int idle_cb_called; 26 | static int timer_cb_called; 27 | 28 | static uv_idle_t idle_handle; 29 | static uv_timer_t timer_handle; 30 | 31 | 32 | /* idle_cb should run before timer_cb */ 33 | static void idle_cb(uv_idle_t* handle) { 34 | ASSERT(idle_cb_called == 0); 35 | ASSERT(timer_cb_called == 0); 36 | uv_idle_stop(handle); 37 | idle_cb_called++; 38 | } 39 | 40 | 41 | static void timer_cb(uv_timer_t* handle) { 42 | ASSERT(idle_cb_called == 1); 43 | ASSERT(timer_cb_called == 0); 44 | uv_timer_stop(handle); 45 | timer_cb_called++; 46 | } 47 | 48 | 49 | static void next_tick(uv_idle_t* handle) { 50 | uv_loop_t* loop = handle->loop; 51 | uv_idle_stop(handle); 52 | uv_idle_init(loop, &idle_handle); 53 | uv_idle_start(&idle_handle, idle_cb); 54 | uv_timer_init(loop, &timer_handle); 55 | uv_timer_start(&timer_handle, timer_cb, 0, 0); 56 | } 57 | 58 | 59 | TEST_IMPL(callback_order) { 60 | uv_loop_t* loop; 61 | uv_idle_t idle; 62 | 63 | loop = uv_default_loop(); 64 | uv_idle_init(loop, &idle); 65 | uv_idle_start(&idle, next_tick); 66 | 67 | ASSERT(idle_cb_called == 0); 68 | ASSERT(timer_cb_called == 0); 69 | 70 | uv_run(loop, UV_RUN_DEFAULT); 71 | 72 | ASSERT(idle_cb_called == 1); 73 | ASSERT(timer_cb_called == 1); 74 | 75 | MAKE_VALGRIND_HAPPY(); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /test/test-process-title.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | 27 | static void set_title(const char* title) { 28 | char buffer[512]; 29 | int err; 30 | 31 | err = uv_get_process_title(buffer, sizeof(buffer)); 32 | ASSERT(err == 0); 33 | 34 | err = uv_set_process_title(title); 35 | ASSERT(err == 0); 36 | 37 | err = uv_get_process_title(buffer, sizeof(buffer)); 38 | ASSERT(err == 0); 39 | 40 | ASSERT(strcmp(buffer, title) == 0); 41 | } 42 | 43 | 44 | static void uv_get_process_title_edge_cases(void) { 45 | char buffer[512]; 46 | int r; 47 | 48 | /* Test a NULL buffer */ 49 | r = uv_get_process_title(NULL, 100); 50 | ASSERT(r == UV_EINVAL); 51 | 52 | /* Test size of zero */ 53 | r = uv_get_process_title(buffer, 0); 54 | ASSERT(r == UV_EINVAL); 55 | 56 | /* Test for insufficient buffer size */ 57 | r = uv_get_process_title(buffer, 1); 58 | ASSERT(r == UV_ENOBUFS); 59 | } 60 | 61 | 62 | TEST_IMPL(process_title) { 63 | #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) 64 | RETURN_SKIP("uv_(get|set)_process_title is not implemented."); 65 | #endif 66 | 67 | /* Check for format string vulnerabilities. */ 68 | set_title("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s"); 69 | set_title("new title"); 70 | 71 | /* Check uv_get_process_title() edge cases */ 72 | uv_get_process_title_edge_cases(); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /test/test-walk-handles.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | #include 26 | #include 27 | 28 | static char magic_cookie[] = "magic cookie"; 29 | static int seen_timer_handle; 30 | static uv_timer_t timer; 31 | 32 | 33 | static void walk_cb(uv_handle_t* handle, void* arg) { 34 | ASSERT(arg == (void*)magic_cookie); 35 | 36 | if (handle == (uv_handle_t*)&timer) { 37 | seen_timer_handle++; 38 | } else { 39 | ASSERT(0 && "unexpected handle"); 40 | } 41 | } 42 | 43 | 44 | static void timer_cb(uv_timer_t* handle) { 45 | ASSERT(handle == &timer); 46 | 47 | uv_walk(handle->loop, walk_cb, magic_cookie); 48 | uv_close((uv_handle_t*)handle, NULL); 49 | } 50 | 51 | 52 | TEST_IMPL(walk_handles) { 53 | uv_loop_t* loop; 54 | int r; 55 | 56 | loop = uv_default_loop(); 57 | 58 | r = uv_timer_init(loop, &timer); 59 | ASSERT(r == 0); 60 | 61 | r = uv_timer_start(&timer, timer_cb, 1, 0); 62 | ASSERT(r == 0); 63 | 64 | /* Start event loop, expect to see the timer handle in walk_cb. */ 65 | ASSERT(seen_timer_handle == 0); 66 | r = uv_run(loop, UV_RUN_DEFAULT); 67 | ASSERT(r == 0); 68 | ASSERT(seen_timer_handle == 1); 69 | 70 | /* Loop is finished, walk_cb should not see our timer handle. */ 71 | seen_timer_handle = 0; 72 | uv_walk(loop, walk_cb, magic_cookie); 73 | ASSERT(seen_timer_handle == 0); 74 | 75 | MAKE_VALGRIND_HAPPY(); 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /test/test-tcp-write-after-connect.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef _WIN32 23 | 24 | #include "uv.h" 25 | #include "task.h" 26 | 27 | uv_loop_t loop; 28 | uv_tcp_t tcp_client; 29 | uv_connect_t connection_request; 30 | uv_write_t write_request; 31 | uv_buf_t buf = { "HELLO", 4 }; 32 | 33 | 34 | static void write_cb(uv_write_t *req, int status) { 35 | ASSERT(status == UV_ECANCELED); 36 | uv_close((uv_handle_t*) req->handle, NULL); 37 | } 38 | 39 | 40 | static void connect_cb(uv_connect_t *req, int status) { 41 | ASSERT(status == UV_ECONNREFUSED); 42 | } 43 | 44 | 45 | TEST_IMPL(tcp_write_after_connect) { 46 | struct sockaddr_in sa; 47 | ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &sa)); 48 | ASSERT(0 == uv_loop_init(&loop)); 49 | ASSERT(0 == uv_tcp_init(&loop, &tcp_client)); 50 | 51 | ASSERT(0 == uv_tcp_connect(&connection_request, 52 | &tcp_client, 53 | (const struct sockaddr *) 54 | &sa, 55 | connect_cb)); 56 | 57 | ASSERT(0 == uv_write(&write_request, 58 | (uv_stream_t *)&tcp_client, 59 | &buf, 1, 60 | write_cb)); 61 | 62 | uv_run(&loop, UV_RUN_DEFAULT); 63 | 64 | MAKE_VALGRIND_HAPPY(); 65 | return 0; 66 | } 67 | 68 | #else 69 | 70 | typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */ 71 | 72 | #endif /* !_WIN32 */ 73 | -------------------------------------------------------------------------------- /docs/code/pipe-echo-server/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #ifdef _WIN32 7 | #define PIPENAME "\\\\?\\pipe\\echo.sock" 8 | #else 9 | #define PIPENAME "/tmp/echo.sock" 10 | #endif 11 | 12 | uv_loop_t *loop; 13 | 14 | typedef struct { 15 | uv_write_t req; 16 | uv_buf_t buf; 17 | } write_req_t; 18 | 19 | void free_write_req(uv_write_t *req) { 20 | write_req_t *wr = (write_req_t*) req; 21 | free(wr->buf.base); 22 | free(wr); 23 | } 24 | 25 | void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 26 | buf->base = malloc(suggested_size); 27 | buf->len = suggested_size; 28 | } 29 | 30 | void echo_write(uv_write_t *req, int status) { 31 | if (status < 0) { 32 | fprintf(stderr, "Write error %s\n", uv_err_name(status)); 33 | } 34 | free_write_req(req); 35 | } 36 | 37 | void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) { 38 | if (nread > 0) { 39 | write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t)); 40 | req->buf = uv_buf_init(buf->base, nread); 41 | uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write); 42 | return; 43 | } 44 | 45 | if (nread < 0) { 46 | if (nread != UV_EOF) 47 | fprintf(stderr, "Read error %s\n", uv_err_name(nread)); 48 | uv_close((uv_handle_t*) client, NULL); 49 | } 50 | 51 | free(buf->base); 52 | } 53 | 54 | void on_new_connection(uv_stream_t *server, int status) { 55 | if (status == -1) { 56 | // error! 57 | return; 58 | } 59 | 60 | uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t)); 61 | uv_pipe_init(loop, client, 0); 62 | if (uv_accept(server, (uv_stream_t*) client) == 0) { 63 | uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read); 64 | } 65 | else { 66 | uv_close((uv_handle_t*) client, NULL); 67 | } 68 | } 69 | 70 | void remove_sock(int sig) { 71 | uv_fs_t req; 72 | uv_fs_unlink(loop, &req, PIPENAME, NULL); 73 | exit(0); 74 | } 75 | 76 | int main() { 77 | loop = uv_default_loop(); 78 | 79 | uv_pipe_t server; 80 | uv_pipe_init(loop, &server, 0); 81 | 82 | signal(SIGINT, remove_sock); 83 | 84 | int r; 85 | if ((r = uv_pipe_bind(&server, PIPENAME))) { 86 | fprintf(stderr, "Bind error %s\n", uv_err_name(r)); 87 | return 1; 88 | } 89 | if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) { 90 | fprintf(stderr, "Listen error %s\n", uv_err_name(r)); 91 | return 2; 92 | } 93 | return uv_run(loop, UV_RUN_DEFAULT); 94 | } 95 | -------------------------------------------------------------------------------- /test/test-loop-close.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | 25 | static uv_timer_t timer_handle; 26 | 27 | static void timer_cb(uv_timer_t* handle) { 28 | ASSERT(handle); 29 | uv_stop(handle->loop); 30 | } 31 | 32 | 33 | TEST_IMPL(loop_close) { 34 | int r; 35 | uv_loop_t loop; 36 | 37 | loop.data = &loop; 38 | ASSERT(0 == uv_loop_init(&loop)); 39 | ASSERT(loop.data == (void*) &loop); 40 | 41 | uv_timer_init(&loop, &timer_handle); 42 | uv_timer_start(&timer_handle, timer_cb, 100, 100); 43 | 44 | ASSERT(UV_EBUSY == uv_loop_close(&loop)); 45 | 46 | uv_run(&loop, UV_RUN_DEFAULT); 47 | 48 | uv_close((uv_handle_t*) &timer_handle, NULL); 49 | r = uv_run(&loop, UV_RUN_DEFAULT); 50 | ASSERT(r == 0); 51 | 52 | ASSERT(loop.data == (void*) &loop); 53 | ASSERT(0 == uv_loop_close(&loop)); 54 | ASSERT(loop.data == (void*) &loop); 55 | 56 | return 0; 57 | } 58 | 59 | static void loop_instant_close_work_cb(uv_work_t* req) { 60 | } 61 | 62 | static void loop_instant_close_after_work_cb(uv_work_t* req, int status) { 63 | } 64 | 65 | TEST_IMPL(loop_instant_close) { 66 | static uv_loop_t loop; 67 | static uv_work_t req; 68 | ASSERT(0 == uv_loop_init(&loop)); 69 | ASSERT(0 == uv_queue_work(&loop, 70 | &req, 71 | loop_instant_close_work_cb, 72 | loop_instant_close_after_work_cb)); 73 | MAKE_VALGRIND_HAPPY(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /test/test-get-passwd.c: -------------------------------------------------------------------------------- 1 | /* Copyright libuv contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "task.h" 24 | #include 25 | 26 | TEST_IMPL(get_passwd) { 27 | uv_passwd_t pwd; 28 | size_t len; 29 | int r; 30 | 31 | /* Test the normal case */ 32 | r = uv_os_get_passwd(&pwd); 33 | ASSERT(r == 0); 34 | len = strlen(pwd.username); 35 | ASSERT(len > 0); 36 | 37 | #ifdef _WIN32 38 | ASSERT(pwd.shell == NULL); 39 | #else 40 | len = strlen(pwd.shell); 41 | ASSERT(len > 0); 42 | #endif 43 | 44 | len = strlen(pwd.homedir); 45 | ASSERT(len > 0); 46 | 47 | #ifdef _WIN32 48 | if (len == 3 && pwd.homedir[1] == ':') 49 | ASSERT(pwd.homedir[2] == '\\'); 50 | else 51 | ASSERT(pwd.homedir[len - 1] != '\\'); 52 | #else 53 | if (len == 1) 54 | ASSERT(pwd.homedir[0] == '/'); 55 | else 56 | ASSERT(pwd.homedir[len - 1] != '/'); 57 | #endif 58 | 59 | #ifdef _WIN32 60 | ASSERT(pwd.uid == -1); 61 | ASSERT(pwd.gid == -1); 62 | #else 63 | ASSERT(pwd.uid >= 0); 64 | ASSERT(pwd.gid >= 0); 65 | #endif 66 | 67 | /* Test uv_os_free_passwd() */ 68 | uv_os_free_passwd(&pwd); 69 | 70 | ASSERT(pwd.username == NULL); 71 | ASSERT(pwd.shell == NULL); 72 | ASSERT(pwd.homedir == NULL); 73 | 74 | /* Test a double free */ 75 | uv_os_free_passwd(&pwd); 76 | 77 | ASSERT(pwd.username == NULL); 78 | ASSERT(pwd.shell == NULL); 79 | ASSERT(pwd.homedir == NULL); 80 | 81 | /* Test invalid input */ 82 | r = uv_os_get_passwd(NULL); 83 | ASSERT(r == UV_EINVAL); 84 | 85 | return 0; 86 | } 87 | --------------------------------------------------------------------------------