VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrlListener.cpp@ 76678

Last change on this file since 76678 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: VBoxManageGuestCtrlListener.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBoxManage - Guest control listener implementations.
4 */
5
6/*
7 * Copyright (C) 2013-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "VBoxManage.h"
23#include "VBoxManageGuestCtrl.h"
24
25#ifndef VBOX_ONLY_DOCS
26
27#include <VBox/com/com.h>
28#include <VBox/com/ErrorInfo.h>
29#include <VBox/com/errorprint.h>
30
31#include <iprt/time.h>
32
33#include <map>
34#include <vector>
35
36
37
38/*
39 * GuestListenerBase
40 * GuestListenerBase
41 * GuestListenerBase
42 */
43
44GuestListenerBase::GuestListenerBase(void)
45 : mfVerbose(false)
46{
47}
48
49GuestListenerBase::~GuestListenerBase(void)
50{
51}
52
53HRESULT GuestListenerBase::init(bool fVerbose)
54{
55 mfVerbose = fVerbose;
56 return S_OK;
57}
58
59
60
61/*
62 * GuestFileEventListener
63 * GuestFileEventListener
64 * GuestFileEventListener
65 */
66
67GuestFileEventListener::GuestFileEventListener(void)
68{
69}
70
71GuestFileEventListener::~GuestFileEventListener(void)
72{
73}
74
75void GuestFileEventListener::uninit(void)
76{
77
78}
79
80STDMETHODIMP GuestFileEventListener::HandleEvent(VBoxEventType_T aType, IEvent *aEvent)
81{
82 switch (aType)
83 {
84 case VBoxEventType_OnGuestFileStateChanged:
85 {
86 HRESULT rc;
87 do
88 {
89 ComPtr<IGuestFileStateChangedEvent> pEvent = aEvent;
90 Assert(!pEvent.isNull());
91
92 ComPtr<IGuestFile> pProcess;
93 CHECK_ERROR_BREAK(pEvent, COMGETTER(File)(pProcess.asOutParam()));
94 AssertBreak(!pProcess.isNull());
95 FileStatus_T fileSts;
96 CHECK_ERROR_BREAK(pEvent, COMGETTER(Status)(&fileSts));
97 Bstr strPath;
98 CHECK_ERROR_BREAK(pProcess, COMGETTER(Filename)(strPath.asOutParam()));
99 ULONG uID;
100 CHECK_ERROR_BREAK(pProcess, COMGETTER(Id)(&uID));
101
102 RTPrintf("File ID=%RU32 \"%s\" changed status to [%s]\n",
103 uID, Utf8Str(strPath).c_str(), gctlFileStatusToText(fileSts));
104
105 } while (0);
106 break;
107 }
108
109 default:
110 AssertFailed();
111 }
112
113 return S_OK;
114}
115
116
117/*
118 * GuestProcessEventListener
119 * GuestProcessEventListener
120 * GuestProcessEventListener
121 */
122
123GuestProcessEventListener::GuestProcessEventListener(void)
124{
125}
126
127GuestProcessEventListener::~GuestProcessEventListener(void)
128{
129}
130
131void GuestProcessEventListener::uninit(void)
132{
133
134}
135
136STDMETHODIMP GuestProcessEventListener::HandleEvent(VBoxEventType_T aType, IEvent *aEvent)
137{
138 switch (aType)
139 {
140 case VBoxEventType_OnGuestProcessStateChanged:
141 {
142 HRESULT rc;
143 do
144 {
145 ComPtr<IGuestProcessStateChangedEvent> pEvent = aEvent;
146 Assert(!pEvent.isNull());
147
148 ComPtr<IGuestProcess> pProcess;
149 CHECK_ERROR_BREAK(pEvent, COMGETTER(Process)(pProcess.asOutParam()));
150 AssertBreak(!pProcess.isNull());
151 ProcessStatus_T procSts;
152 CHECK_ERROR_BREAK(pEvent, COMGETTER(Status)(&procSts));
153 Bstr strPath;
154 CHECK_ERROR_BREAK(pProcess, COMGETTER(ExecutablePath)(strPath.asOutParam()));
155 ULONG uPID;
156 CHECK_ERROR_BREAK(pProcess, COMGETTER(PID)(&uPID));
157
158 RTPrintf("Process PID=%RU32 \"%s\" changed status to [%s]\n",
159 uPID, Utf8Str(strPath).c_str(), gctlProcessStatusToText(procSts));
160
161 } while (0);
162 break;
163 }
164
165 default:
166 AssertFailed();
167 }
168
169 return S_OK;
170}
171
172
173/*
174 * GuestSessionEventListener
175 * GuestSessionEventListener
176 * GuestSessionEventListener
177 */
178
179GuestSessionEventListener::GuestSessionEventListener(void)
180{
181}
182
183GuestSessionEventListener::~GuestSessionEventListener(void)
184{
185}
186
187void GuestSessionEventListener::uninit(void)
188{
189 GuestEventProcs::iterator itProc = mProcs.begin();
190 while (itProc != mProcs.end())
191 {
192 if (!itProc->first.isNull())
193 {
194 HRESULT rc;
195 do
196 {
197 /* Listener unregistration. */
198 ComPtr<IEventSource> pES;
199 CHECK_ERROR_BREAK(itProc->first, COMGETTER(EventSource)(pES.asOutParam()));
200 if (!pES.isNull())
201 CHECK_ERROR_BREAK(pES, UnregisterListener(itProc->second.mListener));
202 } while (0);
203 itProc->first->Release();
204 }
205
206 ++itProc;
207 }
208 mProcs.clear();
209
210 GuestEventFiles::iterator itFile = mFiles.begin();
211 while (itFile != mFiles.end())
212 {
213 if (!itFile->first.isNull())
214 {
215 HRESULT rc;
216 do
217 {
218 /* Listener unregistration. */
219 ComPtr<IEventSource> pES;
220 CHECK_ERROR_BREAK(itFile->first, COMGETTER(EventSource)(pES.asOutParam()));
221 if (!pES.isNull())
222 CHECK_ERROR_BREAK(pES, UnregisterListener(itFile->second.mListener));
223 } while (0);
224 itFile->first->Release();
225 }
226
227 ++itFile;
228 }
229 mFiles.clear();
230}
231
232STDMETHODIMP GuestSessionEventListener::HandleEvent(VBoxEventType_T aType, IEvent *aEvent)
233{
234 switch (aType)
235 {
236 case VBoxEventType_OnGuestFileRegistered:
237 {
238 HRESULT rc;
239 do
240 {
241 ComPtr<IGuestFileRegisteredEvent> pEvent = aEvent;
242 Assert(!pEvent.isNull());
243
244 ComPtr<IGuestFile> pFile;
245 CHECK_ERROR_BREAK(pEvent, COMGETTER(File)(pFile.asOutParam()));
246 AssertBreak(!pFile.isNull());
247 BOOL fRegistered;
248 CHECK_ERROR_BREAK(pEvent, COMGETTER(Registered)(&fRegistered));
249 Bstr strPath;
250 CHECK_ERROR_BREAK(pFile, COMGETTER(Filename)(strPath.asOutParam()));
251
252 RTPrintf("File \"%s\" %s\n",
253 Utf8Str(strPath).c_str(),
254 fRegistered ? "registered" : "unregistered");
255 if (fRegistered)
256 {
257 if (mfVerbose)
258 RTPrintf("Registering ...\n");
259
260 /* Register for IGuestFile events. */
261 ComObjPtr<GuestFileEventListenerImpl> pListener;
262 pListener.createObject();
263 CHECK_ERROR_BREAK(pListener, init(new GuestFileEventListener()));
264
265 ComPtr<IEventSource> es;
266 CHECK_ERROR_BREAK(pFile, COMGETTER(EventSource)(es.asOutParam()));
267 com::SafeArray<VBoxEventType_T> eventTypes;
268 eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
269 CHECK_ERROR_BREAK(es, RegisterListener(pListener, ComSafeArrayAsInParam(eventTypes),
270 true /* Active listener */));
271
272 GuestFileStats fileStats(pListener);
273 mFiles[pFile] = fileStats;
274 }
275 else
276 {
277 GuestEventFiles::iterator itFile = mFiles.find(pFile);
278 if (itFile != mFiles.end())
279 {
280 if (mfVerbose)
281 RTPrintf("Unregistering file ...\n");
282
283 if (!itFile->first.isNull())
284 {
285 /* Listener unregistration. */
286 ComPtr<IEventSource> pES;
287 CHECK_ERROR(itFile->first, COMGETTER(EventSource)(pES.asOutParam()));
288 if (!pES.isNull())
289 CHECK_ERROR(pES, UnregisterListener(itFile->second.mListener));
290 itFile->first->Release();
291 }
292
293 mFiles.erase(itFile);
294 }
295 }
296
297 } while (0);
298 break;
299 }
300
301 case VBoxEventType_OnGuestProcessRegistered:
302 {
303 HRESULT rc;
304 do
305 {
306 ComPtr<IGuestProcessRegisteredEvent> pEvent = aEvent;
307 Assert(!pEvent.isNull());
308
309 ComPtr<IGuestProcess> pProcess;
310 CHECK_ERROR_BREAK(pEvent, COMGETTER(Process)(pProcess.asOutParam()));
311 AssertBreak(!pProcess.isNull());
312 BOOL fRegistered;
313 CHECK_ERROR_BREAK(pEvent, COMGETTER(Registered)(&fRegistered));
314 Bstr strPath;
315 CHECK_ERROR_BREAK(pProcess, COMGETTER(ExecutablePath)(strPath.asOutParam()));
316
317 RTPrintf("Process \"%s\" %s\n",
318 Utf8Str(strPath).c_str(),
319 fRegistered ? "registered" : "unregistered");
320 if (fRegistered)
321 {
322 if (mfVerbose)
323 RTPrintf("Registering ...\n");
324
325 /* Register for IGuestProcess events. */
326 ComObjPtr<GuestProcessEventListenerImpl> pListener;
327 pListener.createObject();
328 CHECK_ERROR_BREAK(pListener, init(new GuestProcessEventListener()));
329
330 ComPtr<IEventSource> es;
331 CHECK_ERROR_BREAK(pProcess, COMGETTER(EventSource)(es.asOutParam()));
332 com::SafeArray<VBoxEventType_T> eventTypes;
333 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
334 CHECK_ERROR_BREAK(es, RegisterListener(pListener, ComSafeArrayAsInParam(eventTypes),
335 true /* Active listener */));
336
337 GuestProcStats procStats(pListener);
338 mProcs[pProcess] = procStats;
339 }
340 else
341 {
342 GuestEventProcs::iterator itProc = mProcs.find(pProcess);
343 if (itProc != mProcs.end())
344 {
345 if (mfVerbose)
346 RTPrintf("Unregistering process ...\n");
347
348 if (!itProc->first.isNull())
349 {
350 /* Listener unregistration. */
351 ComPtr<IEventSource> pES;
352 CHECK_ERROR(itProc->first, COMGETTER(EventSource)(pES.asOutParam()));
353 if (!pES.isNull())
354 CHECK_ERROR(pES, UnregisterListener(itProc->second.mListener));
355 itProc->first->Release();
356 }
357
358 mProcs.erase(itProc);
359 }
360 }
361
362 } while (0);
363 break;
364 }
365
366 case VBoxEventType_OnGuestSessionStateChanged:
367 {
368 HRESULT rc;
369 do
370 {
371 ComPtr<IGuestSessionStateChangedEvent> pEvent = aEvent;
372 Assert(!pEvent.isNull());
373 ComPtr<IGuestSession> pSession;
374 CHECK_ERROR_BREAK(pEvent, COMGETTER(Session)(pSession.asOutParam()));
375 AssertBreak(!pSession.isNull());
376
377 GuestSessionStatus_T sessSts;
378 CHECK_ERROR_BREAK(pSession, COMGETTER(Status)(&sessSts));
379 ULONG uID;
380 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
381 Bstr strName;
382 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
383
384 RTPrintf("Session ID=%RU32 \"%s\" changed status to [%s]\n",
385 uID, Utf8Str(strName).c_str(), gctlGuestSessionStatusToText(sessSts));
386
387 } while (0);
388 break;
389 }
390
391 default:
392 AssertFailed();
393 }
394
395 return S_OK;
396}
397
398
399/*
400 * GuestEventListener
401 * GuestEventListener
402 * GuestEventListener
403 */
404
405GuestEventListener::GuestEventListener(void)
406{
407}
408
409GuestEventListener::~GuestEventListener(void)
410{
411}
412
413void GuestEventListener::uninit(void)
414{
415 GuestEventSessions::iterator itSession = mSessions.begin();
416 while (itSession != mSessions.end())
417 {
418 if (!itSession->first.isNull())
419 {
420 HRESULT rc;
421 do
422 {
423 /* Listener unregistration. */
424 ComPtr<IEventSource> pES;
425 CHECK_ERROR_BREAK(itSession->first, COMGETTER(EventSource)(pES.asOutParam()));
426 if (!pES.isNull())
427 CHECK_ERROR_BREAK(pES, UnregisterListener(itSession->second.mListener));
428
429 } while (0);
430 itSession->first->Release();
431 }
432
433 ++itSession;
434 }
435 mSessions.clear();
436}
437
438STDMETHODIMP GuestEventListener::HandleEvent(VBoxEventType_T aType, IEvent *aEvent)
439{
440 switch (aType)
441 {
442 case VBoxEventType_OnGuestSessionRegistered:
443 {
444 HRESULT rc;
445 do
446 {
447 ComPtr<IGuestSessionRegisteredEvent> pEvent = aEvent;
448 Assert(!pEvent.isNull());
449
450 ComPtr<IGuestSession> pSession;
451 CHECK_ERROR_BREAK(pEvent, COMGETTER(Session)(pSession.asOutParam()));
452 AssertBreak(!pSession.isNull());
453 BOOL fRegistered;
454 CHECK_ERROR_BREAK(pEvent, COMGETTER(Registered)(&fRegistered));
455 Bstr strName;
456 CHECK_ERROR_BREAK(pSession, COMGETTER(Name)(strName.asOutParam()));
457 ULONG uID;
458 CHECK_ERROR_BREAK(pSession, COMGETTER(Id)(&uID));
459
460 RTPrintf("Session ID=%RU32 \"%s\" %s\n",
461 uID, Utf8Str(strName).c_str(),
462 fRegistered ? "registered" : "unregistered");
463 if (fRegistered)
464 {
465 if (mfVerbose)
466 RTPrintf("Registering ...\n");
467
468 /* Register for IGuestSession events. */
469 ComObjPtr<GuestSessionEventListenerImpl> pListener;
470 pListener.createObject();
471 CHECK_ERROR_BREAK(pListener, init(new GuestSessionEventListener()));
472
473 ComPtr<IEventSource> es;
474 CHECK_ERROR_BREAK(pSession, COMGETTER(EventSource)(es.asOutParam()));
475 com::SafeArray<VBoxEventType_T> eventTypes;
476 eventTypes.push_back(VBoxEventType_OnGuestFileRegistered);
477 eventTypes.push_back(VBoxEventType_OnGuestProcessRegistered);
478 CHECK_ERROR_BREAK(es, RegisterListener(pListener, ComSafeArrayAsInParam(eventTypes),
479 true /* Active listener */));
480
481 GuestSessionStats sessionStats(pListener);
482 mSessions[pSession] = sessionStats;
483 }
484 else
485 {
486 GuestEventSessions::iterator itSession = mSessions.find(pSession);
487 if (itSession != mSessions.end())
488 {
489 if (mfVerbose)
490 RTPrintf("Unregistering ...\n");
491
492 if (!itSession->first.isNull())
493 {
494 /* Listener unregistration. */
495 ComPtr<IEventSource> pES;
496 CHECK_ERROR_BREAK(itSession->first, COMGETTER(EventSource)(pES.asOutParam()));
497 if (!pES.isNull())
498 CHECK_ERROR_BREAK(pES, UnregisterListener(itSession->second.mListener));
499 itSession->first->Release();
500 }
501
502 mSessions.erase(itSession);
503 }
504 }
505
506 } while (0);
507 break;
508 }
509
510 default:
511 AssertFailed();
512 }
513
514 return S_OK;
515}
516
517#endif /* !VBOX_ONLY_DOCS */
518
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use