4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / sbin / dmesg / dmesg.c
CommitLineData
0fc6e47b 1/*-
b890173a
KB
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
0fc6e47b 4 *
befa51e1 5 * %sccs.include.redist.c%
76797561
DF
6 */
7
8#ifndef lint
b890173a
KB
9static char copyright[] =
10"@(#) Copyright (c) 1991, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
0fc6e47b
KB
12#endif /* not lint */
13
14#ifndef lint
b890173a 15static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) %G%";
0fc6e47b 16#endif /* not lint */
76797561 17
befa51e1 18#include <sys/cdefs.h>
34a7981f 19#include <sys/msgbuf.h>
7c753ccc 20
1b207d8b 21#include <fcntl.h>
7c753ccc 22#include <kvm.h>
1b207d8b 23#include <limits.h>
a5d34734
KB
24#include <nlist.h>
25#include <stdio.h>
7c753ccc
KB
26#include <stdlib.h>
27#include <time.h>
1b207d8b
KB
28#include <unistd.h>
29#include <vis.h>
34a7981f 30
befa51e1
KB
31struct nlist nl[] = {
32#define X_MSGBUF 0
b0f3bb00 33 { "_msgbufp" },
befa51e1 34 { NULL },
34a7981f
BJ
35};
36
1b207d8b 37void usage __P((void));
befa51e1 38
1b207d8b 39#define KREAD(addr, var) \
c4fb1efb 40 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
1b207d8b
KB
41
42int
34a7981f 43main(argc, argv)
befa51e1 44 int argc;
1b207d8b 45 char *argv[];
34a7981f 46{
befa51e1
KB
47 register int ch, newl, skip;
48 register char *p, *ep;
b0f3bb00 49 struct msgbuf *bufp, cur;
ad07e3a9 50 char *memf, *nlistf;
1b207d8b 51 kvm_t *kd;
2a9ae6db 52 char buf[5];
34a7981f 53
ad07e3a9 54 memf = nlistf = NULL;
befa51e1
KB
55 while ((ch = getopt(argc, argv, "M:N:")) != EOF)
56 switch(ch) {
57 case 'M':
ad07e3a9 58 memf = optarg;
befa51e1
KB
59 break;
60 case 'N':
ad07e3a9 61 nlistf = optarg;
34a7981f 62 break;
befa51e1
KB
63 case '?':
64 default:
65 usage();
34a7981f 66 }
befa51e1
KB
67 argc -= optind;
68 argv += optind;
69
ad07e3a9
KB
70 /*
71 * Discard setgid privileges if not the running kernel so that bad
72 * guys can't print interesting stuff from kernel memory.
73 */
74 if (memf != NULL || nlistf != NULL)
75 setgid(getgid());
76
befa51e1 77 /* Read in kernel message buffer, do sanity checks. */
2a9ae6db
KB
78 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
79 exit (1);
1b207d8b 80 if (kvm_nlist(kd, nl) == -1)
7c753ccc 81 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
befa51e1 82 if (nl[X_MSGBUF].n_type == 0)
7c753ccc 83 errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist");
1b207d8b 84 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
7c753ccc 85 errx(1, "kvm_read: %s", kvm_geterr(kd));
1b207d8b 86 kvm_close(kd);
befa51e1 87 if (cur.msg_magic != MSG_MAGIC)
7c753ccc 88 errx(1, "magic number incorrect");
befa51e1
KB
89 if (cur.msg_bufx >= MSG_BSIZE)
90 cur.msg_bufx = 0;
91
92 /*
93 * The message buffer is circular; start at the read pointer, and
94 * go to the write pointer - 1.
95 */
96 p = cur.msg_bufc + cur.msg_bufx;
97 ep = cur.msg_bufc + cur.msg_bufx - 1;
98 for (newl = skip = 0; p != ep; ++p) {
99 if (p == cur.msg_bufc + MSG_BSIZE)
100 p = cur.msg_bufc;
101 ch = *p;
102 /* Skip "\n<.*>" syslog sequences. */
103 if (skip) {
104 if (ch == '>')
105 newl = skip = 0;
106 continue;
107 }
108 if (newl && ch == '<') {
109 skip = 1;
110 continue;
111 }
112 if (ch == '\0')
113 continue;
1b207d8b 114 newl = ch == '\n';
7c753ccc 115 (void)vis(buf, ch, 0, 0);
1b207d8b 116 if (buf[1] == 0)
7c753ccc 117 (void)putchar(buf[0]);
1b207d8b 118 else
7c753ccc 119 (void)printf("%s", buf);
befa51e1
KB
120 }
121 if (!newl)
122 (void)putchar('\n');
123 exit(0);
34a7981f
BJ
124}
125
befa51e1
KB
126void
127usage()
128{
129 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
130 exit(1);
34a7981f 131}