10SN/A/*- 216932Sserb * SPDX-License-Identifier: BSD-2-Clause 30SN/A * 40SN/A * Copyright (c) 2003 Poul-Henning Kamp. All rights reserved. 50SN/A * 60SN/A * Redistribution and use in source and binary forms, with or without 72362SN/A * modification, are permitted provided that the following conditions 80SN/A * are met: 92362SN/A * 1. Redistributions of source code must retain the above copyright 100SN/A * notice, this list of conditions and the following disclaimer. 110SN/A * 2. Redistributions in binary form must reproduce the above copyright 120SN/A * notice, this list of conditions and the following disclaimer in the 130SN/A * documentation and/or other materials provided with the distribution. 140SN/A * 150SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 160SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 170SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 180SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 190SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 200SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212362SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 222362SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 232362SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 240SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 250SN/A * SUCH DAMAGE. 260SN/A */ 270SN/A 280SN/A#include <sys/param.h> 290SN/A#include <sys/systm.h> 300SN/A#include <sys/eventhandler.h> 311696SN/A#include <sys/conf.h> 320SN/A#include <sys/kernel.h> 330SN/A#include <sys/proc.h> 340SN/A#include <sys/sx.h> 350SN/A#include <sys/vnode.h> 360SN/A 370SN/A#include <fs/devfs/devfs.h> 380SN/A#include <fs/devfs/devfs_int.h> 390SN/A 400SN/Astatic d_open_t cttyopen; 410SN/A 420SN/Astatic struct cdevsw ctty_cdevsw = { 430SN/A .d_version = D_VERSION, 440SN/A .d_open = cttyopen, 4516932Sserb .d_name = "ctty", 461696SN/A}; 470SN/A 480SN/Astatic struct cdev *ctty; 490SN/A 500SN/Astatic int 510SN/Acttyopen(struct cdev *dev, int flag, int mode, struct thread *td) 520SN/A{ 5312839Sprr 5412839Sprr return (ENXIO); 5512839Sprr} 5612839Sprr 570SN/Astatic void 580SN/Actty_clone(void *arg, struct ucred *cred, char *name, int namelen, 590SN/A struct cdev **dev) 600SN/A{ 610SN/A struct proc *p; 620SN/A 630SN/A if (*dev != NULL) 640SN/A return; 650SN/A if (strcmp(name, "tty")) 660SN/A return; 670SN/A p = curproc; 680SN/A sx_slock(&proctree_lock); 690SN/A dev_lock(); 700SN/A if (!(p->p_flag & P_CONTROLT)) 710SN/A *dev = ctty; 720SN/A else if (p->p_session->s_ttyvp == NULL) 730SN/A *dev = ctty; 740SN/A else if (p->p_session->s_ttyvp->v_type == VBAD || 750SN/A p->p_session->s_ttyvp->v_rdev == NULL) { 760SN/A /* e.g. s_ttyvp was revoked */ 770SN/A *dev = ctty; 780SN/A } else 790SN/A *dev = p->p_session->s_ttyvp->v_rdev; 800SN/A dev_refl(*dev); 810SN/A dev_unlock(); 820SN/A sx_sunlock(&proctree_lock); 830SN/A} 840SN/A 850SN/Astatic void 860SN/Actty_drvinit(void *unused) 870SN/A{ 880SN/A 890SN/A EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000); 908565SN/A ctty = make_dev_credf(MAKEDEV_ETERNAL, &ctty_cdevsw, 0, NULL, UID_ROOT, 910SN/A GID_WHEEL, 0666, "ctty"); 920SN/A} 930SN/A 940SN/ASYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ctty_drvinit,NULL); 950SN/A