1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
|
/* $Id: GuestImpl.cpp $ */
/** @file
*
* VirtualBox COM class implementation
*/
/*
* Copyright (C) 2006-2008 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "GuestImpl.h"
#include "Global.h"
#include "ConsoleImpl.h"
#include "ProgressImpl.h"
#include "VMMDev.h"
#include "AutoCaller.h"
#include "Logging.h"
#include <VBox/VMMDev.h>
#ifdef VBOX_WITH_GUEST_CONTROL
# include <VBox/com/array.h>
#endif
#include <iprt/cpp/utils.h>
#include <iprt/getopt.h>
#include <VBox/pgm.h>
// defines
/////////////////////////////////////////////////////////////////////////////
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
DEFINE_EMPTY_CTOR_DTOR (Guest)
HRESULT Guest::FinalConstruct()
{
return S_OK;
}
void Guest::FinalRelease()
{
uninit ();
}
// public methods only for internal purposes
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the guest object.
*/
HRESULT Guest::init (Console *aParent)
{
LogFlowThisFunc(("aParent=%p\n", aParent));
ComAssertRet(aParent, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
unconst(mParent) = aParent;
/* mData.mAdditionsActive is FALSE */
/* Confirm a successful initialization when it's the case */
autoInitSpan.setSucceeded();
ULONG aMemoryBalloonSize;
HRESULT ret = mParent->machine()->COMGETTER(MemoryBalloonSize)(&aMemoryBalloonSize);
if (ret == S_OK)
mMemoryBalloonSize = aMemoryBalloonSize;
else
mMemoryBalloonSize = 0; /* Default is no ballooning */
BOOL fPageFusionEnabled;
ret = mParent->machine()->COMGETTER(PageFusionEnabled)(&fPageFusionEnabled);
if (ret == S_OK)
mfPageFusionEnabled = fPageFusionEnabled;
else
mfPageFusionEnabled = false; /* Default is no page fusion*/
mStatUpdateInterval = 0; /* Default is not to report guest statistics at all */
/* Clear statistics. */
for (unsigned i = 0 ; i < GUESTSTATTYPE_MAX; i++)
mCurrentGuestStat[i] = 0;
#ifdef VBOX_WITH_GUEST_CONTROL
/* Init the context ID counter at 1000. */
mNextContextID = 1000;
#endif
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void Guest::uninit()
{
LogFlowThisFunc(("\n"));
#ifdef VBOX_WITH_GUEST_CONTROL
/*
* Cleanup must be done *before* AutoUninitSpan to cancel all
* all outstanding waits in API functions (which hold AutoCaller
* ref counts).
*/
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* Clean up callback data. */
CallbackListIter it;
for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
destroyCtrlCallbackContext(it);
/* Clear process list. */
mGuestProcessList.clear();
#endif
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
unconst(mParent) = NULL;
}
// IGuest properties
/////////////////////////////////////////////////////////////////////////////
STDMETHODIMP Guest::COMGETTER(OSTypeId) (BSTR *aOSTypeId)
{
CheckComArgOutPointerValid(aOSTypeId);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
// redirect the call to IMachine if no additions are installed
if (mData.mAdditionsVersion.isEmpty())
return mParent->machine()->COMGETTER(OSTypeId)(aOSTypeId);
mData.mOSTypeId.cloneTo(aOSTypeId);
return S_OK;
}
STDMETHODIMP Guest::COMGETTER(AdditionsActive) (BOOL *aAdditionsActive)
{
CheckComArgOutPointerValid(aAdditionsActive);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aAdditionsActive = mData.mAdditionsActive;
return S_OK;
}
STDMETHODIMP Guest::COMGETTER(AdditionsVersion) (BSTR *aAdditionsVersion)
{
CheckComArgOutPointerValid(aAdditionsVersion);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.mAdditionsVersion.cloneTo(aAdditionsVersion);
return S_OK;
}
STDMETHODIMP Guest::COMGETTER(SupportsSeamless) (BOOL *aSupportsSeamless)
{
CheckComArgOutPointerValid(aSupportsSeamless);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aSupportsSeamless = mData.mSupportsSeamless;
return S_OK;
}
STDMETHODIMP Guest::COMGETTER(SupportsGraphics) (BOOL *aSupportsGraphics)
{
CheckComArgOutPointerValid(aSupportsGraphics);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aSupportsGraphics = mData.mSupportsGraphics;
return S_OK;
}
STDMETHODIMP Guest::COMGETTER(PageFusionEnabled) (BOOL *aPageFusionEnabled)
{
CheckComArgOutPointerValid(aPageFusionEnabled);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aPageFusionEnabled = mfPageFusionEnabled;
return S_OK;
}
STDMETHODIMP Guest::COMSETTER(PageFusionEnabled) (BOOL aPageFusionEnabled)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/** todo; API complete, but not implemented */
return E_NOTIMPL;
}
STDMETHODIMP Guest::COMGETTER(MemoryBalloonSize) (ULONG *aMemoryBalloonSize)
{
CheckComArgOutPointerValid(aMemoryBalloonSize);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aMemoryBalloonSize = mMemoryBalloonSize;
return S_OK;
}
STDMETHODIMP Guest::COMSETTER(MemoryBalloonSize) (ULONG aMemoryBalloonSize)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* We must be 100% sure that IMachine::COMSETTER(MemoryBalloonSize)
* does not call us back in any way! */
HRESULT ret = mParent->machine()->COMSETTER(MemoryBalloonSize)(aMemoryBalloonSize);
if (ret == S_OK)
{
mMemoryBalloonSize = aMemoryBalloonSize;
/* forward the information to the VMM device */
VMMDev *pVMMDev = mParent->getVMMDev();
if (pVMMDev)
{
PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
if (pVMMDevPort)
pVMMDevPort->pfnSetMemoryBalloon(pVMMDevPort, aMemoryBalloonSize);
}
}
return ret;
}
STDMETHODIMP Guest::COMGETTER(StatisticsUpdateInterval)(ULONG *aUpdateInterval)
{
CheckComArgOutPointerValid(aUpdateInterval);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aUpdateInterval = mStatUpdateInterval;
return S_OK;
}
STDMETHODIMP Guest::COMSETTER(StatisticsUpdateInterval)(ULONG aUpdateInterval)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mStatUpdateInterval = aUpdateInterval;
/* forward the information to the VMM device */
VMMDev *pVMMDev = mParent->getVMMDev();
if (pVMMDev)
{
PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
if (pVMMDevPort)
pVMMDevPort->pfnSetStatisticsInterval(pVMMDevPort, aUpdateInterval);
}
return S_OK;
}
STDMETHODIMP Guest::InternalGetStatistics(ULONG *aCpuUser, ULONG *aCpuKernel, ULONG *aCpuIdle,
ULONG *aMemTotal, ULONG *aMemFree, ULONG *aMemBalloon, ULONG *aMemShared,
ULONG *aMemCache, ULONG *aPageTotal,
ULONG *aMemAllocTotal, ULONG *aMemFreeTotal, ULONG *aMemBalloonTotal, ULONG *aMemSharedTotal)
{
CheckComArgOutPointerValid(aCpuUser);
CheckComArgOutPointerValid(aCpuKernel);
CheckComArgOutPointerValid(aCpuIdle);
CheckComArgOutPointerValid(aMemTotal);
CheckComArgOutPointerValid(aMemFree);
CheckComArgOutPointerValid(aMemBalloon);
CheckComArgOutPointerValid(aMemShared);
CheckComArgOutPointerValid(aMemCache);
CheckComArgOutPointerValid(aPageTotal);
CheckComArgOutPointerValid(aMemAllocTotal);
CheckComArgOutPointerValid(aMemFreeTotal);
CheckComArgOutPointerValid(aMemBalloonTotal);
CheckComArgOutPointerValid(aMemSharedTotal);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aCpuUser = mCurrentGuestStat[GUESTSTATTYPE_CPUUSER];
*aCpuKernel = mCurrentGuestStat[GUESTSTATTYPE_CPUKERNEL];
*aCpuIdle = mCurrentGuestStat[GUESTSTATTYPE_CPUIDLE];
*aMemTotal = mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
*aMemFree = mCurrentGuestStat[GUESTSTATTYPE_MEMFREE] * (_4K/_1K); /* page (4K) -> 1KB units */
*aMemBalloon = mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON] * (_4K/_1K); /* page (4K) -> 1KB units */
*aMemCache = mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE] * (_4K/_1K); /* page (4K) -> 1KB units */
*aPageTotal = mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
Console::SafeVMPtr pVM (mParent);
if (pVM.isOk())
{
uint64_t uFreeTotal, uAllocTotal, uBalloonedTotal, uSharedTotal;
*aMemFreeTotal = 0;
int rc = PGMR3QueryVMMMemoryStats(pVM.raw(), &uAllocTotal, &uFreeTotal, &uBalloonedTotal, &uSharedTotal);
AssertRC(rc);
if (rc == VINF_SUCCESS)
{
*aMemAllocTotal = (ULONG)(uAllocTotal / _1K); /* bytes -> KB */
*aMemFreeTotal = (ULONG)(uFreeTotal / _1K);
*aMemBalloonTotal = (ULONG)(uBalloonedTotal / _1K);
*aMemSharedTotal = (ULONG)(uSharedTotal / _1K);
}
/* Query the missing per-VM memory statistics. */
*aMemShared = 0;
uint64_t uTotalMem, uPrivateMem, uSharedMem, uZeroMem;
rc = PGMR3QueryMemoryStats(pVM.raw(), &uTotalMem, &uPrivateMem, &uSharedMem, &uZeroMem);
if (rc == VINF_SUCCESS)
{
*aMemShared = (ULONG)(uSharedMem / _1K);
}
}
else
{
*aMemFreeTotal = 0;
*aMemShared = 0;
}
return S_OK;
}
HRESULT Guest::SetStatistic(ULONG aCpuId, GUESTSTATTYPE enmType, ULONG aVal)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (enmType >= GUESTSTATTYPE_MAX)
return E_INVALIDARG;
mCurrentGuestStat[enmType] = aVal;
return S_OK;
}
STDMETHODIMP Guest::SetCredentials(IN_BSTR aUserName, IN_BSTR aPassword,
IN_BSTR aDomain, BOOL aAllowInteractiveLogon)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* forward the information to the VMM device */
VMMDev *pVMMDev = mParent->getVMMDev();
if (pVMMDev)
{
PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
if (pVMMDevPort)
{
uint32_t u32Flags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
if (!aAllowInteractiveLogon)
u32Flags = VMMDEV_SETCREDENTIALS_NOLOCALLOGON;
pVMMDevPort->pfnSetCredentials(pVMMDevPort,
Utf8Str(aUserName).raw(),
Utf8Str(aPassword).raw(),
Utf8Str(aDomain).raw(),
u32Flags);
return S_OK;
}
}
return setError(VBOX_E_VM_ERROR,
tr("VMM device is not available (is the VM running?)"));
}
#ifdef VBOX_WITH_GUEST_CONTROL
/**
* Appends environment variables to the environment block. Each var=value pair is separated
* by NULL (\0) sequence. The whole block will be stored in one blob and disassembled on the
* guest side later to fit into the HGCM param structure.
*
* @returns VBox status code.
*
* @todo
*
*/
int Guest::prepareExecuteEnv(const char *pszEnv, void **ppvList, uint32_t *pcbList, uint32_t *pcEnv)
{
int rc = VINF_SUCCESS;
uint32_t cbLen = strlen(pszEnv);
if (*ppvList)
{
uint32_t cbNewLen = *pcbList + cbLen + 1; /* Include zero termination. */
char *pvTmp = (char*)RTMemRealloc(*ppvList, cbNewLen);
if (NULL == pvTmp)
{
rc = VERR_NO_MEMORY;
}
else
{
memcpy(pvTmp + *pcbList, pszEnv, cbLen);
pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
*ppvList = (void**)pvTmp;
}
}
else
{
char *pcTmp;
if (RTStrAPrintf(&pcTmp, "%s", pszEnv) > 0)
{
*ppvList = (void**)pcTmp;
/* Reset counters. */
*pcEnv = 0;
*pcbList = 0;
}
}
if (RT_SUCCESS(rc))
{
*pcbList += cbLen + 1; /* Include zero termination. */
*pcEnv += 1; /* Increase env pairs count. */
}
return rc;
}
/**
* Static callback function for receiving updates on guest control commands
* from the guest. Acts as a dispatcher for the actual class instance.
*
* @returns VBox status code.
*
* @todo
*
*/
DECLCALLBACK(int) Guest::doGuestCtrlNotification(void *pvExtension,
uint32_t u32Function,
void *pvParms,
uint32_t cbParms)
{
using namespace guestControl;
/*
* No locking, as this is purely a notification which does not make any
* changes to the object state.
*/
LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
pvExtension, u32Function, pvParms, cbParms));
ComObjPtr<Guest> pGuest = reinterpret_cast<Guest *>(pvExtension);
int rc = VINF_SUCCESS;
if (u32Function == GUEST_DISCONNECTED)
{
LogFlowFunc(("GUEST_DISCONNECTED\n"));
PCALLBACKDATACLIENTDISCONNECTED pCBData = reinterpret_cast<PCALLBACKDATACLIENTDISCONNECTED>(pvParms);
AssertPtr(pCBData);
AssertReturn(sizeof(CALLBACKDATACLIENTDISCONNECTED) == cbParms, VERR_INVALID_PARAMETER);
AssertReturn(CALLBACKDATAMAGICCLIENTDISCONNECTED == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
rc = pGuest->notifyCtrlClientDisconnected(u32Function, pCBData);
}
else if (u32Function == GUEST_EXEC_SEND_STATUS)
{
LogFlowFunc(("GUEST_EXEC_SEND_STATUS\n"));
PCALLBACKDATAEXECSTATUS pCBData = reinterpret_cast<PCALLBACKDATAEXECSTATUS>(pvParms);
AssertPtr(pCBData);
AssertReturn(sizeof(CALLBACKDATAEXECSTATUS) == cbParms, VERR_INVALID_PARAMETER);
AssertReturn(CALLBACKDATAMAGICEXECSTATUS == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
rc = pGuest->notifyCtrlExecStatus(u32Function, pCBData);
}
else if (u32Function == GUEST_EXEC_SEND_OUTPUT)
{
LogFlowFunc(("GUEST_EXEC_SEND_OUTPUT\n"));
PCALLBACKDATAEXECOUT pCBData = reinterpret_cast<PCALLBACKDATAEXECOUT>(pvParms);
AssertPtr(pCBData);
AssertReturn(sizeof(CALLBACKDATAEXECOUT) == cbParms, VERR_INVALID_PARAMETER);
AssertReturn(CALLBACKDATAMAGICEXECOUT == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
rc = pGuest->notifyCtrlExecOut(u32Function, pCBData);
}
else
rc = VERR_NOT_SUPPORTED;
return rc;
}
/* Function for handling the execution start/termination notification. */
int Guest::notifyCtrlExecStatus(uint32_t u32Function,
PCALLBACKDATAEXECSTATUS pData)
{
LogFlowFuncEnter();
int rc = VINF_SUCCESS;
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
AssertPtr(pData);
CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
/* Callback can be called several times. */
if (it != mCallbackList.end())
{
PCALLBACKDATAEXECSTATUS pCBData = (PCALLBACKDATAEXECSTATUS)it->pvData;
AssertPtr(pCBData);
pCBData->u32PID = pData->u32PID;
pCBData->u32Status = pData->u32Status;
pCBData->u32Flags = pData->u32Flags;
/** @todo Copy void* buffer contents! */
/* Was progress canceled before? */
BOOL fCancelled;
it->pProgress->COMGETTER(Canceled)(&fCancelled);
/* Do progress handling. */
Utf8Str errMsg;
HRESULT rc2 = S_OK;
switch (pData->u32Status)
{
case PROC_STS_STARTED:
break;
case PROC_STS_TEN: /* Terminated normally. */
if ( !it->pProgress->getCompleted()
&& !fCancelled)
{
it->pProgress->notifyComplete(S_OK);
}
break;
case PROC_STS_TEA: /* Terminated abnormally. */
errMsg = Utf8StrFmt(Guest::tr("Process terminated abnormally with status '%u'"),
pCBData->u32Flags);
break;
case PROC_STS_TES: /* Terminated through signal. */
errMsg = Utf8StrFmt(Guest::tr("Process terminated via signal with status '%u'"),
pCBData->u32Flags);
break;
case PROC_STS_TOK:
errMsg = Utf8StrFmt(Guest::tr("Process timed out and was killed"));
break;
case PROC_STS_TOA:
errMsg = Utf8StrFmt(Guest::tr("Process timed out and could not be killed"));
break;
case PROC_STS_DWN:
errMsg = Utf8StrFmt(Guest::tr("Process exited because system is shutting down"));
break;
default:
break;
}
/* Handle process list. */
/** @todo What happens on/deal with PID reuse? */
/** @todo How to deal with multiple updates at once? */
GuestProcessIter it_proc = getProcessByPID(pCBData->u32PID);
if (it_proc == mGuestProcessList.end())
{
/* Not found, add to list. */
GuestProcess p;
p.mPID = pCBData->u32PID;
p.mStatus = pCBData->u32Status;
p.mExitCode = pCBData->u32Flags; /* Contains exit code. */
p.mFlags = 0;
mGuestProcessList.push_back(p);
}
else /* Update list. */
{
it_proc->mStatus = pCBData->u32Status;
it_proc->mExitCode = pCBData->u32Flags; /* Contains exit code. */
it_proc->mFlags = 0;
}
if ( !it->pProgress.isNull()
&& errMsg.length())
{
if ( !it->pProgress->getCompleted()
&& !fCancelled)
{
it->pProgress->notifyComplete(E_FAIL /** @todo Find a better rc! */, COM_IIDOF(IGuest),
(CBSTR)Guest::getComponentName(), errMsg.c_str());
LogFlowFunc(("Callback (context ID=%u, status=%u) progress marked as completed\n",
pData->hdr.u32ContextID, pData->u32Status));
}
else
LogFlowFunc(("Callback (context ID=%u, status=%u) progress already marked as completed\n",
pData->hdr.u32ContextID, pData->u32Status));
}
ASMAtomicWriteBool(&it->fCalled, true);
}
else
LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
LogFlowFuncLeave();
return rc;
}
/* Function for handling the execution output notification. */
int Guest::notifyCtrlExecOut(uint32_t u32Function,
PCALLBACKDATAEXECOUT pData)
{
LogFlowFuncEnter();
int rc = VINF_SUCCESS;
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
AssertPtr(pData);
CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
if (it != mCallbackList.end())
{
Assert(!it->fCalled);
PCALLBACKDATAEXECOUT pCBData = (CALLBACKDATAEXECOUT*)it->pvData;
AssertPtr(pCBData);
pCBData->u32PID = pData->u32PID;
pCBData->u32HandleId = pData->u32HandleId;
pCBData->u32Flags = pData->u32Flags;
/* Make sure we really got something! */
if ( pData->cbData
&& pData->pvData)
{
/* Allocate data buffer and copy it */
pCBData->pvData = RTMemAlloc(pData->cbData);
pCBData->cbData = pData->cbData;
AssertReturn(pCBData->pvData, VERR_NO_MEMORY);
memcpy(pCBData->pvData, pData->pvData, pData->cbData);
}
else
{
pCBData->pvData = NULL;
pCBData->cbData = 0;
}
ASMAtomicWriteBool(&it->fCalled, true);
}
else
LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
LogFlowFuncLeave();
return rc;
}
int Guest::notifyCtrlClientDisconnected(uint32_t u32Function,
PCALLBACKDATACLIENTDISCONNECTED pData)
{
LogFlowFuncEnter();
int rc = VINF_SUCCESS;
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/** @todo Maybe use a map instead of list for fast context lookup. */
CallbackListIter it;
for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
{
if (it->mContextID == pData->hdr.u32ContextID)
destroyCtrlCallbackContext(it);
}
LogFlowFuncLeave();
return rc;
}
Guest::CallbackListIter Guest::getCtrlCallbackContextByID(uint32_t u32ContextID)
{
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
/** @todo Maybe use a map instead of list for fast context lookup. */
CallbackListIter it;
for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
{
if (it->mContextID == u32ContextID)
return (it);
}
return it;
}
Guest::GuestProcessIter Guest::getProcessByPID(uint32_t u32PID)
{
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
/** @todo Maybe use a map instead of list for fast context lookup. */
GuestProcessIter it;
for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
{
if (it->mPID == u32PID)
return (it);
}
return it;
}
/* No locking here; */
void Guest::destroyCtrlCallbackContext(Guest::CallbackListIter it)
{
LogFlowFuncEnter();
if (it->pvData)
{
RTMemFree(it->pvData);
it->pvData = NULL;
it->cbData = 0;
}
/* Notify outstanding waits for progress ... */
if (it->pProgress && !it->pProgress.isNull())
{
/* Only cancel if not canceled before! */
BOOL fCancelled;
if (SUCCEEDED(it->pProgress->COMGETTER(Canceled)(&fCancelled)) && !fCancelled)
it->pProgress->Cancel();
/*
* Do *not NULL pProgress here, because waiting function like executeProcess()
* will still rely on this object for checking whether they have to give up!
*/
}
LogFlowFuncLeave();
}
/* Adds a callback with a user provided data block and an optional progress object
* to the callback list. A callback is identified by a unique context ID which is used
* to identify a callback from the guest side. */
uint32_t Guest::addCtrlCallbackContext(eVBoxGuestCtrlCallbackType enmType, void *pvData, uint32_t cbData, Progress *pProgress)
{
LogFlowFuncEnter();
uint32_t uNewContext = ASMAtomicIncU32(&mNextContextID);
if (uNewContext == UINT32_MAX)
ASMAtomicUoWriteU32(&mNextContextID, 1000);
/** @todo Put this stuff into a constructor! */
CallbackContext context;
context.mContextID = uNewContext;
context.mType = enmType;
context.fCalled = false;
context.pvData = pvData;
context.cbData = cbData;
context.pProgress = pProgress;
uint32_t nCallbacks;
{
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/// @todo r=bird: check if already in the list and find another one.
mCallbackList.push_back(context);
nCallbacks = mCallbackList.size();
}
#if 0
if (nCallbacks > 256) /* Don't let the container size get too big! */
{
Guest::CallbackListIter it = mCallbackList.begin();
destroyCtrlCallbackContext(it);
{
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mCallbackList.erase(it);
}
}
#endif
LogFlowFuncLeave();
return uNewContext;
}
#endif /* VBOX_WITH_GUEST_CONTROL */
STDMETHODIMP Guest::ExecuteProcess(IN_BSTR aCommand, ULONG aFlags,
ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
IN_BSTR aUserName, IN_BSTR aPassword,
ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
{
/** @todo r=bird: Eventually we should clean up all the timeout parameters
* in the API and have the same way of specifying infinite waits! */
#ifndef VBOX_WITH_GUEST_CONTROL
ReturnComNotImplemented();
#else /* VBOX_WITH_GUEST_CONTROL */
using namespace guestControl;
CheckComArgStrNotEmptyOrNull(aCommand);
CheckComArgOutPointerValid(aPID);
CheckComArgOutPointerValid(aProgress);
/* Do not allow anonymous executions (with system rights). */
if (RT_UNLIKELY((aUserName) == NULL || *(aUserName) == '\0'))
return setError(E_INVALIDARG, tr("No user name specified"));
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
if (aFlags != 0) /* Flags are not supported at the moment. */
return E_INVALIDARG;
HRESULT rc = S_OK;
try
{
/*
* Create progress object.
*/
ComObjPtr <Progress> progress;
rc = progress.createObject();
if (SUCCEEDED(rc))
{
rc = progress->init(static_cast<IGuest*>(this),
BstrFmt(tr("Executing process")),
TRUE);
}
if (FAILED(rc)) return rc;
/*
* Prepare process execution.
*/
int vrc = VINF_SUCCESS;
Utf8Str Utf8Command(aCommand);
/* Adjust timeout */
if (aTimeoutMS == 0)
aTimeoutMS = UINT32_MAX;
/* Prepare arguments. */
char **papszArgv = NULL;
uint32_t uNumArgs = 0;
if (aArguments > 0)
{
com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
uNumArgs = args.size();
papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
AssertReturn(papszArgv, E_OUTOFMEMORY);
for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
{
/// @todo r=bird: RTUtf16ToUtf8().
int cbLen = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
if (cbLen < 0)
vrc = VERR_NO_MEMORY;
}
papszArgv[uNumArgs] = NULL;
}
Utf8Str Utf8UserName(aUserName);
Utf8Str Utf8Password(aPassword);
if (RT_SUCCESS(vrc))
{
uint32_t uContextID = 0;
char *pszArgs = NULL;
if (uNumArgs > 0)
vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
if (RT_SUCCESS(vrc))
{
uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
/* Prepare environment. */
void *pvEnv = NULL;
uint32_t uNumEnv = 0;
uint32_t cbEnv = 0;
if (aEnvironment > 0)
{
com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
for (unsigned i = 0; i < env.size(); i++)
{
vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
if (RT_FAILURE(vrc))
break;
}
}
if (RT_SUCCESS(vrc))
{
PCALLBACKDATAEXECSTATUS pData = (PCALLBACKDATAEXECSTATUS)RTMemAlloc(sizeof(CALLBACKDATAEXECSTATUS));
AssertReturn(pData, VBOX_E_IPRT_ERROR);
uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_START,
pData, sizeof(CALLBACKDATAEXECSTATUS), progress);
Assert(uContextID > 0);
VBOXHGCMSVCPARM paParms[15];
int i = 0;
paParms[i++].setUInt32(uContextID);
paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
paParms[i++].setUInt32(aFlags);
paParms[i++].setUInt32(uNumArgs);
paParms[i++].setPointer((void*)pszArgs, cbArgs);
paParms[i++].setUInt32(uNumEnv);
paParms[i++].setUInt32(cbEnv);
paParms[i++].setPointer((void*)pvEnv, cbEnv);
paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
paParms[i++].setUInt32(aTimeoutMS);
VMMDev *vmmDev;
{
/* Make sure mParent is valid, so set the read lock while using.
* Do not keep this lock while doing the actual call, because in the meanwhile
* another thread could request a write lock which would be a bad idea ... */
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
/* Forward the information to the VMM device. */
AssertPtr(mParent);
vmmDev = mParent->getVMMDev();
}
if (vmmDev)
{
LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
i, paParms);
}
else
vrc = VERR_INVALID_VM_HANDLE;
RTMemFree(pvEnv);
}
RTStrFree(pszArgs);
}
if (RT_SUCCESS(vrc))
{
LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
/*
* Wait for the HGCM low level callback until the process
* has been started (or something went wrong). This is necessary to
* get the PID.
*/
CallbackListIter it = getCtrlCallbackContextByID(uContextID);
BOOL fCanceled = FALSE;
if (it != mCallbackList.end())
{
uint64_t u64Started = RTTimeMilliTS();
while (!it->fCalled)
{
/* Check for timeout. */
unsigned cMsWait;
if (aTimeoutMS == RT_INDEFINITE_WAIT)
cMsWait = 10;
else
{
uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
if (cMsElapsed >= aTimeoutMS)
break; /* Timed out. */
cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
}
/* Check for manual stop. */
if (!it->pProgress.isNull())
{
rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
if (FAILED(rc)) throw rc;
if (fCanceled)
break; /* HGCM/guest wants to abort because of status change. */
}
/// @todo r=bird: two operation progress object and wait first operation.
/// IProgress::WaitForOperationCompletion.
RTThreadSleep(cMsWait);
}
}
/* Was the whole thing canceled? */
if (!fCanceled)
{
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
PCALLBACKDATAEXECSTATUS pData = (PCALLBACKDATAEXECSTATUS)it->pvData;
Assert(it->cbData == sizeof(CALLBACKDATAEXECSTATUS));
AssertPtr(pData);
if (it->fCalled)
{
/* Did we get some status? */
switch (pData->u32Status)
{
case PROC_STS_STARTED:
/* Process is (still) running; get PID. */
*aPID = pData->u32PID;
break;
/* In any other case the process either already
* terminated or something else went wrong, so no PID ... */
case PROC_STS_TEN: /* Terminated normally. */
case PROC_STS_TEA: /* Terminated abnormally. */
case PROC_STS_TES: /* Terminated through signal. */
case PROC_STS_TOK:
case PROC_STS_TOA:
case PROC_STS_DWN:
/*
* Process (already) ended, but we want to get the
* PID anyway to retrieve the output in a later call.
*/
*aPID = pData->u32PID;
break;
case PROC_STS_ERROR:
vrc = pData->u32Flags; /* u32Flags member contains IPRT error code. */
break;
default:
vrc = VERR_INVALID_PARAMETER; /* Unknown status, should never happen! */
break;
}
}
else /* If callback not called within time ... well, that's a timeout! */
vrc = VERR_TIMEOUT;
/*
* Do *not* remove the callback yet - we might wait with the IProgress object on something
* else (like end of process) ...
*/
if (RT_FAILURE(vrc))
{
if (vrc == VERR_FILE_NOT_FOUND) /* This is the most likely error. */
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The file '%s' was not found on guest"), Utf8Command.raw());
}
else if (vrc == VERR_PATH_NOT_FOUND)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The path to file '%s' was not found on guest"), Utf8Command.raw());
}
else if (vrc == VERR_BAD_EXE_FORMAT)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The file '%s' is not an executable format on guest"), Utf8Command.raw());
}
else if (vrc == VERR_LOGON_FAILURE)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The specified user '%s' was not able to logon on guest"), Utf8UserName.raw());
}
else if (vrc == VERR_TIMEOUT)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The guest did not respond within time (%ums)"), aTimeoutMS);
}
else if (vrc == VERR_PERMISSION_DENIED)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("Invalid user/password credentials"));
}
else
{
if (pData->u32Status == PROC_STS_ERROR)
rc = setError(VBOX_E_IPRT_ERROR,
tr("Process could not be started: %Rrc"), pData->u32Flags);
else
rc = setError(E_UNEXPECTED,
tr("The service call failed with error %Rrc"), vrc);
}
}
else /* Execution went fine. */
{
/* Return the progress to the caller. */
progress.queryInterfaceTo(aProgress);
}
}
else /* Operation was canceled. */
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The operation was canceled"));
}
}
else /* HGCM related error codes .*/
{
if (vrc == VERR_INVALID_VM_HANDLE)
{
rc = setError(VBOX_E_VM_ERROR,
tr("VMM device is not available (is the VM running?)"));
}
else if (vrc == VERR_TIMEOUT)
{
rc = setError(VBOX_E_VM_ERROR,
tr("The guest execution service is not ready"));
}
else /* HGCM call went wrong. */
{
rc = setError(E_UNEXPECTED,
tr("The HGCM call failed with error %Rrc"), vrc);
}
}
for (unsigned i = 0; i < uNumArgs; i++)
RTMemFree(papszArgv[i]);
RTMemFree(papszArgv);
}
}
catch (std::bad_alloc &)
{
rc = E_OUTOFMEMORY;
}
return rc;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
STDMETHODIMP Guest::GetProcessOutput(ULONG aPID, ULONG aFlags, ULONG aTimeoutMS, ULONG64 aSize, ComSafeArrayOut(BYTE, aData))
{
/** @todo r=bird: Eventually we should clean up all the timeout parameters
* in the API and have the same way of specifying infinite waits! */
#ifndef VBOX_WITH_GUEST_CONTROL
ReturnComNotImplemented();
#else /* VBOX_WITH_GUEST_CONTROL */
using namespace guestControl;
CheckComArgExpr(aPID, aPID > 0);
if (aFlags != 0) /* Flags are not supported at the moment. */
return E_INVALIDARG;
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
HRESULT rc = S_OK;
try
{
/*
* Create progress object.
* Note that we need at least a local progress object here in order
* to get notified when someone cancels the operation.
*/
ComObjPtr <Progress> progress;
rc = progress.createObject();
if (SUCCEEDED(rc))
{
rc = progress->init(static_cast<IGuest*>(this),
BstrFmt(tr("Getting output of process")),
TRUE);
}
if (FAILED(rc)) return rc;
/* Adjust timeout */
if (aTimeoutMS == 0)
aTimeoutMS = UINT32_MAX;
/* Search for existing PID. */
PCALLBACKDATAEXECOUT pData = (CALLBACKDATAEXECOUT*)RTMemAlloc(sizeof(CALLBACKDATAEXECOUT));
AssertReturn(pData, VBOX_E_IPRT_ERROR);
uint32_t uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT,
pData, sizeof(CALLBACKDATAEXECOUT), progress);
Assert(uContextID > 0);
size_t cbData = (size_t)RT_MIN(aSize, _64K);
com::SafeArray<BYTE> outputData(cbData);
VBOXHGCMSVCPARM paParms[5];
int i = 0;
paParms[i++].setUInt32(uContextID);
paParms[i++].setUInt32(aPID);
paParms[i++].setUInt32(aFlags); /** @todo Should represent stdout and/or stderr. */
int vrc = VINF_SUCCESS;
{
VMMDev *vmmDev;
{
/* Make sure mParent is valid, so set the read lock while using.
* Do not keep this lock while doing the actual call, because in the meanwhile
* another thread could request a write lock which would be a bad idea ... */
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
/* Forward the information to the VMM device. */
AssertPtr(mParent);
vmmDev = mParent->getVMMDev();
}
if (vmmDev)
{
LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_GET_OUTPUT,
i, paParms);
}
}
if (RT_SUCCESS(vrc))
{
LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
/*
* Wait for the HGCM low level callback until the process
* has been started (or something went wrong). This is necessary to
* get the PID.
*/
CallbackListIter it = getCtrlCallbackContextByID(uContextID);
BOOL fCanceled = FALSE;
if (it != mCallbackList.end())
{
uint64_t u64Started = RTTimeMilliTS();
while (!it->fCalled)
{
/* Check for timeout. */
unsigned cMsWait;
if (aTimeoutMS == RT_INDEFINITE_WAIT)
cMsWait = 10;
else
{
uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
if (cMsElapsed >= aTimeoutMS)
break; /* Timed out. */
cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
}
/* Check for manual stop. */
if (!it->pProgress.isNull())
{
rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
if (FAILED(rc)) throw rc;
if (fCanceled)
break; /* Client wants to abort. */
}
RTThreadSleep(cMsWait);
}
/* Was the whole thing canceled? */
if (!fCanceled)
{
if (it->fCalled)
{
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
/* Did we get some output? */
pData = (PCALLBACKDATAEXECOUT)it->pvData;
Assert(it->cbData == sizeof(CALLBACKDATAEXECOUT));
AssertPtr(pData);
if (pData->cbData)
{
/* Do we need to resize the array? */
if (pData->cbData > cbData)
outputData.resize(pData->cbData);
/* Fill output in supplied out buffer. */
memcpy(outputData.raw(), pData->pvData, pData->cbData);
outputData.resize(pData->cbData); /* Shrink to fit actual buffer size. */
}
else
vrc = VERR_NO_DATA; /* This is not an error we want to report to COM. */
}
else /* If callback not called within time ... well, that's a timeout! */
vrc = VERR_TIMEOUT;
}
else /* Operation was canceled. */
vrc = VERR_CANCELLED;
if (RT_FAILURE(vrc))
{
if (vrc == VERR_NO_DATA)
{
/* This is not an error we want to report to COM. */
}
else if (vrc == VERR_TIMEOUT)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The guest did not output within time (%ums)"), aTimeoutMS);
}
else if (vrc == VERR_CANCELLED)
{
rc = setError(VBOX_E_IPRT_ERROR,
tr("The operation was canceled"));
}
else
{
rc = setError(E_UNEXPECTED,
tr("The service call failed with error %Rrc"), vrc);
}
}
{
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
destroyCtrlCallbackContext(it);
}
}
else /* PID lookup failed. */
rc = setError(VBOX_E_IPRT_ERROR,
tr("Process (PID %u) not found!"), aPID);
}
else /* HGCM operation failed. */
rc = setError(E_UNEXPECTED,
tr("The HGCM call failed with error %Rrc"), vrc);
/* Cleanup. */
progress->uninit();
progress.setNull();
/* If something failed (or there simply was no data, indicated by VERR_NO_DATA,
* we return an empty array so that the frontend knows when to give up. */
if (RT_FAILURE(vrc) || FAILED(rc))
outputData.resize(0);
outputData.detachTo(ComSafeArrayOutArg(aData));
}
catch (std::bad_alloc &)
{
rc = E_OUTOFMEMORY;
}
return rc;
#endif
}
STDMETHODIMP Guest::GetProcessStatus(ULONG aPID, ULONG *aExitCode, ULONG *aFlags, ULONG *aStatus)
{
#ifndef VBOX_WITH_GUEST_CONTROL
ReturnComNotImplemented();
#else /* VBOX_WITH_GUEST_CONTROL */
using namespace guestControl;
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
HRESULT rc = S_OK;
try
{
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
GuestProcessIterConst it;
for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
{
if (it->mPID == aPID)
break;
}
if (it != mGuestProcessList.end())
{
*aExitCode = it->mExitCode;
*aFlags = it->mFlags;
*aStatus = it->mStatus;
}
else
rc = setError(VBOX_E_IPRT_ERROR,
tr("Process (PID %u) not found!"), aPID);
}
catch (std::bad_alloc &)
{
rc = E_OUTOFMEMORY;
}
return rc;
#endif
}
// public methods only for internal purposes
/////////////////////////////////////////////////////////////////////////////
void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
{
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.mAdditionsVersion = aVersion;
mData.mAdditionsActive = !aVersion.isEmpty();
/* Older Additions didn't have this finer grained capability bit,
* so enable it by default. Newer Additions will disable it immediately
* if relevant. */
mData.mSupportsGraphics = mData.mAdditionsActive;
mData.mOSTypeId = Global::OSTypeId (aOsType);
}
void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
{
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.mSupportsSeamless = aSupportsSeamless;
}
void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
{
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.mSupportsGraphics = aSupportsGraphics;
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */
|