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
|
/* $Id: VBoxCredProvPoller.cpp $ */
/** @file
* VBoxCredPoller - Thread for querying / retrieving user credentials.
*/
/*
* Copyright (C) 2012 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <Windows.h>
#include <VBox/VBoxGuest.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/VMMDev.h>
#include <iprt/string.h>
#include "VBoxCredProvProvider.h"
#include "VBoxCredProvCredential.h"
#include "VBoxCredProvPoller.h"
#include "VBoxCredProvUtils.h"
VBoxCredProvPoller::VBoxCredProvPoller(void) :
m_hThreadPoller(NIL_RTTHREAD),
m_pProv(NULL)
{
}
VBoxCredProvPoller::~VBoxCredProvPoller(void)
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Destroying ...\n");
Shutdown();
}
int
VBoxCredProvPoller::Initialize(VBoxCredProvProvider *pProvider)
{
AssertPtrReturn(pProvider, VERR_INVALID_POINTER);
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Initializing\n");
/* Don't create more than one of them. */
if (m_hThreadPoller != NIL_RTTHREAD)
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Thread already running, returning\n");
return VINF_SUCCESS;
}
if (m_pProv != NULL)
m_pProv->Release();
m_pProv = pProvider;
/*
* We must not add a reference via AddRef() here, otherwise
* the credential provider does not get destructed properly.
* In order to get this thread terminated normally the credential
* provider has to call Shutdown().
*/
/* Create the poller thread. */
int rc = RTThreadCreate(&m_hThreadPoller, VBoxCredProvPoller::threadPoller, this, 0, RTTHREADTYPE_INFREQUENT_POLLER,
RTTHREADFLAGS_WAITABLE, "credpoll");
if (RT_FAILURE(rc))
VBoxCredProvVerbose(0, "VBoxCredProvPoller::Initialize: Failed to create thread, rc=%Rrc\n", rc);
return rc;
}
int
VBoxCredProvPoller::Shutdown(void)
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Shutdown\n");
if (m_hThreadPoller == NIL_RTTHREAD)
return VINF_SUCCESS;
/* Post termination event semaphore. */
int rc = RTThreadUserSignal(m_hThreadPoller);
if (RT_SUCCESS(rc))
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Waiting for thread to terminate\n");
/* Wait until the thread has terminated. */
rc = RTThreadWait(m_hThreadPoller, RT_INDEFINITE_WAIT, NULL);
if (RT_FAILURE(rc))
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Wait returned error rc=%Rrc\n", rc);
}
else
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Error waiting for thread shutdown, rc=%Rrc\n", rc);
m_pProv = NULL;
m_hThreadPoller = NIL_RTTHREAD;
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Shutdown returned with rc=%Rrc\n", rc);
return rc;
}
/*static*/ DECLCALLBACK(int)
VBoxCredProvPoller::threadPoller(RTTHREAD hThreadSelf, void *pvUser)
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Starting, pvUser=0x%p\n", pvUser);
VBoxCredProvPoller *pThis = (VBoxCredProvPoller*)pvUser;
AssertPtr(pThis);
for (;;)
{
int rc;
rc = VbglR3CredentialsQueryAvailability();
if (RT_FAILURE(rc))
{
if (rc != VERR_NOT_FOUND)
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Could not retrieve credentials! rc=%Rc\n", rc);
}
else
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Credentials available, notifying provider\n");
if (pThis->m_pProv)
pThis->m_pProv->OnCredentialsProvided();
}
/* Wait a bit. */
if (RTThreadUserWait(hThreadSelf, 500) == VINF_SUCCESS)
{
VBoxCredProvVerbose(0, "VBoxCredProvPoller: Terminating\n");
break;
}
}
return VINF_SUCCESS;
}
|