Skip to contentSkip to conversation

gpiosim: remove randomness from configfs name generation

PATCH New Bartosz Golaszewski 1 participant 1 message Apr 6, 2022 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 gpiosim: remove randomness from configfs name generation

None of the test suites use the random name generation at this point so it's very much dead code and also every test-suite implements its own deterministic way of assigning file names to configfs attributes while also doing it wrong (as is evident as soon as we try to remove one simulator but not all of them and then create another one).

This removes the 'name' argument from gpiosim_dev_new() and gpiosim_bank_new() and makes the generation of the file names for configfs attributes deterministic at the libgpiosim level. The names are created from the program invocation name, the PID and an ID allocated using a binary search tree in a similar manner to kernel's IDA API (find the lowest available integer for a new name).

The ID pool is global within the library code and as such has protection using a mutex (unlike the context object and its children which are expected to be protected by the user if needed).

---
 tests/gpiod-test-sim.c           |   9 +-
 tests/gpiosim/Makefile.am        |   2 +-
 tests/gpiosim/gpiosim-selftest.c |  18 +--
 tests/gpiosim/gpiosim.c          | 222 +++++++++++++++++++++++--------
 tests/gpiosim/gpiosim.h          |   4 +-
 5 files changed, 180 insertions(+), 75 deletions(-)
tests/gpiod-test-sim.c +2 -7
index fd4b8cc..fe5db38 100644
@@ -23,8 +23,6 @@ enum {
 };
 
 static struct gpiosim_ctx *sim_ctx;
-static pid_t pid;
-static guint sim_id;
 
 G_DEFINE_TYPE(GPIOSimChip, g_gpiosim_chip, G_TYPE_OBJECT);
 
@@ -41,7 +39,6 @@ static void g_gpiosim_ctx_init(void)
 			g_strerror(errno));
 
 	atexit(g_gpiosim_ctx_unref);
-	pid = getpid();
 }
 
 static void g_gpiosim_chip_constructed(GObject *obj)
@@ -175,7 +172,6 @@ static void g_gpiosim_chip_dispose(GObject *obj)
 	}
 
 	gpiosim_dev_unref(dev);
-	sim_id--;
 }
 
 static void g_gpiosim_chip_finalize(GObject *obj)
@@ -239,13 +235,12 @@ static void g_gpiosim_chip_init(GPIOSimChip *self)
 	if (!sim_ctx)
 		g_gpiosim_ctx_init();
 
-	dev_name = g_strdup_printf("gpiod-test-dev.%u.%u", pid, sim_id++);
-	dev = gpiosim_dev_new(sim_ctx, dev_name);
+	dev = gpiosim_dev_new(sim_ctx);
 	if (!dev)
 		g_error("Unable to instantiate new GPIO device: %s",
 			g_strerror(errno));
 
-	self->bank = gpiosim_bank_new(dev, "bank");
+	self->bank = gpiosim_bank_new(dev);
 	gpiosim_dev_unref(dev);
 	if (!self->bank)
 		g_error("Unable to instantiate new GPIO bank: %s",
tests/gpiosim/Makefile.am +1 -1
index ab5838a..05dce79 100644
@@ -10,7 +10,7 @@ AM_CFLAGS += -include $(top_builddir)/config.h
 libgpiosim_la_SOURCES = gpiosim.c gpiosim.h
 libgpiosim_la_CFLAGS = $(AM_CFLAGS) $(KMOD_CFLAGS) $(MOUNT_CFLAGS)
 libgpiosim_la_LDFLAGS = -version-info $(subst .,:,$(ABI_GPIOSIM_VERSION))
-libgpiosim_la_LDFLAGS += $(KMOD_LIBS) $(MOUNT_LIBS)
+libgpiosim_la_LDFLAGS += $(KMOD_LIBS) $(MOUNT_LIBS) -pthread
 
 gpiosim_selftest_SOURCES = gpiosim-selftest.c
 gpiosim_selftest_LDADD = libgpiosim.la
tests/gpiosim/gpiosim-selftest.c +9 -9
index f2d0b35..b970755 100644
@@ -31,27 +31,27 @@ int main(int argc UNUSED, char **argv UNUSED)
 		return EXIT_FAILURE;
 	}
 
-	printf("Creating a chip with random name\n");
+	printf("Creating a chip\n");
 
-	dev = gpiosim_dev_new(ctx, NULL);
+	dev = gpiosim_dev_new(ctx);
 	if (!dev) {
-		perror("Unable to create a chip with random name");
+		perror("Unable to create a chip");
 		return EXIT_FAILURE;
 	}
 
-	printf("Creating a bank with a random name\n");
+	printf("Creating a bank\n");
 
-	bank0 = gpiosim_bank_new(dev, NULL);
+	bank0 = gpiosim_bank_new(dev);
 	if (!bank0) {
-		perror("Unable to create a bank with random name");
+		perror("Unable to create a bank");
 		return EXIT_FAILURE;
 	}
 
-	printf("Creating a bank with a specific name\n");
+	printf("Creating a second bank\n");
 
-	bank1 = gpiosim_bank_new(dev, "foobar");
+	bank1 = gpiosim_bank_new(dev);
 	if (!bank1) {
-		perror("Unable to create a bank with a specific name");
+		perror("Unable to create a bank");
 		return EXIT_FAILURE;
 	}
 
tests/gpiosim/gpiosim.c +166 -56

323 lines not on this page · raw message

tests/gpiosim/gpiosim.h +2 -2
index 32f81bc..8013595 100644
@@ -36,7 +36,7 @@ struct gpiosim_ctx *gpiosim_ctx_ref(struct gpiosim_ctx *ctx);
 void gpiosim_ctx_unref(struct gpiosim_ctx *ctx);
 
 struct gpiosim_dev *
-gpiosim_dev_new(struct gpiosim_ctx *ctx, const char *name);
+gpiosim_dev_new(struct gpiosim_ctx *ctx);
 struct gpiosim_dev *gpiosim_dev_ref(struct gpiosim_dev *dev);
 void gpiosim_dev_unref(struct gpiosim_dev *dev);
 struct gpiosim_ctx *gpiosim_dev_get_ctx(struct gpiosim_dev *dev);
@@ -47,7 +47,7 @@ int gpiosim_dev_disable(struct gpiosim_dev *dev);
 bool gpiosim_dev_is_live(struct gpiosim_dev *dev);
 
 struct gpiosim_bank*
-gpiosim_bank_new(struct gpiosim_dev *dev, const char *name);
+gpiosim_bank_new(struct gpiosim_dev *dev);
 struct gpiosim_bank *gpiosim_bank_ref(struct gpiosim_bank *bank);
 void gpiosim_bank_unref(struct gpiosim_bank *bank);
 struct gpiosim_dev *gpiosim_bank_get_dev(struct gpiosim_bank *bank);
Signature · 1 line
2.32.0