2010-01-08 14:35:07 +03:00
|
|
|
/* $NetBSD: tty_bsdpty.c,v 1.16 2010/01/08 11:35:10 pooka Exp $ */
|
2004-11-10 20:29:54 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2004 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-01-08 14:35:07 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.16 2010/01/08 11:35:10 pooka Exp $");
|
2004-11-10 20:29:54 +03:00
|
|
|
|
|
|
|
#include "opt_ptm.h"
|
|
|
|
|
|
|
|
#ifdef COMPAT_BSDPTY
|
|
|
|
/* bsd tty implementation for pty multiplexor driver /dev/ptm{,x} */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/ioctl.h>
|
2005-12-11 15:16:03 +03:00
|
|
|
#include <sys/lwp.h>
|
2004-11-10 20:29:54 +03:00
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/pty.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2004-11-10 20:29:54 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pts == /dev/tty[pqrs]?
|
|
|
|
* ptc == /dev/pty[pqrs]?
|
|
|
|
*/
|
|
|
|
|
2004-11-25 01:19:27 +03:00
|
|
|
/*
|
|
|
|
* All this hard-coding is really evil.
|
|
|
|
*/
|
2005-02-27 00:34:55 +03:00
|
|
|
#define TTY_GID 4
|
2004-11-25 01:19:27 +03:00
|
|
|
#define TTY_PERM (S_IRUSR|S_IWUSR|S_IWGRP)
|
2004-11-10 20:29:54 +03:00
|
|
|
#define TTY_TEMPLATE "/dev/XtyXX"
|
|
|
|
#define TTY_NAMESIZE sizeof(TTY_TEMPLATE)
|
|
|
|
#define TTY_LETTERS "pqrstuvwxyzPQRST"
|
|
|
|
#define TTY_OLD_SUFFIX "0123456789abcdef"
|
|
|
|
#define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2006-04-13 21:44:24 +04:00
|
|
|
static int pty_makename(struct ptm_pty *, struct lwp *, char *, size_t, dev_t,
|
|
|
|
char);
|
2005-12-11 15:16:03 +03:00
|
|
|
static int pty_allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
|
2004-11-10 20:29:54 +03:00
|
|
|
dev_t, char);
|
2006-07-24 02:06:03 +04:00
|
|
|
static void pty_getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
|
2004-11-10 20:29:54 +03:00
|
|
|
|
|
|
|
struct ptm_pty ptm_bsdpty = {
|
|
|
|
pty_allocvp,
|
|
|
|
pty_makename,
|
2004-11-25 01:19:27 +03:00
|
|
|
pty_getvattr,
|
2004-11-10 20:29:54 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
/*ARGSUSED*/
|
2006-11-01 13:17:58 +03:00
|
|
|
pty_makename(struct ptm_pty *ptm, struct lwp *l, char *bf,
|
2006-10-12 05:30:41 +04:00
|
|
|
size_t bufsiz, dev_t dev, char c)
|
2004-11-10 20:29:54 +03:00
|
|
|
{
|
|
|
|
size_t nt;
|
|
|
|
dev_t minor = minor(dev);
|
2006-03-24 02:53:54 +03:00
|
|
|
const char *suffix;
|
|
|
|
|
2004-11-10 20:29:54 +03:00
|
|
|
if (bufsiz < TTY_NAMESIZE)
|
|
|
|
return EINVAL;
|
|
|
|
|
2006-03-24 02:53:54 +03:00
|
|
|
(void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE);
|
2004-11-10 20:29:54 +03:00
|
|
|
|
|
|
|
if (minor < 256) {
|
2006-03-24 02:53:54 +03:00
|
|
|
suffix = TTY_OLD_SUFFIX;
|
2004-11-10 20:29:54 +03:00
|
|
|
nt = sizeof(TTY_OLD_SUFFIX) - 1;
|
|
|
|
} else {
|
|
|
|
minor -= 256;
|
2006-03-24 02:53:54 +03:00
|
|
|
suffix = TTY_NEW_SUFFIX;
|
|
|
|
nt = sizeof(TTY_NEW_SUFFIX) - 1;
|
2004-11-10 20:29:54 +03:00
|
|
|
}
|
2006-03-24 02:53:54 +03:00
|
|
|
|
|
|
|
bf[5] = c;
|
|
|
|
bf[8] = TTY_LETTERS[minor / nt];
|
|
|
|
bf[9] = suffix[minor % nt];
|
2004-11-10 20:29:54 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
/*ARGSUSED*/
|
2005-12-11 15:16:03 +03:00
|
|
|
pty_allocvp(struct ptm_pty *ptm, struct lwp *l, struct vnode **vp, dev_t dev,
|
2004-11-10 20:29:54 +03:00
|
|
|
char ms)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
struct nameidata nd;
|
|
|
|
char name[TTY_NAMESIZE];
|
|
|
|
|
2006-04-13 21:44:24 +04:00
|
|
|
error = (*ptm->makename)(ptm, l, name, sizeof(name), dev, ms);
|
2004-11-10 20:29:54 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2007-12-08 22:29:36 +03:00
|
|
|
NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name);
|
2004-11-10 20:29:54 +03:00
|
|
|
if ((error = namei(&nd)) != 0)
|
|
|
|
return error;
|
|
|
|
*vp = nd.ni_vp;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-25 01:19:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
/*ARGSUSED*/
|
2006-11-01 13:17:58 +03:00
|
|
|
pty_getvattr(struct ptm_pty *ptm, struct lwp *l, struct vattr *vattr)
|
2004-11-25 01:19:27 +03:00
|
|
|
{
|
2010-01-08 14:35:07 +03:00
|
|
|
vattr_null(vattr);
|
2004-11-25 01:19:27 +03:00
|
|
|
/* get real uid */
|
2006-07-24 02:06:03 +04:00
|
|
|
vattr->va_uid = kauth_cred_getuid(l->l_cred);
|
2004-11-25 01:19:27 +03:00
|
|
|
vattr->va_gid = TTY_GID;
|
|
|
|
vattr->va_mode = TTY_PERM;
|
|
|
|
}
|
2004-11-10 20:29:54 +03:00
|
|
|
#endif
|