]> git.alsa-project.org Git - alsa-utils.git/commitdiff
axfer: maintain lifetime of file descriptor outside of container module
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Thu, 11 Mar 2021 05:21:37 +0000 (14:21 +0900)
committerJaroslav Kysela <perex@perex.cz>
Thu, 11 Mar 2021 08:28:12 +0000 (09:28 +0100)
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>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
axfer/container.c
axfer/container.h
axfer/subcmd-transfer.c
axfer/test/container-test.c
axfer/test/mapper-test.c

index 255f12ce9fddf50f138839e5c79e397cbffbc9dc..8c88d5c4066ead865e85bfc50d7a2cbbcfa73771 100644 (file)
@@ -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);
 
index 0840369ac5b3911e9770eb22d276b314ee4d1c74..71017a6e23ef59c6e3e63f98bf2ea13ddcd1d90c 100644 (file)
@@ -11,7 +11,6 @@
 
 #define _LARGEFILE64_SOURCE
 #include <sys/types.h>
-#include <unistd.h>
 
 #include <stdbool.h>
 #include <stdint.h>
index 52c32d524367cfb6d75f1181bf44f6ddcbebbf86..27d2cc5bced375c5c119135628380ecf4c719e5d 100644 (file)
@@ -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);
 }
index fbef3a462810c8d51d10e877d4eb0d846e0d6194..d89852ac5f12957c3b96af923a23601a2efd751d 100644 (file)
@@ -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,
index 625290054488125f3577dbd50a3a06bff1b3c8a3..78a063aada37cefc46c28704b6f58e9e6ccd467d 100644 (file)
@@ -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;
 }