-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbmuxdMain.cpp
247 lines (216 loc) · 6.37 KB
/
usbmuxdMain.cpp
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
/******************************************************************************
* usbmuxdHost.cpp
*****************************************************************************/
/******************************************************************************
* Includes
*****************************************************************************/
#include "stdafx.h"
#include "..\usbmuxd.h"
#include "usbmuxdHostShared.h"
#include "usbmuxdHost_internal.h"
#include <shellapi.h>
/* MCE includes */
#include "DebugPrint.h"
#include "WindowsUtil.h"
/******************************************************************************
* RunUsbmuxdHost Function
*****************************************************************************/
static int RunUsbmuxdHost(WORD wPort, DWORD dwHubAddress, LPCWSTR pszReadyEventName, LPCWSTR pszShutdownEventName)
{
int iRet = EXIT_FAILURE;
CString strHostMutexName;
CString strKeepAliveEventName;
HANDLE hHostMutex = NULL;
HANDLE hKeepAliveEvent = NULL;
DWORD dwWaitResult = WAIT_FAILED;
BOOL bShouldShutdown = FALSE;
DWORD dwNoDevicesTimestamp = 0;
HANDLE ahWaitEvents[WAIT_EVENTS_COUNT] = { NULL };
HANDLE hReadEvent = OpenEventW(EVENT_MODIFY_STATE, FALSE, pszReadyEventName);
HANDLE hShutdownEvent = OpenEventW(SYNCHRONIZE, FALSE, pszShutdownEventName);
if ((FALSE == IS_VALID_HANDLE(hReadEvent)) || (FALSE == IS_VALID_HANDLE(hShutdownEvent)))
{
DEBUG_PRINT_WIN32_ERROR("OpenEvent");
goto lblCleanup;
}
/* Create our "single instance" mutex, and verify we are the onky usbmuxdHost
* process */
strHostMutexName.Format(USBMUXD_HOST_MUTEX_NAME_FORMAT, wPort);
strKeepAliveEventName.Format(USBMUXD_HOST_KEEPALIVE_EVENT_NAME_FORMAT, wPort);
hHostMutex = CreateMutexAllAccess(strHostMutexName, TRUE);
if (FALSE == IS_VALID_HANDLE(hHostMutex))
{
DEBUG_PRINT_WIN32_ERROR("CreateMutexAllAccess");
goto lblCleanup;
}
/* Check if another instance is running */
if (ERROR_ALREADY_EXISTS == GetLastError())
{
DEBUG_PRINT("Another instance is already running");
/* Signal the keep alive event, to reset the other usbmuxdHost's
* shutdown timer */
hKeepAliveEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, strKeepAliveEventName);
if (IS_VALID_HANDLE(hKeepAliveEvent))
{
if (FALSE == SetEvent(hKeepAliveEvent))
{
DEBUG_PRINT_WIN32_ERROR("SetEvent");
}
SAFE_CLOSE_HANDLE(hKeepAliveEvent);
}
else
{
DEBUG_PRINT_WIN32_ERROR("OpenEvent");
}
/* Another instance is running. We'll still signal
* the ready event, because its a valid state (and the caller
* can start working with the other intance). */
if (SetEvent(hReadEvent))
{
iRet = EXIT_SUCCESS;
}
else
{
DEBUG_PRINT_WIN32_ERROR("SetEvent");
}
goto lblCleanup;
}
/* Create the keep alive event */
hKeepAliveEvent = CreateEventAllAccess(strKeepAliveEventName);
if (FALSE == IS_VALID_HANDLE(hKeepAliveEvent))
{
DEBUG_PRINT_WIN32_ERROR("CreateEventAllAccess");
goto lblCleanup;
}
/* Start usbmuxd */
if (FALSE == usbmuxd_Start(wPort, dwHubAddress))
{
DEBUG_PRINT_ERROR("Failed to start usbmuxd");
goto lblCleanup;
}
/* Signal the usbmuxdHostClient we are ready */
if (FALSE == SetEvent(hReadEvent))
{
DEBUG_PRINT_WIN32_ERROR("SetEvent");
}
/* Wait for the shutdown and keep alive signals */
ahWaitEvents[WAIT_EVENT_SHUTDOWN] = hShutdownEvent;
ahWaitEvents[WAIT_EVENT_KEEP_ALIVE] = hKeepAliveEvent;
while (FALSE == bShouldShutdown)
{
dwWaitResult = WaitForMultipleObjects(WAIT_EVENTS_COUNT, ahWaitEvents, FALSE, SHUTDOWN_EVENT_WAIT_INTERVAL);
switch (dwWaitResult)
{
/* Shutdown event */
case WAIT_OBJECT_0 + WAIT_EVENT_SHUTDOWN:
DEBUG_PRINT("Shutdown event was signaled");
bShouldShutdown = TRUE;
break;
/* Keepalive event */
case WAIT_OBJECT_0 + WAIT_EVENT_KEEP_ALIVE:
/* Reset the timeout base time */
DEBUG_PRINT("Keepalive event was signaled");
dwNoDevicesTimestamp = 0;
break;
/* Timeout (which is normal, since we are waiting in intervals) */
case WAIT_TIMEOUT:
/* Check if we have monitored devices. When we don't have any devices
* left, after a certain timeout - we'll shutdown */
if (usbmuxd_HasDevices())
{
/* Reset the timeout base time */
dwNoDevicesTimestamp = 0;
}
else
{
/* If this is the first time we don't have a device - store the timestamp */
if (0 == dwNoDevicesTimestamp)
{
dwNoDevicesTimestamp = GetTickCount();
}
/* If its not the first time - check if the timeout has expired */
else
{
if ((GetTickCount() - dwNoDevicesTimestamp) >= NO_DEVICES_SHUTDOWN_TIMEOUT)
{
DEBUG_PRINT("Shutting down because we don't have a device");
bShouldShutdown = TRUE;
}
}
}
break;
/* Error */
default:
DEBUG_PRINT_WIN32_ERROR("WaitForSingleObject");
bShouldShutdown = TRUE;
}
}
/* Shutdown usbmuxd */
DEBUG_PRINT("Calling usbmuxd_Shutdown");
if (usbmuxd_Shutdown())
{
iRet = EXIT_SUCCESS;
}
else
{
DEBUG_PRINT_WIN32_ERROR("usbmuxd_Shutdown");
}
lblCleanup:
SAFE_CLOSE_HANDLE(hReadEvent);
SAFE_CLOSE_HANDLE(hShutdownEvent);
SAFE_CLOSE_HANDLE(hHostMutex);
SAFE_CLOSE_HANDLE(hKeepAliveEvent);
return iRet;
}
/******************************************************************************
* InternalWinMain Function
*****************************************************************************/
static int InternalWinMain()
{
int iRet = EXIT_FAILURE;
/* Parse the command line */
int argc = 0;
LPWSTR * argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (NULL == argv)
{
DEBUG_PRINT_WIN32_ERROR("CommandLineToArgvW");
return iRet;
}
if (CMD_ARGC != argc)
{
DEBUG_PRINT_ERROR("Invalid command line");
goto lblCleanup;
}
/* Run the usbmuxd */
iRet = RunUsbmuxdHost((WORD)_wtoi(argv[CMD_ARG_PORT]),
wcstoul(argv[CMD_ARG_HUB_ADDRESS], NULL, 10),
argv[CMD_ARG_READY_EVENT],
argv[CMD_ARG_SHUTDOWN_EVENT]);
lblCleanup:
(void)LocalFree(argv);
return iRet;
}
/******************************************************************************
* WinMain Function
*****************************************************************************/
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
__try
{
return InternalWinMain();
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
__try
{
DEBUG_PRINT_ERROR("An unhandleded exception was caught: 0x%x", GetExceptionCode());
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
}
return EXIT_FAILURE;
}