1 /* $OpenBSD: subr_log.c,v 1.4 1997/08/31 20:42:20 deraadt Exp $ */ 2 /* $NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93 37 */ 38 39 /* 40 * Error log buffer for kernel printf's. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 #include <sys/vnode.h> 47 #include <sys/ioctl.h> 48 #include <sys/msgbuf.h> 49 #include <sys/file.h> 50 #include <sys/signalvar.h> 51 #include <sys/syslog.h> 52 #include <sys/conf.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 int sc_pgid; /* process/group for async I/O */ 63 uid_t sc_siguid; /* uid for process that set sc_pgid */ 64 uid_t sc_sigeuid; /* euid for process that set sc_pgid */ 65 } logsoftc; 66 67 int log_open; /* also used in log() */ 68 69 /*ARGSUSED*/ 70 int 71 logopen(dev, flags, mode, p) 72 dev_t dev; 73 int flags, mode; 74 struct proc *p; 75 { 76 register struct msgbuf *mbp = msgbufp; 77 78 if (log_open) 79 return (EBUSY); 80 log_open = 1; 81 logsoftc.sc_pgid = p->p_pid; /* signal process only */ 82 /* 83 * Potential race here with putchar() but since putchar should be 84 * called by autoconf, msg_magic should be initialized by the time 85 * we get here. 86 */ 87 if (mbp->msg_magic != MSG_MAGIC) { 88 register int i; 89 90 mbp->msg_magic = MSG_MAGIC; 91 mbp->msg_bufx = mbp->msg_bufr = 0; 92 for (i=0; i < MSG_BSIZE; i++) 93 mbp->msg_bufc[i] = 0; 94 } 95 return (0); 96 } 97 98 /*ARGSUSED*/ 99 int 100 logclose(dev, flag, mode, p) 101 dev_t dev; 102 int flag, mode; 103 struct proc *p; 104 { 105 106 log_open = 0; 107 logsoftc.sc_state = 0; 108 return (0); 109 } 110 111 /*ARGSUSED*/ 112 int 113 logread(dev, uio, flag) 114 dev_t dev; 115 struct uio *uio; 116 int flag; 117 { 118 register struct msgbuf *mbp = msgbufp; 119 register long l; 120 register int s; 121 int error = 0; 122 123 s = splhigh(); 124 while (mbp->msg_bufr == mbp->msg_bufx) { 125 if (flag & IO_NDELAY) { 126 splx(s); 127 return (EWOULDBLOCK); 128 } 129 logsoftc.sc_state |= LOG_RDWAIT; 130 error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH, 131 "klog", 0); 132 if (error) { 133 splx(s); 134 return (error); 135 } 136 } 137 splx(s); 138 logsoftc.sc_state &= ~LOG_RDWAIT; 139 140 while (uio->uio_resid > 0) { 141 l = mbp->msg_bufx - mbp->msg_bufr; 142 if (l < 0) 143 l = MSG_BSIZE - mbp->msg_bufr; 144 l = min(l, uio->uio_resid); 145 if (l == 0) 146 break; 147 error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr], 148 (int)l, uio); 149 if (error) 150 break; 151 mbp->msg_bufr += l; 152 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= MSG_BSIZE) 153 mbp->msg_bufr = 0; 154 } 155 return (error); 156 } 157 158 /*ARGSUSED*/ 159 int 160 logselect(dev, rw, p) 161 dev_t dev; 162 int rw; 163 struct proc *p; 164 { 165 int s = splhigh(); 166 167 switch (rw) { 168 169 case FREAD: 170 if (msgbufp->msg_bufr != msgbufp->msg_bufx) { 171 splx(s); 172 return (1); 173 } 174 selrecord(p, &logsoftc.sc_selp); 175 break; 176 } 177 splx(s); 178 return (0); 179 } 180 181 void 182 logwakeup() 183 { 184 if (!log_open) 185 return; 186 selwakeup(&logsoftc.sc_selp); 187 if (logsoftc.sc_state & LOG_ASYNC) 188 csignal(logsoftc.sc_pgid, SIGIO, 189 logsoftc.sc_siguid, logsoftc.sc_sigeuid); 190 if (logsoftc.sc_state & LOG_RDWAIT) { 191 wakeup((caddr_t)msgbufp); 192 logsoftc.sc_state &= ~LOG_RDWAIT; 193 } 194 } 195 196 /*ARGSUSED*/ 197 int 198 logioctl(dev, com, data, flag, p) 199 dev_t dev; 200 u_long com; 201 caddr_t data; 202 int flag; 203 struct proc *p; 204 { 205 long l; 206 int s; 207 208 switch (com) { 209 210 /* return number of characters immediately available */ 211 case FIONREAD: 212 s = splhigh(); 213 l = msgbufp->msg_bufx - msgbufp->msg_bufr; 214 splx(s); 215 if (l < 0) 216 l += MSG_BSIZE; 217 *(int *)data = l; 218 break; 219 220 case FIONBIO: 221 break; 222 223 case FIOASYNC: 224 if (*(int *)data) 225 logsoftc.sc_state |= LOG_ASYNC; 226 else 227 logsoftc.sc_state &= ~LOG_ASYNC; 228 break; 229 230 case TIOCSPGRP: 231 logsoftc.sc_pgid = *(int *)data; 232 logsoftc.sc_siguid = p->p_cred->p_ruid; 233 logsoftc.sc_sigeuid = p->p_ucred->cr_uid; 234 break; 235 236 case TIOCGPGRP: 237 *(int *)data = logsoftc.sc_pgid; 238 break; 239 240 default: 241 return (-1); 242 } 243 return (0); 244 } 245