Replace home-grown dup2() implementation with actual dup2() calls. This
should slightly reduce the number of system calls in critical portions of the shell, and select a more efficient path through the fdalloc code. Reviewed by: bde
This commit is contained in:
parent
0193eb5ee9
commit
7e1c72660f
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=124780
@ -479,16 +479,14 @@ evalpipe(union node *n)
|
||||
if (forkshell(jp, lp->n, n->npipe.backgnd) == 0) {
|
||||
INTON;
|
||||
if (prevfd > 0) {
|
||||
close(0);
|
||||
copyfd(prevfd, 0);
|
||||
dup2(prevfd, 0);
|
||||
close(prevfd);
|
||||
}
|
||||
if (pip[1] >= 0) {
|
||||
if (!(prevfd >= 0 && pip[0] == 0))
|
||||
close(pip[0]);
|
||||
if (pip[1] != 1) {
|
||||
close(1);
|
||||
copyfd(pip[1], 1);
|
||||
dup2(pip[1], 1);
|
||||
close(pip[1]);
|
||||
}
|
||||
}
|
||||
@ -545,8 +543,7 @@ evalbackcmd(union node *n, struct backcmd *result)
|
||||
FORCEINTON;
|
||||
close(pip[0]);
|
||||
if (pip[1] != 1) {
|
||||
close(1);
|
||||
copyfd(pip[1], 1);
|
||||
dup2(pip[1], 1);
|
||||
close(pip[1]);
|
||||
}
|
||||
evaltree(n, EV_EXIT);
|
||||
@ -742,8 +739,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
|
||||
FORCEINTON;
|
||||
close(pip[0]);
|
||||
if (pip[1] != 1) {
|
||||
close(1);
|
||||
copyfd(pip[1], 1);
|
||||
dup2(pip[1], 1);
|
||||
close(pip[1]);
|
||||
}
|
||||
}
|
||||
|
@ -388,7 +388,7 @@ setinputfile(char *fname, int push)
|
||||
if ((fd = open(fname, O_RDONLY)) < 0)
|
||||
error("Can't open %s: %s", fname, strerror(errno));
|
||||
if (fd < 10) {
|
||||
fd2 = copyfd(fd, 10);
|
||||
fd2 = fcntl(fd, F_DUPFD, 10);
|
||||
close(fd);
|
||||
if (fd2 < 0)
|
||||
error("Out of file descriptors");
|
||||
|
@ -48,7 +48,6 @@ __FBSDID("$FreeBSD$");
|
||||
#include "parser.h"
|
||||
#include "nodes.h"
|
||||
#include "expand.h" /* defines rmescapes() */
|
||||
#include "redir.h" /* defines copyfd() */
|
||||
#include "syntax.h"
|
||||
#include "options.h"
|
||||
#include "input.h"
|
||||
|
@ -182,8 +182,7 @@ openredirect(union node *redir, char memory[10])
|
||||
error("cannot open %s: %s", fname, strerror(errno));
|
||||
movefd:
|
||||
if (f != fd) {
|
||||
close(fd);
|
||||
copyfd(f, fd);
|
||||
dup2(f, fd);
|
||||
close(f);
|
||||
}
|
||||
break;
|
||||
@ -215,12 +214,11 @@ openredirect(union node *redir, char memory[10])
|
||||
if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
|
||||
if (memory[redir->ndup.dupfd])
|
||||
memory[fd] = 1;
|
||||
else {
|
||||
close(fd);
|
||||
copyfd(redir->ndup.dupfd, fd);
|
||||
}
|
||||
} else
|
||||
else
|
||||
dup2(redir->ndup.dupfd, fd);
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
break;
|
||||
case NHERE:
|
||||
case NXHERE:
|
||||
@ -288,10 +286,11 @@ popredir(void)
|
||||
if (rp->renamed[i] != EMPTY) {
|
||||
if (i == 0)
|
||||
fd0_redirected--;
|
||||
close(i);
|
||||
if (rp->renamed[i] >= 0) {
|
||||
copyfd(rp->renamed[i], i);
|
||||
dup2(rp->renamed[i], i);
|
||||
close(rp->renamed[i]);
|
||||
} else {
|
||||
close(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -346,26 +345,3 @@ clearredir(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Copy a file descriptor to be >= to. Returns -1
|
||||
* if the source file descriptor is closed, EMPTY if there are no unused
|
||||
* file descriptors left.
|
||||
*/
|
||||
|
||||
int
|
||||
copyfd(int from, int to)
|
||||
{
|
||||
int newfd;
|
||||
|
||||
newfd = fcntl(from, F_DUPFD, to);
|
||||
if (newfd < 0) {
|
||||
if (errno == EMFILE)
|
||||
return EMPTY;
|
||||
else
|
||||
error("%d: %s", from, strerror(errno));
|
||||
}
|
||||
return newfd;
|
||||
}
|
||||
|
@ -46,5 +46,4 @@ void redirect(union node *, int);
|
||||
void popredir(void);
|
||||
int fd0_redirected_p(void);
|
||||
void clearredir(void);
|
||||
int copyfd(int, int);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user