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