subr_log.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* $OpenBSD: subr_log.c,v 1.30 2015/05/06 08:52:17 mpi Exp $ */
  2. /* $NetBSD: subr_log.c,v 1.11 1996/03/30 22:24:44 christos Exp $ */
  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. * Error log buffer for kernel printf's.
  35. */
  36. #include <sys/param.h>
  37. #include <sys/systm.h>
  38. #include <sys/proc.h>
  39. #include <sys/vnode.h>
  40. #include <sys/ioctl.h>
  41. #include <sys/msgbuf.h>
  42. #include <sys/file.h>
  43. #include <sys/signalvar.h>
  44. #include <sys/syslog.h>
  45. #include <sys/poll.h>
  46. #include <sys/malloc.h>
  47. #include <sys/filedesc.h>
  48. #include <sys/socket.h>
  49. #include <sys/socketvar.h>
  50. #ifdef KTRACE
  51. #include <sys/ktrace.h>
  52. #endif
  53. #include <sys/mount.h>
  54. #include <sys/syscallargs.h>
  55. #define LOG_RDPRI (PZERO + 1)
  56. #define LOG_ASYNC 0x04
  57. #define LOG_RDWAIT 0x08
  58. struct logsoftc {
  59. int sc_state; /* see above for possibilities */
  60. struct selinfo sc_selp; /* process waiting on select call */
  61. int sc_pgid; /* process/group for async I/O */
  62. uid_t sc_siguid; /* uid for process that set sc_pgid */
  63. uid_t sc_sigeuid; /* euid for process that set sc_pgid */
  64. } logsoftc;
  65. int log_open; /* also used in log() */
  66. int msgbufmapped; /* is the message buffer mapped */
  67. struct msgbuf *msgbufp; /* the mapped buffer, itself. */
  68. struct msgbuf *consbufp; /* console message buffer. */
  69. struct file *syslogf;
  70. void filt_logrdetach(struct knote *kn);
  71. int filt_logread(struct knote *kn, long hint);
  72. struct filterops logread_filtops =
  73. { 1, NULL, filt_logrdetach, filt_logread};
  74. void
  75. initmsgbuf(caddr_t buf, size_t bufsize)
  76. {
  77. struct msgbuf *mbp;
  78. long new_bufs;
  79. /* Sanity-check the given size. */
  80. if (bufsize < sizeof(struct msgbuf))
  81. return;
  82. mbp = msgbufp = (struct msgbuf *)buf;
  83. new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc);
  84. if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
  85. (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
  86. (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
  87. /*
  88. * If the buffer magic number is wrong, has changed
  89. * size (which shouldn't happen often), or is
  90. * internally inconsistent, initialize it.
  91. */
  92. memset(buf, 0, bufsize);
  93. mbp->msg_magic = MSG_MAGIC;
  94. mbp->msg_bufs = new_bufs;
  95. }
  96. /* Always start new buffer data on a new line. */
  97. if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n')
  98. msgbuf_putchar(msgbufp, '\n');
  99. /* mark it as ready for use. */
  100. msgbufmapped = 1;
  101. }
  102. void
  103. initconsbuf(void)
  104. {
  105. long new_bufs;
  106. /* Set up a buffer to collect /dev/console output */
  107. consbufp = malloc(CONSBUFSIZE, M_TEMP, M_NOWAIT|M_ZERO);
  108. if (consbufp) {
  109. new_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc);
  110. consbufp->msg_magic = MSG_MAGIC;
  111. consbufp->msg_bufs = new_bufs;
  112. }
  113. }
  114. void
  115. msgbuf_putchar(struct msgbuf *mbp, const char c)
  116. {
  117. if (mbp->msg_magic != MSG_MAGIC)
  118. /* Nothing we can do */
  119. return;
  120. mbp->msg_bufc[mbp->msg_bufx++] = c;
  121. mbp->msg_bufl = min(mbp->msg_bufl+1, mbp->msg_bufs);
  122. if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
  123. mbp->msg_bufx = 0;
  124. /* If the buffer is full, keep the most recent data. */
  125. if (mbp->msg_bufr == mbp->msg_bufx) {
  126. if (++mbp->msg_bufr >= mbp->msg_bufs)
  127. mbp->msg_bufr = 0;
  128. }
  129. }
  130. /*ARGSUSED*/
  131. int
  132. logopen(dev_t dev, int flags, int mode, struct proc *p)
  133. {
  134. if (log_open)
  135. return (EBUSY);
  136. log_open = 1;
  137. return (0);
  138. }
  139. /*ARGSUSED*/
  140. int
  141. logclose(dev_t dev, int flag, int mode, struct proc *p)
  142. {
  143. if (syslogf)
  144. FRELE(syslogf, p);
  145. syslogf = NULL;
  146. log_open = 0;
  147. logsoftc.sc_state = 0;
  148. return (0);
  149. }
  150. /*ARGSUSED*/
  151. int
  152. logread(dev_t dev, struct uio *uio, int flag)
  153. {
  154. struct msgbuf *mbp = msgbufp;
  155. long l;
  156. int s;
  157. int error = 0;
  158. s = splhigh();
  159. while (mbp->msg_bufr == mbp->msg_bufx) {
  160. if (flag & IO_NDELAY) {
  161. splx(s);
  162. return (EWOULDBLOCK);
  163. }
  164. logsoftc.sc_state |= LOG_RDWAIT;
  165. error = tsleep(mbp, LOG_RDPRI | PCATCH,
  166. "klog", 0);
  167. if (error) {
  168. splx(s);
  169. return (error);
  170. }
  171. }
  172. splx(s);
  173. logsoftc.sc_state &= ~LOG_RDWAIT;
  174. while (uio->uio_resid > 0) {
  175. l = mbp->msg_bufx - mbp->msg_bufr;
  176. if (l < 0)
  177. l = mbp->msg_bufs - mbp->msg_bufr;
  178. l = min(l, uio->uio_resid);
  179. if (l == 0)
  180. break;
  181. error = uiomovei(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio);
  182. if (error)
  183. break;
  184. mbp->msg_bufr += l;
  185. if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
  186. mbp->msg_bufr = 0;
  187. }
  188. return (error);
  189. }
  190. /*ARGSUSED*/
  191. int
  192. logpoll(dev_t dev, int events, struct proc *p)
  193. {
  194. int revents = 0;
  195. int s = splhigh();
  196. if (events & (POLLIN | POLLRDNORM)) {
  197. if (msgbufp->msg_bufr != msgbufp->msg_bufx)
  198. revents |= events & (POLLIN | POLLRDNORM);
  199. else
  200. selrecord(p, &logsoftc.sc_selp);
  201. }
  202. splx(s);
  203. return (revents);
  204. }
  205. int
  206. logkqfilter(dev_t dev, struct knote *kn)
  207. {
  208. struct klist *klist;
  209. int s;
  210. switch (kn->kn_filter) {
  211. case EVFILT_READ:
  212. klist = &logsoftc.sc_selp.si_note;
  213. kn->kn_fop = &logread_filtops;
  214. break;
  215. default:
  216. return (EINVAL);
  217. }
  218. kn->kn_hook = (void *)msgbufp;
  219. s = splhigh();
  220. SLIST_INSERT_HEAD(klist, kn, kn_selnext);
  221. splx(s);
  222. return (0);
  223. }
  224. void
  225. filt_logrdetach(struct knote *kn)
  226. {
  227. int s = splhigh();
  228. SLIST_REMOVE(&logsoftc.sc_selp.si_note, kn, knote, kn_selnext);
  229. splx(s);
  230. }
  231. int
  232. filt_logread(struct knote *kn, long hint)
  233. {
  234. struct msgbuf *p = (struct msgbuf *)kn->kn_hook;
  235. kn->kn_data = (int)(p->msg_bufx - p->msg_bufr);
  236. return (p->msg_bufx != p->msg_bufr);
  237. }
  238. void
  239. logwakeup(void)
  240. {
  241. if (!log_open)
  242. return;
  243. selwakeup(&logsoftc.sc_selp);
  244. if (logsoftc.sc_state & LOG_ASYNC)
  245. csignal(logsoftc.sc_pgid, SIGIO,
  246. logsoftc.sc_siguid, logsoftc.sc_sigeuid);
  247. if (logsoftc.sc_state & LOG_RDWAIT) {
  248. wakeup(msgbufp);
  249. logsoftc.sc_state &= ~LOG_RDWAIT;
  250. }
  251. }
  252. /*ARGSUSED*/
  253. int
  254. logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
  255. {
  256. struct file *fp;
  257. long l;
  258. int error, s;
  259. switch (com) {
  260. /* return number of characters immediately available */
  261. case FIONREAD:
  262. s = splhigh();
  263. l = msgbufp->msg_bufx - msgbufp->msg_bufr;
  264. splx(s);
  265. if (l < 0)
  266. l += msgbufp->msg_bufs;
  267. *(int *)data = l;
  268. break;
  269. case FIONBIO:
  270. break;
  271. case FIOASYNC:
  272. if (*(int *)data)
  273. logsoftc.sc_state |= LOG_ASYNC;
  274. else
  275. logsoftc.sc_state &= ~LOG_ASYNC;
  276. break;
  277. case TIOCSPGRP:
  278. logsoftc.sc_pgid = *(int *)data;
  279. logsoftc.sc_siguid = p->p_ucred->cr_ruid;
  280. logsoftc.sc_sigeuid = p->p_ucred->cr_uid;
  281. break;
  282. case TIOCGPGRP:
  283. *(int *)data = logsoftc.sc_pgid;
  284. break;
  285. case LIOCSFD:
  286. if ((error = suser(p, 0)) != 0)
  287. return (error);
  288. if ((error = getsock(p, *(int *)data, &fp)) != 0)
  289. return (error);
  290. if (syslogf)
  291. FRELE(syslogf, p);
  292. syslogf = fp;
  293. break;
  294. default:
  295. return (ENOTTY);
  296. }
  297. return (0);
  298. }
  299. int
  300. sys_sendsyslog(struct proc *p, void *v, register_t *retval)
  301. {
  302. struct sys_sendsyslog_args /* {
  303. syscallarg(const void *) buf;
  304. syscallarg(size_t) nbyte;
  305. } */ *uap = v;
  306. #ifdef KTRACE
  307. struct iovec *ktriov = NULL;
  308. int iovlen;
  309. #endif
  310. struct iovec aiov;
  311. struct uio auio;
  312. struct file *f;
  313. size_t len;
  314. int error;
  315. if (syslogf == NULL)
  316. return (ENOTCONN);
  317. f = syslogf;
  318. FREF(f);
  319. aiov.iov_base = (char *)SCARG(uap, buf);
  320. aiov.iov_len = SCARG(uap, nbyte);
  321. auio.uio_iov = &aiov;
  322. auio.uio_iovcnt = 1;
  323. auio.uio_segflg = UIO_USERSPACE;
  324. auio.uio_rw = UIO_WRITE;
  325. auio.uio_procp = p;
  326. auio.uio_offset = 0;
  327. auio.uio_resid = aiov.iov_len;
  328. #ifdef KTRACE
  329. if (KTRPOINT(p, KTR_GENIO)) {
  330. ktriov = mallocarray(auio.uio_iovcnt, sizeof(struct iovec),
  331. M_TEMP, M_WAITOK);
  332. iovlen = auio.uio_iovcnt * sizeof (struct iovec);
  333. memcpy(ktriov, auio.uio_iov, iovlen);
  334. }
  335. #endif
  336. len = auio.uio_resid;
  337. error = sosend(f->f_data, NULL, &auio, NULL, NULL, 0);
  338. if (error == 0)
  339. len -= auio.uio_resid;
  340. #ifdef KTRACE
  341. if (ktriov != NULL) {
  342. if (error == 0)
  343. ktrgenio(p, -1, UIO_WRITE, ktriov, len);
  344. free(ktriov, M_TEMP, iovlen);
  345. }
  346. #endif
  347. FRELE(f, p);
  348. return error;
  349. }