1 /* $OpenBSD: subr_log.c,v 1.79 2024/07/24 13:37:05 claudio 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93
33 */
34
35 /*
36 * Error log buffer for kernel printf's.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/ioctl.h>
44 #include <sys/msgbuf.h>
45 #include <sys/file.h>
46 #include <sys/tty.h>
47 #include <sys/signalvar.h>
48 #include <sys/syslog.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/mutex.h>
56 #include <sys/timeout.h>
57
58 #ifdef KTRACE
59 #include <sys/ktrace.h>
60 #endif
61
62 #include <sys/mount.h>
63 #include <sys/syscallargs.h>
64
65 #include <dev/cons.h>
66
67 #define LOG_RDPRI (PZERO + 1)
68 #define LOG_TICK 50 /* log tick interval in msec */
69
70 #define LOG_ASYNC 0x04
71 #define LOG_RDWAIT 0x08
72
73 /*
74 * Locking:
75 * L log_mtx
76 * Q log_kq_mtx
77 */
78 struct logsoftc {
79 int sc_state; /* [L] see above for possibilities */
80 struct klist sc_klist; /* [Q] process waiting on kevent call */
81 struct sigio_ref sc_sigio; /* async I/O registration */
82 int sc_need_wakeup; /* if set, wake up waiters */
83 struct timeout sc_tick; /* wakeup poll timeout */
84 } logsoftc;
85
86 int log_open; /* also used in log() */
87 int msgbufmapped; /* is the message buffer mapped */
88 struct msgbuf *msgbufp; /* the mapped buffer, itself. */
89 struct msgbuf *consbufp; /* console message buffer. */
90
91 struct file *syslogf;
92 struct rwlock syslogf_rwlock = RWLOCK_INITIALIZER("syslogf");
93
94 /*
95 * Lock that serializes access to log message buffers.
96 * This should be kept as a leaf lock in order not to constrain where
97 * printf(9) can be used.
98 */
99 struct mutex log_mtx =
100 MUTEX_INITIALIZER_FLAGS(IPL_HIGH, "logmtx", MTX_NOWITNESS);
101 struct mutex log_kq_mtx =
102 MUTEX_INITIALIZER_FLAGS(IPL_HIGH, "logkqmtx", MTX_NOWITNESS);
103
104 void filt_logrdetach(struct knote *kn);
105 int filt_logread(struct knote *kn, long hint);
106 int filt_logmodify(struct kevent *, struct knote *);
107 int filt_logprocess(struct knote *, struct kevent *);
108
109 const struct filterops logread_filtops = {
110 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
111 .f_attach = NULL,
112 .f_detach = filt_logrdetach,
113 .f_event = filt_logread,
114 .f_modify = filt_logmodify,
115 .f_process = filt_logprocess,
116 };
117
118 int dosendsyslog(struct proc *, const char *, size_t, int, enum uio_seg);
119 void logtick(void *);
120 size_t msgbuf_getlen(struct msgbuf *);
121 void msgbuf_putchar_locked(struct msgbuf *, const char);
122
123 void
initmsgbuf(caddr_t buf,size_t bufsize)124 initmsgbuf(caddr_t buf, size_t bufsize)
125 {
126 struct msgbuf *mbp;
127 long new_bufs;
128
129 /* Sanity-check the given size. */
130 if (bufsize < sizeof(struct msgbuf))
131 return;
132
133 mbp = msgbufp = (struct msgbuf *)buf;
134
135 new_bufs = bufsize - offsetof(struct msgbuf, msg_bufc);
136 if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
137 (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
138 (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
139 /*
140 * If the buffer magic number is wrong, has changed
141 * size (which shouldn't happen often), or is
142 * internally inconsistent, initialize it.
143 */
144
145 memset(buf, 0, bufsize);
146 mbp->msg_magic = MSG_MAGIC;
147 mbp->msg_bufs = new_bufs;
148 }
149
150 /*
151 * Always start new buffer data on a new line.
152 * Avoid using log_mtx because mutexes do not work during early boot
153 * on some architectures.
154 */
155 if (mbp->msg_bufx > 0 && mbp->msg_bufc[mbp->msg_bufx - 1] != '\n')
156 msgbuf_putchar_locked(mbp, '\n');
157
158 /* mark it as ready for use. */
159 msgbufmapped = 1;
160 }
161
162 void
initconsbuf(void)163 initconsbuf(void)
164 {
165 /* Set up a buffer to collect /dev/console output */
166 consbufp = malloc(CONSBUFSIZE, M_TTYS, M_WAITOK | M_ZERO);
167 consbufp->msg_magic = MSG_MAGIC;
168 consbufp->msg_bufs = CONSBUFSIZE - offsetof(struct msgbuf, msg_bufc);
169 }
170
171 void
msgbuf_putchar(struct msgbuf * mbp,const char c)172 msgbuf_putchar(struct msgbuf *mbp, const char c)
173 {
174 if (mbp->msg_magic != MSG_MAGIC)
175 /* Nothing we can do */
176 return;
177
178 mtx_enter(&log_mtx);
179 msgbuf_putchar_locked(mbp, c);
180 mtx_leave(&log_mtx);
181 }
182
183 void
msgbuf_putchar_locked(struct msgbuf * mbp,const char c)184 msgbuf_putchar_locked(struct msgbuf *mbp, const char c)
185 {
186 mbp->msg_bufc[mbp->msg_bufx++] = c;
187 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
188 mbp->msg_bufx = 0;
189 /* If the buffer is full, keep the most recent data. */
190 if (mbp->msg_bufr == mbp->msg_bufx) {
191 if (++mbp->msg_bufr >= mbp->msg_bufs)
192 mbp->msg_bufr = 0;
193 mbp->msg_bufd++;
194 }
195 }
196
197 size_t
msgbuf_getlen(struct msgbuf * mbp)198 msgbuf_getlen(struct msgbuf *mbp)
199 {
200 long len;
201
202 len = mbp->msg_bufx - mbp->msg_bufr;
203 if (len < 0)
204 len += mbp->msg_bufs;
205 return (len);
206 }
207
208 int
logopen(dev_t dev,int flags,int mode,struct proc * p)209 logopen(dev_t dev, int flags, int mode, struct proc *p)
210 {
211 if (log_open)
212 return (EBUSY);
213 log_open = 1;
214 klist_init_mutex(&logsoftc.sc_klist, &log_kq_mtx);
215 sigio_init(&logsoftc.sc_sigio);
216 timeout_set(&logsoftc.sc_tick, logtick, NULL);
217 timeout_add_msec(&logsoftc.sc_tick, LOG_TICK);
218 return (0);
219 }
220
221 int
logclose(dev_t dev,int flag,int mode,struct proc * p)222 logclose(dev_t dev, int flag, int mode, struct proc *p)
223 {
224 struct file *fp;
225
226 rw_enter_write(&syslogf_rwlock);
227 fp = syslogf;
228 syslogf = NULL;
229 rw_exit(&syslogf_rwlock);
230
231 if (fp)
232 FRELE(fp, p);
233 log_open = 0;
234 timeout_del(&logsoftc.sc_tick);
235
236 klist_invalidate(&logsoftc.sc_klist);
237 klist_free(&logsoftc.sc_klist);
238
239 logsoftc.sc_state = 0;
240 sigio_free(&logsoftc.sc_sigio);
241 return (0);
242 }
243
244 int
logread(dev_t dev,struct uio * uio,int flag)245 logread(dev_t dev, struct uio *uio, int flag)
246 {
247 struct msgbuf *mbp = msgbufp;
248 size_t l, rpos;
249 int error = 0;
250
251 mtx_enter(&log_mtx);
252 while (mbp->msg_bufr == mbp->msg_bufx) {
253 if (flag & IO_NDELAY) {
254 error = EWOULDBLOCK;
255 goto out;
256 }
257 logsoftc.sc_state |= LOG_RDWAIT;
258 mtx_leave(&log_mtx);
259 /*
260 * Set up and enter sleep manually instead of using msleep()
261 * to keep log_mtx as a leaf lock.
262 */
263 sleep_setup(mbp, LOG_RDPRI | PCATCH, "klog");
264 error = sleep_finish(0, logsoftc.sc_state & LOG_RDWAIT);
265 mtx_enter(&log_mtx);
266 if (error)
267 goto out;
268 }
269
270 if (mbp->msg_bufd > 0) {
271 char buf[64];
272 long ndropped;
273
274 ndropped = mbp->msg_bufd;
275 mtx_leave(&log_mtx);
276 l = snprintf(buf, sizeof(buf),
277 "<%d>klog: dropped %ld byte%s, message buffer full\n",
278 LOG_KERN|LOG_WARNING, ndropped,
279 ndropped == 1 ? "" : "s");
280 error = uiomove(buf, ulmin(l, sizeof(buf) - 1), uio);
281 mtx_enter(&log_mtx);
282 if (error)
283 goto out;
284 mbp->msg_bufd -= ndropped;
285 }
286
287 while (uio->uio_resid > 0) {
288 if (mbp->msg_bufx >= mbp->msg_bufr)
289 l = mbp->msg_bufx - mbp->msg_bufr;
290 else
291 l = mbp->msg_bufs - mbp->msg_bufr;
292 l = ulmin(l, uio->uio_resid);
293 if (l == 0)
294 break;
295 rpos = mbp->msg_bufr;
296 mtx_leave(&log_mtx);
297 /* Ignore that concurrent readers may consume the same data. */
298 error = uiomove(&mbp->msg_bufc[rpos], l, uio);
299 mtx_enter(&log_mtx);
300 if (error)
301 break;
302 mbp->msg_bufr += l;
303 if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
304 mbp->msg_bufr = 0;
305 }
306 out:
307 mtx_leave(&log_mtx);
308 return (error);
309 }
310
311 int
logkqfilter(dev_t dev,struct knote * kn)312 logkqfilter(dev_t dev, struct knote *kn)
313 {
314 struct klist *klist;
315
316 switch (kn->kn_filter) {
317 case EVFILT_READ:
318 klist = &logsoftc.sc_klist;
319 kn->kn_fop = &logread_filtops;
320 break;
321 default:
322 return (EINVAL);
323 }
324
325 kn->kn_hook = (void *)msgbufp;
326 klist_insert(klist, kn);
327
328 return (0);
329 }
330
331 void
filt_logrdetach(struct knote * kn)332 filt_logrdetach(struct knote *kn)
333 {
334 klist_remove(&logsoftc.sc_klist, kn);
335 }
336
337 int
filt_logread(struct knote * kn,long hint)338 filt_logread(struct knote *kn, long hint)
339 {
340 struct msgbuf *mbp = kn->kn_hook;
341
342 mtx_enter(&log_mtx);
343 kn->kn_data = msgbuf_getlen(mbp);
344 mtx_leave(&log_mtx);
345 return (kn->kn_data != 0);
346 }
347
348 int
filt_logmodify(struct kevent * kev,struct knote * kn)349 filt_logmodify(struct kevent *kev, struct knote *kn)
350 {
351 int active;
352
353 mtx_enter(&log_kq_mtx);
354 active = knote_modify(kev, kn);
355 mtx_leave(&log_kq_mtx);
356
357 return (active);
358 }
359
360 int
filt_logprocess(struct knote * kn,struct kevent * kev)361 filt_logprocess(struct knote *kn, struct kevent *kev)
362 {
363 int active;
364
365 mtx_enter(&log_kq_mtx);
366 active = knote_process(kn, kev);
367 mtx_leave(&log_kq_mtx);
368
369 return (active);
370 }
371
372 void
logwakeup(void)373 logwakeup(void)
374 {
375 /*
376 * The actual wakeup has to be deferred because logwakeup() can be
377 * called in very varied contexts.
378 * Keep the print routines usable in as many situations as possible
379 * by not using locking here.
380 */
381
382 /*
383 * Ensure that preceding stores become visible to other CPUs
384 * before the flag.
385 */
386 membar_producer();
387
388 logsoftc.sc_need_wakeup = 1;
389 }
390
391 void
logtick(void * arg)392 logtick(void *arg)
393 {
394 int state;
395
396 if (!log_open)
397 return;
398
399 if (!logsoftc.sc_need_wakeup)
400 goto out;
401 logsoftc.sc_need_wakeup = 0;
402
403 /*
404 * sc_need_wakeup has to be cleared before handling the wakeup.
405 * Visiting log_mtx ensures the proper order.
406 */
407
408 mtx_enter(&log_mtx);
409 state = logsoftc.sc_state;
410 if (logsoftc.sc_state & LOG_RDWAIT)
411 logsoftc.sc_state &= ~LOG_RDWAIT;
412 mtx_leave(&log_mtx);
413
414 knote(&logsoftc.sc_klist, 0);
415
416 if (state & LOG_ASYNC)
417 pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
418 if (state & LOG_RDWAIT)
419 wakeup(msgbufp);
420 out:
421 timeout_add_msec(&logsoftc.sc_tick, LOG_TICK);
422 }
423
424 int
logioctl(dev_t dev,u_long com,caddr_t data,int flag,struct proc * p)425 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
426 {
427 struct file *fp, *newfp;
428 int error;
429
430 switch (com) {
431
432 /* return number of characters immediately available */
433 case FIONREAD:
434 mtx_enter(&log_mtx);
435 *(int *)data = (int)msgbuf_getlen(msgbufp);
436 mtx_leave(&log_mtx);
437 break;
438
439 case FIONBIO:
440 break;
441
442 case FIOASYNC:
443 mtx_enter(&log_mtx);
444 if (*(int *)data)
445 logsoftc.sc_state |= LOG_ASYNC;
446 else
447 logsoftc.sc_state &= ~LOG_ASYNC;
448 mtx_leave(&log_mtx);
449 break;
450
451 case FIOSETOWN:
452 case TIOCSPGRP:
453 return (sigio_setown(&logsoftc.sc_sigio, com, data));
454
455 case FIOGETOWN:
456 case TIOCGPGRP:
457 sigio_getown(&logsoftc.sc_sigio, com, data);
458 break;
459
460 case LIOCSFD:
461 if ((error = suser(p)) != 0)
462 return (error);
463 if ((error = getsock(p, *(int *)data, &newfp)) != 0)
464 return (error);
465
466 rw_enter_write(&syslogf_rwlock);
467 fp = syslogf;
468 syslogf = newfp;
469 rw_exit(&syslogf_rwlock);
470
471 if (fp)
472 FRELE(fp, p);
473 break;
474
475 default:
476 return (ENOTTY);
477 }
478 return (0);
479 }
480
481 /*
482 * If syslogd is not running, temporarily store a limited amount of messages
483 * in kernel. After log stash is full, drop messages and count them. When
484 * syslogd is available again, next log message will flush the stashed
485 * messages and insert a message with drop count. Calls to malloc(9) and
486 * copyin(9) may sleep, protect data structures with rwlock.
487 */
488
489 #define LOGSTASH_SIZE 100
490 struct logstash_message {
491 char *lgs_buffer;
492 size_t lgs_size;
493 } logstash_messages[LOGSTASH_SIZE];
494
495 struct logstash_message *logstash_in = &logstash_messages[0];
496 struct logstash_message *logstash_out = &logstash_messages[0];
497
498 struct rwlock logstash_rwlock = RWLOCK_INITIALIZER("logstash");
499
500 int logstash_dropped, logstash_error, logstash_pid;
501
502 int logstash_insert(const char *, size_t, int, pid_t);
503 void logstash_remove(void);
504 int logstash_sendsyslog(struct proc *);
505
506 static inline int
logstash_full(void)507 logstash_full(void)
508 {
509 rw_assert_anylock(&logstash_rwlock);
510
511 return logstash_out->lgs_buffer != NULL &&
512 logstash_in == logstash_out;
513 }
514
515 static inline void
logstash_increment(struct logstash_message ** msg)516 logstash_increment(struct logstash_message **msg)
517 {
518 rw_assert_wrlock(&logstash_rwlock);
519
520 KASSERT((*msg) >= &logstash_messages[0]);
521 KASSERT((*msg) < &logstash_messages[LOGSTASH_SIZE]);
522 if ((*msg) == &logstash_messages[LOGSTASH_SIZE - 1])
523 (*msg) = &logstash_messages[0];
524 else
525 (*msg)++;
526 }
527
528 int
logstash_insert(const char * buf,size_t nbyte,int logerror,pid_t pid)529 logstash_insert(const char *buf, size_t nbyte, int logerror, pid_t pid)
530 {
531 int error;
532
533 rw_enter_write(&logstash_rwlock);
534
535 if (logstash_full()) {
536 if (logstash_dropped == 0) {
537 logstash_error = logerror;
538 logstash_pid = pid;
539 }
540 logstash_dropped++;
541
542 rw_exit(&logstash_rwlock);
543 return (0);
544 }
545
546 logstash_in->lgs_buffer = malloc(nbyte, M_LOG, M_WAITOK);
547 error = copyin(buf, logstash_in->lgs_buffer, nbyte);
548 if (error) {
549 free(logstash_in->lgs_buffer, M_LOG, nbyte);
550 logstash_in->lgs_buffer = NULL;
551
552 rw_exit(&logstash_rwlock);
553 return (error);
554 }
555 logstash_in->lgs_size = nbyte;
556 logstash_increment(&logstash_in);
557
558 rw_exit(&logstash_rwlock);
559 return (0);
560 }
561
562 void
logstash_remove(void)563 logstash_remove(void)
564 {
565 rw_assert_wrlock(&logstash_rwlock);
566
567 KASSERT(logstash_out->lgs_buffer != NULL);
568 free(logstash_out->lgs_buffer, M_LOG, logstash_out->lgs_size);
569 logstash_out->lgs_buffer = NULL;
570 logstash_increment(&logstash_out);
571
572 /* Insert dropped message in sequence where messages were dropped. */
573 if (logstash_dropped) {
574 size_t l, nbyte;
575 char buf[80];
576
577 l = snprintf(buf, sizeof(buf),
578 "<%d>sendsyslog: dropped %d message%s, error %d, pid %d",
579 LOG_KERN|LOG_WARNING, logstash_dropped,
580 logstash_dropped == 1 ? "" : "s",
581 logstash_error, logstash_pid);
582 logstash_dropped = 0;
583 logstash_error = 0;
584 logstash_pid = 0;
585
586 /* Cannot fail, we have just freed a slot. */
587 KASSERT(!logstash_full());
588 nbyte = ulmin(l, sizeof(buf) - 1);
589 logstash_in->lgs_buffer = malloc(nbyte, M_LOG, M_WAITOK);
590 memcpy(logstash_in->lgs_buffer, buf, nbyte);
591 logstash_in->lgs_size = nbyte;
592 logstash_increment(&logstash_in);
593 }
594 }
595
596 int
logstash_sendsyslog(struct proc * p)597 logstash_sendsyslog(struct proc *p)
598 {
599 int error;
600
601 rw_enter_write(&logstash_rwlock);
602
603 while (logstash_out->lgs_buffer != NULL) {
604 error = dosendsyslog(p, logstash_out->lgs_buffer,
605 logstash_out->lgs_size, 0, UIO_SYSSPACE);
606 if (error) {
607 rw_exit(&logstash_rwlock);
608 return (error);
609 }
610 logstash_remove();
611 }
612
613 rw_exit(&logstash_rwlock);
614 return (0);
615 }
616
617 /*
618 * Send syslog(3) message from userland to socketpair(2) created by syslogd(8).
619 * Store message in kernel log stash for later if syslogd(8) is not available
620 * or sending fails. Send to console if LOG_CONS is set and syslogd(8) socket
621 * does not exist.
622 */
623
624 int
sys_sendsyslog(struct proc * p,void * v,register_t * retval)625 sys_sendsyslog(struct proc *p, void *v, register_t *retval)
626 {
627 struct sys_sendsyslog_args /* {
628 syscallarg(const char *) buf;
629 syscallarg(size_t) nbyte;
630 syscallarg(int) flags;
631 } */ *uap = v;
632 size_t nbyte;
633 int error;
634
635 nbyte = SCARG(uap, nbyte);
636 if (nbyte > LOG_MAXLINE)
637 nbyte = LOG_MAXLINE;
638
639 logstash_sendsyslog(p);
640 error = dosendsyslog(p, SCARG(uap, buf), nbyte, SCARG(uap, flags),
641 UIO_USERSPACE);
642 if (error && error != EFAULT)
643 logstash_insert(SCARG(uap, buf), nbyte, error, p->p_p->ps_pid);
644 return (error);
645 }
646
647 int
dosendsyslog(struct proc * p,const char * buf,size_t nbyte,int flags,enum uio_seg sflg)648 dosendsyslog(struct proc *p, const char *buf, size_t nbyte, int flags,
649 enum uio_seg sflg)
650 {
651 #ifdef KTRACE
652 struct iovec ktriov;
653 #endif
654 struct file *fp;
655 char pri[6], *kbuf;
656 struct iovec aiov;
657 struct uio auio;
658 size_t i, len;
659 int error;
660
661 /* Global variable syslogf may change during sleep, use local copy. */
662 rw_enter_read(&syslogf_rwlock);
663 fp = syslogf;
664 if (fp)
665 FREF(fp);
666 rw_exit(&syslogf_rwlock);
667
668 if (fp == NULL) {
669 if (!ISSET(flags, LOG_CONS))
670 return (ENOTCONN);
671 /*
672 * Strip off syslog priority when logging to console.
673 * LOG_PRIMASK | LOG_FACMASK is 0x03ff, so at most 4
674 * decimal digits may appear in priority as <1023>.
675 */
676 len = MIN(nbyte, sizeof(pri));
677 if (sflg == UIO_USERSPACE) {
678 if ((error = copyin(buf, pri, len)))
679 return (error);
680 } else
681 memcpy(pri, buf, len);
682 if (0 < len && pri[0] == '<') {
683 for (i = 1; i < len; i++) {
684 if (pri[i] < '0' || pri[i] > '9')
685 break;
686 }
687 if (i < len && pri[i] == '>') {
688 i++;
689 /* There must be at least one digit <0>. */
690 if (i >= 3) {
691 buf += i;
692 nbyte -= i;
693 }
694 }
695 }
696 }
697
698 aiov.iov_base = (char *)buf;
699 aiov.iov_len = nbyte;
700 auio.uio_iov = &aiov;
701 auio.uio_iovcnt = 1;
702 auio.uio_segflg = sflg;
703 auio.uio_rw = UIO_WRITE;
704 auio.uio_procp = p;
705 auio.uio_offset = 0;
706 auio.uio_resid = aiov.iov_len;
707 #ifdef KTRACE
708 if (sflg == UIO_USERSPACE && KTRPOINT(p, KTR_GENIO))
709 ktriov = aiov;
710 else
711 ktriov.iov_len = 0;
712 #endif
713
714 len = auio.uio_resid;
715 if (fp) {
716 int flags = (fp->f_flag & FNONBLOCK) ? MSG_DONTWAIT : 0;
717 error = sosend(fp->f_data, NULL, &auio, NULL, NULL, flags);
718 if (error == 0)
719 len -= auio.uio_resid;
720 } else {
721 KERNEL_LOCK();
722 if (constty || cn_devvp) {
723 error = cnwrite(0, &auio, 0);
724 if (error == 0)
725 len -= auio.uio_resid;
726 aiov.iov_base = "\r\n";
727 aiov.iov_len = 2;
728 auio.uio_iov = &aiov;
729 auio.uio_iovcnt = 1;
730 auio.uio_segflg = UIO_SYSSPACE;
731 auio.uio_rw = UIO_WRITE;
732 auio.uio_procp = p;
733 auio.uio_offset = 0;
734 auio.uio_resid = aiov.iov_len;
735 cnwrite(0, &auio, 0);
736 } else {
737 /* XXX console redirection breaks down... */
738 if (sflg == UIO_USERSPACE) {
739 kbuf = malloc(len, M_TEMP, M_WAITOK);
740 error = copyin(aiov.iov_base, kbuf, len);
741 } else {
742 kbuf = aiov.iov_base;
743 error = 0;
744 }
745 if (error == 0)
746 for (i = 0; i < len; i++) {
747 if (kbuf[i] == '\0')
748 break;
749 cnputc(kbuf[i]);
750 auio.uio_resid--;
751 }
752 if (sflg == UIO_USERSPACE)
753 free(kbuf, M_TEMP, len);
754 if (error == 0)
755 len -= auio.uio_resid;
756 cnputc('\n');
757 }
758 KERNEL_UNLOCK();
759 }
760
761 #ifdef KTRACE
762 if (error == 0 && ktriov.iov_len != 0)
763 ktrgenio(p, -1, UIO_WRITE, &ktriov, len);
764 #endif
765 if (fp)
766 FRELE(fp, p);
767 else if (error != EFAULT)
768 error = ENOTCONN;
769 return (error);
770 }
771