syslog

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

syslog, klogctl - カーネルのメッセージリングバッファーを読んだり消去したりする; console_loglevel の設定を行う  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <sys/syscall.h>     /* Definition of SYS_* constants */
#include <unistd.h>

int syscall(SYS_syslog, int type, char *bufp, int size);

 /* glibc インターフェース */
#include <sys/klog.h>

int klogctl(int type, char *bufp, int size);
 

説明

注意: おそらく、あなたが探しているのは、 syslogd(8) と通信する C ライブラリ関数の syslog() でしょう。詳細は syslog(3) を参照のこと。

このページはカーネルの syslog() システムコールについて説明している。 syslog() システムコールはカーネルの printk() バッファーを制御するために使用される。 このシステムコールに対する glibc ラッパー関数は klogctl() と呼ばれている。  

カーネルログバッファー (kernel log buffer)

The kernel has a cyclic buffer of size LOG_BUF_LEN in which messages given as arguments to the kernel function printk() are stored (regardless of their log level). In early kernels, LOG_BUF_LEN had the value 4096; from Linux 1.3.54, it was 8192; from Linux 2.1.113, it was 16384; since Linux 2.4.23/2.6, the value is a kernel configuration option (CONFIG_LOG_BUF_SHIFT, default value dependent on the architecture). Since Linux 2.6.6, the size can be queried with command type 10 (see below).  

コマンド

type 引数はこの関数が行う動作を決定する。 以下のリストに示す値を type に指定できる。 シンボル名はカーネルソースで定義されているが、ユーザー空間には公開されていない。 したがって、数字を使うか、名前を自分で定義する必要がある。
SYSLOG_ACTION_CLOSE (0)
ログをクローズする。現在のところ NOP である。
SYSLOG_ACTION_OPEN (1)
ログをオープンする。現在のところ NOP である。
SYSLOG_ACTION_READ (2)
ログを読み出す。 この呼び出しは、 カーネルログバッファーが空でなくなるまで待って、 最大 len バイトまで bufp が指すバッファーに読み込み、 読み込んだバイト数を返す。 ログから読まれたバイトはログバッファーから消える。 つまり、情報は一度しか読むことができない。 これはユーザーのプログラムが /proc/kmsg を読んだ時にカーネルによって実行される関数でもある。
SYSLOG_ACTION_READ_ALL (3)
リングバッファーに残っているメッセージをすべて読み出し、 bufp が指すバッファーに格納する。 この呼び出しログバッファーの最後の len バイトを (非破壊的に) 読み出すが、 最後の「リングバッファー消去」命令 (下記のコマンド 5 参照) 以降にバッファーに書き込まれた情報しか読み出せない。 返り値は読み込んだバイト数である。
SYSLOG_ACTION_READ_CLEAR (4)
リングバッファーに残っているメッセージをすべて読み出し、クリアする。 この呼び出しは type 3 と全く同じことを行い、追加で「リングバッファー消去」 ("clear ring buffer") コマンドも実行する。
SYSLOG_ACTION_CLEAR (5)
The call executes just the "clear ring buffer" command. The bufp and size arguments are ignored.
このコマンドは実際にリングバッファーをクリアするわけではなく、 コマンド 3 (SYSLOG_ACTION_READ_ALL) と 4 (SYSLOG_ACTION_READ_CLEAR で返す内容を決定するカーネルの管理変数を設定する。 このコマンドはコマンド 2 (SYSLOG_ACTION_READ) と 9 (SYSLOG_ACTION_SIZE_UNREAD) には影響を与えない。
SYSLOG_ACTION_CONSOLE_OFF (6)
このコマンドは console_loglevel の現在の値を保存し、それから console_loglevel を minimum_console_loglevel に設定する。 これにより、コンソールにメッセージが出力されなくなる。 Linux 2.6.32 より前では、 このコマンドは console_loglevel を minimum_console_loglevel に設定するだけであった。 下記の /proc/sys/kernel/printk の議論を参照。
The bufp and size arguments are ignored.
SYSLOG_ACTION_CONSOLE_ON (7)
直前に SYSLOG_ACTION_CONSOLE_OFF コマンドがされた場合、 このコマンドは console_loglevel を前のコマンドが保存した値に戻す。 Linux 2.6.32 より前では、 このコマンドは単に console_loglevel を default_console_loglevel に設定するだけであった。 下記の /proc/sys/kernel/printk の議論を参照。
The bufp and size arguments are ignored.
SYSLOG_ACTION_CONSOLE_LEVEL (8)
The call sets console_loglevel to the value given in size, which must be an integer between 1 and 8 (inclusive). The kernel silently enforces a minimum value of minimum_console_loglevel for size. See the log level section for details. The bufp argument is ignored.
SYSLOG_ACTION_SIZE_UNREAD (9) (Linux 2.4.10 以降)
The call returns the number of bytes currently available to be read from the kernel log buffer via command 2 (SYSLOG_ACTION_READ). The bufp and size arguments are ignored.
SYSLOG_ACTION_SIZE_BUFFER (10) (Linux 2.6.6 以降)
This command returns the total size of the kernel log buffer. The bufp and size arguments are ignored.

All commands except 3 and 10 require privilege. In Linux kernels before Linux 2.6.37, command types 3 and 10 are allowed to unprivileged processes; since Linux 2.6.37, these commands are allowed to unprivileged processes only if /proc/sys/kernel/dmesg_restrict has the value 0. Before Linux 2.6.37, "privileged" means that the caller has the CAP_SYS_ADMIN capability. Since Linux 2.6.37, "privileged" means that the caller has either the CAP_SYS_ADMIN capability (now deprecated for this purpose) or the (new) CAP_SYSLOG capability.  

/proc/sys/kernel/printk

/proc/sys/kernel/printk は書き込み可能なファイルで、 エラーメッセージのログ出力を行う際にカーネルの printk() の動作に影響を持つ 4 つの整数値が入っている。 4 つの値は以下のとおりである。
console_loglevel
Only messages with a log level lower than this value will be printed to the console. The default value for this field is DEFAULT_CONSOLE_LOGLEVEL (7), but it is set to 4 if the kernel command line contains the word "quiet", 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). The value of console_loglevel can be set (to a value in the range 1-8) by a syslog() call with a type of 8.
default_message_loglevel
This value will be used as the log level for printk() messages that do not have an explicit level. Up to and including Linux 2.6.38, the hard-coded default value for this field was 4 (KERN_WARNING); since Linux 2.6.39, the default value is defined by the kernel configuration option CONFIG_DEFAULT_MESSAGE_LOGLEVEL, which defaults to 4.
minimum_console_loglevel
このフィールドの値は console_loglevel に設定できる最小値である。
default_console_loglevel
console_loglevel のデフォルト値である。
 

