FreeBSD kernel kern code
subr_log.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93
32 */
33
34/*
35 * Error log buffer for kernel printf's.
36 */
37
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/conf.h>
44#include <sys/proc.h>
45#include <sys/vnode.h>
46#include <sys/filio.h>
47#include <sys/ttycom.h>
48#include <sys/msgbuf.h>
49#include <sys/signalvar.h>
50#include <sys/kernel.h>
51#include <sys/poll.h>
52#include <sys/filedesc.h>
53#include <sys/sysctl.h>
54
55#define LOG_RDPRI (PZERO + 1)
56
57#define LOG_ASYNC 0x04
58
59static d_open_t logopen;
60static d_close_t logclose;
61static d_read_t logread;
62static d_ioctl_t logioctl;
63static d_poll_t logpoll;
64static d_kqfilter_t logkqfilter;
65
66static void logtimeout(void *arg);
67
68static struct cdevsw log_cdevsw = {
69 .d_version = D_VERSION,
70 .d_open = logopen,
71 .d_close = logclose,
72 .d_read = logread,
73 .d_ioctl = logioctl,
74 .d_poll = logpoll,
75 .d_kqfilter = logkqfilter,
76 .d_name = "log",
77};
78
79static int logkqread(struct knote *note, long hint);
80static void logkqdetach(struct knote *note);
81
82static struct filterops log_read_filterops = {
83 .f_isfd = 1,
84 .f_attach = NULL,
85 .f_detach = logkqdetach,
86 .f_event = logkqread,
87};
88
89static struct logsoftc {
90 int sc_state; /* see above for possibilities */
91 struct selinfo sc_selp; /* process waiting on select call */
92 struct sigio *sc_sigio; /* information for async I/O */
93 struct callout sc_callout; /* callout to wakeup syslog */
95
96int log_open; /* also used in log() */
97static struct cv log_wakeup;
99MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
100
102SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
104 "How often (times per second) to check for /dev/log waiters.");
105
106/*ARGSUSED*/
107static int
108logopen(struct cdev *dev, int flags, int mode, struct thread *td)
109{
110
111 if (log_wakeups_per_second < 1) {
112 printf("syslog wakeup is less than one. Adjusting to 1.\n");
114 }
115
116 mtx_lock(&msgbuf_lock);
117 if (log_open) {
118 mtx_unlock(&msgbuf_lock);
119 return (EBUSY);
120 }
121 log_open = 1;
122 callout_reset_sbt(&logsoftc.sc_callout,
123 SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
124 mtx_unlock(&msgbuf_lock);
125
126 fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */
127 return (0);
128}
129
130/*ARGSUSED*/
131static int
132logclose(struct cdev *dev, int flag, int mode, struct thread *td)
133{
134
136
137 mtx_lock(&msgbuf_lock);
138 callout_stop(&logsoftc.sc_callout);
139 logsoftc.sc_state = 0;
140 log_open = 0;
141 mtx_unlock(&msgbuf_lock);
142
143 return (0);
144}
145
146/*ARGSUSED*/
147static int
148logread(struct cdev *dev, struct uio *uio, int flag)
149{
150 char buf[128];
151 struct msgbuf *mbp = msgbufp;
152 int error = 0, l;
153
154 mtx_lock(&msgbuf_lock);
155 while (msgbuf_getcount(mbp) == 0) {
156 if (flag & IO_NDELAY) {
157 mtx_unlock(&msgbuf_lock);
158 return (EWOULDBLOCK);
159 }
160 if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
161 mtx_unlock(&msgbuf_lock);
162 return (error);
163 }
164 }
165
166 while (uio->uio_resid > 0) {
167 l = imin(sizeof(buf), uio->uio_resid);
168 l = msgbuf_getbytes(mbp, buf, l);
169 if (l == 0)
170 break;
171 mtx_unlock(&msgbuf_lock);
172 error = uiomove(buf, l, uio);
173 if (error || uio->uio_resid == 0)
174 return (error);
175 mtx_lock(&msgbuf_lock);
176 }
177 mtx_unlock(&msgbuf_lock);
178 return (error);
179}
180
181/*ARGSUSED*/
182static int
183logpoll(struct cdev *dev, int events, struct thread *td)
184{
185 int revents = 0;
186
187 if (events & (POLLIN | POLLRDNORM)) {
188 mtx_lock(&msgbuf_lock);
189 if (msgbuf_getcount(msgbufp) > 0)
190 revents |= events & (POLLIN | POLLRDNORM);
191 else
193 mtx_unlock(&msgbuf_lock);
194 }
195 return (revents);
196}
197
198static int
199logkqfilter(struct cdev *dev, struct knote *kn)
200{
201
202 if (kn->kn_filter != EVFILT_READ)
203 return (EINVAL);
204
205 kn->kn_fop = &log_read_filterops;
206 kn->kn_hook = NULL;
207
208 mtx_lock(&msgbuf_lock);
209 knlist_add(&logsoftc.sc_selp.si_note, kn, 1);
210 mtx_unlock(&msgbuf_lock);
211 return (0);
212}
213
214static int
215logkqread(struct knote *kn, long hint)
216{
217
218 mtx_assert(&msgbuf_lock, MA_OWNED);
219 kn->kn_data = msgbuf_getcount(msgbufp);
220 return (kn->kn_data != 0);
221}
222
223static void
225{
226
227 mtx_lock(&msgbuf_lock);
228 knlist_remove(&logsoftc.sc_selp.si_note, kn, 1);
229 mtx_unlock(&msgbuf_lock);
230}
231
232static void
233logtimeout(void *arg)
234{
235
236 if (!log_open)
237 return;
238 if (msgbuftrigger == 0)
239 goto done;
240 msgbuftrigger = 0;
242 KNOTE_LOCKED(&logsoftc.sc_selp.si_note, 0);
243 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
244 pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
246done:
247 if (log_wakeups_per_second < 1) {
248 printf("syslog wakeup is less than one. Adjusting to 1.\n");
250 }
251 callout_reset_sbt(&logsoftc.sc_callout,
252 SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
253}
254
255/*ARGSUSED*/
256static int
257logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
258{
259
260 switch (com) {
261 /* return number of characters immediately available */
262 case FIONREAD:
263 *(int *)data = msgbuf_getcount(msgbufp);
264 break;
265
266 case FIONBIO:
267 break;
268
269 case FIOASYNC:
270 mtx_lock(&msgbuf_lock);
271 if (*(int *)data)
273 else
274 logsoftc.sc_state &= ~LOG_ASYNC;
275 mtx_unlock(&msgbuf_lock);
276 break;
277
278 case FIOSETOWN:
279 return (fsetown(*(int *)data, &logsoftc.sc_sigio));
280
281 case FIOGETOWN:
282 *(int *)data = fgetown(&logsoftc.sc_sigio);
283 break;
284
285 /* This is deprecated, FIOSETOWN should be used instead. */
286 case TIOCSPGRP:
287 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
288
289 /* This is deprecated, FIOGETOWN should be used instead */
290 case TIOCGPGRP:
291 *(int *)data = -fgetown(&logsoftc.sc_sigio);
292 break;
293
294 default:
295 return (ENOTTY);
296 }
297 return (0);
298}
299
300static void
301log_drvinit(void *unused)
302{
303
304 cv_init(&log_wakeup, "klog");
305 callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
307 make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
308 GID_WHEEL, 0600, "klog");
309}
310
311SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);
void cv_init(struct cv *cvp, const char *desc)
Definition: kern_condvar.c:77
void cv_broadcastpri(struct cv *cvp, int pri)
Definition: kern_condvar.c:424
struct cdev * make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:911
int fsetown(pid_t pgid, struct sigio **sigiop)
void funsetown(struct sigio **sigiop)
pid_t fgetown(struct sigio **sigiop)
void knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
Definition: kern_event.c:2467
void knlist_add(struct knlist *knl, struct knote *kn, int islocked)
Definition: kern_event.c:2420
void knote(struct knlist *list, long hint, int lockflags)
Definition: kern_event.c:2363
void knlist_init_mtx(struct knlist *knl, struct mtx *lock)
Definition: kern_event.c:2564
void pgsigio(struct sigio **sigiop, int sig, int checkctty)
Definition: kern_sig.c:4041
uint32_t * data
Definition: msi_if.m:90
struct sigio * sc_sigio
Definition: subr_log.c:92
int sc_state
Definition: subr_log.c:90
struct callout sc_callout
Definition: subr_log.c:93
struct selinfo sc_selp
Definition: subr_log.c:91
static int log_wakeups_per_second
Definition: subr_log.c:101
SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW, &log_wakeups_per_second, 0, "How often (times per second) to check for /dev/log waiters.")
SYSINIT(logdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, log_drvinit, NULL)
#define LOG_RDPRI
Definition: subr_log.c:55
static d_read_t logread
Definition: subr_log.c:61
static d_close_t logclose
Definition: subr_log.c:60
static struct cv log_wakeup
Definition: subr_log.c:97
#define LOG_ASYNC
Definition: subr_log.c:57
static d_poll_t logpoll
Definition: subr_log.c:63
static void logkqdetach(struct knote *note)
Definition: subr_log.c:224
static int logkqread(struct knote *note, long hint)
Definition: subr_log.c:215
static d_open_t logopen
Definition: subr_log.c:59
__FBSDID("$FreeBSD$")
static void logtimeout(void *arg)
Definition: subr_log.c:233
static d_ioctl_t logioctl
Definition: subr_log.c:62
MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF)
int log_open
Definition: subr_log.c:96
static struct logsoftc logsoftc
struct mtx msgbuf_lock
Definition: subr_log.c:98
static struct filterops log_read_filterops
Definition: subr_log.c:82
static void log_drvinit(void *unused)
Definition: subr_log.c:301
static d_kqfilter_t logkqfilter
Definition: subr_log.c:64
static struct cdevsw log_cdevsw
Definition: subr_log.c:68
int msgbuf_getcount(struct msgbuf *mbp)
Definition: subr_msgbuf.c:133
int msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen)
Definition: subr_msgbuf.c:311
int printf(const char *fmt,...)
Definition: subr_prf.c:397
int msgbuftrigger
Definition: subr_prf.c:127
struct msgbuf * msgbufp
Definition: subr_prf.c:128
uint16_t flags
Definition: subr_stats.c:2
int uiomove(void *cp, int n, struct uio *uio)
Definition: subr_uio.c:195
void selwakeuppri(struct selinfo *sip, int pri)
Definition: sys_generic.c:1924
void selrecord(struct thread *selector, struct selinfo *sip)
Definition: sys_generic.c:1869
struct mtx mtx
Definition: uipc_ktls.c:0
struct stat * buf
mode_t mode
int flag