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 #include <sys/klog.h> /* Definition of SYSLOG_* constants */
11 #include <sys/syscall.h> /* Definition of SYS_* constants */
12 #include <unistd.h>
13
14 int syscall(SYS_syslog, int type, char *bufp, int len);
15
16 /* The glibc interface */
17 #include <sys/klog.h>
18
19 int klogctl(int type, char *bufp, int len);
20
22 Note: Probably, you are looking for the C library function syslog(),
23 which talks to syslogd(8); see syslog(3) for details.
24
25 This page describes the kernel syslog() system call, which is used to
26 control the kernel printk() buffer; the glibc wrapper function for the
27 system call is called klogctl().
28
29 The kernel log buffer
30 The kernel has a cyclic buffer of length LOG_BUF_LEN in which messages
31 given as arguments to the kernel function printk() are stored (regard‐
32 less of their log level). In early kernels, LOG_BUF_LEN had the value
33 4096; from kernel 1.3.54, it was 8192; from kernel 2.1.113, it was
34 16384; since kernel 2.4.23/2.6, the value is a kernel configuration op‐
35 tion (CONFIG_LOG_BUF_SHIFT, default value dependent on the architec‐
36 ture). Since Linux 2.6.6, the size can be queried with command type 10
37 (see below).
38
39 Commands
40 The type argument determines the action taken by this function. The
41 list below specifies the values for type. The symbolic names are de‐
42 fined in the kernel source, but are not exported to user space; you
43 will either need to use the numbers, or define the names yourself.
44
45 SYSLOG_ACTION_CLOSE (0)
46 Close the log. Currently a NOP.
47
48 SYSLOG_ACTION_OPEN (1)
49 Open the log. Currently a NOP.
50
51 SYSLOG_ACTION_READ (2)
52 Read from the log. The call waits until the kernel log buffer
53 is nonempty, and then reads at most len bytes into the buffer
54 pointed to by bufp. The call returns the number of bytes read.
55 Bytes read from the log disappear from the log buffer: the in‐
56 formation can be read only once. This is the function executed
57 by the kernel when a user program reads /proc/kmsg.
58
59 SYSLOG_ACTION_READ_ALL (3)
60 Read all messages remaining in the ring buffer, placing them in
61 the buffer pointed to by bufp. The call reads the last len
62 bytes from the log buffer (nondestructively), but will not read
63 more than was written into the buffer since the last "clear ring
64 buffer" command (see command 5 below)). The call returns the
65 number of bytes read.
66
67 SYSLOG_ACTION_READ_CLEAR (4)
68 Read and clear all messages remaining in the ring buffer. The
69 call does precisely the same as for a type of 3, but also exe‐
70 cutes the "clear ring buffer" command.
71
72 SYSLOG_ACTION_CLEAR (5)
73 The call executes just the "clear ring buffer" command. The
74 bufp and len arguments are ignored.
75
76 This command does not really clear the ring buffer. Rather, it
77 sets a kernel bookkeeping variable that determines the results
78 returned by commands 3 (SYSLOG_ACTION_READ_ALL) and 4 (SYS‐
79 LOG_ACTION_READ_CLEAR). This command has no effect on commands
80 2 (SYSLOG_ACTION_READ) and 9 (SYSLOG_ACTION_SIZE_UNREAD).
81
82 SYSLOG_ACTION_CONSOLE_OFF (6)
83 The command saves the current value of console_loglevel and then
84 sets console_loglevel to minimum_console_loglevel, so that no
85 messages are printed to the console. Before Linux 2.6.32, the
86 command simply sets console_loglevel to minimum_con‐
87 sole_loglevel. See the discussion of /proc/sys/kernel/printk,
88 below.
89
90 The bufp and len arguments are ignored.
91
92 SYSLOG_ACTION_CONSOLE_ON (7)
93 If a previous SYSLOG_ACTION_CONSOLE_OFF command has been per‐
94 formed, this command restores console_loglevel to the value that
95 was saved by that command. Before Linux 2.6.32, this command
96 simply sets console_loglevel to default_console_loglevel. See
97 the discussion of /proc/sys/kernel/printk, below.
98
99 The bufp and len arguments are ignored.
100
101 SYSLOG_ACTION_CONSOLE_LEVEL (8)
102 The call sets console_loglevel to the value given in len, which
103 must be an integer between 1 and 8 (inclusive). The kernel
104 silently enforces a minimum value of minimum_console_loglevel
105 for len. See the log level section for details. The bufp argu‐
106 ment is ignored.
107
108 SYSLOG_ACTION_SIZE_UNREAD (9) (since Linux 2.4.10)
109 The call returns the number of bytes currently available to be
110 read from the kernel log buffer via command 2 (SYSLOG_AC‐
111 TION_READ). The bufp and len arguments are ignored.
112
113 SYSLOG_ACTION_SIZE_BUFFER (10) (since Linux 2.6.6)
114 This command returns the total size of the kernel log buffer.
115 The bufp and len arguments are ignored.
116
117 All commands except 3 and 10 require privilege. In Linux kernels be‐
118 fore 2.6.37, command types 3 and 10 are allowed to unprivileged pro‐
119 cesses; since Linux 2.6.37, these commands are allowed to unprivileged
120 processes only if /proc/sys/kernel/dmesg_restrict has the value 0. Be‐
121 fore Linux 2.6.37, "privileged" means that the caller has the
122 CAP_SYS_ADMIN capability. Since Linux 2.6.37, "privileged" means that
123 the caller has either the CAP_SYS_ADMIN capability (now deprecated for
124 this purpose) or the (new) CAP_SYSLOG capability.
125
126 /proc/sys/kernel/printk
127 /proc/sys/kernel/printk is a writable file containing four integer val‐
128 ues that influence kernel printk() behavior when printing or logging
129 error messages. The four values are:
130
131 console_loglevel
132 Only messages with a log level lower than this value will be
133 printed to the console. The default value for this field is DE‐
134 FAULT_CONSOLE_LOGLEVEL [22m(7), but it is set to 4 if the kernel
135 command line contains the word "quiet", 10 if the kernel command
136 line contains the word "debug", and to 15 in case of a kernel
137 fault (the 10 and 15 are just silly, and equivalent to 8). The
138 value of console_loglevel can be set (to a value in the range
139 1–8) by a syslog() call with a type of 8.
140
141 default_message_loglevel
142 This value will be used as the log level for printk() messages
143 that do not have an explicit level. Up to and including Linux
144 2.6.38, the hard-coded default value for this field was 4
145 (KERN_WARNING); since Linux 2.6.39, the default value is a de‐
146 fined by the kernel configuration option CONFIG_DEFAULT_MES‐
147 SAGE_LOGLEVEL, which defaults to 4.
148
149 minimum_console_loglevel
150 The value in this field is the minimum value to which con‐
151 sole_loglevel can be set.
152
153 default_console_loglevel
154 This is the default value for console_loglevel.
155
156 The log level
157 Every printk() message has its own log level. If the log level is not
158 explicitly specified as part of the message, it defaults to de‐
159 fault_message_loglevel. The conventional meaning of the log level is
160 as follows:
161
162 Kernel constant Level value Meaning
163 KERN_EMERG 0 System is unusable
164 KERN_ALERT 1 Action must be taken
165 immediately
166 KERN_CRIT 2 Critical conditions
167 KERN_ERR 3 Error conditions
168 KERN_WARNING 4 Warning conditions
169 KERN_NOTICE 5 Normal but signifi‐
170 cant condition
171 KERN_INFO 6 Informational
172 KERN_DEBUG 7 Debug-level messages
173
174 The kernel printk() routine will print a message on the console only if
175 it has a log level less than the value of console_loglevel.
176
178 For type equal to 2, 3, or 4, a successful call to syslog() returns the
179 number of bytes read. For type 9, syslog() returns the number of bytes
180 currently available to be read on the kernel log buffer. For type 10,
181 syslog() returns the total size of the kernel log buffer. For other
182 values of type, 0 is returned on success.
183
184 In case of error, -1 is returned, and errno is set to indicate the er‐
185 ror.
186
188 EINVAL Bad arguments (e.g., bad type; or for type 2, 3, or 4, buf is
189 NULL, or len is less than zero; or for type 8, the level is out‐
190 side the range 1 to 8).
191
192 ENOSYS This syslog() system call is not available, because the kernel
193 was compiled with the CONFIG_PRINTK kernel-configuration option
194 disabled.
195
196 EPERM An attempt was made to change console_loglevel or clear the ker‐
197 nel message ring buffer by a process without sufficient privi‐
198 lege (more precisely: without the CAP_SYS_ADMIN or CAP_SYSLOG
199 capability).
200
201 ERESTARTSYS
202 System call was interrupted by a signal; nothing was read.
203 (This can be seen only during a trace.)
204
206 This system call is Linux-specific and should not be used in programs
207 intended to be portable.
208
210 From the very start, people noted that it is unfortunate that a system
211 call and a library routine of the same name are entirely different ani‐
212 mals.
213
215 dmesg(1), syslog(3), capabilities(7)
216
218 This page is part of release 5.13 of the Linux man-pages project. A
219 description of the project, information about reporting bugs, and the
220 latest version of this page, can be found at
221 https://www.kernel.org/doc/man-pages/.
222
223
224
225Linux 2021-03-22 SYSLOG(2)