forked from FabricMC/fabric-loom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explore looking up the process holding a file handle.
- Loading branch information
Showing
12 changed files
with
514 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,6 @@ | |
!/checkstyle.xml | ||
!/codenarc.groovy | ||
!/bootstrap | ||
!/native | ||
|
||
/src/**/generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Ignore everything | ||
/* | ||
|
||
!/src | ||
!/.vscode | ||
|
||
!/build.gradle | ||
!/.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**", | ||
"${workspaceFolder}/../build/generated/sources/headers/java/main", | ||
"${env:JAVA_HOME}/include", | ||
"${env:JAVA_HOME}/include/win32" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"windowsSdkVersion": "10.0.22621.0", | ||
"compilerPath": "cl.exe", | ||
"cStandard": "c17", | ||
"cppStandard": "c++20", | ||
"intelliSenseMode": "windows-msvc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{ | ||
"files.associations": { | ||
"optional": "cpp", | ||
"format": "cpp", | ||
"system_error": "cpp", | ||
"filesystem": "cpp", | ||
"algorithm": "cpp", | ||
"array": "cpp", | ||
"atomic": "cpp", | ||
"bit": "cpp", | ||
"cctype": "cpp", | ||
"charconv": "cpp", | ||
"chrono": "cpp", | ||
"clocale": "cpp", | ||
"cmath": "cpp", | ||
"compare": "cpp", | ||
"concepts": "cpp", | ||
"coroutine": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"exception": "cpp", | ||
"forward_list": "cpp", | ||
"initializer_list": "cpp", | ||
"iomanip": "cpp", | ||
"ios": "cpp", | ||
"iosfwd": "cpp", | ||
"istream": "cpp", | ||
"iterator": "cpp", | ||
"limits": "cpp", | ||
"list": "cpp", | ||
"locale": "cpp", | ||
"map": "cpp", | ||
"memory": "cpp", | ||
"new": "cpp", | ||
"ostream": "cpp", | ||
"ratio": "cpp", | ||
"sstream": "cpp", | ||
"stdexcept": "cpp", | ||
"stop_token": "cpp", | ||
"streambuf": "cpp", | ||
"string": "cpp", | ||
"thread": "cpp", | ||
"tuple": "cpp", | ||
"type_traits": "cpp", | ||
"typeinfo": "cpp", | ||
"unordered_map": "cpp", | ||
"utility": "cpp", | ||
"vector": "cpp", | ||
"xfacet": "cpp", | ||
"xhash": "cpp", | ||
"xiosbase": "cpp", | ||
"xlocale": "cpp", | ||
"xlocbuf": "cpp", | ||
"xlocinfo": "cpp", | ||
"xlocmes": "cpp", | ||
"xlocmon": "cpp", | ||
"xlocnum": "cpp", | ||
"xloctime": "cpp", | ||
"xmemory": "cpp", | ||
"xstring": "cpp", | ||
"xtr1common": "cpp", | ||
"xtree": "cpp", | ||
"xutility": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
plugins { | ||
id 'cpp-library' | ||
id 'cpp-unit-test' | ||
alias(libs.plugins.spotless) | ||
} | ||
|
||
library { | ||
targetMachines = [ | ||
machines.windows.x86, machines.windows.x86_64, machines.windows.architecture("aarch64") | ||
] | ||
} | ||
|
||
library { | ||
linkage = [ | ||
Linkage.SHARED | ||
] | ||
} | ||
|
||
import org.gradle.internal.jvm.Jvm | ||
def jvmHome = Jvm.current().javaHome | ||
|
||
tasks.withType(CppCompile).configureEach { | ||
includes.from(new File(jvmHome, "include")) | ||
includes.from(new File(jvmHome, "include/win32")) | ||
includes.from(project.rootProject.getLayout().buildDirectory.dir("generated/sources/headers/java/main")) | ||
|
||
|
||
compilerArgs.addAll("/std:c++20", "/EHsc") | ||
} | ||
|
||
tasks.withType(AbstractLinkTask).configureEach { | ||
linkerArgs.addAll("Rstrtmgr.lib") | ||
} | ||
|
||
spotless { | ||
lineEndings = com.diffplug.spotless.LineEnding.UNIX | ||
|
||
cpp { | ||
target 'src/main/cpp/**', 'src/main/headers/**', 'src/test/cpp/**' | ||
clangFormat('16.0.0') | ||
licenseHeaderFile(rootProject.file("HEADER")).yearSeparator("-") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* This file is part of fabric-loom, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) 2024 FabricMC | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#include "LoomNativePlatform.hpp" | ||
#include "Raii.hpp" | ||
|
||
#define WIN32_LEAN_AND_MEAN | ||
#include <windows.h> | ||
|
||
#include <restartmanager.h> | ||
|
||
namespace Loom { | ||
namespace { | ||
struct RmSessionRaiiTraits { | ||
using type = DWORD; | ||
static constexpr auto invalid_value = 0; | ||
static void close(type t) noexcept { ::RmEndSession(t); } | ||
}; | ||
using RmSession = RaiiWithInvalidValue<RmSessionRaiiTraits>; | ||
|
||
struct ProcessRaiiTraits { | ||
using type = HANDLE; | ||
static constexpr auto invalid_value = nullptr; | ||
static void close(type t) noexcept { ::CloseHandle(t); } | ||
}; | ||
using Process = RaiiWithInvalidValue<ProcessRaiiTraits>; | ||
|
||
[[noreturn]] inline void throwLastError(const std::string &message) { | ||
auto lastError = GetLastError(); | ||
throw std::system_error(lastError, std::system_category(), message); | ||
} | ||
|
||
RmSession createRmSession() { | ||
DWORD dwSession; | ||
WCHAR szSessionKey[CCH_RM_SESSION_KEY + 1] = {0}; | ||
|
||
if (::RmStartSession(&dwSession, 0, szSessionKey) != ERROR_SUCCESS) { | ||
throwLastError("RmStartSession failed"); | ||
} | ||
|
||
return RmSession{dwSession}; | ||
} | ||
} // namespace | ||
|
||
// https://devblogs.microsoft.com/oldnewthing/20120217-00/?p=8283 | ||
// TODO maybe look into using: | ||
// https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifileisinuse-closefile | ||
std::vector<int> getPidHoldingFileLock(const std::filesystem::path &file) { | ||
std::vector<int> pids; | ||
RmSession session = createRmSession(); | ||
|
||
PCWSTR path_ptr = file.c_str(); | ||
if (::RmRegisterResources(session.get(), 1, &path_ptr, 0, NULL, 0, NULL) != | ||
ERROR_SUCCESS) { | ||
throwLastError("RmRegisterResources failed"); | ||
} | ||
|
||
DWORD dwError; | ||
DWORD dwReason; | ||
UINT nProcInfoNeeded = 64, nProcInfo; | ||
std::vector<RM_PROCESS_INFO> rgpi; | ||
|
||
do { | ||
nProcInfo = 2 * nProcInfoNeeded; | ||
nProcInfoNeeded = 0; | ||
rgpi.resize(nProcInfo); | ||
dwError = RmGetList(session.get(), &nProcInfoNeeded, &nProcInfo, | ||
rgpi.data(), &dwReason); | ||
} while (dwError == ERROR_MORE_DATA); | ||
|
||
if (dwError != ERROR_SUCCESS) { | ||
throwLastError("RmGetList failed"); | ||
} | ||
|
||
for (const auto &info : rgpi) { | ||
Process process(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, | ||
info.Process.dwProcessId)); | ||
if (process) { | ||
// Ensure that the process start time matches the one from the | ||
// RM_PROCESS_INFO | ||
FILETIME ftCreate, ftExit, ftKernel, ftUser; | ||
if (GetProcessTimes(process.get(), &ftCreate, &ftExit, &ftKernel, | ||
&ftUser) && | ||
CompareFileTime(&info.Process.ProcessStartTime, &ftCreate) == 0) { | ||
pids.push_back(info.Process.dwProcessId); | ||
} | ||
} | ||
} | ||
|
||
return pids; | ||
} | ||
} // namespace Loom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* This file is part of fabric-loom, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) 2024 FabricMC | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#include "net_fabricmc_loom_util_nativeplatform_LoomNativePlatform.h" | ||
|
||
#include "LoomNativePlatform.hpp" | ||
|
||
#include <string> | ||
|
||
namespace { | ||
std::wstring asWstring(JNIEnv *env, jstring string) { | ||
const jchar *raw = env->GetStringChars(string, 0); | ||
jsize len = env->GetStringLength(string); | ||
|
||
std::wstring_view view(reinterpret_cast<const wchar_t *>(raw), len); | ||
std::wstring value{view.begin(), view.end()}; | ||
|
||
env->ReleaseStringChars(string, raw); | ||
|
||
return value; | ||
} | ||
} // namespace | ||
|
||
JNIEXPORT jint JNICALL | ||
Java_net_fabricmc_loom_util_nativeplatform_LoomNativePlatform_getPidHoldingFileLock( | ||
JNIEnv *env, jclass, jstring path) { | ||
|
||
const auto wpath = asWstring(env, path); | ||
const auto pid = Loom::getPidHoldingFileLock(wpath); | ||
|
||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* This file is part of fabric-loom, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) 2024 FabricMC | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <vector> | ||
|
||
namespace Loom { | ||
std::vector<int> getPidHoldingFileLock(const std::filesystem::path &file); | ||
} |
Oops, something went wrong.