1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-06 23:31:28 +02:00

27284: better use of movefd()

This commit is contained in:
Peter Stephenson 2009-09-22 09:17:05 +00:00
parent 997eafdcad
commit 51409732d0
7 changed files with 66 additions and 18 deletions

View file

@ -1,3 +1,10 @@
2009-09-22 Peter Stephenson <pws@csr.com>
* 27284: Src/exec.c, Src/parse.c, Src/utils.c,
Src/Modules/socket.c, Src/Modules/tcp.c, Src/Modules/zpty.c:
improve use of movefd() and restore closing of original fd
on failure pending further work.
2009-09-21 Peter Stephenson <p.w.stephenson@ntlworld.com> 2009-09-21 Peter Stephenson <p.w.stephenson@ntlworld.com>
* 27283: Src/exec.c, Src/utils.c: failure to dup fd accessed * 27283: Src/exec.c, Src/utils.c: failure to dup fd accessed
@ -12202,5 +12209,5 @@
***************************************************** *****************************************************
* This is used by the shell to define $ZSH_PATCHLEVEL * This is used by the shell to define $ZSH_PATCHLEVEL
* $Revision: 1.4785 $ * $Revision: 1.4786 $
***************************************************** *****************************************************

View file

@ -120,13 +120,19 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func))
} }
if (targetfd) { if (targetfd) {
redup(sfd, targetfd); if (redup(sfd, targetfd) == -1)
sfd = -1;
else
sfd = targetfd; sfd = targetfd;
} }
else { else {
/* move the fd since no one will want to read from it */ /* move the fd since no one will want to read from it */
sfd = movefd(sfd); sfd = movefd(sfd);
} }
if (sfd == -1) {
zerrnam(nam, "cannot duplicate fd %d: %e", sfd, errno);
return 1;
}
setiparam("REPLY", sfd); setiparam("REPLY", sfd);

View file

@ -446,7 +446,9 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
} }
if (targetfd) { if (targetfd) {
redup(sess->fd,targetfd); if (redup(sess->fd,targetfd) == -1)
sess->fd = -1;
else
sess->fd = targetfd; sess->fd = targetfd;
} }
else { else {
@ -454,6 +456,12 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
sess->fd = movefd(sess->fd); sess->fd = movefd(sess->fd);
} }
if (sess->fd == -1) {
zwarnnam(nam, "cannot duplicate fd %d: %e", sess->fd, errno);
tcp_close(sess);
return 1;
}
setiparam("REPLY", sess->fd); setiparam("REPLY", sess->fd);
if (verbose) if (verbose)

View file

@ -401,6 +401,12 @@ newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
zexit(lastval, 0); zexit(lastval, 0);
} }
master = movefd(master); master = movefd(master);
if (master == -1) {
zerrnam(nam, "cannot duplicate fd %d: %e", master, errno);
scriptname = oscriptname;
ineval = oineval;
return 1;
}
p = (Ptycmd) zalloc(sizeof(*p)); p = (Ptycmd) zalloc(sizeof(*p));
@ -423,6 +429,7 @@ newptycmd(char *nam, char *pname, char **args, int echo, int nblock)
scriptname = oscriptname; scriptname = oscriptname;
ineval = oineval; ineval = oineval;
return 0; return 0;
} }

View file

@ -1958,6 +1958,10 @@ addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
if (varid) { if (varid) {
/* fd will be over 10, don't touch mfds */ /* fd will be over 10, don't touch mfds */
fd1 = movefd(fd2); fd1 = movefd(fd2);
if (fd1 == -1) {
zerr("cannot moved fd %d: %e", fd2, errno);
return;
} else {
fdtable[fd1] = FDT_EXTERNAL; fdtable[fd1] = FDT_EXTERNAL;
setiparam(varid, (zlong)fd1); setiparam(varid, (zlong)fd1);
/* /*
@ -1966,6 +1970,7 @@ addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
*/ */
if (errflag) if (errflag)
zclose(fd1); zclose(fd1);
}
} else if (!mfds[fd1] || unset(MULTIOS)) { } else if (!mfds[fd1] || unset(MULTIOS)) {
if(!mfds[fd1]) { /* starting a new multio */ if(!mfds[fd1]) { /* starting a new multio */
mfds[fd1] = (struct multio *) zhalloc(sizeof(struct multio)); mfds[fd1] = (struct multio *) zhalloc(sizeof(struct multio));

View file

@ -3095,6 +3095,8 @@ load_dump_file(char *dump, struct stat *sbuf, int other, int len)
return; return;
fd = movefd(fd); fd = movefd(fd);
if (fd == -1)
return;
if ((addr = (Wordcode) mmap(NULL, mlen, PROT_READ, MAP_SHARED, fd, off)) == if ((addr = (Wordcode) mmap(NULL, mlen, PROT_READ, MAP_SHARED, fd, off)) ==
((Wordcode) -1)) { ((Wordcode) -1)) {

View file

@ -1631,7 +1631,12 @@ movefd(int fd)
#else #else
int fe = movefd(dup(fd)); int fe = movefd(dup(fd));
#endif #endif
if (fe != -1) /*
* To close or not to close if fe is -1?
* If it is -1, we haven't moved the fd, so if we close
* it we lose it; but we're probably not going to be able
* to use it in situ anyway. So probably better to avoid a leak.
*/
zclose(fd); zclose(fd);
fd = fe; fd = fe;
} }
@ -1647,22 +1652,30 @@ movefd(int fd)
return fd; return fd;
} }
/* Move fd x to y. If x == -1, fd y is closed. */ /*
* Move fd x to y. If x == -1, fd y is closed.
* Return 0 for success, -1 for failure.
*/
/**/ /**/
mod_export void mod_export int
redup(int x, int y) redup(int x, int y)
{ {
int ret = 0;
if(x < 0) if(x < 0)
zclose(y); zclose(y);
else if (x != y) { else if (x != y) {
while (y >= fdtable_size) while (y >= fdtable_size)
fdtable = zrealloc(fdtable, (fdtable_size *= 2)*sizeof(*fdtable)); fdtable = zrealloc(fdtable, (fdtable_size *= 2)*sizeof(*fdtable));
dup2(x, y); if (dup2(x, y) == -1)
ret = -1;
if ((fdtable[y] = fdtable[x]) && y > max_zsh_fd) if ((fdtable[y] = fdtable[x]) && y > max_zsh_fd)
max_zsh_fd = y; max_zsh_fd = y;
zclose(x); zclose(x);
} }
return ret;
} }
/* Close the given fd, and clear it from fdtable. */ /* Close the given fd, and clear it from fdtable. */