UTMScriptingGuestProcessImpl.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // Copyright © 2023 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import Foundation
  17. @MainActor
  18. @objc(UTMScriptingGuestProcessImpl)
  19. class UTMScriptingGuestProcessImpl: NSObject, UTMScriptable {
  20. @objc private(set) var id: Int
  21. private var parent: UTMScriptingVirtualMachineImpl
  22. init(from pid: Int, parent: UTMScriptingVirtualMachineImpl) {
  23. self.id = pid
  24. self.parent = parent
  25. }
  26. override var objectSpecifier: NSScriptObjectSpecifier? {
  27. guard let parentDescription = parent.classDescription as? NSScriptClassDescription else {
  28. return nil
  29. }
  30. let parentSpecifier = parent.objectSpecifier
  31. return NSUniqueIDSpecifier(containerClassDescription: parentDescription,
  32. containerSpecifier: parentSpecifier,
  33. key: "processes",
  34. uniqueID: id)
  35. }
  36. @objc func getResult(_ command: NSScriptCommand) {
  37. withScriptCommand(command) { [self] in
  38. guard let guestAgent = await parent.guestAgent else {
  39. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  40. }
  41. let status = try await guestAgent.guestExecStatus(id)
  42. return [
  43. "hasExited": status.hasExited,
  44. "exitCode": status.exitCode,
  45. "signalCode": status.signal,
  46. "outputText": textFromData(status.outData) ?? "",
  47. "outputData": textFromData(status.outData, isBase64Encoded: true) ?? "",
  48. "errorText": textFromData(status.errData) ?? "",
  49. "errorData": textFromData(status.errData, isBase64Encoded: true) ?? "",
  50. ]
  51. }
  52. }
  53. }