Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ifwatchd Use recvmsg to ensure we get every message...



details:   https://anonhg.NetBSD.org/src/rev/5a324e347645
branches:  trunk
changeset: 347891:5a324e347645
user:      roy <roy%NetBSD.org@localhost>
date:      Wed Sep 21 14:46:55 2016 +0000

description:
Use recvmsg to ensure we get every message rather than potentially overflowing our buffer.

diffstat:

 usr.sbin/ifwatchd/ifwatchd.c |  25 +++++++++++++++++--------
 1 files changed, 17 insertions(+), 8 deletions(-)

diffs (47 lines):

diff -r 833cc51f4709 -r 5a324e347645 usr.sbin/ifwatchd/ifwatchd.c
--- a/usr.sbin/ifwatchd/ifwatchd.c      Wed Sep 21 14:11:40 2016 +0000
+++ b/usr.sbin/ifwatchd/ifwatchd.c      Wed Sep 21 14:46:55 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ifwatchd.c,v 1.27 2016/01/27 18:55:51 riastradh Exp $  */
+/*     $NetBSD: ifwatchd.c,v 1.28 2016/09/21 14:46:55 roy Exp $        */
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -117,7 +117,9 @@
 {
        int c, s, n;
        int errs = 0;
-       char msg[2048], *msgp;
+       struct msghdr msg;
+       struct iovec iov[1];
+       char buf[2048];
 
        openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
        while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
@@ -206,13 +208,20 @@
        if (!inhibit_initial)
                run_initial_ups();
 
+       iov[0].iov_base = buf;
+       iov[0].iov_len = sizeof(buf);
+       memset(&msg, 0, sizeof(msg));
+       msg.msg_iov = iov;
+       msg.msg_iovlen = 1;
+
        for (;;) {
-               n = read(s, msg, sizeof msg);
-               msgp = msg;
-               for (msgp = msg; n > 0;
-                    n -= ((struct rt_msghdr*)msgp)->rtm_msglen,
-                    msgp += ((struct rt_msghdr*)msgp)->rtm_msglen)
-                       dispatch(msgp, n);
+               n = recvmsg(s, &msg, 0);
+               if (n == -1) {
+                       syslog(LOG_ERR, "recvmsg: %m");
+                       exit(EXIT_FAILURE);
+               }
+               if (n != 0)
+                       dispatch(iov[0].iov_base, n);
        }
 
        close(s);



Home | Main Index | Thread Index | Old Index