The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/tty_pty.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: releng/8.3/sys/kern/tty_pty.c 199583 2009-11-20 15:27:52Z jhb $");
   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>
   40 #include <sys/syslog.h>
   41 #include <sys/systm.h>
   42 #include <sys/tty.h>
   43 
   44 /*
   45  * This driver implements a BSD-style compatibility naming scheme for
   46  * the pts(4) driver. We just call into pts(4) to create the actual PTY.
   47  * To make sure we don't use the same PTY multiple times, we abuse
   48  * si_drv1 inside the cdev to mark whether the PTY is in use.
   49  */
   50 
   51 static unsigned int pty_warningcnt = 1;
   52 SYSCTL_UINT(_kern, OID_AUTO, tty_pty_warningcnt, CTLFLAG_RW,
   53         &pty_warningcnt, 0,
   54         "Warnings that will be triggered upon legacy PTY allocation");
   55 
   56 static int
   57 ptydev_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
   58 {
   59         int error;
   60         char name[6]; /* "ttyXX" */
   61 
   62         if (!atomic_cmpset_ptr((uintptr_t *)&dev->si_drv1, 0, 1))
   63                 return (EBUSY);
   64 
   65         /* Generate device name and create PTY. */
   66         strcpy(name, devtoname(dev));
   67         name[0] = 't';
   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                 log(LOG_INFO, "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 
   86 static struct cdevsw ptydev_cdevsw = {
   87         .d_version      = D_VERSION,
   88         .d_fdopen       = ptydev_fdopen,
   89         .d_name         = "ptydev",
   90 };
   91 
   92 static void
   93 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
   94     struct cdev **dev)
   95 {
   96 
   97         /* Cloning is already satisfied. */
   98         if (*dev != NULL)
   99                 return;
  100 
  101         /* Only catch /dev/ptyXX. */
  102         if (namelen != 5 || bcmp(name, "pty", 3) != 0)
  103                 return;
  104 
  105         /* Only catch /dev/pty[l-sL-S]X. */
  106         if (!(name[3] >= 'l' && name[3] <= 's') &&
  107             !(name[3] >= 'L' && name[3] <= 'S'))
  108                 return;
  109 
  110         /* Only catch /dev/pty[l-sL-S][0-9a-v]. */
  111         if (!(name[4] >= '' && name[4] <= '9') &&
  112             !(name[4] >= 'a' && name[4] <= 'v'))
  113                 return;
  114 
  115         /* Create the controller device node. */
  116         *dev = make_dev_credf(MAKEDEV_REF, &ptydev_cdevsw, 0,
  117             NULL, UID_ROOT, GID_WHEEL, 0666, "%s", name);
  118 }
  119 
  120 static void
  121 pty_init(void *unused)
  122 {
  123 
  124         EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
  125 }
  126 
  127 SYSINIT(pty, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pty_init, NULL);

Cache object: 178daf71dd99abb48eb9dd2f09346b76


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.