9348 mii: duplicate 'const' declaration specifier
[unleashed.git] / usr / src / uts / common / io / sysmsg.c
bloba82a0786fea1ce16dd69e7881d5d2d8068504cb4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 * System message redirection driver for Sun.
30 * Redirects system message output to the device designated as the underlying
31 * "hardware" console, as given by the value of sysmvp. The implementation
32 * assumes that sysmvp denotes a STREAMS device; the assumption is justified
33 * since consoles must be capable of effecting tty semantics.
36 #include <sys/types.h>
37 #include <sys/kmem.h>
38 #include <sys/open.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/signal.h>
42 #include <sys/cred.h>
43 #include <sys/user.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/uio.h>
47 #include <sys/stat.h>
48 #include <sys/file.h>
49 #include <sys/session.h>
50 #include <sys/stream.h>
51 #include <sys/strsubr.h>
52 #include <sys/poll.h>
53 #include <sys/debug.h>
54 #include <sys/sysmsg_impl.h>
55 #include <sys/conf.h>
56 #include <sys/termios.h>
57 #include <sys/errno.h>
58 #include <sys/modctl.h>
59 #include <sys/pathname.h>
60 #include <sys/ddi.h>
61 #include <sys/sunddi.h>
62 #include <sys/consdev.h>
63 #include <sys/policy.h>
66 * internal functions
68 static int sysmopen(dev_t *, int, int, cred_t *);
69 static int sysmclose(dev_t, int, int, cred_t *);
70 static int sysmread(dev_t, struct uio *, cred_t *);
71 static int sysmwrite(dev_t, struct uio *, cred_t *);
72 static int sysmioctl(dev_t, int, intptr_t, int, cred_t *, int *);
73 static int sysmpoll(dev_t, short, int, short *, struct pollhead **);
74 static int sysm_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
75 static int sysm_attach(dev_info_t *, ddi_attach_cmd_t);
76 static int sysm_detach(dev_info_t *, ddi_detach_cmd_t);
77 static void bind_consadm_conf(char *);
78 static int checkarg(dev_t);
80 static dev_info_t *sysm_dip; /* private copy of devinfo pointer */
82 static struct cb_ops sysm_cb_ops = {
84 sysmopen, /* open */
85 sysmclose, /* close */
86 nodev, /* strategy */
87 nodev, /* print */
88 nodev, /* dump */
89 sysmread, /* read */
90 sysmwrite, /* write */
91 sysmioctl, /* ioctl */
92 nodev, /* devmap */
93 nodev, /* mmap */
94 nodev, /* segmap */
95 sysmpoll, /* poll */
96 ddi_prop_op, /* cb_prop_op */
97 NULL, /* streamtab */
98 D_NEW | D_MP, /* Driver compatibility flag */
99 CB_REV, /* cb_rev */
100 nodev, /* aread */
101 nodev /* awrite */
104 static struct dev_ops sysm_ops = {
106 DEVO_REV, /* devo_rev, */
107 0, /* refcnt */
108 sysm_info, /* info */
109 nulldev, /* identify */
110 nulldev, /* probe */
111 sysm_attach, /* attach */
112 sysm_detach, /* detach */
113 nodev, /* reset */
114 &sysm_cb_ops, /* driver operations */
115 (struct bus_ops *)0, /* bus operations */
116 nulldev, /* power */
117 ddi_quiesce_not_needed, /* quiesce */
122 * Global variables associated with the console device:
125 #define SYS_SYSMIN 0 /* sysmsg minor number */
126 #define SYS_MSGMIN 1 /* msglog minor number */
127 #define SYSPATHLEN 255 /* length of device path */
130 * Private driver state:
133 #define MAXDEVS 5
135 typedef struct {
136 dev_t dca_devt;
137 int dca_flags;
138 vnode_t *dca_vp;
139 krwlock_t dca_lock;
140 char dca_name[SYSPATHLEN];
141 } devicecache_t;
143 /* list of dyn. + persist. config'ed dev's */
144 static devicecache_t sysmcache[MAXDEVS];
145 static kmutex_t dcvp_mutex;
146 static vnode_t *dcvp = NULL;
147 static boolean_t sysmsg_opened;
148 static boolean_t msglog_opened;
150 /* flags for device cache */
151 #define SYSM_DISABLED 0x0
152 #define SYSM_ENABLED 0x1
155 * Module linkage information for the kernel.
158 static struct modldrv modldrv = {
159 &mod_driverops, /* Type of module. This one is a pseudo driver */
160 "System message redirection (fanout) driver",
161 &sysm_ops, /* driver ops */
164 static struct modlinkage modlinkage = {
165 MODREV_1,
166 &modldrv,
167 NULL
171 _init(void)
173 return (mod_install(&modlinkage));
177 _fini(void)
179 return (mod_remove(&modlinkage));
183 _info(struct modinfo *modinfop)
185 return (mod_info(&modlinkage, modinfop));
189 * DDI glue routines
191 static int
192 sysm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
194 int i;
196 switch (cmd) {
197 case DDI_ATTACH:
198 ASSERT(sysm_dip == NULL);
200 if (ddi_create_minor_node(devi, "sysmsg", S_IFCHR,
201 SYS_SYSMIN, DDI_PSEUDO, NULL) == DDI_FAILURE ||
202 ddi_create_minor_node(devi, "msglog", S_IFCHR,
203 SYS_MSGMIN, DDI_PSEUDO, NULL) == DDI_FAILURE) {
204 ddi_remove_minor_node(devi, NULL);
205 return (DDI_FAILURE);
208 for (i = 0; i < MAXDEVS; i++) {
209 rw_init(&sysmcache[i].dca_lock, NULL, RW_DRIVER, NULL);
212 sysm_dip = devi;
213 return (DDI_SUCCESS);
214 case DDI_SUSPEND:
215 case DDI_PM_SUSPEND:
216 return (DDI_SUCCESS);
217 default:
218 return (DDI_FAILURE);
222 static int
223 sysm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
225 int i;
227 switch (cmd) {
228 case DDI_DETACH:
229 ASSERT(sysm_dip == devi);
231 for (i = 0; i < MAXDEVS; i++)
232 rw_destroy(&sysmcache[i].dca_lock);
234 ddi_remove_minor_node(devi, NULL);
235 sysm_dip = NULL;
236 return (DDI_SUCCESS);
238 case DDI_SUSPEND:
239 case DDI_PM_SUSPEND:
240 return (DDI_SUCCESS);
241 default:
242 return (DDI_FAILURE);
247 /* ARGSUSED */
248 static int
249 sysm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
251 int rval = DDI_FAILURE;
252 minor_t instance;
254 instance = getminor((dev_t)arg);
256 switch (infocmd) {
257 case DDI_INFO_DEVT2DEVINFO:
258 if (sysm_dip != NULL &&
259 (instance == SYS_SYSMIN || instance == SYS_MSGMIN)) {
260 *result = sysm_dip;
261 rval = DDI_SUCCESS;
263 break;
265 case DDI_INFO_DEVT2INSTANCE:
266 if (instance == SYS_SYSMIN || instance == SYS_MSGMIN) {
267 *result = NULL;
268 rval = DDI_SUCCESS;
270 break;
272 default:
273 break;
276 return (rval);
280 * Parse the contents of the buffer, and bind the named
281 * devices as auxiliary consoles using our own ioctl routine.
283 * Comments begin with '#' and are terminated only by a newline
284 * Device names begin with a '/', and are terminated by a newline,
285 * space, '#' or tab.
287 static void
288 parse_buffer(char *buf, ssize_t fsize)
290 char *ebuf = buf + fsize;
291 char *devname = NULL;
292 int eatcomments = 0;
294 while (buf < ebuf) {
295 if (eatcomments) {
296 if (*buf++ == '\n')
297 eatcomments = 0;
298 continue;
300 switch (*buf) {
301 case '/':
302 if (devname == NULL)
303 devname = buf;
304 break;
305 case '#':
306 eatcomments = 1;
307 /*FALLTHROUGH*/
308 case ' ':
309 case '\t':
310 case '\n':
311 *buf = '\0';
312 if (devname == NULL)
313 break;
314 (void) sysmioctl(NODEV, CIOCSETCONSOLE,
315 (intptr_t)devname, FNATIVE|FKIOCTL|FREAD|FWRITE,
316 kcred, NULL);
317 devname = NULL;
318 break;
319 default:
320 break;
322 buf++;
326 #define CNSADM_BYTES_MAX 2000 /* XXX nasty fixed size */
328 static void
329 bind_consadm_conf(char *path)
331 struct vattr vattr;
332 vnode_t *vp;
333 void *buf;
334 size_t size;
335 ssize_t resid;
336 int err = 0;
338 if (vn_open(path, UIO_SYSSPACE, FREAD, 0, &vp, 0, 0) != 0)
339 return;
340 vattr.va_mask = AT_SIZE;
341 if ((err = VOP_GETATTR(vp, &vattr, 0, kcred, NULL)) != 0) {
342 cmn_err(CE_WARN, "sysmsg: getattr: '%s': error %d",
343 path, err);
344 goto closevp;
347 size = vattr.va_size > CNSADM_BYTES_MAX ?
348 CNSADM_BYTES_MAX : (ssize_t)vattr.va_size;
349 buf = kmem_alloc(size, KM_SLEEP);
351 if ((err = vn_rdwr(UIO_READ, vp, buf, size, (offset_t)0,
352 UIO_SYSSPACE, 0, (rlim64_t)0, kcred, &resid)) != 0)
353 cmn_err(CE_WARN, "sysmsg: vn_rdwr: '%s': error %d",
354 path, err);
355 else
356 parse_buffer(buf, size - resid);
358 kmem_free(buf, size);
359 closevp:
360 (void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, kcred, NULL);
361 VN_RELE(vp);
364 /* ARGSUSED */
365 static int
366 sysmopen(dev_t *dev, int flag, int state, cred_t *cred)
368 int i;
369 vnode_t *vp;
370 minor_t instance;
371 static boolean_t initialized;
373 instance = getminor(*dev);
375 if (state != OTYP_CHR || (instance != 0 && instance != 1))
376 return (ENXIO);
378 mutex_enter(&dcvp_mutex);
379 if ((dcvp == NULL) && (vn_open("/dev/console",
380 UIO_SYSSPACE, FWRITE, 0, &dcvp, 0, 0) != 0)) {
381 mutex_exit(&dcvp_mutex);
382 return (ENXIO);
385 if (instance == SYS_SYSMIN)
386 sysmsg_opened = B_TRUE;
387 else
388 msglog_opened = B_TRUE;
390 if (!initialized) {
391 bind_consadm_conf("/etc/consadm.conf");
392 initialized = B_TRUE;
394 mutex_exit(&dcvp_mutex);
396 for (i = 0; i < MAXDEVS; i++) {
397 rw_enter(&sysmcache[i].dca_lock, RW_WRITER);
398 if ((sysmcache[i].dca_flags & SYSM_ENABLED) &&
399 sysmcache[i].dca_vp == NULL) {
401 * 4196476 - FTRUNC was causing E10K to return EINVAL
402 * on open
404 flag = flag & ~FTRUNC;
406 * Open failures on the auxiliary consoles are
407 * not returned because we don't care if some
408 * subset get an error. We know the default console
409 * is okay, and preserve the semantics of the
410 * open for the default console.
411 * Set NONBLOCK|NDELAY in case there's no carrier.
413 if (vn_open(sysmcache[i].dca_name, UIO_SYSSPACE,
414 flag | FNONBLOCK | FNDELAY, 0, &vp, 0, 0) == 0)
415 sysmcache[i].dca_vp = vp;
417 rw_exit(&sysmcache[i].dca_lock);
420 return (0);
423 /* ARGSUSED */
424 static int
425 sysmclose(dev_t dev, int flag, int state, cred_t *cred)
427 int i;
428 minor_t instance;
430 ASSERT(dcvp != NULL);
432 if (state != OTYP_CHR)
433 return (ENXIO);
435 instance = getminor(dev);
437 mutex_enter(&dcvp_mutex);
438 if (instance == SYS_SYSMIN)
439 sysmsg_opened = B_FALSE;
440 else
441 msglog_opened = B_FALSE;
443 if (sysmsg_opened || msglog_opened) {
444 mutex_exit(&dcvp_mutex);
445 return (0);
448 (void) VOP_CLOSE(dcvp, FWRITE, 1, (offset_t)0, kcred, NULL);
449 VN_RELE(dcvp);
450 dcvp = NULL;
451 mutex_exit(&dcvp_mutex);
454 * Close the auxiliary consoles, we're not concerned with
455 * passing up the errors.
457 for (i = 0; i < MAXDEVS; i++) {
458 rw_enter(&sysmcache[i].dca_lock, RW_WRITER);
459 if (sysmcache[i].dca_vp != NULL) {
460 (void) VOP_CLOSE(sysmcache[i].dca_vp, flag,
461 1, (offset_t)0, cred, NULL);
462 VN_RELE(sysmcache[i].dca_vp);
463 sysmcache[i].dca_vp = NULL;
465 rw_exit(&sysmcache[i].dca_lock);
468 return (0);
471 /* Reads occur only on the default console */
473 /* ARGSUSED */
474 static int
475 sysmread(dev_t dev, struct uio *uio, cred_t *cred)
477 ASSERT(dcvp != NULL);
478 return (VOP_READ(dcvp, uio, 0, cred, NULL));
481 /* ARGSUSED */
482 static int
483 sysmwrite(dev_t dev, struct uio *uio, cred_t *cred)
485 int i = 0;
486 iovec_t uio_iov;
487 struct uio tuio;
489 ASSERT(dcvp != NULL);
490 ASSERT(uio != NULL);
492 for (i = 0; i < MAXDEVS; i++) {
493 rw_enter(&sysmcache[i].dca_lock, RW_READER);
494 if (sysmcache[i].dca_vp != NULL &&
495 (sysmcache[i].dca_flags & SYSM_ENABLED)) {
496 tuio = *uio;
497 uio_iov = *(uio->uio_iov);
498 tuio.uio_iov = &uio_iov;
499 (void) VOP_WRITE(sysmcache[i].dca_vp, &tuio, 0, cred,
500 NULL);
502 rw_exit(&sysmcache[i].dca_lock);
504 return (VOP_WRITE(dcvp, uio, 0, cred, NULL));
507 /* ARGSUSED */
508 static int
509 sysmioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred, int *rvalp)
511 int rval = 0;
512 int error = 0;
513 size_t size = 0;
514 int i;
515 char *infop;
516 char found = 0;
517 dev_t newdevt = (dev_t)NODEV; /* because 0 == /dev/console */
518 vnode_t *vp;
520 switch (cmd) {
521 case CIOCGETCONSOLE:
522 /* Sum over the number of enabled devices */
523 for (i = 0; i < MAXDEVS; i++) {
524 if (sysmcache[i].dca_flags & SYSM_ENABLED)
525 /* list is space separated, followed by NULL */
526 size += strlen(sysmcache[i].dca_name) + 1;
528 if (size == 0)
529 return (0);
530 break;
531 case CIOCSETCONSOLE:
532 case CIOCRMCONSOLE:
533 size = sizeof (sysmcache[0].dca_name);
534 break;
535 case CIOCTTYCONSOLE:
537 dev_t d;
538 dev32_t d32;
539 extern dev_t rwsconsdev, rconsdev, uconsdev;
540 proc_t *p;
542 if (drv_getparm(UPROCP, &p) != 0)
543 return (ENODEV);
544 else
545 d = cttydev(p);
547 * If the controlling terminal is the real
548 * or workstation console device, map to what the
549 * user thinks is the console device.
551 if (d == rwsconsdev || d == rconsdev)
552 d = uconsdev;
553 if ((flag & FMODELS) != FNATIVE) {
554 if (!cmpldev(&d32, d))
555 return (EOVERFLOW);
556 if (ddi_copyout(&d32, (caddr_t)arg, sizeof (d32),
557 flag))
558 return (EFAULT);
559 } else {
560 if (ddi_copyout(&d, (caddr_t)arg, sizeof (d), flag))
561 return (EFAULT);
563 return (0);
565 default:
566 /* everything else is sent to the console device */
567 return (VOP_IOCTL(dcvp, cmd, arg, flag, cred, rvalp, NULL));
570 if ((rval = secpolicy_console(cred)) != 0)
571 return (EPERM);
573 infop = kmem_alloc(size, KM_SLEEP);
574 if (flag & FKIOCTL)
575 error = copystr((caddr_t)arg, infop, size, NULL);
576 else
577 error = copyinstr((caddr_t)arg, infop, size, NULL);
579 if (error) {
580 switch (cmd) {
581 case CIOCGETCONSOLE:
583 * If the buffer is null, then return a byte count
584 * to user land.
586 *rvalp = size;
587 goto err_exit;
588 default:
589 rval = EFAULT;
590 goto err_exit;
594 if (infop[0] != NULL) {
595 if ((rval = lookupname(infop, UIO_SYSSPACE, FOLLOW,
596 NULLVPP, &vp)) == 0) {
597 if (vp->v_type != VCHR) {
598 VN_RELE(vp);
599 rval = EINVAL;
600 goto err_exit;
602 newdevt = vp->v_rdev;
603 VN_RELE(vp);
604 } else
605 goto err_exit;
608 switch (cmd) {
609 case CIOCGETCONSOLE:
611 * Return the list of device names that are enabled.
613 for (i = 0; i < MAXDEVS; i++) {
614 rw_enter(&sysmcache[i].dca_lock, RW_READER);
615 if (sysmcache[i].dca_flags & SYSM_ENABLED) {
616 if (infop[0] != NULL)
617 (void) strcat(infop, " ");
618 (void) strcat(infop, sysmcache[i].dca_name);
620 rw_exit(&sysmcache[i].dca_lock);
622 if (rval == 0 && copyoutstr(infop, (void *)arg, size, NULL))
623 rval = EFAULT;
624 break;
626 case CIOCSETCONSOLE:
627 if ((rval = checkarg(newdevt)) != 0)
628 break;
630 * The device does not have to be open or disabled to
631 * perform the set console.
633 for (i = 0; i < MAXDEVS; i++) {
634 rw_enter(&sysmcache[i].dca_lock, RW_WRITER);
635 if (sysmcache[i].dca_devt == newdevt &&
636 (sysmcache[i].dca_flags & SYSM_ENABLED)) {
637 (void) strcpy(sysmcache[i].dca_name, infop);
638 rval = EEXIST;
639 rw_exit(&sysmcache[i].dca_lock);
640 break;
641 } else if (sysmcache[i].dca_devt == newdevt &&
642 sysmcache[i].dca_flags == SYSM_DISABLED) {
643 sysmcache[i].dca_flags |= SYSM_ENABLED;
644 (void) strcpy(sysmcache[i].dca_name, infop);
645 rw_exit(&sysmcache[i].dca_lock);
646 found = 1;
647 break;
648 } else if (sysmcache[i].dca_devt == 0) {
649 ASSERT(sysmcache[i].dca_vp == NULL &&
650 sysmcache[i].dca_flags == SYSM_DISABLED);
651 (void) strcpy(sysmcache[i].dca_name, infop);
652 sysmcache[i].dca_flags = SYSM_ENABLED;
653 sysmcache[i].dca_devt = newdevt;
654 rw_exit(&sysmcache[i].dca_lock);
655 found = 1;
656 break;
658 rw_exit(&sysmcache[i].dca_lock);
660 if (found == 0 && rval == 0)
661 rval = ENOENT;
662 break;
664 case CIOCRMCONSOLE:
665 for (i = 0; i < MAXDEVS; i++) {
666 rw_enter(&sysmcache[i].dca_lock, RW_WRITER);
667 if (sysmcache[i].dca_devt == newdevt) {
668 sysmcache[i].dca_flags = SYSM_DISABLED;
669 sysmcache[i].dca_name[0] = '\0';
670 rw_exit(&sysmcache[i].dca_lock);
671 found = 1;
672 break;
674 rw_exit(&sysmcache[i].dca_lock);
676 if (found == 0)
677 rval = ENOENT;
678 break;
680 default:
681 break;
684 err_exit:
685 kmem_free(infop, size);
686 return (rval);
689 /* As with the read, we poll only the default console */
691 /* ARGSUSED */
692 static int
693 sysmpoll(dev_t dev, short events, int anyyet, short *reventsp,
694 struct pollhead **phpp)
696 return (VOP_POLL(dcvp, events, anyyet, reventsp, phpp, NULL));
699 /* Sanity check that the device is good */
700 static int
701 checkarg(dev_t devt)
703 int rval = 0;
704 dev_t sysmsg_dev, msglog_dev;
705 extern dev_t rwsconsdev, rconsdev, uconsdev;
707 if (devt == rconsdev || devt == rwsconsdev || devt == uconsdev) {
708 rval = EBUSY;
709 } else {
710 sysmsg_dev = makedevice(ddi_driver_major(sysm_dip), SYS_SYSMIN);
711 msglog_dev = makedevice(ddi_driver_major(sysm_dip), SYS_MSGMIN);
712 if (devt == sysmsg_dev || devt == msglog_dev)
713 rval = EINVAL;
716 return (rval);