From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7774 invoked from network); 22 Sep 2009 15:43:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from new-brage.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.254.104) by ns1.primenet.com.au with SMTP; 22 Sep 2009 15:43:21 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 50506 invoked from network); 22 Sep 2009 15:36:37 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 22 Sep 2009 15:36:37 -0000 Received: (qmail 6528 invoked by alias); 22 Sep 2009 15:36:34 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 27286 Received: (qmail 6510 invoked from network); 22 Sep 2009 15:36:33 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 22 Sep 2009 15:36:33 -0000 Received: from randymail-a2.g.dreamhost.com (caiajhbdcbhh.dreamhost.com [208.97.132.177]) by bifrost.dotsrc.org (Postfix) with ESMTP id 16E6D804CE03 for ; Tue, 22 Sep 2009 17:36:26 +0200 (CEST) Received: from blorf.net (c-98-234-60-111.hsd1.ca.comcast.net [98.234.60.111]) by randymail-a2.g.dreamhost.com (Postfix) with ESMTP id AF340EE2EC; Tue, 22 Sep 2009 08:36:24 -0700 (PDT) Date: Tue, 22 Sep 2009 08:35:42 -0700 From: Wayne Davison To: Peter Stephenson Cc: zsh-workers@sunsite.dk Subject: Re: latest from CVS segfaults when FD ulimit is set too low Message-ID: <20090922153541.GA1717@blorf.net> References: <87iqfgwplu.fsf@meyering.net> <20090921214528.7c7b412c@pws-pc> <20090922100019.3c302758@news01> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="wRRV7LY7NUeQGEoC" Content-Disposition: inline In-Reply-To: <20090922100019.3c302758@news01> User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: ClamAV 0.94.2/9822/Tue Sep 22 15:17:45 2009 on bifrost X-Virus-Status: Clean --wRRV7LY7NUeQGEoC Content-Type: text/plain; charset=us-ascii Content-Disposition: inline We can simplify the code a bit if we make redup() return the target fd on success. The attached patch also adds a little more error checking for redup(). I'll check it in later on if nobody objects. ..wayne.. --wRRV7LY7NUeQGEoC Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="redup.patch" index 3f47636..ad7eb58 100644 --- a/Src/Modules/socket.c +++ b/Src/Modules/socket.c @@ -120,10 +120,7 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func)) } if (targetfd) { - if (redup(sfd, targetfd) == -1) - sfd = -1; - else - sfd = targetfd; + sfd = redup(sfd, targetfd); } else { /* move the fd since no one will want to read from it */ @@ -205,8 +202,11 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func)) } if (targetfd) { - redup(rfd, targetfd); - sfd = targetfd; + sfd = redup(rfd, targetfd); + if (sfd < 0) { + zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno); + return 1; + } } else { sfd = rfd; @@ -242,8 +242,11 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func)) else { if (targetfd) { - redup(sfd, targetfd); - sfd = targetfd; + sfd = redup(sfd, targetfd); + if (sfd < 0) { + zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno); + return 1; + } } setiparam("REPLY", sfd); index 2825cb9..3f92050 100644 --- a/Src/Modules/tcp.c +++ b/Src/Modules/tcp.c @@ -446,10 +446,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func)) } if (targetfd) { - if (redup(sess->fd,targetfd) == -1) - sess->fd = -1; - else - sess->fd = targetfd; + sess->fd = redup(sess->fd, targetfd); } else { /* move the fd since no one will want to read from it */ @@ -547,8 +544,11 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func)) } if (targetfd) { - redup(rfd, targetfd); - sess->fd = targetfd; + sess->fd = redup(rfd, targetfd); + if (sess->fd < 0) { + zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno); + return 1; + } } else { sess->fd = rfd; @@ -662,8 +662,11 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func)) else { if (targetfd) { - redup(sess->fd, targetfd); - sess->fd = targetfd; + sess->fd = redup(sess->fd, targetfd); + if (sess->fd < 0) { + zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno); + return 1; + } } setiparam("REPLY", sess->fd); index 21a7b43..b807eea 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -1654,14 +1654,14 @@ movefd(int fd) /* * Move fd x to y. If x == -1, fd y is closed. - * Return 0 for success, -1 for failure. + * Returns y for success, -1 for failure. */ /**/ mod_export int redup(int x, int y) { - int ret = 0; + int ret = y; if(x < 0) zclose(y); --wRRV7LY7NUeQGEoC--