diff --git a/channels/audin/client/alsa/audin_alsa.c b/channels/audin/client/alsa/audin_alsa.c index 93a5a0db5..9ccd235cc 100644 --- a/channels/audin/client/alsa/audin_alsa.c +++ b/channels/audin/client/alsa/audin_alsa.c @@ -111,6 +111,7 @@ static WIN32ERROR audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, in BYTE* encoded_data; int rbytes_per_frame; int tbytes_per_frame; + int status; rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel; tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel; @@ -134,7 +135,16 @@ static WIN32ERROR audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, in while (frames > 0) { - if (WaitForSingleObject(alsa->stopEvent, 0) == WAIT_OBJECT_0) + status = WaitForSingleObject(alsa->stopEvent, 0); + + if (status == WAIT_FAILED) + { + ret = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", ret); + break; + } + + if (status == WAIT_OBJECT_0) break; cframes = alsa->frames_per_packet - alsa->buffer_frames; @@ -170,7 +180,16 @@ static WIN32ERROR audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, in encoded_size = alsa->buffer_frames * tbytes_per_frame; } - if (WaitForSingleObject(alsa->stopEvent, 0) == WAIT_OBJECT_0) + status = WaitForSingleObject(alsa->stopEvent, 0); + + if (status == WAIT_FAILED) + { + ret = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", ret); + break; + } + + if (status == WAIT_OBJECT_0) break; else { @@ -201,6 +220,7 @@ static void* audin_alsa_thread_func(void* arg) int tbytes_per_frame; snd_pcm_t* capture_handle = NULL; AudinALSADevice* alsa = (AudinALSADevice*) arg; + DWORD status; DEBUG_DVC("in"); @@ -210,57 +230,66 @@ static void* audin_alsa_thread_func(void* arg) if (!buffer) { WLog_ERR(TAG, "calloc failed!"); + error = CHANNEL_RC_NO_MEMORY; if (alsa->rdpcontext) - setChannelError(alsa->rdpcontext, CHANNEL_RC_NO_MEMORY, "calloc failed!"); - ExitThread((DWORD)CHANNEL_RC_NO_MEMORY); - return NULL; + setChannelError(alsa->rdpcontext, error, "calloc failed!"); + goto out; } freerdp_dsp_context_reset_adpcm(alsa->dsp_context); - do - { - if ((error = snd_pcm_open(&capture_handle, alsa->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) - { - WLog_ERR(TAG, "snd_pcm_open (%s)", snd_strerror(error)); - break; - } + if ((error = snd_pcm_open(&capture_handle, alsa->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) + { + WLog_ERR(TAG, "snd_pcm_open (%s)", snd_strerror(error)); + goto out; + } - if (!audin_alsa_set_params(alsa, capture_handle)) - { - break; - } + if (!audin_alsa_set_params(alsa, capture_handle)) + { + WLog_ERR(TAG, "audin_alsa_set_params failed"); + goto out; + } - while (!(WaitForSingleObject(alsa->stopEvent, 0) == WAIT_OBJECT_0)) - { - error = snd_pcm_readi(capture_handle, buffer, alsa->frames_per_packet); + while(1) + { + status = WaitForSingleObject(alsa->stopEvent, 0); - if (error == -EPIPE) - { - snd_pcm_recover(capture_handle, error, 0); - continue; - } - else if (error < 0) - { - WLog_ERR(TAG, "snd_pcm_readi (%s)", snd_strerror(error)); - break; - } + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } - if ((error = audin_alsa_thread_receive(alsa, buffer, error * rbytes_per_frame))) - { - WLog_ERR(TAG, "audin_alsa_thread_receive failed with error %lu", error); - break; - } + if (status == WAIT_OBJECT_0) + break; - } - } - while (0); + error = snd_pcm_readi(capture_handle, buffer, alsa->frames_per_packet); + + if (error == -EPIPE) + { + snd_pcm_recover(capture_handle, error, 0); + continue; + } + else if (error < 0) + { + WLog_ERR(TAG, "snd_pcm_readi (%s)", snd_strerror(error)); + break; + } + + if ((error = audin_alsa_thread_receive(alsa, buffer, error * rbytes_per_frame))) + { + WLog_ERR(TAG, "audin_alsa_thread_receive failed with error %lu", error); + break; + } + + } free(buffer); if (capture_handle) snd_pcm_close(capture_handle); - +out: DEBUG_DVC("out"); if (error && alsa->rdpcontext) setChannelError(alsa->rdpcontext, error, "audin_alsa_thread_func reported an error"); @@ -389,12 +418,18 @@ error_out: static WIN32ERROR audin_alsa_close(IAudinDevice* device) { + WIN32ERROR error = CHANNEL_RC_OK; AudinALSADevice* alsa = (AudinALSADevice*) device; if (alsa->stopEvent) { SetEvent(alsa->stopEvent); - WaitForSingleObject(alsa->thread, INFINITE); + if (WaitForSingleObject(alsa->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(alsa->stopEvent); alsa->stopEvent = NULL; @@ -409,7 +444,7 @@ static WIN32ERROR audin_alsa_close(IAudinDevice* device) alsa->receive = NULL; alsa->user_data = NULL; - return CHANNEL_RC_OK; + return error; } COMMAND_LINE_ARGUMENT_A audin_alsa_args[] = diff --git a/channels/audin/client/opensles/audin_opensl_es.c b/channels/audin/client/opensles/audin_opensl_es.c index bbe147eaa..01facfc4f 100644 --- a/channels/audin/client/opensles/audin_opensl_es.c +++ b/channels/audin/client/opensles/audin_opensl_es.c @@ -83,6 +83,7 @@ static void* audin_opensles_thread_func(void* arg) const size_t raw_size = opensles->frames_per_packet * opensles->bytes_per_channel; int rc = CHANNEL_RC_OK; WIN32ERROR error = CHANNEL_RC_OK; + DWORD status; DEBUG_DVC("opensles=%p", opensles); @@ -95,18 +96,31 @@ static void* audin_opensles_thread_func(void* arg) buffer.v = calloc(1, raw_size); if (!buffer.v) { + error = CHANNEL_RC_NO_MEMORY; WLog_ERR(TAG, "calloc failed!"); if (opensles->rdpcontext) setChannelError(opensles->rdpcontext, CHANNEL_RC_NO_MEMORY, "audin_opensles_thread_func reported an error"); - ExitThread((DWORD)CHANNEL_RC_NO_MEMORY); - return NULL; + goto out; } freerdp_dsp_context_reset_adpcm(opensles->dsp_context); - while (WAIT_OBJECT_0 != WaitForSingleObject(opensles->stopEvent, 0)) + while (1) { - size_t encoded_size; + + status = WaitForSingleObject(opensles->stopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (status == WAIT_OBJECT_0) + break; + + size_t encoded_size; void *encoded_data; rc = android_RecIn(opensles->stream, buffer.s, raw_size); @@ -154,7 +168,7 @@ static void* audin_opensles_thread_func(void* arg) } free(buffer.v); - +out: DEBUG_DVC("thread shutdown."); if (error && opensles->rdpcontext) @@ -348,6 +362,7 @@ error_out: static WIN32ERROR audin_opensles_close(IAudinDevice* device) { + WIN32ERROR error; AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*) device; DEBUG_DVC("device=%p", device); @@ -367,7 +382,12 @@ static WIN32ERROR audin_opensles_close(IAudinDevice* device) assert(opensles->stream); SetEvent(opensles->stopEvent); - WaitForSingleObject(opensles->thread, INFINITE); + if (WaitForSingleObject(opensles->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(opensles->stopEvent); CloseHandle(opensles->thread); diff --git a/channels/audin/client/opensles/opensl_io.c b/channels/audin/client/opensles/opensl_io.c index ba45ceaca..d9e684fe5 100644 --- a/channels/audin/client/opensles/opensl_io.c +++ b/channels/audin/client/opensles/opensl_io.c @@ -339,6 +339,7 @@ int android_RecIn(OPENSL_STREAM *p,short *buffer,int size) { queue_element *e; int rc; + DWORD status; assert(p); assert(buffer); @@ -366,7 +367,15 @@ int android_RecIn(OPENSL_STREAM *p,short *buffer,int size) /* Wait for queue to be filled... */ if (!Queue_Count(p->queue)) - WaitForSingleObject(p->queue->event, INFINITE); + { + status = WaitForSingleObject(p->queue->event, INFINITE); + if (status == WAIT_FAILED) + { + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", GetLastError()); + return -1; + } + } + e = Queue_Dequeue(p->queue); if (!e) @@ -383,6 +392,6 @@ int android_RecIn(OPENSL_STREAM *p,short *buffer,int size) free(e->data); free(e); - return rc; + return rc; } diff --git a/channels/audin/client/oss/audin_oss.c b/channels/audin/client/oss/audin_oss.c index f097f68d6..9169f49e1 100644 --- a/channels/audin/client/oss/audin_oss.c +++ b/channels/audin/client/oss/audin_oss.c @@ -173,8 +173,9 @@ static void* audin_oss_thread_func(void* arg) int tmp, buffer_size, encoded_size; AudinOSSDevice* oss = (AudinOSSDevice*)arg; WIN32ERROR error; + DWORD status; - if (arg == NULL) + if (arg == NULL) { error = ERROR_INVALID_PARAMETER; goto err_out; @@ -263,8 +264,20 @@ static void* audin_oss_thread_func(void* arg) ZeroMemory(buffer, buffer_size); freerdp_dsp_context_reset_adpcm(oss->dsp_context); - while (WaitForSingleObject(oss->stopEvent, 0) != WAIT_OBJECT_0) + while (1) { + status = WaitForSingleObject(oss->stopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + goto err_out; + } + + if (status == WAIT_OBJECT_0) + break; + tmp = read(pcm_handle, buffer, buffer_size); /* Error happen. */ @@ -353,6 +366,7 @@ static WIN32ERROR audin_oss_open(IAudinDevice *device, AudinReceive receive, voi static WIN32ERROR audin_oss_close(IAudinDevice *device) { + WIN32ERROR error; AudinOSSDevice *oss = (AudinOSSDevice*)device; if (device == NULL) @@ -361,7 +375,12 @@ static WIN32ERROR audin_oss_close(IAudinDevice *device) if (oss->stopEvent != NULL) { SetEvent(oss->stopEvent); - WaitForSingleObject(oss->thread, INFINITE); + if (WaitForSingleObject(oss->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(oss->stopEvent); oss->stopEvent = NULL; CloseHandle(oss->thread); diff --git a/channels/audin/client/winmm/audin_winmm.c b/channels/audin/client/winmm/audin_winmm.c index 3bef7e474..3317d4107 100644 --- a/channels/audin/client/winmm/audin_winmm.c +++ b/channels/audin/client/winmm/audin_winmm.c @@ -100,6 +100,7 @@ static DWORD audin_winmm_thread_func(void* arg) char *buffer; int size, i; WAVEHDR waveHdr[4]; + DWORD status; if (!winmm->hWaveIn) { @@ -136,7 +137,14 @@ static DWORD audin_winmm_thread_func(void* arg) } waveInStart(winmm->hWaveIn); - WaitForSingleObject(winmm->stopEvent, INFINITE); + status = WaitForSingleObject(winmm->stopEvent, INFINITE); + + if (status == WAIT_FAILED) + { + DEBUG_DVC("WaitForSingleObject failed."); + if (winmm->rdpcontext) + setChannelError(winmm->rdpcontext, ERROR_INTERNAL_ERROR, "audin_winmm_thread_func reported an error"); + } waveInStop(winmm->hWaveIn); @@ -176,11 +184,20 @@ static WIN32ERROR audin_winmm_free(IAudinDevice* device) static WIN32ERROR audin_winmm_close(IAudinDevice* device) { + DWORD status; + WIN32ERROR error = CHANNEL_RC_OK; AudinWinmmDevice* winmm = (AudinWinmmDevice*) device; SetEvent(winmm->stopEvent); - WaitForSingleObject(winmm->thread, INFINITE); + status = WaitForSingleObject(winmm->thread, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(winmm->thread); CloseHandle(winmm->stopEvent); @@ -190,7 +207,7 @@ static WIN32ERROR audin_winmm_close(IAudinDevice* device) winmm->receive = NULL; winmm->user_data = NULL; - return CHANNEL_RC_OK; + return error; } static WIN32ERROR audin_winmm_set_format(IAudinDevice* device, audinFormat* format, UINT32 FramesPerPacket) diff --git a/channels/audin/server/audin.c b/channels/audin/server/audin.c index cd1a831cd..daf449870 100644 --- a/channels/audin/server/audin.c +++ b/channels/audin/server/audin.c @@ -215,6 +215,8 @@ static WIN32ERROR audin_server_recv_formats(audin_server* audin, wStream* s, UIN } IFCALLRET(audin->context.Opening, success, &audin->context); + if (success) + WLog_ERR(TAG, "context.Opening failed with error %lu", success); return success; } @@ -267,6 +269,9 @@ static WIN32ERROR audin_server_recv_open_reply(audin_server* audin, wStream* s, IFCALLRET(audin->context.OpenResult, success, &audin->context, Result); + if (success) + WLog_ERR(TAG, "context.OpenResult failed with error %lu", success); + return success; } @@ -330,7 +335,10 @@ static WIN32ERROR audin_server_recv_data(audin_server* audin, wStream* s, UINT32 IFCALLRET(audin->context.ReceiveSamples, success, &audin->context, src, frames); - return success; + if (success) + WLog_ERR(TAG, "context.ReceiveSamples failed with error %lu", success); + + return success; } static void* audin_server_thread_func(void* arg) @@ -345,6 +353,7 @@ static void* audin_server_thread_func(void* arg) DWORD BytesReturned = 0; audin_server* audin = (audin_server*) arg; WIN32ERROR error = CHANNEL_RC_OK; + DWORD status; buffer = NULL; BytesReturned = 0; @@ -372,9 +381,16 @@ static void* audin_server_thread_func(void* arg) while (1) { - if (WaitForMultipleObjects(nCount, events, FALSE, 100) == WAIT_OBJECT_0) + if ((status = WaitForMultipleObjects(nCount, events, FALSE, 100)) == WAIT_OBJECT_0) goto out; + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + goto out; + } + if (WTSVirtualChannelQuery(audin->audin_channel, WTSVirtualChannelReady, &buffer, &BytesReturned) == FALSE) { WLog_ERR(TAG, "WTSVirtualChannelQuery failed"); @@ -410,9 +426,16 @@ static void* audin_server_thread_func(void* arg) while (ready) { - if (WaitForMultipleObjects(nCount, events, FALSE, INFINITE) == WAIT_OBJECT_0) + if ((status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE)) == WAIT_OBJECT_0) break; + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + goto out; + } + Stream_SetPosition(s, 0); if (!WTSVirtualChannelRead(audin->audin_channel, 0, NULL, 0, &BytesReturned)) @@ -559,7 +582,12 @@ static BOOL audin_server_close(audin_server_context* context) if (audin->thread) { SetEvent(audin->stopEvent); - WaitForSingleObject(audin->thread, INFINITE); + if (WaitForSingleObject(audin->thread, INFINITE) == WAIT_FAILED) + { + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", GetLastError()); + return FALSE; + } + CloseHandle(audin->thread); CloseHandle(audin->stopEvent); audin->thread = NULL; diff --git a/channels/cliprdr/client/cliprdr_main.c b/channels/cliprdr/client/cliprdr_main.c index aca4ded8b..53f238429 100644 --- a/channels/cliprdr/client/cliprdr_main.c +++ b/channels/cliprdr/client/cliprdr_main.c @@ -860,7 +860,7 @@ WIN32ERROR cliprdr_add_init_handle_data(void* pInitHandle, void* pUserData) WLog_ERR(TAG, "ListDictionary_Add failed!"); return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* cliprdr_get_init_handle_data(void* pInitHandle) @@ -899,7 +899,7 @@ WIN32ERROR cliprdr_add_open_handle_data(DWORD openHandle, void* pUserData) return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* cliprdr_get_open_handle_data(DWORD openHandle) @@ -1096,8 +1096,12 @@ static WIN32ERROR cliprdr_virtual_channel_event_disconnected(cliprdrPlugin* clip { UINT rc; - if (MessageQueue_PostQuit(cliprdr->queue, 0)) - WaitForSingleObject(cliprdr->thread, INFINITE); + if (MessageQueue_PostQuit(cliprdr->queue, 0) && (WaitForSingleObject(cliprdr->thread, INFINITE) == WAIT_FAILED)) + { + rc = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", rc); + return rc; + } MessageQueue_Free(cliprdr->queue); CloseHandle(cliprdr->thread); diff --git a/channels/cliprdr/server/cliprdr_main.c b/channels/cliprdr/server/cliprdr_main.c index a2f7c423e..6de11d79f 100644 --- a/channels/cliprdr/server/cliprdr_main.c +++ b/channels/cliprdr/server/cliprdr_main.c @@ -106,7 +106,7 @@ WIN32ERROR cliprdr_server_packet_send(CliprdrServerPrivate* cliprdr, wStream* s) Stream_Free(s, TRUE); - return status ? ERROR_SUCCESS : ERROR_INTERNAL_ERROR; + return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } static WIN32ERROR cliprdr_server_capabilities(CliprdrServerContext* context, CLIPRDR_CAPABILITIES* capabilities) @@ -1022,6 +1022,7 @@ WIN32ERROR cliprdr_server_read(CliprdrServerContext* context) CLIPRDR_HEADER header; CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*) context->handle; WIN32ERROR error; + DWORD status; s = cliprdr->s; @@ -1030,8 +1031,17 @@ WIN32ERROR cliprdr_server_read(CliprdrServerContext* context) BytesReturned = 0; BytesToRead = CLIPRDR_HEADER_LENGTH - Stream_GetPosition(s); - if (WaitForSingleObject(cliprdr->ChannelEvent, 0) != WAIT_OBJECT_0) - return ERROR_SUCCESS; + status = WaitForSingleObject(cliprdr->ChannelEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } + + if (status == WAIT_TIMEOUT) + return CHANNEL_RC_OK; if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, (PCHAR) Stream_Pointer(s), BytesToRead, &BytesReturned)) @@ -1065,8 +1075,17 @@ WIN32ERROR cliprdr_server_read(CliprdrServerContext* context) BytesReturned = 0; BytesToRead = (header.dataLen + CLIPRDR_HEADER_LENGTH) - Stream_GetPosition(s); - if (WaitForSingleObject(cliprdr->ChannelEvent, 0) != WAIT_OBJECT_0) - return ERROR_SUCCESS; + status = WaitForSingleObject(cliprdr->ChannelEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } + + if (status == WAIT_TIMEOUT) + return CHANNEL_RC_OK; if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, (PCHAR) Stream_Pointer(s), BytesToRead, &BytesReturned)) @@ -1094,8 +1113,17 @@ WIN32ERROR cliprdr_server_read(CliprdrServerContext* context) /* check for trailing zero bytes */ - if (WaitForSingleObject(cliprdr->ChannelEvent, 0) != WAIT_OBJECT_0) - return ERROR_SUCCESS; + status = WaitForSingleObject(cliprdr->ChannelEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } + + if (status == WAIT_TIMEOUT) + return CHANNEL_RC_OK; BytesReturned = 0; BytesToRead = 4; @@ -1154,10 +1182,35 @@ static void* cliprdr_server_thread(void* arg) { status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); - if (WaitForSingleObject(cliprdr->StopEvent, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + goto out; + } + + status = WaitForSingleObject(cliprdr->StopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + goto out; + } + + if (status == WAIT_OBJECT_0) break; - if (WaitForSingleObject(ChannelEvent, 0) == WAIT_OBJECT_0) + status = WaitForSingleObject(ChannelEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + goto out; + } + + if (status == WAIT_OBJECT_0) { if ((error = context->CheckEventHandle(context))) { @@ -1265,12 +1318,18 @@ static WIN32ERROR cliprdr_server_start(CliprdrServerContext* context) static WIN32ERROR cliprdr_server_stop(CliprdrServerContext* context) { + WIN32ERROR error = CHANNEL_RC_OK; CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*) context->handle; if (cliprdr->StopEvent) { SetEvent(cliprdr->StopEvent); - WaitForSingleObject(cliprdr->Thread, INFINITE); + if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(cliprdr->Thread); CloseHandle(cliprdr->StopEvent); } @@ -1278,7 +1337,7 @@ static WIN32ERROR cliprdr_server_stop(CliprdrServerContext* context) if (cliprdr->ChannelHandle) return context->Close(context); - return CHANNEL_RC_OK; + return error; } static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context) diff --git a/channels/drdynvc/client/drdynvc_main.c b/channels/drdynvc/client/drdynvc_main.c index cb55240c5..477fc3dc8 100644 --- a/channels/drdynvc/client/drdynvc_main.c +++ b/channels/drdynvc/client/drdynvc_main.c @@ -437,6 +437,9 @@ WIN32ERROR dvcman_create_channel(IWTSVirtualChannelManager* pChannelMgr, UINT32 context = dvcman->drdynvc->context; IFCALLRET(context->OnChannelConnected, error, context, ChannelName, listener->iface.pInterface); + if (error) + WLog_ERR(TAG, "context.ReceiveSamples failed with error %lu", error); + return error; } else @@ -475,12 +478,11 @@ WIN32ERROR dvcman_open_channel(IWTSVirtualChannelManager* pChannelMgr, UINT32 Ch if (channel->status == CHANNEL_RC_OK) { pCallback = channel->channel_callback; - if (pCallback->OnOpen) - if ((error = pCallback->OnOpen(pCallback))) - { - WLog_ERR(TAG, "OnOpen failed with eror %lu!", error); - return error; - } + if ((pCallback->OnOpen) && (error = pCallback->OnOpen(pCallback))) + { + WLog_ERR(TAG, "OnOpen failed with eror %lu!", error); + return error; + } WLog_DBG(TAG, "open_channel: ChannelId %d", ChannelId); } @@ -518,12 +520,11 @@ WIN32ERROR dvcman_close_channel(IWTSVirtualChannelManager* pChannelMgr, UINT32 C ichannel = (IWTSVirtualChannel*) channel; - if (ichannel->Close) - if ((error = ichannel->Close(ichannel))) - { - WLog_ERR(TAG, "Close failed with eror %lu!", error); - return error; - } + if ((ichannel->Close) && (error = ichannel->Close(ichannel))) + { + WLog_ERR(TAG, "Close failed with eror %lu!", error); + return error; + } } ArrayList_Remove(dvcman->channels, channel); @@ -1038,7 +1039,7 @@ WIN32ERROR drdynvc_add_init_handle_data(void* pInitHandle, void* pUserData) return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* drdynvc_get_init_handle_data(void* pInitHandle) @@ -1077,7 +1078,7 @@ WIN32ERROR drdynvc_add_open_handle_data(DWORD openHandle, void* pUserData) return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* drdynvc_get_open_handle_data(DWORD openHandle) @@ -1309,8 +1310,12 @@ static WIN32ERROR drdynvc_virtual_channel_event_disconnected(drdynvcPlugin* drdy { WIN32ERROR status; - if (MessageQueue_PostQuit(drdynvc->queue, 0)) - WaitForSingleObject(drdynvc->thread, INFINITE); + if (MessageQueue_PostQuit(drdynvc->queue, 0) && (WaitForSingleObject(drdynvc->thread, INFINITE) == WAIT_FAILED)) + { + status = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", status); + return status; + } MessageQueue_Free(drdynvc->queue); CloseHandle(drdynvc->thread); diff --git a/channels/drdynvc/server/drdynvc_main.c b/channels/drdynvc/server/drdynvc_main.c index 1baebdc04..b15d5adb9 100644 --- a/channels/drdynvc/server/drdynvc_main.c +++ b/channels/drdynvc/server/drdynvc_main.c @@ -142,9 +142,15 @@ static WIN32ERROR drdynvc_server_start(DrdynvcServerContext* context) static WIN32ERROR drdynvc_server_stop(DrdynvcServerContext* context) { + WIN32ERROR error; SetEvent(context->priv->StopEvent); - WaitForSingleObject(context->priv->Thread, INFINITE); + if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(context->priv->Thread); return CHANNEL_RC_OK; diff --git a/channels/drive/client/drive_main.c b/channels/drive/client/drive_main.c index fe6288dd0..f949894b6 100644 --- a/channels/drive/client/drive_main.c +++ b/channels/drive/client/drive_main.c @@ -671,12 +671,17 @@ static WIN32ERROR drive_irp_request(DEVICE* device, IRP* irp) return CHANNEL_RC_OK; } -static void drive_free(DEVICE* device) +static WIN32ERROR drive_free(DEVICE* device) { DRIVE_DEVICE* drive = (DRIVE_DEVICE*) device; + WIN32ERROR error = CHANNEL_RC_OK; - if (MessageQueue_PostQuit(drive->IrpQueue, 0)) - WaitForSingleObject(drive->thread, INFINITE); + if (MessageQueue_PostQuit(drive->IrpQueue, 0) && (WaitForSingleObject(drive->thread, INFINITE) == WAIT_FAILED)) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(drive->thread); @@ -686,6 +691,7 @@ static void drive_free(DEVICE* device) Stream_Free(drive->device.data, TRUE); free(drive); + return error; } WIN32ERROR drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, char* name, char* path) diff --git a/channels/echo/server/echo_main.c b/channels/echo/server/echo_main.c index ff881bccc..80ff59a58 100644 --- a/channels/echo/server/echo_main.c +++ b/channels/echo/server/echo_main.c @@ -76,7 +76,12 @@ static WIN32ERROR echo_server_open_channel(echo_server* echo) while (echo->echo_channel == NULL) { - WaitForSingleObject(hEvent, 1000); + if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED) + { + Error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", Error); + return Error; + } echo->echo_channel = WTSVirtualChannelOpenEx(echo->SessionId, "ECHO", WTS_CHANNEL_OPTION_DYNAMIC); @@ -106,6 +111,7 @@ static void* echo_server_thread_func(void* arg) DWORD BytesReturned = 0; echo_server* echo = (echo_server*) arg; WIN32ERROR error; + DWORD status; if ((error = echo_server_open_channel(echo))) { @@ -134,7 +140,16 @@ static void* echo_server_thread_func(void* arg) while (1) { - if (WaitForMultipleObjects(nCount, events, FALSE, 100) == WAIT_OBJECT_0) + status = WaitForMultipleObjects(nCount, events, FALSE, 100); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + break; + } + + if (status == WAIT_OBJECT_0) { IFCALLRET(echo->context.OpenResult, error, &echo->context, ECHO_SERVER_OPEN_RESULT_CLOSED); if (error) @@ -174,7 +189,16 @@ static void* echo_server_thread_func(void* arg) while (ready) { - if (WaitForMultipleObjects(nCount, events, FALSE, INFINITE) == WAIT_OBJECT_0) + status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + break; + } + + if (status == WAIT_OBJECT_0) break; Stream_SetPosition(s, 0); @@ -240,18 +264,26 @@ static WIN32ERROR echo_server_open(echo_server_context* context) static WIN32ERROR echo_server_close(echo_server_context* context) { + WIN32ERROR error = CHANNEL_RC_OK; echo_server* echo = (echo_server*) context; if (echo->thread) { SetEvent(echo->stopEvent); - WaitForSingleObject(echo->thread, INFINITE); + + if (WaitForSingleObject(echo->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } + CloseHandle(echo->thread); CloseHandle(echo->stopEvent); echo->thread = NULL; echo->stopEvent = NULL; } - return CHANNEL_RC_OK; + return error; } static BOOL echo_server_request(echo_server_context* context, const BYTE* buffer, UINT32 length) diff --git a/channels/encomsp/client/encomsp_main.c b/channels/encomsp/client/encomsp_main.c index 10a0b5217..c5ff21bfd 100644 --- a/channels/encomsp/client/encomsp_main.c +++ b/channels/encomsp/client/encomsp_main.c @@ -1105,8 +1105,12 @@ static WIN32ERROR encomsp_virtual_channel_event_disconnected(encomspPlugin* enco { UINT rc; - if (MessageQueue_PostQuit(encomsp->queue, 0)) - WaitForSingleObject(encomsp->thread, INFINITE); + if (MessageQueue_PostQuit(encomsp->queue, 0) && (WaitForSingleObject(encomsp->thread, INFINITE) == WAIT_FAILED)) + { + rc = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", rc); + return rc; + } MessageQueue_Free(encomsp->queue); CloseHandle(encomsp->thread); diff --git a/channels/encomsp/server/encomsp_main.c b/channels/encomsp/server/encomsp_main.c index 02ac629cb..b915c0ed0 100644 --- a/channels/encomsp/server/encomsp_main.c +++ b/channels/encomsp/server/encomsp_main.c @@ -168,6 +168,7 @@ static void* encomsp_server_thread(void* arg) ENCOMSP_ORDER_HEADER* header; EncomspServerContext* context; WIN32ERROR error = CHANNEL_RC_OK; + DWORD status; context = (EncomspServerContext*) arg; @@ -197,9 +198,25 @@ static void* encomsp_server_thread(void* arg) while (1) { - WaitForMultipleObjects(nCount, events, FALSE, INFINITE); + status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); - if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + break; + } + + status = WaitForSingleObject(context->priv->StopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + break; + } + + if (status == WAIT_OBJECT_0) { break; } @@ -275,12 +292,18 @@ static WIN32ERROR encomsp_server_start(EncomspServerContext* context) static WIN32ERROR encomsp_server_stop(EncomspServerContext* context) { + WIN32ERROR error = CHANNEL_RC_OK; SetEvent(context->priv->StopEvent); - WaitForSingleObject(context->priv->Thread, INFINITE); + if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } CloseHandle(context->priv->Thread); - return CHANNEL_RC_OK; + return error; } EncomspServerContext* encomsp_server_context_new(HANDLE vcm) diff --git a/channels/parallel/client/parallel_main.c b/channels/parallel/client/parallel_main.c index 3991ddfdf..c1d67a8e6 100644 --- a/channels/parallel/client/parallel_main.c +++ b/channels/parallel/client/parallel_main.c @@ -332,18 +332,24 @@ static WIN32ERROR parallel_irp_request(DEVICE* device, IRP* irp) return CHANNEL_RC_OK; } -static void parallel_free(DEVICE* device) +static WIN32ERROR parallel_free(DEVICE* device) { + WIN32ERROR error; PARALLEL_DEVICE* parallel = (PARALLEL_DEVICE*) device; - if (MessageQueue_PostQuit(parallel->queue, 0)) - WaitForSingleObject(parallel->thread, INFINITE); + if (MessageQueue_PostQuit(parallel->queue, 0) && (WaitForSingleObject(parallel->thread, INFINITE) == WAIT_FAILED)) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(parallel->thread); Stream_Free(parallel->device.data, TRUE); MessageQueue_Free(parallel->queue); free(parallel); + return CHANNEL_RC_OK; } #ifdef STATIC_CHANNELS diff --git a/channels/printer/client/printer_main.c b/channels/printer/client/printer_main.c index db1e7cefd..f9562f739 100644 --- a/channels/printer/client/printer_main.c +++ b/channels/printer/client/printer_main.c @@ -204,6 +204,12 @@ static void* printer_thread_func(void* arg) while (1) { DWORD rc = WaitForMultipleObjects(2, obj, FALSE, INFINITE); + if (rc == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } if (rc == WAIT_OBJECT_0 + 1) break; @@ -245,13 +251,19 @@ static WIN32ERROR printer_irp_request(DEVICE* device, IRP* irp) return CHANNEL_RC_OK; } -static void printer_free(DEVICE* device) +static WIN32ERROR printer_free(DEVICE* device) { IRP* irp; PRINTER_DEVICE* printer_dev = (PRINTER_DEVICE*) device; + WIN32ERROR error; SetEvent(printer_dev->stopEvent); - WaitForSingleObject(printer_dev->thread, INFINITE); + if (WaitForSingleObject(printer_dev->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + return error; + } while ((irp = (IRP*) InterlockedPopEntrySList(printer_dev->pIrpList)) != NULL) irp->Discard(irp); @@ -268,6 +280,7 @@ static void printer_free(DEVICE* device) free(printer_dev->device.name); free(printer_dev); + return CHANNEL_RC_OK; } WIN32ERROR printer_register(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, rdpPrinter* printer) diff --git a/channels/rail/client/rail_main.c b/channels/rail/client/rail_main.c index 10168c075..f04211279 100644 --- a/channels/rail/client/rail_main.c +++ b/channels/rail/client/rail_main.c @@ -596,11 +596,15 @@ static WIN32ERROR rail_virtual_channel_event_connected(railPlugin* rail, LPVOID return CHANNEL_RC_OK; } -static void rail_virtual_channel_event_disconnected(railPlugin* rail) +static WIN32ERROR rail_virtual_channel_event_disconnected(railPlugin* rail) { UINT rc; - if (MessageQueue_PostQuit(rail->queue, 0)) - WaitForSingleObject(rail->thread, INFINITE); + if (MessageQueue_PostQuit(rail->queue, 0) && (WaitForSingleObject(rail->thread, INFINITE) == WAIT_FAILED)) + { + rc = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", rc); + return rc; + } MessageQueue_Free(rail->queue); CloseHandle(rail->thread); @@ -613,6 +617,7 @@ static void rail_virtual_channel_event_disconnected(railPlugin* rail) { WLog_ERR(TAG, "pVirtualChannelClose failed with %s [%08X]", WTSErrorToString(rc), rc); + return rc; } if (rail->data_in) @@ -622,6 +627,7 @@ static void rail_virtual_channel_event_disconnected(railPlugin* rail) } rail_remove_open_handle_data(rail->OpenHandle); + return CHANNEL_RC_OK; } static void rail_virtual_channel_event_terminated(railPlugin* rail) @@ -647,14 +653,12 @@ static VOID VCAPITYPE rail_virtual_channel_init_event(LPVOID pInitHandle, UINT e { case CHANNEL_EVENT_CONNECTED: if ((error = rail_virtual_channel_event_connected(rail, pData, dataLength))) - { WLog_ERR(TAG, "rail_virtual_channel_event_connected failed with error %lu!", error); - return; - } break; case CHANNEL_EVENT_DISCONNECTED: - rail_virtual_channel_event_disconnected(rail); + if ((error = rail_virtual_channel_event_disconnected(rail))) + WLog_ERR(TAG, "rail_virtual_channel_event_disconnected failed with error %lu!", error); break; case CHANNEL_EVENT_TERMINATED: diff --git a/channels/rail/client/rail_orders.c b/channels/rail/client/rail_orders.c index ef3fc1d03..f43b92d68 100644 --- a/channels/rail/client/rail_orders.c +++ b/channels/rail/client/rail_orders.c @@ -213,14 +213,17 @@ WIN32ERROR rail_write_client_exec_order(wStream* s, RAIL_EXEC_ORDER* exec) Stream_Write_UINT16(s, exec->arguments.length); /* argumentsLength (2 bytes) */ if ((error = rail_write_unicode_string_value(s, &exec->exeOrFile))) { + WLog_ERR(TAG, "rail_write_unicode_string_value failed with error %lu", error); return error; } if ((error = rail_write_unicode_string_value(s, &exec->workingDir))) { + WLog_ERR(TAG, "rail_write_unicode_string_value failed with error %lu", error); return error; } if ((error = rail_write_unicode_string_value(s, &exec->arguments))) { + WLog_ERR(TAG, "rail_write_unicode_string_value failed with error %lu", error); return error; } return error; @@ -347,7 +350,9 @@ WIN32ERROR rail_recv_handshake_order(railPlugin* rail, RAIL_HANDSHAKE_ORDER* han if (context->custom) { IFCALLRET(context->ServerHandshake, error, context, handshake); - } + if (error) + WLog_ERR(TAG, "context.ServerHandshake failed with error %lu", error); + } return error; } @@ -366,7 +371,10 @@ WIN32ERROR rail_recv_handshake_ex_order(railPlugin* rail, RAIL_HANDSHAKE_EX_ORDE if (context->custom) { IFCALLRET(context->ClientHandshakeEx, error, context, handshakeEx); - } + if (error) + WLog_ERR(TAG, "context.ClientHandshakeEx failed with error %lu", error); + + } return error; } @@ -387,7 +395,10 @@ WIN32ERROR rail_recv_exec_result_order(railPlugin* rail, RAIL_EXEC_RESULT_ORDER* if (context->custom) { IFCALLRET(context->ServerExecuteResult, error, context, execResult); - } + if (error) + WLog_ERR(TAG, "context.ServerExecuteResult failed with error %lu", error); + + } return error; } @@ -406,7 +417,9 @@ WIN32ERROR rail_recv_server_sysparam_order(railPlugin* rail, RAIL_SYSPARAM_ORDER if (context->custom) { IFCALLRET(context->ServerSystemParam, error, context, sysparam); - } + if (error) + WLog_ERR(TAG, "context.ServerSystemParam failed with error %lu", error); + } return error; } @@ -425,7 +438,9 @@ WIN32ERROR rail_recv_server_minmaxinfo_order(railPlugin* rail, RAIL_MINMAXINFO_O if (context->custom) { IFCALLRET(context->ServerMinMaxInfo, error, context, minMaxInfo); - } + if (error) + WLog_ERR(TAG, "context.ServerMinMaxInfo failed with error %lu", error); + } return error; } @@ -444,7 +459,9 @@ WIN32ERROR rail_recv_server_localmovesize_order(railPlugin* rail, RAIL_LOCALMOVE if (context->custom) { IFCALLRET(context->ServerLocalMoveSize, error, context, localMoveSize); - } + if (error) + WLog_ERR(TAG, "context.ServerLocalMoveSize failed with error %lu", error); + } return error; } @@ -463,7 +480,9 @@ WIN32ERROR rail_recv_server_get_appid_resp_order(railPlugin* rail, RAIL_GET_APPI if (context->custom) { IFCALLRET(context->ServerGetAppIdResponse, error, context, getAppIdResp); - } + if (error) + WLog_ERR(TAG, "context.ServerGetAppIdResponse failed with error %lu", error); + } return error; } @@ -482,7 +501,9 @@ WIN32ERROR rail_recv_langbar_info_order(railPlugin* rail, RAIL_LANGBAR_INFO_ORDE if (context->custom) { IFCALLRET(context->ServerLanguageBarInfo, error, context, langBarInfo); - } + if (error) + WLog_ERR(TAG, "context.ServerLanguageBarInfo failed with error %lu", error); + } return error; } diff --git a/channels/rdpdr/client/devman.c b/channels/rdpdr/client/devman.c index 02069c4a6..259908ea5 100644 --- a/channels/rdpdr/client/devman.c +++ b/channels/rdpdr/client/devman.c @@ -101,7 +101,7 @@ static WIN32ERROR devman_register_device(DEVMAN* devman, DEVICE* device) WLog_INFO(TAG, "ListDictionary_Add failed!"); return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } DEVICE* devman_get_device_by_id(DEVMAN* devman, UINT32 id) diff --git a/channels/rdpdr/client/rdpdr_main.c b/channels/rdpdr/client/rdpdr_main.c index 3f903c98d..e522a49b5 100644 --- a/channels/rdpdr/client/rdpdr_main.c +++ b/channels/rdpdr/client/rdpdr_main.c @@ -488,14 +488,15 @@ static void* drive_hotplug_thread_func(void* arg) struct timeval tv; int rv; WIN32ERROR error; + DWORD status; rdpdr = (rdpdrPlugin*) arg; if (!(rdpdr->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) { WLog_ERR(TAG, "CreateEvent failed!"); - ExitThread(ERROR_INTERNAL_ERROR); - return NULL; + error = ERROR_INTERNAL_ERROR; + goto out; } mfd = open("/proc/mounts", O_RDONLY, 0); @@ -503,8 +504,8 @@ static void* drive_hotplug_thread_func(void* arg) if (mfd < 0) { WLog_ERR(TAG, "ERROR: Unable to open /proc/mounts."); - ExitThread(ERROR_INTERNAL_ERROR); - return NULL; + error = ERROR_INTERNAL_ERROR; + goto out; } FD_ZERO(&rfds); @@ -515,13 +516,19 @@ static void* drive_hotplug_thread_func(void* arg) if ((error = handle_hotplug(rdpdr))) { WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error); - ExitThread((DWORD)error); - return NULL; + goto out; } while ((rv = select(mfd+1, NULL, NULL, &rfds, &tv)) >= 0) { - if (WaitForSingleObject(rdpdr->stopEvent, 0) == WAIT_OBJECT_0) + status = WaitForSingleObject(rdpdr->stopEvent, 0); + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + if (status == WAIT_OBJECT_0) break; if (FD_ISSET(mfd, &rfds)) @@ -530,8 +537,7 @@ static void* drive_hotplug_thread_func(void* arg) if ((error = handle_hotplug(rdpdr))) { WLog_ERR(TAG, "handle_hotplug failed with error %lu!", error); - ExitThread((DWORD)error); - return NULL; + goto out; } } @@ -541,20 +547,31 @@ static void* drive_hotplug_thread_func(void* arg) tv.tv_usec = 0; } - ExitThread(CHANNEL_RC_OK); - return NULL; +out: + if (error && rdpdr->rdpcontext) + setChannelError(rdpdr->rdpcontext, error, "drive_hotplug_thread_func reported an error"); + + ExitThread((DWORD)error); + return NULL; } -static void drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr) +static WIN32ERROR drive_hotplug_thread_terminate(rdpdrPlugin* rdpdr) { + WIN32ERROR error; if (rdpdr->hotplugThread) { if (rdpdr->stopEvent) SetEvent(rdpdr->stopEvent); - WaitForSingleObject(rdpdr->hotplugThread, INFINITE); + if (WaitForSingleObject(rdpdr->hotplugThread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } rdpdr->hotplugThread = NULL; } + return CHANNEL_RC_OK; } #endif @@ -793,7 +810,10 @@ static WIN32ERROR rdpdr_process_irp(rdpdrPlugin* rdpdr, wStream* s) IFCALLRET(irp->device->IRPRequest, error, irp->device, irp); - return error; + if (error) + WLog_ERR(TAG, "device->IRPRequest failed with error %lu", error); + + return error; } static WIN32ERROR rdpdr_process_init(rdpdrPlugin* rdpdr) @@ -934,7 +954,7 @@ static WIN32ERROR rdpdr_process_receive(rdpdrPlugin* rdpdr, wStream* s) } Stream_Free(s, TRUE); - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } @@ -961,7 +981,7 @@ WIN32ERROR rdpdr_add_init_handle_data(void* pInitHandle, void* pUserData) WLog_ERR(TAG, "ListDictionary_Add failed!"); return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* rdpdr_get_init_handle_data(void* pInitHandle) @@ -1001,7 +1021,7 @@ WIN32ERROR rdpdr_add_open_handle_data(DWORD openHandle, void* pUserData) WLog_ERR(TAG, "ListDictionary_Add failed!"); return ERROR_INTERNAL_ERROR; } - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } void* rdpdr_get_open_handle_data(DWORD openHandle) @@ -1061,7 +1081,7 @@ static WIN32ERROR rdpdr_virtual_channel_event_data_received(rdpdrPlugin* rdpdr, * ignored in client-to-server data." Thus it would be best practice to cease data * transmission. However, simply returning here avoids a crash. */ - return ERROR_SUCCESS; + return CHANNEL_RC_OK; } if (dataFlags & CHANNEL_FLAG_FIRST) @@ -1193,19 +1213,19 @@ static WIN32ERROR rdpdr_virtual_channel_event_connected(rdpdrPlugin* rdpdr, LPVO status = rdpdr->channelEntryPoints.pVirtualChannelOpen(rdpdr->InitHandle, &rdpdr->OpenHandle, rdpdr->channelDef.name, rdpdr_virtual_channel_open_event); - if ((error = rdpdr_add_open_handle_data(rdpdr->OpenHandle, rdpdr))) + if (status != CHANNEL_RC_OK) + { + WLog_ERR(TAG, "pVirtualChannelOpen failed with %s [%08X]", + WTSErrorToString(status), status); + return status; + } + + if ((error = rdpdr_add_open_handle_data(rdpdr->OpenHandle, rdpdr))) { WLog_ERR(TAG, "rdpdr_add_open_handle_data failed with error %lu!", error); return error; } - if (status != CHANNEL_RC_OK) - { - WLog_ERR(TAG, "pVirtualChannelOpen failed with %s [%08X]", - WTSErrorToString(status), status); - return status; - } - rdpdr->queue = MessageQueue_New(NULL); if (!rdpdr->queue) { @@ -1226,8 +1246,12 @@ static WIN32ERROR rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr) { WIN32ERROR error; - if (MessageQueue_PostQuit(rdpdr->queue, 0)) - WaitForSingleObject(rdpdr->thread, INFINITE); + if (MessageQueue_PostQuit(rdpdr->queue, 0) && (WaitForSingleObject(rdpdr->thread, INFINITE) == WAIT_FAILED)) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } MessageQueue_Free(rdpdr->queue); CloseHandle(rdpdr->thread); @@ -1235,7 +1259,11 @@ static WIN32ERROR rdpdr_virtual_channel_event_disconnected(rdpdrPlugin* rdpdr) rdpdr->queue = NULL; rdpdr->thread = NULL; - drive_hotplug_thread_terminate(rdpdr); + if ((error = drive_hotplug_thread_terminate(rdpdr))) + { + WLog_ERR(TAG, "drive_hotplug_thread_terminate failed with error %lu!", error); + return error; + } error = rdpdr->channelEntryPoints.pVirtualChannelClose(rdpdr->OpenHandle); if (CHANNEL_RC_OK != error) diff --git a/channels/rdpdr/server/rdpdr_main.c b/channels/rdpdr/server/rdpdr_main.c index 6bd0e7386..4ac05d35d 100644 --- a/channels/rdpdr/server/rdpdr_main.c +++ b/channels/rdpdr/server/rdpdr_main.c @@ -95,7 +95,7 @@ static WIN32ERROR rdpdr_server_send_announce_request(RdpdrServerContext* context status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written); Stream_Free(s, TRUE); - return status ? ERROR_SUCCESS : ERROR_INTERNAL_ERROR; + return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } static WIN32ERROR rdpdr_server_receive_announce_response(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header) @@ -470,7 +470,7 @@ static WIN32ERROR rdpdr_server_send_core_capability_request(RdpdrServerContext* status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written); Stream_Free(s, TRUE); - return status ? ERROR_SUCCESS : ERROR_INTERNAL_ERROR; + return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } static WIN32ERROR rdpdr_server_receive_core_capability_response(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header) @@ -581,7 +581,7 @@ static WIN32ERROR rdpdr_server_send_client_id_confirm(RdpdrServerContext* contex status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written); Stream_Free(s, TRUE); - return status ? ERROR_SUCCESS : ERROR_INTERNAL_ERROR; + return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } static WIN32ERROR rdpdr_server_receive_device_list_announce_request(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header) @@ -805,7 +805,7 @@ static WIN32ERROR rdpdr_server_send_user_logged_on(RdpdrServerContext* context) status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_Length(s), &written); Stream_Free(s, TRUE); - return status ? ERROR_SUCCESS : ERROR_INTERNAL_ERROR; + return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; } static WIN32ERROR rdpdr_server_receive_pdu(RdpdrServerContext* context, wStream* s, RDPDR_HEADER* header) @@ -973,7 +973,24 @@ static void* rdpdr_server_thread(void* arg) BytesReturned = 0; status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); - if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + goto out_stream; + } + + status = WaitForSingleObject(context->priv->StopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out_stream; + } + + + if (status == WAIT_OBJECT_0) break; if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, @@ -1041,10 +1058,16 @@ static WIN32ERROR rdpdr_server_start(RdpdrServerContext* context) static WIN32ERROR rdpdr_server_stop(RdpdrServerContext* context) { + WIN32ERROR error; if (context->priv->StopEvent) { SetEvent(context->priv->StopEvent); - WaitForSingleObject(context->priv->Thread, INFINITE); + if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(context->priv->Thread); context->priv->Thread = NULL; CloseHandle(context->priv->StopEvent); diff --git a/channels/rdpei/client/rdpei_main.c b/channels/rdpei/client/rdpei_main.c index 15db248c0..435819aeb 100644 --- a/channels/rdpei/client/rdpei_main.c +++ b/channels/rdpei/client/rdpei_main.c @@ -182,6 +182,14 @@ static void* rdpei_schedule_thread(void* arg) while (1) { status = WaitForMultipleObjects(2, hdl, FALSE, 20); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + if (status == WAIT_OBJECT_0 + 1) break; @@ -607,6 +615,7 @@ error_out: static WIN32ERROR rdpei_plugin_terminated(IWTSPlugin* pPlugin) { RDPEI_PLUGIN* rdpei = (RDPEI_PLUGIN*) pPlugin; + WIN32ERROR error; if (!pPlugin) return ERROR_INVALID_PARAMETER; @@ -614,7 +623,12 @@ static WIN32ERROR rdpei_plugin_terminated(IWTSPlugin* pPlugin) SetEvent(rdpei->stopEvent); EnterCriticalSection(&rdpei->lock); - WaitForSingleObject(rdpei->thread, INFINITE); + if (WaitForSingleObject(rdpei->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(rdpei->stopEvent); CloseHandle(rdpei->event); diff --git a/channels/rdpsnd/client/opensles/opensl_io.c b/channels/rdpsnd/client/opensles/opensl_io.c index 32c2ccf27..0a5ba8a06 100644 --- a/channels/rdpsnd/client/opensles/opensl_io.c +++ b/channels/rdpsnd/client/opensles/opensl_io.c @@ -293,8 +293,11 @@ int android_AudioOut(OPENSL_STREAM *p, const short *buffer,int size) assert(size > 0); /* Assure, that the queue is not full. */ - if (p->queuesize <= Queue_Count(p->queue)) - WaitForSingleObject(p->queue->event, INFINITE); + if (p->queuesize <= Queue_Count(p->queue) && WaitForSingleObject(p->queue->event, INFINITE) == WAIT_FAILED) + { + DEBUG_SND("WaitForSingleObject failed!"); + return -1; + } void *data = calloc(size, sizeof(short)); if (!data) diff --git a/channels/rdpsnd/client/rdpsnd_main.c b/channels/rdpsnd/client/rdpsnd_main.c index f94ba6c64..94dec903f 100644 --- a/channels/rdpsnd/client/rdpsnd_main.c +++ b/channels/rdpsnd/client/rdpsnd_main.c @@ -104,13 +104,46 @@ static void* rdpsnd_schedule_thread(void* arg) rdpsndPlugin* rdpsnd = (rdpsndPlugin*) arg; HANDLE events[2]; WIN32ERROR error = CHANNEL_RC_OK; + DWORD status; events[0] = MessageQueue_Event(rdpsnd->MsgPipe->Out); events[1] = rdpsnd->stopEvent; - while (WaitForMultipleObjects(2, events, FALSE, INFINITE) == WAIT_OBJECT_0) + while (1) { - if (!MessageQueue_Peek(rdpsnd->MsgPipe->Out, &message, TRUE)) + status = WaitForMultipleObjects(2, events, FALSE, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + + status = WaitForSingleObject(rdpsnd->stopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (status == WAIT_OBJECT_0) + break; + + + status = WaitForSingleObject(events[0], 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + + if (!MessageQueue_Peek(rdpsnd->MsgPipe->Out, &message, TRUE)) { WLog_ERR(TAG, "MessageQueue_Peek failed!"); error = ERROR_INTERNAL_ERROR; @@ -964,7 +997,11 @@ static void rdpsnd_process_disconnect(rdpsndPlugin* rdpsnd) if (rdpsnd->ScheduleThread) { SetEvent(rdpsnd->stopEvent); - WaitForSingleObject(rdpsnd->ScheduleThread, INFINITE); + if (WaitForSingleObject(rdpsnd->ScheduleThread, INFINITE) == WAIT_FAILED) + { + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", GetLastError()); + return; + } CloseHandle(rdpsnd->ScheduleThread); CloseHandle(rdpsnd->stopEvent); } @@ -1247,7 +1284,12 @@ static WIN32ERROR rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd WIN32ERROR error; MessagePipe_PostQuit(rdpsnd->MsgPipe, 0); - WaitForSingleObject(rdpsnd->thread, INFINITE); + if (WaitForSingleObject(rdpsnd->thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(rdpsnd->thread); rdpsnd->thread = NULL; diff --git a/channels/rdpsnd/server/rdpsnd_main.c b/channels/rdpsnd/server/rdpsnd_main.c index f982c0e99..6c93ecf1b 100644 --- a/channels/rdpsnd/server/rdpsnd_main.c +++ b/channels/rdpsnd/server/rdpsnd_main.c @@ -239,7 +239,24 @@ static void* rdpsnd_server_thread(void* arg) { status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); - if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + + status = WaitForSingleObject(context->priv->StopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + + if (status == WAIT_OBJECT_0) break; if ((error = rdpsnd_server_handle_messages(context))) @@ -598,13 +615,19 @@ out_close: static WIN32ERROR rdpsnd_server_stop(RdpsndServerContext* context) { + WIN32ERROR error = CHANNEL_RC_OK; if (context->priv->ownThread) { if (context->priv->StopEvent) { SetEvent(context->priv->StopEvent); - WaitForSingleObject(context->priv->Thread, INFINITE); + if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(context->priv->Thread); CloseHandle(context->priv->StopEvent); } @@ -613,7 +636,7 @@ static WIN32ERROR rdpsnd_server_stop(RdpsndServerContext* context) if (context->priv->rdpsnd_pdu) Stream_Free(context->priv->rdpsnd_pdu, TRUE); - return CHANNEL_RC_OK; + return error; } RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm) diff --git a/channels/remdesk/client/remdesk_main.c b/channels/remdesk/client/remdesk_main.c index ea63c7a42..82985c35d 100644 --- a/channels/remdesk/client/remdesk_main.c +++ b/channels/remdesk/client/remdesk_main.c @@ -919,8 +919,12 @@ static WIN32ERROR remdesk_virtual_channel_event_disconnected(remdeskPlugin* remd { UINT rc; - if (MessageQueue_PostQuit(remdesk->queue, 0)) - WaitForSingleObject(remdesk->thread, INFINITE); + if (MessageQueue_PostQuit(remdesk->queue, 0) && (WaitForSingleObject(remdesk->thread, INFINITE) == WAIT_FAILED)) + { + rc = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", rc); + return rc; + } MessageQueue_Free(remdesk->queue); CloseHandle(remdesk->thread); diff --git a/channels/remdesk/server/remdesk_main.c b/channels/remdesk/server/remdesk_main.c index 9f85100fa..c932daf19 100644 --- a/channels/remdesk/server/remdesk_main.c +++ b/channels/remdesk/server/remdesk_main.c @@ -575,7 +575,24 @@ static void* remdesk_server_thread(void* arg) { status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE); - if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu", error); + break; + } + + status = WaitForSingleObject(context->priv->StopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error); + break; + } + + + if (status == WAIT_OBJECT_0) { break; } @@ -654,9 +671,16 @@ static WIN32ERROR remdesk_server_start(RemdeskServerContext* context) static WIN32ERROR remdesk_server_stop(RemdeskServerContext* context) { + WIN32ERROR error; + SetEvent(context->priv->StopEvent); - WaitForSingleObject(context->priv->Thread, INFINITE); + if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(context->priv->Thread); return CHANNEL_RC_OK; diff --git a/channels/serial/client/serial_main.c b/channels/serial/client/serial_main.c index cbc6f5f9e..870d007d7 100644 --- a/channels/serial/client/serial_main.c +++ b/channels/serial/client/serial_main.c @@ -524,6 +524,7 @@ static void create_irp_thread(SERIAL_DEVICE *serial, IRP *irp) /* FIXME: not quite sure a zero timeout is a good thing to check whether a thread is stil alived or not */ waitResult = WaitForSingleObject(irpThread, 0); + if (waitResult == WAIT_OBJECT_0) { /* terminating thread */ @@ -673,7 +674,11 @@ static void terminate_pending_irp_threads(SERIAL_DEVICE *serial) TerminateThread(irpThread, 0); - WaitForSingleObject(irpThread, INFINITE); + if (WaitForSingleObject(irpThread, INFINITE) == WAIT_FAILED) + { + WLog_ERR(TAG,"WaitForSingleObject failed!"); + continue; + } CloseHandle(irpThread); @@ -750,14 +755,20 @@ static WIN32ERROR serial_irp_request(DEVICE* device, IRP* irp) } -static void serial_free(DEVICE* device) +static WIN32ERROR serial_free(DEVICE* device) { + WIN32ERROR error; SERIAL_DEVICE* serial = (SERIAL_DEVICE*) device; WLog_Print(serial->log, WLOG_DEBUG, "freeing"); MessageQueue_PostQuit(serial->MainIrpQueue, 0); - WaitForSingleObject(serial->MainThread, INFINITE); + if (WaitForSingleObject(serial->MainThread, INFINITE) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } CloseHandle(serial->MainThread); if (serial->hComm) @@ -770,6 +781,7 @@ static void serial_free(DEVICE* device) DeleteCriticalSection(&serial->TerminatingIrpThreadsLock); free(serial); + return CHANNEL_RC_OK; } #endif /* __linux__ */ diff --git a/channels/smartcard/client/smartcard_main.c b/channels/smartcard/client/smartcard_main.c index b0b565482..4a9c2f63d 100644 --- a/channels/smartcard/client/smartcard_main.c +++ b/channels/smartcard/client/smartcard_main.c @@ -37,6 +37,7 @@ void* smartcard_context_thread(SMARTCARD_CONTEXT* pContext) { DWORD nCount; LONG status; + DWORD waitStatus; HANDLE hEvents[2]; wMessage message; SMARTCARD_DEVICE* smartcard; @@ -50,9 +51,25 @@ void* smartcard_context_thread(SMARTCARD_CONTEXT* pContext) while (1) { - status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE); + waitStatus = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE); - if (WaitForSingleObject(MessageQueue_Event(pContext->IrpQueue), 0) == WAIT_OBJECT_0) + if (waitStatus == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + + waitStatus = WaitForSingleObject(MessageQueue_Event(pContext->IrpQueue), 0); + + if (waitStatus == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (waitStatus == WAIT_OBJECT_0) { if (!MessageQueue_Peek(pContext->IrpQueue, &message, TRUE)) { @@ -151,14 +168,19 @@ void smartcard_context_free(SMARTCARD_CONTEXT* pContext) free(pContext); } -static void smartcard_free(DEVICE* device) +static WIN32ERROR smartcard_free(DEVICE* device) { + WIN32ERROR error; SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) device; if (smartcard->IrpQueue) { - if (MessageQueue_PostQuit(smartcard->IrpQueue, 0)) - WaitForSingleObject(smartcard->thread, INFINITE); + if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) && (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED)) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } MessageQueue_Free(smartcard->IrpQueue); smartcard->IrpQueue = NULL; @@ -184,6 +206,8 @@ static void smartcard_free(DEVICE* device) } free(device); + + return CHANNEL_RC_OK; } /** @@ -455,7 +479,23 @@ static void* smartcard_thread_func(void* arg) { status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE); - if (WaitForSingleObject(MessageQueue_Event(smartcard->IrpQueue), 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + + status = WaitForSingleObject(MessageQueue_Event(smartcard->IrpQueue), 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (status == WAIT_OBJECT_0) { if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE)) { @@ -467,15 +507,35 @@ static void* smartcard_thread_func(void* arg) if (message.id == WMQ_QUIT) { - while (WaitForSingleObject(Queue_Event(smartcard->CompletedIrpQueue), 0) == WAIT_OBJECT_0) + while (1) { - irp = (IRP*) Queue_Dequeue(smartcard->CompletedIrpQueue); + status = WaitForSingleObject(Queue_Event(smartcard->CompletedIrpQueue), 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + + if (status == WAIT_TIMEOUT) + break; + + irp = (IRP*) Queue_Dequeue(smartcard->CompletedIrpQueue); if (irp) { if (irp->thread) { - WaitForSingleObject(irp->thread, INFINITE); + status = WaitForSingleObject(irp->thread, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + CloseHandle(irp->thread); irp->thread = NULL; } @@ -503,15 +563,33 @@ static void* smartcard_thread_func(void* arg) } } - if (WaitForSingleObject(Queue_Event(smartcard->CompletedIrpQueue), 0) == WAIT_OBJECT_0) - { - irp = (IRP*) Queue_Dequeue(smartcard->CompletedIrpQueue); + status = WaitForSingleObject(Queue_Event(smartcard->CompletedIrpQueue), 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (status == WAIT_OBJECT_0) + { + + irp = (IRP*) Queue_Dequeue(smartcard->CompletedIrpQueue); if (irp) { if (irp->thread) { - WaitForSingleObject(irp->thread, INFINITE); + status = WaitForSingleObject(irp->thread, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + CloseHandle(irp->thread); irp->thread = NULL; } diff --git a/channels/tsmf/client/tsmf_ifman.c b/channels/tsmf/client/tsmf_ifman.c index ee6c55bcb..e0fd97a12 100644 --- a/channels/tsmf/client/tsmf_ifman.c +++ b/channels/tsmf/client/tsmf_ifman.c @@ -492,6 +492,7 @@ WIN32ERROR tsmf_ifman_on_sample(TSMF_IFMAN* ifman) UINT64 ThrottleDuration; UINT32 SampleExtensions; UINT32 cbData; + WIN32ERROR error; if (Stream_GetRemainingLength(ifman->input) < 60) return ERROR_INVALID_DATA; @@ -537,7 +538,11 @@ WIN32ERROR tsmf_ifman_on_sample(TSMF_IFMAN* ifman) return ERROR_OUTOFMEMORY; } - tsmf_presentation_sync(presentation); + if ((error = tsmf_presentation_sync(presentation))) + { + WLog_ERR(TAG, "tsmf_presentation_sync failed with error %lu", error); + return error; + } ifman->output_pending = TRUE; return CHANNEL_RC_OK; diff --git a/channels/tsmf/client/tsmf_media.c b/channels/tsmf/client/tsmf_media.c index 3ccfbb426..6d968bb57 100644 --- a/channels/tsmf/client/tsmf_media.c +++ b/channels/tsmf/client/tsmf_media.c @@ -610,6 +610,13 @@ static void* tsmf_stream_ack_func(void *arg) { DWORD ev = WaitForMultipleObjects(2, hdl, FALSE, INFINITE); + if (ev == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + if (ev == WAIT_OBJECT_0) break; @@ -643,6 +650,7 @@ static void* tsmf_stream_playback_func(void *arg) TSMF_STREAM* stream = (TSMF_STREAM *) arg; TSMF_PRESENTATION* presentation = stream->presentation; WIN32ERROR error = CHANNEL_RC_OK; + DWORD status; DEBUG_TSMF("in %d", stream->stream_id); @@ -668,9 +676,30 @@ static void* tsmf_stream_playback_func(void *arg) hdl[0] = stream->stopEvent; hdl[1] = Queue_Event(stream->sample_list); - while (!(WaitForMultipleObjects(2, hdl, FALSE, INFINITE) == WAIT_OBJECT_0)) + while (1) { - sample = tsmf_stream_pop_sample(stream, 0); + status = WaitForMultipleObjects(2, hdl, FALSE, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + break; + } + + status = WaitForSingleObject(stream->stopEvent, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + break; + } + + if (status == WAIT_OBJECT_0) + break; + + sample = tsmf_stream_pop_sample(stream, 0); if (sample && !tsmf_sample_playback(sample)) { @@ -825,10 +854,11 @@ BOOL tsmf_presentation_start(TSMF_PRESENTATION* presentation) return ret; } -void tsmf_presentation_sync(TSMF_PRESENTATION* presentation) +WIN32ERROR tsmf_presentation_sync(TSMF_PRESENTATION* presentation) { UINT32 index; UINT32 count; + WIN32ERROR error; ArrayList_Lock(presentation->stream_list); count = ArrayList_Count(presentation->stream_list); @@ -836,10 +866,16 @@ void tsmf_presentation_sync(TSMF_PRESENTATION* presentation) for (index = 0; index < count; index++) { TSMF_STREAM* stream = (TSMF_STREAM *) ArrayList_GetItem(presentation->stream_list, index); - WaitForSingleObject(stream->ready, 500); + if (WaitForSingleObject(stream->ready, 500) == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return error; + } } ArrayList_Unlock(presentation->stream_list); + return CHANNEL_RC_OK; } BOOL tsmf_presentation_stop(TSMF_PRESENTATION* presentation) @@ -1041,10 +1077,12 @@ TSMF_STREAM* tsmf_stream_new(TSMF_PRESENTATION* presentation, UINT32 stream_id, error_add: SetEvent(stream->stopEvent); - WaitForSingleObject(stream->ack_thread, INFINITE); + if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED) + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", GetLastError()); error_ack_thread: SetEvent(stream->stopEvent); - WaitForSingleObject(stream->play_thread, INFINITE); + if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED) + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", GetLastError()); error_play_thread: Queue_Free(stream->sample_ack_list); error_sample_ack_list: @@ -1160,14 +1198,23 @@ void _tsmf_stream_free(TSMF_STREAM* stream) if (stream->play_thread) { - WaitForSingleObject(stream->play_thread, INFINITE); + if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED) + { + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", GetLastError()); + return; + } + CloseHandle(stream->play_thread); stream->play_thread = NULL; } if (stream->ack_thread) { - WaitForSingleObject(stream->ack_thread, INFINITE); + if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED) + { + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", GetLastError()); + return; + } CloseHandle(stream->ack_thread); stream->ack_thread = NULL; } diff --git a/channels/tsmf/client/tsmf_media.h b/channels/tsmf/client/tsmf_media.h index 4ed51f879..311ed8be4 100644 --- a/channels/tsmf/client/tsmf_media.h +++ b/channels/tsmf/client/tsmf_media.h @@ -40,7 +40,7 @@ TSMF_PRESENTATION *tsmf_presentation_new(const BYTE *guid, IWTSVirtualChannelCal TSMF_PRESENTATION *tsmf_presentation_find_by_id(const BYTE *guid); BOOL tsmf_presentation_start(TSMF_PRESENTATION *presentation); BOOL tsmf_presentation_stop(TSMF_PRESENTATION *presentation); -void tsmf_presentation_sync(TSMF_PRESENTATION *presentation); +WIN32ERROR tsmf_presentation_sync(TSMF_PRESENTATION *presentation); BOOL tsmf_presentation_paused(TSMF_PRESENTATION *presentation); BOOL tsmf_presentation_restarted(TSMF_PRESENTATION *presentation); BOOL tsmf_presentation_volume_changed(TSMF_PRESENTATION *presentation, UINT32 newVolume, UINT32 muted); diff --git a/channels/urbdrc/client/urbdrc_main.c b/channels/urbdrc/client/urbdrc_main.c index 3dd7ed7ae..d4473c725 100644 --- a/channels/urbdrc/client/urbdrc_main.c +++ b/channels/urbdrc/client/urbdrc_main.c @@ -513,6 +513,8 @@ static void *urbdrc_search_usb_device(void *arg) { int busnum, devnum; int action, success, error, found, on_close; struct sockaddr_un sun; + DWORD status; + UINT32 error; WLog_DBG(TAG, "urbdrc_search_usb_device - devd: start"); @@ -540,6 +542,19 @@ static void *urbdrc_search_usb_device(void *arg) { while (WaitForMultipleObjects(2, listobj, FALSE, INFINITE) != WAIT_OBJECT_0) { + + status = WaitForMultipleObjects(2, listobj, FALSE, INFINITE); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + return 0; + } + + if (status == WAIT_OBJECT_0) + break; + WLog_DBG(TAG, "======= SEARCH ======= "); /* !system=USB subsystem=DEVICE type=ATTACH ugen=ugen3.3 cdev=ugen3.3 vendor=0x046d product=0x082d devclass=0xef devsubclass=0x02 sernum="6E7D726F" release=0x0011 mode=host port=4 parent=ugen3.1 */ @@ -735,6 +750,7 @@ static void* urbdrc_search_usb_device(void* arg) int success = 0, error, on_close = 0, found = 0; WLog_VRB(TAG, ""); channel_mgr = urbdrc->listener_callback->channel_mgr; + DWORD status; /* init usb monitor */ struct udev* udev; @@ -774,15 +790,41 @@ static void* urbdrc_search_usb_device(void* arg) listobj[1] = mon_fd; numobj = 2; - WaitForMultipleObjects(numobj, listobj, FALSE, INFINITE); + status = WaitForMultipleObjects(numobj, listobj, FALSE, INFINITE); - if (WaitForSingleObject(searchman->term_event, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + goto out; + } + + status = WaitForSingleObject(searchman->term_event, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + + if (status == WAIT_OBJECT_0) { sem_post(&searchman->sem_term); - return 0; + goto out; } - if (WaitForSingleObject(mon_fd, 0) == WAIT_OBJECT_0) + status = WaitForSingleObject(mon_fd, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + + + if (status == WAIT_OBJECT_0) { dev = udev_monitor_receive_device(mon); @@ -856,9 +898,25 @@ static void* urbdrc_search_usb_device(void* arg) numobj = 1; timeout = 4000; /* milliseconds */ - WaitForMultipleObjects(numobj, listobj, FALSE, timeout); + status = WaitForMultipleObjects(numobj, listobj, FALSE, timeout); - if (WaitForSingleObject(searchman->term_event, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + goto out; + } + + status = WaitForSingleObject(searchman->term_event, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + goto out; + } + + if (status == WAIT_OBJECT_0) { CloseHandle(mon_fd); sem_post(&searchman->sem_term); @@ -912,9 +970,25 @@ static void* urbdrc_search_usb_device(void* arg) numobj = 1; timeout = 3000; /* milliseconds */ - WaitForMultipleObjects(numobj, listobj, FALSE, timeout); + status = WaitForMultipleObjects(numobj, listobj, FALSE, timeout); - if (WaitForSingleObject(searchman->term_event, 0) == WAIT_OBJECT_0) + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForMultipleObjects failed with error %lu!", error); + goto out; + } + + status = WaitForSingleObject(searchman->term_event, 0); + + if (status == WAIT_FAILED) + { + error = GetLastError(); + WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error); + goto out; + } + + if (status == WAIT_OBJECT_0) { CloseHandle(mon_fd); sem_post(&searchman->sem_term); @@ -936,7 +1010,7 @@ static void* urbdrc_search_usb_device(void* arg) } } } - +out: CloseHandle(mon_fd); fail_create_monfd_event: diff --git a/include/freerdp/channels/rdpdr.h b/include/freerdp/channels/rdpdr.h index cc3c4127d..d259cd6b4 100644 --- a/include/freerdp/channels/rdpdr.h +++ b/include/freerdp/channels/rdpdr.h @@ -319,7 +319,7 @@ typedef struct _DEVMAN DEVMAN; typedef WIN32ERROR (*pcIRPRequest)(DEVICE* device, IRP* irp); typedef WIN32ERROR (*pcInitDevice)(DEVICE* device); -typedef void (*pcFreeDevice)(DEVICE* device); +typedef WIN32ERROR (*pcFreeDevice)(DEVICE* device); struct _DEVICE {