OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
Re: A question about read(2) in ntpd/bgpd

From: Valentin Kozamernik (tinkomna.com)
Date: Sun Mar 18 2007 - 17:34:13 CDT


On Sun, 18 Mar 2007 at 20:05, Henning Brauer wrote:

> * Valentin Kozamernik <tinkomna.com> [2007-03-18 16:25]:
> > The imsg_read() function uses read(2) and returns 0 on EOF, EINTR or
> > EAGAIN. But whereever imsg_read() is used it is assumed that the
> > return value of 0 means that the socket has been closed.
>
> imsg_read actually uses recvmsg(2), but that doesn't really make a
> difference.

Well, I've checked it again; ntpd users read(2), bgpd uses recvmsg(2)
and ospfd uses recv(2).

> you are correct insofar that the return(0) there is incorrect.
> after looking at things a bit I believe we get away with returning -2

Yes, that is what I was thinking too. Here is a possible patch (which
I currently can not test properly!):

Index: usr.sbin/bgpd/control.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/control.c,v
retrieving revision 1.53
diff -u -r1.53 control.c
--- usr.sbin/bgpd/control.c 2006/08/23 08:13:04 1.53
+++ usr.sbin/bgpd/control.c 2007/03/18 22:21:01
-209,7 +209,7
         if (!(pfd->revents & POLLIN))
                 return (0);
 
- if (imsg_read(&c->ibuf) <= 0) {
+ if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) {
                 *ctl_cnt -= control_close(pfd->fd);
                 return (1);
         }
Index: usr.sbin/bgpd/imsg.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/imsg.c,v
retrieving revision 1.38
diff -u -r1.38 imsg.c
--- usr.sbin/bgpd/imsg.c 2006/05/27 19:59:44 1.38
+++ usr.sbin/bgpd/imsg.c 2007/03/18 22:21:01
-61,7 +61,7
                         log_warn("imsg_read: pipe read error");
                         return (-1);
                 }
- return (0);
+ return (-2);
         }
 
         ibuf->r.wpos += n;
Index: usr.sbin/ntpd/imsg.c
===================================================================
RCS file: /cvs/src/usr.sbin/ntpd/imsg.c,v
retrieving revision 1.11
diff -u -r1.11 imsg.c
--- usr.sbin/ntpd/imsg.c 2005/04/26 15:18:22 1.11
+++ usr.sbin/ntpd/imsg.c 2007/03/18 22:21:02
-46,7 +46,7
                         log_warn("imsg_read: pipe read error");
                         return (-1);
                 }
- return (0);
+ return (-2);
         }
 
         ibuf->r.wpos += n;
Index: usr.sbin/ospfd/control.c
===================================================================
RCS file: /cvs/src/usr.sbin/ospfd/control.c,v
retrieving revision 1.16
diff -u -r1.16 control.c
--- usr.sbin/ospfd/control.c 2007/01/23 17:39:33 1.16
+++ usr.sbin/ospfd/control.c 2007/03/18 22:21:02
-197,7 +197,7
 
         switch (event) {
         case EV_READ:
- if ((n = imsg_read(&c->ibuf)) <= 0) {
+ if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) {
                         control_close(fd);
                         return;
                 }
Index: usr.sbin/ospfd/imsg.c
===================================================================
RCS file: /cvs/src/usr.sbin/ospfd/imsg.c,v
retrieving revision 1.7
diff -u -r1.7 imsg.c
--- usr.sbin/ospfd/imsg.c 2006/05/27 20:07:42 1.7
+++ usr.sbin/ospfd/imsg.c 2007/03/18 22:21:02
-50,7 +50,7
                         log_warn("imsg_read: pipe read error");
                         return (-1);
                 }
- return (0);
+ return (-2);
         }
 
         ibuf->r.wpos += n;

--
Tin