#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/proc_internal.h>
#include <sys/tty.h>
#include <sys/vnode_internal.h>
#include <sys/file_internal.h>
#ifndef NeXT
#include <sys/kernel.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif
static d_open_t cttyopen;
static d_read_t cttyread;
static d_write_t cttywrite;
static d_ioctl_t cttyioctl;
static d_select_t cttyselect;
#endif
int cttyopen(dev_t dev, int flag, int mode, struct proc *p);
int cttyread(dev_t dev, struct uio *uio, int flag);
int cttywrite(dev_t dev, struct uio *uio, int flag);
int cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p);
int cttyselect(dev_t dev, int flag, void* wql, struct proc *p);
#ifndef NeXT
#define CDEV_MAJOR 1
struct cdevsw ctty_cdevsw =
{ cttyopen, nullclose, cttyread, cttywrite,
cttyioctl, nullstop, nullreset, nodevtotty,
cttyselect, nommap, NULL, "ctty", NULL, -1 };
#endif
#define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
int
cttyopen(__unused dev_t dev, int flag, __unused int mode, struct proc *p)
{
struct vnode *ttyvp = cttyvp(p);
struct vfs_context context;
int error;
if (ttyvp == NULL)
return (ENXIO);
context.vc_proc = p;
context.vc_ucred = p->p_ucred;
error = VNOP_OPEN(ttyvp, flag, &context);
return (error);
}
int
cttyread(__unused dev_t dev, struct uio *uio, int flag)
{
struct proc *p = current_proc();
register struct vnode *ttyvp = cttyvp(p);
struct vfs_context context;
int error;
if (ttyvp == NULL)
return (EIO);
context.vc_proc = p;
context.vc_ucred = NOCRED;
error = VNOP_READ(ttyvp, uio, flag, &context);
return (error);
}
int
cttywrite(__unused dev_t dev, struct uio *uio, int flag)
{
struct proc *p = current_proc();
register struct vnode *ttyvp = cttyvp(p);
struct vfs_context context;
int error;
if (ttyvp == NULL)
return (EIO);
context.vc_proc = p;
context.vc_ucred = NOCRED;
error = VNOP_WRITE(ttyvp, uio, flag, &context);
return (error);
}
#ifndef NeXT
static int
cttyioctl(dev, cmd, addr, flag, p)
dev_t dev;
int cmd;
caddr_t addr;
int flag;
struct proc *p;
#else
int
cttyioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag,
struct proc *p)
#endif
{
struct vnode *ttyvp = cttyvp(p);
struct vfs_context context;
if (ttyvp == NULL)
return (EIO);
if (cmd == TIOCSCTTY)
return EINVAL;
if (cmd == TIOCNOTTY) {
if (!SESS_LEADER(p)) {
p->p_flag &= ~P_CONTROLT;
return (0);
} else
return (EINVAL);
}
context.vc_proc = p;
context.vc_ucred = NOCRED;
return (VNOP_IOCTL(ttyvp, cmd, addr, flag, &context));
}
int
cttyselect(__unused dev_t dev, int flag, void* wql, struct proc *p)
{
struct vnode *ttyvp = cttyvp(p);
struct vfs_context context;
context.vc_proc = p;
context.vc_ucred = NOCRED;
if (ttyvp == NULL)
return (1);
return (VNOP_SELECT(ttyvp, flag, FREAD|FWRITE, wql, &context));
}
#ifndef NeXT
static ctty_devsw_installed = 0;
#ifdef DEVFS
static void *ctty_devfs_token;
#endif
static void
ctty_drvinit(void *unused)
{
dev_t dev;
if( ! ctty_devsw_installed ) {
dev = makedev(CDEV_MAJOR,0);
cdevsw_add(&dev,&ctty_cdevsw,NULL);
ctty_devsw_installed = 1;
#ifdef DEVFS
ctty_devfs_token =
devfs_add_devswf(&ctty_cdevsw, 0, DV_CHR, 0, 0,
0666, "tty");
#endif
}
}
SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)
#endif