move compatibility hooks for S_ISREG et al to conf.h
[unix-history] / usr / src / sbin / dmesg / dmesg.c
CommitLineData
0fc6e47b 1/*-
befa51e1 2 * Copyright (c) 1991 The Regents of the University of California.
0fc6e47b
KB
3 * All rights reserved.
4 *
befa51e1 5 * %sccs.include.redist.c%
76797561
DF
6 */
7
8#ifndef lint
0fc6e47b 9char copyright[] =
befa51e1 10"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
0fc6e47b
KB
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
c4fb1efb 15static char sccsid[] = "@(#)dmesg.c 5.14 (Berkeley) %G%";
0fc6e47b 16#endif /* not lint */
76797561 17
befa51e1 18#include <sys/cdefs.h>
34a7981f 19#include <sys/msgbuf.h>
1b207d8b
KB
20#include <fcntl.h>
21#include <limits.h>
befa51e1 22#include <time.h>
a5d34734 23#include <nlist.h>
befa51e1
KB
24#include <kvm.h>
25#include <stdlib.h>
a5d34734 26#include <stdio.h>
1b207d8b
KB
27#include <unistd.h>
28#include <vis.h>
34a7981f 29
befa51e1
KB
30struct nlist nl[] = {
31#define X_MSGBUF 0
b0f3bb00 32 { "_msgbufp" },
befa51e1 33 { NULL },
34a7981f
BJ
34};
35
befa51e1 36void err __P((const char *, ...));
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
KB
51 kvm_t *kd;
52 char buf[_POSIX2_LINE_MAX];
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. */
1b207d8b
KB
78 buf[0] = 0;
79 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, buf);
80 if (kd == NULL)
81 err("kvm_open: %s", buf);
82 if (kvm_nlist(kd, nl) == -1)
83 err("kvm_nlist: %s", kvm_geterr(kd));
befa51e1 84 if (nl[X_MSGBUF].n_type == 0)
24c3fce5 85 err("%s: msgbufp not found", nlistf ? nlistf : "namelist");
1b207d8b
KB
86 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
87 err("kvm_read: %s", kvm_geterr(kd));
88 kvm_close(kd);
befa51e1
KB
89 if (cur.msg_magic != MSG_MAGIC)
90 err("magic number incorrect");
91 if (cur.msg_bufx >= MSG_BSIZE)
92 cur.msg_bufx = 0;
93
94 /*
95 * The message buffer is circular; start at the read pointer, and
96 * go to the write pointer - 1.
97 */
98 p = cur.msg_bufc + cur.msg_bufx;
99 ep = cur.msg_bufc + cur.msg_bufx - 1;
100 for (newl = skip = 0; p != ep; ++p) {
101 if (p == cur.msg_bufc + MSG_BSIZE)
102 p = cur.msg_bufc;
103 ch = *p;
104 /* Skip "\n<.*>" syslog sequences. */
105 if (skip) {
106 if (ch == '>')
107 newl = skip = 0;
108 continue;
109 }
110 if (newl && ch == '<') {
111 skip = 1;
112 continue;
113 }
114 if (ch == '\0')
115 continue;
1b207d8b
KB
116 newl = ch == '\n';
117 (void) vis(buf, ch, 0, 0);
118 if (buf[1] == 0)
119 (void) putchar(buf[0]);
120 else
121 (void) fputs(buf, stdout);
befa51e1
KB
122 }
123 if (!newl)
124 (void)putchar('\n');
125 exit(0);
34a7981f
BJ
126}
127
befa51e1
KB
128#if __STDC__
129#include <stdarg.h>
130#else
131#include <varargs.h>
132#endif
133
134void
135#if __STDC__
136err(const char *fmt, ...)
137#else
138err(fmt, va_alist)
139 char *fmt;
140 va_dcl
141#endif
34a7981f 142{
befa51e1
KB
143 va_list ap;
144#if __STDC__
145 va_start(ap, fmt);
146#else
147 va_start(ap);
148#endif
149 (void)fprintf(stderr, "dmesg: ");
150 (void)vfprintf(stderr, fmt, ap);
151 va_end(ap);
152 (void)fprintf(stderr, "\n");
153 exit(1);
154 /* NOTREACHED */
155}
34a7981f 156
befa51e1
KB
157void
158usage()
159{
160 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
161 exit(1);
34a7981f 162}