diff --git a/guest/computer-windows.cs b/guest/computer-windows.cs index 84def381e8c4e2bdaaf553fb39787297a777da68..316df9ad2028cabae8ee6f729b8638f5ab34ed17 100644 --- a/guest/computer-windows.cs +++ b/guest/computer-windows.cs @@ -3,6 +3,7 @@ using System.Text; using System.Diagnostics; using System.Threading; using System.Runtime.InteropServices; +using System.Management; public class ComputerProcess { [DllImport("kernel32.dll", CharSet=CharSet.Unicode)] static extern IntPtr CreateJobObject(IntPtr attributes, string name); [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern IntPtr OpenJobObject(uint access,bool inherit,string name); @@ -17,7 +18,7 @@ public class ComputerProcess { [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern IntPtr CreateFile(string path,uint access,uint share,ref SECURITY attributes,uint mode,uint flags,IntPtr template); [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool CreateProcess(string application,StringBuilder command,IntPtr processAttributes,IntPtr threadAttributes,bool inherit,uint flags,IntPtr environment,string directory,ref STARTUP startup,out CREATED created); [DllImport("kernel32.dll",SetLastError=true)] static extern uint ResumeThread(IntPtr thread); - [DllImport("kernel32.dll")] static extern bool TerminateProcess(IntPtr process,uint code); + [DllImport("kernel32.dll",SetLastError=true)] internal static extern bool TerminateProcess(IntPtr process,uint code); [DllImport("kernel32.dll")] static extern uint WaitForSingleObject(IntPtr handle,uint milliseconds); [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetExitCodeProcess(IntPtr process,out uint code); [StructLayout(LayoutKind.Sequential)] struct BASICLIMIT { public long perProcess,perJob; public uint flags; public UIntPtr minWorking,maxWorking; public uint active; public UIntPtr affinity; public uint priority,scheduling; } @@ -120,14 +121,84 @@ public static class ComputerWorker { static string Text(System.Collections.Generic.Dictionary value, string key, string fallback = "") { return Convert.ToString(Get(value,key,fallback)); } static int Number(System.Collections.Generic.Dictionary value, string key, int fallback = 0) { return Convert.ToInt32(Get(value,key,fallback)); } [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); - [STAThread] public static void Main(string[] args) { - if(args.Length != 2) return; + [STAThread] public static int Main(string[] args) { + if(args.Length==2 && (args[0]=="--prepare" || args[0]=="--cleanup" || args[0]=="--release")) { + try { + if(args[0]=="--prepare")System.IO.Directory.CreateDirectory(args[1]); + else Cleanup(args[1],args[0]=="--release"); + return 0; + } + catch { return 1; } + } + if(args.Length==3 && args[0]=="--launch") { + requestPath=args[1];responsePath=args[2]; + try { Launch(); } + catch(Exception error) { Reply(new {error=error.Message}); } + return 0; + } + if(args.Length != 2) return 1; requestPath=args[0];responsePath=args[1]; object result; try { SetProcessDPIAware(); result=Run(Map(json.DeserializeObject(System.IO.File.ReadAllText(requestPath)))); } catch(Exception error) { result=new { error=error.Message }; } Reply(result); + return 0; } static void Reply(object result) {System.IO.File.WriteAllText(responsePath,json.Serialize(result),new UTF8Encoding(false));} + static void Launch() { + var request=Map(json.DeserializeObject(System.IO.File.ReadAllText(requestPath))); + string user=null; + using(var query=new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")) + using(var rows=query.Get()) + foreach(ManagementObject row in rows) + using(row) { user=row["UserName"] as string; } + if(String.IsNullOrEmpty(user))throw new Exception("Sign in to the VM desktop before running commands."); + dynamic service=Activator.CreateInstance(Type.GetTypeFromProgID("Schedule.Service")); + service.Connect(null,null,null,null); + dynamic task=service.NewTask(0); + task.Principal.UserId=user; + task.Principal.LogonType=3; + task.Principal.RunLevel=1; + task.Settings.ExecutionTimeLimit=Text(request,"action")=="spawn" ? "PT0S" : "PT210S"; + task.Settings.Priority=4; + task.Settings.DisallowStartIfOnBatteries=false; + task.Settings.StopIfGoingOnBatteries=false; + dynamic action=task.Actions.Create(0); + action.Path=System.Reflection.Assembly.GetExecutingAssembly().Location; + action.Arguments="\""+requestPath+"\" \""+responsePath+"\""; + string name="SnowglobeComputer-"+System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(requestPath)); + dynamic registered=service.GetFolder("\\").RegisterTaskDefinition(name,task,6,user,null,3,null); + registered.Run(null); + } + static void Cleanup(string directory,bool keepSpawn) { + string request=System.IO.Path.Combine(directory,"request.json"); + if(!keepSpawn) { + if(System.IO.File.Exists(request))System.IO.File.Delete(request); + ComputerProcess.Stop(request); + using(var query=new ManagementObjectSearcher("SELECT ProcessId,CommandLine FROM Win32_Process")) + using(var rows=query.Get()) + foreach(ManagementObject row in rows) + using(row) { + if((row["CommandLine"] as string ?? "").IndexOf(request,StringComparison.OrdinalIgnoreCase)<0)continue; + try { + using(var process=Process.GetProcessById(Convert.ToInt32(row["ProcessId"]))) { + IntPtr handle=process.Handle; + row.Get(); + if(process.HasExited || (row["CommandLine"] as string ?? "").IndexOf(request,StringComparison.OrdinalIgnoreCase)<0)continue; + if(!ComputerProcess.TerminateProcess(handle,124) && !process.HasExited)throw new System.ComponentModel.Win32Exception(); + if(!process.WaitForExit(10000))throw new Exception("The command worker could not stop."); + } + } catch(ArgumentException) {} catch(ManagementException error) { + if(error.ErrorCode!=ManagementStatus.NotFound)throw; + } + } + } + dynamic service=Activator.CreateInstance(Type.GetTypeFromProgID("Schedule.Service")); + service.Connect(null,null,null,null); + dynamic folder=service.GetFolder("\\"); + try { folder.DeleteTask("SnowglobeComputer-"+System.IO.Path.GetFileName(directory),0); } + catch(COMException error) { if(error.ErrorCode!=unchecked((int)0x80070002))throw; } + if(!keepSpawn && System.IO.Directory.Exists(directory))System.IO.Directory.Delete(directory,true); + } static object Target(System.Windows.Automation.AutomationElement node) { var current=node.Current; using(var process=Process.GetProcessById(current.ProcessId)) { diff --git a/tools/computer-test.py b/tools/computer-test.py index 5214abb95ca2b7b2b302c3268459a6bab8762e5d..2098557c19a50642a535c76a82dcf57a394d40a5 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -117,19 +117,18 @@ class GuestCancellation(unittest.TestCase): def test_windows_stops_guest_task_after_its_operation_budget_expires(self): running = [] - def execute(name, script, **kwargs): - if 'RegisterTaskDefinition' in script: + def execute(name, executable, arguments, **kwargs): + if arguments[0] == '--launch': running.append(True) computer.DEADLINE.set(time.monotonic() - 1) return b'' - if '$t.Stop(0)' in script: + if arguments[0] == '--cleanup': self.assertGreater(computer.remaining(20), 0) running.clear() return b'' - return b'[true,true]' token = computer.DEADLINE.set(time.monotonic() + 30) try: - with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'): + with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'M'): with self.assertRaises(TimeoutError): computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]}) self.assertEqual(running, []) @@ -320,18 +319,18 @@ class GuestCancellation(unittest.TestCase): def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self): result = {'id': '123:456', 'running': True} - def execute(name, script, **kwargs): - if 'DeleteTask' in script: + def execute(name, executable, arguments, **kwargs): + if arguments[0] == '--release': raise TimeoutError() - return b'[true,true]' - with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): + return b'' + with patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result) pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' operation = json.loads(pending.read_text()) self.assertTrue(operation['keep_spawn']) - with patch.object(computer, 'powershell', return_value=b'') as execute: + with patch.object(computer, 'guest_exec', return_value=b'') as execute: computer.finish_operation('fixture', operation) - self.assertNotIn('$t.Stop(0)', execute.call_args.args[1]) + self.assertEqual(execute.call_args.args[2][0], '--release') self.assertFalse(pending.exists()) def test_spawn_is_stopped_if_its_success_cannot_be_persisted(self): @@ -340,12 +339,30 @@ class GuestCancellation(unittest.TestCase): if operation.get('keep_spawn'): raise OSError('Disk full') record(name, operation) - with patch.object(computer, 'record_operation', persist), patch.object(computer, 'powershell', return_value=b'[true,true]') as execute, patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'{"id":"123:456"}'): + with patch.object(computer, 'record_operation', persist), patch.object(computer, 'powershell', return_value=b'[true,true]'), patch.object(computer, 'guest_exec', return_value=b'') as execute, patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'{"id":"123:456"}'): with self.assertRaises(OSError): computer.worker('fixture', 'windows', {'action': 'spawn'}) - self.assertIn('$t.Stop(0)', execute.call_args.args[1]) + self.assertEqual(execute.call_args.args[2][0], '--cleanup') self.assertFalse((computer.vms.DISKS / 'fixture' / 'computer-operation.json').exists()) + def test_cleanup_uses_the_worker_that_started_the_operation(self): + operation = {'platform': 'windows', 'id': 'a' * 32, + 'worker': 'C:\\ProgramData\\SnowglobeComputer\\worker-previous.exe'} + computer.record_operation('fixture', operation) + with patch.object(computer, 'guest_exec', return_value=b'') as execute, patch.object(computer, 'powershell') as legacy: + computer.finish_operation('fixture', operation) + self.assertEqual(execute.call_args.args[1], operation['worker']) + self.assertEqual(execute.call_args.args[2][0], '--cleanup') + legacy.assert_not_called() + + def test_warm_windows_commands_do_not_start_powershell_for_coordination(self): + def read(name, path, **kwargs): + return b'{"exit":0,"stdout":"ready"}' if path.endswith('response.json') else b'M' + with patch.object(computer, 'read_file', read), patch.object(computer, 'write_file'), patch.object(computer, 'guest_exec', return_value=b'') as execute, patch.object(computer, 'powershell', side_effect=AssertionError('Warm coordination must stay native')): + result = computer.worker('fixture', 'windows', {'action': 'exec', 'runtime': 'cmd', 'script': 'echo ready'}) + self.assertEqual(result['stdout'], 'ready') + self.assertEqual([call.args[2][0] for call in execute.call_args_list], ['--prepare', '--launch', '--cleanup']) + def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self): units = [] def execute(name, executable, arguments, data=None, **kwargs): diff --git a/tools/vm-computer.py b/tools/vm-computer.py index 42ea10a20c583023b9c87f5d2c26e505a35347b8..3f2d06a0c3c41bf9e264d50bd17fe7b54a8573e7 100644 --- a/tools/vm-computer.py +++ b/tools/vm-computer.py @@ -370,6 +370,9 @@ def finish_operation(name, operation): if operation['platform'] == 'linux': guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], json.dumps({'_stop': operation['id']}).encode(), timeout=18) + elif operation.get('worker'): + directory = 'C:\\ProgramData\\SnowglobeComputer\\' + operation['id'] + guest_exec(name, operation['worker'], ['--release' if operation.get('keep_spawn') else '--cleanup', directory], timeout=58) else: run = operation['id'] directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run @@ -437,9 +440,20 @@ def worker(name, platform, request): content = source.read_bytes() digest = hashlib.sha256(content).hexdigest()[:16] installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' - setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; " - setup += "@((Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe),(Test-Path -LiteralPath '" + installed_worker + "')) | ConvertTo-Json -Compress" - installed, exists = json.loads(powershell(name,setup)) + try: + read_file(name, installed_worker, length=1) + except ValueError: + setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; " + setup += "@((Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe),(Test-Path -LiteralPath '" + installed_worker + "')) | ConvertTo-Json -Compress" + installed, exists = json.loads(powershell(name,setup)) + else: + exists = True + guest_exec(name, installed_worker, ['--prepare', directory]) + try: + read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) + installed = True + except ValueError: + installed = False ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') if not installed and ahk: write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) @@ -455,7 +469,7 @@ if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return} try { if(Test-Path -LiteralPath 'WORKER'){return} $references=@('System.dll','System.Core.dll') -foreach($assembly in @('System.Web.Extensions','UIAutomationClient','UIAutomationTypes','WindowsBase')) { +foreach($assembly in @('System.Web.Extensions','System.Management','Microsoft.CSharp','UIAutomationClient','UIAutomationTypes','WindowsBase')) { $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location } Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue @@ -465,31 +479,10 @@ Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER' """.replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest) if powershell(name, compile_script, timeout=60).strip() == b'pending': raise ValueError('Computer tools are still installing. Retry in a moment.') - launch = """ -$ErrorActionPreference='Stop' -$d='DIRECTORY' -$user=(Get-WmiObject Win32_ComputerSystem).UserName -if(!$user){throw 'No interactive user'} -$service=New-Object -ComObject Schedule.Service -$service.Connect() -$task=$service.NewTask(0) -$task.Principal.UserId=$user -$task.Principal.LogonType=3 -$task.Principal.RunLevel=1 -$task.Settings.ExecutionTimeLimit='TASKLIMIT' -$task.Settings.Priority=4 -$task.Settings.DisallowStartIfOnBatteries=$false -$task.Settings.StopIfGoingOnBatteries=$false -$action=$task.Actions.Create(0) -$action.Path='WORKER' -$action.Arguments='"'+$d+'\\request.json" "'+$d+'\\response.json"' -$registered=$service.GetFolder('\\').RegisterTaskDefinition('SnowglobeComputer-RUN',$task,6,$user,$null,3) -$registered.Run($null) | Out-Null -""".replace('DIRECTORY',directory).replace('RUN',run).replace('WORKER',installed_worker).replace('TASKLIMIT','PT0S' if request['action'] == 'spawn' else 'PT210S') - operation = {'platform': platform, 'id': run} + operation = {'platform': platform, 'id': run, 'worker': installed_worker} record_operation(name, operation) try: - powershell(name,launch) + guest_exec(name, installed_worker, ['--launch', directory+'\\request.json', directory+'\\response.json']) deadline=time.monotonic()+remaining(200) while time.monotonic()