The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_log.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: subr_log.c,v 1.39 2006/11/01 10:17:58 yamt Exp $       */
    2 
    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.3 (Berkeley) 2/14/95
   32  */
   33 
   34 /*
   35  * Error log buffer for kernel printf's.
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.39 2006/11/01 10:17:58 yamt Exp $");
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/proc.h>
   44 #include <sys/vnode.h>
   45 #include <sys/ioctl.h>
   46 #include <sys/msgbuf.h>
   47 #include <sys/file.h>
   48 #include <sys/signalvar.h>
   49 #include <sys/syslog.h>
   50 #include <sys/conf.h>
   51 #include <sys/select.h>
   52 #include <sys/poll.h>
   53 
   54 #define LOG_RDPRI       (PZERO + 1)
   55 
   56 #define LOG_ASYNC       0x04
   57 #define LOG_RDWAIT      0x08
   58 
   59 struct logsoftc {
   60         int     sc_state;               /* see above for possibilities */
   61         struct  selinfo sc_selp;        /* process waiting on select call */
   62         pid_t   sc_pgid;                /* process/group for async I/O */
   63 } logsoftc;
   64 
   65 int     log_open;                       /* also used in log() */
   66 int     msgbufmapped;                   /* is the message buffer mapped */
   67 int     msgbufenabled;                  /* is logging to the buffer enabled */
   68 struct  kern_msgbuf *msgbufp;           /* the mapped buffer, itself. */
   69 
   70 void
   71 initmsgbuf(void *bf, size_t bufsize)
   72 {
   73         struct kern_msgbuf *mbp;
   74         long new_bufs;
   75 
   76         /* Sanity-check the given size. */
   77         if (bufsize < sizeof(struct kern_msgbuf))
   78                 return;
   79 
   80         mbp = msgbufp = (struct kern_msgbuf *)bf;
   81 
   82         new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
   83         if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
   84             (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
   85             (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
   86                 /*
   87                  * If the buffer magic number is wrong, has changed
   88                  * size (which shouldn't happen often), or is
   89                  * internally inconsistent, initialize it.
   90                  */
   91 
   92                 memset(bf, 0, bufsize);
   93                 mbp->msg_magic = MSG_MAGIC;
   94                 mbp->msg_bufs = new_bufs;
   95         }
   96 
   97         /* mark it as ready for use. */
   98         msgbufmapped = msgbufenabled = 1;
   99 }
  100 
  101 /*ARGSUSED*/
  102 static int
  103 logopen(dev_t dev, int flags, int mode, struct lwp *l)
  104 {
  105         struct kern_msgbuf *mbp = msgbufp;
  106 
  107         if (log_open)
  108                 return (EBUSY);
  109         log_open = 1;
  110         logsoftc.sc_pgid = l->l_proc->p_pid;    /* signal process only */
  111         /*
  112          * The message buffer is initialized during system configuration.
  113          * If it's been clobbered, note that and return an error.  (This
  114          * allows a user to potentially read the buffer via /dev/kmem,
  115          * and try to figure out what clobbered it.
  116          */
  117         if (mbp->msg_magic != MSG_MAGIC) {
  118                 msgbufenabled = 0;
  119                 return (ENXIO);
  120         }
  121 
  122         return (0);
  123 }
  124 
  125 /*ARGSUSED*/
  126 static int
  127 logclose(dev_t dev, int flag, int mode, struct lwp *l)
  128 {
  129 
  130         log_open = 0;
  131         logsoftc.sc_state = 0;
  132         return (0);
  133 }
  134 
  135 /*ARGSUSED*/
  136 static int
  137 logread(dev_t dev, struct uio *uio, int flag)
  138 {
  139         struct kern_msgbuf *mbp = msgbufp;
  140         long l;
  141         int s;
  142         int error = 0;
  143 
  144         s = splhigh();
  145         while (mbp->msg_bufr == mbp->msg_bufx) {
  146                 if (flag & IO_NDELAY) {
  147                         splx(s);
  148                         return (EWOULDBLOCK);
  149                 }
  150                 logsoftc.sc_state |= LOG_RDWAIT;
  151                 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
  152                                "klog", 0);
  153                 if (error) {
  154                         splx(s);
  155                         return (error);
  156                 }
  157         }
  158         splx(s);
  159         logsoftc.sc_state &= ~LOG_RDWAIT;
  160 
  161         while (uio->uio_resid > 0) {
  162                 l = mbp->msg_bufx - mbp->msg_bufr;
  163                 if (l < 0)
  164                         l = mbp->msg_bufs - mbp->msg_bufr;
  165                 l = min(l, uio->uio_resid);
  166                 if (l == 0)
  167                         break;
  168                 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
  169                         (int)l, uio);
  170                 if (error)
  171                         break;
  172                 mbp->msg_bufr += l;
  173                 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
  174                         mbp->msg_bufr = 0;
  175         }
  176         return (error);
  177 }
  178 
  179 /*ARGSUSED*/
  180 static int
  181 logpoll(dev_t dev, int events, struct lwp *l)
  182 {
  183         int revents = 0;
  184         int s = splhigh();
  185 
  186         if (events & (POLLIN | POLLRDNORM)) {
  187                 if (msgbufp->msg_bufr != msgbufp->msg_bufx)
  188                         revents |= events & (POLLIN | POLLRDNORM);
  189                 else
  190                         selrecord(l, &logsoftc.sc_selp);
  191         }
  192 
  193         splx(s);
  194         return (revents);
  195 }
  196 
  197 static void
  198 filt_logrdetach(struct knote *kn)
  199 {
  200         int s;
  201 
  202         s = splhigh();
  203         SLIST_REMOVE(&logsoftc.sc_selp.sel_klist, kn, knote, kn_selnext);
  204         splx(s);
  205 }
  206 
  207 static int
  208 filt_logread(struct knote *kn, long hint)
  209 {
  210 
  211         if (msgbufp->msg_bufr == msgbufp->msg_bufx)
  212                 return (0);
  213 
  214         if (msgbufp->msg_bufr < msgbufp->msg_bufx)
  215                 kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
  216         else
  217                 kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
  218                     msgbufp->msg_bufx;
  219 
  220         return (1);
  221 }
  222 
  223 static const struct filterops logread_filtops =
  224         { 1, NULL, filt_logrdetach, filt_logread };
  225 
  226 static int
  227 logkqfilter(dev_t dev, struct knote *kn)
  228 {
  229         struct klist *klist;
  230         int s;
  231 
  232         switch (kn->kn_filter) {
  233         case EVFILT_READ:
  234                 klist = &logsoftc.sc_selp.sel_klist;
  235                 kn->kn_fop = &logread_filtops;
  236                 break;
  237 
  238         default:
  239                 return (1);
  240         }
  241 
  242         kn->kn_hook = NULL;
  243 
  244         s = splhigh();
  245         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
  246         splx(s);
  247 
  248         return (0);
  249 }
  250 
  251 void
  252 logwakeup(void)
  253 {
  254         if (!log_open)
  255                 return;
  256         selnotify(&logsoftc.sc_selp, 0);
  257         if (logsoftc.sc_state & LOG_ASYNC)
  258                 fownsignal(logsoftc.sc_pgid, SIGIO, 0, 0, NULL);
  259         if (logsoftc.sc_state & LOG_RDWAIT) {
  260                 wakeup((caddr_t)msgbufp);
  261                 logsoftc.sc_state &= ~LOG_RDWAIT;
  262         }
  263 }
  264 
  265 /*ARGSUSED*/
  266 static int
  267 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct lwp *lwp)
  268 {
  269         struct proc *p = lwp->l_proc;
  270         long l;
  271         int s;
  272 
  273         switch (com) {
  274 
  275         /* return number of characters immediately available */
  276         case FIONREAD:
  277                 s = splhigh();
  278                 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
  279                 splx(s);
  280                 if (l < 0)
  281                         l += msgbufp->msg_bufs;
  282                 *(int *)data = l;
  283                 break;
  284 
  285         case FIONBIO:
  286                 break;
  287 
  288         case FIOASYNC:
  289                 if (*(int *)data)
  290                         logsoftc.sc_state |= LOG_ASYNC;
  291                 else
  292                         logsoftc.sc_state &= ~LOG_ASYNC;
  293                 break;
  294 
  295         case TIOCSPGRP:
  296         case FIOSETOWN:
  297                 return fsetown(p, &logsoftc.sc_pgid, com, data);
  298 
  299         case TIOCGPGRP:
  300         case FIOGETOWN:
  301                 return fgetown(p, logsoftc.sc_pgid, com, data);
  302 
  303         default:
  304                 return (EPASSTHROUGH);
  305         }
  306         return (0);
  307 }
  308 
  309 const struct cdevsw log_cdevsw = {
  310         logopen, logclose, logread, nowrite, logioctl,
  311             nostop, notty, logpoll, nommap, logkqfilter, D_OTHER,
  312 };

Cache object: 0403b9a8979ab156ee732db34759308a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.