+ for (i = 0; i < count; i++) {
+ if (p >= old_logdata + old_logsize)
+ p = old_logdata;
+
+ ch = *p++;
+ new_logdata[i] = ch;
+ }
+
+ new_bufx = i;
+ if (new_bufx >= new_logsize)
+ new_bufx = 0;
+ msgbufp->msg_bufx = new_bufx;
+
+ new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
+ if (new_bufr < 0)
+ new_bufr += old_logsize;
+ new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
+ if (new_bufr < 0)
+ new_bufr += new_logsize;
+ msgbufp->msg_bufr = new_bufr;
+
+ msgbufp->msg_size = new_logsize;
+ msgbufp->msg_bufc = new_logdata;
+
+ LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
+ size, new_logdata, new_logsize, new_bufr, new_bufx);
+
+ LOG_UNLOCK();
+
+ /* this memory is now dead - clear it so that it compresses better
+ in case of suspend to disk etc. */
+ bzero(old_logdata, old_logsize);
+ if (old_logdata != smsg_bufc) {
+ /* dynamic memory that must be freed */
+ kfree(old_logdata, old_logsize);
+ }
+
+ printf("set system log size to %d bytes\n", new_logsize);
+
+ return 0;
+}
+
+SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_kern_msgbuf, "I", "");
+
+static int sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
+ void *arg1 __unused,
+ int arg2 __unused,
+ struct sysctl_req *req)
+{
+ int old_bufsize, bufsize;
+ int error;
+
+ LOG_LOCK();
+ old_bufsize = bufsize = msgbufp->msg_size;
+ LOG_UNLOCK();
+
+ error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
+ if (error)
+ return (error);
+
+ if (bufsize != old_bufsize) {
+ error = log_setsize(bufsize);
+ }
+
+ return (error);
+}
+
+
+/*
+ * 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);