kernel - Refactor the kernel message buffer code (2)
authorMatthew Dillon <dillon@apollo.backplane.com>
Tue, 3 Mar 2015 02:40:21 +0000 (18:40 -0800)
committerMatthew Dillon <dillon@apollo.backplane.com>
Tue, 3 Mar 2015 02:40:21 +0000 (18:40 -0800)
* Fix an edge case, the sysctl was not working properly when the msgbuf
  was completely empty.  Generate a single newline output.

* Also if dmesg gets a msgbuf size of 0, allocate at least 1 byte to
  avoid a malloc failure.

sbin/dmesg/dmesg.c
sys/kern/subr_prf.c

index e3312b3..16ff83e 100644 (file)
@@ -117,6 +117,9 @@ main(int argc, char **argv)
                buflen = 0;
                if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
                        err(1, "sysctl kern.msgbuf");
+               if (buflen == 0)        /* can happen if msgbuf was cleared */
+                       buflen = 1;
+
                if ((bp = malloc(buflen)) == NULL)
                        errx(1, "malloc failed");
                if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
index c3c882e..f8caff8 100644 (file)
@@ -1151,16 +1151,31 @@ sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
        xindex_modulo = xindex % mbp->msg_size;
 
        if (rindex_modulo < xindex_modulo) {
+               /*
+                * Can handle in one linear section.
+                */
                error = sysctl_handle_opaque(oidp,
                                             mbp->msg_ptr + rindex_modulo,
                                             xindex_modulo - rindex_modulo,
                                             req);
+       } else if (rindex_modulo == xindex_modulo) {
+               /*
+                * Empty buffer, just return a single newline
+                */
+               error = sysctl_handle_opaque(oidp, "\n", 1, req);
        } else if (n <= mbp->msg_size - rindex_modulo) {
+               /*
+                * Can handle in one linear section.
+                */
                error = sysctl_handle_opaque(oidp,
                                             mbp->msg_ptr + rindex_modulo,
                                             n - rindex_modulo,
                                             req);
        } else {
+               /*
+                * Glue together two linear sections into one contiguous
+                * output.
+                */
                error = sysctl_handle_opaque(oidp,
                                             mbp->msg_ptr + rindex_modulo,
                                             mbp->msg_size - rindex_modulo,