From e7b6876877243ca27dafe0d584f74684573138c8 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Sat, 3 Oct 2026 22:58:55 -0400 Subject: [PATCH] Add the droiddeck-esync pack pipeline (#189) Builds esync packs for new Valve, GE and CachyOS Proton 11 releases on arm64 runners, smoke-tests them against stock with esstress, and on main publishes each pack and a signed index as non-latest releases. A watcher checks Steam and the GE/CachyOS releases every 20 minutes and starts a build for new ones. The app side lands separately with feat/userspace-esync-ntsync. --- .../workflows/build-droiddeck-esync-packs.yml | 449 +++ .github/workflows/watch-proton-releases.yml | 112 + tools/droiddeck-esync/PATCHES.md | 170 ++ tools/droiddeck-esync/build-local.sh | 27 + tools/droiddeck-esync/build-pack.sh | 125 + tools/droiddeck-esync/discover.py | 654 +++++ tools/droiddeck-esync/flavors.json | 84 + tools/droiddeck-esync/index-key.pub | 4 + tools/droiddeck-esync/make_index.py | 285 ++ tools/droiddeck-esync/make_pack.py | 367 +++ .../cachyos-11/0001-droiddeck-esync.patch | 2436 +++++++++++++++++ .../valve-11/0001-droiddeck-esync.patch | 2436 +++++++++++++++++ tools/droiddeck-esync/revoked.txt | 0 tools/droiddeck-esync/smoke-test.sh | 62 + tools/droiddeck-esync/tests/esstress.c | 618 +++++ .../overlay/usr/local/bin/droiddeck-esync | 1171 ++++++++ 16 files changed, 9000 insertions(+) create mode 100644 .github/workflows/build-droiddeck-esync-packs.yml create mode 100644 .github/workflows/watch-proton-releases.yml create mode 100644 tools/droiddeck-esync/PATCHES.md create mode 100755 tools/droiddeck-esync/build-local.sh create mode 100755 tools/droiddeck-esync/build-pack.sh create mode 100755 tools/droiddeck-esync/discover.py create mode 100644 tools/droiddeck-esync/flavors.json create mode 100644 tools/droiddeck-esync/index-key.pub create mode 100755 tools/droiddeck-esync/make_index.py create mode 100755 tools/droiddeck-esync/make_pack.py create mode 100644 tools/droiddeck-esync/patches/cachyos-11/0001-droiddeck-esync.patch create mode 100644 tools/droiddeck-esync/patches/valve-11/0001-droiddeck-esync.patch create mode 100644 tools/droiddeck-esync/revoked.txt create mode 100755 tools/droiddeck-esync/smoke-test.sh create mode 100644 tools/droiddeck-esync/tests/esstress.c create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-esync diff --git a/.github/workflows/build-droiddeck-esync-packs.yml b/.github/workflows/build-droiddeck-esync-packs.yml new file mode 100644 index 000000000..a98ae23ab --- /dev/null +++ b/.github/workflows/build-droiddeck-esync-packs.yml @@ -0,0 +1,449 @@ +name: Build droiddeck-esync packs + +on: + schedule: + - cron: '17 5 * * *' + push: + branches: [ main ] + paths: + - 'tools/droiddeck-esync/**' + - '.github/workflows/build-droiddeck-esync-packs.yml' + workflow_dispatch: + inputs: + flavor: + description: 'Proton flavor to look at' + type: choice + options: [ all, valve, ge, cachyos ] + default: all + ref: + description: 'One release tag to build (e.g. GE-Proton11-7); empty = the newest builds of each family' + required: false + default: '' + publish: + description: 'Publish the packs and sign a new index' + type: boolean + default: false + +permissions: + contents: write + +concurrency: + group: droiddeck-esync-packs-${{ (github.repository == 'Droid-Deck/DroidDeck' && github.ref == 'refs/heads/main' && (github.event_name != 'workflow_dispatch' || inputs.publish)) && 'publish' || format('build-{0}', github.run_id) }} + cancel-in-progress: false + +env: + SYNC_BASE_URL: https://github.com/Droid-Deck/DroidDeck/releases/download + +jobs: + discover: + if: ${{ github.repository == 'Droid-Deck/DroidDeck' || github.event_name == 'workflow_dispatch' }} + runs-on: ubuntu-24.04 + timeout-minutes: 15 + permissions: + contents: read + outputs: + matrix: ${{ steps.discover.outputs.matrix }} + count: ${{ steps.discover.outputs.count }} + unindexed: ${{ steps.discover.outputs.unindexed }} + publish: ${{ steps.discover.outputs.publish }} + steps: + - uses: actions/checkout@v4 + + - name: Find the Proton builds without a pack + id: discover + env: + GH_TOKEN: ${{ github.token }} + FLAVOR: ${{ inputs.flavor || 'all' }} + REF: ${{ inputs.ref }} + ASKED: ${{ github.event_name != 'workflow_dispatch' || inputs.publish }} + PUBLISH: ${{ github.repository == 'Droid-Deck/DroidDeck' && github.ref == 'refs/heads/main' && (github.event_name != 'workflow_dispatch' || inputs.publish) }} + run: | + set -euo pipefail + if [ "$ASKED" = true ] && [ "$PUBLISH" != true ]; then + echo "::notice::packs are published only from main of Droid-Deck/DroidDeck; this run only builds them" + fi + echo "publish=$PUBLISH" >> "$GITHUB_OUTPUT" + python3 tools/droiddeck-esync/discover.py matrix --flavor "$FLAVOR" --ref "$REF" \ + --index-release droiddeck-esync-index --repo "$GITHUB_REPOSITORY" --revoked tools/droiddeck-esync/revoked.txt + + build: + name: ${{ matrix.flavor }} ${{ matrix.tag }} + needs: discover + if: needs.discover.outputs.count != '0' + runs-on: ubuntu-24.04-arm + timeout-minutes: 360 + permissions: + contents: read + strategy: + fail-fast: false + max-parallel: 2 + matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} + env: + FLAVOR: ${{ matrix.flavor }} + REPO: ${{ matrix.repo }} + TAG: ${{ matrix.tag }} + ASSET: ${{ matrix.asset }} + VERSION_TOKEN: ${{ matrix.token }} + DEPOT_APP: ${{ matrix.depot_app }} + REV: ${{ matrix.rev }} + SERIES: ${{ matrix.series }} + PROFILE: ${{ matrix.profile }} + SOURCE_MATCH: ${{ matrix.source_match }} + SUBMODULES: ${{ matrix.submodules }} + CLONE: ${{ matrix.clone }} + PREP: ${{ matrix.prep }} + BUILD_DIR: ${{ matrix.build_dir }} + HAVE_DEPOT_LOGIN: ${{ secrets.STEAM_DEPOT_USER != '' && secrets.STEAM_DEPOT_TOKEN != '' }} + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Check the build script and the patch series + run: | + set -euo pipefail + test -f tools/droiddeck-esync/build-pack.sh + ls "tools/droiddeck-esync/patches/$SERIES"/*.patch + + - name: Install the host tools + run: | + set -euo pipefail + sudo apt-get update -qq + sudo apt-get install -y -qq autoconf automake git patch perl python3 rsync unzip wget xz-utils zstd >/dev/null + + - name: Fetch the stock binaries + if: matrix.asset != '' + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + rm -rf stock-download stock + mkdir -p stock-download stock + gh release download "$TAG" -R "$REPO" -p "$ASSET" -D stock-download + tar -xf "stock-download/$ASSET" -C stock + rm -rf stock-download + mapfile -t tops < <(find stock -mindepth 1 -maxdepth 1 -type d) + if [ "${#tops[@]}" != 1 ]; then + echo "::error::$ASSET does not hold exactly one Proton directory" + exit 1 + fi + dir=$(realpath "${tops[0]}") + test -f "$dir/files/lib/wine/aarch64-unix/ntdll.so" + test -f "$dir/files/bin-arm64/wineserver" + line=$(head -n1 "$dir/version" | tr -d '\r') + if ! grep -qE '^[0-9]+[[:space:]]+[[:graph:]]+' <<<"$line"; then + echo "::error::$ASSET has no usable version file" + exit 1 + fi + echo "Stock $line" + { + echo "STOCK_DIR=$dir" + echo "VERSION_LINE=$line" + } >> "$GITHUB_ENV" + + - name: Fetch the stock depot (optional, Valve only, needs STEAM_DEPOT_USER, STEAM_DEPOT_TOKEN and DEPOT_DOWNLOADER_SHA256) + if: matrix.flavor == 'valve' && env.HAVE_DEPOT_LOGIN == 'true' + continue-on-error: true + env: + GH_TOKEN: ${{ github.token }} + DEPOT_DOWNLOADER_TAG: DepotDownloader_3.4.0 + DEPOT_DOWNLOADER_SHA256: ${{ vars.DEPOT_DOWNLOADER_SHA256 }} + STEAM_DEPOT_USER: ${{ secrets.STEAM_DEPOT_USER }} + STEAM_DEPOT_TOKEN: ${{ secrets.STEAM_DEPOT_TOKEN }} + run: | + set -euo pipefail + if ! grep -qxE '[0-9a-f]{64}' <<<"$DEPOT_DOWNLOADER_SHA256"; then + echo "::warning::set the repository variable DEPOT_DOWNLOADER_SHA256 to the sha256 of DepotDownloader-linux-arm64.zip from $DEPOT_DOWNLOADER_TAG to fetch the depot" + exit 0 + fi + rm -rf depotdownloader depot + gh release download "$DEPOT_DOWNLOADER_TAG" -R SteamRE/DepotDownloader -p 'DepotDownloader-linux-arm64.zip' -D depotdownloader + echo "$DEPOT_DOWNLOADER_SHA256 depotdownloader/DepotDownloader-linux-arm64.zip" | sha256sum -c - + unzip -q depotdownloader/DepotDownloader-linux-arm64.zip -d depotdownloader + chmod +x depotdownloader/DepotDownloader + printf '%s\n' version files/lib/wine/aarch64-unix/ntdll.so files/bin-arm64/wineserver > depot-files.txt + timeout 30m depotdownloader/DepotDownloader -app "$DEPOT_APP" -os linux -all-archs \ + -username "$STEAM_DEPOT_USER" -password "$STEAM_DEPOT_TOKEN" \ + -filelist depot-files.txt -dir depot > "$GITHUB_ENV" + + - name: Prepare the wine source + run: | + set -euo pipefail + git config --global user.name "ci" + git config --global user.email "ci@users.noreply.github.com" + rm -rf src + git init -q src + git -C src remote add origin "https://github.com/$REPO.git" + git -C src fetch -q --depth 1 origin "refs/tags/$TAG:refs/tags/$TAG" + git -C src checkout -q "refs/tags/$TAG" + read -r -a modules <<<"$SUBMODULES" + case "$CLONE" in + shallow) history=(--depth 1) ;; + treeless) history=(--filter=tree:0) ;; + *) echo "::error::unknown clone mode $CLONE"; exit 1 ;; + esac + git -C src submodule update -q --init "${history[@]}" -- "${modules[@]}" + if [ -n "$PREP" ]; then + if ! (cd src && bash "$PREP") > prep.log 2>&1; then + tail -n 200 prep.log + echo "::error::$PREP failed" + exit 1 + fi + fi + test -f src/wine/server/protocol.def + image=$(python3 tools/droiddeck-esync/discover.py sdk-image src/Makefile.in) + commit=$(git -C src rev-parse HEAD) + wine_commit=$(git -C src rev-parse HEAD:wine) + line=${VERSION_LINE:-} + if [ -z "$line" ]; then + if [ -z "$VERSION_TOKEN" ]; then + echo "::error::no stock version file and no version token for $TAG" + exit 1 + fi + line="$(git -C src log -1 --format=%ct HEAD) $VERSION_TOKEN" + fi + echo "SDK image $image, $REPO $commit, wine $wine_commit, version $line" + { + echo "SDK_IMAGE=$image" + echo "SOURCE_COMMIT=$commit" + echo "WINE_COMMIT=$wine_commit" + echo "VERSION_LINE=$line" + echo "SOURCE_DATE_EPOCH=${line%%[[:space:]]*}" + } >> "$GITHUB_ENV" + + - name: Build the control and the patched binaries + run: | + set -euo pipefail + docker pull -q "$SDK_IMAGE" + for kind in control patched; do + work=$PWD/build/work-$kind + out=$PWD/build/$kind + sudo rm -rf "$work" "$out" + mkdir -p "$work" "$out" + if [ "$kind" = patched ]; then + bash tools/droiddeck-esync/build-pack.sh prepare "$PWD/src/wine" "$work" "$PWD/tools/droiddeck-esync/patches/$SERIES" + else + bash tools/droiddeck-esync/build-pack.sh prepare "$PWD/src/wine" "$work" + fi + docker run --rm \ + -v "$work:$BUILD_DIR" -v "$out:/out" -v "$PWD/tools/droiddeck-esync/build-pack.sh:/build-pack.sh:ro" \ + -w "$BUILD_DIR" -e HOME=/tmp -e LANG=C.UTF-8 -e JOBS="$(nproc)" \ + -e SOURCE_DATE_EPOCH -e SYNC_PROFILE="$PROFILE" \ + "$SDK_IMAGE" /bin/bash /build-pack.sh build "$BUILD_DIR" /out + done + sudo chown -R "$(id -u):$(id -g)" build + sha256sum build/*/files/lib/wine/aarch64-unix/ntdll.so build/*/files/bin-arm64/wineserver + + - name: Make the pack + run: | + set -euo pipefail + source_json=$(python3 -c 'import json, os; print(json.dumps({"repo": os.environ["REPO"], "ref": os.environ["TAG"], "asset": os.environ["ASSET"], "commit": os.environ["SOURCE_COMMIT"], "wine_commit": os.environ["WINE_COMMIT"], "sdk_image": os.environ["SDK_IMAGE"]}, sort_keys=True))') + match=() + if [ -z "${STOCK_DIR:-}" ] && [ "$SOURCE_MATCH" = true ]; then + match=(--source-match) + fi + rm -rf out + id=$(python3 tools/droiddeck-esync/make_pack.py --flavor "$FLAVOR" --rev "$REV" --stock-dir "${STOCK_DIR:-}" \ + --version-line "$VERSION_LINE" --control build/control --patched build/patched \ + --source-json "$source_json" --patch-series "$SERIES" --out out "${match[@]}") + echo "Pack \`$id\` from $FLAVOR $TAG" >> "$GITHUB_STEP_SUMMARY" + ls -l out + + - name: Smoke-test the pack on the release it was made for + if: matrix.asset != '' + run: | + set -euo pipefail + id=$(basename out/*.tzst .tzst) + docker run --rm -v "$PWD:/work" -w /work "$SDK_IMAGE" \ + /opt/llvm-mingw/bin/aarch64-w64-mingw32-clang -O2 -o esstress.exe tools/droiddeck-esync/tests/esstress.c + set +e + bash tools/droiddeck-esync/smoke-test.sh "$STOCK_DIR" "out/$id.tzst" esstress.exe smoke + status=$? + set -e + case $status in + 0) echo "Smoke test passed for \`$id\`" >> "$GITHUB_STEP_SUMMARY" ;; + 3) echo "::warning::stock $TAG does not run on this runner, so $id could not be smoke-tested" ;; + *) echo "::error::$id failed the smoke test on $TAG"; exit 1 ;; + esac + + - uses: actions/upload-artifact@v4 + with: + name: droiddeck-esync-pack-${{ matrix.key }} + path: out/ + if-no-files-found: error + + - uses: actions/upload-artifact@v4 + if: failure() + with: + name: droiddeck-esync-logs-${{ matrix.key }} + path: | + prep.log + build/work-*/*.log + smoke/*.log + if-no-files-found: ignore + + publish: + name: Publish the packs + needs: [ discover, build ] + if: ${{ !cancelled() && needs.discover.result == 'success' && needs.discover.outputs.publish == 'true' && needs.discover.outputs.count != '0' }} + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - uses: actions/download-artifact@v4 + with: + pattern: droiddeck-esync-pack-* + path: packs + + - name: Upload the packs to their releases + env: + GH_TOKEN: ${{ github.token }} + MATRIX: ${{ needs.discover.outputs.matrix }} + run: | + set -euo pipefail + mkdir -p packs + python3 tools/droiddeck-esync/discover.py packs --matrix "$MATRIX" packs > packs.tsv + if [ ! -s packs.tsv ]; then + echo "::notice::no build made a pack" + exit 0 + fi + while IFS=$'\t' read -r -u 3 flavor dir id; do + tag="droiddeck-esync-$flavor" + if ! gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + gh release create "$tag" -R "$GITHUB_REPOSITORY" --latest=false --prerelease=false --title "$tag" \ + --notes "droiddeck-esync packs for $flavor Proton builds (tools/droiddeck-esync). The app fetches them through the signed index in the droiddeck-esync-index release." \ + || gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null + fi + gh release upload "$tag" -R "$GITHUB_REPOSITORY" "$dir/$id.tzst" --clobber + gh release upload "$tag" -R "$GITHUB_REPOSITORY" "$dir/$id.json" --clobber + gh release edit "$tag" -R "$GITHUB_REPOSITORY" --latest=false --prerelease=false + echo "Published \`$id\` to $tag" >> "$GITHUB_STEP_SUMMARY" + done 3< packs.tsv + + - name: Record the builds that made no pack + env: + GH_TOKEN: ${{ github.token }} + MATRIX: ${{ needs.discover.outputs.matrix }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + set -euo pipefail + mkdir -p packs failed + python3 tools/droiddeck-esync/discover.py failures --matrix "$MATRIX" packs > failed.tsv + while IFS=$'\t' read -r -u 3 flavor marker; do + tag="droiddeck-esync-$flavor" + if ! gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + gh release create "$tag" -R "$GITHUB_REPOSITORY" --latest=false --prerelease=false --title "$tag" \ + --notes "droiddeck-esync packs for $flavor Proton builds (tools/droiddeck-esync). The app fetches them through the signed index in the droiddeck-esync-index release." \ + || gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null + fi + echo "$RUN_URL" > "failed/$marker" + gh release upload "$tag" -R "$GITHUB_REPOSITORY" "failed/$marker" --clobber + echo "::warning::$marker: no pack was made; discovery skips this build for a day unless rev or the patch series changes" + done 3< failed.tsv + + index: + name: Sign the index + needs: [ discover, build, publish ] + if: ${{ !cancelled() && needs.discover.result == 'success' && needs.discover.outputs.publish == 'true' && (needs.discover.outputs.count != '0' || needs.discover.outputs.unindexed != '0') }} + runs-on: ubuntu-24.04 + timeout-minutes: 30 + environment: droiddeck-esync-signing + steps: + - uses: actions/checkout@v4 + + - name: Collect the published packs + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + fetch() { + local out + if out=$(gh release download "$1" -R "$GITHUB_REPOSITORY" -p "$2" -D "$3" --clobber 2>&1); then + return 0 + fi + if grep -qiE 'release not found|no assets match' <<<"$out"; then + echo "::notice::$1 has no $2 yet" + return 0 + fi + echo "$out" >&2 + return 1 + } + rm -rf entries previous bundle-packs out + mkdir -p entries previous bundle-packs + for flavor in valve ge cachyos; do + fetch "droiddeck-esync-$flavor" '*.json' entries + done + fetch droiddeck-esync-index index.json previous + fetch droiddeck-esync-index index.json.sig previous + args=(--entries entries --base-url "$SYNC_BASE_URL" --revoked tools/droiddeck-esync/revoked.txt) + if [ -f previous/index.json ]; then + args+=(--previous previous/index.json --previous-sig previous/index.json.sig) + fi + python3 tools/droiddeck-esync/make_index.py "${args[@]}" --list-bundle > bundle.tsv + while IFS=$'\t' read -r flavor id; do + fetch "droiddeck-esync-$flavor" "$id.tzst" bundle-packs + done < bundle.tsv + echo "Entries: $(find entries -name '*.json' | wc -l); bundled packs: $(wc -l < bundle.tsv)" + + - name: Sign the index and make the apk bundle + id: index + env: + SYNC_INDEX_KEY: ${{ secrets.SYNC_INDEX_KEY }} + run: | + set -euo pipefail + if [ -z "$SYNC_INDEX_KEY" ]; then + echo "::error::the droiddeck-esync-signing environment has no SYNC_INDEX_KEY secret" + exit 1 + fi + args=(--entries entries --base-url "$SYNC_BASE_URL" --revoked tools/droiddeck-esync/revoked.txt) + if [ -f previous/index.json ]; then + args+=(--previous previous/index.json --previous-sig previous/index.json.sig) + fi + python3 tools/droiddeck-esync/make_index.py "${args[@]}" --out out --bundle-packs-dir bundle-packs + + - name: Publish the index + env: + GH_TOKEN: ${{ github.token }} + CHANGED: ${{ steps.index.outputs.changed }} + BUNDLE: ${{ steps.index.outputs.bundle }} + run: | + set -euo pipefail + if [ "$CHANGED" != true ]; then + echo "::notice::no pack was added, replaced or revoked; the published index stays" + exit 0 + fi + tag=droiddeck-esync-index + if ! gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + gh release create "$tag" -R "$GITHUB_REPOSITORY" --latest=false --prerelease=false --title "$tag" \ + --notes "Signed index of the droiddeck-esync packs (tools/droiddeck-esync). The app fetches index.json and index.json.sig; the apk build bundles the bundle-*.tzst that tools/droiddeck-esync/release.env names." \ + || gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null + fi + gh release upload "$tag" -R "$GITHUB_REPOSITORY" "out/$BUNDLE" + gh release upload "$tag" -R "$GITHUB_REPOSITORY" out/index.json out/index.json.sig --clobber + sum=$(sha256sum "out/$BUNDLE" | cut -d' ' -f1) + gh release edit "$tag" -R "$GITHUB_REPOSITORY" --latest=false --prerelease=false + { + echo "### tools/droiddeck-esync/release.env" + echo + echo '```' + echo "SYNC_BUNDLE_TAG=$tag" + echo "SYNC_BUNDLE_ASSET=$BUNDLE" + echo "SYNC_BUNDLE_SHA256=$sum" + echo '```' + } | tee -a "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/watch-proton-releases.yml b/.github/workflows/watch-proton-releases.yml new file mode 100644 index 000000000..b5f56b39a --- /dev/null +++ b/.github/workflows/watch-proton-releases.yml @@ -0,0 +1,112 @@ +name: Watch Proton releases + +on: + schedule: + - cron: '*/20 * * * *' + workflow_dispatch: + +permissions: + contents: write + actions: write + +concurrency: + group: watch-proton-releases + cancel-in-progress: false + +jobs: + watch: + if: ${{ github.repository == 'Droid-Deck/DroidDeck' }} + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Pick the steamcmd cache week + id: week + run: echo "week=$(date -u +%G-%V)" >> "$GITHUB_OUTPUT" + + - uses: actions/cache@v4 + with: + path: ~/steamcmd + key: steamcmd-${{ runner.os }}-${{ steps.week.outputs.week }} + restore-keys: steamcmd-${{ runner.os }}- + + - name: Read the Proton builds Steam ships + run: | + set -euo pipefail + sudo apt-get update -qq + sudo apt-get install -y -qq lib32gcc-s1 >/dev/null + mkdir -p ~/steamcmd + if [ ! -x ~/steamcmd/steamcmd.sh ]; then + curl -fsSL https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz | tar -xz -C ~/steamcmd + fi + apps=$(python3 tools/droiddeck-esync/discover.py steam-apps) + args=(+login anonymous +app_info_update 1) + for app in $apps; do + args+=(+app_info_print "$app") + done + if ! timeout 300 ~/steamcmd/steamcmd.sh "${args[@]}" +quit > steam-info.txt 2>&1; then + tail -n 20 steam-info.txt + echo "::warning::steamcmd failed; this run watches the source tags only" + : > steam-info.txt + fi + + - name: Fetch the previous watch state + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + mkdir -p previous + if ! out=$(gh release download droiddeck-esync-index -R "$GITHUB_REPOSITORY" -p watch-state.json -D previous --clobber 2>&1); then + if grep -qiE 'release not found|no assets match' <<<"$out"; then + echo "::notice::no watch state yet; every flavor counts as new" + else + echo "$out" >&2 + exit 1 + fi + fi + + - name: Compare with the last run + id: watch + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + info=() + if [ -s steam-info.txt ]; then + info=(--steam-info steam-info.txt) + fi + python3 tools/droiddeck-esync/discover.py watch --state previous/watch-state.json "${info[@]}" --out watch-state.json + + - name: Start the pack builds + if: steps.watch.outputs.changed != '' + env: + GH_TOKEN: ${{ github.token }} + CHANGED: ${{ steps.watch.outputs.changed }} + run: | + set -euo pipefail + read -r -a flavors <<<"$CHANGED" + if [ "${#flavors[@]}" -gt 1 ]; then + flavors=(all) + fi + for flavor in "${flavors[@]}"; do + gh workflow run build-droiddeck-esync-packs.yml -R "$GITHUB_REPOSITORY" --ref main -f flavor="$flavor" -f publish=true + echo "Started the pack build for $flavor" >> "$GITHUB_STEP_SUMMARY" + done + + - name: Save the watch state + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + tag=droiddeck-esync-index + if ! gh release view "$tag" -R "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + echo "::notice::$tag does not exist yet; the state is saved once the first index is published" + exit 0 + fi + if cmp -s watch-state.json previous/watch-state.json; then + exit 0 + fi + gh release upload "$tag" -R "$GITHUB_REPOSITORY" watch-state.json --clobber diff --git a/tools/droiddeck-esync/PATCHES.md b/tools/droiddeck-esync/PATCHES.md new file mode 100644 index 000000000..bfefaade9 --- /dev/null +++ b/tools/droiddeck-esync/PATCHES.md @@ -0,0 +1,170 @@ +# droiddeck-esync packs + +DroidDeck gives Proton three in-process sync backends, picked with the Wine sync tabs in the +Steam settings' Games group: ntsync, fsync (the default), esync and wineserver. The wineserver tab +sets `PROTON_NO_ESYNC=1`, `PROTON_NO_FSYNC=1` and `PROTON_NO_NTSYNC=1` (unless a game's own +environment sets them), so every sync object goes through the server. + +- droiddeck-esync: eventfd and shared memory sync. Stock Wine 11 / Proton 11 builds + have no eventfd-based sync, so the patch series under `patches/` adds it to wineserver + and the unix side of ntdll. A Proton build without a pack (a release newer than the published + packs, or one whose pack failed to install) runs stock with droiddeck-fsync standing in, or + droiddeck-ntsync where fsync is off for the game (`PROTON_NO_FSYNC=1`, or one of the games + Proton itself keeps off fsync and esync), so it still gets in-process sync; `PROTON_NO_NTSYNC=1` + as well leaves it on wineserver-only sync. A Proton with esync built in (GE, cachyos) runs its + own esync, and a game that turns esync off moves to droiddeck-fsync or droiddeck-ntsync there + too. +- droiddeck-fsync: Proton's own fsync on the stock binaries. fsync waits with `futex_waitv(2)`, + which the app sandbox refuses, so `libblsession.so` (`BL_FSYNC=1`) answers that system call in + userspace: every waiter registers the futexes it waits on in a shared table next to the fsync + shared memory and sleeps on a futex of its own, and every `FUTEX_WAKE` on the fsync shared memory + wakes the waiters registered on that word. No pack is involved, so it keeps working the moment + Steam updates Proton. It is the default (the fsync tab, `BL_FSYNC_FIRST=1`, also per game), + ahead of droiddeck-esync; on the esync tab it stands in where no pack fits, and it also takes + over for a game whose environment turns esync off (`PROTON_NO_ESYNC=1`, `WINEESYNC=0`, or too + few file descriptors); where fsync is off as well, such a game gets droiddeck-ntsync, with the + pack's wineserver when a pack fits. A launch into a prefix whose wineserver is still running + keeps the sync choice that server was started with (`prefixes.json` in the store), because a + Wine client that disagrees with its server about fsync exits. It inherits stock fsync's behaviour, the same as on a Linux kernel: + a wait-all on a mutex abandoned by a dead thread is not satisfied, a pending user APC is + delivered ahead of an object that is already signaled, a mutex owned by a process killed with + `TerminateProcess` is not abandoned, and `PulseEvent` can miss a waiter that has not run yet. +- droiddeck-ntsync: a userspace implementation of the `/dev/ntsync` interface in + `libblsession.so` (`BL_SYNC=1`), which stock Proton 11 builds use as they would the kernel + driver. It is the ntsync tab. A pack still supplies the binaries when one fits, and its + wineserver then picks ntsync over esync; a Proton whose wineserver has no ntsync client (no + `/dev/ntsync` in it) gets droiddeck-fsync instead. + +wineserver picks the backend at startup: `PROTON_NO_NTSYNC` unset -> `/dev/ntsync` +(droiddeck-ntsync, or a kernel driver the device provides) -> fsync (droiddeck-fsync, or a kernel +and sandbox that allow `futex_waitv`) -> droiddeck-esync (needs `WINEESYNC` nonzero) -> +server-side sync. When a pack is in use and droiddeck-esync is on, the guest's `droiddeck-esync` +script also sets `PROTON_NO_FSYNC=1` unless the game's environment sets it, so droiddeck-esync +comes before fsync unless droiddeck-fsync was chosen. A wineserver from a pack reports the +backend on stderr as `droiddeck-ntsync: up and running.` or `droiddeck-esync: up and running.`; +with droiddeck-fsync the stock wineserver prints `fsync: up and running.` after +`droiddeck-fsync: up and running.`. A pack's wineserver also abandons, when a process dies, every +mutex still owned by one of its threads, and, once a thread killed by `TerminateThread` has really +exited, every mutex it still owns. A killed thread keeps running until the kill signal reaches it +and can take a mutex after the server abandoned its mutexes; without these passes that mutex would +stay locked for good (they cover droiddeck-ntsync and kernel ntsync as well). The ntdll side +reads, writes and polls its eventfds with raw system calls, so no `LD_PRELOAD` wrapper of +`read`/`write`/`poll` (the guest preloads several) sits in the sync path or in the unwind of a +thread Wine kills. + +The patch changes no wineserver request: `make_pack.py` refuses a build whose +`server_protocol.h`, `request_handlers.h` or `request_trace.h` differ from the unpatched +control build, or whose ntdll.so exports other symbols. Only `files/lib/wine/aarch64-unix/ntdll.so` +and `files/bin-arm64/wineserver` are replaced, so every Proton build needs its own pair, built +from that build's exact wine source. That pair plus `pack.json` is a pack (`.tzst`). On the +device the guest runs Proton from a shadow tree that links the stock tool and holds the two +patched files (`droiddeck-esync`, store in `~/.local/share/droiddeck-esync`). + +## Credits + +droiddeck-esync follows the esync design and droiddeck-ntsync implements the ntsync interface; +both are the work of Elizabeth Figura for Wine, Proton and the Linux kernel. droiddeck-fsync runs +fsync, by Elizabeth Figura and Paul Gofman for Proton, over `futex_waitv(2)` by AndrĂ© Almeida. +DroidDeck builds on that work for Android, where neither the ntsync driver, `futex_waitv` nor a +Proton build with esync is available. + +## Matching + +- `ge`, `cachyos`: built against the release tarball's own binaries. `pack.json` `stock` holds + their sha256; the guest uses the pack only when both files hash the same. `make_pack.py` also + requires the control build to reproduce both files byte for byte, so the patched pair differs + from the release only by the patch (`--allow-unreproduced` lifts this for local experiments). +- `valve`: the depots cannot be downloaded without a Steam login, so `stock` holds the control + build's hashes and `source_match` is true: the guest also accepts a tool whose version token + (second field of `/version`) and ntdll.so exports hash equal the pack's. With the optional + `STEAM_DEPOT_USER` / `STEAM_DEPOT_TOKEN` secrets (and the `DEPOT_DOWNLOADER_SHA256` variable) + the job downloads the depot instead and, when the depot is at the tag being built, makes an + exact pack. The guest's launch line says which kind matched (`exact match` or `source match`). +- A stock wineserver that already has esync built in (it prints `esync: up and running`) never + gets a pack. + +## Building and publishing + +`.github/workflows/watch-proton-releases.yml` runs every 20 minutes and decides when a build is +due. It asks Steam anonymously (`steamcmd +app_info_print`) which public build of each Valve ARM64 +depot app in `flavors.json` is live, and `discover.py watch` compares that, the newest `keep` tags +and releases of every flavor, each flavor's `rev` and its patch series digest with the state of the +last run (`watch-state.json` in the `droiddeck-esync-index` release). A flavor whose builds, rev or +patch series changed, or whose Steam build moved, starts +`.github/workflows/build-droiddeck-esync-packs.yml` for that flavor; nothing else starts a build. +A run that finds no change takes about a minute and builds nothing. If steamcmd fails, the run +watches the tags only and keeps the last Steam state. + +`.github/workflows/build-droiddeck-esync-packs.yml` also runs once a day as a safety net, on pushes +to `tools/droiddeck-esync/**` and by hand (flavor, one tag, publish yes/no). Only runs on `main` of +`Droid-Deck/DroidDeck` publish packs; the schedule and pushes do nothing in other repositories, +and manual runs elsewhere only build. + +1. `discover.py matrix` lists the newest `keep` builds of each family in `flavors.json` (Valve + tags, GE and CachyOS releases with an arm64 tarball) and drops the ones the published index, + or the `droiddeck-esync-` release, already has at the flavor's `rev`, the ones + `revoked.txt` names, and the ones that failed in the last day at the same `rev` and patch + series (a `-r-.failed` asset in the flavor's release). Naming the tag + in a manual run builds it anyway. +2. Each build runs on an arm64 runner with a read-only token, since it runs upstream code: the + wine source is checked out at the tag (GE also runs its `protonprep-valve-staging.sh`), + `build-pack.sh` builds a control and a patched pair inside the SDK image named by the + source's own `Makefile.in` (arm64-llvm), at the flavor's `build_dir` in `flavors.json` (the + path the release itself was built at, which ends up in the binaries), and `make_pack.py` makes + the pack. Patches apply with `--fuzz=0`: a hunk whose context moved fails the build instead of + landing somewhere else. For GE and CachyOS, whose release tarball holds the whole Proton, + `smoke-test.sh` then boots a prefix with that stock Proton and runs `tests/esstress.c` (built + with the SDK's llvm-mingw) under server-side sync as a baseline, and again with the pack under + droiddeck-esync. A pack that fails while stock passes is not published; when stock itself cannot + run on the runner the step only warns. +3. The publish job checks each build's artifact against the matrix (`discover.py packs`) and + uploads it to the `droiddeck-esync-valve`, `droiddeck-esync-ge` or `droiddeck-esync-cachyos` + release as `.tzst` with its index entry `.json`. A build that made no pack gets its + `.failed` marker there (`discover.py failures`), so a patch that does not apply or a stock + wineserver with esync built in is not rebuilt on every run. +4. The index job (environment `droiddeck-esync-signing`) runs only when the run built a pack or + a published pack is not in the index yet. It collects every `.json`, keeps the + highest `rev` per stock pair and version, marks revoked ids, signs `index.json` (ECDSA P-256, + `index-key.pub`) and publishes `bundle--.tzst` (named after `generated`, so + every signed index has its own), `index.json` and `index.json.sig` to `droiddeck-esync-index`. + `generated` only grows. Packs stay in the index until they are revoked, also when their + entry asset disappears; the previous index counts only when its signature verifies. + +A pack is rebuilt only for a new `rev`: bump `rev` of the flavor in `flavors.json` whenever the +patch series or the build changes. Local builds: `BUILD_DIR= SYNC_PROFILE= build-local.sh [patch-dir]`, +then `make_pack.py`. + +## Setup + +- Signing key: create the repository environment `droiddeck-esync-signing` (Settings -> + Environments, limit it to `main`) and store the PEM private key as its secret + `SYNC_INDEX_KEY`: `gh secret set SYNC_INDEX_KEY --env droiddeck-esync-signing < index-key.pem`. + The key never enters the repository; the app trusts only `index-key.pub` (also compiled into + `EsyncPacks.kt`), so a new key needs an app release. +- Optional: `STEAM_DEPOT_USER` and `STEAM_DEPOT_TOKEN` repository secrets for exact Valve packs, + plus the repository variable `DEPOT_DOWNLOADER_SHA256`: the sha256 of + `DepotDownloader-linux-arm64.zip` from the DepotDownloader release the workflow pins + (`DEPOT_DOWNLOADER_TAG`). The depot step is skipped while it is unset. +- APK bundle: after an index run, the job summary prints three lines. Put them in + `tools/droiddeck-esync/release.env`: + + ``` + SYNC_BUNDLE_TAG=droiddeck-esync-index + SYNC_BUNDLE_ASSET=bundle-20261002-061733.tzst + SYNC_BUNDLE_SHA256= + ``` + + `build.yml` then unpacks that bundle into `app/src/main/assets/droiddeck-esync/` (gitignored). A + published bundle is never overwritten, so the pinned sha256 stays valid; bump `release.env` + to ship newer packs inside the apk. The app fetches newer packs on its own. A pack the bundle + carries that `revoked.txt` names is left out of the apk, with a warning to bump `release.env`. + +## Revocation + +Add the pack id as the first word of a line in `revoked.txt` and push to `main`. The workflow +re-signs the index with `"revoked": true` for that id; the app deletes installed copies and +never installs it again, and `discover.py` does not rebuild it at the same `rev`. The apk build +also leaves it out of the bundled packs. To ship a fixed pack for the same Proton build, bump the +flavor's `rev`. Runs that only build (other refs, or manual runs without publish) never queue in +front of a publishing run, so a revocation is signed by the next run on `main`. diff --git a/tools/droiddeck-esync/build-local.sh b/tools/droiddeck-esync/build-local.sh new file mode 100755 index 000000000..c10d93980 --- /dev/null +++ b/tools/droiddeck-esync/build-local.sh @@ -0,0 +1,27 @@ +#!/bin/bash +set -euo pipefail + +if [ $# -lt 4 ]; then + echo "usage: $0 [patch-dir]" >&2 + echo "env: BUILD_DIR (path the build runs at inside the SDK), SYNC_PROFILE (valve or cachyos), JOBS, SOURCE_DATE_EPOCH" >&2 + exit 2 +fi +here=$(cd "$(dirname "$0")" && pwd) +sdk=$(realpath "$1") +src=$(realpath "$2") +work=$(realpath -m "$3") +out=$(realpath -m "$4") +patches=${5:+$(realpath "$5")} +build_dir=${BUILD_DIR:-/builds/proton/proton/build-dir} +rm -rf "$work" +mkdir -p "$work" "$out" +"$here/build-pack.sh" prepare "$src" "$work" ${patches:+"$patches"} +bwrap --unshare-user --uid 0 --gid 0 --unshare-ipc --unshare-pid \ + --bind "$sdk" / --proc /proc --dev /dev --tmpfs /tmp \ + --bind "$work" "$build_dir" --bind "$out" /out --ro-bind "$here/build-pack.sh" /build-pack.sh \ + --setenv PATH /opt/llvm-mingw/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ + --setenv HOME /tmp --setenv LANG C.UTF-8 --setenv JOBS "${JOBS:-$(nproc)}" \ + --setenv SOURCE_DATE_EPOCH "${SOURCE_DATE_EPOCH:-1700000000}" --setenv SYNC_PROFILE "${SYNC_PROFILE:-valve}" \ + --chdir "$build_dir" \ + /bin/bash /build-pack.sh build "$build_dir" /out +sha256sum "$out/files/lib/wine/aarch64-unix/ntdll.so" "$out/files/bin-arm64/wineserver" diff --git a/tools/droiddeck-esync/build-pack.sh b/tools/droiddeck-esync/build-pack.sh new file mode 100755 index 000000000..33627d8c8 --- /dev/null +++ b/tools/droiddeck-esync/build-pack.sh @@ -0,0 +1,125 @@ +#!/bin/bash +set -euo pipefail + +usage() { + echo "usage: $0 prepare [patch-dir]" >&2 + echo " $0 build " >&2 + echo " $0 all [patch-dir]" >&2 + exit 2 +} + +prepare() { + local origin=$1 b=$2 patches=${3:-} + local s=$b/src-wine + mkdir -p "$s" + rsync --filter=:C --exclude '*~' --exclude .git --exclude compile_commands.json \ + --exclude configure --exclude autom4te.cache --exclude include/config.h.in --exclude include/config.h.ink \ + --exclude include/wine/vulkan.h --exclude dlls/vulkan-1/vulkan-1.spec \ + --exclude dlls/winevulkan/loader_thunks.c --exclude dlls/winevulkan/loader_thunks.h \ + --exclude dlls/winevulkan/vulkan_thunks.c --exclude dlls/winevulkan/vulkan_thunks.h \ + --exclude dlls/winevulkan/winevulkan.json --exclude dlls/winevulkan/winevulkan.spec \ + --exclude dlls/ntdll/ntsyscalls.h --exclude dlls/win32u/win32syscalls.h \ + --exclude include/wine/server_protocol.h --exclude server/request_handlers.h --exclude server/request_trace.h \ + -Oarx --delete "$origin/" "$s/" + for f in include/wine/server_protocol.h server/request_handlers.h server/request_trace.h; do + cp -a "$origin/$f" "$s/$f" + done + if [ -n "$patches" ]; then + local p found=0 + for p in "$patches"/*.patch; do + [ -f "$p" ] || continue + patch -d "$s" -p1 --forward --fuzz=0 --no-backup-if-mismatch -s < "$p" + found=1 + done + [ "$found" = 1 ] || { echo "no patches in $patches" >&2; exit 1; } + fi + ( + cd "$s" + XDG_CACHE_HOME=$b/cache dlls/winevulkan/make_vulkan -x vk.xml -X video.xml + tools/make_specfiles || true + tools/make_requests + ) > "$b/generate.log" 2>&1 + touch "$b/.prepared" +} + +build() { + local b=$1 out=$2 + local s=$b/src-wine o=$b/obj-wine-aarch64 d=$b/dst-wine-aarch64 + [ -f "$b/.prepared" ] || { echo "$b is not prepared" >&2; exit 1; } + local jobs=${JOBS:-$(nproc)} + local epoch=$(( ${SOURCE_DATE_EPOCH:-1700000000} - 20 )) + local flags args pe_common i386_flags x86_64_flags conf + case ${SYNC_PROFILE:-valve} in + valve) + flags="-Wno-discarded-qualifiers -march=armv8.2-a -mtune=cortex-x3 -O2 -fwrapv -fno-strict-aliasing -ggdb -ffunction-sections -fdata-sections -fno-omit-frame-pointer -ffile-prefix-map=$s=." + args=(--enable-werror --with-mingw=clang --disable-tests --enable-archs=arm64ec,aarch64,i386,x86_64) + pe_common="-O2 -fwrapv -fno-strict-aliasing -ggdb -ffunction-sections -fdata-sections -fno-omit-frame-pointer -ffile-prefix-map=$s=." + i386_flags="-Wno-discarded-qualifiers -mstackrealign -march=nocona -mtune=core-avx2 -mfpmath=sse $pe_common" + x86_64_flags="-Wno-discarded-qualifiers -mcmodel=small -march=nocona -mtune=core-avx2 -mfpmath=sse $pe_common" + conf="../$(basename "$s")/configure" + ;; + cachyos) + flags="-Wno-discarded-qualifiers -Wno-stringop-overflow -Wno-incompatible-pointer-types -O2 -march=armv8.2-a -mtune=cortex-x3 -fwrapv -fno-strict-aliasing -ffunction-sections -fdata-sections -fno-omit-frame-pointer -ffile-prefix-map=$s=." + args=(--with-mingw=clang --enable-build-id --disable-tests --enable-archs=arm64ec,aarch64,i386,x86_64) + pe_common="-fwrapv -fno-strict-aliasing -ffunction-sections -fdata-sections -fno-omit-frame-pointer -ffile-prefix-map=$s=." + i386_flags="-Wno-discarded-qualifiers -Wno-stringop-overflow -Wno-incompatible-pointer-types -mstackrealign -O2 -march=nocona -mtune=core-avx2 -pipe -mfpmath=sse $pe_common" + x86_64_flags="-Wno-discarded-qualifiers -Wno-stringop-overflow -Wno-incompatible-pointer-types -mcmodel=small -O2 -march=nocona -mtune=core-avx2 -pipe -mfpmath=sse $pe_common" + conf="$s/configure" + ;; + *) + echo "unknown SYNC_PROFILE ${SYNC_PROFILE}" >&2 + exit 2 + ;; + esac + local env=(STRIP=llvm-strip AR=aarch64-linux-gnu-ar RANLIB=aarch64-linux-gnu-ranlib + CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ LD=aarch64-linux-gnu-ld + RC=aarch64-w64-mingw32-windres WIDL=aarch64-w64-mingw32-widl PKG_CONFIG=aarch64-linux-gnu-pkg-config + "PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig" + "CFLAGS=$flags" "CPPFLAGS=$flags" "CXXFLAGS=-std=c++17 $flags" "LDFLAGS=" "SOURCE_DATE_EPOCH=$epoch" + "CROSSCFLAGS=$flags" "CROSSLDFLAGS=" + "aarch64_CFLAGS=$flags" "aarch64_CPPFLAGS=$flags" "aarch64_CXXFLAGS=-std=c++17 $flags" "aarch64_LDFLAGS=" + i386_AR=i686-w64-mingw32-ar i386_RANLIB=i686-w64-mingw32-ranlib i386_CC=i686-w64-mingw32-gcc + i386_CXX=i686-w64-mingw32-g++ i386_LD=i686-w64-mingw32-ld + "i386_CFLAGS=$i386_flags" "i386_CPPFLAGS=$i386_flags" "i386_CXXFLAGS=-std=c++17 $i386_flags" "i386_LDFLAGS=" + "i386_PKG_CONFIG_LIBDIR=/usr/lib/i386-w64-mingw32/pkgconfig:/usr/share/pkgconfig" + x86_64_AR=x86_64-w64-mingw32-ar x86_64_RANLIB=x86_64-w64-mingw32-ranlib x86_64_CC=x86_64-w64-mingw32-gcc + x86_64_CXX=x86_64-w64-mingw32-g++ x86_64_LD=x86_64-w64-mingw32-ld + "x86_64_CFLAGS=$x86_64_flags" "x86_64_CPPFLAGS=$x86_64_flags" "x86_64_CXXFLAGS=-std=c++17 $x86_64_flags" "x86_64_LDFLAGS=" + "x86_64_PKG_CONFIG_LIBDIR=/usr/lib/x86_64-w64-mingw32/pkgconfig:/usr/share/pkgconfig") + export PATH=/opt/llvm-mingw/bin:$PATH + ( cd "$s" && autoreconf -fi && rm -rf autom4te.cache ) > "$b/autoreconf.log" 2>&1 + rm -rf "$o" "$d" + mkdir -p "$o" "$d" + ( cd "$o" && env "${env[@]}" "$conf" -C --prefix="$d" --libdir="$d/lib" --host=aarch64-linux-gnu "${env[@]}" \ + "${args[@]}" ) > "$b/configure.log" 2>&1 + ( cd "$o" && env "${env[@]}" make -j"$jobs" __tooldeps__ ) > "$b/tools.log" 2>&1 + ( cd "$o" && env "${env[@]}" make -j"$jobs" dlls/ntdll/ntdll.so server/wineserver ) > "$b/make.log" 2>&1 + mkdir -p "$out/files/lib/wine/aarch64-unix" "$out/files/bin-arm64" + llvm-objcopy --strip-debug --set-section-flags .text=contents,alloc,load,readonly,code \ + "$o/dlls/ntdll/ntdll.so" "$out/files/lib/wine/aarch64-unix/ntdll.so" + llvm-objcopy --strip-debug "$o/server/wineserver" "$out/files/bin-arm64/wineserver" + chmod 0755 "$out/files/lib/wine/aarch64-unix/ntdll.so" "$out/files/bin-arm64/wineserver" + cp "$s/include/wine/server_protocol.h" "$s/server/request_handlers.h" "$s/server/request_trace.h" "$out/" + sed -n 's/^#define SERVER_PROTOCOL_VERSION \([0-9]*\)$/\1/p' "$out/server_protocol.h" > "$out/protocol" +} + +[ $# -ge 1 ] || usage +case $1 in + prepare) + [ $# -ge 3 ] || usage + prepare "$(realpath "$2")" "$(realpath -m "$3")" "${4:+$(realpath "$4")}" + ;; + build) + [ $# -eq 3 ] || usage + build "$(realpath "$2")" "$(realpath -m "$3")" + ;; + all) + [ $# -ge 3 ] || usage + b=${BUILD_DIR:-/builds/proton/proton/build-dir} + prepare "$(realpath "$2")" "$b" "${4:+$(realpath "$4")}" + build "$b" "$(realpath -m "$3")" + ;; + *) + usage + ;; +esac diff --git a/tools/droiddeck-esync/discover.py b/tools/droiddeck-esync/discover.py new file mode 100755 index 000000000..028603e6d --- /dev/null +++ b/tools/droiddeck-esync/discover.py @@ -0,0 +1,654 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +import urllib.parse +from datetime import datetime, timedelta, timezone +from pathlib import Path + +from make_index import check_entry +from make_pack import FLAVORS, HERE, PATCHES, REVOKED, SERIES, PackError, patch_digest, read_revoked, sha256_file, slug + +FLAVORS_FILE = HERE / "flavors.json" +ARCHIVE = re.compile(r"\.tar\.(gz|xz)$") +ASSIGNMENT = re.compile(r"(?:export\s+|override\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*(?:\?|\+|!|:{1,3})?=\s*(.*)") +REFERENCE = re.compile(r"\$[({]([A-Za-z_][A-Za-z0-9_]*)[)}]") +IMAGE = re.compile(r"[A-Za-z0-9][A-Za-z0-9.-]*(?::[0-9]+)?(?:/[A-Za-z0-9._-]+)+(?::[A-Za-z0-9._-]+)?(?:@sha256:[0-9a-f]{64})?") +REPO = re.compile(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+") +CLONES = ("shallow", "treeless") +PACK_REV = re.compile(r"-r([0-9]+)$") +ARTIFACT_PREFIX = "droiddeck-esync-pack-" +FAILED = ".failed" +RETRY_AFTER = timedelta(days=1) +VDF_TOKEN = re.compile(r'"((?:[^"\\]|\\.)*)"|([{}])') +APP_HEADER = re.compile(r"^AppID : ([0-9]+),", re.M) +ANSI = re.compile(r"\x1b\[[0-9;]*[A-Za-z]") + + +def gh(*args: str) -> subprocess.CompletedProcess: + try: + return subprocess.run(["gh", *args], capture_output=True, text=True, check=False) + except FileNotFoundError as e: + raise PackError("the GitHub CLI (gh) is not installed") from e + + +def json_values(text: str) -> list: + decoder = json.JSONDecoder() + values = [] + position = 0 + while True: + while position < len(text) and text[position].isspace(): + position += 1 + if position == len(text): + return values + value, position = decoder.raw_decode(text, position) + values.append(value) + + +def gh_api(path: str, paginate: bool = False, missing_ok: bool = False): + result = gh("api", *(["--paginate"] if paginate else []), path) + if result.returncode != 0: + if missing_ok and "HTTP 404" in result.stderr: + return None + raise PackError(f"gh api {path}: {result.stderr.strip() or f'exit {result.returncode}'}") + try: + values = json_values(result.stdout) + except ValueError as e: + raise PackError(f"gh api {path}: {e}") from e + if len(values) == 1: + return values[0] + if values and all(isinstance(value, list) for value in values): + return [item for value in values for item in value] + raise PackError(f"gh api {path}: unexpected output") + + +def quote(value: str) -> str: + return urllib.parse.quote(value, safe="") + + +def load_flavors(path: Path) -> dict: + try: + data = json.loads(path.read_text()) + except (OSError, ValueError) as e: + raise PackError(f"cannot read {path}: {e}") from e + + def fail(why: str): + raise PackError(f"{path}: {why}") + + def compiles(pattern: str, where: str): + try: + re.compile(pattern) + except re.error as e: + fail(f"{where}: {pattern!r} is not a regular expression: {e}") + + if not isinstance(data, dict) or data.get("format") != 1 or not isinstance(data.get("flavors"), dict): + fail("is not a format 1 flavor list") + image = data.get("sdk_image") + if not isinstance(image, dict) or not all(isinstance(image.get(key), str) and image[key] for key in ("file", "variable", "match")): + fail("sdk_image needs file, variable and match") + for name, cfg in data["flavors"].items(): + if name not in FLAVORS: + fail(f"unknown flavor {name!r}") + if not isinstance(cfg, dict) or not isinstance(cfg.get("repo"), str) or not REPO.fullmatch(cfg["repo"]): + fail(f"{name}: repo must be owner/name") + for key in ("keep", "rev"): + if type(cfg.get(key)) is not int or cfg[key] < 1: + fail(f"{name}: {key} must be a positive number") + if not isinstance(cfg.get("series"), str) or not SERIES.fullmatch(cfg["series"]): + fail(f"{name}: series is not a patch series name") + if cfg.get("profile") not in ("valve", "cachyos"): + fail(f"{name}: profile must be valve or cachyos") + if type(cfg.get("source_match")) is not bool: + fail(f"{name}: source_match must be a boolean") + build_dir = cfg.get("build_dir") + if not isinstance(build_dir, str) or not re.fullmatch(r"(?:/[A-Za-z0-9._-]+)+", build_dir) or ".." in build_dir.split("/"): + fail(f"{name}: build_dir must be the absolute path the release was built at") + wine = cfg.get("wine") + submodules = wine.get("submodules") if isinstance(wine, dict) else None + if not isinstance(submodules, list) or "wine" not in submodules \ + or not all(isinstance(item, str) and re.fullmatch(r"[A-Za-z0-9._-]+(?:/[A-Za-z0-9._-]+)*", item) for item in submodules): + fail(f"{name}: wine.submodules must list the submodules to check out, wine among them") + if wine.get("clone") not in CLONES: + fail(f"{name}: wine.clone must be one of {', '.join(CLONES)}") + prep = wine.get("prep") + if not isinstance(prep, str) or (prep and not re.fullmatch(r"[A-Za-z0-9._-]+(?:/[A-Za-z0-9._-]+)*", prep)) or ".." in prep.split("/"): + fail(f"{name}: wine.prep must be a path inside the checkout or empty") + if cfg.get("list") == "tags": + families = cfg.get("families") + if not isinstance(families, dict) or not families: + fail(f"{name}: families must name at least one tag family") + for family, settings in families.items(): + if not isinstance(settings, dict) or not isinstance(settings.get("tag"), str) \ + or not isinstance(settings.get("prefix"), str) or not re.fullmatch(r"[A-Za-z0-9._-]+", settings["prefix"]) \ + or not isinstance(settings.get("depot_app", ""), str): + fail(f"{name}: family {family} needs a tag prefix and a tag pattern") + compiles(settings["tag"], f"{name}: family {family}") + if not isinstance(cfg.get("version_token"), str) or "{tag}" not in cfg["version_token"]: + fail(f"{name}: version_token must contain {{tag}}") + elif cfg.get("list") == "releases": + if not isinstance(cfg.get("tag"), str) or not isinstance(cfg.get("assets"), list) or not cfg["assets"] \ + or not all(isinstance(pattern, str) for pattern in cfg["assets"]): + fail(f"{name}: releases need a tag pattern and asset patterns") + for pattern in (cfg["tag"], *cfg["assets"]): + compiles(pattern, name) + else: + fail(f"{name}: list must be tags or releases") + return data + + +def natural_key(text: str) -> list[tuple]: + return [(0, int(part), "") if part[0] in "0123456789" else (1, 0, part) for part in re.findall(r"[0-9]+|[^0-9]+", text)] + + +def candidate(flavor: str, cfg: dict, tag: str, asset: str = "", token: str = "", depot_app: str = "") -> dict: + return { + "key": slug(f"{flavor}-{ARCHIVE.sub('', asset) if asset else tag}"), + "flavor": flavor, + "repo": cfg["repo"], + "tag": tag, + "asset": asset, + "token": token, + "depot_app": depot_app, + "rev": cfg["rev"], + "series": cfg["series"], + "profile": cfg["profile"], + "source_match": cfg["source_match"], + "submodules": " ".join(cfg["wine"]["submodules"]), + "clone": cfg["wine"]["clone"], + "prep": cfg["wine"]["prep"], + "build_dir": cfg["build_dir"], + } + + +def tag_candidates(flavor: str, cfg: dict, ref: str) -> list[dict]: + found = [] + for family, settings in sorted(cfg["families"].items()): + if ref and not ref.startswith(settings["prefix"]): + continue + refs = gh_api(f"repos/{cfg['repo']}/git/matching-refs/tags/{quote(settings['prefix'])}", paginate=True) + if not isinstance(refs, list): + raise PackError(f"{cfg['repo']}: the tag list is not a list") + pattern = re.compile(settings["tag"]) + names = set() + for item in refs: + name = item.get("ref", "") if isinstance(item, dict) else "" + if isinstance(name, str) and name.startswith("refs/tags/") and pattern.search(name[len("refs/tags/"):]): + names.add(name[len("refs/tags/"):]) + ordered = sorted(names, key=natural_key, reverse=True) + chosen = [name for name in ordered if name == ref] if ref else ordered[:cfg["keep"]] + for name in chosen: + found.append(candidate(flavor, cfg, name, token=cfg["version_token"].format(tag=name), depot_app=settings.get("depot_app", ""))) + return found + + +def pick_assets(assets: list, patterns: list[re.Pattern]) -> list[str]: + chosen = {} + for asset in assets: + name = asset.get("name") if isinstance(asset, dict) else None + if not isinstance(name, str) or not any(pattern.search(name) for pattern in patterns): + continue + stem = ARCHIVE.sub("", name) + if stem not in chosen or name.endswith(".tar.xz"): + chosen[stem] = name + return sorted(chosen.values()) + + +def release_candidates(flavor: str, cfg: dict, ref: str) -> list[dict]: + repo = cfg["repo"] + if ref: + release = gh_api(f"repos/{repo}/releases/tags/{quote(ref)}", missing_ok=True) + releases = [release] if release else [] + else: + releases = gh_api(f"repos/{repo}/releases?per_page=100") + if not isinstance(releases, list): + raise PackError(f"{repo}: the release list is not a list") + pattern = re.compile(cfg["tag"]) + patterns = [re.compile(item) for item in cfg["assets"]] + usable = [] + for release in releases: + tag = release.get("tag_name") if isinstance(release, dict) else None + if not isinstance(tag, str) or release.get("draft") or not pattern.search(tag): + continue + assets = pick_assets(release.get("assets") or [], patterns) + if assets: + usable.append((release.get("published_at") or release.get("created_at") or "", natural_key(tag), tag, assets)) + usable.sort(reverse=True) + if not ref: + usable = usable[:cfg["keep"]] + return [candidate(flavor, cfg, tag, asset=asset) for _, _, tag, assets in usable for asset in assets] + + +def built(index: dict) -> dict[tuple[str, str, str], int]: + revs = {} + packs = index.get("packs") if isinstance(index, dict) else None + for pack in packs if isinstance(packs, list) else []: + if not isinstance(pack, dict) or not isinstance(pack.get("source"), dict): + continue + rev = pack.get("rev") if type(pack.get("rev")) is int else 0 + key = (pack.get("flavor"), pack["source"].get("ref"), pack["source"].get("asset", "")) + revs[key] = max(revs.get(key, 0), rev) + return revs + + +def revoked_build(entry: dict, revoked: set[str]) -> bool: + prefixes = {f"{entry['flavor']}-{slug(name)}-" for name in (entry["token"], entry["tag"]) if name} + suffix = f"-r{entry['rev']}" + return any(ident.endswith(suffix) and ident.startswith(prefix) for ident in revoked for prefix in prefixes) + + +def chosen_flavors(flavors: dict, selected: str) -> list[str]: + return sorted(flavors["flavors"]) if selected == "all" else [selected] + + +def failure_marker(entry: dict, digests: dict[str, str] | None = None) -> str: + series = entry["series"] + if not SERIES.fullmatch(series): + raise PackError(f"{series!r} is not a patch series name") + digest = digests.get(series) if digests is not None else None + if digest is None: + digest = patch_digest(PATCHES / series) + if digests is not None: + digests[series] = digest + return f"{entry['key']}-r{entry['rev']}-{digest[:12]}{FAILED}" + + +def discover(flavors: dict, selected: str, ref: str, index: dict, revoked: set[str], + failed: frozenset[str] = frozenset()) -> list[dict]: + names = chosen_flavors(flavors, selected) + done = built(index) + digests = {} + entries = [] + seen = set() + matched = False + for flavor in names: + cfg = flavors["flavors"].get(flavor) + if cfg is None: + raise PackError(f"flavors.json has no {flavor}") + found = tag_candidates(flavor, cfg, ref) if cfg["list"] == "tags" else release_candidates(flavor, cfg, ref) + matched = matched or bool(found) + for entry in found: + if entry["key"] in seen: + continue + seen.add(entry["key"]) + label = f"{flavor} {entry['tag']}{' ' + entry['asset'] if entry['asset'] else ''}" + if revoked_build(entry, revoked): + print(f"discover: {label}: revoked at rev {entry['rev']}", file=sys.stderr) + elif done.get((flavor, entry["tag"], entry["asset"]), 0) >= entry["rev"]: + print(f"discover: {label}: in the index at rev {entry['rev']}", file=sys.stderr) + elif not ref and failure_marker(entry, digests) in failed: + print(f"discover: {label}: failed at rev {entry['rev']} with this patch series in the last " + f"{RETRY_AFTER.days} days; skipped (bump rev, change the series or build the tag by name)", file=sys.stderr) + else: + print(f"discover: {label}: to build", file=sys.stderr) + entries.append(entry) + if ref and not matched: + raise PackError(f"no {'' if selected == 'all' else selected + ' '}build is tagged {ref}") + return entries + + +def vdf(text: str) -> dict: + stack = [{}] + key = None + for match in VDF_TOKEN.finditer(text): + word, brace = match.group(1), match.group(2) + if brace == "{": + if key is None: + raise PackError("app info: a block has no name") + child = {} + stack[-1][key] = child + stack.append(child) + key = None + elif brace == "}": + if len(stack) == 1: + raise PackError("app info: unbalanced braces") + stack.pop() + key = None + elif key is None: + key = word + else: + stack[-1][key] = word + key = None + if len(stack) != 1: + raise PackError("app info: unbalanced braces") + return stack[0] + + +def steam_builds(text: str, apps: list[str]) -> dict[str, dict]: + found = {} + text = ANSI.sub("", text) + starts = [(m.start(), m.group(1)) for m in APP_HEADER.finditer(text)] + for number, (start, app) in enumerate(starts): + end = starts[number + 1][0] if number + 1 < len(starts) else len(text) + if app not in apps: + continue + body = text[text.find("\n", start) + 1:end] + info = vdf(body).get(app) + depots = info.get("depots") if isinstance(info, dict) else None + public = depots.get("branches", {}).get("public") if isinstance(depots, dict) else None + if not isinstance(public, dict) or not str(public.get("buildid", "")).isdigit(): + continue + manifests = sorted(depot["manifests"]["public"]["gid"] for name, depot in depots.items() + if name.isdigit() and isinstance(depot, dict) and isinstance(depot.get("manifests"), dict) + and isinstance(depot["manifests"].get("public"), dict) and depot["manifests"]["public"].get("gid")) + found[app] = {"buildid": public["buildid"], "built": public.get("timebuildupdated", ""), "manifests": manifests} + return found + + +def steam_apps(flavors: dict) -> list[str]: + return sorted({family["depot_app"] for cfg in flavors["flavors"].values() if cfg["list"] == "tags" + for family in cfg["families"].values() if family.get("depot_app")}) + + +def watch(flavors: dict, previous: dict, steam: dict[str, dict] | None) -> tuple[dict, list[str], list[str]]: + state = {} + changed = [] + notes = [] + for flavor in sorted(flavors["flavors"]): + cfg = flavors["flavors"][flavor] + found = tag_candidates(flavor, cfg, "") if cfg["list"] == "tags" else release_candidates(flavor, cfg, "") + builds = sorted(f"{entry['tag']} {entry['asset']}".strip() for entry in found) + ships = {} + if cfg["list"] == "tags" and steam is not None: + for family in cfg["families"].values(): + app = family.get("depot_app") + if app and app in steam: + ships[app] = steam[app] + old = previous.get(flavor) if isinstance(previous.get(flavor), dict) else {} + if steam is None and isinstance(old.get("steam"), dict): + ships = old["steam"] + mark = {"builds": builds, "rev": cfg["rev"], "series": cfg["series"], "patch": patch_digest(PATCHES / cfg["series"])} + fingerprint = hashlib.sha256(json.dumps(mark, sort_keys=True).encode()).hexdigest() + state[flavor] = {**mark, "fingerprint": fingerprint, "steam": ships} + if old.get("fingerprint") != fingerprint: + changed.append(flavor) + fresh = sorted(set(builds) - set(old.get("builds") or [])) + notes.append(f"{flavor}: {', '.join(fresh) if fresh else 'the patch series or rev changed'}") + for app, ship in sorted(ships.items()): + before = (old.get("steam") or {}).get(app) if isinstance(old.get("steam"), dict) else None + if isinstance(before, dict) and before.get("buildid") != ship.get("buildid"): + notes.append(f"{flavor}: Steam app {app} moved from build {before.get('buildid')} to {ship.get('buildid')}") + if flavor not in changed: + changed.append(flavor) + return state, changed, notes + + +def asset_text(repo: str, asset_id, what: str) -> str: + result = gh("api", "-H", "Accept: application/octet-stream", f"repos/{repo}/releases/assets/{asset_id}") + if result.returncode != 0: + raise PackError(f"cannot download {what}: {result.stderr.strip()}") + return result.stdout + + +def release_index(repo: str, tag: str) -> dict: + release = gh_api(f"repos/{repo}/releases/tags/{quote(tag)}", missing_ok=True) + if release is None: + return {} + for asset in release.get("assets") or []: + if isinstance(asset, dict) and asset.get("name") == "index.json": + text = asset_text(repo, asset["id"], f"the index of {repo}") + try: + return json.loads(text) + except ValueError as e: + raise PackError(f"the index of {repo} is not JSON: {e}") from e + return {} + + +def published(repo: str, flavors: dict, selected: str, index: dict) -> list[dict]: + packs = index.get("packs") if isinstance(index, dict) else None + listed = {pack.get("id") for pack in packs if isinstance(pack, dict)} if isinstance(packs, list) else set() + found = [] + for flavor in chosen_flavors(flavors, selected): + cfg = flavors["flavors"].get(flavor) + tag = f"droiddeck-esync-{flavor}" + release = gh_api(f"repos/{repo}/releases/tags/{quote(tag)}", missing_ok=True) if cfg else None + if not isinstance(release, dict) or type(release.get("id")) is not int: + continue + assets = gh_api(f"repos/{repo}/releases/{release['id']}/assets?per_page=100", paginate=True) + if not isinstance(assets, list): + raise PackError(f"{repo} {tag}: the asset list is not a list") + for asset in assets: + name = asset.get("name") if isinstance(asset, dict) else None + if not isinstance(name, str) or not name.endswith(".json"): + continue + ident = name[:-len(".json")] + rev = PACK_REV.search(ident) + if ident in listed or not rev or int(rev.group(1)) < cfg["rev"]: + continue + try: + entry = json.loads(asset_text(repo, asset.get("id"), f"{tag} {name}")) + except ValueError: + entry = None + if not isinstance(entry, dict) or entry.get("id") != ident or entry.get("flavor") != flavor: + print(f"discover: {tag} {name} is not a pack entry", file=sys.stderr) + continue + print(f"discover: {tag} {name}: published, not in the index yet", file=sys.stderr) + found.append(entry) + return found + + +def parse_time(value) -> datetime | None: + if not isinstance(value, str): + return None + try: + moment = datetime.fromisoformat(value.replace("Z", "+00:00")) + except ValueError: + return None + return moment if moment.tzinfo else moment.replace(tzinfo=timezone.utc) + + +def recent_failures(repo: str, flavors: dict, selected: str, now: datetime) -> frozenset[str]: + found = set() + for flavor in chosen_flavors(flavors, selected): + tag = f"droiddeck-esync-{flavor}" + release = gh_api(f"repos/{repo}/releases/tags/{quote(tag)}", missing_ok=True) if flavor in flavors["flavors"] else None + if not isinstance(release, dict) or type(release.get("id")) is not int: + continue + assets = gh_api(f"repos/{repo}/releases/{release['id']}/assets?per_page=100", paginate=True) + if not isinstance(assets, list): + raise PackError(f"{repo} {tag}: the asset list is not a list") + for asset in assets: + name = asset.get("name") if isinstance(asset, dict) else None + if not isinstance(name, str) or not name.endswith(FAILED): + continue + moment = parse_time(asset.get("updated_at")) or parse_time(asset.get("created_at")) + if moment is not None and now - moment < RETRY_AFTER: + found.add(name) + return frozenset(found) + + +def failed_builds(matrix: dict, directory: Path) -> list[tuple[str, str]]: + rows = matrix.get("include") if isinstance(matrix, dict) else None + if not isinstance(rows, list) or not all(isinstance(row, dict) and isinstance(row.get("key"), str) for row in rows): + raise PackError("the build matrix has no include list") + made = {row["key"] for row in rows if (directory / f"{ARTIFACT_PREFIX}{row['key']}").is_dir()} + digests = {} + return [(row["flavor"], failure_marker(row, digests)) for row in rows if row["key"] not in made] + + +def artifact_packs(matrix: dict, directory: Path) -> list[tuple[str, Path, str]]: + rows = matrix.get("include") if isinstance(matrix, dict) else None + if not isinstance(rows, list) or not all(isinstance(row, dict) and isinstance(row.get("key"), str) for row in rows): + raise PackError("the build matrix has no include list") + builds = {row["key"]: row for row in rows} + found = [] + for folder in sorted(directory.iterdir()) if directory.is_dir() else []: + row = builds.get(folder.name[len(ARTIFACT_PREFIX):]) if folder.name.startswith(ARTIFACT_PREFIX) else None + if row is None or folder.is_symlink() or not folder.is_dir(): + raise PackError(f"{folder.name} is not the artifact of a build in this run") + names = sorted(path.name for path in folder.iterdir()) + entries = [name for name in names if name.endswith(".json")] + if len(entries) != 1 or names != sorted([entries[0], entries[0][:-len(".json")] + ".tzst"]): + raise PackError(f"{folder.name} must hold exactly .json and .tzst") + ident = entries[0][:-len(".json")] + entry_path, archive = folder / entries[0], folder / f"{ident}.tzst" + if entry_path.is_symlink() or archive.is_symlink() or not entry_path.is_file() or not archive.is_file(): + raise PackError(f"{folder.name} holds links or special files") + try: + data = json.loads(entry_path.read_text()) + except ValueError as e: + raise PackError(f"{entry_path}: {e}") from e + entry = check_entry(data, str(entry_path)) + source = data.get("source") if isinstance(data.get("source"), dict) else {} + if entry["id"] != ident or entry["flavor"] != row["flavor"] or entry["rev"] != row["rev"] \ + or source.get("ref") != row["tag"] or source.get("asset", "") != row["asset"]: + raise PackError(f"{entry_path} is not the pack of {row['flavor']} {row['tag']} at rev {row['rev']}") + if archive.stat().st_size != entry["asset"]["size"] or sha256_file(archive) != entry["asset"]["sha256"]: + raise PackError(f"{archive} is not the asset its entry describes") + found.append((row["flavor"], folder, ident)) + return found + + +def emit(entries: list[dict], unindexed: int = 0) -> None: + matrix = json.dumps({"include": entries}, sort_keys=True, separators=(",", ":")) + path = os.environ.get("GITHUB_OUTPUT") + if path: + with open(path, "a") as stream: + stream.write(f"matrix={matrix}\ncount={len(entries)}\nunindexed={unindexed}\n") + print(matrix) + + +def make_variables(text: str) -> tuple[dict[str, str], list[tuple[str, str]]]: + variables = {} + assignments = [] + for raw in text.splitlines(): + match = ASSIGNMENT.fullmatch(raw.split("#", 1)[0].strip()) + if match: + name, value = match.group(1), match.group(2).strip() + variables.setdefault(name, value) + assignments.append((name, value)) + return variables, assignments + + +def expand(value: str, variables: dict[str, str], depth: int = 0) -> str: + if depth > 16: + raise PackError("Makefile variables nest too deeply") + + def lookup(match: re.Match) -> str: + if match.group(1) not in variables: + raise PackError(f"$({match.group(1)}) is not set in the Makefile") + return expand(variables[match.group(1)], variables, depth + 1) + + return REFERENCE.sub(lookup, value) + + +def sdk_image(text: str, variable: str = "STEAMRT_IMAGE", match: str = "arm64-llvm") -> str: + variables, assignments = make_variables(text) + problem = None + for name, value in assignments: + if not name.startswith(variable): + continue + try: + image = expand(value, variables) + except PackError as e: + problem = problem or e + continue + if match not in image: + continue + if not IMAGE.fullmatch(image): + raise PackError(f"{name} = {image!r} is not an image reference") + return image + raise problem or PackError(f"no {variable} assignment names an {match} image") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Find the Proton builds that have no droiddeck-esync pack yet, as a GitHub Actions matrix.") + parser.add_argument("--flavors", default=str(FLAVORS_FILE)) + commands = parser.add_subparsers(dest="command", required=True) + matrix = commands.add_parser("matrix") + matrix.add_argument("--flavor", default="all", choices=("all", *FLAVORS)) + matrix.add_argument("--ref", default="") + matrix.add_argument("--index", default="") + matrix.add_argument("--index-release", default="") + matrix.add_argument("--repo", default=os.environ.get("GITHUB_REPOSITORY", "Droid-Deck/DroidDeck")) + matrix.add_argument("--revoked", default=str(REVOKED)) + image = commands.add_parser("sdk-image") + image.add_argument("makefile") + artifacts = commands.add_parser("packs") + artifacts.add_argument("--matrix", required=True) + artifacts.add_argument("directory") + failures = commands.add_parser("failures") + failures.add_argument("--matrix", required=True) + failures.add_argument("directory") + commands.add_parser("steam-apps") + watching = commands.add_parser("watch") + watching.add_argument("--state", default="") + watching.add_argument("--steam-info", default="") + watching.add_argument("--out", required=True) + args = parser.parse_args(argv) + try: + flavors = load_flavors(Path(args.flavors)) + if args.command == "sdk-image": + settings = flavors["sdk_image"] + print(sdk_image(Path(args.makefile).read_text(), settings["variable"], settings["match"])) + return 0 + if args.command == "packs": + for flavor, folder, ident in artifact_packs(json.loads(args.matrix), Path(args.directory)): + print(f"{flavor}\t{folder}\t{ident}") + return 0 + if args.command == "failures": + for flavor, marker in failed_builds(json.loads(args.matrix), Path(args.directory)): + print(f"{flavor}\t{marker}") + return 0 + if args.command == "steam-apps": + print(" ".join(steam_apps(flavors))) + return 0 + if args.command == "watch": + previous = {} + if args.state and Path(args.state).is_file(): + try: + previous = json.loads(Path(args.state).read_text()) + except ValueError: + print("discover: the previous watch state is not JSON; every flavor counts as changed", file=sys.stderr) + previous = previous.get("flavors", {}) if isinstance(previous, dict) and previous.get("format") == 1 else {} + steam = None + if args.steam_info: + try: + steam = steam_builds(Path(args.steam_info).read_text(errors="replace"), steam_apps(flavors)) + except (OSError, PackError) as e: + print(f"discover: Steam app info unusable ({e}); watching the source tags only", file=sys.stderr) + if steam is not None and len(steam) != len(steam_apps(flavors)): + print(f"discover: Steam app info covers {sorted(steam)} of {steam_apps(flavors)}", file=sys.stderr) + state, changed, notes = watch(flavors, previous, steam) + Path(args.out).write_text(json.dumps({"format": 1, "flavors": state}, indent=1, sort_keys=True) + "\n") + for note in notes: + print(f"discover: {note}", file=sys.stderr) + path = os.environ.get("GITHUB_OUTPUT") + if path: + with open(path, "a") as stream: + stream.write(f"changed={' '.join(changed)}\n") + print(" ".join(changed)) + return 0 + if args.index and args.index_release: + raise PackError("pass --index or --index-release, not both") + failed = frozenset() + unindexed = 0 + if args.index: + path = Path(args.index) + index = json.loads(path.read_text()) if path.exists() else {} + elif args.index_release: + if not REPO.fullmatch(args.repo): + raise PackError(f"{args.repo!r} is not owner/name") + index = release_index(args.repo, args.index_release) + listed = index.get("packs") if isinstance(index, dict) and isinstance(index.get("packs"), list) else [] + waiting = published(args.repo, flavors, args.flavor, index) + unindexed = len(waiting) + index = {"packs": [*listed, *waiting]} + failed = recent_failures(args.repo, flavors, args.flavor, datetime.now(timezone.utc)) + else: + index = {} + revoked = read_revoked(Path(args.revoked)) if args.revoked else set() + emit(discover(flavors, args.flavor, args.ref.strip(), index, revoked, failed), unindexed) + except (PackError, OSError, ValueError) as e: + print(f"discover: {e}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/droiddeck-esync/flavors.json b/tools/droiddeck-esync/flavors.json new file mode 100644 index 000000000..c4673f1f3 --- /dev/null +++ b/tools/droiddeck-esync/flavors.json @@ -0,0 +1,84 @@ +{ + "format": 1, + "sdk_image": { + "file": "Makefile.in", + "variable": "STEAMRT_IMAGE", + "match": "arm64-llvm" + }, + "flavors": { + "valve": { + "repo": "ValveSoftware/Proton", + "list": "tags", + "families": { + "experimental": { + "prefix": "experimental-11.0-", + "tag": "^experimental-11\\.0-\\d{8}[a-z]?$", + "depot_app": "4427310" + }, + "proton": { + "prefix": "proton-11.0-", + "tag": "^proton-11\\.0-\\d+[a-z]?$", + "depot_app": "4628740" + } + }, + "version_token": "{tag}-arm64", + "keep": 2, + "rev": 1, + "series": "valve-11", + "profile": "valve", + "source_match": true, + "build_dir": "/builds/proton/proton/build-dir", + "wine": { + "submodules": [ + "wine" + ], + "clone": "shallow", + "prep": "" + } + }, + "ge": { + "repo": "GloriousEggroll/proton-ge-custom", + "list": "releases", + "tag": "^GE-Proton11-\\d+$", + "assets": [ + "aarch64\\.tar\\.(gz|xz)$", + "arm64\\.tar\\.(gz|xz)$" + ], + "keep": 3, + "rev": 1, + "series": "valve-11", + "profile": "valve", + "source_match": false, + "build_dir": "/home/tcrider/build", + "wine": { + "submodules": [ + "wine", + "wine-staging" + ], + "clone": "treeless", + "prep": "patches/protonprep-valve-staging.sh" + } + }, + "cachyos": { + "repo": "CachyOS/proton-cachyos", + "list": "releases", + "tag": "^cachyos-11\\.0-", + "assets": [ + "arm64\\.tar\\.(gz|xz)$" + ], + "keep": 3, + "rev": 1, + "series": "cachyos-11", + "profile": "cachyos", + "source_match": false, + "build_dir": "/home/runner/work/proton-cachyos/proton-cachyos/build", + "wine": { + "submodules": [ + "wine" + ], + "clone": "shallow", + "prep": "" + } + } + } +} diff --git a/tools/droiddeck-esync/index-key.pub b/tools/droiddeck-esync/index-key.pub new file mode 100644 index 000000000..706ca85b4 --- /dev/null +++ b/tools/droiddeck-esync/index-key.pub @@ -0,0 +1,4 @@ +-----BEGIN PUBLIC KEY----- +MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFyqZQAMh3g3nsEGLU+kBGVPBEkXr +Dv5F3kPtuQ+FV1DkTCGmYUQ1LS9wJtHGt0Ht4wToHGGEp9yzDibYZ4/zhg== +-----END PUBLIC KEY----- diff --git a/tools/droiddeck-esync/make_index.py b/tools/droiddeck-esync/make_index.py new file mode 100755 index 000000000..9802e277b --- /dev/null +++ b/tools/droiddeck-esync/make_index.py @@ -0,0 +1,285 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import argparse +import datetime as dt +import json +import os +import re +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +from make_pack import ( + BINARIES, + EPOCH, + FLAVORS, + HERE, + NTDLL, + PACK_ID, + REVOKED, + SHA256, + WINESERVER, + PackError, + compress, + dump_json, + read_revoked, + sha256_bytes, + sha256_file, + tar_bytes, + write_atomic, +) + +PUBKEY = HERE / "index-key.pub" +SCHEMA = 1 +INDEX_KEYS = ("id", "flavor", "rev", "version", "version_line", "stock", "exports", "source_match", "files", "protocol", "source", "patch") + + +def check_entry(entry, origin: str) -> dict: + def fail(why: str): + raise PackError(f"{origin}: {why}") + + if not isinstance(entry, dict): + fail("is not an object") + ident = entry.get("id") + if not isinstance(ident, str) or not PACK_ID.fullmatch(ident): + fail(f"{ident!r} is not a pack id") + if entry.get("flavor") not in FLAVORS: + fail(f"unknown flavor {entry.get('flavor')!r}") + if type(entry.get("rev")) is not int or entry["rev"] < 1: + fail("rev is not a positive number") + version, line = entry.get("version"), entry.get("version_line") + fields = line.split() if isinstance(line, str) else [] + if not isinstance(version, str) or len(fields) < 2 or fields[1] != version or not EPOCH.fullmatch(fields[0]): + fail("version and version_line disagree") + for field in ("stock", "files"): + value = entry.get(field) + if not isinstance(value, dict) or set(value) != set(BINARIES) or not all(isinstance(v, str) and SHA256.fullmatch(v) for v in value.values()): + fail(f"{field} must give the sha256 of {' and '.join(BINARIES)}") + if not isinstance(entry.get("exports"), str) or not SHA256.fullmatch(entry["exports"]): + fail("exports is not a sha256") + if type(entry.get("source_match")) is not bool: + fail("source_match is not a boolean") + asset = entry.get("asset") + if not isinstance(asset, dict) or not isinstance(asset.get("sha256"), str) or not SHA256.fullmatch(asset["sha256"]) \ + or type(asset.get("size")) is not int or asset["size"] <= 0: + fail("asset needs its sha256 and size") + clean = {key: entry[key] for key in INDEX_KEYS if key in entry} + clean["asset"] = {"sha256": asset["sha256"], "size": asset["size"]} + return clean + + +def load_entries(directory: Path) -> list[dict]: + if not directory.is_dir(): + raise PackError(f"{directory} is not a directory") + entries = [] + for path in sorted(directory.glob("*.json")): + try: + data = json.loads(path.read_text()) + except ValueError as e: + raise PackError(f"{path}: {e}") from e + entry = check_entry(data, str(path)) + if path.stem != entry["id"]: + raise PackError(f"{path} holds the entry of {entry['id']}") + entries.append(entry) + return entries + + +def load_previous(path: Path) -> tuple[int, list[dict], list]: + try: + data = json.loads(path.read_text()) + except (OSError, ValueError) as e: + raise PackError(f"cannot read the previous index {path}: {e}") from e + if not isinstance(data, dict) or data.get("schema") != SCHEMA or type(data.get("generated")) is not int \ + or not isinstance(data.get("packs"), list): + raise PackError(f"{path} is not a schema {SCHEMA} index") + packs = [] + for number, pack in enumerate(data["packs"]): + entry = check_entry(pack, f"{path} pack {number}") + entry["revoked"] = pack.get("revoked") is True + packs.append(entry) + return data["generated"], packs, data["packs"] + + +def epoch(entry: dict) -> int: + return int(entry["version_line"].split()[0]) + + +def rank(entry: dict) -> tuple: + return entry["rev"], epoch(entry), entry["id"] + + +def choose(entries: list[dict], previous: list[dict], revoked: set[str]) -> list[dict]: + known = {entry["id"]: entry for entry in entries} + for entry in previous: + known.setdefault(entry["id"], entry) + active = {} + withdrawn = [] + for entry in known.values(): + if entry["id"] in revoked: + withdrawn.append(dict(entry, revoked=True)) + continue + key = (entry["stock"][NTDLL], entry["stock"][WINESERVER], entry["version"]) + if key not in active or rank(entry) > rank(active[key]): + active[key] = dict(entry, revoked=False) + return sorted([*active.values(), *withdrawn], key=lambda entry: entry["id"]) + + +def build_index(packs: list[dict], base_url: str, generated: int) -> dict: + listed = [] + for pack in packs: + entry = dict(pack) + entry["asset"] = { + "url": f"{base_url}/droiddeck-esync-{pack['flavor']}/{pack['id']}.tzst", + "sha256": pack["asset"]["sha256"], + "size": pack["asset"]["size"], + } + listed.append(entry) + return {"schema": SCHEMA, "generated": generated, "packs": listed} + + +def family(entry: dict) -> tuple[str, str, str]: + token = entry["version"] + major = re.search(r"[0-9]+", token) + return entry["flavor"], re.match(r"[A-Za-z]*", token).group(0).lower(), major.group(0) if major else "" + + +def bundle_choice(packs: list[dict], available: set[str] | None = None) -> list[dict]: + newest = {} + for pack in packs: + if pack["revoked"] or (available is not None and pack["id"] not in available): + continue + key = family(pack) + if key not in newest or (epoch(pack), pack["rev"], pack["id"]) > (epoch(newest[key]), newest[key]["rev"], newest[key]["id"]): + newest[key] = pack + return sorted(newest.values(), key=lambda pack: pack["id"]) + + +def openssl(*args: str) -> subprocess.CompletedProcess: + try: + return subprocess.run(["openssl", *args], capture_output=True, text=True, check=False) + except FileNotFoundError as e: + raise PackError("the openssl command is not installed") from e + + +def verified(index: Path, signature: Path, pubkey: Path) -> bool: + result = openssl("dgst", "-sha256", "-verify", str(pubkey), "-signature", str(signature), str(index)) + return result.returncode == 0 and "Verified OK" in result.stdout + + +def trusted_previous(path: Path, signature: Path | None, pubkey: Path) -> tuple[int, list[dict], list | None]: + if signature is not None and not (path.is_file() and signature.is_file() and verified(path, signature, pubkey)): + print(f"make_index: the previous index {path} does not verify with {signature} and {pubkey}; it is ignored", file=sys.stderr) + return 0, [], None + return load_previous(path) + + +def sign(index: Path, signature: Path, key: Path | None, pubkey: Path) -> None: + with tempfile.TemporaryDirectory() as tmp: + if key is None: + pem = os.environ.get("SYNC_INDEX_KEY", "") + if not pem.strip(): + raise PackError("no signing key: pass --key or set SYNC_INDEX_KEY") + key = Path(tmp) / "key.pem" + with os.fdopen(os.open(key, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w") as stream: + stream.write(pem if pem.endswith("\n") else pem + "\n") + result = openssl("dgst", "-sha256", "-sign", str(key), "-out", str(signature), str(index)) + if result.returncode != 0: + raise PackError(f"openssl could not sign the index: {result.stderr.strip()}") + if not verified(index, signature, pubkey): + raise PackError(f"the signature does not verify with {pubkey}: the signing key is not the one the app trusts") + + +def bundle_name(generated: int) -> str: + return f"bundle-{dt.datetime.fromtimestamp(generated, dt.timezone.utc):%Y%m%d-%H%M%S}.tzst" + + +def bundle(out: Path, packs: list[dict], index: bytes, signature: bytes, packs_dir: Path, generated: int) -> tuple[str, str]: + if not packs_dir.is_dir(): + raise PackError(f"{packs_dir} is not a directory") + available = {path.name[:-len(".tzst")] for path in packs_dir.glob("*.tzst") if path.is_file()} + members = {"index.json": (index, 0o644), "index.json.sig": (signature, 0o644)} + for pack in bundle_choice(packs, available): + path = packs_dir / f"{pack['id']}.tzst" + if path.stat().st_size != pack["asset"]["size"] or sha256_file(path) != pack["asset"]["sha256"]: + raise PackError(f"{path} is not the asset the index lists for {pack['id']}") + members[f"packs/{pack['id']}.tzst"] = (path.read_bytes(), 0o644) + name = bundle_name(generated) + archive = compress(tar_bytes(members)) + write_atomic(out / name, archive) + return name, sha256_bytes(archive) + + +def report(values: dict[str, str]) -> None: + path = os.environ.get("GITHUB_OUTPUT") + if path: + with open(path, "a") as stream: + for key, value in values.items(): + stream.write(f"{key}={value}\n") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Make, sign and bundle the droiddeck-esync pack index from the published pack entries.") + parser.add_argument("--entries", required=True) + parser.add_argument("--base-url", required=True) + parser.add_argument("--key", default="") + parser.add_argument("--pubkey", default=str(PUBKEY)) + parser.add_argument("--out", default="") + parser.add_argument("--bundle-packs-dir", default="") + parser.add_argument("--revoked", default=str(REVOKED)) + parser.add_argument("--previous", default="") + parser.add_argument("--previous-sig", default="") + parser.add_argument("--list-bundle", action="store_true") + args = parser.parse_args(argv) + try: + base_url = args.base_url.rstrip("/") + if not base_url.startswith("https://"): + raise PackError("--base-url must be an https URL") + revoked = read_revoked(Path(args.revoked)) if args.revoked else set() + if args.previous_sig and not args.previous: + raise PackError("--previous-sig needs --previous") + previous_signature = Path(args.previous_sig) if args.previous_sig else None + previous_generated, previous, previous_raw = trusted_previous(Path(args.previous), previous_signature, Path(args.pubkey)) \ + if args.previous else (0, [], None) + packs = choose(load_entries(Path(args.entries)), previous, revoked) + if args.list_bundle: + for pack in bundle_choice(packs): + print(f"{pack['flavor']}\t{pack['id']}") + return 0 + if not args.out: + raise PackError("--out is required") + out = Path(args.out) + out.mkdir(parents=True, exist_ok=True) + generated = max(int(time.time()), previous_generated + 1) + index = build_index(packs, base_url, generated) + data = dump_json(index) + changed = bool(packs) if previous_raw is None else previous_raw != index["packs"] + temp_index = out / f".index.json.tmp-{os.getpid()}" + temp_signature = out / f".index.json.sig.tmp-{os.getpid()}" + try: + temp_index.write_bytes(data) + sign(temp_index, temp_signature, Path(args.key) if args.key else None, Path(args.pubkey)) + signature = temp_signature.read_bytes() + os.replace(temp_index, out / "index.json") + os.replace(temp_signature, out / "index.json.sig") + finally: + temp_index.unlink(missing_ok=True) + temp_signature.unlink(missing_ok=True) + outputs = {"changed": "true" if changed else "false", "generated": str(generated), "index_sha256": sha256_bytes(data)} + live = sum(1 for pack in packs if not pack["revoked"]) + print(f"index.json {outputs['index_sha256']} {live} pack(s), {len(packs) - live} revoked, generated {generated}, {'changed' if changed else 'unchanged'}") + if args.bundle_packs_dir: + name, digest = bundle(out, packs, data, signature, Path(args.bundle_packs_dir), generated) + outputs.update(bundle=name, bundle_sha256=digest) + print(f"{name} {digest}") + report(outputs) + except (PackError, OSError) as e: + print(f"make_index: {e}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/droiddeck-esync/make_pack.py b/tools/droiddeck-esync/make_pack.py new file mode 100755 index 000000000..9f7676b95 --- /dev/null +++ b/tools/droiddeck-esync/make_pack.py @@ -0,0 +1,367 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import argparse +import functools +import hashlib +import importlib.machinery +import importlib.util +import io +import json +import os +import re +import subprocess +import sys +import tarfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +GUEST = ROOT / "tools/linuxfs/overlay/usr/local/bin/droiddeck-esync" +PATCHES = HERE / "patches" +REVOKED = HERE / "revoked.txt" +NTDLL = "files/lib/wine/aarch64-unix/ntdll.so" +WINESERVER = "files/bin-arm64/wineserver" +BINARIES = (NTDLL, WINESERVER) +COPY = ("files/bin-arm64/wine", "files/lib/wine/aarch64-unix/wine", "files/lib/wine/aarch64-unix/wine-preloader") +HEADERS = ("server_protocol.h", "request_handlers.h", "request_trace.h") +FLAVORS = ("valve", "ge", "cachyos") +SOURCE_KEYS = ("repo", "ref", "commit", "wine_commit", "sdk_image") +ENTRY_KEYS = ("id", "flavor", "rev", "version", "version_line", "stock", "exports", "source_match", "files", "protocol", "source", "patch") +PACK_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,127}") +SERIES = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]*") +SHA256 = re.compile(r"[0-9a-f]{64}") +EPOCH = re.compile(r"[0-9]{1,12}") +PROTOCOL = re.compile(rb"^#define SERVER_PROTOCOL_VERSION ([0-9]+)[ \t]*$", re.M) +BUILTIN_ESYNC = b"esync: up and running" +ELF_MAGIC = b"\x7fELF" +GUEST_MODULE = "droiddeck_esync_tools" + + +class PackError(RuntimeError): + pass + + +@functools.lru_cache(maxsize=None) +def guest(): + loader = importlib.machinery.SourceFileLoader(GUEST_MODULE, str(GUEST)) + module = importlib.util.module_from_spec(importlib.util.spec_from_loader(GUEST_MODULE, loader)) + sys.modules[GUEST_MODULE] = module + write_bytecode = sys.dont_write_bytecode + sys.dont_write_bytecode = True + try: + loader.exec_module(module) + except Exception as e: + sys.modules.pop(GUEST_MODULE, None) + raise PackError(f"cannot load {GUEST}: {e}") from e + finally: + sys.dont_write_bytecode = write_bytecode + return module + + +def sha256_bytes(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def sha256_file(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for block in iter(lambda: stream.read(1 << 20), b""): + digest.update(block) + return digest.hexdigest() + + +def slug(text: str) -> str: + return re.sub(r"[^A-Za-z0-9._-]", "-", text) + + +def dump_json(value) -> bytes: + return (json.dumps(value, indent=1, sort_keys=True, ensure_ascii=True) + "\n").encode() + + +def write_atomic(path: Path, data: bytes) -> None: + temp = path.with_name(f".{path.name}.tmp-{os.getpid()}") + try: + with temp.open("wb") as stream: + stream.write(data) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temp, path) + except BaseException: + temp.unlink(missing_ok=True) + raise + + +def read_revoked(path: Path | None) -> set[str]: + if path is None: + return set() + try: + text = path.read_text() + except OSError as e: + raise PackError(f"cannot read the revocation list {path}: {e}") from e + revoked = set() + for raw in text.splitlines(): + line = raw.strip() + if not line or line.startswith("#"): + continue + ident = line.split()[0] + if not PACK_ID.fullmatch(ident): + raise PackError(f"{path}: {ident!r} is not a pack id") + revoked.add(ident) + return revoked + + +def tar_bytes(members: dict[str, tuple[bytes, int]]) -> bytes: + buffer = io.BytesIO() + with tarfile.open(fileobj=buffer, mode="w", format=tarfile.USTAR_FORMAT) as tar: + for name in sorted(members): + data, mode = members[name] + info = tarfile.TarInfo(name) + info.size = len(data) + info.mode = mode + info.mtime = 0 + info.uid = 0 + info.gid = 0 + info.uname = "" + info.gname = "" + tar.addfile(info, io.BytesIO(data)) + return buffer.getvalue() + + +def zstd(data: bytes, *args: str) -> bytes: + try: + result = subprocess.run(["zstd", "-q", *args], input=data, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) + except FileNotFoundError as e: + raise PackError("the zstd command is not installed") from e + if result.returncode != 0: + raise PackError(f"zstd {' '.join(args)} failed: {result.stderr.decode(errors='replace').strip()}") + return result.stdout + + +def compress(data: bytes) -> bytes: + return zstd(data, "-19", "-T1", "-c") + + +def unpack(archive: bytes) -> dict[str, tuple[bytes, int]]: + members = {} + try: + with tarfile.open(fileobj=io.BytesIO(zstd(archive, "-d", "-c")), mode="r:") as tar: + for info in tar: + if not info.isreg() or info.name in members: + raise PackError(f"unexpected entry {info.name!r} in the archive") + members[info.name] = (tar.extractfile(info).read(), info.mode) + except tarfile.TarError as e: + raise PackError(f"the archive does not read back: {e}") from e + return members + + +def binary(base: Path, rel: str) -> Path: + path = base / rel + if path.is_symlink() or not path.is_file(): + raise PackError(f"{path} is not a regular file") + with path.open("rb") as stream: + if stream.read(len(ELF_MAGIC)) != ELF_MAGIC: + raise PackError(f"{path} is not an ELF file") + return path + + +def exports_hash(path: Path) -> str: + try: + value = guest().exports_hash(path) + except PackError: + raise + except Exception as e: + raise PackError(f"{path}: cannot read its exported symbols: {e}") from e + if not isinstance(value, str) or not SHA256.fullmatch(value): + raise PackError(f"{path}: has no exported symbols") + return value + + +def parse_version_line(line: str) -> tuple[int, str]: + fields = line.split() + if "\n" in line or len(fields) < 2 or not EPOCH.fullmatch(fields[0]): + raise PackError(f"version line {line!r} is not ' '") + token = guest().version_token(line) + if not isinstance(token, str) or not token: + raise PackError(f"version line {line!r} has no version token") + return int(fields[0]), token + + +def parse_source(text: str) -> dict[str, str]: + try: + source = json.loads(text) + except ValueError as e: + raise PackError(f"--source-json is not JSON: {e}") from e + if not isinstance(source, dict) or not all(isinstance(key, str) and isinstance(value, str) for key, value in source.items()): + raise PackError("--source-json must be an object of strings") + missing = [key for key in SOURCE_KEYS if not source.get(key)] + if missing: + raise PackError(f"--source-json lacks {', '.join(missing)}") + return source + + +def patch_digest(directory: Path) -> str: + patches = sorted(path for path in directory.glob("*.patch") if path.is_file()) + if not patches: + raise PackError(f"{directory} holds no *.patch files") + digest = hashlib.sha256() + for path in patches: + digest.update(f"{path.name}\0{sha256_file(path)}\n".encode()) + return digest.hexdigest() + + +def protocol_version(control: Path, patched: Path) -> int: + for name in HEADERS: + before, after = control / name, patched / name + if not before.is_file() or not after.is_file(): + raise PackError(f"{name} is missing from the control or the patched build") + if before.read_bytes() != after.read_bytes(): + raise PackError(f"the patch changes {name}: the droiddeck-esync patch must not change the wineserver protocol") + found = PROTOCOL.findall((control / HEADERS[0]).read_bytes()) + if len(found) != 1: + raise PackError("server_protocol.h does not define SERVER_PROTOCOL_VERSION once") + version = int(found[0]) + stamp = control / "protocol" + if stamp.is_file() and stamp.read_text().strip() != str(version): + raise PackError(f"{stamp} disagrees with server_protocol.h ({version})") + return version + + +def make_pack( + flavor: str, + rev: int, + stock_dir: Path | None, + version_line: str, + control: Path, + patched: Path, + source: dict[str, str], + series: str, + out: Path, + source_match: bool, + patch_dir: Path | None = None, + allow_unreproduced: bool = False, +) -> dict: + if flavor not in FLAVORS: + raise PackError(f"unknown flavor {flavor!r}") + if type(rev) is not int or rev < 1: + raise PackError("--rev must be a positive number") + if not SERIES.fullmatch(series): + raise PackError(f"{series!r} is not a patch series name") + if stock_dir is None and not source_match: + raise PackError("without --stock-dir a pack can only match by source: pass --source-match") + if stock_dir is not None and source_match: + raise PackError("a pack made against stock binaries matches them exactly: drop --source-match") + patch_dir = patch_dir or PATCHES / series + if not patch_dir.is_dir(): + raise PackError(f"the patch series {patch_dir} does not exist") + version_line = version_line.strip() + if stock_dir is not None: + stock_line = guest().version_line(stock_dir) + if stock_line and version_line and stock_line != version_line: + raise PackError(f"{stock_dir}/version says {stock_line!r}, not {version_line!r}") + version_line = version_line or stock_line + epoch, token = parse_version_line(version_line) + + stock_paths = {rel: binary(stock_dir or control, rel) for rel in BINARIES} + control_paths = {rel: binary(control, rel) for rel in BINARIES} + patched_paths = {rel: binary(patched, rel) for rel in BINARIES} + if BUILTIN_ESYNC in stock_paths[WINESERVER].read_bytes(): + raise PackError(f"{stock_paths[WINESERVER]} has esync built in and needs no pack") + stock = {rel: sha256_file(path) for rel, path in stock_paths.items()} + built = {rel: sha256_file(path) for rel, path in control_paths.items()} + files = {rel: sha256_file(path) for rel, path in patched_paths.items()} + for rel in BINARIES: + if files[rel] == built[rel]: + raise PackError(f"the patch left {rel} unchanged") + + exports = exports_hash(stock_paths[NTDLL]) + control_exports = exports_hash(control_paths[NTDLL]) + if exports_hash(patched_paths[NTDLL]) != control_exports: + raise PackError("the patch changes the symbols ntdll.so exports") + if exports != control_exports: + raise PackError("the control build of ntdll.so exports other symbols than the stock one: the wine source does not match this release") + protocol = protocol_version(control, patched) + if stock_dir is not None: + for rel in BINARIES: + state = "reproduces" if stock[rel] == built[rel] else "differs from" + print(f"make_pack: the control build {state} the stock {rel}", file=sys.stderr) + unreproduced = [rel for rel in BINARIES if stock[rel] != built[rel]] + if unreproduced and not allow_unreproduced: + raise PackError(f"the control build does not reproduce the stock {', '.join(unreproduced)}: the patched files would differ " + "from the release in more than the patch (check the build directory and the SDK image, or pass --allow-unreproduced)") + + pack_id = f"{flavor}-{slug(token)}-{epoch}-{stock[NTDLL][:12]}-r{rev}" + if not PACK_ID.fullmatch(pack_id): + raise PackError(f"{pack_id!r} is not a valid pack id") + pack = { + "format": 1, + "id": pack_id, + "flavor": flavor, + "rev": rev, + "version": token, + "version_line": version_line, + "stock": stock, + "exports": exports, + "source_match": source_match, + "files": files, + "copy": list(COPY), + "protocol": protocol, + "source": dict(source), + "patch": {"series": series, "sha256": patch_digest(patch_dir)}, + } + members = { + "pack.json": (dump_json(pack), 0o644), + NTDLL: (patched_paths[NTDLL].read_bytes(), 0o755), + WINESERVER: (patched_paths[WINESERVER].read_bytes(), 0o755), + } + archive = compress(tar_bytes(members)) + if unpack(archive) != members: + raise PackError("the archive does not read back as written") + entry = {key: pack[key] for key in ENTRY_KEYS} + entry["asset"] = {"sha256": sha256_bytes(archive), "size": len(archive)} + entry["revoked"] = False + out.mkdir(parents=True, exist_ok=True) + write_atomic(out / f"{pack_id}.tzst", archive) + write_atomic(out / f"{pack_id}.json", dump_json(entry)) + return entry + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description="Make a droiddeck-esync pack from a control and a patched wine build of one Proton release.") + parser.add_argument("--flavor", required=True, choices=FLAVORS) + parser.add_argument("--rev", required=True, type=int) + parser.add_argument("--stock-dir", default="") + parser.add_argument("--version-line", default="") + parser.add_argument("--control", required=True) + parser.add_argument("--patched", required=True) + parser.add_argument("--source-json", required=True) + parser.add_argument("--patch-series", required=True) + parser.add_argument("--patch-dir", default="") + parser.add_argument("--out", required=True) + parser.add_argument("--source-match", action="store_true") + parser.add_argument("--allow-unreproduced", action="store_true") + args = parser.parse_args(argv) + try: + entry = make_pack( + flavor=args.flavor, + rev=args.rev, + stock_dir=Path(args.stock_dir) if args.stock_dir else None, + version_line=args.version_line, + control=Path(args.control), + patched=Path(args.patched), + source=parse_source(args.source_json), + series=args.patch_series, + out=Path(args.out), + source_match=args.source_match, + patch_dir=Path(args.patch_dir) if args.patch_dir else None, + allow_unreproduced=args.allow_unreproduced, + ) + except (PackError, OSError) as e: + print(f"make_pack: {e}", file=sys.stderr) + return 1 + print(entry["id"]) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/droiddeck-esync/patches/cachyos-11/0001-droiddeck-esync.patch b/tools/droiddeck-esync/patches/cachyos-11/0001-droiddeck-esync.patch new file mode 100644 index 000000000..80beac4ac --- /dev/null +++ b/tools/droiddeck-esync/patches/cachyos-11/0001-droiddeck-esync.patch @@ -0,0 +1,2436 @@ +diff --git a/dlls/ntdll/Makefile.in b/dlls/ntdll/Makefile.in +index 38c865b..2a7afcc 100644 +--- a/dlls/ntdll/Makefile.in ++++ b/dlls/ntdll/Makefile.in +@@ -50,6 +50,7 @@ SOURCES = \ + unix/debug.c \ + unix/env.c \ + unix/file.c \ ++ unix/esync.c \ + unix/fsync.c \ + unix/loader.c \ + unix/loadorder.c \ +diff --git a/dlls/ntdll/unix/esync.c b/dlls/ntdll/unix/esync.c +new file mode 100644 +index 0000000..de02a56 +--- /dev/null ++++ b/dlls/ntdll/unix/esync.c +@@ -0,0 +1,1140 @@ ++#if 0 ++#pragma makedep unix ++#endif ++/* droiddeck-esync: the client side of eventfd and shared memory synchronization. ++ * Credit: based on the esync design by Elizabeth Figura for Wine and Proton. */ ++ ++#include "config.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "ntstatus.h" ++#define WIN32_NO_STATUS ++#include "windef.h" ++#include "winternl.h" ++#include "wine/server.h" ++#include "wine/debug.h" ++ ++#include "unix_private.h" ++#include "esync.h" ++ ++WINE_DEFAULT_DEBUG_CHANNEL(esync); ++ ++#define ESYNC_CHUNK_SIZE 0x10000 ++#define ESYNC_SLOT_SIZE 16 ++#define ESYNC_PER_CHUNK (ESYNC_CHUNK_SIZE / ESYNC_SLOT_SIZE) ++#define ESYNC_CHUNKS 8192 ++#define ESYNC_MANUAL 1 ++#define ESYNC_WAITER 0x100 ++#define ESYNC_RELEASING 1 ++#define ESYNC_PULSE_NS 10000000 ++#define ESYNC_STALL_NS 2000000 ++#define ESYNC_TOKEN_MS 100 ++#define ESYNC_TOKEN_TRIES 10 ++#define ESYNC_SPINS 16 ++#define ESYNC_SPIN_NS 50000 ++#define ESYNC_SLICE_NS 1000000000 ++ ++struct esync_slot ++{ ++ union ++ { ++ struct ++ { ++ int state; ++ int aux; ++ }; ++ LONG64 pair; ++ }; ++ int flags; ++ int ref; ++}; ++ ++C_ASSERT( sizeof(struct esync_slot) == ESYNC_SLOT_SIZE ); ++ ++union esync_pair ++{ ++ struct { int state; int aux; } s; ++ LONG64 word; ++}; ++ ++C_ASSERT( offsetof( struct esync_slot, state ) == offsetof( union esync_pair, s.state ) ); ++C_ASSERT( offsetof( struct esync_slot, aux ) == offsetof( union esync_pair, s.aux ) ); ++ ++static BOOL swap_pair( struct esync_slot *slot, int state, int aux, int new_state, int new_aux ) ++{ ++ union esync_pair cur = { .s = { state, aux } }, next = { .s = { new_state, new_aux } }; ++ ++ return __atomic_compare_exchange_n( &slot->pair, &cur.word, next.word, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++} ++ ++enum esync_kind ++{ ++ ESYNC_KIND_SEMAPHORE, ++ ESYNC_KIND_MUTEX, ++ ESYNC_KIND_AUTO, ++ ESYNC_KIND_MANUAL, ++}; ++ ++struct esync_obj ++{ ++ int fd; ++ enum esync_kind kind; ++ struct esync_slot *slot; ++}; ++ ++enum esync_clock ++{ ++ ESYNC_NO_TIMEOUT, ++ ESYNC_MONOTONIC, ++ ESYNC_REALTIME, ++}; ++ ++struct esync_deadline ++{ ++ enum esync_clock clock; ++ ULONGLONG end; ++}; ++ ++struct esync_held ++{ ++ struct esync_slot *slot; ++ int fd; ++}; ++ ++struct esync_waits ++{ ++ struct esync_slot *slots[MAXIMUM_WAIT_OBJECTS]; ++ DWORD count; ++ struct esync_held held[MAXIMUM_WAIT_OBJECTS]; ++ DWORD held_count; ++ DWORD tid; ++ int unix_tid; ++}; ++ ++static int shm_fd = -1; ++static void *chunks[ESYNC_CHUNKS]; ++static sigset_t block_set; ++static __thread struct esync_waits waits; ++static pthread_key_t waits_key; ++static pthread_once_t waits_once = PTHREAD_ONCE_INIT; ++ ++#ifdef __linux__ ++ ++void esync_init( int fd ) ++{ ++ shm_fd = fd; ++ block_set = server_block_set; ++ sigaddset( &block_set, SIGQUIT ); ++ TRACE( "shm fd %d\n", fd ); ++} ++ ++static struct esync_slot *get_slot( unsigned int idx ) ++{ ++ unsigned int chunk = idx / ESYNC_PER_CHUNK; ++ void *base, *addr; ++ ++ if (!idx || chunk >= ESYNC_CHUNKS) return NULL; ++ if (!(base = __atomic_load_n( &chunks[chunk], __ATOMIC_ACQUIRE ))) ++ { ++ addr = mmap( NULL, ESYNC_CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, ++ (off_t)chunk * ESYNC_CHUNK_SIZE ); ++ if (addr == MAP_FAILED) ++ { ++ ERR( "cannot map chunk %u: %s\n", chunk, strerror( errno ) ); ++ return NULL; ++ } ++ if (__atomic_compare_exchange_n( &chunks[chunk], &base, addr, FALSE, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE )) ++ base = addr; ++ else ++ munmap( addr, ESYNC_CHUNK_SIZE ); ++ } ++ return (struct esync_slot *)((char *)base + (idx % ESYNC_PER_CHUNK) * ESYNC_SLOT_SIZE); ++} ++ ++void esync_release( unsigned int shm_idx ) ++{ ++ if (!shm_idx) return; ++ SERVER_START_REQ( fsync_free_shm_idx ) ++ { ++ req->shm_idx = shm_idx; ++ wine_server_call( req ); ++ } ++ SERVER_END_REQ; ++} ++ ++static void block_signals( sigset_t *old ) ++{ ++ pthread_sigmask( SIG_BLOCK, &block_set, old ); ++} ++ ++static void restore_signals( const sigset_t *old ) ++{ ++ pthread_sigmask( SIG_SETMASK, old, NULL ); ++} ++ ++static ssize_t read_fd( int fd, uint64_t *value ) ++{ ++ return syscall( __NR_read, fd, value, sizeof(*value) ); ++} ++ ++static int poll_fds( struct pollfd *fds, nfds_t count, const struct timespec *timeout ) ++{ ++ struct timespec left; ++ ++ if (timeout) left = *timeout; ++ return syscall( __NR_ppoll, fds, count, timeout ? &left : NULL, NULL, 0 ); ++} ++ ++static NTSTATUS wake_fd( int fd, uint64_t count ) ++{ ++ if (syscall( __NR_write, fd, &count, sizeof(count) ) == -1) return errno_to_status( errno ); ++ return STATUS_SUCCESS; ++} ++ ++static BOOL drain_fd( int fd ) ++{ ++ uint64_t value; ++ ++ if (read_fd( fd, &value ) == sizeof(value)) return TRUE; ++ if (errno != EAGAIN) WARN( "read fd %d: %s\n", fd, strerror( errno ) ); ++ return FALSE; ++} ++ ++static BOOL fd_readable( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ struct timespec now = { 0, 0 }; ++ int ret; ++ ++ while ((ret = poll_fds( &pfd, 1, &now )) < 0 && errno == EINTR); ++ return ret > 0 && (pfd.revents & POLLIN); ++} ++ ++static BOOL consume_token( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ struct timespec pause = { 0, ESYNC_TOKEN_MS * 1000000 }; ++ unsigned int tries = 0; ++ uint64_t value; ++ ++ while (read_fd( fd, &value ) != sizeof(value)) ++ { ++ if (errno != EAGAIN && errno != EINTR) ++ { ++ WARN( "read fd %d: %s\n", fd, strerror( errno ) ); ++ return TRUE; ++ } ++ if (tries == ESYNC_TOKEN_TRIES) ++ { ++ WARN( "no token on fd %d, the count it was released with is lost\n", fd ); ++ return FALSE; ++ } ++ if (!poll_fds( &pfd, 1, &pause )) tries++; ++ } ++ return TRUE; ++} ++ ++static int current_unix_tid(void) ++{ ++ if (!waits.unix_tid) waits.unix_tid = syscall( __NR_gettid ); ++ return waits.unix_tid; ++} ++ ++static void repair_slot( struct esync_slot *slot, int fd ) ++{ ++ BOOL readable = fd_readable( fd ); ++ ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) ++ { ++ if (!readable) wake_fd( fd, 1 ); ++ } ++ else if (readable) drain_fd( fd ); ++} ++ ++static void lock_slot( struct esync_slot *slot, int fd, int self ) ++{ ++ unsigned int spins = 0; ++ int owner; ++ ++ for (;;) ++ { ++ owner = 0; ++ if (__atomic_compare_exchange_n( &slot->aux, &owner, self, FALSE, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) return; ++ if (++spins < 128) ++ { ++ YieldProcessor(); ++ continue; ++ } ++ if (!(spins & 1023) && owner > 0 && kill( owner, 0 ) == -1 && errno == ESRCH && ++ __atomic_compare_exchange_n( &slot->aux, &owner, self, FALSE, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) ++ { ++ WARN( "lock owner %d is gone, taking over\n", owner ); ++ repair_slot( slot, fd ); ++ return; ++ } ++ sched_yield(); ++ } ++} ++ ++static void unlock_slot( struct esync_slot *slot, int self ) ++{ ++ __atomic_compare_exchange_n( &slot->aux, &self, 0, FALSE, __ATOMIC_RELEASE, __ATOMIC_RELAXED ); ++} ++ ++static void drop_waits( void *arg ) ++{ ++ struct esync_waits *list = arg; ++ struct esync_held *held; ++ ++ while (list->count) __atomic_sub_fetch( &list->slots[--list->count]->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ while (list->held_count) ++ { ++ held = &list->held[--list->held_count]; ++ if (swap_pair( held->slot, list->tid, __atomic_load_n( &held->slot->aux, __ATOMIC_SEQ_CST ), ~0, 0 )) ++ wake_fd( held->fd, 1 ); ++ } ++} ++ ++static void init_waits_key(void) ++{ ++ pthread_key_create( &waits_key, drop_waits ); ++} ++ ++static void arm_waits(void) ++{ ++ pthread_once( &waits_once, init_waits_key ); ++ if (!pthread_getspecific( waits_key )) pthread_setspecific( waits_key, &waits ); ++} ++ ++static void add_waits( DWORD count, const struct esync_obj *objs ) ++{ ++ DWORD i; ++ ++ arm_waits(); ++ for (i = 0; i < count && waits.count < MAXIMUM_WAIT_OBJECTS; i++) ++ { ++ if (objs[i].kind != ESYNC_KIND_AUTO && objs[i].kind != ESYNC_KIND_MANUAL) continue; ++ waits.slots[waits.count] = objs[i].slot; ++ __atomic_add_fetch( &objs[i].slot->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ __atomic_store_n( &waits.count, waits.count + 1, __ATOMIC_RELAXED ); ++ } ++} ++ ++static void remove_waits( DWORD base ) ++{ ++ struct esync_slot *slot; ++ ++ while (waits.count > base) ++ { ++ slot = waits.slots[waits.count - 1]; ++ __atomic_store_n( &waits.count, waits.count - 1, __ATOMIC_RELAXED ); ++ __atomic_sub_fetch( &slot->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ } ++} ++ ++static void hold_mutex( struct esync_obj *obj, DWORD tid ) ++{ ++ arm_waits(); ++ waits.tid = tid; ++ waits.held[waits.held_count].slot = obj->slot; ++ waits.held[waits.held_count].fd = obj->fd; ++ waits.held_count++; ++} ++ ++static ULONGLONG monotonic_ns(void) ++{ ++ struct timespec ts; ++ ++ clock_gettime( CLOCK_MONOTONIC, &ts ); ++ return (ULONGLONG)ts.tv_sec * 1000000000 + ts.tv_nsec; ++} ++ ++NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret = STATUS_SUCCESS; ++ sigset_t old; ++ int current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ block_signals( &old ); ++ current = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ do ++ { ++ if ((ULONGLONG)(unsigned int)current + count > (unsigned int)slot->aux) ++ { ++ restore_signals( &old ); ++ return STATUS_SEMAPHORE_LIMIT_EXCEEDED; ++ } ++ } ++ while (!__atomic_compare_exchange_n( &slot->state, ¤t, (int)((unsigned int)current + count), ++ FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (count) ret = wake_fd( fd, count ); ++ restore_signals( &old ); ++ if (!ret && prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ info->CurrentCount = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->MaximumCount = slot->aux; ++ return STATUS_SUCCESS; ++} ++ ++static NTSTATUS set_manual( struct esync_slot *slot, int fd, LONG *prev ) ++{ ++ int self = current_unix_tid(); ++ NTSTATUS ret = STATUS_SUCCESS; ++ sigset_t old; ++ LONG current; ++ ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ) == 1) ++ { ++ if (prev) *prev = 1; ++ return STATUS_SUCCESS; ++ } ++ block_signals( &old ); ++ lock_slot( slot, fd, self ); ++ if (!(current = __atomic_exchange_n( &slot->state, 1, __ATOMIC_SEQ_CST ))) ret = wake_fd( fd, 1 ); ++ unlock_slot( slot, self ); ++ restore_signals( &old ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++static LONG reset_manual( struct esync_slot *slot, int fd ) ++{ ++ int self = current_unix_tid(); ++ sigset_t old; ++ LONG current; ++ ++ if (!__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) return 0; ++ block_signals( &old ); ++ lock_slot( slot, fd, self ); ++ if ((current = __atomic_exchange_n( &slot->state, 0, __ATOMIC_SEQ_CST ))) drain_fd( fd ); ++ unlock_slot( slot, self ); ++ restore_signals( &old ); ++ return current; ++} ++ ++NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret; ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) return set_manual( slot, fd, prev ); ++ current = prev ? fd_readable( fd ) : 0; ++ ret = wake_fd( fd, 1 ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) current = reset_manual( slot, fd ); ++ else ++ { ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ current = drain_fd( fd ); ++ } ++ if (prev) *prev = current; ++ return STATUS_SUCCESS; ++} ++ ++static void pulse_wait( struct esync_slot *slot, int fd, BOOL manual ) ++{ ++ ULONGLONG now = monotonic_ns(), deadline = now + ESYNC_PULSE_NS, stall = now + ESYNC_STALL_NS; ++ int waiters = __atomic_load_n( &slot->flags, __ATOMIC_SEQ_CST ) / ESYNC_WAITER, left; ++ ++ while (waiters > 0 && now < deadline && now < stall) ++ { ++ if (!manual && !fd_readable( fd )) break; ++ sched_yield(); ++ left = __atomic_load_n( &slot->flags, __ATOMIC_SEQ_CST ) / ESYNC_WAITER; ++ now = monotonic_ns(); ++ if (left < waiters) stall = now + ESYNC_STALL_NS; ++ waiters = left; ++ } ++} ++ ++NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret; ++ sigset_t old; ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ block_signals( &old ); ++ if (slot->flags & ESYNC_MANUAL) ++ { ++ if (!(ret = set_manual( slot, fd, ¤t ))) pulse_wait( slot, fd, TRUE ); ++ reset_manual( slot, fd ); ++ } ++ else ++ { ++ current = prev ? fd_readable( fd ) : 0; ++ ret = wake_fd( fd, 1 ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ if (!ret) pulse_wait( slot, fd, FALSE ); ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ drain_fd( fd ); ++ } ++ restore_signals( &old ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) ++ { ++ info->EventState = !!__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->EventType = NotificationEvent; ++ } ++ else ++ { ++ info->EventState = fd_readable( fd ); ++ info->EventType = SynchronizationEvent; ++ } ++ return STATUS_SUCCESS; ++} ++ ++NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ DWORD tid = GetCurrentThreadId(); ++ NTSTATUS ret = STATUS_SUCCESS; ++ int owner = tid | ESYNC_RELEASING, count; ++ sigset_t old; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if ((DWORD)__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ) != tid) return STATUS_MUTANT_NOT_OWNED; ++ block_signals( &old ); ++ count = __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ if (count > 1) ++ { ++ if (!swap_pair( slot, tid, count, tid, count - 1 )) ret = STATUS_MUTANT_NOT_OWNED; ++ } ++ else if (!swap_pair( slot, tid, count, tid | ESYNC_RELEASING, 0 )) ret = STATUS_MUTANT_NOT_OWNED; ++ else if ((ret = wake_fd( fd, 1 ))) swap_pair( slot, tid | ESYNC_RELEASING, 0, tid, count ); ++ else __atomic_compare_exchange_n( &slot->state, &owner, 0, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++ restore_signals( &old ); ++ if (!ret && prev) *prev = 1 - count; ++ return ret; ++} ++ ++NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ DWORD owner; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ owner = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->CurrentCount = 1 - __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ info->OwnedByCaller = owner == GetCurrentThreadId(); ++ info->AbandonedState = owner == ~0u; ++ return STATUS_SUCCESS; ++} ++ ++static void get_deadline( const LARGE_INTEGER *timeout, struct esync_deadline *end ) ++{ ++ ULONGLONG now, rel; ++ ++ end->clock = ESYNC_NO_TIMEOUT; ++ if (!timeout || timeout->QuadPart == TIMEOUT_INFINITE) return; ++ if (timeout->QuadPart > 0) ++ { ++ end->clock = ESYNC_REALTIME; ++ end->end = timeout->QuadPart; ++ return; ++ } ++ now = monotonic_ns(); ++ rel = -(ULONGLONG)timeout->QuadPart; ++ if (rel > (ULLONG_MAX - now) / 100) return; ++ end->clock = ESYNC_MONOTONIC; ++ end->end = now + rel * 100; ++} ++ ++static BOOL time_left( const struct esync_deadline *end, struct timespec *ts, BOOL *sliced ) ++{ ++ ULONGLONG left = 0, now; ++ struct timespec rt; ++ LONGLONG ticks; ++ ++ *sliced = FALSE; ++ switch (end->clock) ++ { ++ case ESYNC_NO_TIMEOUT: ++ return FALSE; ++ case ESYNC_MONOTONIC: ++ now = monotonic_ns(); ++ if (now < end->end) left = end->end - now; ++ break; ++ case ESYNC_REALTIME: ++ clock_gettime( CLOCK_REALTIME, &rt ); ++ ticks = ticks_from_time_t( rt.tv_sec ) + rt.tv_nsec / 100; ++ if ((LONGLONG)end->end > ticks) ++ { ++ left = (ULONGLONG)((LONGLONG)end->end - ticks); ++ left = left > ESYNC_SLICE_NS / 100 ? ESYNC_SLICE_NS : left * 100; ++ } ++ if (left == ESYNC_SLICE_NS) *sliced = TRUE; ++ break; ++ } ++ ts->tv_sec = left / 1000000000; ++ ts->tv_nsec = left % 1000000000; ++ return TRUE; ++} ++ ++static BOOL expired( const struct esync_deadline *end ) ++{ ++ struct timespec ts; ++ BOOL sliced; ++ ++ return time_left( end, &ts, &sliced ) && !sliced && !ts.tv_sec && !ts.tv_nsec; ++} ++ ++static int do_poll( struct pollfd *fds, nfds_t count, const struct esync_deadline *end ) ++{ ++ struct timespec ts; ++ BOOL sliced; ++ int ret; ++ ++ for (;;) ++ { ++ ret = poll_fds( fds, count, time_left( end, &ts, &sliced ) ? &ts : NULL ); ++ if (ret < 0 && errno == EINTR) continue; ++ if (!ret && sliced) continue; ++ return ret; ++ } ++} ++ ++static NTSTATUS user_apc(void) ++{ ++ static const LARGE_INTEGER zero; ++ ++ return server_wait( NULL, 0, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE, &zero ); ++} ++ ++static BOOL take_semaphore( struct esync_obj *obj ) ++{ ++ int current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ sigset_t old; ++ BOOL ret; ++ ++ if (current <= 0) return FALSE; ++ block_signals( &old ); ++ do ++ { ++ if (current <= 0) ++ { ++ restore_signals( &old ); ++ return FALSE; ++ } ++ } ++ while (!__atomic_compare_exchange_n( &obj->slot->state, ¤t, current - 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ ret = consume_token( obj->fd ); ++ restore_signals( &old ); ++ return ret; ++} ++ ++static void back_off( unsigned int *spins ) ++{ ++ struct timespec pause = { 0, 1000000 }; ++ ++ if (++*spins <= ESYNC_SPINS) sched_yield(); ++ else nanosleep( &pause, NULL ); ++} ++ ++static BOOL mutex_free( int state ) ++{ ++ return !state || (state & ESYNC_RELEASING); ++} ++ ++static BOOL claim_mutex( struct esync_obj *obj, DWORD tid, int *prev ) ++{ ++ int owner = tid; ++ uint64_t value; ++ ++ if (!mutex_free( *prev )) return FALSE; ++ if (!__atomic_compare_exchange_n( &obj->slot->state, prev, tid, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ return FALSE; ++ if (read_fd( obj->fd, &value ) != sizeof(value)) ++ { ++ __atomic_compare_exchange_n( &obj->slot->state, &owner, *prev, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++ return FALSE; ++ } ++ if ((DWORD)__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ) != tid) ++ { ++ wake_fd( obj->fd, 1 ); ++ return FALSE; ++ } ++ return TRUE; ++} ++ ++static void unclaim_mutex( struct esync_obj *obj, DWORD tid, int prev ) ++{ ++ int owner = tid; ++ ++ if ((DWORD)prev == ~0u) ++ { ++ if (__atomic_compare_exchange_n( &obj->slot->state, &owner, ~0, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ wake_fd( obj->fd, 1 ); ++ return; ++ } ++ if (!__atomic_compare_exchange_n( &obj->slot->state, &owner, tid | ESYNC_RELEASING, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ return; ++ wake_fd( obj->fd, 1 ); ++ owner = tid | ESYNC_RELEASING; ++ __atomic_compare_exchange_n( &obj->slot->state, &owner, prev, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++} ++ ++static BOOL take_mutex( struct esync_obj *obj, DWORD tid, BOOL *abandoned ) ++{ ++ int prev = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ BOOL ret = FALSE; ++ sigset_t old; ++ ++ if (!mutex_free( prev )) return FALSE; ++ block_signals( &old ); ++ if (claim_mutex( obj, tid, &prev )) ++ { ++ if ((DWORD)prev == ~0u) *abandoned = TRUE; ++ hold_mutex( obj, tid ); ++ swap_pair( obj->slot, tid, __atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ), tid, 1 ); ++ ret = TRUE; ++ } ++ restore_signals( &old ); ++ waits.held_count = 0; ++ return ret; ++} ++ ++static BOOL take_auto( struct esync_obj *obj ) ++{ ++ uint64_t value; ++ ++ __atomic_store_n( &obj->slot->state, 0, __ATOMIC_SEQ_CST ); ++ return read_fd( obj->fd, &value ) == sizeof(value); ++} ++ ++static NTSTATUS recurse_mutex( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ int count = __atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ); ++ ++ if (count == INT_MAX) return STATUS_MUTANT_LIMIT_EXCEEDED; ++ swap_pair( obj->slot, tid, count, tid, count + 1 ); ++ return i; ++} ++ ++static NTSTATUS try_now( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ int state = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ BOOL abandoned = FALSE; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ if ((DWORD)state == tid) return recurse_mutex( obj, i, tid ); ++ if (!mutex_free( state )) break; ++ if (take_mutex( obj, tid, &abandoned )) return abandoned ? STATUS_ABANDONED_WAIT_0 + i : i; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ if (take_semaphore( obj )) return i; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (state && take_auto( obj )) return i; ++ break; ++ case ESYNC_KIND_MANUAL: ++ if (state) return i; ++ break; ++ } ++ return STATUS_PENDING; ++} ++ ++static NTSTATUS try_ready( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ BOOL abandoned = FALSE; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ if (take_mutex( obj, tid, &abandoned )) return abandoned ? STATUS_ABANDONED_WAIT_0 + i : i; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ if (take_semaphore( obj )) return i; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (take_auto( obj )) return i; ++ break; ++ case ESYNC_KIND_MANUAL: ++ return i; ++ } ++ return STATUS_PENDING; ++} ++ ++static NTSTATUS wait_any( DWORD count, struct esync_obj *objs, int alert_fd, const struct esync_deadline *end, BOOL track ) ++{ ++ struct pollfd fds[MAXIMUM_WAIT_OBJECTS + 1]; ++ DWORD tid = GetCurrentThreadId(), i; ++ unsigned int spins = 0; ++ ULONGLONG polled; ++ NTSTATUS status; ++ BOOL busy; ++ nfds_t nfds; ++ int ret; ++ ++ for (;;) ++ { ++ for (i = 0; i < count; i++) ++ if ((status = try_now( &objs[i], i, tid )) != STATUS_PENDING) return status; ++ if (track) ++ { ++ add_waits( count, objs ); ++ track = FALSE; ++ continue; ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ fds[i].fd = objs[i].fd; ++ fds[i].events = POLLIN; ++ fds[i].revents = 0; ++ } ++ nfds = count; ++ if (alert_fd >= 0) ++ { ++ fds[nfds].fd = alert_fd; ++ fds[nfds].events = POLLIN; ++ fds[nfds].revents = 0; ++ nfds++; ++ } ++ ++ polled = monotonic_ns(); ++ if ((ret = do_poll( fds, nfds, end )) < 0) return errno_to_status( errno ); ++ if (!ret) return STATUS_TIMEOUT; ++ ++ busy = FALSE; ++ for (i = 0; i < count; i++) ++ { ++ if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) return STATUS_INVALID_HANDLE; ++ if (!(fds[i].revents & POLLIN)) continue; ++ if ((status = try_ready( &objs[i], i, tid )) != STATUS_PENDING) return status; ++ busy = TRUE; ++ } ++ if (alert_fd >= 0 && (fds[count].revents & POLLIN) && (status = user_apc()) != STATUS_TIMEOUT) ++ return status; ++ if (expired( end )) return STATUS_TIMEOUT; ++ if (busy && monotonic_ns() - polled < ESYNC_SPIN_NS) back_off( &spins ); ++ else spins = 0; ++ } ++} ++ ++static BOOL owned_mutex( struct esync_obj *obj, DWORD tid ) ++{ ++ return obj->kind == ESYNC_KIND_MUTEX && (DWORD)__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ) == tid; ++} ++ ++static BOOL ready_now( struct esync_obj *obj, DWORD tid ) ++{ ++ int state = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ return (DWORD)state == tid || (mutex_free( state ) && fd_readable( obj->fd )); ++ case ESYNC_KIND_SEMAPHORE: ++ return state > 0; ++ case ESYNC_KIND_AUTO: ++ return fd_readable( obj->fd ); ++ case ESYNC_KIND_MANUAL: ++ return state != 0; ++ } ++ return FALSE; ++} ++ ++static NTSTATUS grab_all( DWORD count, struct esync_obj *objs, DWORD tid ) ++{ ++ BOOL grabbed[MAXIMUM_WAIT_OBJECTS], owned[MAXIMUM_WAIT_OBJECTS], abandoned = FALSE; ++ DWORD order[MAXIMUM_WAIT_OBJECTS], n = 0, i, j; ++ int current, prev[MAXIMUM_WAIT_OBJECTS]; ++ NTSTATUS status = STATUS_PENDING; ++ sigset_t old; ++ ++ for (i = 0; i < count; i++) ++ if (!ready_now( &objs[i], tid )) return STATUS_PENDING; ++ for (i = 0; i < count; i++) ++ { ++ grabbed[i] = owned[i] = FALSE; ++ if (objs[i].kind != ESYNC_KIND_AUTO) order[n++] = i; ++ } ++ for (i = 0; i < count; i++) ++ if (objs[i].kind == ESYNC_KIND_AUTO) order[n++] = i; ++ ++ block_signals( &old ); ++ for (j = 0; j < count; j++) ++ { ++ struct esync_obj *obj = &objs[i = order[j]]; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ if ((DWORD)current == tid) ++ { ++ if (__atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ) == INT_MAX) ++ { ++ status = STATUS_MUTANT_LIMIT_EXCEEDED; ++ goto rollback; ++ } ++ owned[i] = TRUE; ++ break; ++ } ++ prev[i] = current; ++ if (!claim_mutex( obj, tid, &prev[i] )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ do if (current <= 0) goto rollback; ++ while (!__atomic_compare_exchange_n( &obj->slot->state, ¤t, current - 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (!consume_token( obj->fd )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (!take_auto( obj )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_MANUAL: ++ if (!__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST )) goto rollback; ++ break; ++ } ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ struct esync_slot *slot = objs[i].slot; ++ ++ if (owned[i]) ++ { ++ current = __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ swap_pair( slot, tid, current, tid, current + 1 ); ++ } ++ else if (grabbed[i] && objs[i].kind == ESYNC_KIND_MUTEX) ++ { ++ if ((DWORD)prev[i] == ~0u) abandoned = TRUE; ++ hold_mutex( &objs[i], tid ); ++ swap_pair( slot, tid, __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ), tid, 1 ); ++ } ++ } ++ restore_signals( &old ); ++ waits.held_count = 0; ++ return abandoned ? STATUS_ABANDONED : STATUS_SUCCESS; ++ ++rollback: ++ while (j--) ++ { ++ i = order[j]; ++ if (!grabbed[i]) continue; ++ switch (objs[i].kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ unclaim_mutex( &objs[i], tid, prev[i] ); ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ current = __atomic_load_n( &objs[i].slot->state, __ATOMIC_SEQ_CST ); ++ do if (current >= objs[i].slot->aux) break; ++ while (!__atomic_compare_exchange_n( &objs[i].slot->state, ¤t, current + 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (current < objs[i].slot->aux) wake_fd( objs[i].fd, 1 ); ++ break; ++ case ESYNC_KIND_AUTO: ++ wake_fd( objs[i].fd, 1 ); ++ __atomic_store_n( &objs[i].slot->state, 1, __ATOMIC_SEQ_CST ); ++ break; ++ case ESYNC_KIND_MANUAL: ++ break; ++ } ++ } ++ restore_signals( &old ); ++ return status; ++} ++ ++static NTSTATUS wait_all( DWORD count, struct esync_obj *objs, int alert_fd, const struct esync_deadline *end, BOOL track ) ++{ ++ struct pollfd fds[2]; ++ DWORD tid = GetCurrentThreadId(), i, j; ++ NTSTATUS status; ++ nfds_t nfds; ++ int ret; ++ ++ for (i = 0; i < count; i++) ++ for (j = i + 1; j < count; j++) ++ if (objs[i].slot == objs[j].slot) return STATUS_INVALID_PARAMETER; ++ ++ for (;;) ++ { ++ if ((status = grab_all( count, objs, tid )) != STATUS_PENDING) return status; ++ if (track) ++ { ++ add_waits( count, objs ); ++ track = FALSE; ++ continue; ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ if (owned_mutex( &objs[i], tid )) continue; ++ fds[0].fd = objs[i].fd; ++ fds[0].events = POLLIN; ++ fds[0].revents = 0; ++ nfds = 1; ++ if (alert_fd >= 0) ++ { ++ fds[1].fd = alert_fd; ++ fds[1].events = POLLIN; ++ fds[1].revents = 0; ++ nfds = 2; ++ } ++ if ((ret = do_poll( fds, nfds, end )) < 0) return errno_to_status( errno ); ++ if (!ret) return STATUS_TIMEOUT; ++ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return STATUS_INVALID_HANDLE; ++ if (!(fds[0].revents & POLLIN) && nfds == 2 && (fds[1].revents & POLLIN)) ++ { ++ if ((status = grab_all( count, objs, tid )) != STATUS_PENDING) return status; ++ if ((status = user_apc()) != STATUS_TIMEOUT) return status; ++ break; ++ } ++ } ++ if (i == count) ++ { ++ if (expired( end )) return STATUS_TIMEOUT; ++ sched_yield(); ++ } ++ } ++} ++ ++NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ) ++{ ++ struct esync_obj objs[MAXIMUM_WAIT_OBJECTS]; ++ struct esync_deadline end; ++ DWORD i, base = waits.count; ++ BOOL track = FALSE; ++ NTSTATUS ret; ++ ++ assert( count <= MAXIMUM_WAIT_OBJECTS ); ++ for (i = 0; i < count; i++) ++ { ++ if (!(objs[i].slot = get_slot( shm_idxs[i] ))) return STATUS_INVALID_HANDLE; ++ objs[i].fd = fds[i]; ++ switch (types[i]) ++ { ++ case INPROC_SYNC_SEMAPHORE: ++ objs[i].kind = ESYNC_KIND_SEMAPHORE; ++ break; ++ case INPROC_SYNC_MUTEX: ++ objs[i].kind = ESYNC_KIND_MUTEX; ++ break; ++ default: ++ objs[i].kind = (objs[i].slot->flags & ESYNC_MANUAL) ? ESYNC_KIND_MANUAL : ESYNC_KIND_AUTO; ++ track = TRUE; ++ break; ++ } ++ } ++ ++ get_deadline( timeout, &end ); ++ if (timeout && !timeout->QuadPart) track = FALSE; ++ if (type != WaitAll || count <= 1) ret = wait_any( count, objs, alert_fd, &end, track ); ++ else ret = wait_all( count, objs, alert_fd, &end, track ); ++ remove_waits( base ); ++ return ret; ++} ++ ++#else ++ ++void esync_init( int fd ) ++{ ++} ++ ++void esync_release( unsigned int shm_idx ) ++{ ++} ++ ++NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++#endif +diff --git a/dlls/ntdll/unix/esync.h b/dlls/ntdll/unix/esync.h +new file mode 100644 +index 0000000..afa19fc +--- /dev/null ++++ b/dlls/ntdll/unix/esync.h +@@ -0,0 +1,20 @@ ++#define ESYNC_USED_BY_SERVER 0x7eadeeef ++#define ESYNC_SHM_TOKEN 0x7eadeef0 ++ ++static inline int do_esync(void) ++{ ++ return inproc_device_fd == ESYNC_USED_BY_SERVER; ++} ++ ++extern void esync_init( int shm_fd ); ++extern void esync_release( unsigned int shm_idx ); ++extern NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ); ++extern NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ); +diff --git a/dlls/ntdll/unix/server.c b/dlls/ntdll/unix/server.c +index 112157b..864c2f9 100644 +--- a/dlls/ntdll/unix/server.c ++++ b/dlls/ntdll/unix/server.c +@@ -81,6 +81,7 @@ + #include "unix_private.h" + #include "ddk/wdm.h" + ++#include "esync.h" + #include "fsync.h" + + WINE_DEFAULT_DEBUG_CHANNEL(server); +@@ -1703,6 +1704,14 @@ size_t server_init_process(void) + inproc_device_fd = FSYNC_USED_BY_SERVER; + fsync_init( pid ); + } ++ else if (reply->inproc_device == ESYNC_USED_BY_SERVER) ++ { ++ int shm_fd = wine_server_receive_fd( &handle ); ++ assert( handle == ESYNC_SHM_TOKEN ); ++ if (shm_fd < 0) fatal_error( "droiddeck-esync: no shared memory from wineserver\n" ); ++ inproc_device_fd = ESYNC_USED_BY_SERVER; ++ esync_init( shm_fd ); ++ } + else if (reply->inproc_device) + { + inproc_device_fd = wine_server_receive_fd( &handle ); +diff --git a/dlls/ntdll/unix/sync.c b/dlls/ntdll/unix/sync.c +index 67ab945..f44a526 100644 +--- a/dlls/ntdll/unix/sync.c ++++ b/dlls/ntdll/unix/sync.c +@@ -72,6 +72,7 @@ + #include "wine/debug.h" + #include "unix_private.h" + ++#include "esync.h" + #include "fsync.h" + + WINE_DEFAULT_DEBUG_CHANNEL(sync); +@@ -542,9 +543,10 @@ struct inproc_sync + unsigned int access; /* handle access rights */ + unsigned short type; /* enum inproc_sync_type as short to save space */ + unsigned short closed; /* fd has been closed but sync is still referenced */ ++ unsigned int shm_idx; + }; + +-#define INPROC_SYNC_CACHE_BLOCK_SIZE (65536 / sizeof(struct inproc_sync)) ++#define INPROC_SYNC_CACHE_BLOCK_SIZE 4096 + #define INPROC_SYNC_CACHE_ENTRIES 128 + + static struct inproc_sync *inproc_sync_cache[INPROC_SYNC_CACHE_ENTRIES]; +@@ -582,7 +584,8 @@ static struct inproc_sync *cache_inproc_sync( HANDLE handle, struct inproc_sync + if (!entry) inproc_sync_cache[0] = inproc_sync_cache_initial_block; + else + { +- static const size_t size = INPROC_SYNC_CACHE_BLOCK_SIZE * sizeof(struct inproc_sync); ++ size_t page = sysconf( _SC_PAGESIZE ); ++ size_t size = (INPROC_SYNC_CACHE_BLOCK_SIZE * sizeof(struct inproc_sync) + page - 1) & ~(page - 1); + void *ptr = anon_mmap_alloc( size, PROT_READ | PROT_WRITE ); + if (ptr == MAP_FAILED) return sync; + inproc_sync_cache[entry] = ptr; +@@ -604,6 +607,7 @@ static struct inproc_sync *cache_inproc_sync( HANDLE handle, struct inproc_sync + cache->access = sync->access; + cache->type = sync->type; + cache->closed = sync->closed; ++ cache->shm_idx = sync->shm_idx; + /* Make sure we set the other members before the refcount; this store needs + * release semantics [paired with the load in get_cached_inproc_sync()]. + * Set the refcount to 2 (one for the handle, one for the caller). */ +@@ -633,10 +637,13 @@ static void release_inproc_sync( struct inproc_sync *sync ) + /* save the fd now; as soon as the refcount hits 0 we cannot + * access the cache anymore */ + int fd = sync->fd; ++ unsigned int shm_idx = sync->shm_idx; + LONG ref = InterlockedDecrement( &sync->refcount ); + + assert( ref >= 0 ); +- if (!ref) close( fd ); ++ if (ref) return; ++ if (fd >= 0) close( fd ); ++ if (shm_idx) esync_release( shm_idx ); + } + + static struct inproc_sync *get_cached_inproc_sync( HANDLE handle ) +@@ -682,10 +689,19 @@ static NTSTATUS get_server_inproc_sync( HANDLE handle, struct inproc_sync *sync + sync->access = reply->access; + sync->type = reply->type; + sync->closed = 0; ++ sync->shm_idx = do_esync() ? reply->fsync_shm_idx : 0; + } + } + SERVER_END_REQ; + ++ if (!ret && do_esync() && (sync->fd < 0 || !sync->shm_idx)) ++ { ++ ERR( "droiddeck-esync: cannot receive fd for handle %p, out of file descriptors?\n", handle ); ++ if (sync->fd >= 0) close( sync->fd ); ++ esync_release( sync->shm_idx ); ++ memset( sync, 0, sizeof(*sync) ); ++ ret = STATUS_TOO_MANY_OPENED_FILES; ++ } + return ret; + } + +@@ -789,7 +805,8 @@ static NTSTATUS inproc_release_semaphore( HANDLE handle, ULONG count, ULONG *pre + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_SEMAPHORE, SEMAPHORE_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_release_semaphore_obj( sync->fd, count, prev_count ); ++ if (do_esync()) ret = esync_release_semaphore( sync->fd, sync->shm_idx, count, prev_count ); ++ else ret = linux_release_semaphore_obj( sync->fd, count, prev_count ); + release_inproc_sync( sync ); + return ret; + } +@@ -803,7 +820,8 @@ static NTSTATUS inproc_query_semaphore( HANDLE handle, SEMAPHORE_BASIC_INFORMATI + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_SEMAPHORE, SEMAPHORE_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_semaphore_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_semaphore( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_semaphore_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -817,7 +835,8 @@ static NTSTATUS inproc_set_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_set_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_set_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_set_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -831,7 +850,8 @@ static NTSTATUS inproc_reset_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_reset_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_reset_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_reset_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -845,7 +865,8 @@ static NTSTATUS inproc_pulse_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_pulse_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_pulse_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_pulse_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -859,7 +880,8 @@ static NTSTATUS inproc_query_event( HANDLE handle, EVENT_BASIC_INFORMATION *info + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_event_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_event( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_event_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -873,7 +895,8 @@ static NTSTATUS inproc_release_mutex( HANDLE handle, LONG *prev_count ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_MUTEX, 0, &stack, &sync ))) return ret; +- ret = linux_release_mutex_obj( sync->fd, prev_count ); ++ if (do_esync()) ret = esync_release_mutex( sync->fd, sync->shm_idx, prev_count ); ++ else ret = linux_release_mutex_obj( sync->fd, prev_count ); + release_inproc_sync( sync ); + return ret; + } +@@ -887,7 +910,8 @@ static NTSTATUS inproc_query_mutex( HANDLE handle, MUTANT_BASIC_INFORMATION *inf + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_MUTEX, MUTANT_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_mutex_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_mutex( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_mutex_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -928,6 +952,8 @@ static NTSTATUS inproc_wait( DWORD count, const HANDLE *handles, WAIT_TYPE type, + { + struct inproc_sync *syncs[64], stack[ARRAY_SIZE(syncs)]; + int objs[ARRAY_SIZE(syncs)], alert_fd = 0; ++ unsigned int shm_idxs[ARRAY_SIZE(syncs)]; ++ unsigned short types[ARRAY_SIZE(syncs)]; + NTSTATUS ret; + + if (do_fsync()) return fsync_wait_objects( count, handles, type != WaitAll, alertable, timeout ); +@@ -944,6 +970,17 @@ static NTSTATUS inproc_wait( DWORD count, const HANDLE *handles, WAIT_TYPE type, + return ret; + } + objs[i] = syncs[i]->fd; ++ shm_idxs[i] = syncs[i]->shm_idx; ++ types[i] = syncs[i]->type; ++ } ++ ++ if (do_esync()) ++ { ++ alert_fd = alertable ? get_inproc_alert_fd() : -1; ++ if (alertable && alert_fd < 0) ret = STATUS_TOO_MANY_OPENED_FILES; ++ else ret = esync_wait( count, objs, shm_idxs, types, type, alert_fd, timeout ); ++ while (count--) release_inproc_sync( syncs[count] ); ++ return ret; + } + + if (alertable) alert_fd = get_inproc_alert_fd(); +@@ -969,6 +1006,25 @@ static NTSTATUS inproc_signal_and_wait( HANDLE signal, HANDLE wait, + + if ((ret = get_inproc_sync( wait, INPROC_SYNC_UNKNOWN, SYNCHRONIZE, &stack_wait, &wait_sync ))) goto done; + ++ if (do_esync()) ++ { ++ switch (signal_sync->type) ++ { ++ case INPROC_SYNC_EVENT: ret = esync_set_event( signal_sync->fd, signal_sync->shm_idx, NULL ); break; ++ case INPROC_SYNC_MUTEX: ret = esync_release_mutex( signal_sync->fd, signal_sync->shm_idx, NULL ); break; ++ case INPROC_SYNC_SEMAPHORE: ret = esync_release_semaphore( signal_sync->fd, signal_sync->shm_idx, 1, NULL ); break; ++ default: assert( 0 ); break; ++ } ++ if (!ret) ++ { ++ alert_fd = alertable ? get_inproc_alert_fd() : -1; ++ if (alertable && alert_fd < 0) ret = STATUS_TOO_MANY_OPENED_FILES; ++ else ret = esync_wait( 1, &wait_sync->fd, &wait_sync->shm_idx, &wait_sync->type, WaitAny, alert_fd, timeout ); ++ } ++ release_inproc_sync( wait_sync ); ++ goto done; ++ } ++ + switch (signal_sync->type) + { + case INPROC_SYNC_EVENT: ret = linux_set_event_obj( signal_sync->fd, NULL ); break; +@@ -2461,6 +2517,20 @@ NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeou + if (ret != STATUS_NOT_IMPLEMENTED) + return ret; + } ++ else if (do_esync()) ++ { ++ int alert_fd = get_inproc_alert_fd(); ++ ++ if (alert_fd >= 0) ++ { ++ if ((status = esync_wait( 0, NULL, NULL, NULL, WaitAny, alert_fd, timeout )) == STATUS_TIMEOUT) ++ { ++ NtYieldExecution(); ++ status = STATUS_SUCCESS; ++ } ++ return status; ++ } ++ } + /* Since server_wait will result in an unconditional implicit yield, + we never return STATUS_NO_YIELD_PERFORMED */ + if ((status = server_wait( NULL, 0, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE, timeout )) == STATUS_TIMEOUT) +diff --git a/server/Makefile.in b/server/Makefile.in +index d9ac607..b370a0b 100644 +--- a/server/Makefile.in ++++ b/server/Makefile.in +@@ -12,6 +12,7 @@ SOURCES = \ + debugger.c \ + device.c \ + directory.c \ ++ esync.c \ + event.c \ + fd.c \ + file.c \ +diff --git a/server/esync.c b/server/esync.c +new file mode 100644 +index 0000000..508a189 +--- /dev/null ++++ b/server/esync.c +@@ -0,0 +1,525 @@ ++/* droiddeck-esync: eventfd and shared memory synchronization for Wine. ++ * Credit: based on the esync design by Elizabeth Figura for Wine and Proton. */ ++#include "config.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#ifdef HAVE_SYS_EVENTFD_H ++# include ++#endif ++#include ++ ++#include "ntstatus.h" ++#define WIN32_NO_STATUS ++#include "windef.h" ++#include "winternl.h" ++ ++#include "handle.h" ++#include "request.h" ++#include "esync.h" ++ ++#define ESYNC_CHUNK_SIZE 0x10000 ++#define ESYNC_SLOT_SIZE 16 ++#define ESYNC_PER_CHUNK (ESYNC_CHUNK_SIZE / ESYNC_SLOT_SIZE) ++#define ESYNC_CHUNKS 8192 ++#define ESYNC_MANUAL 1 ++#define ESYNC_RELEASING 1 ++ ++struct esync_slot ++{ ++ union ++ { ++ struct ++ { ++ int state; ++ int aux; ++ }; ++ int64_t pair; ++ }; ++ int flags; ++ int ref; ++}; ++ ++union esync_pair ++{ ++ struct { int state; int aux; } s; ++ int64_t word; ++}; ++ ++struct holder ++{ ++ process_id_t pid; ++ unsigned int count; ++ struct holder *next; ++}; ++ ++int esync_active; ++ ++static int shm_fd = -1; ++static unsigned int shm_chunks; ++static void *chunks[ESYNC_CHUNKS]; ++static struct holder *holders[ESYNC_CHUNKS]; ++static uint64_t *free_map; ++static unsigned int free_words; ++static unsigned int free_hint; ++ ++#if defined(HAVE_SYS_EVENTFD_H) && defined(HAVE_MEMFD_CREATE) ++ ++static void *map_chunk( unsigned int chunk ) ++{ ++ void *addr; ++ ++ if (chunk >= ESYNC_CHUNKS) return NULL; ++ if (chunks[chunk]) return chunks[chunk]; ++ if (!holders[chunk] && !(holders[chunk] = calloc( ESYNC_PER_CHUNK, sizeof(struct holder) ))) return NULL; ++ if (chunk >= shm_chunks) ++ { ++ if (ftruncate( shm_fd, (off_t)(chunk + 1) * ESYNC_CHUNK_SIZE ) == -1) ++ { ++ perror( "droiddeck-esync: ftruncate" ); ++ return NULL; ++ } ++ shm_chunks = chunk + 1; ++ } ++ addr = mmap( NULL, ESYNC_CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, ++ (off_t)chunk * ESYNC_CHUNK_SIZE ); ++ if (addr == MAP_FAILED) ++ { ++ perror( "droiddeck-esync: mmap" ); ++ return NULL; ++ } ++ return chunks[chunk] = addr; ++} ++ ++static struct esync_slot *get_slot( unsigned int idx ) ++{ ++ char *base = chunks[idx / ESYNC_PER_CHUNK]; ++ return (struct esync_slot *)(base + (idx % ESYNC_PER_CHUNK) * ESYNC_SLOT_SIZE); ++} ++ ++static struct holder *get_holder( unsigned int idx ) ++{ ++ return &holders[idx / ESYNC_PER_CHUNK][idx % ESYNC_PER_CHUNK]; ++} ++ ++static void clear_holders( unsigned int idx ) ++{ ++ struct holder *head = get_holder( idx ), *next; ++ ++ while ((next = head->next)) ++ { ++ head->next = next->next; ++ free( next ); ++ } ++ head->pid = 0; ++ head->count = 0; ++} ++ ++static unsigned int alloc_slot(void) ++{ ++ unsigned int i, bit, idx; ++ ++ for (i = free_hint; i < free_words; i++) if (free_map[i]) break; ++ if (i == free_words) ++ { ++ unsigned int words = free_words * 2; ++ uint64_t *map; ++ ++ if (words > ESYNC_CHUNKS * (ESYNC_PER_CHUNK / 64)) words = ESYNC_CHUNKS * (ESYNC_PER_CHUNK / 64); ++ if (words == free_words) return 0; ++ if (!(map = realloc( free_map, words * sizeof(*map) ))) return 0; ++ memset( map + free_words, 0xff, (words - free_words) * sizeof(*map) ); ++ free_map = map; ++ free_words = words; ++ } ++ bit = __builtin_ctzll( free_map[i] ); ++ idx = i * 64 + bit; ++ if (!map_chunk( idx / ESYNC_PER_CHUNK )) return 0; ++ free_map[i] &= ~((uint64_t)1 << bit); ++ free_hint = i; ++ return idx; ++} ++ ++static void free_slot( unsigned int idx ) ++{ ++ clear_holders( idx ); ++ free_map[idx / 64] |= (uint64_t)1 << (idx % 64); ++ if (idx / 64 < free_hint) free_hint = idx / 64; ++} ++ ++static int slot_in_use( unsigned int idx ) ++{ ++ if (!idx || idx / 64 >= free_words) return 0; ++ return !(free_map[idx / 64] & ((uint64_t)1 << (idx % 64))); ++} ++ ++static void put_ref( unsigned int idx ) ++{ ++ struct esync_slot *slot = get_slot( idx ); ++ ++ if (slot->ref > 0 && !--slot->ref) free_slot( idx ); ++} ++ ++static void report_fd_error( const char *what ) ++{ ++ static int warned; ++ struct rlimit limit; ++ ++ if (warned) return; ++ warned = 1; ++ if ((errno == EMFILE || errno == ENFILE) && !getrlimit( RLIMIT_NOFILE, &limit )) ++ fprintf( stderr, "droiddeck-esync: out of file descriptors in %s (limit %llu); set PROTON_NO_ESYNC=1 for this game if it fails.\n", ++ what, (unsigned long long)limit.rlim_cur ); ++ else ++ fprintf( stderr, "droiddeck-esync: %s failed: %s\n", what, strerror( errno )); ++} ++ ++static void wake_fd( int fd ) ++{ ++ static const uint64_t value = 1; ++ if (fd >= 0 && write( fd, &value, sizeof(value) ) == -1 && errno != EAGAIN) perror( "droiddeck-esync: write" ); ++} ++ ++static void drain_fd( int fd ) ++{ ++ uint64_t value; ++ if (fd >= 0 && read( fd, &value, sizeof(value) ) == -1 && errno != EAGAIN) perror( "droiddeck-esync: read" ); ++} ++ ++static int fd_readable( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ ++ return fd >= 0 && poll( &pfd, 1, 0 ) > 0 && (pfd.revents & POLLIN); ++} ++ ++static int owner_dead( int owner ) ++{ ++ return owner > 0 && kill( owner, 0 ) == -1 && errno == ESRCH; ++} ++ ++static void repair_slot( struct esync_slot *slot, int fd ) ++{ ++ int readable; ++ ++ if (fd < 0) return; ++ readable = fd_readable( fd ); ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) ++ { ++ if (!readable) wake_fd( fd ); ++ } ++ else if (readable) drain_fd( fd ); ++} ++ ++static void lock_slot( struct esync_slot *slot, int fd ) ++{ ++ int self = getpid(), owner; ++ unsigned int spins = 0; ++ ++ for (;;) ++ { ++ owner = 0; ++ if (__atomic_compare_exchange_n( &slot->aux, &owner, self, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) return; ++ if (++spins < 128) continue; ++ if (!(spins & 1023) && owner_dead( owner ) && ++ __atomic_compare_exchange_n( &slot->aux, &owner, self, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) ++ { ++ repair_slot( slot, fd ); ++ return; ++ } ++ sched_yield(); ++ } ++} ++ ++static void unlock_slot( struct esync_slot *slot ) ++{ ++ int self = getpid(); ++ __atomic_compare_exchange_n( &slot->aux, &self, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED ); ++} ++ ++int esync_init(void) ++{ ++ const char *env = getenv( "WINEESYNC" ); ++ struct rlimit limit; ++ int probe; ++ ++ if (!env || !atoi( env )) return 0; ++ if ((probe = eventfd( 0, EFD_CLOEXEC | EFD_NONBLOCK )) == -1) ++ { ++ perror( "droiddeck-esync: eventfd" ); ++ return 0; ++ } ++ close( probe ); ++ if ((shm_fd = memfd_create( "droiddeck-esync", MFD_CLOEXEC )) == -1) ++ { ++ perror( "droiddeck-esync: memfd_create" ); ++ return 0; ++ } ++ free_words = 64; ++ if (!(free_map = malloc( free_words * sizeof(*free_map) )) || !map_chunk( 0 )) ++ { ++ free( free_map ); ++ free_map = NULL; ++ free_words = 0; ++ close( shm_fd ); ++ shm_fd = -1; ++ return 0; ++ } ++ memset( free_map, 0xff, free_words * sizeof(*free_map) ); ++ free_map[0] &= ~(uint64_t)1; ++ esync_active = 1; ++ fprintf( stderr, "droiddeck-esync: up and running.\n" ); ++ if (!getrlimit( RLIMIT_NOFILE, &limit )) ++ fprintf( stderr, "droiddeck-esync: file descriptor limit %llu.\n", (unsigned long long)limit.rlim_cur ); ++ return 1; ++} ++ ++int esync_shm_fd(void) ++{ ++ return shm_fd; ++} ++ ++int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ) ++{ ++ struct esync_slot *slot; ++ unsigned int idx; ++ int fd = ESYNC_NO_FD; ++ ++ if (!lazy && (fd = eventfd( initval, EFD_CLOEXEC | EFD_NONBLOCK | (semaphore ? EFD_SEMAPHORE : 0) )) == -1) ++ { ++ report_fd_error( "eventfd" ); ++ return -1; ++ } ++ if (!(idx = alloc_slot())) ++ { ++ if (fd >= 0) close( fd ); ++ return -1; ++ } ++ slot = get_slot( idx ); ++ slot->state = state; ++ slot->aux = aux; ++ slot->flags = manual ? ESYNC_MANUAL : 0; ++ __atomic_store_n( &slot->ref, 1, __ATOMIC_SEQ_CST ); ++ *shm_idx = idx; ++ return fd; ++} ++ ++int esync_open( unsigned int shm_idx ) ++{ ++ int fd; ++ ++ if (!slot_in_use( shm_idx )) return -1; ++ if ((fd = eventfd( __atomic_load_n( &get_slot( shm_idx )->state, __ATOMIC_SEQ_CST ) ? 1 : 0, ++ EFD_CLOEXEC | EFD_NONBLOCK )) == -1) ++ report_fd_error( "eventfd" ); ++ return fd; ++} ++ ++void esync_destroy( int fd, unsigned int shm_idx ) ++{ ++ if (fd >= 0) close( fd ); ++ if (slot_in_use( shm_idx )) put_ref( shm_idx ); ++} ++ ++unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ) ++{ ++ struct holder *head, *node; ++ ++ if (!slot_in_use( shm_idx )) return 0; ++ head = get_holder( shm_idx ); ++ for (node = head; node; node = node->next) ++ { ++ if (node->count && node->pid == pid) ++ { ++ node->count++; ++ get_slot( shm_idx )->ref++; ++ return shm_idx; ++ } ++ } ++ if (!head->count) ++ { ++ head->pid = pid; ++ head->count = 1; ++ } ++ else ++ { ++ if (!(node = malloc( sizeof(*node) ))) return 0; ++ node->pid = pid; ++ node->count = 1; ++ node->next = head->next; ++ head->next = node; ++ } ++ get_slot( shm_idx )->ref++; ++ return shm_idx; ++} ++ ++static unsigned int drop_holder( unsigned int idx, process_id_t pid, int all ) ++{ ++ struct holder *head = get_holder( idx ), *prev = NULL, *node, *next; ++ unsigned int dropped; ++ ++ for (node = head; node; prev = node, node = node->next) ++ if (node->count && node->pid == pid) break; ++ if (!node) return 0; ++ dropped = all ? node->count : 1; ++ if ((node->count -= dropped)) return dropped; ++ if (node != head) ++ { ++ prev->next = node->next; ++ free( node ); ++ } ++ else if ((next = head->next)) ++ { ++ *head = *next; ++ free( next ); ++ } ++ else head->pid = 0; ++ return dropped; ++} ++ ++void esync_client_release( process_id_t pid, unsigned int shm_idx ) ++{ ++ if (!slot_in_use( shm_idx )) return; ++ if (drop_holder( shm_idx, pid, 0 )) put_ref( shm_idx ); ++} ++ ++void esync_cleanup_process( process_id_t pid ) ++{ ++ unsigned int i, idx, count; ++ uint64_t used; ++ ++ for (i = 0; i < free_words; i++) ++ { ++ used = ~free_map[i]; ++ while (used) ++ { ++ idx = i * 64 + __builtin_ctzll( used ); ++ used &= used - 1; ++ if (!idx || !slot_in_use( idx )) continue; ++ for (count = drop_holder( idx, pid, 1 ); count && slot_in_use( idx ); count--) put_ref( idx ); ++ } ++ } ++} ++ ++void esync_set( int fd, unsigned int shm_idx, int locked ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!(slot->flags & ESYNC_MANUAL)) ++ { ++ wake_fd( fd ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ return; ++ } ++ if (locked) lock_slot( slot, fd ); ++ if (!__atomic_exchange_n( &slot->state, 1, __ATOMIC_SEQ_CST )) wake_fd( fd ); ++ if (locked) unlock_slot( slot ); ++} ++ ++void esync_reset( int fd, unsigned int shm_idx, int locked ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!(slot->flags & ESYNC_MANUAL)) ++ { ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ drain_fd( fd ); ++ return; ++ } ++ if (locked) lock_slot( slot, fd ); ++ if (__atomic_exchange_n( &slot->state, 0, __ATOMIC_SEQ_CST )) drain_fd( fd ); ++ if (locked) unlock_slot( slot ); ++} ++ ++void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ union esync_pair cur, next = { .s = { ~0, 0 } }; ++ ++ cur.word = __atomic_load_n( &slot->pair, __ATOMIC_SEQ_CST ); ++ for (;;) ++ { ++ if (cur.s.state != (int)tid && ++ (!releasing || cur.s.state != (int)(tid | ESYNC_RELEASING) || fd_readable( fd ))) return; ++ if (__atomic_compare_exchange_n( &slot->pair, &cur.word, next.word, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) break; ++ } ++ wake_fd( fd ); ++} ++ ++thread_id_t esync_mutex_owner( unsigned int shm_idx ) ++{ ++ int owner; ++ ++ if (!slot_in_use( shm_idx )) return 0; ++ owner = __atomic_load_n( &get_slot( shm_idx )->state, __ATOMIC_SEQ_CST ); ++ return owner == ~0 ? 0 : owner & ~ESYNC_RELEASING; ++} ++ ++#else ++ ++int esync_init(void) ++{ ++ return 0; ++} ++ ++int esync_shm_fd(void) ++{ ++ return -1; ++} ++ ++int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ) ++{ ++ errno = ENOSYS; ++ return -1; ++} ++ ++int esync_open( unsigned int shm_idx ) ++{ ++ return -1; ++} ++ ++void esync_destroy( int fd, unsigned int shm_idx ) ++{ ++} ++ ++unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ) ++{ ++ return 0; ++} ++ ++void esync_client_release( process_id_t pid, unsigned int shm_idx ) ++{ ++} ++ ++void esync_cleanup_process( process_id_t pid ) ++{ ++} ++ ++void esync_set( int fd, unsigned int shm_idx, int locked ) ++{ ++} ++ ++void esync_reset( int fd, unsigned int shm_idx, int locked ) ++{ ++} ++ ++void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ) ++{ ++} ++ ++thread_id_t esync_mutex_owner( unsigned int shm_idx ) ++{ ++ return 0; ++} ++ ++#endif +diff --git a/server/esync.h b/server/esync.h +new file mode 100644 +index 0000000..b94565c +--- /dev/null ++++ b/server/esync.h +@@ -0,0 +1,22 @@ ++#define ESYNC_USED_BY_SERVER 0x7eadeeef ++#define ESYNC_SHM_TOKEN 0x7eadeef0 ++#define ESYNC_NO_FD (-2) ++ ++extern int esync_active; ++static inline int do_esync(void) ++{ ++ return esync_active; ++} ++ ++extern int esync_init(void); ++extern int esync_shm_fd(void); ++extern int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ); ++extern int esync_open( unsigned int shm_idx ); ++extern void esync_destroy( int fd, unsigned int shm_idx ); ++extern unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ); ++extern void esync_client_release( process_id_t pid, unsigned int shm_idx ); ++extern void esync_cleanup_process( process_id_t pid ); ++extern void esync_set( int fd, unsigned int shm_idx, int locked ); ++extern void esync_reset( int fd, unsigned int shm_idx, int locked ); ++extern void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ); ++extern thread_id_t esync_mutex_owner( unsigned int shm_idx ); +diff --git a/server/fsync.c b/server/fsync.c +index 7d3ec64..f4d9ac9 100644 +--- a/server/fsync.c ++++ b/server/fsync.c +@@ -43,6 +43,9 @@ + + #include "handle.h" + #include "request.h" ++#include "process.h" ++#include "thread.h" ++#include "esync.h" + #include "fsync.h" + + #include "pshpack4.h" +@@ -359,6 +362,11 @@ void fsync_abandon_mutex( unsigned int shm_idx, thread_id_t tid ) + + DECL_HANDLER(fsync_free_shm_idx) + { ++ if (do_esync()) ++ { ++ esync_client_release( get_process_id( current->process ), req->shm_idx ); ++ return; ++ } + if (!req->shm_idx || req->shm_idx >= shm_idx_free_map_size * BITS_IN_FREE_MAP_WORD) + { + set_error( STATUS_INVALID_PARAMETER ); +diff --git a/server/inproc_sync.c b/server/inproc_sync.c +index a7020ef..b555837 100644 +--- a/server/inproc_sync.c ++++ b/server/inproc_sync.c +@@ -23,6 +23,7 @@ + #include + #include + #include ++#include + + #include "ntstatus.h" + #define WIN32_NO_STATUS +@@ -31,15 +32,18 @@ + #include "file.h" + #include "handle.h" + #include "request.h" ++#include "process.h" + #include "thread.h" + #include "user.h" + ++#include "esync.h" + #include "fsync.h" + + #include "ntsync_tmp.h" + + #ifdef NTSYNC_IOC_EVENT_READ + ++#include + #include + #include + #include +@@ -57,9 +61,10 @@ int get_inproc_device_fd(void) + if (fd >= 0) + { + do_fsync_cached = 0; +- fprintf( stderr, "ntsync: up and running.\n" ); ++ fprintf( stderr, "droiddeck-ntsync: up and running.\n" ); + } + else if (do_fsync()) fd = FSYNC_USED_BY_SERVER; ++ else if (esync_init()) fd = ESYNC_USED_BY_SERVER; + else fprintf( stderr, "wineserver: using server-side synchronization.\n" ); + } + return fd; +@@ -71,6 +76,7 @@ struct inproc_sync + enum inproc_sync_type type; + int fd; + struct list entry; ++ unsigned int shm_idx; + }; + + static struct list inproc_mutexes = LIST_INIT( inproc_mutexes ); +@@ -107,6 +113,12 @@ static const struct object_ops inproc_sync_ops = + int get_inproc_sync_fd( struct inproc_sync *sync ) + { + if (!sync) return -1; ++ if (do_esync() && sync->fd == ESYNC_NO_FD && (sync->fd = esync_open( sync->shm_idx )) == -1) ++ { ++ sync->fd = ESYNC_NO_FD; ++ set_error( STATUS_TOO_MANY_OPENED_FILES ); ++ return -1; ++ } + return sync->fd; + } + +@@ -116,11 +128,17 @@ struct inproc_sync *create_inproc_internal_sync( int manual, int signaled ) + struct inproc_sync *event; + + if (!(event = alloc_object( &inproc_sync_ops ))) return NULL; ++ event->shm_idx = 0; + if (do_fsync()) + { + event->type = manual ? FSYNC_MANUAL_SERVER : FSYNC_AUTO_SERVER; + event->fd = fsync_alloc_shm( signaled, 0xdeadbeef ); + } ++ else if (do_esync()) ++ { ++ event->type = INPROC_SYNC_INTERNAL; ++ event->fd = esync_create( 0, manual, !!signaled, !!signaled, 0, 1, &event->shm_idx ); ++ } + else + { + event->type = INPROC_SYNC_INTERNAL; +@@ -143,11 +161,17 @@ struct inproc_sync *create_inproc_event_sync( int manual, int signaled ) + struct inproc_sync *event; + + if (!(event = alloc_object( &inproc_sync_ops ))) return NULL; ++ event->shm_idx = 0; + if (do_fsync()) + { + event->type = manual ? FSYNC_MANUAL_EVENT : FSYNC_AUTO_EVENT; + event->fd = fsync_alloc_shm( signaled, 0xdeadbeef ); + } ++ else if (do_esync()) ++ { ++ event->type = INPROC_SYNC_EVENT; ++ event->fd = esync_create( 0, manual, !!signaled, !!signaled, 0, 0, &event->shm_idx ); ++ } + else + { + event->type = INPROC_SYNC_EVENT; +@@ -170,11 +194,17 @@ struct inproc_sync *create_inproc_mutex_sync( thread_id_t owner, unsigned int co + struct inproc_sync *mutex; + + if (!(mutex = alloc_object( &inproc_sync_ops ))) return NULL; ++ mutex->shm_idx = 0; + if (do_fsync()) + { + mutex->type = FSYNC_MUTEX; + mutex->fd = fsync_alloc_shm( owner, count ); + } ++ else if (do_esync()) ++ { ++ mutex->type = INPROC_SYNC_MUTEX; ++ mutex->fd = esync_create( 0, 0, owner ? 0 : 1, owner, owner ? count : 0, 0, &mutex->shm_idx ); ++ } + else + { + mutex->type = INPROC_SYNC_MUTEX; +@@ -197,12 +227,18 @@ struct inproc_sync *create_inproc_semaphore_sync( unsigned int initial, unsigned + struct inproc_sync *sem; + + if (!(sem = alloc_object( &inproc_sync_ops ))) return NULL; ++ sem->shm_idx = 0; + + if (do_fsync()) + { + sem->type = FSYNC_SEMAPHORE; + sem->fd = fsync_alloc_shm( initial, max ); + } ++ else if (do_esync()) ++ { ++ sem->type = INPROC_SYNC_SEMAPHORE; ++ sem->fd = esync_create( 1, 0, initial, initial, max, 0, &sem->shm_idx ); ++ } + else + { + sem->type = INPROC_SYNC_SEMAPHORE; +@@ -231,6 +267,7 @@ void signal_inproc_sync( struct inproc_sync *sync ) + __u32 count; + if (debug_level) fprintf( stderr, "set_inproc_event %d\n", sync->fd ); + if (do_fsync()) fsync_set_event( sync->fd ); ++ else if (do_esync()) esync_set( sync->fd, sync->shm_idx, sync->type == INPROC_SYNC_EVENT ); + else ioctl( sync->fd, NTSYNC_IOC_EVENT_SET, &count ); + } + +@@ -239,6 +276,7 @@ void reset_inproc_sync( struct inproc_sync *sync ) + __u32 count; + if (debug_level) fprintf( stderr, "reset_inproc_event %d\n", sync->fd ); + if (do_fsync()) fsync_reset_event( sync->fd ); ++ else if (do_esync()) esync_reset( sync->fd, sync->shm_idx, sync->type == INPROC_SYNC_EVENT ); + else ioctl( sync->fd, NTSYNC_IOC_EVENT_RESET, &count ); + } + +@@ -262,6 +300,7 @@ static void inproc_sync_destroy( struct object *obj ) + assert( obj->ops == &inproc_sync_ops ); + list_remove( &sync->entry ); + if (do_fsync()) fsync_free_shm_idx( sync->fd ); ++ else if (do_esync()) esync_destroy( sync->fd, sync->shm_idx ); + else close( sync->fd ); + } + +@@ -275,12 +314,97 @@ void abandon_inproc_mutexes( thread_id_t tid ) + fsync_abandon_mutex( mutex->fd, tid ); + return; + } ++ if (do_esync()) ++ { ++ LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) ++ esync_abandon_mutex( mutex->fd, mutex->shm_idx, tid, 0 ); ++ return; ++ } + + LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) + ioctl( mutex->fd, NTSYNC_IOC_MUTEX_KILL, &tid ); + } + +-static int get_obj_inproc_sync( struct object *obj, int *type ) ++struct mutex_reaper ++{ ++ struct list entry; ++ struct thread *thread; ++ struct timeout_user *timeout; ++ unsigned int checks; ++}; ++ ++static struct list mutex_reapers = LIST_INIT( mutex_reapers ); ++ ++static void finish_reaper( struct mutex_reaper *reaper ) ++{ ++ if (reaper->timeout) remove_timeout_user( reaper->timeout ); ++ list_remove( &reaper->entry ); ++ abandon_inproc_mutexes( reaper->thread->id ); ++ release_object( reaper->thread ); ++ free( reaper ); ++} ++ ++static void reap_inproc_mutexes( void *private ) ++{ ++ struct mutex_reaper *reaper = private; ++ ++ reaper->timeout = NULL; ++ if (++reaper->checks < 20 && send_thread_signal( reaper->thread, 0 ) && ++ (reaper->timeout = add_timeout_user( -TICKS_PER_SEC / 20, reap_inproc_mutexes, reaper ))) ++ return; ++ finish_reaper( reaper ); ++} ++ ++void abandon_inproc_mutexes_later( struct thread *thread ) ++{ ++ struct mutex_reaper *reaper; ++ ++ if (list_empty( &inproc_mutexes ) || thread->process->is_terminating) return; ++ if (!(reaper = malloc( sizeof(*reaper) ))) return; ++ reaper->thread = (struct thread *)grab_object( thread ); ++ reaper->checks = 0; ++ if (!(reaper->timeout = add_timeout_user( -TICKS_PER_SEC / 100, reap_inproc_mutexes, reaper ))) ++ { ++ release_object( thread ); ++ free( reaper ); ++ return; ++ } ++ list_add_tail( &mutex_reapers, &reaper->entry ); ++} ++ ++void abandon_dead_inproc_mutexes( struct process *process ) ++{ ++ unsigned int error = get_error(); ++ struct mutex_reaper *reaper, *next; ++ struct ntsync_mutex_args args; ++ struct inproc_sync *mutex; ++ struct thread *thread; ++ thread_id_t owner; ++ int dead, gone; ++ ++ LIST_FOR_EACH_ENTRY_SAFE( reaper, next, &mutex_reapers, struct mutex_reaper, entry ) ++ if (reaper->thread->process == process) finish_reaper( reaper ); ++ if (!do_fsync()) ++ { ++ LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) ++ { ++ if (do_esync()) owner = esync_mutex_owner( mutex->shm_idx ); ++ else if (ioctl( mutex->fd, NTSYNC_IOC_MUTEX_READ, &args ) == -1 && errno != EOWNERDEAD) continue; ++ else owner = args.owner; ++ if (!owner) continue; ++ thread = get_thread_from_id( owner ); ++ dead = !thread || thread->state == TERMINATED; ++ gone = !thread || thread->process == process; ++ if (thread) release_object( thread ); ++ if (!dead) continue; ++ if (do_esync()) esync_abandon_mutex( mutex->fd, mutex->shm_idx, owner, gone ); ++ else ioctl( mutex->fd, NTSYNC_IOC_MUTEX_KILL, &owner ); ++ } ++ } ++ set_error( error ); ++} ++ ++static int get_obj_inproc_sync( struct object *obj, int *type, unsigned int *shm_idx ) + { + struct object *sync; + int fd = -1; +@@ -290,7 +414,8 @@ static int get_obj_inproc_sync( struct object *obj, int *type ) + { + struct inproc_sync *inproc = (struct inproc_sync *)sync; + *type = inproc->type; +- fd = inproc->fd; ++ *shm_idx = inproc->shm_idx; ++ fd = get_inproc_sync_fd( inproc ); + } + + release_object( sync ); +@@ -341,7 +466,15 @@ void abandon_inproc_mutexes( thread_id_t tid ) + { + } + +-static int get_obj_inproc_sync( struct object *obj, int *type ) ++void abandon_inproc_mutexes_later( struct thread *thread ) ++{ ++} ++ ++void abandon_dead_inproc_mutexes( struct process *process ) ++{ ++} ++ ++static int get_obj_inproc_sync( struct object *obj, int *type, unsigned int *shm_idx ) + { + return -1; + } +@@ -351,14 +484,23 @@ static int get_obj_inproc_sync( struct object *obj, int *type ) + DECL_HANDLER(get_inproc_sync_fd) + { + struct object *obj; ++ unsigned int shm_idx = 0; + int fd; + + if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return; + + reply->access = get_handle_access( current->process, req->handle ); + +- if ((fd = get_obj_inproc_sync( obj, &reply->type )) < 0) set_error( STATUS_NOT_IMPLEMENTED ); ++ if ((fd = get_obj_inproc_sync( obj, &reply->type, &shm_idx )) < 0) ++ { ++ if (!get_error()) set_error( STATUS_NOT_IMPLEMENTED ); ++ } + else if (do_fsync()) reply->fsync_shm_idx = fsync_grab_shm_idx( fd ); ++ else if (do_esync()) ++ { ++ if (!(reply->fsync_shm_idx = esync_grab( get_process_id( current->process ), shm_idx ))) set_error( STATUS_NO_MEMORY ); ++ else send_client_fd( current->process, fd, req->handle ); ++ } + else send_client_fd( current->process, fd, req->handle ); + + release_object( obj ); +diff --git a/server/object.h b/server/object.h +index 21f5ea8..cd21fb7 100644 +--- a/server/object.h ++++ b/server/object.h +@@ -255,6 +255,8 @@ extern struct inproc_sync *create_inproc_event_sync( int manual, int signaled ); + extern struct inproc_sync *create_inproc_semaphore_sync( unsigned int initial, unsigned int max ); + extern struct inproc_sync *create_inproc_mutex_sync( thread_id_t owner, unsigned int count ); + extern void abandon_inproc_mutexes( thread_id_t owner ); ++extern void abandon_inproc_mutexes_later( struct thread *thread ); ++extern void abandon_dead_inproc_mutexes( struct process *process ); + extern void signal_inproc_sync( struct inproc_sync *sync ); + extern void reset_inproc_sync( struct inproc_sync *sync ); + +diff --git a/server/process.c b/server/process.c +index 287718a..5963884 100644 +--- a/server/process.c ++++ b/server/process.c +@@ -65,6 +65,7 @@ + #include "user.h" + #include "security.h" + ++#include "esync.h" + #include "fsync.h" + + /* process object */ +@@ -618,6 +619,8 @@ static void process_died( struct process *process ) + if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE) + shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL ); + } ++ abandon_dead_inproc_mutexes( process ); ++ if (do_esync()) esync_cleanup_process( process->id ); + release_object( process ); + if (!--running_processes && shutdown_stage) close_master_socket( 0 ); + } +diff --git a/server/thread.c b/server/thread.c +index 435d12a..f35a788 100644 +--- a/server/thread.c ++++ b/server/thread.c +@@ -61,6 +61,7 @@ + #include "user.h" + #include "security.h" + ++#include "esync.h" + #include "fsync.h" + + /* thread queues */ +@@ -1658,7 +1659,11 @@ void kill_thread( struct thread *thread, int violent_death ) + kill_console_processes( thread, 0 ); + abandon_mutexes( thread ); + signal_sync( thread->sync ); +- if (violent_death) send_thread_signal( thread, SIGQUIT ); ++ if (violent_death) ++ { ++ send_thread_signal( thread, SIGQUIT ); ++ abandon_inproc_mutexes_later( thread ); ++ } + cleanup_thread( thread ); + remove_process_thread( thread->process, thread ); + release_object( thread ); +@@ -1836,7 +1841,12 @@ DECL_HANDLER(init_first_thread) + { + reply->inproc_device = FSYNC_USED_BY_SERVER; + } +- else if ((fd = get_inproc_device_fd()) >= 0) ++ else if ((fd = get_inproc_device_fd()) == ESYNC_USED_BY_SERVER) ++ { ++ reply->inproc_device = ESYNC_USED_BY_SERVER; ++ send_client_fd( process, esync_shm_fd(), ESYNC_SHM_TOKEN ); ++ } ++ else if (fd >= 0) + { + reply->inproc_device = get_process_id( process ) | 1; + send_client_fd( process, fd, reply->inproc_device ); +@@ -2478,7 +2488,10 @@ DECL_HANDLER(get_inproc_alert_fd) + { + int fd; + +- if ((fd = get_inproc_sync_fd( current->alert_sync )) < 0) set_error( STATUS_INVALID_PARAMETER ); ++ if ((fd = get_inproc_sync_fd( current->alert_sync )) < 0) ++ { ++ if (!get_error()) set_error( STATUS_INVALID_PARAMETER ); ++ } + else if (do_fsync()) reply->fsync_shm_idx = fd; + else + { diff --git a/tools/droiddeck-esync/patches/valve-11/0001-droiddeck-esync.patch b/tools/droiddeck-esync/patches/valve-11/0001-droiddeck-esync.patch new file mode 100644 index 000000000..80beac4ac --- /dev/null +++ b/tools/droiddeck-esync/patches/valve-11/0001-droiddeck-esync.patch @@ -0,0 +1,2436 @@ +diff --git a/dlls/ntdll/Makefile.in b/dlls/ntdll/Makefile.in +index 38c865b..2a7afcc 100644 +--- a/dlls/ntdll/Makefile.in ++++ b/dlls/ntdll/Makefile.in +@@ -50,6 +50,7 @@ SOURCES = \ + unix/debug.c \ + unix/env.c \ + unix/file.c \ ++ unix/esync.c \ + unix/fsync.c \ + unix/loader.c \ + unix/loadorder.c \ +diff --git a/dlls/ntdll/unix/esync.c b/dlls/ntdll/unix/esync.c +new file mode 100644 +index 0000000..de02a56 +--- /dev/null ++++ b/dlls/ntdll/unix/esync.c +@@ -0,0 +1,1140 @@ ++#if 0 ++#pragma makedep unix ++#endif ++/* droiddeck-esync: the client side of eventfd and shared memory synchronization. ++ * Credit: based on the esync design by Elizabeth Figura for Wine and Proton. */ ++ ++#include "config.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "ntstatus.h" ++#define WIN32_NO_STATUS ++#include "windef.h" ++#include "winternl.h" ++#include "wine/server.h" ++#include "wine/debug.h" ++ ++#include "unix_private.h" ++#include "esync.h" ++ ++WINE_DEFAULT_DEBUG_CHANNEL(esync); ++ ++#define ESYNC_CHUNK_SIZE 0x10000 ++#define ESYNC_SLOT_SIZE 16 ++#define ESYNC_PER_CHUNK (ESYNC_CHUNK_SIZE / ESYNC_SLOT_SIZE) ++#define ESYNC_CHUNKS 8192 ++#define ESYNC_MANUAL 1 ++#define ESYNC_WAITER 0x100 ++#define ESYNC_RELEASING 1 ++#define ESYNC_PULSE_NS 10000000 ++#define ESYNC_STALL_NS 2000000 ++#define ESYNC_TOKEN_MS 100 ++#define ESYNC_TOKEN_TRIES 10 ++#define ESYNC_SPINS 16 ++#define ESYNC_SPIN_NS 50000 ++#define ESYNC_SLICE_NS 1000000000 ++ ++struct esync_slot ++{ ++ union ++ { ++ struct ++ { ++ int state; ++ int aux; ++ }; ++ LONG64 pair; ++ }; ++ int flags; ++ int ref; ++}; ++ ++C_ASSERT( sizeof(struct esync_slot) == ESYNC_SLOT_SIZE ); ++ ++union esync_pair ++{ ++ struct { int state; int aux; } s; ++ LONG64 word; ++}; ++ ++C_ASSERT( offsetof( struct esync_slot, state ) == offsetof( union esync_pair, s.state ) ); ++C_ASSERT( offsetof( struct esync_slot, aux ) == offsetof( union esync_pair, s.aux ) ); ++ ++static BOOL swap_pair( struct esync_slot *slot, int state, int aux, int new_state, int new_aux ) ++{ ++ union esync_pair cur = { .s = { state, aux } }, next = { .s = { new_state, new_aux } }; ++ ++ return __atomic_compare_exchange_n( &slot->pair, &cur.word, next.word, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++} ++ ++enum esync_kind ++{ ++ ESYNC_KIND_SEMAPHORE, ++ ESYNC_KIND_MUTEX, ++ ESYNC_KIND_AUTO, ++ ESYNC_KIND_MANUAL, ++}; ++ ++struct esync_obj ++{ ++ int fd; ++ enum esync_kind kind; ++ struct esync_slot *slot; ++}; ++ ++enum esync_clock ++{ ++ ESYNC_NO_TIMEOUT, ++ ESYNC_MONOTONIC, ++ ESYNC_REALTIME, ++}; ++ ++struct esync_deadline ++{ ++ enum esync_clock clock; ++ ULONGLONG end; ++}; ++ ++struct esync_held ++{ ++ struct esync_slot *slot; ++ int fd; ++}; ++ ++struct esync_waits ++{ ++ struct esync_slot *slots[MAXIMUM_WAIT_OBJECTS]; ++ DWORD count; ++ struct esync_held held[MAXIMUM_WAIT_OBJECTS]; ++ DWORD held_count; ++ DWORD tid; ++ int unix_tid; ++}; ++ ++static int shm_fd = -1; ++static void *chunks[ESYNC_CHUNKS]; ++static sigset_t block_set; ++static __thread struct esync_waits waits; ++static pthread_key_t waits_key; ++static pthread_once_t waits_once = PTHREAD_ONCE_INIT; ++ ++#ifdef __linux__ ++ ++void esync_init( int fd ) ++{ ++ shm_fd = fd; ++ block_set = server_block_set; ++ sigaddset( &block_set, SIGQUIT ); ++ TRACE( "shm fd %d\n", fd ); ++} ++ ++static struct esync_slot *get_slot( unsigned int idx ) ++{ ++ unsigned int chunk = idx / ESYNC_PER_CHUNK; ++ void *base, *addr; ++ ++ if (!idx || chunk >= ESYNC_CHUNKS) return NULL; ++ if (!(base = __atomic_load_n( &chunks[chunk], __ATOMIC_ACQUIRE ))) ++ { ++ addr = mmap( NULL, ESYNC_CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, ++ (off_t)chunk * ESYNC_CHUNK_SIZE ); ++ if (addr == MAP_FAILED) ++ { ++ ERR( "cannot map chunk %u: %s\n", chunk, strerror( errno ) ); ++ return NULL; ++ } ++ if (__atomic_compare_exchange_n( &chunks[chunk], &base, addr, FALSE, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE )) ++ base = addr; ++ else ++ munmap( addr, ESYNC_CHUNK_SIZE ); ++ } ++ return (struct esync_slot *)((char *)base + (idx % ESYNC_PER_CHUNK) * ESYNC_SLOT_SIZE); ++} ++ ++void esync_release( unsigned int shm_idx ) ++{ ++ if (!shm_idx) return; ++ SERVER_START_REQ( fsync_free_shm_idx ) ++ { ++ req->shm_idx = shm_idx; ++ wine_server_call( req ); ++ } ++ SERVER_END_REQ; ++} ++ ++static void block_signals( sigset_t *old ) ++{ ++ pthread_sigmask( SIG_BLOCK, &block_set, old ); ++} ++ ++static void restore_signals( const sigset_t *old ) ++{ ++ pthread_sigmask( SIG_SETMASK, old, NULL ); ++} ++ ++static ssize_t read_fd( int fd, uint64_t *value ) ++{ ++ return syscall( __NR_read, fd, value, sizeof(*value) ); ++} ++ ++static int poll_fds( struct pollfd *fds, nfds_t count, const struct timespec *timeout ) ++{ ++ struct timespec left; ++ ++ if (timeout) left = *timeout; ++ return syscall( __NR_ppoll, fds, count, timeout ? &left : NULL, NULL, 0 ); ++} ++ ++static NTSTATUS wake_fd( int fd, uint64_t count ) ++{ ++ if (syscall( __NR_write, fd, &count, sizeof(count) ) == -1) return errno_to_status( errno ); ++ return STATUS_SUCCESS; ++} ++ ++static BOOL drain_fd( int fd ) ++{ ++ uint64_t value; ++ ++ if (read_fd( fd, &value ) == sizeof(value)) return TRUE; ++ if (errno != EAGAIN) WARN( "read fd %d: %s\n", fd, strerror( errno ) ); ++ return FALSE; ++} ++ ++static BOOL fd_readable( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ struct timespec now = { 0, 0 }; ++ int ret; ++ ++ while ((ret = poll_fds( &pfd, 1, &now )) < 0 && errno == EINTR); ++ return ret > 0 && (pfd.revents & POLLIN); ++} ++ ++static BOOL consume_token( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ struct timespec pause = { 0, ESYNC_TOKEN_MS * 1000000 }; ++ unsigned int tries = 0; ++ uint64_t value; ++ ++ while (read_fd( fd, &value ) != sizeof(value)) ++ { ++ if (errno != EAGAIN && errno != EINTR) ++ { ++ WARN( "read fd %d: %s\n", fd, strerror( errno ) ); ++ return TRUE; ++ } ++ if (tries == ESYNC_TOKEN_TRIES) ++ { ++ WARN( "no token on fd %d, the count it was released with is lost\n", fd ); ++ return FALSE; ++ } ++ if (!poll_fds( &pfd, 1, &pause )) tries++; ++ } ++ return TRUE; ++} ++ ++static int current_unix_tid(void) ++{ ++ if (!waits.unix_tid) waits.unix_tid = syscall( __NR_gettid ); ++ return waits.unix_tid; ++} ++ ++static void repair_slot( struct esync_slot *slot, int fd ) ++{ ++ BOOL readable = fd_readable( fd ); ++ ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) ++ { ++ if (!readable) wake_fd( fd, 1 ); ++ } ++ else if (readable) drain_fd( fd ); ++} ++ ++static void lock_slot( struct esync_slot *slot, int fd, int self ) ++{ ++ unsigned int spins = 0; ++ int owner; ++ ++ for (;;) ++ { ++ owner = 0; ++ if (__atomic_compare_exchange_n( &slot->aux, &owner, self, FALSE, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) return; ++ if (++spins < 128) ++ { ++ YieldProcessor(); ++ continue; ++ } ++ if (!(spins & 1023) && owner > 0 && kill( owner, 0 ) == -1 && errno == ESRCH && ++ __atomic_compare_exchange_n( &slot->aux, &owner, self, FALSE, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) ++ { ++ WARN( "lock owner %d is gone, taking over\n", owner ); ++ repair_slot( slot, fd ); ++ return; ++ } ++ sched_yield(); ++ } ++} ++ ++static void unlock_slot( struct esync_slot *slot, int self ) ++{ ++ __atomic_compare_exchange_n( &slot->aux, &self, 0, FALSE, __ATOMIC_RELEASE, __ATOMIC_RELAXED ); ++} ++ ++static void drop_waits( void *arg ) ++{ ++ struct esync_waits *list = arg; ++ struct esync_held *held; ++ ++ while (list->count) __atomic_sub_fetch( &list->slots[--list->count]->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ while (list->held_count) ++ { ++ held = &list->held[--list->held_count]; ++ if (swap_pair( held->slot, list->tid, __atomic_load_n( &held->slot->aux, __ATOMIC_SEQ_CST ), ~0, 0 )) ++ wake_fd( held->fd, 1 ); ++ } ++} ++ ++static void init_waits_key(void) ++{ ++ pthread_key_create( &waits_key, drop_waits ); ++} ++ ++static void arm_waits(void) ++{ ++ pthread_once( &waits_once, init_waits_key ); ++ if (!pthread_getspecific( waits_key )) pthread_setspecific( waits_key, &waits ); ++} ++ ++static void add_waits( DWORD count, const struct esync_obj *objs ) ++{ ++ DWORD i; ++ ++ arm_waits(); ++ for (i = 0; i < count && waits.count < MAXIMUM_WAIT_OBJECTS; i++) ++ { ++ if (objs[i].kind != ESYNC_KIND_AUTO && objs[i].kind != ESYNC_KIND_MANUAL) continue; ++ waits.slots[waits.count] = objs[i].slot; ++ __atomic_add_fetch( &objs[i].slot->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ __atomic_store_n( &waits.count, waits.count + 1, __ATOMIC_RELAXED ); ++ } ++} ++ ++static void remove_waits( DWORD base ) ++{ ++ struct esync_slot *slot; ++ ++ while (waits.count > base) ++ { ++ slot = waits.slots[waits.count - 1]; ++ __atomic_store_n( &waits.count, waits.count - 1, __ATOMIC_RELAXED ); ++ __atomic_sub_fetch( &slot->flags, ESYNC_WAITER, __ATOMIC_SEQ_CST ); ++ } ++} ++ ++static void hold_mutex( struct esync_obj *obj, DWORD tid ) ++{ ++ arm_waits(); ++ waits.tid = tid; ++ waits.held[waits.held_count].slot = obj->slot; ++ waits.held[waits.held_count].fd = obj->fd; ++ waits.held_count++; ++} ++ ++static ULONGLONG monotonic_ns(void) ++{ ++ struct timespec ts; ++ ++ clock_gettime( CLOCK_MONOTONIC, &ts ); ++ return (ULONGLONG)ts.tv_sec * 1000000000 + ts.tv_nsec; ++} ++ ++NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret = STATUS_SUCCESS; ++ sigset_t old; ++ int current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ block_signals( &old ); ++ current = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ do ++ { ++ if ((ULONGLONG)(unsigned int)current + count > (unsigned int)slot->aux) ++ { ++ restore_signals( &old ); ++ return STATUS_SEMAPHORE_LIMIT_EXCEEDED; ++ } ++ } ++ while (!__atomic_compare_exchange_n( &slot->state, ¤t, (int)((unsigned int)current + count), ++ FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (count) ret = wake_fd( fd, count ); ++ restore_signals( &old ); ++ if (!ret && prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ info->CurrentCount = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->MaximumCount = slot->aux; ++ return STATUS_SUCCESS; ++} ++ ++static NTSTATUS set_manual( struct esync_slot *slot, int fd, LONG *prev ) ++{ ++ int self = current_unix_tid(); ++ NTSTATUS ret = STATUS_SUCCESS; ++ sigset_t old; ++ LONG current; ++ ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ) == 1) ++ { ++ if (prev) *prev = 1; ++ return STATUS_SUCCESS; ++ } ++ block_signals( &old ); ++ lock_slot( slot, fd, self ); ++ if (!(current = __atomic_exchange_n( &slot->state, 1, __ATOMIC_SEQ_CST ))) ret = wake_fd( fd, 1 ); ++ unlock_slot( slot, self ); ++ restore_signals( &old ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++static LONG reset_manual( struct esync_slot *slot, int fd ) ++{ ++ int self = current_unix_tid(); ++ sigset_t old; ++ LONG current; ++ ++ if (!__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) return 0; ++ block_signals( &old ); ++ lock_slot( slot, fd, self ); ++ if ((current = __atomic_exchange_n( &slot->state, 0, __ATOMIC_SEQ_CST ))) drain_fd( fd ); ++ unlock_slot( slot, self ); ++ restore_signals( &old ); ++ return current; ++} ++ ++NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret; ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) return set_manual( slot, fd, prev ); ++ current = prev ? fd_readable( fd ) : 0; ++ ret = wake_fd( fd, 1 ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) current = reset_manual( slot, fd ); ++ else ++ { ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ current = drain_fd( fd ); ++ } ++ if (prev) *prev = current; ++ return STATUS_SUCCESS; ++} ++ ++static void pulse_wait( struct esync_slot *slot, int fd, BOOL manual ) ++{ ++ ULONGLONG now = monotonic_ns(), deadline = now + ESYNC_PULSE_NS, stall = now + ESYNC_STALL_NS; ++ int waiters = __atomic_load_n( &slot->flags, __ATOMIC_SEQ_CST ) / ESYNC_WAITER, left; ++ ++ while (waiters > 0 && now < deadline && now < stall) ++ { ++ if (!manual && !fd_readable( fd )) break; ++ sched_yield(); ++ left = __atomic_load_n( &slot->flags, __ATOMIC_SEQ_CST ) / ESYNC_WAITER; ++ now = monotonic_ns(); ++ if (left < waiters) stall = now + ESYNC_STALL_NS; ++ waiters = left; ++ } ++} ++ ++NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ NTSTATUS ret; ++ sigset_t old; ++ LONG current; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ block_signals( &old ); ++ if (slot->flags & ESYNC_MANUAL) ++ { ++ if (!(ret = set_manual( slot, fd, ¤t ))) pulse_wait( slot, fd, TRUE ); ++ reset_manual( slot, fd ); ++ } ++ else ++ { ++ current = prev ? fd_readable( fd ) : 0; ++ ret = wake_fd( fd, 1 ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ if (!ret) pulse_wait( slot, fd, FALSE ); ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ drain_fd( fd ); ++ } ++ restore_signals( &old ); ++ if (prev) *prev = current; ++ return ret; ++} ++ ++NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if (slot->flags & ESYNC_MANUAL) ++ { ++ info->EventState = !!__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->EventType = NotificationEvent; ++ } ++ else ++ { ++ info->EventState = fd_readable( fd ); ++ info->EventType = SynchronizationEvent; ++ } ++ return STATUS_SUCCESS; ++} ++ ++NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ DWORD tid = GetCurrentThreadId(); ++ NTSTATUS ret = STATUS_SUCCESS; ++ int owner = tid | ESYNC_RELEASING, count; ++ sigset_t old; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ if ((DWORD)__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ) != tid) return STATUS_MUTANT_NOT_OWNED; ++ block_signals( &old ); ++ count = __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ if (count > 1) ++ { ++ if (!swap_pair( slot, tid, count, tid, count - 1 )) ret = STATUS_MUTANT_NOT_OWNED; ++ } ++ else if (!swap_pair( slot, tid, count, tid | ESYNC_RELEASING, 0 )) ret = STATUS_MUTANT_NOT_OWNED; ++ else if ((ret = wake_fd( fd, 1 ))) swap_pair( slot, tid | ESYNC_RELEASING, 0, tid, count ); ++ else __atomic_compare_exchange_n( &slot->state, &owner, 0, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++ restore_signals( &old ); ++ if (!ret && prev) *prev = 1 - count; ++ return ret; ++} ++ ++NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ DWORD owner; ++ ++ if (!slot) return STATUS_INVALID_HANDLE; ++ owner = __atomic_load_n( &slot->state, __ATOMIC_SEQ_CST ); ++ info->CurrentCount = 1 - __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ info->OwnedByCaller = owner == GetCurrentThreadId(); ++ info->AbandonedState = owner == ~0u; ++ return STATUS_SUCCESS; ++} ++ ++static void get_deadline( const LARGE_INTEGER *timeout, struct esync_deadline *end ) ++{ ++ ULONGLONG now, rel; ++ ++ end->clock = ESYNC_NO_TIMEOUT; ++ if (!timeout || timeout->QuadPart == TIMEOUT_INFINITE) return; ++ if (timeout->QuadPart > 0) ++ { ++ end->clock = ESYNC_REALTIME; ++ end->end = timeout->QuadPart; ++ return; ++ } ++ now = monotonic_ns(); ++ rel = -(ULONGLONG)timeout->QuadPart; ++ if (rel > (ULLONG_MAX - now) / 100) return; ++ end->clock = ESYNC_MONOTONIC; ++ end->end = now + rel * 100; ++} ++ ++static BOOL time_left( const struct esync_deadline *end, struct timespec *ts, BOOL *sliced ) ++{ ++ ULONGLONG left = 0, now; ++ struct timespec rt; ++ LONGLONG ticks; ++ ++ *sliced = FALSE; ++ switch (end->clock) ++ { ++ case ESYNC_NO_TIMEOUT: ++ return FALSE; ++ case ESYNC_MONOTONIC: ++ now = monotonic_ns(); ++ if (now < end->end) left = end->end - now; ++ break; ++ case ESYNC_REALTIME: ++ clock_gettime( CLOCK_REALTIME, &rt ); ++ ticks = ticks_from_time_t( rt.tv_sec ) + rt.tv_nsec / 100; ++ if ((LONGLONG)end->end > ticks) ++ { ++ left = (ULONGLONG)((LONGLONG)end->end - ticks); ++ left = left > ESYNC_SLICE_NS / 100 ? ESYNC_SLICE_NS : left * 100; ++ } ++ if (left == ESYNC_SLICE_NS) *sliced = TRUE; ++ break; ++ } ++ ts->tv_sec = left / 1000000000; ++ ts->tv_nsec = left % 1000000000; ++ return TRUE; ++} ++ ++static BOOL expired( const struct esync_deadline *end ) ++{ ++ struct timespec ts; ++ BOOL sliced; ++ ++ return time_left( end, &ts, &sliced ) && !sliced && !ts.tv_sec && !ts.tv_nsec; ++} ++ ++static int do_poll( struct pollfd *fds, nfds_t count, const struct esync_deadline *end ) ++{ ++ struct timespec ts; ++ BOOL sliced; ++ int ret; ++ ++ for (;;) ++ { ++ ret = poll_fds( fds, count, time_left( end, &ts, &sliced ) ? &ts : NULL ); ++ if (ret < 0 && errno == EINTR) continue; ++ if (!ret && sliced) continue; ++ return ret; ++ } ++} ++ ++static NTSTATUS user_apc(void) ++{ ++ static const LARGE_INTEGER zero; ++ ++ return server_wait( NULL, 0, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE, &zero ); ++} ++ ++static BOOL take_semaphore( struct esync_obj *obj ) ++{ ++ int current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ sigset_t old; ++ BOOL ret; ++ ++ if (current <= 0) return FALSE; ++ block_signals( &old ); ++ do ++ { ++ if (current <= 0) ++ { ++ restore_signals( &old ); ++ return FALSE; ++ } ++ } ++ while (!__atomic_compare_exchange_n( &obj->slot->state, ¤t, current - 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ ret = consume_token( obj->fd ); ++ restore_signals( &old ); ++ return ret; ++} ++ ++static void back_off( unsigned int *spins ) ++{ ++ struct timespec pause = { 0, 1000000 }; ++ ++ if (++*spins <= ESYNC_SPINS) sched_yield(); ++ else nanosleep( &pause, NULL ); ++} ++ ++static BOOL mutex_free( int state ) ++{ ++ return !state || (state & ESYNC_RELEASING); ++} ++ ++static BOOL claim_mutex( struct esync_obj *obj, DWORD tid, int *prev ) ++{ ++ int owner = tid; ++ uint64_t value; ++ ++ if (!mutex_free( *prev )) return FALSE; ++ if (!__atomic_compare_exchange_n( &obj->slot->state, prev, tid, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ return FALSE; ++ if (read_fd( obj->fd, &value ) != sizeof(value)) ++ { ++ __atomic_compare_exchange_n( &obj->slot->state, &owner, *prev, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++ return FALSE; ++ } ++ if ((DWORD)__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ) != tid) ++ { ++ wake_fd( obj->fd, 1 ); ++ return FALSE; ++ } ++ return TRUE; ++} ++ ++static void unclaim_mutex( struct esync_obj *obj, DWORD tid, int prev ) ++{ ++ int owner = tid; ++ ++ if ((DWORD)prev == ~0u) ++ { ++ if (__atomic_compare_exchange_n( &obj->slot->state, &owner, ~0, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ wake_fd( obj->fd, 1 ); ++ return; ++ } ++ if (!__atomic_compare_exchange_n( &obj->slot->state, &owner, tid | ESYNC_RELEASING, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) ++ return; ++ wake_fd( obj->fd, 1 ); ++ owner = tid | ESYNC_RELEASING; ++ __atomic_compare_exchange_n( &obj->slot->state, &owner, prev, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ); ++} ++ ++static BOOL take_mutex( struct esync_obj *obj, DWORD tid, BOOL *abandoned ) ++{ ++ int prev = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ BOOL ret = FALSE; ++ sigset_t old; ++ ++ if (!mutex_free( prev )) return FALSE; ++ block_signals( &old ); ++ if (claim_mutex( obj, tid, &prev )) ++ { ++ if ((DWORD)prev == ~0u) *abandoned = TRUE; ++ hold_mutex( obj, tid ); ++ swap_pair( obj->slot, tid, __atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ), tid, 1 ); ++ ret = TRUE; ++ } ++ restore_signals( &old ); ++ waits.held_count = 0; ++ return ret; ++} ++ ++static BOOL take_auto( struct esync_obj *obj ) ++{ ++ uint64_t value; ++ ++ __atomic_store_n( &obj->slot->state, 0, __ATOMIC_SEQ_CST ); ++ return read_fd( obj->fd, &value ) == sizeof(value); ++} ++ ++static NTSTATUS recurse_mutex( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ int count = __atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ); ++ ++ if (count == INT_MAX) return STATUS_MUTANT_LIMIT_EXCEEDED; ++ swap_pair( obj->slot, tid, count, tid, count + 1 ); ++ return i; ++} ++ ++static NTSTATUS try_now( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ int state = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ BOOL abandoned = FALSE; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ if ((DWORD)state == tid) return recurse_mutex( obj, i, tid ); ++ if (!mutex_free( state )) break; ++ if (take_mutex( obj, tid, &abandoned )) return abandoned ? STATUS_ABANDONED_WAIT_0 + i : i; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ if (take_semaphore( obj )) return i; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (state && take_auto( obj )) return i; ++ break; ++ case ESYNC_KIND_MANUAL: ++ if (state) return i; ++ break; ++ } ++ return STATUS_PENDING; ++} ++ ++static NTSTATUS try_ready( struct esync_obj *obj, DWORD i, DWORD tid ) ++{ ++ BOOL abandoned = FALSE; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ if (take_mutex( obj, tid, &abandoned )) return abandoned ? STATUS_ABANDONED_WAIT_0 + i : i; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ if (take_semaphore( obj )) return i; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (take_auto( obj )) return i; ++ break; ++ case ESYNC_KIND_MANUAL: ++ return i; ++ } ++ return STATUS_PENDING; ++} ++ ++static NTSTATUS wait_any( DWORD count, struct esync_obj *objs, int alert_fd, const struct esync_deadline *end, BOOL track ) ++{ ++ struct pollfd fds[MAXIMUM_WAIT_OBJECTS + 1]; ++ DWORD tid = GetCurrentThreadId(), i; ++ unsigned int spins = 0; ++ ULONGLONG polled; ++ NTSTATUS status; ++ BOOL busy; ++ nfds_t nfds; ++ int ret; ++ ++ for (;;) ++ { ++ for (i = 0; i < count; i++) ++ if ((status = try_now( &objs[i], i, tid )) != STATUS_PENDING) return status; ++ if (track) ++ { ++ add_waits( count, objs ); ++ track = FALSE; ++ continue; ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ fds[i].fd = objs[i].fd; ++ fds[i].events = POLLIN; ++ fds[i].revents = 0; ++ } ++ nfds = count; ++ if (alert_fd >= 0) ++ { ++ fds[nfds].fd = alert_fd; ++ fds[nfds].events = POLLIN; ++ fds[nfds].revents = 0; ++ nfds++; ++ } ++ ++ polled = monotonic_ns(); ++ if ((ret = do_poll( fds, nfds, end )) < 0) return errno_to_status( errno ); ++ if (!ret) return STATUS_TIMEOUT; ++ ++ busy = FALSE; ++ for (i = 0; i < count; i++) ++ { ++ if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) return STATUS_INVALID_HANDLE; ++ if (!(fds[i].revents & POLLIN)) continue; ++ if ((status = try_ready( &objs[i], i, tid )) != STATUS_PENDING) return status; ++ busy = TRUE; ++ } ++ if (alert_fd >= 0 && (fds[count].revents & POLLIN) && (status = user_apc()) != STATUS_TIMEOUT) ++ return status; ++ if (expired( end )) return STATUS_TIMEOUT; ++ if (busy && monotonic_ns() - polled < ESYNC_SPIN_NS) back_off( &spins ); ++ else spins = 0; ++ } ++} ++ ++static BOOL owned_mutex( struct esync_obj *obj, DWORD tid ) ++{ ++ return obj->kind == ESYNC_KIND_MUTEX && (DWORD)__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ) == tid; ++} ++ ++static BOOL ready_now( struct esync_obj *obj, DWORD tid ) ++{ ++ int state = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ return (DWORD)state == tid || (mutex_free( state ) && fd_readable( obj->fd )); ++ case ESYNC_KIND_SEMAPHORE: ++ return state > 0; ++ case ESYNC_KIND_AUTO: ++ return fd_readable( obj->fd ); ++ case ESYNC_KIND_MANUAL: ++ return state != 0; ++ } ++ return FALSE; ++} ++ ++static NTSTATUS grab_all( DWORD count, struct esync_obj *objs, DWORD tid ) ++{ ++ BOOL grabbed[MAXIMUM_WAIT_OBJECTS], owned[MAXIMUM_WAIT_OBJECTS], abandoned = FALSE; ++ DWORD order[MAXIMUM_WAIT_OBJECTS], n = 0, i, j; ++ int current, prev[MAXIMUM_WAIT_OBJECTS]; ++ NTSTATUS status = STATUS_PENDING; ++ sigset_t old; ++ ++ for (i = 0; i < count; i++) ++ if (!ready_now( &objs[i], tid )) return STATUS_PENDING; ++ for (i = 0; i < count; i++) ++ { ++ grabbed[i] = owned[i] = FALSE; ++ if (objs[i].kind != ESYNC_KIND_AUTO) order[n++] = i; ++ } ++ for (i = 0; i < count; i++) ++ if (objs[i].kind == ESYNC_KIND_AUTO) order[n++] = i; ++ ++ block_signals( &old ); ++ for (j = 0; j < count; j++) ++ { ++ struct esync_obj *obj = &objs[i = order[j]]; ++ ++ switch (obj->kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ if ((DWORD)current == tid) ++ { ++ if (__atomic_load_n( &obj->slot->aux, __ATOMIC_SEQ_CST ) == INT_MAX) ++ { ++ status = STATUS_MUTANT_LIMIT_EXCEEDED; ++ goto rollback; ++ } ++ owned[i] = TRUE; ++ break; ++ } ++ prev[i] = current; ++ if (!claim_mutex( obj, tid, &prev[i] )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ current = __atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST ); ++ do if (current <= 0) goto rollback; ++ while (!__atomic_compare_exchange_n( &obj->slot->state, ¤t, current - 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (!consume_token( obj->fd )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_AUTO: ++ if (!take_auto( obj )) goto rollback; ++ grabbed[i] = TRUE; ++ break; ++ case ESYNC_KIND_MANUAL: ++ if (!__atomic_load_n( &obj->slot->state, __ATOMIC_SEQ_CST )) goto rollback; ++ break; ++ } ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ struct esync_slot *slot = objs[i].slot; ++ ++ if (owned[i]) ++ { ++ current = __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ); ++ swap_pair( slot, tid, current, tid, current + 1 ); ++ } ++ else if (grabbed[i] && objs[i].kind == ESYNC_KIND_MUTEX) ++ { ++ if ((DWORD)prev[i] == ~0u) abandoned = TRUE; ++ hold_mutex( &objs[i], tid ); ++ swap_pair( slot, tid, __atomic_load_n( &slot->aux, __ATOMIC_SEQ_CST ), tid, 1 ); ++ } ++ } ++ restore_signals( &old ); ++ waits.held_count = 0; ++ return abandoned ? STATUS_ABANDONED : STATUS_SUCCESS; ++ ++rollback: ++ while (j--) ++ { ++ i = order[j]; ++ if (!grabbed[i]) continue; ++ switch (objs[i].kind) ++ { ++ case ESYNC_KIND_MUTEX: ++ unclaim_mutex( &objs[i], tid, prev[i] ); ++ break; ++ case ESYNC_KIND_SEMAPHORE: ++ current = __atomic_load_n( &objs[i].slot->state, __ATOMIC_SEQ_CST ); ++ do if (current >= objs[i].slot->aux) break; ++ while (!__atomic_compare_exchange_n( &objs[i].slot->state, ¤t, current + 1, FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )); ++ if (current < objs[i].slot->aux) wake_fd( objs[i].fd, 1 ); ++ break; ++ case ESYNC_KIND_AUTO: ++ wake_fd( objs[i].fd, 1 ); ++ __atomic_store_n( &objs[i].slot->state, 1, __ATOMIC_SEQ_CST ); ++ break; ++ case ESYNC_KIND_MANUAL: ++ break; ++ } ++ } ++ restore_signals( &old ); ++ return status; ++} ++ ++static NTSTATUS wait_all( DWORD count, struct esync_obj *objs, int alert_fd, const struct esync_deadline *end, BOOL track ) ++{ ++ struct pollfd fds[2]; ++ DWORD tid = GetCurrentThreadId(), i, j; ++ NTSTATUS status; ++ nfds_t nfds; ++ int ret; ++ ++ for (i = 0; i < count; i++) ++ for (j = i + 1; j < count; j++) ++ if (objs[i].slot == objs[j].slot) return STATUS_INVALID_PARAMETER; ++ ++ for (;;) ++ { ++ if ((status = grab_all( count, objs, tid )) != STATUS_PENDING) return status; ++ if (track) ++ { ++ add_waits( count, objs ); ++ track = FALSE; ++ continue; ++ } ++ ++ for (i = 0; i < count; i++) ++ { ++ if (owned_mutex( &objs[i], tid )) continue; ++ fds[0].fd = objs[i].fd; ++ fds[0].events = POLLIN; ++ fds[0].revents = 0; ++ nfds = 1; ++ if (alert_fd >= 0) ++ { ++ fds[1].fd = alert_fd; ++ fds[1].events = POLLIN; ++ fds[1].revents = 0; ++ nfds = 2; ++ } ++ if ((ret = do_poll( fds, nfds, end )) < 0) return errno_to_status( errno ); ++ if (!ret) return STATUS_TIMEOUT; ++ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) return STATUS_INVALID_HANDLE; ++ if (!(fds[0].revents & POLLIN) && nfds == 2 && (fds[1].revents & POLLIN)) ++ { ++ if ((status = grab_all( count, objs, tid )) != STATUS_PENDING) return status; ++ if ((status = user_apc()) != STATUS_TIMEOUT) return status; ++ break; ++ } ++ } ++ if (i == count) ++ { ++ if (expired( end )) return STATUS_TIMEOUT; ++ sched_yield(); ++ } ++ } ++} ++ ++NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ) ++{ ++ struct esync_obj objs[MAXIMUM_WAIT_OBJECTS]; ++ struct esync_deadline end; ++ DWORD i, base = waits.count; ++ BOOL track = FALSE; ++ NTSTATUS ret; ++ ++ assert( count <= MAXIMUM_WAIT_OBJECTS ); ++ for (i = 0; i < count; i++) ++ { ++ if (!(objs[i].slot = get_slot( shm_idxs[i] ))) return STATUS_INVALID_HANDLE; ++ objs[i].fd = fds[i]; ++ switch (types[i]) ++ { ++ case INPROC_SYNC_SEMAPHORE: ++ objs[i].kind = ESYNC_KIND_SEMAPHORE; ++ break; ++ case INPROC_SYNC_MUTEX: ++ objs[i].kind = ESYNC_KIND_MUTEX; ++ break; ++ default: ++ objs[i].kind = (objs[i].slot->flags & ESYNC_MANUAL) ? ESYNC_KIND_MANUAL : ESYNC_KIND_AUTO; ++ track = TRUE; ++ break; ++ } ++ } ++ ++ get_deadline( timeout, &end ); ++ if (timeout && !timeout->QuadPart) track = FALSE; ++ if (type != WaitAll || count <= 1) ret = wait_any( count, objs, alert_fd, &end, track ); ++ else ret = wait_all( count, objs, alert_fd, &end, track ); ++ remove_waits( base ); ++ return ret; ++} ++ ++#else ++ ++void esync_init( int fd ) ++{ ++} ++ ++void esync_release( unsigned int shm_idx ) ++{ ++} ++ ++NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ) ++{ ++ return STATUS_NOT_IMPLEMENTED; ++} ++ ++#endif +diff --git a/dlls/ntdll/unix/esync.h b/dlls/ntdll/unix/esync.h +new file mode 100644 +index 0000000..afa19fc +--- /dev/null ++++ b/dlls/ntdll/unix/esync.h +@@ -0,0 +1,20 @@ ++#define ESYNC_USED_BY_SERVER 0x7eadeeef ++#define ESYNC_SHM_TOKEN 0x7eadeef0 ++ ++static inline int do_esync(void) ++{ ++ return inproc_device_fd == ESYNC_USED_BY_SERVER; ++} ++ ++extern void esync_init( int shm_fd ); ++extern void esync_release( unsigned int shm_idx ); ++extern NTSTATUS esync_release_semaphore( int fd, unsigned int shm_idx, ULONG count, ULONG *prev ); ++extern NTSTATUS esync_query_semaphore( int fd, unsigned int shm_idx, SEMAPHORE_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_set_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_reset_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_pulse_event( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_query_event( int fd, unsigned int shm_idx, EVENT_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_release_mutex( int fd, unsigned int shm_idx, LONG *prev ); ++extern NTSTATUS esync_query_mutex( int fd, unsigned int shm_idx, MUTANT_BASIC_INFORMATION *info ); ++extern NTSTATUS esync_wait( DWORD count, const int *fds, const unsigned int *shm_idxs, const unsigned short *types, ++ WAIT_TYPE type, int alert_fd, const LARGE_INTEGER *timeout ); +diff --git a/dlls/ntdll/unix/server.c b/dlls/ntdll/unix/server.c +index 112157b..864c2f9 100644 +--- a/dlls/ntdll/unix/server.c ++++ b/dlls/ntdll/unix/server.c +@@ -81,6 +81,7 @@ + #include "unix_private.h" + #include "ddk/wdm.h" + ++#include "esync.h" + #include "fsync.h" + + WINE_DEFAULT_DEBUG_CHANNEL(server); +@@ -1703,6 +1704,14 @@ size_t server_init_process(void) + inproc_device_fd = FSYNC_USED_BY_SERVER; + fsync_init( pid ); + } ++ else if (reply->inproc_device == ESYNC_USED_BY_SERVER) ++ { ++ int shm_fd = wine_server_receive_fd( &handle ); ++ assert( handle == ESYNC_SHM_TOKEN ); ++ if (shm_fd < 0) fatal_error( "droiddeck-esync: no shared memory from wineserver\n" ); ++ inproc_device_fd = ESYNC_USED_BY_SERVER; ++ esync_init( shm_fd ); ++ } + else if (reply->inproc_device) + { + inproc_device_fd = wine_server_receive_fd( &handle ); +diff --git a/dlls/ntdll/unix/sync.c b/dlls/ntdll/unix/sync.c +index 67ab945..f44a526 100644 +--- a/dlls/ntdll/unix/sync.c ++++ b/dlls/ntdll/unix/sync.c +@@ -72,6 +72,7 @@ + #include "wine/debug.h" + #include "unix_private.h" + ++#include "esync.h" + #include "fsync.h" + + WINE_DEFAULT_DEBUG_CHANNEL(sync); +@@ -542,9 +543,10 @@ struct inproc_sync + unsigned int access; /* handle access rights */ + unsigned short type; /* enum inproc_sync_type as short to save space */ + unsigned short closed; /* fd has been closed but sync is still referenced */ ++ unsigned int shm_idx; + }; + +-#define INPROC_SYNC_CACHE_BLOCK_SIZE (65536 / sizeof(struct inproc_sync)) ++#define INPROC_SYNC_CACHE_BLOCK_SIZE 4096 + #define INPROC_SYNC_CACHE_ENTRIES 128 + + static struct inproc_sync *inproc_sync_cache[INPROC_SYNC_CACHE_ENTRIES]; +@@ -582,7 +584,8 @@ static struct inproc_sync *cache_inproc_sync( HANDLE handle, struct inproc_sync + if (!entry) inproc_sync_cache[0] = inproc_sync_cache_initial_block; + else + { +- static const size_t size = INPROC_SYNC_CACHE_BLOCK_SIZE * sizeof(struct inproc_sync); ++ size_t page = sysconf( _SC_PAGESIZE ); ++ size_t size = (INPROC_SYNC_CACHE_BLOCK_SIZE * sizeof(struct inproc_sync) + page - 1) & ~(page - 1); + void *ptr = anon_mmap_alloc( size, PROT_READ | PROT_WRITE ); + if (ptr == MAP_FAILED) return sync; + inproc_sync_cache[entry] = ptr; +@@ -604,6 +607,7 @@ static struct inproc_sync *cache_inproc_sync( HANDLE handle, struct inproc_sync + cache->access = sync->access; + cache->type = sync->type; + cache->closed = sync->closed; ++ cache->shm_idx = sync->shm_idx; + /* Make sure we set the other members before the refcount; this store needs + * release semantics [paired with the load in get_cached_inproc_sync()]. + * Set the refcount to 2 (one for the handle, one for the caller). */ +@@ -633,10 +637,13 @@ static void release_inproc_sync( struct inproc_sync *sync ) + /* save the fd now; as soon as the refcount hits 0 we cannot + * access the cache anymore */ + int fd = sync->fd; ++ unsigned int shm_idx = sync->shm_idx; + LONG ref = InterlockedDecrement( &sync->refcount ); + + assert( ref >= 0 ); +- if (!ref) close( fd ); ++ if (ref) return; ++ if (fd >= 0) close( fd ); ++ if (shm_idx) esync_release( shm_idx ); + } + + static struct inproc_sync *get_cached_inproc_sync( HANDLE handle ) +@@ -682,10 +689,19 @@ static NTSTATUS get_server_inproc_sync( HANDLE handle, struct inproc_sync *sync + sync->access = reply->access; + sync->type = reply->type; + sync->closed = 0; ++ sync->shm_idx = do_esync() ? reply->fsync_shm_idx : 0; + } + } + SERVER_END_REQ; + ++ if (!ret && do_esync() && (sync->fd < 0 || !sync->shm_idx)) ++ { ++ ERR( "droiddeck-esync: cannot receive fd for handle %p, out of file descriptors?\n", handle ); ++ if (sync->fd >= 0) close( sync->fd ); ++ esync_release( sync->shm_idx ); ++ memset( sync, 0, sizeof(*sync) ); ++ ret = STATUS_TOO_MANY_OPENED_FILES; ++ } + return ret; + } + +@@ -789,7 +805,8 @@ static NTSTATUS inproc_release_semaphore( HANDLE handle, ULONG count, ULONG *pre + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_SEMAPHORE, SEMAPHORE_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_release_semaphore_obj( sync->fd, count, prev_count ); ++ if (do_esync()) ret = esync_release_semaphore( sync->fd, sync->shm_idx, count, prev_count ); ++ else ret = linux_release_semaphore_obj( sync->fd, count, prev_count ); + release_inproc_sync( sync ); + return ret; + } +@@ -803,7 +820,8 @@ static NTSTATUS inproc_query_semaphore( HANDLE handle, SEMAPHORE_BASIC_INFORMATI + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_SEMAPHORE, SEMAPHORE_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_semaphore_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_semaphore( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_semaphore_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -817,7 +835,8 @@ static NTSTATUS inproc_set_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_set_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_set_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_set_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -831,7 +850,8 @@ static NTSTATUS inproc_reset_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_reset_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_reset_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_reset_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -845,7 +865,8 @@ static NTSTATUS inproc_pulse_event( HANDLE handle, LONG *prev_state ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_MODIFY_STATE, &stack, &sync ))) return ret; +- ret = linux_pulse_event_obj( sync->fd, prev_state ); ++ if (do_esync()) ret = esync_pulse_event( sync->fd, sync->shm_idx, prev_state ); ++ else ret = linux_pulse_event_obj( sync->fd, prev_state ); + release_inproc_sync( sync ); + return ret; + } +@@ -859,7 +880,8 @@ static NTSTATUS inproc_query_event( HANDLE handle, EVENT_BASIC_INFORMATION *info + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_EVENT, EVENT_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_event_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_event( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_event_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -873,7 +895,8 @@ static NTSTATUS inproc_release_mutex( HANDLE handle, LONG *prev_count ) + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_MUTEX, 0, &stack, &sync ))) return ret; +- ret = linux_release_mutex_obj( sync->fd, prev_count ); ++ if (do_esync()) ret = esync_release_mutex( sync->fd, sync->shm_idx, prev_count ); ++ else ret = linux_release_mutex_obj( sync->fd, prev_count ); + release_inproc_sync( sync ); + return ret; + } +@@ -887,7 +910,8 @@ static NTSTATUS inproc_query_mutex( HANDLE handle, MUTANT_BASIC_INFORMATION *inf + + if (inproc_device_fd < 0) return STATUS_NOT_IMPLEMENTED; + if ((ret = get_inproc_sync( handle, INPROC_SYNC_MUTEX, MUTANT_QUERY_STATE, &stack, &sync ))) return ret; +- ret = linux_query_mutex_obj( sync->fd, info ); ++ if (do_esync()) ret = esync_query_mutex( sync->fd, sync->shm_idx, info ); ++ else ret = linux_query_mutex_obj( sync->fd, info ); + release_inproc_sync( sync ); + return ret; + } +@@ -928,6 +952,8 @@ static NTSTATUS inproc_wait( DWORD count, const HANDLE *handles, WAIT_TYPE type, + { + struct inproc_sync *syncs[64], stack[ARRAY_SIZE(syncs)]; + int objs[ARRAY_SIZE(syncs)], alert_fd = 0; ++ unsigned int shm_idxs[ARRAY_SIZE(syncs)]; ++ unsigned short types[ARRAY_SIZE(syncs)]; + NTSTATUS ret; + + if (do_fsync()) return fsync_wait_objects( count, handles, type != WaitAll, alertable, timeout ); +@@ -944,6 +970,17 @@ static NTSTATUS inproc_wait( DWORD count, const HANDLE *handles, WAIT_TYPE type, + return ret; + } + objs[i] = syncs[i]->fd; ++ shm_idxs[i] = syncs[i]->shm_idx; ++ types[i] = syncs[i]->type; ++ } ++ ++ if (do_esync()) ++ { ++ alert_fd = alertable ? get_inproc_alert_fd() : -1; ++ if (alertable && alert_fd < 0) ret = STATUS_TOO_MANY_OPENED_FILES; ++ else ret = esync_wait( count, objs, shm_idxs, types, type, alert_fd, timeout ); ++ while (count--) release_inproc_sync( syncs[count] ); ++ return ret; + } + + if (alertable) alert_fd = get_inproc_alert_fd(); +@@ -969,6 +1006,25 @@ static NTSTATUS inproc_signal_and_wait( HANDLE signal, HANDLE wait, + + if ((ret = get_inproc_sync( wait, INPROC_SYNC_UNKNOWN, SYNCHRONIZE, &stack_wait, &wait_sync ))) goto done; + ++ if (do_esync()) ++ { ++ switch (signal_sync->type) ++ { ++ case INPROC_SYNC_EVENT: ret = esync_set_event( signal_sync->fd, signal_sync->shm_idx, NULL ); break; ++ case INPROC_SYNC_MUTEX: ret = esync_release_mutex( signal_sync->fd, signal_sync->shm_idx, NULL ); break; ++ case INPROC_SYNC_SEMAPHORE: ret = esync_release_semaphore( signal_sync->fd, signal_sync->shm_idx, 1, NULL ); break; ++ default: assert( 0 ); break; ++ } ++ if (!ret) ++ { ++ alert_fd = alertable ? get_inproc_alert_fd() : -1; ++ if (alertable && alert_fd < 0) ret = STATUS_TOO_MANY_OPENED_FILES; ++ else ret = esync_wait( 1, &wait_sync->fd, &wait_sync->shm_idx, &wait_sync->type, WaitAny, alert_fd, timeout ); ++ } ++ release_inproc_sync( wait_sync ); ++ goto done; ++ } ++ + switch (signal_sync->type) + { + case INPROC_SYNC_EVENT: ret = linux_set_event_obj( signal_sync->fd, NULL ); break; +@@ -2461,6 +2517,20 @@ NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeou + if (ret != STATUS_NOT_IMPLEMENTED) + return ret; + } ++ else if (do_esync()) ++ { ++ int alert_fd = get_inproc_alert_fd(); ++ ++ if (alert_fd >= 0) ++ { ++ if ((status = esync_wait( 0, NULL, NULL, NULL, WaitAny, alert_fd, timeout )) == STATUS_TIMEOUT) ++ { ++ NtYieldExecution(); ++ status = STATUS_SUCCESS; ++ } ++ return status; ++ } ++ } + /* Since server_wait will result in an unconditional implicit yield, + we never return STATUS_NO_YIELD_PERFORMED */ + if ((status = server_wait( NULL, 0, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE, timeout )) == STATUS_TIMEOUT) +diff --git a/server/Makefile.in b/server/Makefile.in +index d9ac607..b370a0b 100644 +--- a/server/Makefile.in ++++ b/server/Makefile.in +@@ -12,6 +12,7 @@ SOURCES = \ + debugger.c \ + device.c \ + directory.c \ ++ esync.c \ + event.c \ + fd.c \ + file.c \ +diff --git a/server/esync.c b/server/esync.c +new file mode 100644 +index 0000000..508a189 +--- /dev/null ++++ b/server/esync.c +@@ -0,0 +1,525 @@ ++/* droiddeck-esync: eventfd and shared memory synchronization for Wine. ++ * Credit: based on the esync design by Elizabeth Figura for Wine and Proton. */ ++#include "config.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#ifdef HAVE_SYS_EVENTFD_H ++# include ++#endif ++#include ++ ++#include "ntstatus.h" ++#define WIN32_NO_STATUS ++#include "windef.h" ++#include "winternl.h" ++ ++#include "handle.h" ++#include "request.h" ++#include "esync.h" ++ ++#define ESYNC_CHUNK_SIZE 0x10000 ++#define ESYNC_SLOT_SIZE 16 ++#define ESYNC_PER_CHUNK (ESYNC_CHUNK_SIZE / ESYNC_SLOT_SIZE) ++#define ESYNC_CHUNKS 8192 ++#define ESYNC_MANUAL 1 ++#define ESYNC_RELEASING 1 ++ ++struct esync_slot ++{ ++ union ++ { ++ struct ++ { ++ int state; ++ int aux; ++ }; ++ int64_t pair; ++ }; ++ int flags; ++ int ref; ++}; ++ ++union esync_pair ++{ ++ struct { int state; int aux; } s; ++ int64_t word; ++}; ++ ++struct holder ++{ ++ process_id_t pid; ++ unsigned int count; ++ struct holder *next; ++}; ++ ++int esync_active; ++ ++static int shm_fd = -1; ++static unsigned int shm_chunks; ++static void *chunks[ESYNC_CHUNKS]; ++static struct holder *holders[ESYNC_CHUNKS]; ++static uint64_t *free_map; ++static unsigned int free_words; ++static unsigned int free_hint; ++ ++#if defined(HAVE_SYS_EVENTFD_H) && defined(HAVE_MEMFD_CREATE) ++ ++static void *map_chunk( unsigned int chunk ) ++{ ++ void *addr; ++ ++ if (chunk >= ESYNC_CHUNKS) return NULL; ++ if (chunks[chunk]) return chunks[chunk]; ++ if (!holders[chunk] && !(holders[chunk] = calloc( ESYNC_PER_CHUNK, sizeof(struct holder) ))) return NULL; ++ if (chunk >= shm_chunks) ++ { ++ if (ftruncate( shm_fd, (off_t)(chunk + 1) * ESYNC_CHUNK_SIZE ) == -1) ++ { ++ perror( "droiddeck-esync: ftruncate" ); ++ return NULL; ++ } ++ shm_chunks = chunk + 1; ++ } ++ addr = mmap( NULL, ESYNC_CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, ++ (off_t)chunk * ESYNC_CHUNK_SIZE ); ++ if (addr == MAP_FAILED) ++ { ++ perror( "droiddeck-esync: mmap" ); ++ return NULL; ++ } ++ return chunks[chunk] = addr; ++} ++ ++static struct esync_slot *get_slot( unsigned int idx ) ++{ ++ char *base = chunks[idx / ESYNC_PER_CHUNK]; ++ return (struct esync_slot *)(base + (idx % ESYNC_PER_CHUNK) * ESYNC_SLOT_SIZE); ++} ++ ++static struct holder *get_holder( unsigned int idx ) ++{ ++ return &holders[idx / ESYNC_PER_CHUNK][idx % ESYNC_PER_CHUNK]; ++} ++ ++static void clear_holders( unsigned int idx ) ++{ ++ struct holder *head = get_holder( idx ), *next; ++ ++ while ((next = head->next)) ++ { ++ head->next = next->next; ++ free( next ); ++ } ++ head->pid = 0; ++ head->count = 0; ++} ++ ++static unsigned int alloc_slot(void) ++{ ++ unsigned int i, bit, idx; ++ ++ for (i = free_hint; i < free_words; i++) if (free_map[i]) break; ++ if (i == free_words) ++ { ++ unsigned int words = free_words * 2; ++ uint64_t *map; ++ ++ if (words > ESYNC_CHUNKS * (ESYNC_PER_CHUNK / 64)) words = ESYNC_CHUNKS * (ESYNC_PER_CHUNK / 64); ++ if (words == free_words) return 0; ++ if (!(map = realloc( free_map, words * sizeof(*map) ))) return 0; ++ memset( map + free_words, 0xff, (words - free_words) * sizeof(*map) ); ++ free_map = map; ++ free_words = words; ++ } ++ bit = __builtin_ctzll( free_map[i] ); ++ idx = i * 64 + bit; ++ if (!map_chunk( idx / ESYNC_PER_CHUNK )) return 0; ++ free_map[i] &= ~((uint64_t)1 << bit); ++ free_hint = i; ++ return idx; ++} ++ ++static void free_slot( unsigned int idx ) ++{ ++ clear_holders( idx ); ++ free_map[idx / 64] |= (uint64_t)1 << (idx % 64); ++ if (idx / 64 < free_hint) free_hint = idx / 64; ++} ++ ++static int slot_in_use( unsigned int idx ) ++{ ++ if (!idx || idx / 64 >= free_words) return 0; ++ return !(free_map[idx / 64] & ((uint64_t)1 << (idx % 64))); ++} ++ ++static void put_ref( unsigned int idx ) ++{ ++ struct esync_slot *slot = get_slot( idx ); ++ ++ if (slot->ref > 0 && !--slot->ref) free_slot( idx ); ++} ++ ++static void report_fd_error( const char *what ) ++{ ++ static int warned; ++ struct rlimit limit; ++ ++ if (warned) return; ++ warned = 1; ++ if ((errno == EMFILE || errno == ENFILE) && !getrlimit( RLIMIT_NOFILE, &limit )) ++ fprintf( stderr, "droiddeck-esync: out of file descriptors in %s (limit %llu); set PROTON_NO_ESYNC=1 for this game if it fails.\n", ++ what, (unsigned long long)limit.rlim_cur ); ++ else ++ fprintf( stderr, "droiddeck-esync: %s failed: %s\n", what, strerror( errno )); ++} ++ ++static void wake_fd( int fd ) ++{ ++ static const uint64_t value = 1; ++ if (fd >= 0 && write( fd, &value, sizeof(value) ) == -1 && errno != EAGAIN) perror( "droiddeck-esync: write" ); ++} ++ ++static void drain_fd( int fd ) ++{ ++ uint64_t value; ++ if (fd >= 0 && read( fd, &value, sizeof(value) ) == -1 && errno != EAGAIN) perror( "droiddeck-esync: read" ); ++} ++ ++static int fd_readable( int fd ) ++{ ++ struct pollfd pfd = { .fd = fd, .events = POLLIN }; ++ ++ return fd >= 0 && poll( &pfd, 1, 0 ) > 0 && (pfd.revents & POLLIN); ++} ++ ++static int owner_dead( int owner ) ++{ ++ return owner > 0 && kill( owner, 0 ) == -1 && errno == ESRCH; ++} ++ ++static void repair_slot( struct esync_slot *slot, int fd ) ++{ ++ int readable; ++ ++ if (fd < 0) return; ++ readable = fd_readable( fd ); ++ if (__atomic_load_n( &slot->state, __ATOMIC_SEQ_CST )) ++ { ++ if (!readable) wake_fd( fd ); ++ } ++ else if (readable) drain_fd( fd ); ++} ++ ++static void lock_slot( struct esync_slot *slot, int fd ) ++{ ++ int self = getpid(), owner; ++ unsigned int spins = 0; ++ ++ for (;;) ++ { ++ owner = 0; ++ if (__atomic_compare_exchange_n( &slot->aux, &owner, self, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) return; ++ if (++spins < 128) continue; ++ if (!(spins & 1023) && owner_dead( owner ) && ++ __atomic_compare_exchange_n( &slot->aux, &owner, self, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED )) ++ { ++ repair_slot( slot, fd ); ++ return; ++ } ++ sched_yield(); ++ } ++} ++ ++static void unlock_slot( struct esync_slot *slot ) ++{ ++ int self = getpid(); ++ __atomic_compare_exchange_n( &slot->aux, &self, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED ); ++} ++ ++int esync_init(void) ++{ ++ const char *env = getenv( "WINEESYNC" ); ++ struct rlimit limit; ++ int probe; ++ ++ if (!env || !atoi( env )) return 0; ++ if ((probe = eventfd( 0, EFD_CLOEXEC | EFD_NONBLOCK )) == -1) ++ { ++ perror( "droiddeck-esync: eventfd" ); ++ return 0; ++ } ++ close( probe ); ++ if ((shm_fd = memfd_create( "droiddeck-esync", MFD_CLOEXEC )) == -1) ++ { ++ perror( "droiddeck-esync: memfd_create" ); ++ return 0; ++ } ++ free_words = 64; ++ if (!(free_map = malloc( free_words * sizeof(*free_map) )) || !map_chunk( 0 )) ++ { ++ free( free_map ); ++ free_map = NULL; ++ free_words = 0; ++ close( shm_fd ); ++ shm_fd = -1; ++ return 0; ++ } ++ memset( free_map, 0xff, free_words * sizeof(*free_map) ); ++ free_map[0] &= ~(uint64_t)1; ++ esync_active = 1; ++ fprintf( stderr, "droiddeck-esync: up and running.\n" ); ++ if (!getrlimit( RLIMIT_NOFILE, &limit )) ++ fprintf( stderr, "droiddeck-esync: file descriptor limit %llu.\n", (unsigned long long)limit.rlim_cur ); ++ return 1; ++} ++ ++int esync_shm_fd(void) ++{ ++ return shm_fd; ++} ++ ++int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ) ++{ ++ struct esync_slot *slot; ++ unsigned int idx; ++ int fd = ESYNC_NO_FD; ++ ++ if (!lazy && (fd = eventfd( initval, EFD_CLOEXEC | EFD_NONBLOCK | (semaphore ? EFD_SEMAPHORE : 0) )) == -1) ++ { ++ report_fd_error( "eventfd" ); ++ return -1; ++ } ++ if (!(idx = alloc_slot())) ++ { ++ if (fd >= 0) close( fd ); ++ return -1; ++ } ++ slot = get_slot( idx ); ++ slot->state = state; ++ slot->aux = aux; ++ slot->flags = manual ? ESYNC_MANUAL : 0; ++ __atomic_store_n( &slot->ref, 1, __ATOMIC_SEQ_CST ); ++ *shm_idx = idx; ++ return fd; ++} ++ ++int esync_open( unsigned int shm_idx ) ++{ ++ int fd; ++ ++ if (!slot_in_use( shm_idx )) return -1; ++ if ((fd = eventfd( __atomic_load_n( &get_slot( shm_idx )->state, __ATOMIC_SEQ_CST ) ? 1 : 0, ++ EFD_CLOEXEC | EFD_NONBLOCK )) == -1) ++ report_fd_error( "eventfd" ); ++ return fd; ++} ++ ++void esync_destroy( int fd, unsigned int shm_idx ) ++{ ++ if (fd >= 0) close( fd ); ++ if (slot_in_use( shm_idx )) put_ref( shm_idx ); ++} ++ ++unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ) ++{ ++ struct holder *head, *node; ++ ++ if (!slot_in_use( shm_idx )) return 0; ++ head = get_holder( shm_idx ); ++ for (node = head; node; node = node->next) ++ { ++ if (node->count && node->pid == pid) ++ { ++ node->count++; ++ get_slot( shm_idx )->ref++; ++ return shm_idx; ++ } ++ } ++ if (!head->count) ++ { ++ head->pid = pid; ++ head->count = 1; ++ } ++ else ++ { ++ if (!(node = malloc( sizeof(*node) ))) return 0; ++ node->pid = pid; ++ node->count = 1; ++ node->next = head->next; ++ head->next = node; ++ } ++ get_slot( shm_idx )->ref++; ++ return shm_idx; ++} ++ ++static unsigned int drop_holder( unsigned int idx, process_id_t pid, int all ) ++{ ++ struct holder *head = get_holder( idx ), *prev = NULL, *node, *next; ++ unsigned int dropped; ++ ++ for (node = head; node; prev = node, node = node->next) ++ if (node->count && node->pid == pid) break; ++ if (!node) return 0; ++ dropped = all ? node->count : 1; ++ if ((node->count -= dropped)) return dropped; ++ if (node != head) ++ { ++ prev->next = node->next; ++ free( node ); ++ } ++ else if ((next = head->next)) ++ { ++ *head = *next; ++ free( next ); ++ } ++ else head->pid = 0; ++ return dropped; ++} ++ ++void esync_client_release( process_id_t pid, unsigned int shm_idx ) ++{ ++ if (!slot_in_use( shm_idx )) return; ++ if (drop_holder( shm_idx, pid, 0 )) put_ref( shm_idx ); ++} ++ ++void esync_cleanup_process( process_id_t pid ) ++{ ++ unsigned int i, idx, count; ++ uint64_t used; ++ ++ for (i = 0; i < free_words; i++) ++ { ++ used = ~free_map[i]; ++ while (used) ++ { ++ idx = i * 64 + __builtin_ctzll( used ); ++ used &= used - 1; ++ if (!idx || !slot_in_use( idx )) continue; ++ for (count = drop_holder( idx, pid, 1 ); count && slot_in_use( idx ); count--) put_ref( idx ); ++ } ++ } ++} ++ ++void esync_set( int fd, unsigned int shm_idx, int locked ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!(slot->flags & ESYNC_MANUAL)) ++ { ++ wake_fd( fd ); ++ __atomic_store_n( &slot->state, 1, __ATOMIC_SEQ_CST ); ++ return; ++ } ++ if (locked) lock_slot( slot, fd ); ++ if (!__atomic_exchange_n( &slot->state, 1, __ATOMIC_SEQ_CST )) wake_fd( fd ); ++ if (locked) unlock_slot( slot ); ++} ++ ++void esync_reset( int fd, unsigned int shm_idx, int locked ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ ++ if (!(slot->flags & ESYNC_MANUAL)) ++ { ++ __atomic_store_n( &slot->state, 0, __ATOMIC_SEQ_CST ); ++ drain_fd( fd ); ++ return; ++ } ++ if (locked) lock_slot( slot, fd ); ++ if (__atomic_exchange_n( &slot->state, 0, __ATOMIC_SEQ_CST )) drain_fd( fd ); ++ if (locked) unlock_slot( slot ); ++} ++ ++void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ) ++{ ++ struct esync_slot *slot = get_slot( shm_idx ); ++ union esync_pair cur, next = { .s = { ~0, 0 } }; ++ ++ cur.word = __atomic_load_n( &slot->pair, __ATOMIC_SEQ_CST ); ++ for (;;) ++ { ++ if (cur.s.state != (int)tid && ++ (!releasing || cur.s.state != (int)(tid | ESYNC_RELEASING) || fd_readable( fd ))) return; ++ if (__atomic_compare_exchange_n( &slot->pair, &cur.word, next.word, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) break; ++ } ++ wake_fd( fd ); ++} ++ ++thread_id_t esync_mutex_owner( unsigned int shm_idx ) ++{ ++ int owner; ++ ++ if (!slot_in_use( shm_idx )) return 0; ++ owner = __atomic_load_n( &get_slot( shm_idx )->state, __ATOMIC_SEQ_CST ); ++ return owner == ~0 ? 0 : owner & ~ESYNC_RELEASING; ++} ++ ++#else ++ ++int esync_init(void) ++{ ++ return 0; ++} ++ ++int esync_shm_fd(void) ++{ ++ return -1; ++} ++ ++int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ) ++{ ++ errno = ENOSYS; ++ return -1; ++} ++ ++int esync_open( unsigned int shm_idx ) ++{ ++ return -1; ++} ++ ++void esync_destroy( int fd, unsigned int shm_idx ) ++{ ++} ++ ++unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ) ++{ ++ return 0; ++} ++ ++void esync_client_release( process_id_t pid, unsigned int shm_idx ) ++{ ++} ++ ++void esync_cleanup_process( process_id_t pid ) ++{ ++} ++ ++void esync_set( int fd, unsigned int shm_idx, int locked ) ++{ ++} ++ ++void esync_reset( int fd, unsigned int shm_idx, int locked ) ++{ ++} ++ ++void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ) ++{ ++} ++ ++thread_id_t esync_mutex_owner( unsigned int shm_idx ) ++{ ++ return 0; ++} ++ ++#endif +diff --git a/server/esync.h b/server/esync.h +new file mode 100644 +index 0000000..b94565c +--- /dev/null ++++ b/server/esync.h +@@ -0,0 +1,22 @@ ++#define ESYNC_USED_BY_SERVER 0x7eadeeef ++#define ESYNC_SHM_TOKEN 0x7eadeef0 ++#define ESYNC_NO_FD (-2) ++ ++extern int esync_active; ++static inline int do_esync(void) ++{ ++ return esync_active; ++} ++ ++extern int esync_init(void); ++extern int esync_shm_fd(void); ++extern int esync_create( int semaphore, int manual, unsigned int initval, int state, int aux, int lazy, unsigned int *shm_idx ); ++extern int esync_open( unsigned int shm_idx ); ++extern void esync_destroy( int fd, unsigned int shm_idx ); ++extern unsigned int esync_grab( process_id_t pid, unsigned int shm_idx ); ++extern void esync_client_release( process_id_t pid, unsigned int shm_idx ); ++extern void esync_cleanup_process( process_id_t pid ); ++extern void esync_set( int fd, unsigned int shm_idx, int locked ); ++extern void esync_reset( int fd, unsigned int shm_idx, int locked ); ++extern void esync_abandon_mutex( int fd, unsigned int shm_idx, thread_id_t tid, int releasing ); ++extern thread_id_t esync_mutex_owner( unsigned int shm_idx ); +diff --git a/server/fsync.c b/server/fsync.c +index 7d3ec64..f4d9ac9 100644 +--- a/server/fsync.c ++++ b/server/fsync.c +@@ -43,6 +43,9 @@ + + #include "handle.h" + #include "request.h" ++#include "process.h" ++#include "thread.h" ++#include "esync.h" + #include "fsync.h" + + #include "pshpack4.h" +@@ -359,6 +362,11 @@ void fsync_abandon_mutex( unsigned int shm_idx, thread_id_t tid ) + + DECL_HANDLER(fsync_free_shm_idx) + { ++ if (do_esync()) ++ { ++ esync_client_release( get_process_id( current->process ), req->shm_idx ); ++ return; ++ } + if (!req->shm_idx || req->shm_idx >= shm_idx_free_map_size * BITS_IN_FREE_MAP_WORD) + { + set_error( STATUS_INVALID_PARAMETER ); +diff --git a/server/inproc_sync.c b/server/inproc_sync.c +index a7020ef..b555837 100644 +--- a/server/inproc_sync.c ++++ b/server/inproc_sync.c +@@ -23,6 +23,7 @@ + #include + #include + #include ++#include + + #include "ntstatus.h" + #define WIN32_NO_STATUS +@@ -31,15 +32,18 @@ + #include "file.h" + #include "handle.h" + #include "request.h" ++#include "process.h" + #include "thread.h" + #include "user.h" + ++#include "esync.h" + #include "fsync.h" + + #include "ntsync_tmp.h" + + #ifdef NTSYNC_IOC_EVENT_READ + ++#include + #include + #include + #include +@@ -57,9 +61,10 @@ int get_inproc_device_fd(void) + if (fd >= 0) + { + do_fsync_cached = 0; +- fprintf( stderr, "ntsync: up and running.\n" ); ++ fprintf( stderr, "droiddeck-ntsync: up and running.\n" ); + } + else if (do_fsync()) fd = FSYNC_USED_BY_SERVER; ++ else if (esync_init()) fd = ESYNC_USED_BY_SERVER; + else fprintf( stderr, "wineserver: using server-side synchronization.\n" ); + } + return fd; +@@ -71,6 +76,7 @@ struct inproc_sync + enum inproc_sync_type type; + int fd; + struct list entry; ++ unsigned int shm_idx; + }; + + static struct list inproc_mutexes = LIST_INIT( inproc_mutexes ); +@@ -107,6 +113,12 @@ static const struct object_ops inproc_sync_ops = + int get_inproc_sync_fd( struct inproc_sync *sync ) + { + if (!sync) return -1; ++ if (do_esync() && sync->fd == ESYNC_NO_FD && (sync->fd = esync_open( sync->shm_idx )) == -1) ++ { ++ sync->fd = ESYNC_NO_FD; ++ set_error( STATUS_TOO_MANY_OPENED_FILES ); ++ return -1; ++ } + return sync->fd; + } + +@@ -116,11 +128,17 @@ struct inproc_sync *create_inproc_internal_sync( int manual, int signaled ) + struct inproc_sync *event; + + if (!(event = alloc_object( &inproc_sync_ops ))) return NULL; ++ event->shm_idx = 0; + if (do_fsync()) + { + event->type = manual ? FSYNC_MANUAL_SERVER : FSYNC_AUTO_SERVER; + event->fd = fsync_alloc_shm( signaled, 0xdeadbeef ); + } ++ else if (do_esync()) ++ { ++ event->type = INPROC_SYNC_INTERNAL; ++ event->fd = esync_create( 0, manual, !!signaled, !!signaled, 0, 1, &event->shm_idx ); ++ } + else + { + event->type = INPROC_SYNC_INTERNAL; +@@ -143,11 +161,17 @@ struct inproc_sync *create_inproc_event_sync( int manual, int signaled ) + struct inproc_sync *event; + + if (!(event = alloc_object( &inproc_sync_ops ))) return NULL; ++ event->shm_idx = 0; + if (do_fsync()) + { + event->type = manual ? FSYNC_MANUAL_EVENT : FSYNC_AUTO_EVENT; + event->fd = fsync_alloc_shm( signaled, 0xdeadbeef ); + } ++ else if (do_esync()) ++ { ++ event->type = INPROC_SYNC_EVENT; ++ event->fd = esync_create( 0, manual, !!signaled, !!signaled, 0, 0, &event->shm_idx ); ++ } + else + { + event->type = INPROC_SYNC_EVENT; +@@ -170,11 +194,17 @@ struct inproc_sync *create_inproc_mutex_sync( thread_id_t owner, unsigned int co + struct inproc_sync *mutex; + + if (!(mutex = alloc_object( &inproc_sync_ops ))) return NULL; ++ mutex->shm_idx = 0; + if (do_fsync()) + { + mutex->type = FSYNC_MUTEX; + mutex->fd = fsync_alloc_shm( owner, count ); + } ++ else if (do_esync()) ++ { ++ mutex->type = INPROC_SYNC_MUTEX; ++ mutex->fd = esync_create( 0, 0, owner ? 0 : 1, owner, owner ? count : 0, 0, &mutex->shm_idx ); ++ } + else + { + mutex->type = INPROC_SYNC_MUTEX; +@@ -197,12 +227,18 @@ struct inproc_sync *create_inproc_semaphore_sync( unsigned int initial, unsigned + struct inproc_sync *sem; + + if (!(sem = alloc_object( &inproc_sync_ops ))) return NULL; ++ sem->shm_idx = 0; + + if (do_fsync()) + { + sem->type = FSYNC_SEMAPHORE; + sem->fd = fsync_alloc_shm( initial, max ); + } ++ else if (do_esync()) ++ { ++ sem->type = INPROC_SYNC_SEMAPHORE; ++ sem->fd = esync_create( 1, 0, initial, initial, max, 0, &sem->shm_idx ); ++ } + else + { + sem->type = INPROC_SYNC_SEMAPHORE; +@@ -231,6 +267,7 @@ void signal_inproc_sync( struct inproc_sync *sync ) + __u32 count; + if (debug_level) fprintf( stderr, "set_inproc_event %d\n", sync->fd ); + if (do_fsync()) fsync_set_event( sync->fd ); ++ else if (do_esync()) esync_set( sync->fd, sync->shm_idx, sync->type == INPROC_SYNC_EVENT ); + else ioctl( sync->fd, NTSYNC_IOC_EVENT_SET, &count ); + } + +@@ -239,6 +276,7 @@ void reset_inproc_sync( struct inproc_sync *sync ) + __u32 count; + if (debug_level) fprintf( stderr, "reset_inproc_event %d\n", sync->fd ); + if (do_fsync()) fsync_reset_event( sync->fd ); ++ else if (do_esync()) esync_reset( sync->fd, sync->shm_idx, sync->type == INPROC_SYNC_EVENT ); + else ioctl( sync->fd, NTSYNC_IOC_EVENT_RESET, &count ); + } + +@@ -262,6 +300,7 @@ static void inproc_sync_destroy( struct object *obj ) + assert( obj->ops == &inproc_sync_ops ); + list_remove( &sync->entry ); + if (do_fsync()) fsync_free_shm_idx( sync->fd ); ++ else if (do_esync()) esync_destroy( sync->fd, sync->shm_idx ); + else close( sync->fd ); + } + +@@ -275,12 +314,97 @@ void abandon_inproc_mutexes( thread_id_t tid ) + fsync_abandon_mutex( mutex->fd, tid ); + return; + } ++ if (do_esync()) ++ { ++ LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) ++ esync_abandon_mutex( mutex->fd, mutex->shm_idx, tid, 0 ); ++ return; ++ } + + LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) + ioctl( mutex->fd, NTSYNC_IOC_MUTEX_KILL, &tid ); + } + +-static int get_obj_inproc_sync( struct object *obj, int *type ) ++struct mutex_reaper ++{ ++ struct list entry; ++ struct thread *thread; ++ struct timeout_user *timeout; ++ unsigned int checks; ++}; ++ ++static struct list mutex_reapers = LIST_INIT( mutex_reapers ); ++ ++static void finish_reaper( struct mutex_reaper *reaper ) ++{ ++ if (reaper->timeout) remove_timeout_user( reaper->timeout ); ++ list_remove( &reaper->entry ); ++ abandon_inproc_mutexes( reaper->thread->id ); ++ release_object( reaper->thread ); ++ free( reaper ); ++} ++ ++static void reap_inproc_mutexes( void *private ) ++{ ++ struct mutex_reaper *reaper = private; ++ ++ reaper->timeout = NULL; ++ if (++reaper->checks < 20 && send_thread_signal( reaper->thread, 0 ) && ++ (reaper->timeout = add_timeout_user( -TICKS_PER_SEC / 20, reap_inproc_mutexes, reaper ))) ++ return; ++ finish_reaper( reaper ); ++} ++ ++void abandon_inproc_mutexes_later( struct thread *thread ) ++{ ++ struct mutex_reaper *reaper; ++ ++ if (list_empty( &inproc_mutexes ) || thread->process->is_terminating) return; ++ if (!(reaper = malloc( sizeof(*reaper) ))) return; ++ reaper->thread = (struct thread *)grab_object( thread ); ++ reaper->checks = 0; ++ if (!(reaper->timeout = add_timeout_user( -TICKS_PER_SEC / 100, reap_inproc_mutexes, reaper ))) ++ { ++ release_object( thread ); ++ free( reaper ); ++ return; ++ } ++ list_add_tail( &mutex_reapers, &reaper->entry ); ++} ++ ++void abandon_dead_inproc_mutexes( struct process *process ) ++{ ++ unsigned int error = get_error(); ++ struct mutex_reaper *reaper, *next; ++ struct ntsync_mutex_args args; ++ struct inproc_sync *mutex; ++ struct thread *thread; ++ thread_id_t owner; ++ int dead, gone; ++ ++ LIST_FOR_EACH_ENTRY_SAFE( reaper, next, &mutex_reapers, struct mutex_reaper, entry ) ++ if (reaper->thread->process == process) finish_reaper( reaper ); ++ if (!do_fsync()) ++ { ++ LIST_FOR_EACH_ENTRY( mutex, &inproc_mutexes, struct inproc_sync, entry ) ++ { ++ if (do_esync()) owner = esync_mutex_owner( mutex->shm_idx ); ++ else if (ioctl( mutex->fd, NTSYNC_IOC_MUTEX_READ, &args ) == -1 && errno != EOWNERDEAD) continue; ++ else owner = args.owner; ++ if (!owner) continue; ++ thread = get_thread_from_id( owner ); ++ dead = !thread || thread->state == TERMINATED; ++ gone = !thread || thread->process == process; ++ if (thread) release_object( thread ); ++ if (!dead) continue; ++ if (do_esync()) esync_abandon_mutex( mutex->fd, mutex->shm_idx, owner, gone ); ++ else ioctl( mutex->fd, NTSYNC_IOC_MUTEX_KILL, &owner ); ++ } ++ } ++ set_error( error ); ++} ++ ++static int get_obj_inproc_sync( struct object *obj, int *type, unsigned int *shm_idx ) + { + struct object *sync; + int fd = -1; +@@ -290,7 +414,8 @@ static int get_obj_inproc_sync( struct object *obj, int *type ) + { + struct inproc_sync *inproc = (struct inproc_sync *)sync; + *type = inproc->type; +- fd = inproc->fd; ++ *shm_idx = inproc->shm_idx; ++ fd = get_inproc_sync_fd( inproc ); + } + + release_object( sync ); +@@ -341,7 +466,15 @@ void abandon_inproc_mutexes( thread_id_t tid ) + { + } + +-static int get_obj_inproc_sync( struct object *obj, int *type ) ++void abandon_inproc_mutexes_later( struct thread *thread ) ++{ ++} ++ ++void abandon_dead_inproc_mutexes( struct process *process ) ++{ ++} ++ ++static int get_obj_inproc_sync( struct object *obj, int *type, unsigned int *shm_idx ) + { + return -1; + } +@@ -351,14 +484,23 @@ static int get_obj_inproc_sync( struct object *obj, int *type ) + DECL_HANDLER(get_inproc_sync_fd) + { + struct object *obj; ++ unsigned int shm_idx = 0; + int fd; + + if (!(obj = get_handle_obj( current->process, req->handle, 0, NULL ))) return; + + reply->access = get_handle_access( current->process, req->handle ); + +- if ((fd = get_obj_inproc_sync( obj, &reply->type )) < 0) set_error( STATUS_NOT_IMPLEMENTED ); ++ if ((fd = get_obj_inproc_sync( obj, &reply->type, &shm_idx )) < 0) ++ { ++ if (!get_error()) set_error( STATUS_NOT_IMPLEMENTED ); ++ } + else if (do_fsync()) reply->fsync_shm_idx = fsync_grab_shm_idx( fd ); ++ else if (do_esync()) ++ { ++ if (!(reply->fsync_shm_idx = esync_grab( get_process_id( current->process ), shm_idx ))) set_error( STATUS_NO_MEMORY ); ++ else send_client_fd( current->process, fd, req->handle ); ++ } + else send_client_fd( current->process, fd, req->handle ); + + release_object( obj ); +diff --git a/server/object.h b/server/object.h +index 21f5ea8..cd21fb7 100644 +--- a/server/object.h ++++ b/server/object.h +@@ -255,6 +255,8 @@ extern struct inproc_sync *create_inproc_event_sync( int manual, int signaled ); + extern struct inproc_sync *create_inproc_semaphore_sync( unsigned int initial, unsigned int max ); + extern struct inproc_sync *create_inproc_mutex_sync( thread_id_t owner, unsigned int count ); + extern void abandon_inproc_mutexes( thread_id_t owner ); ++extern void abandon_inproc_mutexes_later( struct thread *thread ); ++extern void abandon_dead_inproc_mutexes( struct process *process ); + extern void signal_inproc_sync( struct inproc_sync *sync ); + extern void reset_inproc_sync( struct inproc_sync *sync ); + +diff --git a/server/process.c b/server/process.c +index 287718a..5963884 100644 +--- a/server/process.c ++++ b/server/process.c +@@ -65,6 +65,7 @@ + #include "user.h" + #include "security.h" + ++#include "esync.h" + #include "fsync.h" + + /* process object */ +@@ -618,6 +619,8 @@ static void process_died( struct process *process ) + if (!--user_processes && !shutdown_stage && master_socket_timeout != TIMEOUT_INFINITE) + shutdown_timeout = add_timeout_user( master_socket_timeout, server_shutdown_timeout, NULL ); + } ++ abandon_dead_inproc_mutexes( process ); ++ if (do_esync()) esync_cleanup_process( process->id ); + release_object( process ); + if (!--running_processes && shutdown_stage) close_master_socket( 0 ); + } +diff --git a/server/thread.c b/server/thread.c +index 435d12a..f35a788 100644 +--- a/server/thread.c ++++ b/server/thread.c +@@ -61,6 +61,7 @@ + #include "user.h" + #include "security.h" + ++#include "esync.h" + #include "fsync.h" + + /* thread queues */ +@@ -1658,7 +1659,11 @@ void kill_thread( struct thread *thread, int violent_death ) + kill_console_processes( thread, 0 ); + abandon_mutexes( thread ); + signal_sync( thread->sync ); +- if (violent_death) send_thread_signal( thread, SIGQUIT ); ++ if (violent_death) ++ { ++ send_thread_signal( thread, SIGQUIT ); ++ abandon_inproc_mutexes_later( thread ); ++ } + cleanup_thread( thread ); + remove_process_thread( thread->process, thread ); + release_object( thread ); +@@ -1836,7 +1841,12 @@ DECL_HANDLER(init_first_thread) + { + reply->inproc_device = FSYNC_USED_BY_SERVER; + } +- else if ((fd = get_inproc_device_fd()) >= 0) ++ else if ((fd = get_inproc_device_fd()) == ESYNC_USED_BY_SERVER) ++ { ++ reply->inproc_device = ESYNC_USED_BY_SERVER; ++ send_client_fd( process, esync_shm_fd(), ESYNC_SHM_TOKEN ); ++ } ++ else if (fd >= 0) + { + reply->inproc_device = get_process_id( process ) | 1; + send_client_fd( process, fd, reply->inproc_device ); +@@ -2478,7 +2488,10 @@ DECL_HANDLER(get_inproc_alert_fd) + { + int fd; + +- if ((fd = get_inproc_sync_fd( current->alert_sync )) < 0) set_error( STATUS_INVALID_PARAMETER ); ++ if ((fd = get_inproc_sync_fd( current->alert_sync )) < 0) ++ { ++ if (!get_error()) set_error( STATUS_INVALID_PARAMETER ); ++ } + else if (do_fsync()) reply->fsync_shm_idx = fd; + else + { diff --git a/tools/droiddeck-esync/revoked.txt b/tools/droiddeck-esync/revoked.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tools/droiddeck-esync/smoke-test.sh b/tools/droiddeck-esync/smoke-test.sh new file mode 100755 index 000000000..994f60bda --- /dev/null +++ b/tools/droiddeck-esync/smoke-test.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -euo pipefail + +if [ $# -ne 4 ]; then + echo "usage: $0 " >&2 + exit 2 +fi +tool=$(realpath "$1") +archive=$(realpath "$2") +exe=$(realpath "$3") +work=$(realpath -m "$4") +here=$(cd "$(dirname "$0")" && pwd) +guest=$here/../linuxfs/overlay/usr/local/bin/droiddeck-esync +rm -rf "$work" +mkdir -p "$work/pack" +tar --zstd -xf "$archive" -C "$work/pack" +python3 "$guest" build-shadow "$tool" "$work/pack" "$work/dist" >/dev/null +[ -e "$work/dist/files/bin" ] || ln -s bin-arm64 "$work/dist/files/bin" +export WINEDEBUG=-all WINEDLLOVERRIDES="mscoree,mshtml=" + +run() { + local mode=$1 bin=$2 want=$3 + shift 3 + local prefix=$work/prefix-$mode + mkdir -p "$prefix" + ( + export WINEPREFIX=$prefix WINESERVER=$bin/wineserver "$@" + timeout "${BOOT_TIMEOUT:-900}" "$bin/wine" wineboot -i > "$work/$mode-boot.log" 2>&1 || true + "$bin/wineserver" -w || true + if ! grep -q "$want" "$work/$mode-boot.log"; then + echo "$mode: wine never said \"$want\"" + tail -n 30 "$work/$mode-boot.log" + exit 1 + fi + cp "$exe" "$prefix/drive_c/esstress.exe" + status=0 + timeout "${STRESS_TIMEOUT:-1800}" "$bin/wine" 'C:\esstress.exe' > "$work/$mode-stress.log" 2>&1 || status=$? + "$bin/wineserver" -k 2>/dev/null || true + if ! grep -q '^esstress:' "$work/$mode-stress.log"; then + echo "$mode: esstress did not finish (status $status)" + tail -n 30 "$work/$mode-stress.log" + exit 1 + fi + grep -o '^FAIL [^ ]*:[0-9]*:' "$work/$mode-stress.log" | sort -u > "$work/$mode-failures" || true + echo "$mode: $(grep -E '^esstress:' "$work/$mode-stress.log" | tail -n 1)" + ) +} + +if ! run stock "$tool/files/bin-arm64" "server-side synchronization" PROTON_NO_NTSYNC=1 WINEESYNC=0 WINEFSYNC=0; then + echo "stock Proton does not run here either; the pack cannot be judged on this machine" + exit 3 +fi +run esync "$work/dist/files/bin-arm64" "esync: up and running" PROTON_NO_NTSYNC=1 WINEESYNC=1 WINEFSYNC=0 +if [ -s "$work/stock-failures" ]; then + echo "stock fails these as well, so they do not count against the pack: $(tr '\n' ' ' < "$work/stock-failures")" +fi +new=$(comm -13 "$work/stock-failures" "$work/esync-failures") +if [ -n "$new" ]; then + echo "esync: fails checks stock passes:" + grep -F "$new" "$work/esync-stress.log" || echo "$new" + exit 1 +fi diff --git a/tools/droiddeck-esync/tests/esstress.c b/tools/droiddeck-esync/tests/esstress.c new file mode 100644 index 000000000..7400f221b --- /dev/null +++ b/tools/droiddeck-esync/tests/esstress.c @@ -0,0 +1,618 @@ +#include +#include +#include +#include + +static int failures; +#define CHECK(c, ...) do { if (!(c)) { failures++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); fflush(stdout); } } while (0) + +static volatile LONG counter; +static HANDLE mutex, sem, ev_auto, ev_manual, start_ev; +static volatile LONG stop; + +static DWORD WINAPI mutex_worker(void *arg) +{ + int i; + WaitForSingleObject(start_ev, INFINITE); + for (i = 0; i < 20000; i++) + { + DWORD r = WaitForSingleObject(mutex, INFINITE); + LONG v; + if (r != WAIT_OBJECT_0) { CHECK(0, "mutex wait %lu", r); break; } + v = counter; + if (i % 7 == 0) WaitForSingleObject(mutex, 0), ReleaseMutex(mutex); + counter = v + 1; + ReleaseMutex(mutex); + } + return 0; +} + +static void test_mutex_contention(void) +{ + HANDLE th[8]; + int i; + counter = 0; + mutex = CreateMutexA(NULL, FALSE, NULL); + start_ev = CreateEventA(NULL, TRUE, FALSE, NULL); + for (i = 0; i < 8; i++) th[i] = CreateThread(NULL, 0, mutex_worker, NULL, 0, NULL); + SetEvent(start_ev); + WaitForMultipleObjects(8, th, TRUE, INFINITE); + CHECK(counter == 8 * 20000, "mutex counter %ld", counter); + for (i = 0; i < 8; i++) CloseHandle(th[i]); + CloseHandle(mutex); + CloseHandle(start_ev); + printf("mutex_contention ok counter=%ld\n", counter); +} + +static volatile LONG produced, consumed; + +static DWORD WINAPI sem_producer(void *arg) +{ + int i; + for (i = 0; i < 50000; i++) + { + LONG prev; + while (!ReleaseSemaphore(sem, 1, &prev)) + { + CHECK(GetLastError() == ERROR_TOO_MANY_POSTS, "release err %lu", GetLastError()); + Sleep(0); + } + InterlockedIncrement(&produced); + } + return 0; +} + +static DWORD WINAPI sem_consumer(void *arg) +{ + for (;;) + { + DWORD r = WaitForSingleObject(sem, 2000); + if (r == WAIT_TIMEOUT) break; + CHECK(r == WAIT_OBJECT_0, "sem wait %lu", r); + InterlockedIncrement(&consumed); + } + return 0; +} + +static void test_semaphore(void) +{ + HANDLE th[8]; + LONG prev; + int i; + produced = consumed = 0; + sem = CreateSemaphoreA(NULL, 0, 16, NULL); + for (i = 0; i < 4; i++) th[i] = CreateThread(NULL, 0, sem_producer, NULL, 0, NULL); + for (i = 4; i < 8; i++) th[i] = CreateThread(NULL, 0, sem_consumer, NULL, 0, NULL); + WaitForMultipleObjects(8, th, TRUE, INFINITE); + CHECK(produced == 200000 && consumed == 200000, "sem produced %ld consumed %ld", produced, consumed); + CHECK(!ReleaseSemaphore(sem, 17, &prev) && GetLastError() == ERROR_TOO_MANY_POSTS, "limit"); + CHECK(ReleaseSemaphore(sem, 16, &prev) && prev == 0, "prev %ld", prev); + CHECK(!ReleaseSemaphore(sem, 1, &prev), "over max"); + for (i = 0; i < 8; i++) CloseHandle(th[i]); + CloseHandle(sem); + printf("semaphore ok %ld/%ld\n", produced, consumed); +} + +static volatile LONG auto_hits; + +static DWORD WINAPI auto_waiter(void *arg) +{ + while (!stop) + { + DWORD r = WaitForSingleObject(ev_auto, 50); + if (r == WAIT_OBJECT_0) InterlockedIncrement(&auto_hits); + } + return 0; +} + +static void test_auto_event(void) +{ + HANDLE th[6]; + int i; + LONG expected = 0; + stop = 0; auto_hits = 0; + ev_auto = CreateEventA(NULL, FALSE, FALSE, NULL); + for (i = 0; i < 6; i++) th[i] = CreateThread(NULL, 0, auto_waiter, NULL, 0, NULL); + for (i = 0; i < 30000; i++) + { + LONG before = auto_hits; + SetEvent(ev_auto); + expected++; + while (auto_hits == before) SwitchToThread(); + } + stop = 1; + WaitForMultipleObjects(6, th, TRUE, INFINITE); + CHECK(auto_hits == expected, "auto hits %ld expected %ld", auto_hits, expected); + CHECK(WaitForSingleObject(ev_auto, 0) == WAIT_TIMEOUT, "auto event left signaled"); + SetEvent(ev_auto); SetEvent(ev_auto); + CHECK(WaitForSingleObject(ev_auto, 0) == WAIT_OBJECT_0, "auto set"); + CHECK(WaitForSingleObject(ev_auto, 0) == WAIT_TIMEOUT, "auto coalesce"); + for (i = 0; i < 6; i++) CloseHandle(th[i]); + CloseHandle(ev_auto); + printf("auto_event ok hits=%ld\n", auto_hits); +} + +static volatile LONG manual_seen; + +static DWORD WINAPI manual_waiter(void *arg) +{ + DWORD r = WaitForSingleObject(ev_manual, 10000); + if (r == WAIT_OBJECT_0) InterlockedIncrement(&manual_seen); + return 0; +} + +static void test_manual_event(void) +{ + HANDLE th[16]; + int i, round; + ev_manual = CreateEventA(NULL, TRUE, FALSE, NULL); + for (round = 0; round < 50; round++) + { + manual_seen = 0; + ResetEvent(ev_manual); + for (i = 0; i < 16; i++) th[i] = CreateThread(NULL, 0, manual_waiter, NULL, 0, NULL); + Sleep(2); + SetEvent(ev_manual); + WaitForMultipleObjects(16, th, TRUE, INFINITE); + CHECK(manual_seen == 16, "manual round %d seen %ld", round, manual_seen); + for (i = 0; i < 16; i++) CloseHandle(th[i]); + } + for (i = 0; i < 20000; i++) + { + SetEvent(ev_manual); + if (i & 1) ResetEvent(ev_manual); + } + CHECK(WaitForSingleObject(ev_manual, 0) == WAIT_TIMEOUT, "manual final reset"); + SetEvent(ev_manual); + CHECK(WaitForSingleObject(ev_manual, 0) == WAIT_OBJECT_0, "manual stays"); + CHECK(WaitForSingleObject(ev_manual, 0) == WAIT_OBJECT_0, "manual stays 2"); + CloseHandle(ev_manual); + printf("manual_event ok\n"); +} + +static HANDLE wa_objs[3]; +static volatile LONG wa_count; + +static DWORD WINAPI wait_all_worker(void *arg) +{ + int i; + for (i = 0; i < 3000; i++) + { + DWORD r = WaitForMultipleObjects(3, wa_objs, TRUE, 5000); + if (r != WAIT_OBJECT_0) { CHECK(0, "wait all %lu", r); break; } + InterlockedIncrement(&wa_count); + ReleaseMutex(wa_objs[0]); + ReleaseSemaphore(wa_objs[1], 1, NULL); + SetEvent(wa_objs[2]); + } + return 0; +} + +static void test_wait_all(void) +{ + HANDLE th[6], dup[2]; + int i; + DWORD r; + wa_count = 0; + wa_objs[0] = CreateMutexA(NULL, FALSE, NULL); + wa_objs[1] = CreateSemaphoreA(NULL, 1, 1, NULL); + wa_objs[2] = CreateEventA(NULL, FALSE, TRUE, NULL); + for (i = 0; i < 6; i++) th[i] = CreateThread(NULL, 0, wait_all_worker, NULL, 0, NULL); + WaitForMultipleObjects(6, th, TRUE, INFINITE); + CHECK(wa_count == 18000, "wait all count %ld", wa_count); + dup[0] = wa_objs[2]; + DuplicateHandle(GetCurrentProcess(), wa_objs[2], GetCurrentProcess(), &dup[1], 0, FALSE, DUPLICATE_SAME_ACCESS); + r = WaitForMultipleObjects(2, dup, TRUE, 0); + CHECK(r == WAIT_FAILED && GetLastError() == ERROR_INVALID_PARAMETER, "dup wait all %lu %lu", r, GetLastError()); + CloseHandle(dup[1]); + for (i = 0; i < 6; i++) CloseHandle(th[i]); + for (i = 0; i < 3; i++) CloseHandle(wa_objs[i]); + printf("wait_all ok %ld\n", wa_count); +} + +static DWORD WINAPI hold_and_die(void *arg) +{ + WaitForSingleObject((HANDLE)arg, INFINITE); + return 0; +} + +static void test_abandoned(void) +{ + HANDLE m = CreateMutexA(NULL, FALSE, NULL); + HANDLE th = CreateThread(NULL, 0, hold_and_die, m, 0, NULL); + HANDLE objs[2]; + DWORD r; + WaitForSingleObject(th, INFINITE); + r = WaitForSingleObject(m, 1000); + CHECK(r == WAIT_ABANDONED, "abandoned %lu", r); + CHECK(ReleaseMutex(m), "release after abandon"); + th = CreateThread(NULL, 0, hold_and_die, m, 0, NULL); + WaitForSingleObject(th, INFINITE); + objs[0] = CreateEventA(NULL, TRUE, TRUE, NULL); + objs[1] = m; + r = WaitForMultipleObjects(2, objs, TRUE, 1000); + CHECK(r == WAIT_ABANDONED_0, "abandoned all %lu", r); + ReleaseMutex(m); + r = WaitForMultipleObjects(2, objs, FALSE, 1000); + CHECK(r == WAIT_OBJECT_0, "any after %lu", r); + CloseHandle(th); CloseHandle(objs[0]); CloseHandle(m); + printf("abandoned ok\n"); +} + +static volatile LONG apc_ran; +static void CALLBACK apc_proc(ULONG_PTR p) { InterlockedIncrement(&apc_ran); } + +static HANDLE apc_ready; + +static DWORD WINAPI alert_waiter(void *arg) +{ + SetEvent(apc_ready); + return WaitForSingleObjectEx((HANDLE)arg, 5000, TRUE); +} + +static void test_apc(void) +{ + HANDLE ev = CreateEventA(NULL, TRUE, FALSE, NULL); + HANDLE th; + DWORD r, code; + int i; + apc_ready = CreateEventA(NULL, FALSE, FALSE, NULL); + for (i = 0; i < 200; i++) + { + apc_ran = 0; + th = CreateThread(NULL, 0, alert_waiter, ev, 0, NULL); + WaitForSingleObject(apc_ready, INFINITE); + if (i & 1) Sleep(1); + QueueUserAPC(apc_proc, th, 0); + WaitForSingleObject(th, INFINITE); + GetExitCodeThread(th, &code); + CHECK(code == WAIT_IO_COMPLETION && apc_ran == 1, "apc round %d code %lu ran %ld", i, code, apc_ran); + CloseHandle(th); + } + apc_ran = 0; + QueueUserAPC(apc_proc, GetCurrentThread(), 0); + r = SleepEx(1000, TRUE); + CHECK(r == WAIT_IO_COMPLETION && apc_ran == 1, "sleepex %lu %ld", r, apc_ran); + r = SleepEx(10, TRUE); + CHECK(r == 0, "sleepex timeout %lu", r); + QueueUserAPC(apc_proc, GetCurrentThread(), 0); + r = WaitForSingleObjectEx(ev, 0, FALSE); + CHECK(r == WAIT_TIMEOUT, "non alertable %lu", r); + r = WaitForSingleObjectEx(ev, 0, TRUE); + CHECK(r == WAIT_IO_COMPLETION, "alertable pending %lu", r); + CloseHandle(ev); + printf("apc ok\n"); +} + +static DWORD WINAPI close_waiter(void *arg) +{ + return WaitForSingleObject((HANDLE)arg, 300); +} + +static void test_close_while_waiting(void) +{ + int i; + for (i = 0; i < 300; i++) + { + HANDLE ev = CreateEventA(NULL, TRUE, FALSE, NULL), ev2, th; + DWORD code; + th = CreateThread(NULL, 0, close_waiter, ev, 0, NULL); + Sleep(0); + CloseHandle(ev); + ev2 = CreateEventA(NULL, TRUE, TRUE, NULL); + WaitForSingleObject(th, INFINITE); + GetExitCodeThread(th, &code); + CHECK(code == WAIT_TIMEOUT || code == WAIT_FAILED || code == WAIT_OBJECT_0, "close round %d code %lu", i, code); + CHECK(WaitForSingleObject(ev2, 0) == WAIT_OBJECT_0, "reused handle state"); + CloseHandle(ev2); + CloseHandle(th); + } + printf("close_while_waiting ok\n"); +} + +static void test_pulse(void) +{ + HANDLE ev = CreateEventA(NULL, TRUE, FALSE, NULL), th[4]; + DWORD code; + int i, woke = 0; + for (i = 0; i < 4; i++) th[i] = CreateThread(NULL, 0, close_waiter, ev, 0, NULL); + Sleep(50); + PulseEvent(ev); + WaitForMultipleObjects(4, th, TRUE, INFINITE); + for (i = 0; i < 4; i++) { GetExitCodeThread(th[i], &code); if (code == WAIT_OBJECT_0) woke++; CloseHandle(th[i]); } + CHECK(WaitForSingleObject(ev, 0) == WAIT_TIMEOUT, "pulse left signaled"); + CHECK(woke == 4, "pulse woke %d of 4", woke); + CloseHandle(ev); + ev = CreateEventA(NULL, FALSE, FALSE, NULL); + PulseEvent(ev); + CHECK(WaitForSingleObject(ev, 0) == WAIT_TIMEOUT, "auto pulse without waiter left signaled"); + th[0] = CreateThread(NULL, 0, close_waiter, ev, 0, NULL); + th[1] = CreateThread(NULL, 0, close_waiter, ev, 0, NULL); + Sleep(50); + PulseEvent(ev); + WaitForMultipleObjects(2, th, TRUE, INFINITE); + woke = 0; + for (i = 0; i < 2; i++) { GetExitCodeThread(th[i], &code); if (code == WAIT_OBJECT_0) woke++; CloseHandle(th[i]); } + CHECK(woke == 1, "auto pulse woke %d of 2", woke); + CHECK(WaitForSingleObject(ev, 0) == WAIT_TIMEOUT, "auto pulse left signaled"); + CloseHandle(ev); + printf("pulse ok\n"); +} + +static void test_semantics(void) +{ + HANDLE ev = CreateEventA(NULL, TRUE, TRUE, NULL), sem = CreateSemaphoreA(NULL, 2, 3, NULL), h[2]; + NTSTATUS (WINAPI *pNtWaitForSingleObject)(HANDLE, BOOLEAN, const LARGE_INTEGER *); + LARGE_INTEGER when; + LONG prev = -1; + DWORD r, start; + int i; + pNtWaitForSingleObject = (void *)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtWaitForSingleObject"); + apc_ran = 0; + QueueUserAPC(apc_proc, GetCurrentThread(), 0); + r = WaitForSingleObjectEx(ev, INFINITE, TRUE); + CHECK(r == WAIT_OBJECT_0 && apc_ran == 0, "object before apc %lu ran %ld", r, apc_ran); + r = SleepEx(0, TRUE); + CHECK(r == WAIT_IO_COMPLETION && apc_ran == 1, "apc after object %lu ran %ld", r, apc_ran); + r = SleepEx(0, TRUE); + CHECK(r == 0, "sleepex zero %lu", r); + ResetEvent(ev); + GetSystemTimeAsFileTime((FILETIME *)&when); + when.QuadPart += 2000000; + start = GetTickCount(); + r = pNtWaitForSingleObject(ev, FALSE, &when); + CHECK(r == STATUS_TIMEOUT, "absolute timeout status %lx", r); + CHECK(GetTickCount() - start >= 150 && GetTickCount() - start < 1500, "absolute timeout took %lu", GetTickCount() - start); + CHECK(!ReleaseSemaphore(sem, 2, &prev) && GetLastError() == ERROR_TOO_MANY_POSTS, "semaphore limit"); + CHECK(ReleaseSemaphore(sem, 1, &prev) && prev == 2, "semaphore prev %ld", prev); + for (i = 0; i < 3; i++) CHECK(WaitForSingleObject(sem, 0) == WAIT_OBJECT_0, "semaphore take %d", i); + CHECK(WaitForSingleObject(sem, 0) == WAIT_TIMEOUT, "semaphore empty"); + h[0] = sem; h[1] = ev; + ReleaseSemaphore(sem, 1, NULL); + CHECK(WaitForMultipleObjects(2, h, TRUE, 50) == WAIT_TIMEOUT, "wait all partial"); + CHECK(ReleaseSemaphore(sem, 0, &prev) && prev == 1, "wait all rollback lost token prev %ld", prev); + SetEvent(ev); + CHECK(WaitForMultipleObjects(2, h, TRUE, 50) == WAIT_OBJECT_0, "wait all ready"); + CHECK(ReleaseSemaphore(sem, 0, &prev) && prev == 0, "wait all consumed prev %ld", prev); + CHECK(WaitForSingleObject(ev, 0) == WAIT_OBJECT_0, "manual stays set"); + CloseHandle(sem); + CloseHandle(ev); + printf("semantics ok\n"); +} + +static HANDLE token_sem; +static volatile LONG token_count; + +static DWORD WINAPI token_worker(void *arg) +{ + HANDLE h[2] = { token_sem, (HANDLE)arg }; + int i; + for (i = 0; i < 20000; i++) + { + DWORD r = (i & 1) ? WaitForSingleObject(token_sem, INFINITE) : WaitForMultipleObjects(2, h, TRUE, INFINITE); + if (r != WAIT_OBJECT_0) { CHECK(0, "token wait %lu", r); break; } + if (InterlockedIncrement(&token_count) > 3) CHECK(0, "token overcommit"); + InterlockedDecrement(&token_count); + if (!(i & 1)) SetEvent((HANDLE)arg); + ReleaseSemaphore(token_sem, 1, NULL); + } + return 0; +} + +static void test_semaphore_tokens(void) +{ + HANDLE th[6], ev = CreateEventA(NULL, FALSE, TRUE, NULL); + LONG prev = -1; + int i; + token_sem = CreateSemaphoreA(NULL, 3, 3, NULL); + for (i = 0; i < 6; i++) th[i] = CreateThread(NULL, 0, token_worker, ev, 0, NULL); + WaitForMultipleObjects(6, th, TRUE, INFINITE); + for (i = 0; i < 6; i++) CloseHandle(th[i]); + CHECK(ReleaseSemaphore(token_sem, 0, &prev) && prev == 3, "token final count %ld", prev); + for (i = 0; i < 3; i++) CHECK(WaitForSingleObject(token_sem, 0) == WAIT_OBJECT_0, "token drain %d", i); + CHECK(WaitForSingleObject(token_sem, 0) == WAIT_TIMEOUT, "token extra"); + CHECK(WaitForSingleObject(ev, 0) == WAIT_OBJECT_0, "token event lost"); + CloseHandle(token_sem); + CloseHandle(ev); + printf("semaphore_tokens ok\n"); +} + +static volatile LONG churn_ready; + +static DWORD WINAPI churn_mutex(void *arg) +{ + HANDLE h[2] = { (HANDLE)arg, ev_manual }; + unsigned int i = 0; + if (WaitForMultipleObjects(2, h, TRUE, INFINITE) <= WAIT_ABANDONED_0 + 1) ReleaseMutex(h[0]); + InterlockedIncrement(&churn_ready); + for (;;) + { + DWORD r = (i++ & 1) ? WaitForSingleObject(h[0], INFINITE) : WaitForMultipleObjects(2, h, TRUE, INFINITE); + if (r == WAIT_OBJECT_0 || r == WAIT_ABANDONED || r == WAIT_ABANDONED_0) ReleaseMutex(h[0]); + } + return 0; +} + +static DWORD WINAPI blocked_waiter(void *arg) +{ + WaitForSingleObject((HANDLE)arg, 0); + InterlockedIncrement(&churn_ready); + return WaitForSingleObject((HANDLE)arg, INFINITE); +} + +static void test_terminate(void) +{ + HANDLE th[4], m, ev; + DWORD r, start; + int round, i, stuck = 0; + ev_manual = CreateEventA(NULL, TRUE, TRUE, NULL); + for (round = 0; round < 150; round++) + { + m = CreateMutexA(NULL, FALSE, NULL); + churn_ready = 0; + for (i = 0; i < 4; i++) th[i] = CreateThread(NULL, 0, churn_mutex, m, 0, NULL); + while (churn_ready < 4) Sleep(0); + Sleep(round % 4); + for (i = 0; i < 4; i++) TerminateThread(th[i], 7); + WaitForMultipleObjects(4, th, TRUE, INFINITE); + r = WaitForSingleObject(m, 3000); + if (r == WAIT_TIMEOUT) stuck++; + else ReleaseMutex(m); + for (i = 0; i < 4; i++) CloseHandle(th[i]); + CloseHandle(m); + } + CHECK(!stuck, "mutex stuck after terminate in %d rounds", stuck); + ev = CreateEventA(NULL, TRUE, FALSE, NULL); + churn_ready = 0; + for (i = 0; i < 4; i++) th[i] = CreateThread(NULL, 0, blocked_waiter, ev, 0, NULL); + while (churn_ready < 4) Sleep(0); + Sleep(50); + for (i = 0; i < 4; i++) TerminateThread(th[i], 7); + WaitForMultipleObjects(4, th, TRUE, INFINITE); + for (i = 0; i < 4; i++) CloseHandle(th[i]); + start = GetTickCount(); + for (i = 0; i < 100; i++) PulseEvent(ev); + CHECK(GetTickCount() - start < 400, "pulse after killed waiters took %lu ms", GetTickCount() - start); + CHECK(WaitForSingleObject(ev, 0) == WAIT_TIMEOUT, "pulse left event set"); + CloseHandle(ev); + CloseHandle(ev_manual); + printf("terminate ok\n"); +} + +static void test_cross_process(const char *self) +{ + HANDLE ev = CreateEventA(NULL, FALSE, FALSE, "Local\\esstress_ev"); + HANDLE done = CreateEventA(NULL, TRUE, FALSE, "Local\\esstress_done"); + HANDLE sm = CreateSemaphoreA(NULL, 0, 100000, "Local\\esstress_sem"); + PROCESS_INFORMATION pi; + STARTUPINFOA si = { sizeof(si) }; + char cmd[MAX_PATH + 16]; + DWORD r, code; + int i; + sprintf(cmd, "\"%s\" child", self); + CHECK(CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi), "createprocess %lu", GetLastError()); + for (i = 0; i < 5000; i++) + { + SetEvent(ev); + r = WaitForSingleObject(sm, 5000); + if (r != WAIT_OBJECT_0) { CHECK(0, "xproc wait %lu at %d", r, i); break; } + } + SetEvent(done); + r = WaitForSingleObject(pi.hProcess, 10000); + CHECK(r == WAIT_OBJECT_0, "child exit wait %lu", r); + GetExitCodeProcess(pi.hProcess, &code); + CHECK(code == 0, "child code %lu", code); + CloseHandle(pi.hProcess); CloseHandle(pi.hThread); + CloseHandle(ev); CloseHandle(done); CloseHandle(sm); + printf("cross_process ok\n"); +} + +static int child(void) +{ + HANDLE ev = OpenEventA(SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, "Local\\esstress_ev"); + HANDLE done = OpenEventA(SYNCHRONIZE, FALSE, "Local\\esstress_done"); + HANDLE sm = OpenSemaphoreA(SEMAPHORE_MODIFY_STATE | SYNCHRONIZE, FALSE, "Local\\esstress_sem"); + HANDLE objs[2]; + objs[0] = done; objs[1] = ev; + if (!ev || !done || !sm) return 1; + for (;;) + { + DWORD r = WaitForMultipleObjects(2, objs, FALSE, 10000); + if (r == WAIT_OBJECT_0) return 0; + if (r != WAIT_OBJECT_0 + 1) return 2; + ReleaseSemaphore(sm, 1, NULL); + } +} + +static void test_kill_holder(const char *self) +{ + HANDLE m = CreateMutexA(NULL, FALSE, "Local\\esstress_mutex"); + PROCESS_INFORMATION pi; + STARTUPINFOA si = { sizeof(si) }; + char cmd[MAX_PATH + 16]; + DWORD r; + sprintf(cmd, "\"%s\" holder", self); + CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); + Sleep(500); + TerminateProcess(pi.hProcess, 7); + WaitForSingleObject(pi.hProcess, INFINITE); + r = WaitForSingleObject(m, 3000); + CHECK(r == WAIT_ABANDONED, "killed holder %lu", r); + ReleaseMutex(m); + CloseHandle(pi.hProcess); CloseHandle(pi.hThread); CloseHandle(m); + printf("kill_holder ok\n"); +} + +static int holder(void) +{ + HANDLE m = OpenMutexA(SYNCHRONIZE, FALSE, "Local\\esstress_mutex"); + WaitForSingleObject(m, INFINITE); + Sleep(INFINITE); + return 0; +} + +static void test_many_objects(void) +{ + static HANDLE h[20000]; + int i, n = 0; + for (i = 0; i < 20000; i++) + { + if (!(h[i] = CreateEventA(NULL, i & 1, i & 2, NULL))) break; + n++; + } + CHECK(n == 20000, "created %d", n); + for (i = 0; i < n; i++) CHECK(WaitForSingleObject(h[i], 0) == ((i & 2) ? WAIT_OBJECT_0 : WAIT_TIMEOUT), "obj %d", i); + for (i = 0; i < n; i++) CloseHandle(h[i]); + for (i = 0; i < 100000; i++) + { + HANDLE e = CreateEventA(NULL, FALSE, TRUE, NULL); + if (WaitForSingleObject(e, 0) != WAIT_OBJECT_0) { CHECK(0, "churn %d", i); CloseHandle(e); break; } + CloseHandle(e); + } + printf("many_objects ok\n"); +} + +static void test_timers_and_threads(void) +{ + HANDLE t = CreateWaitableTimerA(NULL, FALSE, NULL); + LARGE_INTEGER due; + DWORD start = GetTickCount(), r; + int i; + due.QuadPart = -10 * 1000 * 20; + SetWaitableTimer(t, &due, 10, NULL, NULL, FALSE); + for (i = 0; i < 20; i++) + { + r = WaitForSingleObject(t, 1000); + CHECK(r == WAIT_OBJECT_0, "timer %d %lu", i, r); + } + CHECK(GetTickCount() - start < 2000, "timer slow %lu", GetTickCount() - start); + CancelWaitableTimer(t); + CloseHandle(t); + r = WaitForSingleObject(GetCurrentProcess(), 10); + CHECK(r == WAIT_TIMEOUT, "process self %lu", r); + printf("timers ok\n"); +} + +int main(int argc, char **argv) +{ + if (argc > 1 && !strcmp(argv[1], "child")) return child(); + if (argc > 1 && !strcmp(argv[1], "holder")) return holder(); + test_mutex_contention(); + test_semaphore(); + test_auto_event(); + test_manual_event(); + test_wait_all(); + test_abandoned(); + test_apc(); + test_close_while_waiting(); + test_pulse(); + test_semantics(); + test_semaphore_tokens(); + if (argc > 1 && !strcmp(argv[1], "terminate")) test_terminate(); + test_cross_process(argv[0]); + test_kill_holder(argv[0]); + test_many_objects(); + test_timers_and_threads(); + printf("esstress: %d failures\n", failures); + return failures ? 1 : 0; +} diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-esync b/tools/linuxfs/overlay/usr/local/bin/droiddeck-esync new file mode 100755 index 000000000..208f4473b --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-esync @@ -0,0 +1,1171 @@ +#!/usr/bin/python3 +# droiddeck-esync: picks and stages the esync pack for each Proton build. +# Credit: esync and ntsync were designed by Elizabeth Figura for Wine and Proton, and fsync by Elizabeth Figura +# and Paul Gofman; droiddeck-esync, droiddeck-ntsync and droiddeck-fsync build on that work. +import contextlib +import fcntl +import hashlib +import json +import os +import re +import resource +import shutil +import stat +import struct +import sys +import time +from pathlib import Path + +NTDLL = "files/lib/wine/aarch64-unix/ntdll.so" +WINESERVER = "files/bin-arm64/wineserver" +PACK_FILES = (NTDLL, WINESERVER) +DEFAULT_COPY = ("files/bin-arm64/wine", "files/lib/wine/aarch64-unix/wine", "files/lib/wine/aarch64-unix/wine-preloader") +REAL_DIRS = (".", "files", "files/bin-arm64", "files/lib", "files/lib/wine", "files/lib/wine/aarch64-unix") +STOCK_LINKS = ("dist.lock", "files/steampipe_fixups_mtime") +SKIPPED = frozenset(("toolmanifest.vdf", "compatibilitytool.vdf", "droiddeck-proton-wrap", "bannerlator-proton-wrap")) +SKIPPED_SUFFIX = (".droiddeck-orig", ".bannerlator-orig") +RECORD = ".droiddeck-esync.json" +STORE = ".local/share/droiddeck-esync" +STORE_LOCK = "store.lock" +BUILTIN_MARKER = b"esync: up and running" +FSYNC_MARKER = b"fsync: up and running" +NTSYNC_MARKER = b"/dev/ntsync" +WINESERVER_ONLY = ("PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "PROTON_NO_NTSYNC") +NOSYNC_APPS = frozenset(("2630", "1060210", "414740", "201510", "1233880")) +SOURCES = ("Proton Experimental (ARM64)", "Proton 11.0 (ARM64)") +SD_LIBRARY = "/mnt/droiddeck-sd" +OWN_TOOL_PREFIX = "droiddeck-proton-" +PACK_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,127}") +SHA256 = re.compile(r"[0-9a-f]{64}") +TEMP_NAME = re.compile(r"\..+\.tmp-[0-9]+") +MIN_NOFILE = 8192 +LOG_LINES = 200 +LIVE_FILE = "prefixes.json" +LIVE_ROWS = 200 +LIVE_KEYS = ("WINESERVER", "BL_SYNC_PACK", "WINEESYNC", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "BL_SYNC", "BL_FSYNC", "WINEFSYNC") +CACHE_ROWS = 512 +TEMP_AGE = 3600 +GC_GRACE = 600 +CHUNK = 1 << 20 + + +def say(message): + with contextlib.suppress(Exception): + print(message, file=sys.stderr, flush=True) + + +def truthy(value): + return bool(value) and value != "0" + + +def store_root(env): + home = env.get("HOME") or os.path.expanduser("~") + root = Path(home) / STORE + return root if root.is_dir() else None + + +def read_text(path): + try: + with open(path, encoding="utf-8", errors="surrogateescape") as source: + return source.read() + except FileNotFoundError: + return "" + + +def write_text(path, text): + folder, name = os.path.split(path) + temp = os.path.join(folder, ".%s.tmp-%d" % (name, os.getpid())) + try: + with open(temp, "w", encoding="utf-8", errors="surrogateescape") as target: + target.write(text) + os.replace(temp, path) + except BaseException: + with contextlib.suppress(OSError): + os.unlink(temp) + raise + + +def acquire(path, block=True): + for _ in range(64): + fd = os.open(path, os.O_RDWR | os.O_CREAT | os.O_CLOEXEC, 0o644) + try: + try: + fcntl.flock(fd, fcntl.LOCK_EX if block else fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + os.close(fd) + return None + held = os.fstat(fd) + try: + current = os.stat(path) + except FileNotFoundError: + current = None + if current is not None and (held.st_dev, held.st_ino) == (current.st_dev, current.st_ino): + return fd + except BaseException: + os.close(fd) + raise + os.close(fd) + raise OSError("%s keeps being replaced" % path) + + +@contextlib.contextmanager +def locked(path, block=True): + fd = acquire(path, block) + try: + yield fd + finally: + if fd is not None: + os.close(fd) + + +def update_file(root, name, change): + path = os.path.join(root, name) + with locked(os.path.join(root, STORE_LOCK)): + old = read_text(path) + new = change(old) + if new != old: + write_text(path, new) + return new + + +def tsv(rows): + return "".join("\t".join(re.sub(r"[\t\r\n]", " ", str(field)) for field in row) + "\n" for row in rows) + + +def parse_tsv(text, width): + return [line.split("\t") for line in text.split("\n") if line and line.count("\t") == width - 1] + + +def exports_hash(path): + with open(path, "rb") as source: + data = source.read() + if data[:4] != b"\x7fELF" or data[4:6] != b"\x02\x01": + raise ValueError("%s is not a little-endian ELF64 file" % path) + names = set() + try: + offset = struct.unpack_from("> 4 in (1, 2): + stop = data.index(b"\0", start + name, end) + if stop > start + name: + names.add(data[start + name:stop]) + except (struct.error, IndexError) as error: + raise ValueError("%s is a truncated ELF file (%s)" % (path, error)) + return hashlib.sha256(b"\n".join(sorted(names))).hexdigest() + + +def stat_key(info): + return (info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns) + + +def digest(path): + sha = hashlib.sha256() + found = {BUILTIN_MARKER: False, FSYNC_MARKER: False, NTSYNC_MARKER: False} + tail = b"" + keep = max(len(marker) for marker in found) - 1 + with open(path, "rb") as source: + before = stat_key(os.fstat(source.fileno())) + for chunk in iter(lambda: source.read(CHUNK), b""): + sha.update(chunk) + window = tail + chunk + for marker in found: + found[marker] = found[marker] or marker in window + tail = window[-keep:] + after = stat_key(os.fstat(source.fileno())) + return sha.hexdigest(), found[BUILTIN_MARKER], found[FSYNC_MARKER], found[NTSYNC_MARKER], before if before == after else None + + +def parse_cache(text): + rows = {} + for fields in parse_tsv(text, 11): + if not SHA256.fullmatch(fields[6]): + continue + try: + key = tuple(int(value) for value in fields[1:6]) + except ValueError: + continue + rows.pop(fields[0], None) + rows[fields[0]] = {"key": key, "sha": fields[6], "builtin": fields[7] == "1", "exports": fields[8], "fsync": fields[9] == "1", + "ntsync": fields[10] == "1"} + return rows + + +def format_cache(items): + return tsv([real] + list(row["key"]) + [row["sha"], "1" if row["builtin"] else "0", row["exports"], "1" if row["fsync"] else "0", + "1" if row["ntsync"] else "0"] + for real, row in items) + + +class HashCache: + def __init__(self, root): + self.root = os.fspath(root) if root is not None else None + self.rows = parse_cache(read_text(os.path.join(self.root, "hash-cache.tsv"))) if self.root else {} + self.changed = set() + + def lookup(self, path, exports=False): + real = os.path.realpath(path) + key = stat_key(os.stat(real)) + row = self.rows.get(real) + if row is None or row["key"] != key: + sha, builtin, fsync, ntsync, stable = digest(real) + row = {"key": stable, "sha": sha, "builtin": builtin, "exports": "", "fsync": fsync, "ntsync": ntsync} + if stable is not None: + self.rows[real] = row + self.changed.add(real) + if exports and not row["exports"]: + try: + row["exports"] = exports_hash(real) + except (OSError, ValueError): + row["exports"] = "-" + if row["key"] is not None: + self.changed.add(real) + return row + + def save(self, prune=False): + if not self.root or not (self.changed or prune): + return + changed = {real: self.rows[real] for real in self.changed if real in self.rows} + + def merge(text): + rows = parse_cache(text) + for real, row in changed.items(): + rows.pop(real, None) + rows[real] = row + if prune: + rows = {real: row for real, row in rows.items() if os.path.exists(real)} + return format_cache(list(rows.items())[-CACHE_ROWS:]) + + try: + update_file(self.root, "hash-cache.tsv", merge) + except OSError as error: + say("droiddeck-esync: hash cache not saved (%s)" % error) + self.changed.clear() + + +def version_line(tool_dir): + try: + with open(os.path.join(tool_dir, "version"), encoding="utf-8", errors="replace") as source: + return source.readline().strip() + except OSError: + return "" + + +def version_token(line): + fields = line.split() + return fields[1] if len(fields) > 1 else "" + + +class Measured(dict): + def __init__(self, values, exports): + super().__init__(values) + self.exports = exports + + def __missing__(self, key): + if key != "exports": + raise KeyError(key) + value = self.exports() + self[key] = value + return value + + +def measure(tool_dir, cache=None): + cache = cache if cache is not None else HashCache(None) + tool = os.fspath(tool_dir) + ntdll = cache.lookup(os.path.join(tool, NTDLL)) + server = cache.lookup(os.path.join(tool, WINESERVER)) + line = version_line(tool) + return Measured({"tool": tool, "stock": os.path.realpath(tool), "ntdll": ntdll["sha"], "wineserver": server["sha"], + "builtin": server["builtin"], "fsync": server["fsync"], "ntsync": server["ntsync"], "version_line": line, + "token": version_token(line)}, + lambda: cache.lookup(os.path.join(tool, NTDLL), True)["exports"]) + + +def has_stock(tool): + return all(os.path.isfile(os.path.join(tool, rel)) for rel in PACK_FILES) + + +def relative(folder, name): + return name if folder == "." else folder + "/" + name + + +def valid_copy(rel): + if not isinstance(rel, str) or not rel or rel.startswith("/") or "\0" in rel: + return False + if any(part in ("", ".", "..") for part in rel.split("/")): + return False + folder = os.path.dirname(rel) or "." + return folder in REAL_DIRS and rel not in PACK_FILES and rel not in REAL_DIRS and rel not in STOCK_LINKS and rel != RECORD + + +def valid_hashes(value): + return isinstance(value, dict) and all(isinstance(value.get(rel), str) and SHA256.fullmatch(value[rel]) for rel in PACK_FILES) + + +def is_int(value): + return isinstance(value, int) and not isinstance(value, bool) + + +def valid_pack(data, name=None): + if not isinstance(data, dict) or not is_int(data.get("format")) or data["format"] != 1: + return False + ident = data.get("id") + copy = data.get("copy", list(DEFAULT_COPY)) + return bool(isinstance(ident, str) and PACK_ID.fullmatch(ident) and (name is None or ident == name) + and isinstance(data.get("flavor"), str) and data["flavor"] + and is_int(data.get("rev")) + and isinstance(data.get("version"), str) and isinstance(data.get("version_line", ""), str) + and valid_hashes(data.get("stock")) and valid_hashes(data.get("files")) + and isinstance(data.get("exports"), str) and isinstance(data.get("source_match"), bool) + and isinstance(copy, list) and all(valid_copy(rel) for rel in copy) + and not data.get("revoked") and not data.get("ignored")) + + +def read_pack(pack_dir, name=None): + with open(os.path.join(pack_dir, "pack.json"), encoding="utf-8") as source: + data = json.load(source) + if not valid_pack(data, name) or not all(os.path.isfile(os.path.join(pack_dir, rel)) for rel in PACK_FILES): + raise ValueError("%s is not a usable droiddeck-esync pack" % pack_dir) + return data + + +def load_packs(root): + packs = [] + folder = os.path.join(root, "packs") + try: + names = sorted(os.listdir(folder)) + except OSError: + return packs + for name in names: + pack_dir = os.path.join(folder, name) + if name.startswith(".") or not os.path.isfile(os.path.join(pack_dir, ".complete")): + continue + if os.path.lexists(os.path.join(pack_dir, ".ignored")): + continue + try: + packs.append(read_pack(pack_dir, name)) + except (OSError, ValueError): + continue + return packs + + +def match_kind(pack, measured): + if pack.get("revoked") or pack.get("ignored") or measured["builtin"]: + return None + stock = pack["stock"] + if stock[NTDLL] == measured["ntdll"] and stock[WINESERVER] == measured["wineserver"]: + return "exact" + if (pack.get("source_match") is True and measured["token"] and pack.get("version") == measured["token"] + and pack.get("exports") == measured["exports"]): + return "source" + return None + + +def find_pack(packs, measured): + best = None + best_key = None + for pack in packs: + kind = match_kind(pack, measured) + if kind is None: + continue + key = (kind == "exact", pack["rev"], pack["id"]) + if best_key is None or key > best_key: + best, best_key = pack, key + return best + + +def absolute(path): + path = os.fspath(path).rstrip("/") or "/" + return path if os.path.isabs(path) else os.path.join(os.getcwd(), path) + + +def dist_name(pack_id, stock): + return "%s~%s" % (pack_id, hashlib.sha256(os.fsencode(stock)).hexdigest()[:8]) + + +def read_record(dist): + try: + with open(os.path.join(dist, RECORD), encoding="utf-8") as source: + record = json.load(source) + except (FileNotFoundError, NotADirectoryError, ValueError): + return None + return record if isinstance(record, dict) else None + + +def write_json(path, data): + text = json.dumps(data, indent=1, sort_keys=True) + "\n" + if read_text(path) != text: + write_text(path, text) + + +def remove(path): + if os.path.islink(path) or (os.path.lexists(path) and not os.path.isdir(path)): + with contextlib.suppress(FileNotFoundError): + os.unlink(path) + elif os.path.isdir(path): + shutil.rmtree(path, ignore_errors=True) + + +def age(path): + try: + return time.time() - os.lstat(path).st_mtime + except OSError: + return 0 + + +def put_link(target, path): + folder, name = os.path.split(path) + temp = os.path.join(folder, ".%s.tmp-%d" % (name, os.getpid())) + remove(temp) + os.symlink(target, temp) + try: + os.replace(temp, path) + except BaseException: + remove(temp) + raise + + +class PackDamaged(ValueError): + pass + + +def install(source, path, expected=None): + folder, name = os.path.split(path) + temp = os.path.join(folder, ".%s.tmp-%d" % (name, os.getpid())) + remove(temp) + try: + sha = hashlib.sha256() + with open(source, "rb") as reader, open(temp, "xb") as writer: + for chunk in iter(lambda: reader.read(CHUNK), b""): + sha.update(chunk) + writer.write(chunk) + result = sha.hexdigest() + if expected is not None and result != expected: + raise PackDamaged("%s does not match its pack (sha256 %s)" % (source, result[:12])) + os.chmod(temp, 0o755) + os.replace(temp, path) + except BaseException: + remove(temp) + raise + return result + + +def intact(path, expected, cache): + try: + info = os.lstat(path) + except OSError: + return False + return (stat.S_ISREG(info.st_mode) and (info.st_mode & 0o555) == 0o555 + and cache.lookup(path)["sha"] == expected) + + +def plan(tool, base, pack): + real = set(PACK_FILES).union(pack.get("copy", DEFAULT_COPY)) + links = {} + for folder in REAL_DIRS: + try: + names = os.listdir(os.path.join(tool, folder)) + except OSError: + continue + for name in names: + rel = relative(folder, name) + if rel in REAL_DIRS or rel in real or rel == RECORD or name in SKIPPED or name.endswith(SKIPPED_SUFFIX): + continue + text = None + source = os.path.join(tool, rel) + if os.path.islink(source): + with contextlib.suppress(OSError): + text = os.readlink(source) + links[rel] = text if text and not os.path.isabs(text) else os.path.join(base, rel) + for rel in STOCK_LINKS: + links[rel] = os.path.join(base, rel) + return links + + +def sync(dist, tool, base, pack, pack_dir, record, cache): + links = plan(tool, base, pack) + copy = list(pack.get("copy", DEFAULT_COPY)) + real = set(PACK_FILES).union(copy) + for folder in REAL_DIRS: + path = os.path.join(dist, folder) + if os.path.islink(path) or (os.path.lexists(path) and not os.path.isdir(path)): + os.unlink(path) + os.makedirs(path, exist_ok=True) + for folder in REAL_DIRS: + for name in os.listdir(os.path.join(dist, folder)): + rel = relative(folder, name) + if rel in links or rel in real or rel in REAL_DIRS or rel == RECORD: + continue + path = os.path.join(dist, rel) + if os.path.islink(path) or (TEMP_NAME.fullmatch(name) and os.path.isfile(path)): + os.unlink(path) + for rel, target in sorted(links.items()): + path = os.path.join(dist, rel) + try: + current = os.readlink(path) + except OSError: + current = None + if current != target and not (current is None and os.path.isdir(path)): + put_link(target, path) + for rel in PACK_FILES: + path = os.path.join(dist, rel) + if not intact(path, pack["files"][rel], cache): + install(os.path.join(pack_dir, rel), path, pack["files"][rel]) + previous = record.get("copies") if isinstance(record.get("copies"), dict) else {} + copies = {} + for rel in copy: + source = os.path.join(tool, rel) + path = os.path.join(dist, rel) + entry = previous.get(rel) + try: + info = os.stat(source) + except OSError: + info = None + if info is None or not stat.S_ISREG(info.st_mode): + if entry is not None and os.path.lexists(path): + copies[rel] = entry + continue + if (isinstance(entry, list) and len(entry) == 3 and entry[:2] == [info.st_size, info.st_mtime_ns] + and isinstance(entry[2], str) and intact(path, entry[2], cache)): + copies[rel] = entry + else: + copies[rel] = [info.st_size, info.st_mtime_ns, install(source, path)] + write_json(os.path.join(dist, RECORD), {"pack": pack["id"], "stock": os.path.realpath(tool), "tool": base, + "version": version_line(tool), "copies": copies}) + + +def build_tree(tool, base, pack, pack_dir, out, cache): + folder, name = os.path.split(out) + temp = os.path.join(folder, ".%s.tmp-%d" % (name, os.getpid())) + remove(temp) + try: + os.makedirs(temp) + sync(temp, tool, base, pack, pack_dir, {}, cache) + except BaseException: + remove(temp) + raise + return temp + + +def place(temp, out): + try: + os.rename(temp, out) + except OSError: + remove(temp) + if not os.path.isdir(out): + raise + return False + return True + + +def build_shadow(tool_dir, pack_dir, out_dir, cache=None): + tool = absolute(tool_dir) + out = absolute(out_dir) + pack = read_pack(pack_dir) + os.makedirs(os.path.dirname(out), exist_ok=True) + place(build_tree(tool, tool, pack, os.fspath(pack_dir), out, cache if cache is not None else HashCache(None)), out) + return out + + +def ensure_dist(root, tool_dir, pack, cache=None): + root = os.fspath(root) + cache = cache if cache is not None else HashCache(root) + tool = absolute(tool_dir) + stock = os.path.realpath(tool) + folder = os.path.join(root, "dist") + os.makedirs(folder, exist_ok=True) + name = dist_name(pack["id"], stock) + dist = os.path.join(folder, name) + pack_dir = os.path.join(root, "packs", pack["id"]) + with locked(dist + ".lock"): + record = read_record(dist) + if record is not None and record.get("pack") == pack["id"] and record.get("stock") == stock: + previous = record.get("tool") + base = previous if (isinstance(previous, str) and os.path.isabs(previous) + and os.path.realpath(previous) == stock) else tool + sync(dist, tool, base, pack, pack_dir, record, cache) + elif os.path.lexists(dist): + fresh = build_tree(tool, tool, pack, pack_dir, dist, cache) + old = os.path.join(folder, ".%s.old.tmp-%d" % (name, os.getpid())) + remove(old) + os.rename(dist, old) + os.rename(fresh, dist) + remove(old) + elif not place(build_tree(tool, tool, pack, pack_dir, dist, cache), dist): + sync(dist, tool, tool, pack, pack_dir, read_record(dist) or {}, cache) + os.utime(dist) + cache.save() + return dist + + +def quarantine(root, pack, error): + if not isinstance(error, PackDamaged): + return + with contextlib.suppress(OSError): + os.unlink(os.path.join(root, "packs", pack["id"], ".complete")) + say("droiddeck-esync: pack %s is damaged; the app fetches it again" % pack["id"]) + + +def app_id(env): + value = env.get("STEAM_COMPAT_APP_ID") or env.get("SteamAppId") + if not value: + value = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/").rsplit("/", 1)[-1] + return value + + +def minimum_nofile(env): + try: + return int(env.get("BL_SYNC_MIN_NOFILE") or MIN_NOFILE) + except ValueError: + return MIN_NOFILE + + +def limit(value): + return "unlimited" if value == resource.RLIM_INFINITY else str(value) + + +def log_launch(root, command, env, tool, pack_id): + try: + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + line = tsv([[int(time.time()), app_id(env) or "-", command[1] if len(command) > 1 else "-", tool, pack_id or "-", + env.get("WINEESYNC") or "-", env.get("BL_SYNC") or "-", "%s/%s" % (limit(soft), limit(hard)), + env.get("BL_FSYNC") or "-"]]) + + def append(text): + lines = [entry for entry in text.split("\n") if entry] + lines.append(line.rstrip("\n")) + return "\n".join(lines[-LOG_LINES:]) + "\n" + + update_file(root, "launches.log", append) + except Exception as error: + say("droiddeck-esync: launch not logged (%s)" % error) + + +def wanted_row(measured): + return [measured["ntdll"], measured["wineserver"], measured["token"] or "-", measured["exports"] or "-", + measured["tool"], str(int(time.time()))] + + +def merge_wanted(text, fresh, drop): + rows = [] + seen = set() + for row in parse_tsv(text, 6): + key = (row[0], row[1]) + if key in seen or key in drop: + continue + seen.add(key) + if key in fresh: + row[2:5] = fresh[key][2:5] + rows.append(row) + rows.extend(row for key, row in fresh.items() if key not in seen) + return tsv(rows) + + +def want(root, measured): + try: + row = wanted_row(measured) + update_file(root, "wanted.tsv", lambda text: merge_wanted(text, {(row[0], row[1]): row}, set())) + except Exception as error: + say("droiddeck-esync: wanted.tsv not updated (%s)" % error) + + +def select(command, env): + result = (list(command), dict(env)) + try: + command = list(command) + env = dict(env) + tool = os.path.dirname(command[0]) or "." + if env.get("PROTON_USE_ARM64", "1") != "1" or not has_stock(tool) or os.path.lexists(os.path.join(tool, RECORD)): + return result + prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + last = prefix.rsplit("/", 1)[-1] + if not prefix or last == "0" or last.startswith("0-"): + return result + if truthy(env.get("PROTON_NO_NTSYNC")): + env["BL_SYNC"] = "0" + result = (list(command), dict(env)) + root = store_root(env) + if root is None: + return result + root = os.fspath(root) + kept = keep_live(root, command, env, tool) + if kept is not None: + return kept + chosen = choose(root, command, env, tool, result) + remember(root, env, tool, chosen) + return chosen + except Exception as error: + say("droiddeck-esync: skipped (%s); stock binaries" % error) + record(result[0], result[1]) + return result + + +def record(command, env): + try: + root = store_root(env) + if root is not None and command: + remember(os.fspath(root), env, os.path.dirname(command[0]) or ".", (list(command), dict(env))) + except Exception as error: + say("droiddeck-esync: sync choice not recorded (%s)" % error) + + +def prefix_of(env): + data = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + return os.path.realpath(os.path.join(data, "pfx")) if data else None + + +def server_running(prefix): + for entry in os.listdir("/proc"): + if not entry.isdigit(): + continue + try: + with open("/proc/%s/comm" % entry, "rb") as source: + if source.read().strip() != b"wineserver": + continue + with open("/proc/%s/environ" % entry, "rb") as source: + items = source.read().split(b"\0") + except OSError: + continue + for item in items: + if item.startswith(b"WINEPREFIX=") and os.path.realpath(os.fsdecode(item[11:])) == prefix: + return True + return False + + +def read_live(root): + try: + rows = json.loads(read_text(os.path.join(root, LIVE_FILE)) or "{}") + except ValueError: + return {} + return rows if isinstance(rows, dict) else {} + + +def remember(root, env, tool, chosen): + prefix = prefix_of(env) + if prefix is None: + return + command, chosen_env = chosen + row = {"tool": os.path.realpath(tool), "command": command[0], "env": {name: chosen_env.get(name) for name in LIVE_KEYS}} + + def merge(text): + try: + rows = json.loads(text or "{}") + except ValueError: + rows = {} + if not isinstance(rows, dict): + rows = {} + rows.pop(prefix, None) + rows[prefix] = row + return json.dumps(dict(list(rows.items())[-LIVE_ROWS:]), sort_keys=False) + "\n" + + try: + update_file(root, LIVE_FILE, merge) + except OSError as error: + say("droiddeck-esync: sync choice not recorded (%s)" % error) + + +def keep_live(root, command, env, tool): + prefix = prefix_of(env) + row = read_live(root).get(prefix) if prefix else None + if not isinstance(row, dict) or not isinstance(row.get("env"), dict) or row.get("tool") != os.path.realpath(tool): + return None + kept_command = row.get("command") + if not isinstance(kept_command, str) or os.path.basename(kept_command) != os.path.basename(command[0]): + return None + if not os.path.isfile(kept_command) or not server_running(prefix): + return None + command, env = list(command), dict(env) + command[0] = kept_command + for name in LIVE_KEYS: + value = row["env"].get(name) + if isinstance(value, str): + env[name] = value + else: + env.pop(name, None) + say("droiddeck-esync: Wine is still running in this prefix; keeping its sync choice (%s)" % describe(env)) + log_launch(root, command, env, tool, env.get("BL_SYNC_PACK")) + return command, env + + +def describe(env): + if env.get("BL_SYNC") == "1": + return "droiddeck-ntsync" + if env.get("BL_FSYNC") == "1": + return "droiddeck-fsync" + if env.get("BL_SYNC_PACK") and truthy(env.get("WINEESYNC")): + return "droiddeck-esync" + if all(truthy(env.get(name)) for name in WINESERVER_ONLY): + return "wineserver" + return "Proton's own sync" + + +def fsync_ready(env, measured): + listed = "PROTON_NO_FSYNC" not in env and app_id(env) in NOSYNC_APPS + return measured["fsync"] and env.get("BL_SYNC") != "1" and not truthy(env.get("PROTON_NO_FSYNC")) and not listed + + +def with_fsync(root, command, env, tool, reason): + command, env = list(command), dict(env) + env["BL_FSYNC"] = "1" + env["BL_SYNC"] = "0" + env["WINEFSYNC"] = "1" + say("droiddeck-fsync: %s; stock binaries with Proton's fsync" % reason) + log_launch(root, command, env, tool, None) + return command, env + + +def stand_in(root, command, env, tool, reason, measured=None): + if measured is not None and fsync_ready(env, measured): + return with_fsync(root, command, env, tool, reason) + command, env = list(command), dict(env) + if truthy(env.get("PROTON_NO_NTSYNC")): + say("droiddeck-esync: %s; stock binaries" % reason) + elif measured is not None and not measured["ntsync"]: + env["BL_SYNC"] = "0" + say("droiddeck-esync: %s; stock binaries (this Wine has no ntsync)" % reason) + else: + env["BL_SYNC"] = "1" + say("droiddeck-esync: %s; stock binaries with droiddeck-ntsync" % reason) + log_launch(root, command, env, tool, None) + return command, env + + +def esync_unwanted(env): + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + starved = hard != resource.RLIM_INFINITY and hard < minimum_nofile(env) + listed = "PROTON_NO_ESYNC" not in env and app_id(env) in NOSYNC_APPS + return truthy(env.get("PROTON_NO_ESYNC")) or env.get("WINEESYNC") == "0" or listed or starved + + +def wineserver_only(env): + env = dict(env) + for name in WINESERVER_ONLY: + env.setdefault(name, "1") + if truthy(env["PROTON_NO_ESYNC"]): + env["WINEESYNC"] = "0" + if truthy(env["PROTON_NO_FSYNC"]): + env["WINEFSYNC"] = "0" + if truthy(env["PROTON_NO_NTSYNC"]): + env["BL_SYNC"] = "0" + return env + + +def choose(root, command, env, tool, stock): + name = os.path.basename(tool.rstrip("/")) or tool + wants_fsync = env.get("BL_FSYNC_FIRST") == "1" + if "BL_FSYNC" in env: + env["BL_FSYNC"] = "0" + stock = (stock[0], dict(stock[1], BL_FSYNC="0")) + if env.get("BL_SYNC_FALLBACK") != "1" and not wants_fsync and env.get("BL_SYNC") != "1": + stock = (stock[0], wineserver_only(stock[1])) + say("droiddeck-esync: off; wineserver sync only") + log_launch(root, stock[0], stock[1], tool, None) + return stock + cache = HashCache(root) + try: + measured = measure(tool, cache) + if env.get("BL_SYNC") == "1" and not measured["ntsync"]: + env["BL_SYNC"] = "0" + stock = (stock[0], dict(stock[1], BL_SYNC="0")) + wants_fsync = True + say("droiddeck-ntsync: %s has no ntsync; trying droiddeck-fsync" % name) + if wants_fsync and fsync_ready(env, measured): + return with_fsync(root, command, env, tool, "chosen for %s" % name) + if env.get("BL_SYNC_FALLBACK") != "1": + say("droiddeck-esync: off") + log_launch(root, command, env, tool, None) + return stock + if esync_unwanted(env) and fsync_ready(env, measured): + return with_fsync(root, command, env, tool, "esync is off for this game") + if measured["builtin"]: + if esync_unwanted(env): + env["PROTON_NO_ESYNC"] = "1" + env["WINEESYNC"] = "0" + return stand_in(root, command, env, tool, "esync is off for this game on %s" % name, measured) + say("droiddeck-esync: %s has esync built in" % name) + log_launch(root, command, env, tool, None) + return stock + pack = find_pack(load_packs(root), measured) + if pack is None: + want(root, measured) + return stand_in(root, command, env, tool, "no pack for %s (%s, ntdll %s)" + % (name, measured["version_line"], measured["ntdll"][:12]), measured) + try: + dist = ensure_dist(root, tool, pack, cache) + except Exception as error: + quarantine(root, pack, error) + return stand_in(root, command, env, tool, "pack %s unusable for %s (%s)" % (pack["id"], name, error), measured) + finally: + cache.save() + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + if esync_unwanted(env): + env["WINEESYNC"] = "0" + env["PROTON_NO_ESYNC"] = "1" + if not truthy(env.get("PROTON_NO_NTSYNC")) and measured["ntsync"]: + env["BL_SYNC"] = "1" + elif not env.get("WINEESYNC"): + env["WINEESYNC"] = "1" + if truthy(env["WINEESYNC"]) and "PROTON_NO_FSYNC" not in env: + env["PROTON_NO_FSYNC"] = "1" + env["WINESERVER"] = os.path.join(dist, WINESERVER) + env["BL_SYNC_PACK"] = pack["id"] + command[0] = os.path.join(dist, os.path.basename(command[0])) + say("droiddeck-esync: pack %s for %s (%s, %s match); droiddeck-ntsync %s, droiddeck-esync %s, fds %s/%s" + % (pack["id"], name, measured["version_line"], match_kind(pack, measured), "on" if env.get("BL_SYNC") == "1" else "off", + "on" if truthy(env["WINEESYNC"]) else "off", limit(soft), limit(hard))) + log_launch(root, command, env, tool, pack["id"]) + return command, env + + +def find_tools(steam_root): + candidates = [os.path.join(library, "steamapps", "common", name) for library in (steam_root, SD_LIBRARY) for name in SOURCES] + folder = os.path.join(steam_root, "compatibilitytools.d") + try: + names = sorted(os.listdir(folder)) + except OSError: + names = [] + candidates += [os.path.join(folder, name) for name in names if not name.startswith((OWN_TOOL_PREFIX, "."))] + tools = [] + seen = set() + for tool in candidates: + if os.path.isdir(tool) and has_stock(tool): + real = os.path.realpath(tool) + if real not in seen: + seen.add(real) + tools.append(tool) + return tools + + +def still_wanted(row, scanned, packs, cache): + tool = row[4] + try: + if os.path.realpath(tool) in scanned or not has_stock(tool): + return False + measured = measure(tool, cache) + return ((measured["ntdll"], measured["wineserver"]) == (row[0], row[1]) and not measured["builtin"] + and find_pack(packs, measured) is None) + except Exception: + return True + + +def update_wanted(root, fresh, scanned, packs, cache): + drop = set() + for row in parse_tsv(read_text(os.path.join(root, "wanted.tsv")), 6): + key = (row[0], row[1]) + if key not in fresh and not still_wanted(row, scanned, packs, cache): + drop.add(key) + return len(parse_tsv(update_file(root, "wanted.tsv", lambda text: merge_wanted(text, fresh, drop)), 6)) + + +def reconcile_store(root, steam_root, env): + cache = HashCache(root) + packs = load_packs(root) + enabled = env.get("BL_SYNC_FALLBACK") == "1" + rows = [] + fresh = {} + scanned = set() + used = 0 + for tool in find_tools(os.fspath(steam_root)): + try: + measured = measure(tool, cache) + pack = None if measured["builtin"] else find_pack(packs, measured) + if measured["builtin"]: + state = "builtin" + elif pack is not None: + state = "pack" + used += 1 + else: + state = "wanted" + fresh.setdefault((measured["ntdll"], measured["wineserver"]), wanted_row(measured)) + scanned.add(measured["stock"]) + except Exception as error: + say("droiddeck-esync: %s skipped (%s)" % (tool, error)) + continue + rows.append([tool, measured["version_line"], measured["ntdll"], measured["wineserver"], state, + pack["id"] if pack is not None else "-"]) + if pack is not None and enabled: + try: + ensure_dist(root, tool, pack, cache) + except Exception as error: + say("droiddeck-esync: pack %s unusable for %s (%s)" % (pack["id"], tool, error)) + quarantine(root, pack, error) + wanted = update_wanted(root, fresh, scanned, packs, cache) + update_file(root, "tools.tsv", lambda text: tsv(sorted(rows))) + cache.save(True) + gc(root) + return "droiddeck-esync: %d pack(s) in use, %d wanted" % (used, wanted) + + +def reconcile(steam_root, env): + try: + root = store_root(env) + if root is None: + return None + return reconcile_store(os.fspath(root), steam_root, env) + except Exception as error: + say("droiddeck-esync: reconcile failed (%s)" % error) + return None + + +def process_cmdlines(): + try: + entries = os.listdir("/proc") + except OSError: + return None + lines = [] + for entry in entries: + if not entry.isdigit(): + continue + try: + with open(os.path.join("/proc", entry, "cmdline"), "rb") as source: + lines.append(source.read()) + except OSError: + continue + return lines + + +def in_use(path, cmdlines): + needles = {os.fsencode(path), os.fsencode(os.path.realpath(path))} + return any(needle in line for line in cmdlines for needle in needles) + + +def stale(path, name, packs, cache): + record = read_record(path) + if record is None: + return True + pack = packs.get(record.get("pack")) + stock = record.get("stock") + if pack is None or not isinstance(stock, str) or name != dist_name(pack["id"], stock): + return True + if not has_stock(stock): + return True + try: + measured = measure(stock, cache) + if match_kind(pack, measured) is None: + return True + best = find_pack(list(packs.values()), measured) + return best is None or best["id"] != pack["id"] + except Exception: + return False + + +def collect(root): + folder = os.path.join(root, "dist") + try: + names = sorted(os.listdir(folder)) + except FileNotFoundError: + return 0 + packs = {pack["id"]: pack for pack in load_packs(root)} + cache = HashCache(root) + candidates = [] + for name in names: + path = os.path.join(folder, name) + if name.startswith("."): + if ".tmp-" in name and age(path) > TEMP_AGE: + remove(path) + elif (not name.endswith(".lock") and "~" in name and not os.path.islink(path) and os.path.isdir(path) + and stale(path, name, packs, cache)): + candidates.append(name) + cmdlines = process_cmdlines() if candidates else [] + removed = 0 + for name in candidates: + path = os.path.join(folder, name) + lock = path + ".lock" + with locked(lock, False) as fd: + if (fd is None or cmdlines is None or age(path) < GC_GRACE or in_use(path, cmdlines) + or not stale(path, name, packs, cache)): + continue + doomed = os.path.join(folder, ".%s.tmp-%d" % (name, os.getpid())) + remove(doomed) + os.rename(path, doomed) + os.unlink(lock) + remove(doomed) + removed += 1 + for name in sorted(os.listdir(folder)): + if name.startswith(".") or not name.endswith(".lock"): + continue + lock = os.path.join(folder, name) + dist = lock[:-len(".lock")] + if os.path.lexists(dist): + continue + with locked(lock, False) as fd: + if fd is not None and not os.path.lexists(dist): + os.unlink(lock) + cache.save() + return removed + + +def gc(root): + try: + if root is None: + return 0 + return collect(os.fspath(root)) + except Exception as error: + say("droiddeck-esync: gc failed (%s)" % error) + return 0 + + +def status(root): + root = os.fspath(root) + packs = load_packs(root) + try: + dists = sorted(name for name in os.listdir(os.path.join(root, "dist")) + if "~" in name and not name.startswith(".") and not name.endswith(".lock")) + except OSError: + dists = [] + tools = parse_tsv(read_text(os.path.join(root, "tools.tsv")), 6) + wanted = parse_tsv(read_text(os.path.join(root, "wanted.tsv")), 6) + lines = ["droiddeck-esync store %s" % root, "packs: %d installed" % len(packs)] + lines += [" %s (%s, rev %d)" % (pack["id"], pack["flavor"], pack["rev"]) for pack in packs] + lines.append("dists: %d" % len(dists)) + lines += [" %s" % name for name in dists] + lines.append("tools: %d" % len(tools)) + lines += [" %s %s: %s (%s)" % (row[4], row[5], row[0], row[1]) for row in tools] + lines.append("wanted: %d" % len(wanted)) + lines += [" %s %s (%s, ntdll %s)" % (row[2], row[4], row[3][:12], row[0][:12]) for row in wanted] + return "\n".join(lines) + + +def main(argv): + command = argv[1:] + if command == ["status"]: + root = store_root(os.environ) + print(status(root) if root is not None else "droiddeck-esync: no store at %s" % (Path(os.environ.get("HOME") or os.path.expanduser("~")) / STORE)) + return 0 + if len(command) == 2 and command[0] == "reconcile": + if store_root(os.environ) is None: + print("droiddeck-esync: no store") + return 0 + summary = reconcile(command[1], os.environ) + if summary is None: + return 1 + print(summary) + return 0 + if command == ["gc"]: + root = store_root(os.environ) + print("droiddeck-esync: %d dist(s) removed" % gc(root) if root is not None else "droiddeck-esync: no store") + return 0 + if len(command) == 4 and command[0] == "build-shadow": + print(build_shadow(command[1], command[2], command[3])) + return 0 + print("usage: droiddeck-esync status|reconcile |gc|build-shadow ", file=sys.stderr) + return 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv))