diff mbox series

[alsa-utils,06/14] axfer: maintain lifetime of file descriptor outside of container module

Message ID 20210311052146.404003-7-o-takashi@sakamocchi.jp
State New
Headers show
Series axfer: reduce test time | expand

Commit Message

Takashi Sakamoto March 11, 2021, 5:21 a.m. UTC
This commit closes file descriptor outside of container module so
that maintenance of lifetime for the descriptor is delegated to container
user.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/container.c           |  1 -
 axfer/container.h           |  1 -
 axfer/subcmd-transfer.c     | 18 +++++++++++++--
 axfer/test/container-test.c |  2 ++
 axfer/test/mapper-test.c    | 44 +++++++++++++++++++++++++++----------
 5 files changed, 50 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/axfer/container.c b/axfer/container.c
index 255f12c..8c88d5c 100644
--- a/axfer/container.c
+++ b/axfer/container.c
@@ -451,7 +451,6 @@  void container_context_destroy(struct container_context *cntr)
 {
 	assert(cntr);
 
-	close(cntr->fd);
 	if (cntr->private_data)
 		free(cntr->private_data);
 
diff --git a/axfer/container.h b/axfer/container.h
index 0840369..71017a6 100644
--- a/axfer/container.h
+++ b/axfer/container.h
@@ -11,7 +11,6 @@ 
 
 #define _LARGEFILE64_SOURCE
 #include <sys/types.h>
-#include <unistd.h>
 
 #include <stdbool.h>
 #include <stdint.h>
diff --git a/axfer/subcmd-transfer.c b/axfer/subcmd-transfer.c
index 52c32d5..27d2cc5 100644
--- a/axfer/subcmd-transfer.c
+++ b/axfer/subcmd-transfer.c
@@ -19,6 +19,8 @@  struct context {
 	struct container_context *cntrs;
 	unsigned int cntr_count;
 
+	int *cntr_fds;
+
 	// NOTE: To handling Unix signal.
 	bool interrupted;
 	int signal;
@@ -153,6 +155,10 @@  static int allocate_containers(struct context *ctx, unsigned int count)
 		return -ENOMEM;
 	ctx->cntr_count = count;
 
+	ctx->cntr_fds = calloc(count, sizeof(*ctx->cntrs));
+	if (ctx->cntr_fds == NULL)
+		return -ENOMEM;
+
 	return 0;
 }
 
@@ -196,8 +202,9 @@  static int capture_pre_process(struct context *ctx, snd_pcm_access_t *access,
 			if (fd < 0)
 				return -errno;
 		}
+		ctx->cntr_fds[i] = fd;
 
-		err = container_builder_init(ctx->cntrs + i, fd,
+		err = container_builder_init(ctx->cntrs + i, ctx->cntr_fds[i],
 					     ctx->xfer.cntr_format,
 					     ctx->xfer.verbose > 1);
 		if (err < 0)
@@ -249,8 +256,9 @@  static int playback_pre_process(struct context *ctx, snd_pcm_access_t *access,
 			if (fd < 0)
 				return -errno;
 		}
+		ctx->cntr_fds[i] = fd;
 
-		err = container_parser_init(ctx->cntrs + i, fd,
+		err = container_parser_init(ctx->cntrs + i, ctx->cntr_fds[i],
 					    ctx->xfer.verbose > 1);
 		if (err < 0)
 			return err;
@@ -447,6 +455,12 @@  static void context_post_process(struct context *ctx,
 		free(ctx->cntrs);
 	}
 
+	if (ctx->cntr_fds) {
+		for (i = 0; i < ctx->cntr_count; ++i)
+			close(ctx->cntr_fds[i]);
+		free(ctx->cntr_fds);
+	}
+
 	mapper_context_post_process(&ctx->mapper);
 	mapper_context_destroy(&ctx->mapper);
 }
diff --git a/axfer/test/container-test.c b/axfer/test/container-test.c
index fbef3a4..d89852a 100644
--- a/axfer/test/container-test.c
+++ b/axfer/test/container-test.c
@@ -73,6 +73,7 @@  static void test_builder(struct container_context *cntr,
 	assert(total_frame_count == frame_count);
 
 	container_context_destroy(cntr);
+	close(fd);
 }
 
 static void test_parser(struct container_context *cntr,
@@ -121,6 +122,7 @@  static void test_parser(struct container_context *cntr,
 	assert(total_frame_count == handled_frame_count);
 
 	container_context_destroy(cntr);
+	close(fd);
 }
 
 static int callback(struct test_generator *gen, snd_pcm_access_t access,
diff --git a/axfer/test/mapper-test.c b/axfer/test/mapper-test.c
index 6252900..78a063a 100644
--- a/axfer/test/mapper-test.c
+++ b/axfer/test/mapper-test.c
@@ -67,23 +67,29 @@  static int test_demux(struct mapper_trial *trial, snd_pcm_access_t access,
 {
 	struct container_context *cntrs = trial->cntrs;
 	enum container_format cntr_format = trial->cntr_format;
+	int *cntr_fds;
 	unsigned int bytes_per_sample;
 	uint64_t total_frame_count;
 	int i;
 	int err = 0;
 
+	cntr_fds = calloc(cntr_count, sizeof(*cntr_fds));
+	if (cntr_fds == NULL)
+		return -ENOMEM;
+
 	for (i = 0; i < cntr_count; ++i) {
 		const char *path = trial->paths[i];
-		int fd;
 		snd_pcm_format_t format;
 		unsigned int channels;
 		unsigned int rate;
 
-		fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
-		if (fd < 0)
-			return -errno;
+		cntr_fds[i] = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
+		if (cntr_fds[i] < 0) {
+			err = -errno;
+			goto end;
+		}
 
-		err = container_builder_init(cntrs + i, fd, cntr_format, 0);
+		err = container_builder_init(cntrs + i, cntr_fds[i], cntr_format, 0);
 		if (err < 0)
 			goto end;
 
@@ -118,8 +124,12 @@  static int test_demux(struct mapper_trial *trial, snd_pcm_access_t access,
 		assert(total_frame_count == frame_count);
 	}
 end:
-	for (i = 0; i < cntr_count; ++i)
+	for (i = 0; i < cntr_count; ++i) {
 		container_context_destroy(cntrs + i);
+		close(cntr_fds[i]);
+	}
+
+	free(cntr_fds);
 
 	return err;
 }
@@ -163,23 +173,29 @@  static int test_mux(struct mapper_trial *trial, snd_pcm_access_t access,
 		    unsigned int cntr_count)
 {
 	struct container_context *cntrs = trial->cntrs;
+	int *cntr_fds;
 	unsigned int bytes_per_sample;
 	uint64_t total_frame_count;
 	int i;
 	int err = 0;
 
+	cntr_fds = calloc(cntr_count, sizeof(*cntr_fds));
+	if (cntr_fds == NULL)
+		return -ENOMEM;
+
 	for (i = 0; i < cntr_count; ++i) {
 		const char *path = trial->paths[i];
-		int fd;
 		snd_pcm_format_t format;
 		unsigned int channels;
 		unsigned int rate;
 
-		fd = open(path, O_RDONLY);
-		if (fd < 0)
-			return -errno;
+		cntr_fds[i] = open(path, O_RDONLY);
+		if (cntr_fds[i] < 0) {
+			err = -errno;
+			goto end;
+		}
 
-		err = container_parser_init(cntrs + i, fd, 0);
+		err = container_parser_init(cntrs + i, cntr_fds[i], 0);
 		if (err < 0)
 			goto end;
 
@@ -214,8 +230,12 @@  static int test_mux(struct mapper_trial *trial, snd_pcm_access_t access,
 		assert(total_frame_count == frame_count);
 	}
 end:
-	for (i = 0; i < cntr_count; ++i)
+	for (i = 0; i < cntr_count; ++i) {
 		container_context_destroy(cntrs + i);
+		close(cntr_fds[i]);
+	}
+
+	free(cntr_fds);
 
 	return err;
 }