Home | History | Annotate | Line # | Download | only in kern
tty_bsdpty.c revision 1.9
      1  1.9        ad /*	$NetBSD: tty_bsdpty.c,v 1.9 2006/07/23 22:06:11 ad Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.1  christos  *    must display the following acknowledgement:
     17  1.1  christos  *        This product includes software developed by the NetBSD
     18  1.1  christos  *        Foundation, Inc. and its contributors.
     19  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.1  christos  *    contributors may be used to endorse or promote products derived
     21  1.1  christos  *    from this software without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos #include <sys/cdefs.h>
     37  1.9        ad __KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.9 2006/07/23 22:06:11 ad Exp $");
     38  1.1  christos 
     39  1.1  christos #include "opt_ptm.h"
     40  1.1  christos 
     41  1.1  christos #ifdef COMPAT_BSDPTY
     42  1.1  christos /* bsd tty implementation for pty multiplexor driver /dev/ptm{,x} */
     43  1.1  christos 
     44  1.1  christos #include <sys/param.h>
     45  1.1  christos #include <sys/systm.h>
     46  1.1  christos #include <sys/ioctl.h>
     47  1.5  christos #include <sys/lwp.h>
     48  1.1  christos #include <sys/tty.h>
     49  1.1  christos #include <sys/stat.h>
     50  1.1  christos #include <sys/file.h>
     51  1.1  christos #include <sys/uio.h>
     52  1.1  christos #include <sys/kernel.h>
     53  1.1  christos #include <sys/vnode.h>
     54  1.1  christos #include <sys/namei.h>
     55  1.1  christos #include <sys/signalvar.h>
     56  1.1  christos #include <sys/uio.h>
     57  1.1  christos #include <sys/filedesc.h>
     58  1.1  christos #include <sys/conf.h>
     59  1.1  christos #include <sys/poll.h>
     60  1.1  christos #include <sys/malloc.h>
     61  1.1  christos #include <sys/pty.h>
     62  1.8      elad #include <sys/kauth.h>
     63  1.1  christos 
     64  1.1  christos /*
     65  1.1  christos  * pts == /dev/tty[pqrs]?
     66  1.1  christos  * ptc == /dev/pty[pqrs]?
     67  1.1  christos  */
     68  1.1  christos 
     69  1.2  christos /*
     70  1.2  christos  * All this hard-coding is really evil.
     71  1.2  christos  */
     72  1.3     perry #define TTY_GID		4
     73  1.2  christos #define TTY_PERM	(S_IRUSR|S_IWUSR|S_IWGRP)
     74  1.1  christos #define TTY_TEMPLATE	"/dev/XtyXX"
     75  1.1  christos #define TTY_NAMESIZE	sizeof(TTY_TEMPLATE)
     76  1.1  christos #define TTY_LETTERS	"pqrstuvwxyzPQRST"
     77  1.1  christos #define TTY_OLD_SUFFIX  "0123456789abcdef"
     78  1.1  christos #define TTY_NEW_SUFFIX  "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     79  1.1  christos 
     80  1.7  christos static int pty_makename(struct ptm_pty *, struct lwp *, char *, size_t, dev_t,
     81  1.7  christos     char);
     82  1.5  christos static int pty_allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
     83  1.1  christos     dev_t, char);
     84  1.9        ad static void pty_getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
     85  1.1  christos 
     86  1.1  christos struct ptm_pty ptm_bsdpty = {
     87  1.1  christos 	pty_allocvp,
     88  1.1  christos 	pty_makename,
     89  1.2  christos 	pty_getvattr,
     90  1.1  christos 	NULL
     91  1.1  christos };
     92  1.1  christos 
     93  1.1  christos static int
     94  1.1  christos /*ARGSUSED*/
     95  1.7  christos pty_makename(struct ptm_pty *ptm, struct lwp *l, char *bf, size_t bufsiz,
     96  1.7  christos     dev_t dev, char c)
     97  1.1  christos {
     98  1.1  christos 	size_t nt;
     99  1.1  christos 	dev_t minor = minor(dev);
    100  1.6  christos 	const char *suffix;
    101  1.6  christos 
    102  1.1  christos 	if (bufsiz < TTY_NAMESIZE)
    103  1.1  christos 		return EINVAL;
    104  1.6  christos 
    105  1.4  christos 	(void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE);
    106  1.1  christos 
    107  1.1  christos 	if (minor < 256) {
    108  1.6  christos 		suffix = TTY_OLD_SUFFIX;
    109  1.1  christos 		nt = sizeof(TTY_OLD_SUFFIX) - 1;
    110  1.1  christos 	} else {
    111  1.1  christos 		minor -= 256;
    112  1.6  christos 		suffix = TTY_NEW_SUFFIX;
    113  1.6  christos 		nt = sizeof(TTY_NEW_SUFFIX) - 1;
    114  1.1  christos 	}
    115  1.6  christos 
    116  1.6  christos 	bf[5] = c;
    117  1.6  christos 	bf[8] = TTY_LETTERS[minor / nt];
    118  1.6  christos 	bf[9] = suffix[minor % nt];
    119  1.1  christos 	return 0;
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos 
    123  1.1  christos static int
    124  1.1  christos /*ARGSUSED*/
    125  1.5  christos pty_allocvp(struct ptm_pty *ptm, struct lwp *l, struct vnode **vp, dev_t dev,
    126  1.1  christos     char ms)
    127  1.1  christos {
    128  1.1  christos 	int error;
    129  1.1  christos 	struct nameidata nd;
    130  1.1  christos 	char name[TTY_NAMESIZE];
    131  1.1  christos 
    132  1.7  christos 	error = (*ptm->makename)(ptm, l, name, sizeof(name), dev, ms);
    133  1.1  christos 	if (error)
    134  1.1  christos 		return error;
    135  1.1  christos 
    136  1.5  christos 	NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name, l);
    137  1.1  christos 	if ((error = namei(&nd)) != 0)
    138  1.1  christos 		return error;
    139  1.1  christos 	*vp = nd.ni_vp;
    140  1.1  christos 	return 0;
    141  1.1  christos }
    142  1.2  christos 
    143  1.2  christos 
    144  1.2  christos static void
    145  1.2  christos /*ARGSUSED*/
    146  1.9        ad pty_getvattr(struct ptm_pty *ptm, struct lwp *l, struct vattr *vattr)
    147  1.2  christos {
    148  1.2  christos 	VATTR_NULL(vattr);
    149  1.2  christos 	/* get real uid */
    150  1.9        ad 	vattr->va_uid = kauth_cred_getuid(l->l_cred);
    151  1.2  christos 	vattr->va_gid = TTY_GID;
    152  1.2  christos 	vattr->va_mode = TTY_PERM;
    153  1.2  christos }
    154  1.1  christos #endif
    155