pty.c revision 298337
168651Skris/*- 268651Skris * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 368651Skris * All rights reserved. 468651Skris * 568651Skris * Portions of this software were developed under sponsorship from Snow 668651Skris * B.V., the Netherlands. 768651Skris * 868651Skris * Redistribution and use in source and binary forms, with or without 968651Skris * modification, are permitted provided that the following conditions 1068651Skris * are met: 1168651Skris * 1. Redistributions of source code must retain the above copyright 1268651Skris * notice, this list of conditions and the following disclaimer. 1368651Skris * 2. Redistributions in binary form must reproduce the above copyright 1468651Skris * notice, this list of conditions and the following disclaimer in the 1568651Skris * documentation and/or other materials provided with the distribution. 1668651Skris * 1768651Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1868651Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1968651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2068651Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2168651Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2268651Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2368651Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2468651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2568651Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2668651Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2768651Skris * SUCH DAMAGE. 2868651Skris */ 2968651Skris 3068651Skris#include <sys/cdefs.h> 3168651Skris__FBSDID("$FreeBSD: head/sys/dev/pty/pty.c 298337 2016-04-20 04:50:33Z cem $"); 3268651Skris 3368651Skris#include <sys/param.h> 3468651Skris#include <sys/conf.h> 3568651Skris#include <sys/eventhandler.h> 3668651Skris#include <sys/fcntl.h> 3768651Skris#include <sys/kernel.h> 3868651Skris#include <sys/module.h> 3968651Skris#include <sys/proc.h> 4072613Skris#include <sys/sysctl.h> 4168651Skris#include <sys/syslog.h> 4268651Skris#include <sys/systm.h> 4368651Skris#include <sys/tty.h> 44 45/* 46 * This driver implements a BSD-style compatibility naming scheme for 47 * the pts(4) driver. We just call into pts(4) to create the actual PTY. 48 * To make sure we don't use the same PTY multiple times, we abuse 49 * si_drv1 inside the cdev to mark whether the PTY is in use. 50 * 51 * It also implements a /dev/ptmx device node, which is useful for Linux 52 * binary emulation. 53 */ 54 55static unsigned int pty_warningcnt = 1; 56SYSCTL_UINT(_kern, OID_AUTO, tty_pty_warningcnt, CTLFLAG_RW, 57 &pty_warningcnt, 0, 58 "Warnings that will be triggered upon legacy PTY allocation"); 59 60static int 61ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp) 62{ 63 int error; 64 char name[6]; /* "ttyXX" */ 65 66 if (!atomic_cmpset_ptr((uintptr_t *)&dev->si_drv1, 0, 1)) 67 return (EBUSY); 68 69 /* Generate device name and create PTY. */ 70 strlcpy(name, devtoname(dev), sizeof(name)); 71 name[0] = 't'; 72 73 error = pts_alloc_external(fflags & (FREAD|FWRITE), td, fp, dev, name); 74 if (error != 0) { 75 destroy_dev_sched(dev); 76 return (error); 77 } 78 79 /* Raise a warning when a legacy PTY has been allocated. */ 80 if (pty_warningcnt > 0) { 81 pty_warningcnt--; 82 log(LOG_INFO, "pid %d (%s) is using legacy pty devices%s\n", 83 td->td_proc->p_pid, td->td_name, 84 pty_warningcnt ? "" : " - not logging anymore"); 85 } 86 87 return (0); 88} 89 90static struct cdevsw ptydev_cdevsw = { 91 .d_version = D_VERSION, 92 .d_fdopen = ptydev_fdopen, 93 .d_name = "ptydev", 94}; 95 96static void 97pty_clone(void *arg, struct ucred *cr, char *name, int namelen, 98 struct cdev **dev) 99{ 100 struct make_dev_args mda; 101 int error; 102 103 /* Cloning is already satisfied. */ 104 if (*dev != NULL) 105 return; 106 107 /* Only catch /dev/ptyXX. */ 108 if (namelen != 5 || bcmp(name, "pty", 3) != 0) 109 return; 110 111 /* Only catch /dev/pty[l-sL-S]X. */ 112 if (!(name[3] >= 'l' && name[3] <= 's') && 113 !(name[3] >= 'L' && name[3] <= 'S')) 114 return; 115 116 /* Only catch /dev/pty[l-sL-S][0-9a-v]. */ 117 if (!(name[4] >= '0' && name[4] <= '9') && 118 !(name[4] >= 'a' && name[4] <= 'v')) 119 return; 120 121 /* Create the controller device node. */ 122 make_dev_args_init(&mda); 123 mda.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_REF; 124 mda.mda_devsw = &ptydev_cdevsw; 125 mda.mda_uid = UID_ROOT; 126 mda.mda_gid = GID_WHEEL; 127 mda.mda_mode = 0666; 128 error = make_dev_s(&mda, dev, "%s", name); 129 if (error != 0) 130 *dev = NULL; 131} 132 133static int 134ptmx_fdopen(struct cdev *dev __unused, int fflags, struct thread *td, 135 struct file *fp) 136{ 137 138 return (pts_alloc(fflags & (FREAD|FWRITE), td, fp)); 139} 140 141static struct cdevsw ptmx_cdevsw = { 142 .d_version = D_VERSION, 143 .d_fdopen = ptmx_fdopen, 144 .d_name = "ptmx", 145}; 146 147static int 148pty_modevent(module_t mod, int type, void *data) 149{ 150 151 switch(type) { 152 case MOD_LOAD: 153 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 154 make_dev_credf(MAKEDEV_ETERNAL_KLD, &ptmx_cdevsw, 0, NULL, 155 UID_ROOT, GID_WHEEL, 0666, "ptmx"); 156 break; 157 case MOD_SHUTDOWN: 158 break; 159 case MOD_UNLOAD: 160 /* XXX: No unloading support yet. */ 161 return (EBUSY); 162 default: 163 return (EOPNOTSUPP); 164 } 165 166 return (0); 167} 168 169DEV_MODULE(pty, pty_modevent, NULL); 170