diff options
| author | cvs2svn <cvs2svn@FreeBSD.org> | 1999-08-10 05:58:59 +0000 |
|---|---|---|
| committer | cvs2svn <cvs2svn@FreeBSD.org> | 1999-08-10 05:58:59 +0000 |
| commit | 0437711af06bb3298fdb7bfb5db21dcda34a2235 (patch) | |
| tree | 40309c308c406ecf6ee6ae1e152d06a7cd0585a0 | |
| parent | 37deeb40bb7a433eb0b56e0e75b33628f7dc7d0d (diff) | |
Notes
| -rw-r--r-- | lib/libc/string/strlcat.c | 71 | ||||
| -rw-r--r-- | lib/libc/string/strlcpy.c | 68 | ||||
| -rw-r--r-- | sys/i386/conf/PCCARD | 211 | ||||
| -rw-r--r-- | usr.sbin/ppp/tty.c | 446 |
4 files changed, 796 insertions, 0 deletions
diff --git a/lib/libc/string/strlcat.c b/lib/libc/string/strlcat.c new file mode 100644 index 000000000000..599994edf5af --- /dev/null +++ b/lib/libc/string/strlcat.c @@ -0,0 +1,71 @@ +/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <string.h> + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t strlcat(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (*d != '\0' && n-- != 0) + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/lib/libc/string/strlcpy.c b/lib/libc/string/strlcpy.c new file mode 100644 index 000000000000..300a28bc3911 --- /dev/null +++ b/lib/libc/string/strlcpy.c @@ -0,0 +1,68 @@ +/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <sys/types.h> +#include <string.h> + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t strlcpy(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/sys/i386/conf/PCCARD b/sys/i386/conf/PCCARD new file mode 100644 index 000000000000..52c08ab1f2f6 --- /dev/null +++ b/sys/i386/conf/PCCARD @@ -0,0 +1,211 @@ +# +# PCCARD -- Generic machine with WD/AHx/NCR/BTx family disks and PCMCIA +# hardware support +# +# For more information read the handbook part System Administration -> +# Configuring the FreeBSD Kernel -> The Configuration File. +# The handbook is available in /usr/share/doc/handbook or online as +# latest version from the FreeBSD World Wide Web server +# <URL:http://www.FreeBSD.ORG/> +# +# An exhaustive list of options and more detailed explanations of the +# device lines is present in the ./LINT configuration file. If you are +# in doubt as to the purpose or necessity of a line, check first in LINT. +# +# $Id: PCCARD,v 1.14 1999/07/19 15:18:21 hosokawa Exp $ + +machine i386 +cpu I386_CPU +cpu I486_CPU +cpu I586_CPU +cpu I686_CPU +ident GENERIC +maxusers 32 + +#makeoptions DEBUG=-g #Build kernel with gdb(1) debug symbols + +options MATH_EMULATE #Support for x87 emulation +options INET #InterNETworking +options FFS #Berkeley Fast Filesystem +options FFS_ROOT #FFS usable as root device [keep this!] +options MFS #Memory Filesystem +options MFS_ROOT #MFS usable as root device, "MFS" req'ed +options NFS #Network Filesystem +options NFS_ROOT #NFS usable as root device, "NFS" req'ed +options MSDOSFS #MSDOS Filesystem +options CD9660 #ISO 9660 Filesystem +options CD9660_ROOT #CD-ROM usable as root. "CD9660" req'ed +options PROCFS #Process filesystem +options COMPAT_43 #Compatible with BSD 4.3 [KEEP THIS!] +options SCSI_DELAY=15000 #Be pessimistic about Joe SCSI device +options UCONSOLE #Allow users to grab the console +options USERCONFIG #boot -c editor +options VISUAL_USERCONFIG #visual boot -c editor +options KTRACE #ktrace(1) syscall trace support +options SYSVSHM #SYSV-style shared memory +options SYSVMSG #SYSV-style message queues +options SYSVSEM #SYSV-style semaphores + +# To make an SMP kernel, the next two are needed +#options SMP # Symmetric MultiProcessor Kernel +#options APIC_IO # Symmetric (APIC) I/O +# Optionally these may need tweaked, (defaults shown): +#options NCPU=2 # number of CPUs +#options NBUS=4 # number of busses +#options NAPIC=1 # number of IO APICs +#options NINTR=24 # number of INTs + +controller isa0 +controller pnp0 # PnP support for ISA +controller eisa0 +controller pci0 + +# Floppy drives +controller fdc0 at isa? port IO_FD1 irq 6 drq 2 +disk fd0 at fdc0 drive 0 +disk fd1 at fdc0 drive 1 + +# IDE controller and disks +controller wdc0 at isa? port IO_WD1 irq 14 +disk wd0 at wdc0 drive 0 +disk wd1 at wdc0 drive 1 + +controller wdc1 at isa? port IO_WD2 irq 15 +disk wd2 at wdc1 drive 0 +disk wd3 at wdc1 drive 1 + +# ATAPI devices on wdc? +device wcd0 #IDE CD-ROM +device wfd0 #IDE Floppy (e.g. LS-120) +device wst0 #IDE Tape (e.g. Travan) + +# SCSI Controllers +# A single entry for any of these controllers (ncr, ahb, ahc) is +# sufficient for any number of installed devices. +controller ncr0 # NCR/Symbios Logic +controller ahb0 # EISA AHA1742 family +controller ahc0 # AHA2940 and onboard AIC7xxx devices +controller isp0 # Qlogic family +controller dpt0 # DPT Smartcache - See LINT for options! + +controller adv0 at isa? port ? irq ? +controller adw0 +controller bt0 at isa? port ? irq ? +controller aha0 at isa? port ? irq ? + +# SCSI peripherals +# Only one of each of these is needed, they are dynamically allocated. +controller scbus0 # SCSI bus (required) +device da0 # Direct Access (disks) +device sa0 # Sequential Access (tape etc) +device cd0 # CD +device pass0 # Passthrough device (direct SCSI access) + +# Proprietary or custom CD-ROM Interfaces +device wt0 at isa? port 0x300 irq 5 drq 1 +device mcd0 at isa? port 0x300 irq 10 +device matcd0 at isa? port 0x230 +device scd0 at isa? port 0x230 + +# atkbdc0 controls both the keyboard and the PS/2 mouse +controller atkbdc0 at isa? port IO_KBD +device atkbd0 at atkbdc? irq 1 +device psm0 at atkbdc? irq 12 + +device vga0 at isa? port ? conflicts + +# splash screen/screen saver +pseudo-device splash + +# syscons is the default console driver, resembling an SCO console +device sc0 at isa? + +# Enable this and PCVT_FREEBSD for pcvt vt220 compatible console driver +#device vt0 at isa? +#options XSERVER # support for X server +#options FAT_CURSOR # start with block cursor +# If you have a ThinkPAD, uncomment this along with the rest of the PCVT lines +#options PCVT_SCANSET=2 # IBM keyboards are non-std + +# Floating point support - do not disable. +device npx0 at nexus? port IO_NPX irq 13 + +# Power management support (see LINT for more options) +device apm0 at nexus? disable flags 0x31 # Advanced Power Management + +# PCCARD (PCMCIA) support +controller card0 +device pcic0 at card? +device pcic1 at card? + +# Serial (COM) ports +device sio0 at isa? port IO_COM1 flags 0x10 irq 4 +device sio1 at isa? port IO_COM2 irq 3 +device sio2 at isa? disable port IO_COM3 irq 5 +device sio3 at isa? disable port IO_COM4 irq 9 + +# Parallel port +device ppc0 at isa? port? flags 0x40 irq 7 +controller ppbus0 # Parallel port bus (required) +device lpt0 # Printer +device plip0 # TCP/IP over parallel +device ppi0 # Parallel port interface device +#controller vpo0 # Requires scbus and da0 + +# PCI Ethernet NICs. +device al0 # ADMtek AL981 (``Comet'') +device ax0 # ASIX AX88140A +device de0 # DEC/Intel DC21x4x (``Tulip'') +device fxp0 # Intel EtherExpress PRO/100B (82557, 82558) +device mx0 # Macronix 98713/98715/98725 (``PMAC'') +device pn0 # Lite-On 82c168/82c169 (``PNIC'') +device rl0 # RealTek 8129/8139 +device tl0 # Texas Instruments ThunderLAN +device tx0 # SMC 9432TX (83c170 ``EPIC'') +device vr0 # VIA Rhine, Rhine II +device vx0 # 3Com 3c590, 3c595 (``Vortex'') +device wb0 # Winbond W89C840F +device xl0 # 3Com 3c90x (``Boomerang'', ``Cyclone'') + +# ISA Ethernet NICs. +# The probe order of these is presently determined by i386/isa/isa_compat.c. +device ed0 at isa? port 0x280 irq 10 iomem 0xd8000 +device ie0 at isa? port 0x300 irq 10 iomem 0xd0000 +device ep0 at isa? port 0x300 irq 10 +device ex0 at isa? port? irq? +device fe0 at isa? port 0x300 irq ? +device le0 at isa? port 0x300 irq 5 iomem 0xd0000 +device lnc0 at isa? port 0x280 irq 10 drq 0 +device cs0 at isa? port 0x300 irq ? +# requires PCCARD (PCMCIA) support to be activated +#device xe0 at isa? port? irq ? + +# PCCARD NIC drivers. +# ze and zp take over the pcic and cannot coexist with generic pccard +# support, nor the ed and ep drivers they replace. +#device ze0 at isa? port 0x300 irq 10 iomem 0xd8000 +#device zp0 at isa? port 0x300 irq 10 iomem 0xd8000 + +# Pseudo devices - the number indicates how many units to allocated. +pseudo-device loop # Network loopback +pseudo-device ether # Ethernet support +pseudo-device sl 1 # Kernel SLIP +pseudo-device ppp 1 # Kernel PPP +pseudo-device tun 1 # Packet tunnel, for ppp(1) +pseudo-device pty # Pseudo-ttys (telnet etc) +pseudo-device gzip # Exec gzipped a.out's + +# The `bpf' pseudo-device enables the Berkeley Packet Filter. +# Be aware of the legal and administrative consequences of enabling this! +#pseudo-device bpf 4 #Berkeley packet filter + +# USB support +#controller uhci0 # UHCI PCI->USB interface +#controller ohci0 # OHCI PCI->USB interface +#controller usb0 # USB Bus (required) +#device ugen0 # Generic +#device uhid0 # "Human Interface Devices" +#device ukbd0 # Keyboard +#device ulpt0 # Printer +#controller umass0 # Disks/Mass storage - Requires scbus and da0 +#device ums0 # Mouse diff --git a/usr.sbin/ppp/tty.c b/usr.sbin/ppp/tty.c new file mode 100644 index 000000000000..e2201ece51d5 --- /dev/null +++ b/usr.sbin/ppp/tty.c @@ -0,0 +1,446 @@ +/*- + * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: tty.c,v 1.10 1999/08/06 20:04:07 brian Exp $ + */ + +#include <sys/param.h> +#include <sys/un.h> +#if defined(__OpenBSD__) || defined(__NetBSD__) +#include <sys/ioctl.h> +#endif + +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <sys/uio.h> +#include <termios.h> +#include <unistd.h> + +#include "layer.h" +#include "defs.h" +#include "mbuf.h" +#include "log.h" +#include "timer.h" +#include "lqr.h" +#include "hdlc.h" +#include "throughput.h" +#include "fsm.h" +#include "lcp.h" +#include "ccp.h" +#include "link.h" +#include "async.h" +#include "descriptor.h" +#include "physical.h" +#include "mp.h" +#include "chat.h" +#include "auth.h" +#include "chap.h" +#include "cbcp.h" +#include "datalink.h" +#include "main.h" +#include "tty.h" + +#define Online(dev) ((dev)->mbits & TIOCM_CD) + +struct ttydevice { + struct device dev; /* What struct physical knows about */ + struct pppTimer Timer; /* CD checks */ + int mbits; /* Current DCD status */ + int carrier_seconds; /* seconds before CD is *required* */ + struct termios ios; /* To be able to reset from raw mode */ +}; + +#define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL) + +int +tty_DeviceSize(void) +{ + return sizeof(struct ttydevice); +} + +/* + * tty_Timeout() watches the DCD signal and mentions it if it's status + * changes. + */ +static void +tty_Timeout(void *data) +{ + struct physical *p = data; + struct ttydevice *dev = device2tty(p->handler); + int ombits, change; + + timer_Stop(&dev->Timer); + dev->Timer.load = SECTICKS; /* Once a second please */ + timer_Start(&dev->Timer); + ombits = dev->mbits; + + if (p->fd >= 0) { + if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) { + /* we must be a pty ? */ + log_Printf(LogDEBUG, "%s: ioctl error (%s)!\n", p->link.name, + strerror(errno)); + timer_Stop(&dev->Timer); + return; + } + } else + dev->mbits = 0; + + if (ombits == -1) { + /* First time looking for carrier */ + if (Online(dev)) + log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full); + else if (++dev->carrier_seconds >= p->cfg.cd.delay) { + if (p->cfg.cd.required) + log_Printf(LogPHASE, "%s: %s: Required CD not detected\n", + p->link.name, p->name.full); + else { + log_Printf(LogPHASE, "%s: %s doesn't support CD\n", + p->link.name, p->name.full); + dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */ + } + timer_Stop(&dev->Timer); + /* tty_AwaitCarrier() will notice */ + } else { + /* Keep waiting */ + log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n", + p->link.name, p->name.full, dev->carrier_seconds, + p->cfg.cd.delay); + dev->mbits = -1; + } + } else { + change = ombits ^ dev->mbits; + if (change & TIOCM_CD) { + if (dev->mbits & TIOCM_CD) + log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name); + else { + log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name); + log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name); + datalink_Down(p->dl, CLOSE_NORMAL); + timer_Stop(&dev->Timer); + } + } else + log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name, + Online(dev) ? "on" : "off"); + } +} + +static void +tty_StartTimer(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + + timer_Stop(&dev->Timer); + dev->Timer.load = SECTICKS; + dev->Timer.func = tty_Timeout; + dev->Timer.name = "tty CD"; + dev->Timer.arg = p; + log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n", + p->link.name, tty_Timeout); + timer_Start(&dev->Timer); +} + +static int +tty_AwaitCarrier(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + + if (physical_IsSync(p)) + return CARRIER_OK; + + if (dev->mbits == -1) { + if (dev->Timer.state == TIMER_STOPPED) { + dev->carrier_seconds = 0; + tty_StartTimer(p); + } + return CARRIER_PENDING; /* Not yet ! */ + } + + return Online(dev) || !p->cfg.cd.required ? CARRIER_OK : CARRIER_LOST; +} + +static int +tty_Raw(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + struct termios ios; + int oldflag; + + log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name); + + if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev)) + log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n", + p->link.name, p->fd, dev->mbits); + + if (!physical_IsSync(p)) { + tcgetattr(p->fd, &ios); + cfmakeraw(&ios); + if (p->cfg.rts_cts) + ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; + else + ios.c_cflag |= CLOCAL; + + if (p->type != PHYS_DEDICATED) + ios.c_cflag |= HUPCL; + + tcsetattr(p->fd, TCSANOW, &ios); + } + + oldflag = fcntl(p->fd, F_GETFL, 0); + if (oldflag < 0) + return 0; + fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK); + + return 1; +} + +static void +tty_Offline(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + + if (p->fd >= 0) { + timer_Stop(&dev->Timer); + dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */ + if (Online(dev)) { + struct termios tio; + + tcgetattr(p->fd, &tio); + if (cfsetspeed(&tio, B0) == -1) + log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n", + p->link.name); + else + tcsetattr(p->fd, TCSANOW, &tio); + } + } +} + +static void +tty_Cooked(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + int oldflag; + + tty_Offline(p); /* In case of emergency close()s */ + + tcflush(p->fd, TCIOFLUSH); + + if (!physical_IsSync(p)) + tcsetattr(p->fd, TCSAFLUSH, &dev->ios); + + if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1) + fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); +} + +static void +tty_StopTimer(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + + timer_Stop(&dev->Timer); +} + +static void +tty_Free(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + + tty_Offline(p); /* In case of emergency close()s */ + free(dev); +} + +static int +tty_Speed(struct physical *p) +{ + struct termios ios; + + if (tcgetattr(p->fd, &ios) == -1) + return 0; + + return SpeedToInt(cfgetispeed(&ios)); +} + +static const char * +tty_OpenInfo(struct physical *p) +{ + struct ttydevice *dev = device2tty(p->handler); + static char buf[13]; + + if (Online(dev)) + strcpy(buf, "with"); + else + strcpy(buf, "no"); + strcat(buf, " carrier"); + + return buf; +} + +static void +tty_device2iov(struct device *d, struct iovec *iov, int *niov, + int maxiov, pid_t newpid) +{ + struct ttydevice *dev = device2tty(d); + int sz = physical_MaxDeviceSize(); + + iov[*niov].iov_base = realloc(d, sz); + if (iov[*niov].iov_base == NULL) { + log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); + AbortProgram(EX_OSERR); + } + iov[*niov].iov_len = sz; + (*niov)++; + + if (dev->Timer.state != TIMER_STOPPED) { + timer_Stop(&dev->Timer); + dev->Timer.state = TIMER_RUNNING; + } +} + +static struct device basettydevice = { + TTY_DEVICE, + "tty", + tty_AwaitCarrier, + tty_Raw, + tty_Offline, + tty_Cooked, + tty_StopTimer, + tty_Free, + NULL, + NULL, + tty_device2iov, + tty_Speed, + tty_OpenInfo +}; + +struct device * +tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, + int maxiov) +{ + if (type == TTY_DEVICE) { + struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base; + + dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ + if (dev == NULL) { + log_Printf(LogALERT, "Failed to allocate memory: %d\n", + (int)(sizeof *dev)); + AbortProgram(EX_OSERR); + } + + /* Refresh function pointers etc */ + memcpy(&dev->dev, &basettydevice, sizeof dev->dev); + + physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); + if (dev->Timer.state != TIMER_STOPPED) { + dev->Timer.state = TIMER_STOPPED; + p->handler = &dev->dev; /* For the benefit of StartTimer */ + tty_StartTimer(p); + } + return &dev->dev; + } + + return NULL; +} + +struct device * +tty_Create(struct physical *p) +{ + struct ttydevice *dev; + struct termios ios; + int oldflag; + + if (p->fd < 0 || !isatty(p->fd)) + /* Don't want this */ + return NULL; + + if (*p->name.full == '\0') { + physical_SetDevice(p, ttyname(p->fd)); + log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n", + p->link.name, p->name.full); + } else + log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full); + + /* We're gonna return a ttydevice (unless something goes horribly wrong) */ + + if ((dev = malloc(sizeof *dev)) == NULL) { + /* Complete failure - parent doesn't continue trying to ``create'' */ + close(p->fd); + p->fd = -1; + return NULL; + } + + memcpy(&dev->dev, &basettydevice, sizeof dev->dev); + memset(&dev->Timer, '\0', sizeof dev->Timer); + dev->mbits = -1; + tcgetattr(p->fd, &ios); + dev->ios = ios; + + log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d," + " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd, + (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag); + + cfmakeraw(&ios); + if (p->cfg.rts_cts) + ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; + else { + ios.c_cflag |= CLOCAL; + ios.c_iflag |= IXOFF; + } + ios.c_iflag |= IXON; + if (p->type != PHYS_DEDICATED) + ios.c_cflag |= HUPCL; + + if (p->type != PHYS_DIRECT) { + /* Change tty speed when we're not in -direct mode */ + ios.c_cflag &= ~(CSIZE | PARODD | PARENB); + ios.c_cflag |= p->cfg.parity; + if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1) + log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n", + p->link.name, p->name.full, p->cfg.speed); + } + tcsetattr(p->fd, TCSADRAIN, &ios); + log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, " + "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag, + (u_long)ios.c_oflag, (u_long)ios.c_cflag); + + oldflag = fcntl(p->fd, F_GETFL, 0); + if (oldflag < 0) { + /* Complete failure - parent doesn't continue trying to ``create'' */ + + log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n", + p->link.name, strerror(errno)); + tty_Cooked(p); + close(p->fd); + p->fd = -1; + free(dev); + return NULL; + } else + fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); + + physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); + + return &dev->dev; +} |
