1 /* $NetBSD: tty_60.c,v 1.11 2021/07/21 06:35:44 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Alan Barrett 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: tty_60.c,v 1.11 2021/07/21 06:35:44 skrll Exp $"); 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_compat_netbsd.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 42 #include <sys/conf.h> 43 #include <sys/errno.h> 44 #include <sys/systm.h> 45 #include <sys/compat_stub.h> 46 #include <sys/kmem.h> 47 48 #include <sys/tty.h> 49 50 #include <compat/common/compat_mod.h> 51 #include <compat/sys/ttycom.h> 52 53 /* convert struct ptmget to struct compat_60_ptmget */ 54 static int 55 ptmget_to_ptmget60(struct ptmget *pg, struct compat_60_ptmget *pg60) 56 { 57 memset(pg60, 0, sizeof(*pg60)); 58 pg60->cfd = pg->cfd; 59 pg60->sfd = pg->sfd; 60 strlcpy(pg60->cn, pg->cn, sizeof(pg60->cn)); 61 strlcpy(pg60->sn, pg->sn, sizeof(pg60->sn)); 62 if (strlen(pg->cn) >= sizeof(pg60->cn) 63 || strlen(pg->sn) >= sizeof(pg60->sn)) 64 return E2BIG; 65 return 0; 66 } 67 68 /* Helper for compat ioctls that use struct compat_60_ptmget. */ 69 static int 70 compat_60_ptmget_ioctl(dev_t dev, u_long cmd, void *data, int flag, 71 struct lwp *l) 72 { 73 int ret; 74 u_long newcmd; 75 struct ptmget *pg; 76 const struct cdevsw *cd = cdevsw_lookup(dev); 77 78 if (cd == NULL || cd->d_ioctl == NULL) 79 return ENXIO; 80 81 switch (cmd) { 82 case COMPAT_60_TIOCPTMGET: newcmd = TIOCPTMGET; break; 83 case COMPAT_60_TIOCPTSNAME: newcmd = TIOCPTSNAME; break; 84 default: return ENOTTY; 85 } 86 87 pg = kmem_alloc(sizeof(*pg), KM_SLEEP); 88 89 ret = (cd->d_ioctl)(dev, newcmd, pg, flag, l); 90 if (ret != 0) 91 goto out; 92 93 ret = ptmget_to_ptmget60(pg, data); 94 95 out: 96 kmem_free(pg, sizeof(*pg)); 97 return ret; 98 } 99 100 /* 101 * COMPAT_60 versions of ttioctl and ptmioctl. 102 */ 103 int 104 compat_60_ttioctl(struct tty *tp, u_long cmd, void *data, int flag, 105 struct lwp *l) 106 { 107 108 switch (cmd) { 109 case COMPAT_60_TIOCPTMGET: 110 case COMPAT_60_TIOCPTSNAME: 111 return compat_60_ptmget_ioctl(tp->t_dev, cmd, data, flag, l); 112 default: 113 return EPASSTHROUGH; 114 } 115 } 116 117 int 118 compat_60_ptmioctl(dev_t dev, u_long cmd, void *data, int flag, 119 struct lwp *l) 120 { 121 122 switch (cmd) { 123 case COMPAT_60_TIOCPTMGET: 124 return compat_60_ptmget_ioctl(dev, cmd, data, flag, l); 125 default: 126 return EPASSTHROUGH; 127 } 128 } 129 130 void 131 kern_tty_60_init(void) 132 { 133 134 MODULE_HOOK_SET(tty_ttioctl_60_hook, compat_60_ttioctl); 135 MODULE_HOOK_SET(tty_ptmioctl_60_hook, compat_60_ptmioctl); 136 } 137 138 void 139 kern_tty_60_fini(void) 140 { 141 MODULE_HOOK_UNSET(tty_ttioctl_60_hook); 142 MODULE_HOOK_UNSET(tty_ptmioctl_60_hook); 143 } 144