| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
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. | |
| 1de703da MD |
32 | * |
| 33 | * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. | |
| 34 | * @(#)dmesg.c 8.1 (Berkeley) 6/5/93 | |
| 35 | * $FreeBSD: src/sbin/dmesg/dmesg.c,v 1.11.2.3 2001/08/08 22:32:15 obrien Exp $ | |
| f44d99cd | 36 | * $DragonFly: src/sbin/dmesg/dmesg.c,v 1.7 2005/01/14 06:38:41 cpressey Exp $ |
| 984263bc MD |
37 | */ |
| 38 | ||
| 984263bc MD |
39 | #include <sys/types.h> |
| 40 | #include <sys/msgbuf.h> | |
| 41 | #include <sys/sysctl.h> | |
| 42 | ||
| 43 | #include <err.h> | |
| 44 | #include <fcntl.h> | |
| 45 | #include <kvm.h> | |
| 46 | #include <locale.h> | |
| 47 | #include <stdio.h> | |
| 48 | #include <stdlib.h> | |
| 49 | #include <unistd.h> | |
| 50 | #include <vis.h> | |
| 51 | #include <sys/syslog.h> | |
| 52 | ||
| 53 | struct nlist nl[] = { | |
| 54 | #define X_MSGBUF 0 | |
| f44d99cd CP |
55 | { "_msgbufp", 0, 0, 0, 0 }, |
| 56 | { NULL, 0, 0, 0, 0 }, | |
| 984263bc MD |
57 | }; |
| 58 | ||
| 9a76cd75 | 59 | void usage(void); |
| 984263bc MD |
60 | |
| 61 | #define KREAD(addr, var) \ | |
| 62 | kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) | |
| 63 | ||
| 64 | int | |
| b5744197 | 65 | main(int argc, char **argv) |
| 984263bc MD |
66 | { |
| 67 | int ch, newl, skip; | |
| 68 | char *p, *ep; | |
| 69 | struct msgbuf *bufp, cur; | |
| 70 | char *bp, *memf, *nlistf; | |
| 71 | kvm_t *kd; | |
| 72 | char buf[5]; | |
| 73 | int all = 0; | |
| 8af0d623 | 74 | int pri = 0; |
| f44d99cd | 75 | size_t buflen, bufpos; |
| 984263bc | 76 | |
| 7cee7052 | 77 | setlocale(LC_CTYPE, ""); |
| 984263bc MD |
78 | memf = nlistf = NULL; |
| 79 | while ((ch = getopt(argc, argv, "aM:N:")) != -1) | |
| 80 | switch(ch) { | |
| 81 | case 'a': | |
| 82 | all++; | |
| 83 | break; | |
| 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 | if (memf == NULL && nlistf == NULL) { | |
| 98 | /* Running kernel. Use sysctl. */ | |
| 99 | if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) | |
| 100 | err(1, "sysctl kern.msgbuf"); | |
| 35bb81ce | 101 | buflen += 4096; /* add some slop */ |
| 984263bc MD |
102 | if ((bp = malloc(buflen)) == NULL) |
| 103 | errx(1, "malloc failed"); | |
| 104 | if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) | |
| 105 | err(1, "sysctl kern.msgbuf"); | |
| 106 | /* We get a dewrapped buffer using sysctl. */ | |
| 107 | bufpos = 0; | |
| 108 | } else { | |
| 109 | /* Read in kernel message buffer, do sanity checks. */ | |
| 110 | kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); | |
| 111 | if (kd == NULL) | |
| 112 | exit (1); | |
| 113 | if (kvm_nlist(kd, nl) == -1) | |
| 114 | errx(1, "kvm_nlist: %s", kvm_geterr(kd)); | |
| 115 | if (nl[X_MSGBUF].n_type == 0) | |
| 116 | errx(1, "%s: msgbufp not found", | |
| 117 | nlistf ? nlistf : "namelist"); | |
| 118 | if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) | |
| 119 | errx(1, "kvm_read: %s", kvm_geterr(kd)); | |
| 120 | if (cur.msg_magic != MSG_MAGIC) | |
| 121 | errx(1, "kernel message buffer has different magic " | |
| 122 | "number"); | |
| 123 | bp = malloc(cur.msg_size); | |
| 124 | if (!bp) | |
| 125 | errx(1, "malloc failed"); | |
| 126 | if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) != | |
| f44d99cd | 127 | (ssize_t)cur.msg_size) |
| 984263bc MD |
128 | errx(1, "kvm_read: %s", kvm_geterr(kd)); |
| 129 | kvm_close(kd); | |
| 130 | buflen = cur.msg_size; | |
| 131 | bufpos = cur.msg_bufx; | |
| 132 | if (bufpos >= buflen) | |
| 133 | bufpos = 0; | |
| 134 | } | |
| 135 | ||
| 136 | /* | |
| 137 | * The message buffer is circular. If the buffer has wrapped, the | |
| 138 | * write pointer points to the oldest data. Otherwise, the write | |
| 139 | * pointer points to \0's following the data. Read the entire | |
| 140 | * buffer starting at the write pointer and ignore nulls so that | |
| 141 | * we effectively start at the oldest data. | |
| 142 | */ | |
| 143 | p = bp + bufpos; | |
| 144 | ep = (bufpos == 0 ? bp + buflen : p); | |
| 145 | newl = skip = 0; | |
| 146 | do { | |
| 147 | if (p == bp + buflen) | |
| 148 | p = bp; | |
| 149 | ch = *p; | |
| 150 | /* Skip "\n<.*>" syslog sequences. */ | |
| 151 | if (skip) { | |
| 152 | if (ch == '\n') { | |
| 153 | skip = 0; | |
| 154 | newl = 1; | |
| 155 | } if (ch == '>') { | |
| 156 | if (LOG_FAC(pri) == LOG_KERN || all) | |
| 157 | newl = skip = 0; | |
| 158 | } else if (ch >= '0' && ch <= '9') { | |
| 159 | pri *= 10; | |
| 160 | pri += ch - '0'; | |
| 161 | } | |
| 162 | continue; | |
| 163 | } | |
| 164 | if (newl && ch == '<') { | |
| 165 | pri = 0; | |
| 166 | skip = 1; | |
| 167 | continue; | |
| 168 | } | |
| 169 | if (ch == '\0') | |
| 170 | continue; | |
| 171 | newl = ch == '\n'; | |
| 7cee7052 | 172 | vis(buf, ch, 0, 0); |
| 984263bc | 173 | if (buf[1] == 0) |
| 7cee7052 | 174 | putchar(buf[0]); |
| 984263bc | 175 | else |
| 7cee7052 | 176 | printf("%s", buf); |
| 984263bc MD |
177 | } while (++p != ep); |
| 178 | if (!newl) | |
| 7cee7052 | 179 | putchar('\n'); |
| 984263bc MD |
180 | exit(0); |
| 181 | } | |
| 182 | ||
| 183 | void | |
| b5744197 | 184 | usage(void) |
| 984263bc | 185 | { |
| 7cee7052 | 186 | fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n"); |
| 984263bc MD |
187 | exit(1); |
| 188 | } |