xref: /freebsd/sys/dev/pty/pty.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1bfdaa523SEd Schouten /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4bfdaa523SEd Schouten  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5bfdaa523SEd Schouten  * All rights reserved.
6bfdaa523SEd Schouten  *
7bfdaa523SEd Schouten  * Portions of this software were developed under sponsorship from Snow
8bfdaa523SEd Schouten  * B.V., the Netherlands.
9bfdaa523SEd Schouten  *
10bfdaa523SEd Schouten  * Redistribution and use in source and binary forms, with or without
11bfdaa523SEd Schouten  * modification, are permitted provided that the following conditions
12bfdaa523SEd Schouten  * are met:
13bfdaa523SEd Schouten  * 1. Redistributions of source code must retain the above copyright
14bfdaa523SEd Schouten  *    notice, this list of conditions and the following disclaimer.
15bfdaa523SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
16bfdaa523SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
17bfdaa523SEd Schouten  *    documentation and/or other materials provided with the distribution.
18bfdaa523SEd Schouten  *
19bfdaa523SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20bfdaa523SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bfdaa523SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bfdaa523SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bfdaa523SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bfdaa523SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bfdaa523SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bfdaa523SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bfdaa523SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bfdaa523SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bfdaa523SEd Schouten  * SUCH DAMAGE.
30bfdaa523SEd Schouten  */
31bfdaa523SEd Schouten 
32bfdaa523SEd Schouten #include <sys/param.h>
33bfdaa523SEd Schouten #include <sys/conf.h>
34bfdaa523SEd Schouten #include <sys/eventhandler.h>
35bfdaa523SEd Schouten #include <sys/fcntl.h>
36bfdaa523SEd Schouten #include <sys/kernel.h>
37bfdaa523SEd Schouten #include <sys/module.h>
38bfdaa523SEd Schouten #include <sys/proc.h>
39bfdaa523SEd Schouten #include <sys/sysctl.h>
40bfdaa523SEd Schouten #include <sys/syslog.h>
41bfdaa523SEd Schouten #include <sys/systm.h>
42bfdaa523SEd Schouten #include <sys/tty.h>
43bfdaa523SEd Schouten 
44bfdaa523SEd Schouten /*
45bfdaa523SEd Schouten  * This driver implements a BSD-style compatibility naming scheme for
46bfdaa523SEd Schouten  * the pts(4) driver. We just call into pts(4) to create the actual PTY.
47bfdaa523SEd Schouten  * To make sure we don't use the same PTY multiple times, we abuse
48bfdaa523SEd Schouten  * si_drv1 inside the cdev to mark whether the PTY is in use.
494d3b1aacSEd Schouten  *
504d3b1aacSEd Schouten  * It also implements a /dev/ptmx device node, which is useful for Linux
514d3b1aacSEd Schouten  * binary emulation.
52bfdaa523SEd Schouten  */
53bfdaa523SEd Schouten 
549837947bSKonstantin Belousov static unsigned pty_warningcnt = 1;
55bfdaa523SEd Schouten SYSCTL_UINT(_kern, OID_AUTO, tty_pty_warningcnt, CTLFLAG_RW,
56bfdaa523SEd Schouten     &pty_warningcnt, 0,
57bfdaa523SEd Schouten     "Warnings that will be triggered upon legacy PTY allocation");
58bfdaa523SEd Schouten 
59bfdaa523SEd Schouten static int
ptydev_fdopen(struct cdev * dev,int fflags,struct thread * td,struct file * fp)60bfdaa523SEd Schouten ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
61bfdaa523SEd Schouten {
62bfdaa523SEd Schouten 	int error;
63bfdaa523SEd Schouten 	char name[6]; /* "ttyXX" */
64bfdaa523SEd Schouten 
65bfdaa523SEd Schouten 	if (!atomic_cmpset_ptr((uintptr_t *)&dev->si_drv1, 0, 1))
66bfdaa523SEd Schouten 		return (EBUSY);
67bfdaa523SEd Schouten 
68bfdaa523SEd Schouten 	/* Generate device name and create PTY. */
69b51230b7SConrad Meyer 	strlcpy(name, devtoname(dev), sizeof(name));
70bfdaa523SEd Schouten 	name[0] = 't';
71bfdaa523SEd Schouten 
72bfdaa523SEd Schouten 	error = pts_alloc_external(fflags & (FREAD|FWRITE), td, fp, dev, name);
73bfdaa523SEd Schouten 	if (error != 0) {
74bfdaa523SEd Schouten 		destroy_dev_sched(dev);
75bfdaa523SEd Schouten 		return (error);
76bfdaa523SEd Schouten 	}
77bfdaa523SEd Schouten 
78bfdaa523SEd Schouten 	/* Raise a warning when a legacy PTY has been allocated. */
799837947bSKonstantin Belousov 	counted_warning(&pty_warningcnt, "is using legacy pty devices");
80bfdaa523SEd Schouten 
81bfdaa523SEd Schouten 	return (0);
82bfdaa523SEd Schouten }
83bfdaa523SEd Schouten 
84bfdaa523SEd Schouten static struct cdevsw ptydev_cdevsw = {
85bfdaa523SEd Schouten 	.d_version	= D_VERSION,
86bfdaa523SEd Schouten 	.d_fdopen	= ptydev_fdopen,
87bfdaa523SEd Schouten 	.d_name		= "ptydev",
88bfdaa523SEd Schouten };
89bfdaa523SEd Schouten 
90bfdaa523SEd Schouten static void
pty_clone(void * arg,struct ucred * cr,char * name,int namelen,struct cdev ** dev)91bfdaa523SEd Schouten pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
92bfdaa523SEd Schouten     struct cdev **dev)
93bfdaa523SEd Schouten {
947ea3f72eSKonstantin Belousov 	struct make_dev_args mda;
957ea3f72eSKonstantin Belousov 	int error;
96bfdaa523SEd Schouten 
97bfdaa523SEd Schouten 	/* Cloning is already satisfied. */
98bfdaa523SEd Schouten 	if (*dev != NULL)
99bfdaa523SEd Schouten 		return;
100bfdaa523SEd Schouten 
101bfdaa523SEd Schouten 	/* Only catch /dev/ptyXX. */
102bfdaa523SEd Schouten 	if (namelen != 5 || bcmp(name, "pty", 3) != 0)
103bfdaa523SEd Schouten 		return;
104bfdaa523SEd Schouten 
105bfdaa523SEd Schouten 	/* Only catch /dev/pty[l-sL-S]X. */
106bfdaa523SEd Schouten 	if (!(name[3] >= 'l' && name[3] <= 's') &&
107bfdaa523SEd Schouten 	    !(name[3] >= 'L' && name[3] <= 'S'))
108bfdaa523SEd Schouten 		return;
109bfdaa523SEd Schouten 
110bfdaa523SEd Schouten 	/* Only catch /dev/pty[l-sL-S][0-9a-v]. */
111bfdaa523SEd Schouten 	if (!(name[4] >= '0' && name[4] <= '9') &&
112bfdaa523SEd Schouten 	    !(name[4] >= 'a' && name[4] <= 'v'))
113bfdaa523SEd Schouten 		return;
114bfdaa523SEd Schouten 
115bfdaa523SEd Schouten 	/* Create the controller device node. */
1167ea3f72eSKonstantin Belousov 	make_dev_args_init(&mda);
1177ea3f72eSKonstantin Belousov 	mda.mda_flags =  MAKEDEV_CHECKNAME | MAKEDEV_REF;
1187ea3f72eSKonstantin Belousov 	mda.mda_devsw = &ptydev_cdevsw;
1197ea3f72eSKonstantin Belousov 	mda.mda_uid = UID_ROOT;
1207ea3f72eSKonstantin Belousov 	mda.mda_gid = GID_WHEEL;
1217ea3f72eSKonstantin Belousov 	mda.mda_mode = 0666;
1227ea3f72eSKonstantin Belousov 	error = make_dev_s(&mda, dev, "%s", name);
123be62a642SKonstantin Belousov 	if (error != 0)
1247ea3f72eSKonstantin Belousov 		*dev = NULL;
1257ea3f72eSKonstantin Belousov }
126bfdaa523SEd Schouten 
127bfdaa523SEd Schouten static int
ptmx_fdopen(struct cdev * dev __unused,int fflags,struct thread * td,struct file * fp)1284d3b1aacSEd Schouten ptmx_fdopen(struct cdev *dev __unused, int fflags, struct thread *td,
1294d3b1aacSEd Schouten     struct file *fp)
1304d3b1aacSEd Schouten {
1314d3b1aacSEd Schouten 
1324d3b1aacSEd Schouten 	return (pts_alloc(fflags & (FREAD|FWRITE), td, fp));
1334d3b1aacSEd Schouten }
1344d3b1aacSEd Schouten 
1354d3b1aacSEd Schouten static struct cdevsw ptmx_cdevsw = {
1364d3b1aacSEd Schouten 	.d_version	= D_VERSION,
1374d3b1aacSEd Schouten 	.d_fdopen	= ptmx_fdopen,
1384d3b1aacSEd Schouten 	.d_name		= "ptmx",
1394d3b1aacSEd Schouten };
1404d3b1aacSEd Schouten 
1414d3b1aacSEd Schouten static int
pty_modevent(module_t mod,int type,void * data)142bfdaa523SEd Schouten pty_modevent(module_t mod, int type, void *data)
143bfdaa523SEd Schouten {
144bfdaa523SEd Schouten 
145bfdaa523SEd Schouten 	switch(type) {
146bfdaa523SEd Schouten 	case MOD_LOAD:
147bfdaa523SEd Schouten 		EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
14823b70c1aSKonstantin Belousov 		make_dev_credf(MAKEDEV_ETERNAL_KLD, &ptmx_cdevsw, 0, NULL,
14923b70c1aSKonstantin Belousov 		    UID_ROOT, GID_WHEEL, 0666, "ptmx");
150bfdaa523SEd Schouten 		break;
151bfdaa523SEd Schouten 	case MOD_SHUTDOWN:
152bfdaa523SEd Schouten 		break;
153bfdaa523SEd Schouten 	case MOD_UNLOAD:
154bfdaa523SEd Schouten 		/* XXX: No unloading support yet. */
155bfdaa523SEd Schouten 		return (EBUSY);
156bfdaa523SEd Schouten 	default:
157bfdaa523SEd Schouten 		return (EOPNOTSUPP);
158bfdaa523SEd Schouten 	}
159bfdaa523SEd Schouten 
160bfdaa523SEd Schouten 	return (0);
161bfdaa523SEd Schouten }
162bfdaa523SEd Schouten 
163bfdaa523SEd Schouten DEV_MODULE(pty, pty_modevent, NULL);
164