Skip to content

Commit

Permalink
Deterministically wait on hotplug_libusb shutdown
Browse files Browse the repository at this point in the history
Change HPStopHotPluggables() to actually wait until the background
hotplug thread exits.

This makes sure that the hotplug mechanism doesn't continue working
throughout the daemon's shutdown process, and hence use-after-frees
(if a reader is added/removed after the readerfactory is shut down),
memory leaks etc. are prevented.

Note: this is expected to make the "SYS_Sleep(1)" trick in pcscdaemon.c
unnecessary, replacing it with a more reliable alternative. We don't
delete the sleep in the same commit in case it turns out to be crucial
for some other reason.
  • Loading branch information
emaxx-google committed Apr 12, 2024
1 parent e50c01e commit cc9edab
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/hotplug_libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ LONG HPSearchHotPluggables(const char * hpDirPath)
return -1;
}

ThreadCreate(&usbNotifyThread, THREAD_ATTR_DETACHED,
ThreadCreate(&usbNotifyThread, 0,
(PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, pipefd);

/* Wait for initial readers to setup */
Expand All @@ -596,12 +596,16 @@ LONG HPSearchHotPluggables(const char * hpDirPath)

LONG HPStopHotPluggables(void)
{
/* tell the rescan thread to shut down; it checks the ara kiri flag, but it
* might also need to be awaken from reading the rescan pipe */
AraKiriHotPlug = true;
if (rescan_pipe[1] >= 0)
{
close(rescan_pipe[1]);
rescan_pipe[1] = -1;
}
/* wait for the rescan thread to complete its cleanup */
pthread_join(usbNotifyThread, NULL);

return 0;
}
Expand Down

0 comments on commit cc9edab

Please sign in to comment.