+
+/*
+ * This should be called by /sbin/dmesg only via libproc.
+ * It returns as much data still in the buffer as possible.
+ */
+int
+log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval) {
+ uint32_t i;
+ uint32_t localbuff_size;
+ int error = 0, newl, skip;
+ char *localbuff, *p, *copystart, ch;
+ size_t copysize;
+
+ LOG_LOCK();
+ localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
+ LOG_UNLOCK();
+
+ /* Allocate a temporary non-circular buffer for copyout */
+ if (!(localbuff = (char *)kalloc(localbuff_size))) {
+ printf("log_dmesg: unable to allocate memory\n");
+ return (ENOMEM);
+ }
+
+ /* in between here, the log could become bigger, but that's fine */
+ LOG_LOCK();
+
+ /*
+ * The message buffer is circular; start at the write pointer, and
+ * make one loop up to write pointer - 1.
+ */
+ p = msgbufp->msg_bufc + msgbufp->msg_bufx;
+ for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) {
+ if (p >= msgbufp->msg_bufc + msgbufp->msg_size)
+ p = msgbufp->msg_bufc;
+ ch = *p;
+ /* Skip "\n<.*>" syslog sequences. */
+ if (skip) {
+ if (ch == '>')
+ newl = skip = 0;
+ continue;
+ }
+ if (newl && ch == '<') {
+ skip = 1;
+ continue;
+ }
+ if (ch == '\0')
+ continue;
+ newl = (ch == '\n');
+ localbuff[i++] = ch;
+ /* The original version of this routine contained a buffer
+ * overflow. At the time, a "small" targeted fix was desired
+ * so the change below to check the buffer bounds was made.
+ * TODO: rewrite this needlessly convoluted routine.
+ */
+ if (i == (localbuff_size - 2))
+ break;
+ }
+ if (!newl)
+ localbuff[i++] = '\n';
+ localbuff[i++] = 0;
+
+ if (buffersize >= i) {
+ copystart = localbuff;
+ copysize = i;
+ } else {
+ copystart = localbuff + i - buffersize;
+ copysize = buffersize;
+ }
+
+ LOG_UNLOCK();
+
+ error = copyout(copystart, buffer, copysize);
+ if (!error)
+ *retval = copysize;
+
+ kfree(localbuff, localbuff_size);
+ return (error);
+}