fix: report known catalog conflicts before WMI startup
This commit is contained in:
1 parent
0ee24d7beb
commit
75612cd4da
4 files changed
+88
-17
No files matched your search
@@ -1134,7 +1134,7 @@ def _open_catalog_manager_library(pid):
|
||||
return next(iter(paths)) if len(paths) == 1 else None
|
||||
|
||||
|
||||
def _check_catalog_owners(library, processes):
|
||||
def _check_catalog_owners(library, processes, *, known_conflicts_only=False):
|
||||
"""Screen existing managers before starting a separately owned catalog instance."""
|
||||
library = Path(library).resolve()
|
||||
manager_pids = [pid for pid, *_ in processes]
|
||||
@@ -1158,9 +1158,13 @@ def _check_catalog_owners(library, processes):
|
||||
observed_libraries[pid] = str(other)
|
||||
continue
|
||||
except (OSError, ValueError) as error:
|
||||
if known_conflicts_only:
|
||||
continue
|
||||
detail = f'无法确认另一中心库的资源范围:{error}'
|
||||
raise NativeOperationError(f'LIBRARY_MANAGER_BUSY: PID {pid} · {detail}。',
|
||||
safe_to_continue=True, blocking_manager_pids=(pid,))
|
||||
if known_conflicts_only:
|
||||
return observed_libraries
|
||||
try:
|
||||
_probe_catalog_files(library)
|
||||
except OSError as error:
|
||||
@@ -1204,10 +1208,20 @@ def _catalog_com(job, root):
|
||||
desktop, name = _current_desktop()
|
||||
if name != job['desktop'] or not name.startswith('CbbManagement_'):
|
||||
raise RuntimeError('拒绝在非本次私有桌面读取原生目录。')
|
||||
read_registrations = (job.get('worker_role') == 'catalog_readonly'
|
||||
and not job.get('include_engineering_identity', True))
|
||||
if read_registrations:
|
||||
from .owner_probe import snapshot_process_ids
|
||||
try:
|
||||
pids = snapshot_process_ids('LibraryManager.exe')
|
||||
if pids:
|
||||
_check_catalog_owners(job['library'], [(pid,) for pid in pids], known_conflicts_only=True)
|
||||
except OSError:
|
||||
pass # The full owner and file checks still run before activation.
|
||||
previous_managers = _process_details('LibraryManager.exe')
|
||||
existing_libraries = {}
|
||||
if previous_managers:
|
||||
if job.get('worker_role') != 'catalog_readonly' or job.get('include_engineering_identity', True):
|
||||
if not read_registrations:
|
||||
raise RuntimeError('LIBRARY_MANAGER_BUSY: 已有 Library Manager 运行,请关闭后重试。')
|
||||
expected = Path(job['mentor_home'])/'lm/win32/bin/LibraryManager.exe'
|
||||
if hash_file(expected) != _BINARY_PROOF['lm/win32/bin/LibraryManager.exe']:
|
||||
|
||||
@@ -7,6 +7,42 @@ import sys
|
||||
import tempfile
|
||||
|
||||
|
||||
def snapshot_process_ids(name):
|
||||
"""Read executable names from the local process table without starting WMI."""
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
|
||||
class ProcessEntry(ctypes.Structure):
|
||||
_fields_ = [('size', wintypes.DWORD), ('usage', wintypes.DWORD), ('pid', wintypes.DWORD),
|
||||
('heap_id', ctypes.c_size_t), ('module_id', wintypes.DWORD), ('threads', wintypes.DWORD),
|
||||
('parent', wintypes.DWORD), ('priority', wintypes.LONG), ('flags', wintypes.DWORD),
|
||||
('name', wintypes.WCHAR * 260)]
|
||||
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
kernel.CreateToolhelp32Snapshot.argtypes = [wintypes.DWORD, wintypes.DWORD]
|
||||
kernel.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
|
||||
kernel.CloseHandle.argtypes = [wintypes.HANDLE]
|
||||
for step in (kernel.Process32FirstW, kernel.Process32NextW):
|
||||
step.argtypes = [wintypes.HANDLE, ctypes.POINTER(ProcessEntry)]
|
||||
step.restype = wintypes.BOOL
|
||||
handle = kernel.CreateToolhelp32Snapshot(2, 0) # TH32CS_SNAPPROCESS
|
||||
if handle == ctypes.c_void_p(-1).value:
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
try:
|
||||
entry = ProcessEntry(size=ctypes.sizeof(ProcessEntry))
|
||||
found = []
|
||||
processing = kernel.Process32FirstW(handle, ctypes.byref(entry))
|
||||
while processing:
|
||||
if entry.name.casefold() == name.casefold():
|
||||
found.append(entry.pid)
|
||||
processing = kernel.Process32NextW(handle, ctypes.byref(entry))
|
||||
if ctypes.get_last_error() != 18: # ERROR_NO_MORE_FILES
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
return found
|
||||
finally:
|
||||
kernel.CloseHandle(handle)
|
||||
|
||||
|
||||
def query_owners(names, *, timeout_seconds=10):
|
||||
names = tuple(names)
|
||||
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
|
||||
|
||||
Reference in new issue
Block a user