Home | History | Annotate | Line # | Download | only in kern
tty_bsdpty.c revision 1.3
      1  1.3     perry /*	$NetBSD: tty_bsdpty.c,v 1.3 2005/02/26 21:34:55 perry 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.3     perry __KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.3 2005/02/26 21:34:55 perry 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.1  christos #include <sys/proc.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.1  christos 
     63  1.1  christos /*
     64  1.1  christos  * pts == /dev/tty[pqrs]?
     65  1.1  christos  * ptc == /dev/pty[pqrs]?
     66  1.1  christos  */
     67  1.1  christos 
     68  1.2  christos /*
     69  1.2  christos  * All this hard-coding is really evil.
     70  1.2  christos  */
     71  1.3     perry #define TTY_GID		4
     72  1.2  christos #define TTY_PERM	(S_IRUSR|S_IWUSR|S_IWGRP)
     73  1.1  christos #define TTY_TEMPLATE	"/dev/XtyXX"
     74  1.1  christos #define TTY_NAMESIZE	sizeof(TTY_TEMPLATE)
     75  1.1  christos #define TTY_LETTERS	"pqrstuvwxyzPQRST"
     76  1.1  christos #define TTY_OLD_SUFFIX  "0123456789abcdef"
     77  1.1  christos #define TTY_NEW_SUFFIX  "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     78  1.1  christos 
     79  1.1  christos static int pty_makename(struct ptm_pty *, char *, size_t, dev_t, char);
     80  1.1  christos static int pty_allocvp(struct ptm_pty *, struct proc *, struct vnode **,
     81  1.1  christos     dev_t, char);
     82  1.2  christos static void pty_getvattr(struct ptm_pty *, struct proc *, struct vattr *);
     83  1.1  christos 
     84  1.1  christos struct ptm_pty ptm_bsdpty = {
     85  1.1  christos 	pty_allocvp,
     86  1.1  christos 	pty_makename,
     87  1.2  christos 	pty_getvattr,
     88  1.1  christos 	NULL
     89  1.1  christos };
     90  1.1  christos 
     91  1.1  christos static int
     92  1.1  christos /*ARGSUSED*/
     93  1.1  christos pty_makename(struct ptm_pty *ptm, char *buf, size_t bufsiz, dev_t dev, char c)
     94  1.1  christos {
     95  1.1  christos 	size_t nt;
     96  1.1  christos 	dev_t minor = minor(dev);
     97  1.1  christos 	if (bufsiz < TTY_NAMESIZE)
     98  1.1  christos 		return EINVAL;
     99  1.1  christos 	(void)memcpy(buf, TTY_TEMPLATE, TTY_NAMESIZE);
    100  1.1  christos 
    101  1.1  christos 	buf[5] = c;
    102  1.1  christos 
    103  1.1  christos 	if (minor < 256) {
    104  1.1  christos 		nt = sizeof(TTY_OLD_SUFFIX) - 1;
    105  1.1  christos 		buf[8] = TTY_LETTERS[minor / nt];
    106  1.1  christos 		buf[9] = TTY_OLD_SUFFIX[minor % nt];
    107  1.1  christos 	} else {
    108  1.1  christos 		minor -= 256;
    109  1.1  christos 		nt = sizeof(TTY_NEW_SUFFIX) - sizeof(TTY_OLD_SUFFIX);
    110  1.1  christos 		buf[8] = TTY_LETTERS[minor / nt];
    111  1.1  christos 		buf[9] = TTY_NEW_SUFFIX[minor % nt];
    112  1.1  christos 	}
    113  1.1  christos 	return 0;
    114  1.1  christos }
    115  1.1  christos 
    116  1.1  christos 
    117  1.1  christos static int
    118  1.1  christos /*ARGSUSED*/
    119  1.1  christos pty_allocvp(struct ptm_pty *ptm, struct proc *p, struct vnode **vp, dev_t dev,
    120  1.1  christos     char ms)
    121  1.1  christos {
    122  1.1  christos 	int error;
    123  1.1  christos 	struct nameidata nd;
    124  1.1  christos 	char name[TTY_NAMESIZE];
    125  1.1  christos 
    126  1.1  christos 	error = (*ptm->makename)(ptm, name, sizeof(name), dev, ms);
    127  1.1  christos 	if (error)
    128  1.1  christos 		return error;
    129  1.1  christos 
    130  1.1  christos 	NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name, p);
    131  1.1  christos 	if ((error = namei(&nd)) != 0)
    132  1.1  christos 		return error;
    133  1.1  christos 	*vp = nd.ni_vp;
    134  1.1  christos 	return 0;
    135  1.1  christos }
    136  1.2  christos 
    137  1.2  christos 
    138  1.2  christos static void
    139  1.2  christos /*ARGSUSED*/
    140  1.2  christos pty_getvattr(struct ptm_pty *ptm, struct proc *p, struct vattr *vattr)
    141  1.2  christos {
    142  1.2  christos 	VATTR_NULL(vattr);
    143  1.2  christos 	/* get real uid */
    144  1.2  christos 	vattr->va_uid = p->p_cred->p_ruid;
    145  1.2  christos 	vattr->va_gid = TTY_GID;
    146  1.2  christos 	vattr->va_mode = TTY_PERM;
    147  1.2  christos }
    148  1.1  christos #endif
    149