-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
guestagent: add support for QEMU guest agent
* Introduce delimiter and skipping to JSON parser * Add support for setting up and connecting to GA via SPICE * New class to handle GA commands
- Loading branch information
Showing
12 changed files
with
212 additions
and
10 deletions.
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
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
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
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,35 @@ | ||
// | ||
// Copyright © 2023 osy. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import "UTMQemuManager.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/// Interface with QEMU Guest Agent | ||
@interface UTMQemuGuestAgent : UTMQemuManager | ||
|
||
/// Attempt synchronization with guest agent | ||
/// | ||
/// If an error is returned, any number of things could have happened including: | ||
/// * Guest Agent has not started on the guest side | ||
/// * Guest Agent has not been installed yet | ||
/// * Guest Agent is too slow to respond | ||
/// - Parameter completion: Callback to run on completion | ||
- (void)synchronizeWithCompletion:(void (^ _Nullable)(NSError * _Nullable))completion; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
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,106 @@ | ||
// | ||
// Copyright © 2023 osy. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#import "UTMQemuGuestAgent.h" | ||
#import "UTMQemuManager-Protected.h" | ||
#import "qga-qapi-commands.h" | ||
|
||
extern NSString *const kUTMErrorDomain; | ||
|
||
@interface UTMQemuGuestAgent () | ||
|
||
@property (nonatomic) BOOL isGuestAgentResponsive; | ||
@property (nonatomic, readwrite) BOOL shouldSynchronizeParser; | ||
@property (nonatomic) dispatch_queue_t guestAgentQueue; | ||
|
||
@end | ||
|
||
@implementation UTMQemuGuestAgent | ||
|
||
- (NSInteger)timeoutSeconds { | ||
if (self.isGuestAgentResponsive) { | ||
return 10; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
|
||
- (instancetype)initWithPort:(CSPort *)port { | ||
if (self = [super initWithPort:port]) { | ||
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, QOS_MIN_RELATIVE_PRIORITY); | ||
self.guestAgentQueue = dispatch_queue_create("QEMU Guest Agent Server", attr); | ||
} | ||
return self; | ||
} | ||
|
||
- (void)jsonStream:(UTMJSONStream *)stream seenError:(NSError *)error { | ||
self.isGuestAgentResponsive = NO; | ||
[super jsonStream:stream seenError:error]; | ||
} | ||
|
||
- (void)synchronizeWithCompletion:(void (^ _Nullable)(NSError * _Nullable))completion { | ||
self.isGuestAgentResponsive = NO; | ||
dispatch_async(self.guestAgentQueue, ^{ | ||
Error *qerr = NULL; | ||
int64_t random = g_random_int(); | ||
int64_t response = 0; | ||
self.shouldSynchronizeParser = YES; | ||
response = qmp_guest_sync_delimited(random, &qerr, (__bridge void *)self); | ||
self.shouldSynchronizeParser = NO; | ||
if (qerr) { | ||
if (completion) { | ||
completion([self errorForQerror:qerr]); | ||
} | ||
return; | ||
} | ||
if (response != random) { | ||
if (completion) { | ||
completion([NSError errorWithDomain:kUTMErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Mismatched id from guest-sync-delimited.", "UTMQemuGuestAgent")}]); | ||
} | ||
return; | ||
} | ||
self.isGuestAgentResponsive = YES; | ||
if (completion) { | ||
completion(nil); | ||
} | ||
}); | ||
} | ||
|
||
- (void)_withSynchronizeBlock:(NSError * _Nullable (^)(void))block withCompletion:(void (^ _Nullable)(NSError * _Nullable))completion { | ||
dispatch_async(self.guestAgentQueue, ^{ | ||
if (!self.isGuestAgentResponsive) { | ||
[self synchronizeWithCompletion:^(NSError *error) { | ||
if (error) { | ||
if (completion) { | ||
completion(error); | ||
} | ||
} else { | ||
NSError *error = block(); | ||
if (completion) { | ||
completion(error); | ||
} | ||
} | ||
}]; | ||
} else { | ||
NSError *error = block(); | ||
if (completion) { | ||
completion(error); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@end |
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
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
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
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
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
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
Oops, something went wrong.