void dmesg_main(void)
{
/
/
...省略
if
(!(toys.optflags&FLAG_S)) {
/
/
...省略
fd
=
open
(
"/dev/kmsg"
, O_RDONLY|(O_NONBLOCK
*
!(toys.optflags&FLAG_w)));
if
(fd
=
=
-
1
) goto klogctl_mode;
lseek(fd,
0
, SEEK_DATA);
for
(;;) {
if
(
-
1
=
=
(
len
=
read(fd, msg, sizeof(msg))) && errno
=
=
EPIPE)
continue
;
/
/
read()
from
kmsg always fails on a pre
-
3.5
kernel.
if
(
len
=
=
-
1
&& errno
=
=
EINVAL) goto klogctl_mode;
if
(
len
<
1
)
break
;
msg[
len
]
=
0
;
format_message(msg,
1
);
}
close(fd);
}
else
{
char
*
data,
*
to,
*
from
,
*
end;
int
size;
klogctl_mode:
/
/
Figure out how much data we need,
and
fetch it.
if
(!(size
=
TT.size)) size
=
xklogctl(
10
,
0
,
0
);
data
=
from
=
xmalloc(size
+
1
);
data[size
=
xklogctl(
3
+
(toys.optflags&FLAG_c), data, size)]
=
0
;
/
/
Send each line to format_message.
to
=
data
+
size;
while
(
from
< to) {
if
(!(end
=
memchr(
from
,
'\n'
, to
-
from
)))
break
;
*
end
=
0
;
format_message(
from
,
0
);
from
=
end
+
1
;
}
if
(CFG_TOYBOX_FREE) free(data);
}
no_output:
/
/
Set
the log level?
if
(toys.optflags & FLAG_n) xklogctl(
8
,
0
, TT.level);
/
/
Clear the
buffer
?
if
(toys.optflags & (FLAG_C|FLAG_c)) xklogctl(
5
,
0
,
0
);
}