sr_driver_list() now takes a context pointer.

This requires sr_hw_cleanup_all() and sanity_check_all_drivers()
to also take a context.

The (runtime) generation of the driver list now happens in sr_init()
and sr_driver_list() always returns that pre-generated list. This fixes
a segfault when (correctly) invoking multiple sr_init() and sr_exit()
calls with different contexts (caught by the unit tests).

This fixes bug #565.
This commit is contained in:
Uwe Hermann 2015-04-04 20:57:22 +02:00
parent 07962655ec
commit 032da34b78
7 changed files with 54 additions and 37 deletions

View File

@ -77,7 +77,7 @@ Context::Context() :
{ {
check(sr_init(&_structure)); check(sr_init(&_structure));
struct sr_dev_driver **driver_list = sr_driver_list(); struct sr_dev_driver **driver_list = sr_driver_list(_structure);
if (driver_list) if (driver_list)
for (int i = 0; driver_list[i]; i++) for (int i = 0; driver_list[i]; i++)
_drivers[driver_list[i]->name] = _drivers[driver_list[i]->name] =

View File

@ -79,7 +79,7 @@ SR_API int sr_dev_inst_channel_add(struct sr_dev_inst *sdi, int index, int type,
/*--- hwdriver.c ------------------------------------------------------------*/ /*--- hwdriver.c ------------------------------------------------------------*/
SR_API struct sr_dev_driver **sr_driver_list(void); SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx);
SR_API int sr_driver_init(struct sr_context *ctx, SR_API int sr_driver_init(struct sr_context *ctx,
struct sr_dev_driver *driver); struct sr_dev_driver *driver);
SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options); SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options);

View File

