Skip to content

Commit

Permalink
check pthread_mutex_lock return code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Nov 8, 2023
1 parent 7eda07b commit a3238fc
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 51 deletions.
20 changes: 17 additions & 3 deletions src/inotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "buildconf.h"

#include <inttypes.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <sys/inotify.h>
Expand Down Expand Up @@ -142,7 +143,12 @@ void* inotify_thread(void* arg)
entry = NULL;
do
{
pthread_mutex_lock( &ctx->inotify_mutex );
if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
return NULL;
}

entry = get_entry_by_wd( ctx->fs_db, event->wd, entry );
if ( get_file_info( ctx, event, entry, &fileinfo, 0 ) )
{
Expand Down Expand Up @@ -189,7 +195,11 @@ void* inotify_thread(void* arg)

do
{
pthread_mutex_lock( &ctx->inotify_mutex );
if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
return NULL;
}

entry = get_entry_by_wd( ctx->fs_db, event->wd, entry );
if ( get_file_info( ctx, event, entry, &fileinfo, 1 ) )
Expand Down Expand Up @@ -224,7 +234,11 @@ void* inotify_thread(void* arg)

do
{
pthread_mutex_lock( &ctx->inotify_mutex );
if ( pthread_mutex_lock( &ctx->inotify_mutex ) )
{
PRINT_ERROR( "inotify_thread - pthread_mutex_lock failure !");
return NULL;
}

entry = get_entry_by_wd( ctx->fs_db, event->wd, entry );
if ( get_file_info( ctx, event, entry, &fileinfo, 1 ) )
Expand Down
56 changes: 30 additions & 26 deletions src/msgqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ void* msgqueue_thread( void* arg )

if(store_index >= 0)
{
pthread_mutex_lock( &ctx->inotify_mutex );

mount_store( ctx, store_index, 1 );
if( !pthread_mutex_lock( &ctx->inotify_mutex ) )
{
mount_store( ctx, store_index, 1 );

handle[0] = ctx->storages[store_index].storage_id;
mtp_push_event( ctx, MTP_EVENT_STORE_ADDED, 1, (uint32_t *)&handle );
handle[0] = ctx->storages[store_index].storage_id;
mtp_push_event( ctx, MTP_EVENT_STORE_ADDED, 1, (uint32_t *)&handle );

pthread_mutex_unlock( &ctx->inotify_mutex );
pthread_mutex_unlock( &ctx->inotify_mutex );
}
}
else
{
Expand All @@ -126,15 +127,16 @@ void* msgqueue_thread( void* arg )
store_index = mtp_get_storage_index_by_name(ctx, (char*)&msg_buf.mesg_text + 8);
if(store_index >= 0)
{
pthread_mutex_lock( &ctx->inotify_mutex );

umount_store( ctx, store_index, 1 );
if( !pthread_mutex_lock( &ctx->inotify_mutex ) )
{
umount_store( ctx, store_index, 1 );

handle[0] = ctx->storages[store_index].storage_id;
handle[0] = ctx->storages[store_index].storage_id;

mtp_push_event( ctx, MTP_EVENT_STORE_REMOVED, 1, (uint32_t *)&handle );
mtp_push_event( ctx, MTP_EVENT_STORE_REMOVED, 1, (uint32_t *)&handle );

pthread_mutex_unlock( &ctx->inotify_mutex );
pthread_mutex_unlock( &ctx->inotify_mutex );
}
}
else
{
Expand All @@ -152,17 +154,18 @@ void* msgqueue_thread( void* arg )
!(ctx->storages[store_index].flags & UMTP_STORAGE_LOCKED)
)
{
pthread_mutex_lock( &ctx->inotify_mutex );
if( !pthread_mutex_lock( &ctx->inotify_mutex ) )
{
umount_store( ctx, store_index, 0 );

umount_store( ctx, store_index, 0 );
ctx->storages[store_index].flags |= UMTP_STORAGE_LOCKED;

ctx->storages[store_index].flags |= UMTP_STORAGE_LOCKED;
handle[0] = ctx->storages[store_index].storage_id;

handle[0] = ctx->storages[store_index].storage_id;

mtp_push_event( ctx, MTP_EVENT_STORE_REMOVED, 1, (uint32_t *)&handle );
mtp_push_event( ctx, MTP_EVENT_STORE_REMOVED, 1, (uint32_t *)&handle );

pthread_mutex_unlock( &ctx->inotify_mutex );
pthread_mutex_unlock( &ctx->inotify_mutex );
}
}

store_index++;
Expand All @@ -179,17 +182,18 @@ void* msgqueue_thread( void* arg )
(ctx->storages[store_index].flags & UMTP_STORAGE_LOCKED)
)
{
pthread_mutex_lock( &ctx->inotify_mutex );
if( !pthread_mutex_lock( &ctx->inotify_mutex ) )
{
mount_store( ctx, store_index, 0 );

mount_store( ctx, store_index, 0 );
ctx->storages[store_index].flags &= ~UMTP_STORAGE_LOCKED;

ctx->storages[store_index].flags &= ~UMTP_STORAGE_LOCKED;
handle[0] = ctx->storages[store_index].storage_id;

handle[0] = ctx->storages[store_index].storage_id;
mtp_push_event( ctx, MTP_EVENT_STORE_ADDED, 1, (uint32_t *)&handle );

mtp_push_event( ctx, MTP_EVENT_STORE_ADDED, 1, (uint32_t *)&handle );

pthread_mutex_unlock( &ctx->inotify_mutex );
pthread_mutex_unlock( &ctx->inotify_mutex );
}
}

store_index++;
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_begineditobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ uint32_t mtp_op_BeginEditObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

response_code = MTP_RESPONSE_OK;

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_deleteobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ uint32_t mtp_op_DeleteObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, i
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - object handle

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_endeditobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ uint32_t mtp_op_EndEditObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr,
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

response_code = MTP_RESPONSE_OK;

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getdevicepropdesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ uint32_t mtp_op_GetDevicePropDesc(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_h
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
goto error;

property_id = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - property id

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getdevicepropvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ uint32_t mtp_op_GetDevicePropValue(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

prop_code = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - PropCode

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ uint32_t mtp_op_GetObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, int
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - object handle
entry = get_entry_by_handle(ctx->fs_db, handle);
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjecthandles.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ uint32_t mtp_op_GetObjectHandles(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hd
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

storageid = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER) + 0, 4); // Get param 1 - Storage ID

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjectinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ uint32_t mtp_op_GetObjectInfo(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr,
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - object handle
entry = get_entry_by_handle(ctx->fs_db, handle);
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjectpropdesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ uint32_t mtp_op_GetObjectPropDesc(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_h
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

property_id = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - property id
format_id = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER) + 4, 4); // Get param 2 - format
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjectproplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ uint32_t mtp_op_GetObjectPropList(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_h
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4);
format_id = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER) + 4, 4);
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjectpropvalue.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ uint32_t mtp_op_GetObjectPropValue(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - object handle
prop_code = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER) + 4, 4); // Get param 2 - PropCode
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getobjectreferences.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ uint32_t mtp_op_GetObjectReferences(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4);

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_getpartialobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ uint32_t mtp_op_GetPartialObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hd
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - Object handle

