syslogのヘルプ・マニュアル
日本語 英語
syslog --help
man syslog
SYSLOG(2) Linux Programmer’s Manual SYSLOG(2)
名前
syslog, klogctl - カーネルのメッセージ・リング・バッファを読んだり消去
したりする; console_loglevel の設定を行う
書式
int syslog(int type, char *bufp, int len);
/* glibc ではラッパー関数は提供されていない */
/* glibc のインタフェース */
#include
int klogctl(int type, char *bufp, int len);
説明
(syslogd(8) と話す) C ライブラリ関数の syslog() が必要な場合は 、 sys-
log(3) を見ること。この名前のシステム・コールはカーネルの printk() バッ
ファを制御するものであり、glibc では klogctl() と呼ばれている。
type 引き数はこの関数の動作を決定する。
kernel/printk.c からの引用 :
/*
* Commands to sys_syslog:
*
* 0 -- ログを閉じる。現在の実装では何もしない (NOP) 。
* 1 -- ログを開く。現在の実装では何もしない (NOP) 。
* 2 -- ログから読み出す。
* 3 -- リング・バッファに残っているメッセージをすべて読み出す。
* 4 -- リング・バッファに残っているメッセージをすべて読み出し、消去する。
* 5 -- リングバッファを消去する。
* 6 -- コンソールへの printk を無効にする。
* 7 -- コンソールへの printk を有効にする。
* 8 -- コンソールに表示されるメッセージのレベルを設定する。
* 9 -- ログバッファの未読の文字数を返す。
* 10 -- ログバッファのサイズを返す。
*/
非特権プロセスにはコマンド 3 と 10 のみが許可されている (コマンド 9 は
Linux 2.4.10 で、コマンド 10 は Linux 2.6.6 で追加された)。
カーネル・ログ・バッファ (kernel log buffer)
カーネルは長さ LOG_BUF_LEN の巡回式のバッファを持っており、それにはカー
ネル関数の printk() の引き数として与えられたメッセージが (そのログレ ベ
ル にかかわらず) 格納される。初期のカーネルでは LOG_BUF_LEN の値は 4096
であった。カーネル 1.3.54 からは 8192、カーネル 2.1.113 からは 16384 に
なり、カーネル 2.4.23 以降および 2.6 以降ではカーネルのコンパイル時に値
を設定できるようになっている。最近のカーネルでは、コマンド 10 でバッ フ
ァのサイズを問い合わせできる。
syslog(2,buf,len) の呼び出しはカーネル・ログ・バッファが空でなくなるま
で待って、最大 len バイトまで buf へと読み出し、読み込んだバイト数を 返
す 。ログから読まれたバイトはログ・バッファから消える: 情報は一度しか読
むことができない。これはユーザーのプログラムが /proc/kmsg を読んだ時 に
カーネルによって実行される関数でもある。
syslog(3,buf,len) の呼び出しはログ・バッファの最後の len バイトを (非破
壊的に)読み出す、しかし、直近の「リング・バッファ消去」命令 (この命令は
バ ッファを消去するわけではない) 以降にバッファに書き込まれた情報しか読
み出せない。返り値は読み込んだバイト数である。
syslog(4,buf,len) 呼び出しは「リング・バッファ消去」命令も実行する以 外
は機能 3 と完全に同じである。
syslog(5,dummy,dummy) 呼び出しは「リング・バッファ消去」命令のみを実行
する (呼び出しの書式で、 buf や len が "dummy" と記載されている場合、そ
の引き数の値が無視されることを表す)。
syslog(6,dummy,dummy) 呼び出しはコンソールのログレベルを最小に設定し、
コンソールにメッセージが表示されないようにする。
syslog(7,dummy,dummy) 呼び出しはコンソールのログレベルをデフォルトに 設
定し、コンソールにメッセージが表示されるようにする。
syslog(8,dummy,level) 呼び出しはコンソールのログレベルを level に設定す
る。 level は 1 以上 8 以下の整数でなければならない。詳細は ログレベ ル
(loglevel) の節を参照のこと。
syslog(9,dummy,dummy) 呼び出しはカーネル・ログバッファにある現在読み出
し可能なバイト数を返す。
syslog(10,dummy,dummy) 呼び出しはカーネル・ログバッファの総量を返す。
ログレベル (loglevel)
カーネル・ルーチンの printk() は、ログレベルが console_loglevel 変数 よ
り小さいときにのみ、コンソールにメッセージを表示する。 console_loglevel
は最初 DEFAULT_CONSOLE_LOGLEVEL (7) に設定されるが、起動時にカーネル の
コ マ ンド・ライン・オプションに "debug" という単語が含まれている場合は
10 に設定され、カーネル・フォールトが発生した場合には 15 に設定される (
但し、10 や 15 という数字に意味はなく、8 と同等である)。この変数は sys-
log(8,dummy,value). 呼び出しによって設定され、値の範囲は 1-8 である 。
syslog(type,dummy,dummy) 呼び出しで type が 6 もしくは 7 の場合、 con-
sole_loglevel は 1 (カーネル・パニックのみ)、 7 (デバッグ・メッセージ以
外の全て) にそれぞれ設定される。
メ ッ セ ー ジ の 各 行 は それぞれにログレベルを持つ。このログレベルは
DEFAULT_MESSAGE_LOGLEVEL - 1 (6) であるが、 (d は 1-7 の範囲の数字)
で 始 ま る 行 の ロ グ レ ベ ルは d である。ログレベルの慣習的な意味は
に以下のように定義されている:
#define KERN_EMERG "<0>" /* システムが使用不能 */
#define KERN_ALERT "<1>" /* 直ちに対処が必要 */
#define KERN_CRIT "<2>" /* 致命的な状態 */
#define KERN_ERR "<3>" /* エラー状態 */
#define KERN_WARNING "<4>" /* 警告状態 */
#define KERN_NOTICE "<5>" /* 通常状態だが大事な情報 */
#define KERN_INFO "<6>" /* 通知 */
#define KERN_DEBUG "<7>" /* デバッグレベルの情報 */
返り値
type が 2, 3, 4 の場合、成功すると syslog() は読み出したバイト数を返 す
。 type が 9 の場合、カーネル・ログバッファにある現在読み出し可能なバイ
ト数を返す。 type が 10 の場合、カーネル・ログバッファの総量を 返 す 。
type がそれ以外の値の場合、成功すると 0 が返される。
エラーの場合は、-1 が返り、 errno にエラーを示す値が設定される。
エラー
EINVAL 不 正な引き数 (具体的には、 type が正しくない、もしくは type が
2, 3, 4 の場合に buf が NULL か len が 0 未満である、も し く は
type が 8 の場合に level が 1 以上 8 以下の範囲に入っていない)。
EPERM 十分な権限を持たないプロセス (正確には CAP_SYS_ADMIN ケーパビ リ
テ ィを持たないプロセス) が console_loglevelを変更しようとしたか
、カーネル・メッセージ・リングを消去しようとした。
ERESTARTSYS
システム・コールがシグナルによって割り込まれ、何も読み出せなかっ
た。 (トレース中にしか発生することはない)
ENOSYS カーネルの設定オプション CONFIG_PRINTK を無効にしてカーネルがコ
ンパイルされているため、 syslog() システムコールが利用できない。
準拠
このシステム・コールは Linux 特有であり、移植を意図したプログラムでは使
用してはいけない。
注意
かなり初期の頃から、同じ名前を持つシステム・コールとライブラリ・ルー チ
ン が 全 く 異なる代物であるのは不幸なことだと気付かれていた。 libc4 と
libc5 ではこのコールの番号は SYS_klog と定義されていた。 glibc2.0 で こ
のシステムコールは klogctl() という名前に改められた。
関連項目
syslog(3)
Linux 2008-06-20 SYSLOG(2)
SYSLOG(2) Linux Programmer’s Manual SYSLOG(2)
NAME
syslog, klogctl - read and/or clear kernel message ring buffer; set
console_loglevel
SYNOPSIS
int syslog(int type, char *bufp, int len);
/* No wrapper provided in glibc */
/* The glibc interface */
#include
int klogctl(int type, char *bufp, int len);
DESCRIPTION
If you need the C library function syslog() (which talks to sys-
logd(8)), then look at syslog(3). The system call of this name is
about controlling the kernel printk() buffer, and the glibc version is
called klogctl().
The type argument determines the action taken by this function.
Quoting from kernel/printk.c:
/*
* Commands to sys_syslog:
*
* 0 -- Close the log. Currently a NOP.
* 1 -- Open the log. Currently a NOP.
* 2 -- Read from the log.
* 3 -- Read all messages remaining in the ring buffer.
* 4 -- Read and clear all messages remaining in the ring buffer
* 5 -- Clear ring buffer.
* 6 -- Disable printk to console
* 7 -- Enable printk to console
* 8 -- Set level of messages printed to console
* 9 -- Return number of unread characters in the log buffer
* 10 -- Return size of the log buffer
*/
Only command types 3 and 10 are allowed to unprivileged processes.
Type 9 was added in 2.4.10; type 10 in 2.6.6.
The kernel log buffer
The kernel has a cyclic buffer of length LOG_BUF_LEN in which messages
given as arguments to the kernel function printk() are stored (regard-
less of their loglevel). In early kernels, LOG_BUF_LEN had the value
4096; from kernel 1.3.54, it was 8192; from kernel 2.1.113 it was
16384; since 2.4.23/2.6 the value is a kernel configuration option. In
recent kernels the size can be queried with command type 10.
The call syslog(2,buf,len) waits until this kernel log buffer is non-
empty, and then reads at most len bytes into the buffer buf. It
returns the number of bytes read. Bytes read from the log disappear
from the log buffer: the information can only be read once. This is
the function executed by the kernel when a user program reads
/proc/kmsg.
The call syslog(3,buf,len) will read the last len bytes from the log
buffer (non-destructively), but will not read more than was written
into the buffer since the last "clear ring buffer" command (which does
not clear the buffer at all). It returns the number of bytes read.
The call syslog(4,buf,len) does precisely the same, but also executes
the "clear ring buffer" command.
The call syslog(5,dummy,dummy) executes just the "clear ring buffer"
command. (In each call where buf or len is shown as "dummy", the value
of the argument is ignored by the call.)
The call syslog(6,dummy,dummy) sets the console log level to minimum,
so that no messages are printed to the console.
The call syslog(7,dummy,dummy) sets the console log level to default,
so that messages are printed to the console.
The call syslog(8,dummy,level) sets the console log level to level,
which must be an integer between 1 and 8 (inclusive). See the loglevel
section for details.
The call syslog(9,dummy,dummy) returns the number of bytes currently
available to be read on the kernel log buffer.
The call syslog(10,dummy,dummy) returns the total size of the kernel
log buffer.
The loglevel
The kernel routine printk() will only print a message on the console,
if it has a loglevel less than the value of the variable con-
sole_loglevel. This variable initially has the value DEFAULT_CON-
SOLE_LOGLEVEL (7), but is set to 10 if the kernel command line contains
the word "debug", and to 15 in case of a kernel fault (the 10 and 15
are just silly, and equivalent to 8). This variable is set (to a value
in the range 1-8) by the call syslog(8,dummy,value). The calls sys-
log(type,dummy,dummy) with type equal to 6 or 7, set it to 1 (kernel
panics only) or 7 (all except debugging messages), respectively.
Every text line in a message has its own loglevel. This level is
DEFAULT_MESSAGE_LOGLEVEL - 1 (6) unless the line starts with where
d is a digit in the range 1-7, in which case the level is d. The con-
ventional meaning of the loglevel is defined in as
follows:
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */
RETURN VALUE
For type equal to 2, 3, or 4, a successful call to syslog() returns the
number of bytes read. For type 9, syslog() returns the number of bytes
currently available to be read on the kernel log buffer. For type 10,
syslog() returns the total size of the kernel log buffer. For other
values of type, 0 is returned on success.
In case of error, -1 is returned, and errno is set to indicate the
error.
ERRORS
EINVAL Bad arguments (e.g., bad type; or for type 2, 3, or 4, buf is
NULL, or len is less than zero; or for type 8, the level is out-
side the range 1 to 8).
EPERM An attempt was made to change console_loglevel or clear the ker-
nel message ring buffer by a process without sufficient privi-
lege (more precisely: without the CAP_SYS_ADMIN capability).
ERESTARTSYS
System call was interrupted by a signal; nothing was read.
(This can be seen only during a trace.)
ENOSYS This syslog() system call is not available, because the kernel
was compiled with the CONFIG_PRINTK kernel-configuration option
disabled.
CONFORMING TO
This system call is Linux-specific and should not be used in programs
intended to be portable.
NOTES
From the very start people noted that it is unfortunate that a system
call and a library routine of the same name are entirely different ani-
mals. In libc4 and libc5 the number of this call was defined by
SYS_klog. In glibc 2.0 the syscall is baptized klogctl().
SEE ALSO
syslog(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-06-20 SYSLOG(2)