1/*- 2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Portions of this software were developed under sponsorship from Snow 6 * B.V., the Netherlands. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Portions of this software were developed under sponsorship from Snow 6 * B.V., the Netherlands. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h>
|
31__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 181905 2008-08-20 08:31:58Z ed $");
| 31__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 182069 2008-08-23 16:03:00Z ed $");
|
32 33#include <sys/param.h> 34#include <sys/conf.h> 35#include <sys/eventhandler.h> 36#include <sys/fcntl.h> 37#include <sys/kernel.h> 38#include <sys/proc.h>
| 32 33#include <sys/param.h> 34#include <sys/conf.h> 35#include <sys/eventhandler.h> 36#include <sys/fcntl.h> 37#include <sys/kernel.h> 38#include <sys/proc.h>
|
| 39#include <sys/sysctl.h>
|
39#include <sys/systm.h> 40#include <sys/tty.h> 41 42/* 43 * This driver implements a BSD-style compatibility naming scheme for 44 * the pts(4) driver. We just call into pts(4) to create the actual PTY. 45 * To make sure we don't use the same PTY multiple times, we abuse 46 * si_drv1 inside the cdev to mark whether the PTY is in use. 47 */ 48
| 40#include <sys/systm.h> 41#include <sys/tty.h> 42 43/* 44 * This driver implements a BSD-style compatibility naming scheme for 45 * the pts(4) driver. We just call into pts(4) to create the actual PTY. 46 * To make sure we don't use the same PTY multiple times, we abuse 47 * si_drv1 inside the cdev to mark whether the PTY is in use. 48 */ 49
|
49static int pty_warningcnt = 10;
| 50static unsigned int pty_warningcnt = 10; 51SYSCTL_UINT(_kern, OID_AUTO, tty_pty_warningcnt, CTLFLAG_RW, 52 &pty_warningcnt, 0, 53 "Warnings that will be triggered upon PTY allocation");
|
50 51static int 52ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp) 53{ 54 int u, error; 55 char name[] = "ttyXX"; 56 57 if (!atomic_cmpset_ptr((uintptr_t *)&dev->si_drv1, 0, 1)) 58 return (EBUSY); 59 60 /* Generate device name and create PTY. */ 61 u = dev2unit(dev); 62 name[3] = u >> 8; 63 name[4] = u; 64 65 error = pts_alloc_external(fflags & (FREAD|FWRITE), td, fp, dev, name); 66 if (error != 0) { 67 destroy_dev_sched(dev); 68 return (error); 69 } 70 71 /* Raise a warning when a legacy PTY has been allocated. */ 72 if (pty_warningcnt > 0) { 73 pty_warningcnt--; 74 printf("pid %d (%s) is using legacy pty devices%s\n", 75 td->td_proc->p_pid, td->td_name, 76 pty_warningcnt ? "" : " - not logging anymore"); 77 } 78 79 return (0); 80} 81 82static struct cdevsw ptydev_cdevsw = { 83 .d_version = D_VERSION, 84 .d_fdopen = ptydev_fdopen, 85 .d_name = "ptydev", 86}; 87 88static void 89pty_clone(void *arg, struct ucred *cr, char *name, int namelen, 90 struct cdev **dev) 91{ 92 int u; 93 94 /* Cloning is already satisfied. */ 95 if (*dev != NULL) 96 return; 97 98 /* Only catch /dev/ptyXX. */ 99 if (namelen != 5 || bcmp(name, "pty", 3) != 0) 100 return; 101 102 /* Only catch /dev/pty[l-sL-S]X. */ 103 if (!(name[3] >= 'l' && name[3] <= 's') && 104 !(name[3] >= 'L' && name[3] <= 'S')) 105 return; 106 107 /* Only catch /dev/pty[l-sL-S][0-9a-v]. */ 108 if (!(name[4] >= '0' && name[4] <= '9') && 109 !(name[4] >= 'a' && name[4] <= 'v')) 110 return; 111 112 /* Create the controller device node. */ 113 u = (unsigned int)name[3] << 8 | name[4]; 114 *dev = make_dev_credf(MAKEDEV_REF, &ptydev_cdevsw, u, 115 NULL, UID_ROOT, GID_WHEEL, 0666, name); 116} 117 118static void 119pty_init(void *unused) 120{ 121 122 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 123} 124 125SYSINIT(pty, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pty_init, NULL);
| 54 55static int 56ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp) 57{ 58 int u, error; 59 char name[] = "ttyXX"; 60 61 if (!atomic_cmpset_ptr((uintptr_t *)&dev->si_drv1, 0, 1)) 62 return (EBUSY); 63 64 /* Generate device name and create PTY. */ 65 u = dev2unit(dev); 66 name[3] = u >> 8; 67 name[4] = u; 68 69 error = pts_alloc_external(fflags & (FREAD|FWRITE), td, fp, dev, name); 70 if (error != 0) { 71 destroy_dev_sched(dev); 72 return (error); 73 } 74 75 /* Raise a warning when a legacy PTY has been allocated. */ 76 if (pty_warningcnt > 0) { 77 pty_warningcnt--; 78 printf("pid %d (%s) is using legacy pty devices%s\n", 79 td->td_proc->p_pid, td->td_name, 80 pty_warningcnt ? "" : " - not logging anymore"); 81 } 82 83 return (0); 84} 85 86static struct cdevsw ptydev_cdevsw = { 87 .d_version = D_VERSION, 88 .d_fdopen = ptydev_fdopen, 89 .d_name = "ptydev", 90}; 91 92static void 93pty_clone(void *arg, struct ucred *cr, char *name, int namelen, 94 struct cdev **dev) 95{ 96 int u; 97 98 /* Cloning is already satisfied. */ 99 if (*dev != NULL) 100 return; 101 102 /* Only catch /dev/ptyXX. */ 103 if (namelen != 5 || bcmp(name, "pty", 3) != 0) 104 return; 105 106 /* Only catch /dev/pty[l-sL-S]X. */ 107 if (!(name[3] >= 'l' && name[3] <= 's') && 108 !(name[3] >= 'L' && name[3] <= 'S')) 109 return; 110 111 /* Only catch /dev/pty[l-sL-S][0-9a-v]. */ 112 if (!(name[4] >= '0' && name[4] <= '9') && 113 !(name[4] >= 'a' && name[4] <= 'v')) 114 return; 115 116 /* Create the controller device node. */ 117 u = (unsigned int)name[3] << 8 | name[4]; 118 *dev = make_dev_credf(MAKEDEV_REF, &ptydev_cdevsw, u, 119 NULL, UID_ROOT, GID_WHEEL, 0666, name); 120} 121 122static void 123pty_init(void *unused) 124{ 125 126 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 127} 128 129SYSINIT(pty, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pty_init, NULL);
|