1SYSLOG(2) Linux Programmer's Manual SYSLOG(2)
2
3
4
6 syslog, klogctl - read and/or clear kernel message ring buffer; set
7 console_loglevel
8
10 /* The glibc interface */
11 #include <sys/klog.h>
12
13 int klogctl(int type, char *bufp, int len);
14
15 /* The handcrafted system call */
16 #include <unistd.h>
17 #include <linux/unistd.h>
18 #include <errno.h>
19
20 _syscall3(int, syslog, int, type, char *, bufp, int, len)
21 /* Using syscall(2) may be preferable; see intro(2) */
22
23 int syslog(int type, char *bufp, int len);
24
26 If you need the libc function syslog(), (that talks to syslogd(8)),
27 then look at syslog(3). The system call of this name is about control‐
28 ling the kernel printk() buffer, and the glibc version is called
29 klogctl().
30
31 The type argument determines the action taken by this function.
32
33 Quoting from kernel/printk.c:
34 /*
35 * Commands to sys_syslog:
36 *
37 * 0 -- Close the log. Currently a NOP.
38 * 1 -- Open the log. Currently a NOP.
39 * 2 -- Read from the log.
40 * 3 -- Read up to the last 4k of messages in the ring buffer.
41 * 4 -- Read and clear last 4k of messages in the ring buffer
42 * 5 -- Clear ring buffer.
43 * 6 -- Disable printk's to console
44 * 7 -- Enable printk's to console
45 * 8 -- Set level of messages printed to console
46 * 9 -- Return number of unread characters in the log buffer
47 */
48
49 Only function 3 is allowed to non-root processes. (Function 9 was
50 added in 2.4.10.)
51
52 The kernel log buffer
53 The kernel has a cyclic buffer of length LOG_BUF_LEN (4096, since
54 1.3.54: 8192, since 2.1.113: 16384; in recent kernels the size can be
55 set at compile time) in which messages given as argument to the kernel
56 function printk() are stored (regardless of their loglevel).
57
58 The call syslog() (2,buf,len) waits until this kernel log buffer is
59 nonempty, and then reads at most len bytes into the buffer buf. It
60 returns the number of bytes read. Bytes read from the log disappear
61 from the log buffer: the information can only be read once. This is
62 the function executed by the kernel when a user program reads
63 /proc/kmsg.
64
65 The call syslog() (3,buf,len) will read the last len bytes from the log
66 buffer (nondestructively), but will not read more than was written into
67 the buffer since the last `clear ring buffer' command (which does not
68 clear the buffer at all). It returns the number of bytes read.
69
70 The call syslog() (4,buf,len) does precisely the same, but also exe‐
71 cutes the `clear ring buffer' command.
72
73 The call syslog() (5,dummy,idummy) only executes the `clear ring buf‐
74 fer' command.
75
76 The loglevel
77 The kernel routine printk() will only print a message on the console,
78 if it has a loglevel less than the value of the variable con‐
79 sole_loglevel. This variable initially has the value DEFAULT_CON‐
80 SOLE_LOGLEVEL (7), but is set to 10 if the kernel command line contains
81 the word `debug', and to 15 in case of a kernel fault (the 10 and 15
82 are just silly, and equivalent to 8). This variable is set (to a value
83 in the range 1-8) by the call syslog() (8,dummy,value). The calls sys‐
84 log() (type,dummy,idummy) with type equal to 6 or 7, set it to 1 (ker‐
85 nel panics only) or 7 (all except debugging messages), respectively.
86
87 Every text line in a message has its own loglevel. This level is
88 DEFAULT_MESSAGE_LOGLEVEL - 1 (6) unless the line starts with <d> where
89 d is a digit in the range 1-7, in which case the level is d. The con‐
90 ventional meaning of the loglevel is defined in <linux/kernel.h> as
91 follows:
92
93 #define KERN_EMERG "<0>" /* system is unusable */
94 #define KERN_ALERT "<1>" /* action must be taken immediately */
95 #define KERN_CRIT "<2>" /* critical conditions */
96 #define KERN_ERR "<3>" /* error conditions */
97 #define KERN_WARNING "<4>" /* warning conditions */
98 #define KERN_NOTICE "<5>" /* normal but significant condition */
99 #define KERN_INFO "<6>" /* informational */
100 #define KERN_DEBUG "<7>" /* debug-level messages */
101
102
104 In case of error, -1 is returned, and errno is set. Otherwise, for type
105 equal to 2, 3 or 4, syslog() returns the number of bytes read, and oth‐
106 erwise 0.
107
109 EINVAL Bad parameters.
110
111 EPERM An attempt was made to change console_loglevel or clear the ker‐
112 nel message ring buffer by a process without root permissions.
113
114 ERESTARTSYS
115 System call was interrupted by a signal; nothing was read.
116 (This can be seen only during a trace.)
117
119 This system call is Linux specific and should not be used in programs
120 intended to be portable.
121
123 From the very start people noted that it is unfortunate that kernel
124 call and library routine of the same name are entirely different ani‐
125 mals. In libc4 and libc5 the number of this call was defined by
126 SYS_klog. In glibc 2.0 the syscall is baptised klogctl().
127
128
130 syslog(3)
131
132
133
134Linux 1.2.9 2001-11-25 SYSLOG(2)