[MPlayer-dev-eng] [PATCH] Use unrar for open vobsubs if available

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Fri Dec 7 23:22:41 CET 2007


Hello,
On Sat, Dec 08, 2007 at 12:43:36AM +0800, Ulion wrote:
> 2007/12/8, Reimar Doeffinger <Reimar.Doeffinger at stud.uni-karlsruhe.de>:
[...]
> > > How about:
> > >
> > >         fd = open("/dev/null", O_WRONLY);
> > >         if(fd == -1)
> > >             _exit(EXIT_FAILURE);
> > >         if (fd != 2) {
> > >             dup2(fd, 2);
> > >             close(fd);
> > >         }
> >
> > What is the point of the != 2 check then? Either you assume that
> > stdin, stdout and stderr are correctly set at the beginning,
> > then fd _can_ not be 2 (because you did not close it before).
> > Or if you don't assume that and you should check for that case (which,
> > due to "open" being required to return the smallest non-open
> > descriptor, my < 3 test does, though I admit it is not ideal
> > in clearness).
> 
> I assume stdout is correctly set, does not assume whether stdin and
> stderr are correctly set.
> So my last code will works fine with the assume -- stdout is correctly set.
> That's enough for current implement since it only care the stdout.

Except that if you only assume stdout is opened, the first file opened
by the unrar binary would become its stdin.
The same applies to stderr if this dup2 fails.
A truly proper solution would also try to close all other open file
descriptors that are not needed, but that is quite ugly (I think OpenBSD
has an extra syscall for that, I must say that really is a very
advisable thing).
I think code like this would make sense (note you'll have to replace
that pipefd by the right variable, I forgot the name):
> // close MPlayer's stdin, stdout and stderr so the unrar binary
> // can not mess them up
> // TODO: close all other files except the pipe
> close(0); close(1); close(2);
> // assign new stdin, stdout and stderr and check they actually got the
> // right descriptors
> if (open("/dev/null", O_RDONLY) != 0 || dup(pipefd) != 1 || open("/dev/null", O_WRONLY) != 2)
>   _exit(EXIT_FAILURE);

Greetings,
Reimar Döffinger



More information about the MPlayer-dev-eng mailing list