148128Sbostic /*-
2*61482Sbostic * Copyright (c) 1991, 1993
3*61482Sbostic * The Regents of the University of California. All rights reserved.
448128Sbostic *
548966Sbostic * %sccs.include.redist.c%
622058Sdist */
722058Sdist
822058Sdist #ifndef lint
9*61482Sbostic static char copyright[] =
10*61482Sbostic "@(#) Copyright (c) 1991, 1993\n\
11*61482Sbostic The Regents of the University of California. All rights reserved.\n";
1248128Sbostic #endif /* not lint */
1322058Sdist
1448128Sbostic #ifndef lint
15*61482Sbostic static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 06/05/93";
1648128Sbostic #endif /* not lint */
1748128Sbostic
1848966Sbostic #include <sys/cdefs.h>
196019Swnj #include <sys/msgbuf.h>
2059445Sbostic
2153665Sbostic #include <fcntl.h>
2259445Sbostic #include <kvm.h>
2353665Sbostic #include <limits.h>
2437263Sbostic #include <nlist.h>
2559445Sbostic #include <stdio.h>
2648966Sbostic #include <stdlib.h>
2759445Sbostic #include <time.h>
2853665Sbostic #include <unistd.h>
2953665Sbostic #include <vis.h>
306019Swnj
3148966Sbostic struct nlist nl[] = {
3248966Sbostic #define X_MSGBUF 0
3352479Skarels { "_msgbufp" },
3448966Sbostic { NULL },
356019Swnj };
366019Swnj
3753665Sbostic void usage __P((void));
3848966Sbostic
3953665Sbostic #define KREAD(addr, var) \
4053677Sbostic kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
4153665Sbostic
4253665Sbostic int
main(argc,argv)436019Swnj main(argc, argv)
4448966Sbostic int argc;
4553665Sbostic char *argv[];
466019Swnj {
4748966Sbostic register int ch, newl, skip;
4848966Sbostic register char *p, *ep;
4952479Skarels struct msgbuf *bufp, cur;
5052241Sbostic char *memf, *nlistf;
5153665Sbostic kvm_t *kd;
5259577Sbostic char buf[5];
536019Swnj
5452241Sbostic memf = nlistf = NULL;
5548966Sbostic while ((ch = getopt(argc, argv, "M:N:")) != EOF)
5648966Sbostic switch(ch) {
5748966Sbostic case 'M':
5852241Sbostic memf = optarg;
596019Swnj break;
6048966Sbostic case 'N':
6152241Sbostic nlistf = optarg;
6248966Sbostic break;
6348966Sbostic case '?':
6448966Sbostic default:
6548966Sbostic usage();
666019Swnj }
6748966Sbostic argc -= optind;
6848966Sbostic argv += optind;
6948966Sbostic
7052241Sbostic /*
7152241Sbostic * Discard setgid privileges if not the running kernel so that bad
7252241Sbostic * guys can't print interesting stuff from kernel memory.
7352241Sbostic */
7452241Sbostic if (memf != NULL || nlistf != NULL)
7552241Sbostic setgid(getgid());
7652241Sbostic
7748966Sbostic /* Read in kernel message buffer, do sanity checks. */
7859577Sbostic if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
7959577Sbostic exit (1);
8053665Sbostic if (kvm_nlist(kd, nl) == -1)
8159445Sbostic errx(1, "kvm_nlist: %s", kvm_geterr(kd));
8248966Sbostic if (nl[X_MSGBUF].n_type == 0)
8359445Sbostic errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist");
8453665Sbostic if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
8559445Sbostic errx(1, "kvm_read: %s", kvm_geterr(kd));
8653665Sbostic kvm_close(kd);
8748966Sbostic if (cur.msg_magic != MSG_MAGIC)
8859445Sbostic errx(1, "magic number incorrect");
8948966Sbostic if (cur.msg_bufx >= MSG_BSIZE)
9048966Sbostic cur.msg_bufx = 0;
9148966Sbostic
9248966Sbostic /*
9348966Sbostic * The message buffer is circular; start at the read pointer, and
9448966Sbostic * go to the write pointer - 1.
9548966Sbostic */
9648966Sbostic p = cur.msg_bufc + cur.msg_bufx;
9748966Sbostic ep = cur.msg_bufc + cur.msg_bufx - 1;
9848966Sbostic for (newl = skip = 0; p != ep; ++p) {
9948966Sbostic if (p == cur.msg_bufc + MSG_BSIZE)
10048966Sbostic p = cur.msg_bufc;
10148966Sbostic ch = *p;
10248966Sbostic /* Skip "\n<.*>" syslog sequences. */
10348966Sbostic if (skip) {
10448966Sbostic if (ch == '>')
10548966Sbostic newl = skip = 0;
10648966Sbostic continue;
10748966Sbostic }
10848966Sbostic if (newl && ch == '<') {
10948966Sbostic skip = 1;
11048966Sbostic continue;
11148966Sbostic }
11248966Sbostic if (ch == '\0')
11348966Sbostic continue;
11453665Sbostic newl = ch == '\n';
11559445Sbostic (void)vis(buf, ch, 0, 0);
11653665Sbostic if (buf[1] == 0)
11759445Sbostic (void)putchar(buf[0]);
11853665Sbostic else
11959445Sbostic (void)printf("%s", buf);
12048966Sbostic }
12148966Sbostic if (!newl)
12248966Sbostic (void)putchar('\n');
12348966Sbostic exit(0);
1246019Swnj }
1256019Swnj
12648966Sbostic void
usage()12748966Sbostic usage()
12848966Sbostic {
12948966Sbostic (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
13048966Sbostic exit(1);
1316019Swnj }
132