Skip to content

Commit

Permalink
Push the event to the usb outside the critical section to avoid to lo…
Browse files Browse the repository at this point in the history
…ck all the mtp communication if the event endpoint doesn't work properly (deadlock issue).
  • Loading branch information
jfdelnero committed Nov 10, 2023
1 parent f660638 commit 7e479ef
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void *inotify_gotsig(int sig, siginfo_t *info, void *ucontext)
void* inotify_thread(void* arg)
{
mtp_ctx * ctx;
int i,length;
int i, length;
int send_event_flag;
fs_entry * entry;
fs_entry * deleted_entry;
fs_entry * modified_entry;
Expand Down Expand Up @@ -144,6 +145,8 @@ void* inotify_thread(void* arg)
entry = NULL;
do
{
send_event_flag = 0;

if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
Expand All @@ -162,7 +165,7 @@ void* inotify_thread(void* arg)
{
// Send an "ObjectAdded" (0x4002) MTP event message with the entry handle.
handle[0] = new_entry->handle;
mtp_push_event( ctx, MTP_EVENT_OBJECT_ADDED, 1, (uint32_t *)&handle );
send_event_flag = 1;

PRINT_DEBUG( "inotify_thread (IN_CREATE): Entry %s created (Handle 0x%.8X)", event->name, new_entry->handle );
}
Expand Down Expand Up @@ -192,6 +195,11 @@ void* inotify_thread(void* arg)
return NULL;
}

if( send_event_flag )
{
mtp_push_event( ctx, MTP_EVENT_OBJECT_ADDED, 1, (uint32_t *)&handle );
}

}while(entry);
}

Expand All @@ -201,6 +209,8 @@ void* inotify_thread(void* arg)

do
{
send_event_flag = 0;

if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
Expand All @@ -215,7 +225,7 @@ void* inotify_thread(void* arg)
{
// Send an "ObjectInfoChanged" (0x4007) MTP event message with the entry handle.
handle[0] = modified_entry->handle;
mtp_push_event( ctx, MTP_EVENT_OBJECT_INFO_CHANGED, 1, (uint32_t *)&handle );
send_event_flag = 1;

PRINT_DEBUG( "inotify_thread (IN_MODIFY): Entry %s modified (Handle 0x%.8X)", event->name, modified_entry->handle);
}
Expand All @@ -236,6 +246,11 @@ void* inotify_thread(void* arg)
return NULL;
}

if( send_event_flag )
{
mtp_push_event( ctx, MTP_EVENT_OBJECT_INFO_CHANGED, 1, (uint32_t *)&handle );
}

}while(entry);
}

Expand All @@ -245,6 +260,8 @@ void* inotify_thread(void* arg)

do
{
send_event_flag = 0;

if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
Expand All @@ -266,7 +283,7 @@ void* inotify_thread(void* arg)

// Send an "ObjectRemoved" (0x4003) MTP event message with the entry handle.
handle[0] = deleted_entry->handle;
mtp_push_event( ctx, MTP_EVENT_OBJECT_REMOVED, 1, (uint32_t *)&handle );
send_event_flag = 1;

PRINT_DEBUG( "inotify_thread (IN_DELETE): Entry %s deleted (Handle 0x%.8X)", event->name, deleted_entry->handle);
}
Expand All @@ -287,6 +304,10 @@ void* inotify_thread(void* arg)
return NULL;
}

if( send_event_flag )
{
mtp_push_event( ctx, MTP_EVENT_OBJECT_REMOVED, 1, (uint32_t *)&handle );
}
}while(entry);
}
}
Expand Down

0 comments on commit 7e479ef

Please sign in to comment.