ログレベル

すべての printk() メッセージにはそれぞれログレベルがある。 ログレベルがメッセージの一部として明示的に指定されなかった場合は、 ログレベルは default_message_loglevel になる。ログレベルの一般的な意味は以下のとおりである。
カーネル定数レベル値意味
KERN_EMERG0システムが使用不可
KERN_ALERT1 直ちに対応しなければならない
KERN_CRIT2危険な状況
KERN_ERR3エラー状況
KERN_WARNING4警告状況
KERN_NOTICE5 通常だが重要な状況
KERN_INFO6参考情報
KERN_DEBUG7デバッグレベルのメッセージ

カーネルの printk() ルーチンは、メッセージのログレベルが console_loglevel よりも小さい値の場合にのみ、 メッセージをコンソールに出力する。  

返り値

type が 2, 3, 4 の場合、成功すると syslog() は読み出したバイト数を返す。 type が 9 の場合、 カーネルログバッファーにある現在読み出し可能なバイト数を返す。 type が 10 の場合、 カーネルログバッファーの総量を返す。 type がそれ以外の値の場合、成功すると 0 が返される。

エラーの場合は、-1 が返り、 errno にエラーを示す値が設定される。  

エラー

EINVAL
Bad arguments (e.g., bad type; or for type 2, 3, or 4, buf is NULL, or size is less than zero; or for type 8, the level is outside the range 1 to 8).
ENOSYS
カーネルの設定オプション CONFIG_PRINTK を無効にしてカーネルがコンパイルされているため、 syslog() システムコールが利用できない。
EPERM
十分な権限を持たないプロセス (正確にはケーパビリティ CAP_SYS_ADMIN も CAP_SYSLOG も持たないプロセス) が console_loglevel を変更しようとしたか、 カーネルメッセージリングを消去しようとした。
ERESTARTSYS
システムコールがシグナルによって割り込まれ、何も読み出せなかった。 (トレース中にしか発生することはない)
 

標準規格

Linux.  

歴史

かなり初期の頃から、同じ名前を持つシステムコールとライブラリルーチンが 全く異なる別物であるのは不幸なことだと指摘されてきた。  

関連項目


 dmesg(1), syslog(3), capabilities(7)


 

Index

名前
ライブラリ
書式
説明
カーネルログバッファー (kernel log buffer)
コマンド
/proc/sys/kernel/printk
ログレベル
返り値
エラー
標準規格
歴史
関連項目

This document was created by man2html, using the manual pages.