Commit 2bc3be02 authored by Stefan Westerfeld's avatar Stefan Westerfeld

avfilter/asubprocess: cleanup subprocess in uninit

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent a80a1199
......@@ -98,11 +98,36 @@ static void spb_free_all(SPB **spbp)
*spbp = NULL;
}
static void
close_pipe(int *pipefd)
{
close(pipefd[0]);
close(pipefd[1]);
}
static void
sp_cleanup(SP *sp)
{
if (sp->input_pipe >= 0)
close (sp->input_pipe);
if (sp->output_pipe >= 0)
close (sp->output_pipe);
if (!sp->done) {
int status;
waitpid(sp->pid, &status, 0);
}
spb_free_all(&sp->unused_buffers);
spb_free_all(&sp->out_buffers);
av_free(sp);
}
static int
sp_new(AVFilterContext *ctx)
{
ASubProcessContext *s = ctx->priv;
SP *sp = av_mallocz(sizeof(SP));
SP *sp;
int output_pipe[2];
int input_pipe[2];
int i;
......@@ -116,11 +141,14 @@ sp_new(AVFilterContext *ctx)
if (pipe(input_pipe) < 0) {
int ret = AVERROR(errno);
av_log(ctx, AV_LOG_ERROR, "Failed create subprocess input pipe: %s.\n", strerror(errno));
close_pipe(output_pipe);
return ret;
}
pid = fork();
if (pid < 0) {
int ret = AVERROR(errno);
close_pipe(input_pipe);
close_pipe(output_pipe);
av_log(ctx, AV_LOG_ERROR, "Failed to fork() subprocess: %s.\n", strerror(errno));
return ret;
}
......@@ -133,15 +161,16 @@ sp_new(AVFilterContext *ctx)
perror("asubprocess dup2() for stdout failed");
exit(EXIT_FAILURE);
}
close(output_pipe[1]);
close(output_pipe[0]);
close(input_pipe[1]);
close(input_pipe[0]);
close_pipe(input_pipe);
close_pipe(output_pipe);
execl("/bin/sh", "/bin/sh", "-c", s->command, NULL);
perror("asubprocess execl() failed");
exit(EXIT_FAILURE);
}
sp = av_mallocz(sizeof(SP));
sp->pid = pid;
sp->input_pipe = input_pipe[1];
sp->output_pipe = output_pipe[0];
close(input_pipe[0]);
close(output_pipe[1]);
......@@ -150,6 +179,7 @@ sp_new(AVFilterContext *ctx)
i = fcntl(output_pipe[0], F_GETFL);
if (i < 0) {
int ret = AVERROR(errno);
sp_cleanup(sp);
av_log(ctx, AV_LOG_ERROR, "Failed to get output pipe flags: %s.\n", strerror(errno));
return ret;
}
......@@ -160,14 +190,13 @@ sp_new(AVFilterContext *ctx)
i = fcntl(input_pipe[1], F_GETFL);
if (i < 0) {
int ret = AVERROR(errno);
sp_cleanup(sp);
av_log(ctx, AV_LOG_ERROR, "Failed to get input pipe flags: %s.\n", strerror(errno));
return ret;
}
i |= O_NONBLOCK;
fcntl(input_pipe[1], F_SETFL, i);
sp->input_pipe = input_pipe[1];
sp->output_pipe = output_pipe[0];
s->sp = sp;
return 0;
}
......@@ -410,11 +439,9 @@ static av_cold void uninit(AVFilterContext *ctx)
{
ASubProcessContext *s = ctx->priv;
if (s->sp) {
spb_free_all(&s->sp->unused_buffers);
spb_free_all(&s->sp->out_buffers);
av_freep(&s->sp);
}
if (s->sp)
sp_cleanup(s->sp);
ff_framequeue_free(&s->frame_queue);
av_freep(&s->sample_buffer);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment