about summary refs log tree commit diff
path: root/Src
diff options
context:
space:
mode:
authorWayne Davison <wayned@users.sourceforge.net>2009-12-16 18:39:06 +0000
committerWayne Davison <wayned@users.sourceforge.net>2009-12-16 18:39:06 +0000
commit693fd5b56e51de2910a5681533033b9530650270 (patch)
tree652a4cdfda9874c0eac0ef801a8d266528dd6af1 /Src
parent74d17bc282cb7e9e0b0d73c87aff09ecf256dda7 (diff)
downloadzsh-693fd5b56e51de2910a5681533033b9530650270.tar.gz
zsh-693fd5b56e51de2910a5681533033b9530650270.tar.xz
zsh-693fd5b56e51de2910a5681533033b9530650270.zip
Check the return value of all pipe(), read(), and write() calls.
Gets rid of all the remaining "ignoring return value" compiler
warnings, and makes some read/write operations safer by ensuring
that an EINTR is handled.
Diffstat (limited to 'Src')
-rw-r--r--Src/Modules/zftp.c10
-rw-r--r--Src/Modules/zpty.c4
-rw-r--r--Src/Zle/zle_main.c2
-rw-r--r--Src/exec.c88
-rw-r--r--Src/init.c2
-rw-r--r--Src/input.c2
-rw-r--r--Src/jobs.c2
-rw-r--r--Src/parse.c10
8 files changed, 75 insertions, 45 deletions
diff --git a/Src/Modules/zftp.c b/Src/Modules/zftp.c
index f637d37dc..1558d354f 100644
--- a/Src/Modules/zftp.c
+++ b/Src/Modules/zftp.c
@@ -644,7 +644,7 @@ zfgetline(char *ln, int lnsize, int tmout)
 		cmdbuf[0] = (char)IAC;
 		cmdbuf[1] = (char)DONT;
 		cmdbuf[2] = ch;
-		write(zfsess->control->fd, cmdbuf, 3);
+		write_loop(zfsess->control->fd, cmdbuf, 3);
 		continue;
 
 	    case DO:
@@ -654,7 +654,7 @@ zfgetline(char *ln, int lnsize, int tmout)
 		cmdbuf[0] = (char)IAC;
 		cmdbuf[1] = (char)WONT;
 		cmdbuf[2] = ch;
-		write(zfsess->control->fd, cmdbuf, 3);
+		write_loop(zfsess->control->fd, cmdbuf, 3);
 		continue;
 
 	    case EOF:
@@ -2754,7 +2754,7 @@ zfclose(int leaveparams)
 	if (!zfnopen) {
 	    /* Write the final status in case this is a subshell */
 	    lseek(zfstatfd, zfsessno*sizeof(int), 0);
-	    write(zfstatfd, (char *)zfstatusp+zfsessno, sizeof(int));
+	    write_loop(zfstatfd, (char *)zfstatusp+zfsessno, sizeof(int));
 
 	    close(zfstatfd);
 	    zfstatfd = -1;
@@ -3033,7 +3033,7 @@ bin_zftp(char *name, char **args, UNUSED(Options ops), UNUSED(int func))
 	/* Get the status in case it was set by a forked process */
 	int oldstatus = zfstatusp[zfsessno];
 	lseek(zfstatfd, 0, 0);
-	read(zfstatfd, (char *)zfstatusp, sizeof(int)*zfsesscnt);
+	read_loop(zfstatfd, (char *)zfstatusp, sizeof(int)*zfsesscnt);
 	if (zfsess->control && (zfstatusp[zfsessno] & ZFST_CLOS)) {
 	    /* got closed in subshell without us knowing */
 	    zcfinish = 2;
@@ -3124,7 +3124,7 @@ bin_zftp(char *name, char **args, UNUSED(Options ops), UNUSED(int func))
 	 * but only for the active session.
 	 */
 	lseek(zfstatfd, zfsessno*sizeof(int), 0);
-	write(zfstatfd, (char *)zfstatusp+zfsessno, sizeof(int));
+	write_loop(zfstatfd, (char *)zfstatusp+zfsessno, sizeof(int));
     }
     return ret;
 }
diff --git a/Src/Modules/zpty.c b/Src/Modules/zpty.c
index 16bec2bc9..f25d442b2 100644
--- a/Src/Modules/zpty.c
+++ b/Src/Modules/zpty.c
@@ -586,7 +586,7 @@ ptyread(char *nam, Ptycmd cmd, char **args, int noblock, int mustmatch)
 	    seen = 1;
 	    if (++used == blen) {
 		if (!*args) {
-		    write(1, buf, used);
+		    write_loop(1, buf, used);
 		    used = 0;
 		} else {
 		    buf = hrealloc(buf, blen, blen << 1);
@@ -632,7 +632,7 @@ ptyread(char *nam, Ptycmd cmd, char **args, int noblock, int mustmatch)
     if (*args)
 	setsparam(*args, ztrdup(metafy(buf, used, META_HREALLOC)));
     else if (used)
-	write(1, buf, used);
+	write_loop(1, buf, used);
 
     if (seen && (!prog || matchok || !mustmatch))
 	return 0;
diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c
index ef14342bc..16f98186f 100644
--- a/Src/Zle/zle_main.c
+++ b/Src/Zle/zle_main.c
@@ -1125,7 +1125,7 @@ zleread(char **lp, char **rp, int flags, int context)
 	pptbuf = unmetafy(promptexpand(lp ? *lp : NULL, 0, NULL, NULL,
 				       &pmpt_attr),
 			  &pptlen);
-	write(2, (WRITE_ARG_2_T)pptbuf, pptlen);
+	write_loop(2, (WRITE_ARG_2_T)pptbuf, pptlen);
 	free(pptbuf);
 	return shingetline();
     }
diff --git a/Src/exec.c b/Src/exec.c
index 172d302da..06ff8e30f 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1344,11 +1344,19 @@ execpline(Estate state, wordcode slcode, int how, int last1)
 	    zclose(coprocin);
 	    zclose(coprocout);
 	}
-	mpipe(ipipe);
-	mpipe(opipe);
-	coprocin = ipipe[0];
-	coprocout = opipe[1];
-	fdtable[coprocin] = fdtable[coprocout] = FDT_UNUSED;
+	if (mpipe(ipipe) < 0) {
+	    coprocin = coprocout = -1;
+	    slflags &= ~WC_SUBLIST_COPROC;
+	} else if (mpipe(opipe) < 0) {
+	    close(ipipe[0]);
+	    close(ipipe[1]);
+	    coprocin = coprocout = -1;
+	    slflags &= ~WC_SUBLIST_COPROC;
+	} else {
+	    coprocin = ipipe[0];
+	    coprocout = opipe[1];
+	    fdtable[coprocin] = fdtable[coprocout] = FDT_UNUSED;
+	}
     }
     /* This used to set list_pipe_pid=0 unconditionally, but in things
      * like `ls|if true; then sleep 20; cat; fi' where the sleep was
@@ -1456,16 +1464,17 @@ execpline(Estate state, wordcode slcode, int how, int last1)
 		    ((jn->stat & STAT_STOPPED) ||
 		     (list_pipe_job && pline_level &&
 		      (jobtab[list_pipe_job].stat & STAT_STOPPED)))) {
-		    pid_t pid;
+		    pid_t pid = 0;
 		    int synch[2];
 		    struct timeval bgtime;
 
-		    pipe(synch);
-
-		    if ((pid = zfork(&bgtime)) == -1) {
+		    if (pipe(synch) < 0 || (pid = zfork(&bgtime)) == -1) {
+			if (pid < 0) {
+			    close(synch[0]);
+			    close(synch[1]);
+			} else
+			    zerr("pipe failed: %e", errno);
 			zleentry(ZLE_CMD_TRASH);
-			close(synch[0]);
-			close(synch[1]);
 			fprintf(stderr, "zsh: job can't be suspended\n");
 			fflush(stderr);
 			makerunning(jn);
@@ -1482,7 +1491,7 @@ execpline(Estate state, wordcode slcode, int how, int last1)
 			nowait = errflag = 1;
 			breaks = loops;
 			close(synch[1]);
-			read(synch[0], &dummy, 1);
+			read_loop(synch[0], &dummy, 1);
 			close(synch[0]);
 			/* If this job has finished, we leave it as a
 			 * normal (non-super-) job. */
@@ -1586,7 +1595,9 @@ execpline2(Estate state, wordcode pcode,
 	for (pc = state->pc; wc_code(code = *pc) == WC_REDIR;
 	     pc += WC_REDIR_WORDS(code));
 
-	mpipe(pipes);
+	if (mpipe(pipes) < 0) {
+	    /* FIXME */
+	}
 
 	/* if we are doing "foo | bar" where foo is a current *
 	 * shell command, do foo in a subshell and do the     *
@@ -1595,8 +1606,9 @@ execpline2(Estate state, wordcode pcode,
 	    int synch[2];
 	    struct timeval bgtime;
 
-	    pipe(synch);
-	    if ((pid = zfork(&bgtime)) == -1) {
+	    if (pipe(synch) < 0) {
+		zerr("pipe failed: %e", errno);
+	    } else if ((pid = zfork(&bgtime)) == -1) {
 		close(synch[0]);
 		close(synch[1]);
 	    } else if (pid) {
@@ -1605,7 +1617,7 @@ execpline2(Estate state, wordcode pcode,
 		text = getjobtext(state->prog, state->pc);
 		addproc(pid, text, 0, &bgtime);
 		close(synch[1]);
-		read(synch[0], &dummy, 1);
+		read_loop(synch[0], &dummy, 1);
 		close(synch[0]);
 	    } else {
 		zclose(pipes[0]);
@@ -1873,7 +1885,7 @@ closemn(struct multio **mfds, int fd)
 			break;
 		}
 		for (i = 0; i < mn->ct; i++)
-		    write(mn->fds[i], buf, len);
+		    write_loop(mn->fds[i], buf, len);
 	    }
 	} else {
 	    /* cat process */
@@ -1885,7 +1897,7 @@ closemn(struct multio **mfds, int fd)
 			else
 			    break;
 		    }
-		    write(mn->pipe, buf, len);
+		    write_loop(mn->pipe, buf, len);
 		}
 	}
 	_exit(0);
@@ -2019,7 +2031,11 @@ addfd(int forked, int *save, struct multio **mfds, int fd1, int fd2, int rflag,
 		return;
 	    }
 	    mfds[fd1]->fds[1] = fdN;
-	    mpipe(pipes);
+	    if (mpipe(pipes) < 0) {
+		zerr("multio failed for fd %d: %e", fd2, errno);
+		closemnodes(mfds);
+		return;
+	    }
 	    mfds[fd1]->pipe = pipes[1 - rflag];
 	    redup(pipes[rflag], fd1);
 	    mfds[fd1]->ct = 2;
@@ -2708,9 +2724,13 @@ execcmd(Estate state, int input, int output, int how, int last1)
 	struct timeval bgtime;
 
 	child_block();
-	pipe(synch);
 
