From 76c986bdc7c85a9efc42a19dd8c043c35f831ef8 Mon Sep 17 00:00:00 2001 From: mehrdadn Date: Sun, 5 Jan 2020 21:21:17 -0800 Subject: [PATCH] Windows compatibility stubs (#6706) --- BUILD.bazel | 31 ++++++++++++++++++++++--------- src/shims/windows/sys/wait.cc | 27 +++++++++++++++++++++++++++ src/shims/windows/sys/wait.h | 11 +++++++++++ 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/shims/windows/sys/wait.cc diff --git a/BUILD.bazel b/BUILD.bazel index a1f6cfd54..df47cf0ae 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1093,15 +1093,28 @@ genrule( "redis-server", "redis-cli", ], - cmd = """ - set -x && - curl -sL \"https://github.com/antirez/redis/archive/5.0.3.tar.gz\" | tar -xzf - --strip-components=1 -C . && - make && - mv ./src/redis-server $(location redis-server) && - chmod +x $(location redis-server) && - mv ./src/redis-cli $(location redis-cli) && - chmod +x $(location redis-cli) - """, + cmd = select({ + # Windows is not supported yet, so make stubs for it + "@bazel_tools//src/conditions:windows": """ + set -eu + for f in $(OUTS); do + { + echo '#!/usr/bin/env bash' + echo 'echo "$${0##*/} is unsupported on this platform" 1>&2 && false' + } > "$$f" + chmod +x "$$f" + done + """, + "//conditions:default": """ + set -x && + curl -sL \"https://github.com/antirez/redis/archive/5.0.3.tar.gz\" | tar -xzf - --strip-components=1 -C . && + make && + mv ./src/redis-server $(location redis-server) && + chmod +x $(location redis-server) && + mv ./src/redis-cli $(location redis-cli) && + chmod +x $(location redis-cli) + """, + }), visibility = ["//java:__subpackages__"], ) diff --git a/src/shims/windows/sys/wait.cc b/src/shims/windows/sys/wait.cc new file mode 100644 index 000000000..30e7ba930 --- /dev/null +++ b/src/shims/windows/sys/wait.cc @@ -0,0 +1,27 @@ +#include + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#include + +pid_t waitpid(pid_t pid, int *status, int options) { + int result; + if (pid <= 0) { + result = -1; + errno = ECHILD; + } else if (HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid)) { + DWORD timeout = status && *status == WNOHANG ? 0 : INFINITE; + if (WaitForSingleObject(process, timeout) != WAIT_FAILED) { + result = 0; + } else { + result = -1; + errno = ECHILD; + } + CloseHandle(process); + } else { + result = -1; + errno = ECHILD; + } + return result; +} diff --git a/src/shims/windows/sys/wait.h b/src/shims/windows/sys/wait.h index 442218408..767b07735 100644 --- a/src/shims/windows/sys/wait.h +++ b/src/shims/windows/sys/wait.h @@ -1,4 +1,15 @@ #ifndef WAIT_H #define WAIT_H +#include // pid_t + +#define WNOHANG 1 + +__declspec( + deprecated("Waiting on a process by ID has an inherent race condition" + " on Windows and is discouraged. " + "Please use a wrapper that keeps the process handle alive" + " and waits on it directly as needed." + "")) pid_t waitpid(pid_t pid, int *status, int options); + #endif /* WAIT_H */