UTMScriptingGuestProcessImpl.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. weak 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 parent = parent else {
  28. return nil
  29. }
  30. guard let parentDescription = parent.classDescription as? NSScriptClassDescription else {
  31. return nil
  32. }
  33. let parentSpecifier = parent.objectSpecifier
  34. return NSUniqueIDSpecifier(containerClassDescription: parentDescription,
  35. containerSpecifier: parentSpecifier,
  36. key: "processes",
  37. uniqueID: id)
  38. }
  39. @objc func getResult(_ command: NSScriptCommand) {
  40. withScriptCommand(command) { [self] in
  41. guard let guestAgent = await parent?.guestAgent else {
  42. throw UTMScriptingVirtualMachineImpl.ScriptingError.guestAgentNotRunning
  43. }
  44. let status = try await guestAgent.guestExecStatus(id)
  45. return [
  46. "hasExited": status.hasExited,
  47. "exitCode": status.exitCode,
  48. "signalCode": status.signal,
  49. "outputText": textFromData(status.outData) ?? "",
  50. "outputData": textFromData(status.outData, isBase64Encoded: true) ?? "",
  51. "errorText": textFromData(status.errData) ?? "",
  52. "errorData": textFromData(status.errData, isBase64Encoded: true) ?? "",
  53. ]
  54. }
  55. }
  56. }