-	if ((pid = zfork(&bgtime)) == -1) {
+	if (pipe(synch) < 0) {
+	    zerr("pipe failed: %e", errno);
+	    if (oautocont >= 0)
+		opts[AUTOCONTINUE] = oautocont;
+	    return;
+	} else if ((pid = zfork(&bgtime)) == -1) {
 	    close(synch[0]);
 	    close(synch[1]);
 	    if (oautocont >= 0)
@@ -2720,7 +2740,7 @@ execcmd(Estate state, int input, int output, int how, int last1)
 	if (pid) {
 
 	    close(synch[1]);
-	    read(synch[0], &dummy, 1);
+	    read_loop(synch[0], &dummy, 1);
 	    close(synch[0]);
 #ifdef PATH_DEV_FD
 	    closem(FDT_PROC_SUBST);
@@ -3445,7 +3465,7 @@ getherestr(struct redir *fn)
     t[len++] = '\n';
     if ((fd = gettempfile(NULL, 1, &s)) < 0)
 	return -1;
-    write(fd, t, len);
+    write_loop(fd, t, len);
     close(fd);
     fd = open(s, O_RDONLY | O_NOCTTY);
     unlink(s);
@@ -3507,7 +3527,11 @@ getoutput(char *cmd, int qt)
 	}
 	return readoutput(stream, qt);
     }
-    mpipe(pipes);
+    if (mpipe(pipes) < 0) {
+	errflag = 1;
+	cmdoutpid = 0;
+	return NULL;
+    }
     child_block();
     cmdoutval = 0;
     if ((cmdoutpid = pid = zfork(NULL)) == -1) {
@@ -3664,7 +3688,7 @@ getoutputfile(char *cmd, char **eptr)
 	/* optimised here-string */
 	int len;
 	unmetafy(s, &len);
-	write(fd, s, len);
+	write_loop(fd, s, len);
 	close(fd);
 	return nam;
     }
@@ -3767,7 +3791,8 @@ getproc(char *cmd, char **eptr)
     pnam = hcalloc(strlen(PATH_DEV_FD) + 6);
     if (!(prog = parsecmd(cmd, eptr)))
 	return NULL;
-    mpipe(pipes);
+    if (mpipe(pipes) < 0)
+	return NULL;
     if ((pid = zfork(&bgtime))) {
 	sprintf(pnam, "%s/%d", PATH_DEV_FD, pipes[!out]);
 	zclose(pipes[out]);
@@ -3821,7 +3846,8 @@ getpipe(char *cmd, int nullexec)
 	zerr("invalid syntax for process substitution in redirection");
 	return -1;
     }
-    mpipe(pipes);
+    if (mpipe(pipes) < 0)
+	return -1;
     if ((pid = zfork(&bgtime))) {
 	zclose(pipes[out]);
 	if (pid == -1) {
@@ -3845,12 +3871,16 @@ getpipe(char *cmd, int nullexec)
 /* open pipes with fds >= 10 */
 
 /**/
-static void
+static int
 mpipe(int *pp)
 {
-    pipe(pp);
+    if (pipe(pp) < 0) {
+	zerr("pipe failed: %e", errno);
+	return -1;
+    }
     pp[0] = movefd(pp[0]);
     pp[1] = movefd(pp[1]);
+    return 0;
 }
 
 /*
diff --git a/Src/init.c b/Src/init.c
index cf5a6074d..123e20e85 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -1331,7 +1331,7 @@ VA_DCL
 	pptbuf = unmetafy(promptexpand(lp ? *lp : NULL, 0, NULL, NULL,
 				       NULL),
 			  &pptlen);
-	write(2, (WRITE_ARG_2_T)pptbuf, pptlen);
+	write_loop(2, (WRITE_ARG_2_T)pptbuf, pptlen);
 	free(pptbuf);
 
 	ret = shingetline();
diff --git a/Src/input.c b/Src/input.c
index 5d780f3c4..80f8ec8d5 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -257,7 +257,7 @@ inputline(void)
 	    int pptlen;
 	    pptbuf = unmetafy(promptexpand(ingetcpmptl ? *ingetcpmptl : NULL,
 					   0, NULL, NULL, NULL), &pptlen);
-	    write(2, (WRITE_ARG_2_T)pptbuf, pptlen);
+	    write_loop(2, (WRITE_ARG_2_T)pptbuf, pptlen);
 	    free(pptbuf);
 	}
 	ingetcline = shingetline();
diff --git a/Src/jobs.c b/Src/jobs.c
index 9b7b053ee..a3cf4711b 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -2398,7 +2398,7 @@ acquire_pgrp(void)
 	    if (mypgrp == gettygrp())
 		break;
 	    signal_setmask(oldset);
-	    read(0, NULL, 0); /* Might generate SIGT* */
+	    if (read(0, NULL, 0) != 0) {} /* Might generate SIGT* */
 	    signal_block(blockset);
 	    mypgrp = GETPGRP();
 	}
diff --git a/Src/parse.c b/Src/parse.c
index 7d736e4d3..677d19615 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -2747,7 +2747,7 @@ write_dump(int dfd, LinkList progs, int map, int hlen, int tlen)
 	fdsetflags(pre, ((map ? FDF_MAP : 0) | other));
 	fdsetother(pre, tlen);
 	strcpy(fdversion(pre), ZSH_VERSION);
-	write(dfd, pre, FD_PRELEN * sizeof(wordcode));
+	write_loop(dfd, pre, FD_PRELEN * sizeof(wordcode));
 
 	for (node = firstnode(progs); node; incnode(node)) {
 	    wcf = (WCFunc) getdata(node);
@@ -2768,11 +2768,11 @@ write_dump(int dfd, LinkList progs, int map, int hlen, int tlen)
 	    head.flags = fdhbldflags(wcf->flags, (tail - n));
 	    if (other)
 		fdswap((Wordcode) &head, sizeof(head) / sizeof(wordcode));
-	    write(dfd, &head, sizeof(head));
+	    write_loop(dfd, &head, sizeof(head));
 	    tmp = strlen(n) + 1;
-	    write(dfd, n, tmp);
+	    write_loop(dfd, n, tmp);
 	    if ((tmp &= (sizeof(wordcode) - 1)))
-		write(dfd, &head, sizeof(wordcode) - tmp);
+		write_loop(dfd, &head, sizeof(wordcode) - tmp);
 	}
 	for (node = firstnode(progs); node; incnode(node)) {
 	    prog = ((WCFunc) getdata(node))->prog;
@@ -2780,7 +2780,7 @@ write_dump(int dfd, LinkList progs, int map, int hlen, int tlen)
 		   sizeof(wordcode) - 1) / sizeof(wordcode);
 	    if (other)
 		fdswap(prog->prog, (((Wordcode) prog->strs) - prog->prog));
-	    write(dfd, prog->prog, tmp * sizeof(wordcode));
+	    write_loop(dfd, prog->prog, tmp * sizeof(wordcode));
 	}
 	if (other)
 	    break;