syslog() - Unix、Linux 系統呼叫
廣告
名稱syslog、klogctl - 讀取和/或清除核心訊息環形緩衝區;設定 console_loglevel概要
int syslog(int type, char *bufp, int len);
/* No wrapper provided in glibc */
/* The glibc interface */
#include <sys/klog.h>
int klogctl(int type, char *bufp, int len);
|
描述如果您需要 libc 函式 syslog()(它與 syslogd(8) 通訊),請參閱 syslog(3)。此名稱的系統呼叫與控制核心 printk() 緩衝區有關,而 glibc 版本稱為 klogctl()。type 引數確定此函式執行的操作。 引用自 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 up to the last 4k of messages in the ring buffer.
* 4 -- Read and clear last 4k of messages in the ring buffer
* 5 -- Clear ring buffer.
* 6 -- Disable printk’s to console
* 7 -- Enable printk’s to console
* 8 -- Set level of messages printed to console
* 9 -- Return number of unread characters in the log buffer
*/
|
只有函式 3 允許非 root 程序使用。(函式 9 在 2.4.10 中新增。)
核心日誌緩衝區 核心有一個長度為 LOG_BUF_LEN(4096,自 1.3.54 起:8192,自 2.1.113 起:16384;在最近的核心中,大小可以在編譯時設定)的迴圈緩衝區,其中儲存傳遞給核心函式 printk() 的訊息作為引數(無論其日誌級別如何)。 呼叫 syslog() (2,buf,len) 會等待此核心日誌緩衝區變為非空,然後最多讀取 len 位元組到緩衝區 buf 中。它返回讀取的位元組數。從日誌中讀取的位元組會從日誌緩衝區中消失:資訊只能讀取一次。這是核心在使用者程式讀取 /proc/kmsg 時執行的函式。 呼叫 syslog() (3,buf,len) 將從日誌緩衝區讀取最後 len 個位元組(非破壞性),但不會讀取自上次“清除環形緩衝區”命令(根本不會清除緩衝區)以來寫入緩衝區的更多位元組。它返回讀取的位元組數。 呼叫 syslog() (4,buf,len) 執行完全相同的操作,但也會執行“清除環形緩衝區”命令。 呼叫 syslog() (5,dummy,idummy) 只執行“清除環形緩衝區”命令。
日誌級別 核心例程 printk() 僅在訊息的日誌級別小於變數 console_loglevel 的值時才會在控制檯上列印訊息。此變數最初的值為 DEFAULT_CONSOLE_LOGLEVEL(7),但如果核心命令列包含單詞“debug”,則將其設定為 10,如果發生核心故障,則設定為 15(10 和 15 只是胡說八道,等效於 8)。此變數由呼叫 syslog() (8,dummy,value) 設定(設定為 1-8 範圍內的值)。type 等於 6 或 7 的 syslog() (type,dummy,idummy) 呼叫分別將其設定為 1(僅核心恐慌)或 7(除除錯訊息之外的所有訊息)。 訊息中的每一行文字都有自己的日誌級別。除非該行以 <d> 開頭,其中 d 是 1-7 範圍內的數字,在這種情況下級別為 d,否則此級別為 DEFAULT_MESSAGE_LOGLEVEL - 1(6)。日誌級別的常規含義在 <linux/kernel.h> 中定義如下
#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 */
|
返回值如果發生錯誤,則返回 -1,並設定 errno。否則,對於 type 等於 2、3 或 4,syslog() 返回讀取的位元組數,否則返回 0。錯誤
標籤 | 描述 |
EINVAL | 引數錯誤。 |
EPERM | 程序嘗試更改 console_loglevel 或清除核心訊息環形緩衝區,但沒有 root 許可權。 |
ERESTARTSYS | | 系統呼叫被訊號中斷;未讀取任何內容。(這隻能在跟蹤期間看到。) |
符合標準此係統呼叫是 Linux 特定的,不應在旨在可移植的程式中使用。註釋從一開始,人們就注意到,核心呼叫和同名庫例程是完全不同的東西,這很不幸。在 libc4 和 libc5 中,此呼叫的編號由 SYS_klog 定義。在 glibc 2.0 中,系統呼叫被命名為 klogctl()。參見
廣告
|