@ -122,18 +122,24 @@
/** /**
* Sanity-check all libsigrok drivers. * Sanity-check all libsigrok drivers.
* *
* @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
*
* @retval SR_OK All drivers are OK * @retval SR_OK All drivers are OK
* @retval SR_ERR One or more drivers have issues. * @retval SR_ERR One or more drivers have issues.
* @retval SR_ERR_ARG Invalid argument.
*/ */
static int sanity_check_all_drivers(void) static int sanity_check_all_drivers(const struct sr_context *ctx)
{ {
int i, errors, ret = SR_OK; int i, errors, ret = SR_OK;
struct sr_dev_driver **drivers; struct sr_dev_driver **drivers;
const char *d; const char *d;
if (!ctx)
return SR_ERR_ARG;
sr_spew("Sanity-checking all drivers."); sr_spew("Sanity-checking all drivers.");
drivers = sr_driver_list(); drivers = sr_driver_list(ctx);
for (i = 0; drivers[i]; i++) { for (i = 0; drivers[i]; i++) {
errors = 0; errors = 0;
@ -375,13 +381,25 @@ SR_API int sr_init(struct sr_context **ctx)
{ {
int ret = SR_ERR; int ret = SR_ERR;
struct sr_context *context; struct sr_context *context;
struct sr_dev_driver ***lists, **drivers;
GArray *array;
if (!ctx) { if (!ctx) {
sr_err("%s(): libsigrok context was NULL.", __func__); sr_err("%s(): libsigrok context was NULL.", __func__);
return SR_ERR; return SR_ERR;
} }
if (sanity_check_all_drivers() < 0) { context = g_malloc0(sizeof(struct sr_context));
/* Generate ctx->driver_list at runtime. */
array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
for (lists = drivers_lists; *lists; lists++)
for (drivers = *lists; *drivers; drivers++)
g_array_append_val(array, *drivers);
context->driver_list = (struct sr_dev_driver **)array->data;
g_array_free(array, FALSE);
if (sanity_check_all_drivers(context) < 0) {
sr_err("Internal driver error(s), aborting."); sr_err("Internal driver error(s), aborting.");
return ret; return ret;
} }
@ -401,9 +419,6 @@ SR_API int sr_init(struct sr_context **ctx)
return ret; return ret;
} }
/* + 1 to handle when struct sr_context has no members. */
context = g_malloc0(sizeof(struct sr_context) + 1);
#ifdef HAVE_LIBUSB_1_0 #ifdef HAVE_LIBUSB_1_0
ret = libusb_init(&context->libusb_ctx); ret = libusb_init(&context->libusb_ctx);
if (LIBUSB_SUCCESS != ret) { if (LIBUSB_SUCCESS != ret) {
@ -439,13 +454,13 @@ SR_API int sr_exit(struct sr_context *ctx)
return SR_ERR; return SR_ERR;
} }
sr_hw_cleanup_all(); sr_hw_cleanup_all(ctx);
#ifdef HAVE_LIBUSB_1_0 #ifdef HAVE_LIBUSB_1_0
libusb_exit(ctx->libusb_ctx); libusb_exit(ctx->libusb_ctx);
#endif #endif
g_free(sr_driver_list()); g_free(sr_driver_list(ctx));
g_free(ctx); g_free(ctx);
return SR_OK; return SR_OK;

View File

@ -31,8 +31,6 @@
#define LOG_PREFIX "hwdriver" #define LOG_PREFIX "hwdriver"
/** @endcond */ /** @endcond */
extern SR_PRIV struct sr_dev_driver **drivers_lists[];
/** /**
* @file * @file
* *
@ -259,27 +257,20 @@ SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *value)
/** /**
* Return the list of supported hardware drivers. * Return the list of supported hardware drivers.
* *
* @return Pointer to the NULL-terminated list of hardware driver pointers. * @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
* *
* @since 0.1.0 * @retval NULL The ctx argument was NULL, or there are no supported drivers.
* @retval Other Pointer to the NULL-terminated list of hardware drivers.
* The user should NOT g_free() this list, sr_exit() will do that.
*
* @since 0.4.0
*/ */
SR_API struct sr_dev_driver **sr_driver_list(void) SR_API struct sr_dev_driver **sr_driver_list(const struct sr_context *ctx)
{ {
static struct sr_dev_driver **combined_list = NULL; if (!ctx)
struct sr_dev_driver ***lists, **drivers; return NULL;
GArray *array;
if (combined_list) return ctx->driver_list;
return combined_list;
array = g_array_new(TRUE, FALSE, sizeof(struct sr_dev_driver *));
for (lists = drivers_lists; *lists; lists++)
for (drivers = *lists; *drivers; drivers++)
g_array_append_val(array, *drivers);
combined_list = (struct sr_dev_driver **)array->data;
g_array_free(array, FALSE);
return combined_list;
} }
/** /**
@ -418,14 +409,22 @@ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
return l; return l;
} }
/** Call driver cleanup function for all drivers. /**
* @private */ * Call driver cleanup function for all drivers.
SR_PRIV void sr_hw_cleanup_all(void) *
* @param[in] ctx Pointer to a libsigrok context struct. Must not be NULL.
*
* @private
*/
SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx)
{ {
int i; int i;
struct sr_dev_driver **drivers; struct sr_dev_driver **drivers;
drivers = sr_driver_list(); if (!ctx)
return;
drivers = sr_driver_list(ctx);
for (i = 0; drivers[i]; i++) { for (i = 0; drivers[i]; i++) {
if (drivers[i]->cleanup) if (drivers[i]->cleanup)
drivers[i]->cleanup(drivers[i]); drivers[i]->cleanup(drivers[i]);

View File

@ -174,6 +174,7 @@
#endif #endif
struct sr_context { struct sr_context {
struct sr_dev_driver **driver_list;
#ifdef HAVE_LIBUSB_1_0 #ifdef HAVE_LIBUSB_1_0
libusb_context *libusb_ctx; libusb_context *libusb_ctx;
gboolean usb_source_present; gboolean usb_source_present;
@ -645,9 +646,11 @@ SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc);
/*--- hwdriver.c ------------------------------------------------------------*/ /*--- hwdriver.c ------------------------------------------------------------*/
extern SR_PRIV struct sr_dev_driver **drivers_lists[];
SR_PRIV const GVariantType *sr_variant_type_get(int datatype); SR_PRIV const GVariantType *sr_variant_type_get(int datatype);
SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data); SR_PRIV int sr_variant_type_check(uint32_t key, GVariant *data);
SR_PRIV void sr_hw_cleanup_all(void); SR_PRIV void sr_hw_cleanup_all(const struct sr_context *ctx);
SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data); SR_PRIV struct sr_config *sr_config_new(uint32_t key, GVariant *data);
SR_PRIV void sr_config_free(struct sr_config *src); SR_PRIV void sr_config_free(struct sr_config *src);
SR_PRIV int sr_source_remove(int fd); SR_PRIV int sr_source_remove(int fd);

View File

@ -28,7 +28,7 @@ START_TEST(test_driver_available)
{ {
struct sr_dev_driver **drivers; struct sr_dev_driver **drivers;
drivers = sr_driver_list(); drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found."); fail_unless(drivers != NULL, "No drivers found.");
} }
END_TEST END_TEST

View File

@ -50,7 +50,7 @@ struct sr_dev_driver *srtest_driver_get(const char *drivername)
struct sr_dev_driver **drivers, *driver = NULL; struct sr_dev_driver **drivers, *driver = NULL;
int i; int i;
drivers = sr_driver_list(); drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found."); fail_unless(drivers != NULL, "No drivers found.");
for (i = 0; drivers[i]; i++) { for (i = 0; drivers[i]; i++) {
@ -79,7 +79,7 @@ void srtest_driver_init_all(struct sr_context *sr_ctx)
struct sr_dev_driver **drivers, *driver; struct sr_dev_driver **drivers, *driver;
int i, ret; int i, ret;
drivers = sr_driver_list(); drivers = sr_driver_list(srtest_ctx);
fail_unless(drivers != NULL, "No drivers found."); fail_unless(drivers != NULL, "No drivers found.");
for (i = 0; drivers[i]; i++) { for (i = 0; drivers[i]; i++) {