axfer: maintain lifetime of file descriptor outside of container module

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>
This commit is contained in:
Takashi Sakamoto 2021-03-11 14:21:37 +09:00 committed by Jaroslav Kysela
parent 5cb67b5ab6
commit 6b0f1b20ae
5 changed files with 50 additions and 16 deletions

View 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);

View File

@ -11,7 +11,6 @@
#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>

View 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);
}

View 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,

View 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;
}