Expand Down
5 changes: 4 additions & 1 deletion src/mtp_operations/mtp_op_opensession.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ uint32_t mtp_op_OpenSession(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, in
i = 0;
while( (i < MAX_STORAGE_NB) && ctx->storages[i].root_path)
{
pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

alloc_root_entry(ctx->fs_db, ctx->storages[i].storage_id);

pthread_mutex_unlock( &ctx->inotify_mutex );

i++;
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_sendobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ uint32_t mtp_op_SendObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr, int
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

response_code = MTP_RESPONSE_GENERAL_ERROR;

Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_sendobjectinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ uint32_t mtp_op_SendObjectInfo(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr,
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

storageid = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER), 4); // Get param 1 - storage id
parent_handle = peek(mtp_packet_hdr, sizeof(MTP_PACKET_HEADER) + 4, 4); // Get param 2 - parent handle
Expand Down
3 changes: 2 additions & 1 deletion src/mtp_operations/mtp_op_truncateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ uint32_t mtp_op_TruncateObject(mtp_ctx * ctx,MTP_PACKET_HEADER * mtp_packet_hdr,
if(!ctx->fs_db)
return MTP_RESPONSE_SESSION_NOT_OPEN;

pthread_mutex_lock( &ctx->inotify_mutex );
if( pthread_mutex_lock( &ctx->inotify_mutex ) )
return MTP_RESPONSE_GENERAL_ERROR;

response_code = MTP_RESPONSE_GENERAL_ERROR;

Expand Down
15 changes: 10 additions & 5 deletions src/usb_gadget.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,16 @@ int handle_ffs_ep0(usb_gadget * ctx)
ctx->stop = 0;

// Drop the file system db
pthread_mutex_lock( &mtp_context->inotify_mutex );
deinit_fs_db(mtp_context->fs_db);
mtp_context->fs_db = 0;
pthread_mutex_unlock( &mtp_context->inotify_mutex );

if ( !pthread_mutex_lock( &mtp_context->inotify_mutex ) )
{
deinit_fs_db(mtp_context->fs_db);
mtp_context->fs_db = 0;
pthread_mutex_unlock( &mtp_context->inotify_mutex );
}
else
{
PRINT_ERROR("handle_ffs_ep0 : Mutex error !");
}
break;
case FUNCTIONFS_SETUP:
PRINT_DEBUG("EP0 FFS SETUP");
Expand Down

0 comments on commit a3238fc

Please sign in to comment.