diff --git a/src/modules/systemlib/flashparams/flashparams.c b/src/modules/systemlib/flashparams/flashparams.c index da1e954ed6..f74d2adb36 100644 --- a/src/modules/systemlib/flashparams/flashparams.c +++ b/src/modules/systemlib/flashparams/flashparams.c @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -158,6 +159,12 @@ out: size_t buf_size = bson_encoder_buf_size(&encoder); + int shutdown_lock_ret = px4_shutdown_lock(); + + if (shutdown_lock_ret) { + PX4_ERR("px4_shutdown_lock() failed (%i)", shutdown_lock_ret); + } + /* Get a buffer from the flash driver with enough space */ uint8_t *buffer; @@ -186,6 +193,11 @@ out: free(enc_buff); parameter_flashfs_free(); } + + if (shutdown_lock_ret == 0) { + px4_shutdown_unlock(); + } + } return result; diff --git a/src/modules/systemlib/param/param.c b/src/modules/systemlib/param/param.c index 428b1866ba..cb0d7c1241 100644 --- a/src/modules/systemlib/param/param.c +++ b/src/modules/systemlib/param/param.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -1003,6 +1004,12 @@ param_export(int fd, bool only_unsaved) struct bson_encoder_s encoder; int result = -1; + int shutdown_lock_ret = px4_shutdown_lock(); + + if (shutdown_lock_ret) { + PX4_ERR("px4_shutdown_lock() failed (%i)", shutdown_lock_ret); + } + param_lock_writer(); bson_encoder_init_file(&encoder, fd); @@ -1090,6 +1097,12 @@ param_export(int fd, bool only_unsaved) out: param_unlock_writer(); + fsync(fd); // make sure the data is flushed before releasing the shutdown lock + + if (shutdown_lock_ret == 0) { + px4_shutdown_unlock(); + } + if (result == 0) { result = bson_encoder_fini(&encoder); } diff --git a/src/modules/systemlib/param/param_shmem.c b/src/modules/systemlib/param/param_shmem.c index d24a04a2e9..71e5ae176a 100644 --- a/src/modules/systemlib/param/param_shmem.c +++ b/src/modules/systemlib/param/param_shmem.c @@ -44,6 +44,7 @@ //#include #include #include +#include #include #include #include @@ -945,14 +946,6 @@ param_save_default(void) goto exit; } - // After writing the file, also do a fsync to prevent loosing params if power is cut. - res = fsync(fd); - - if (res != 0) { - PX4_ERR("failed to do fsync: %s", strerror(errno)); - goto exit; - } - PARAM_CLOSE(fd); @@ -1044,6 +1037,12 @@ param_export(int fd, bool only_unsaved) struct bson_encoder_s encoder; int result = -1; + int shutdown_lock_ret = px4_shutdown_lock(); + + if (shutdown_lock_ret) { + PX4_ERR("px4_shutdown_lock() failed (%i)", shutdown_lock_ret); + } + param_lock(); bson_encoder_init_file(&encoder, fd); @@ -1123,6 +1122,12 @@ param_export(int fd, bool only_unsaved) out: param_unlock(); + fsync(fd); // make sure the data is flushed before releasing the shutdown lock + + if (shutdown_lock_ret == 0) { + px4_shutdown_unlock(); + } + if (result == 0) { result = bson_encoder_fini(&encoder); }