1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1991, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; 42 #endif /* not lint */ 43 44 #include <sys/cdefs.h> 45 #include <sys/msgbuf.h> 46 47 #include <err.h> 48 #include <fcntl.h> 49 #include <kvm.h> 50 #include <limits.h> 51 #include <nlist.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <time.h> 55 #include <unistd.h> 56 #include <vis.h> 57 58 struct nlist nl[] = { 59 #define X_MSGBUF 0 60 { "_msgbufp" }, 61 { NULL }, 62 }; 63 64 void usage __P((void)); 65 66 #define KREAD(addr, var) \ 67 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 68 69 int 70 main(argc, argv) 71 int argc; 72 char *argv[]; 73 { 74 register int ch, newl, skip; 75 register char *p, *ep; 76 struct msgbuf *bufp, cur; 77 char *memf, *nlistf; 78 kvm_t *kd; 79 char buf[5]; 80 81 memf = nlistf = NULL; 82 while ((ch = getopt(argc, argv, "M:N:")) != EOF) 83 switch(ch) { 84 case 'M': 85 memf = optarg; 86 break; 87 case 'N': 88 nlistf = optarg; 89 break; 90 case '?': 91 default: 92 usage(); 93 } 94 argc -= optind; 95 argv += optind; 96 97 /* 98 * Discard setgid privileges if not the running kernel so that bad 99 * guys can't print interesting stuff from kernel memory. 100 */ 101 if (memf != NULL || nlistf != NULL) 102 setgid(getgid()); 103 104 /* Read in kernel message buffer, do sanity checks. */ 105 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL) 106 exit (1); 107 if (kvm_nlist(kd, nl) == -1) 108 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 109 if (nl[X_MSGBUF].n_type == 0) 110 errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); 111 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 112 errx(1, "kvm_read: %s", kvm_geterr(kd)); 113 kvm_close(kd); 114 if (cur.msg_magic != MSG_MAGIC) 115 errx(1, "magic number incorrect"); 116 if (cur.msg_bufx >= MSG_BSIZE) 117 cur.msg_bufx = 0; 118 119 /* 120 * The message buffer is circular; start at the read pointer, and 121 * go to the write pointer - 1. 122 */ 123 p = cur.msg_bufc + cur.msg_bufx; 124 ep = cur.msg_bufc + cur.msg_bufx - 1; 125 for (newl = skip = 0; p != ep; ++p) { 126 if (p == cur.msg_bufc + MSG_BSIZE) 127 p = cur.msg_bufc; 128 ch = *p; 129 /* Skip "\n<.*>" syslog sequences. */ 130 if (skip) { 131 if (ch == '>') 132 newl = skip = 0; 133 continue; 134 } 135 if (newl && ch == '<') { 136 skip = 1; 137 continue; 138 } 139 if (ch == '\0') 140 continue; 141 newl = ch == '\n'; 142 (void)vis(buf, ch, 0, 0); 143 if (buf[1] == 0) 144 (void)putchar(buf[0]); 145 else 146 (void)printf("%s", buf); 147 } 148 if (!newl) 149 (void)putchar('\n'); 150 exit(0); 151 } 152 153 void 154 usage() 155 { 156 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 157 exit(1); 158 } 159