1999-05-08 11:07:56 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*
|
1999-05-13 16:35:13 +00:00
|
|
|
* $Id: tty.c,v 1.2 1999/05/12 09:49:07 brian Exp $
|
1999-05-08 11:07:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <util.h>
|
|
|
|
#else
|
|
|
|
#include <libutil.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
1999-05-12 09:49:12 +00:00
|
|
|
#include <sys/uio.h>
|
1999-05-08 11:07:56 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "layer.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "id.h"
|
|
|
|
#include "sync.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 "slcompress.h"
|
|
|
|
#include "iplist.h"
|
|
|
|
#include "ipcp.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "physical.h"
|
|
|
|
#include "mp.h"
|
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
|
|
|
#include "chat.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "bundle.h"
|
|
|
|
#include "prompt.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "chap.h"
|
|
|
|
#include "cbcp.h"
|
|
|
|
#include "datalink.h"
|
|
|
|
#include "tty.h"
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
#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 */
|
|
|
|
struct termios ios; /* To be able to reset from raw mode */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL)
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
tty_Lock(struct physical *p, int tunno)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
FILE *lockfile;
|
|
|
|
char fn[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (*p->name.full != '/')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (p->type != PHYS_DIRECT &&
|
|
|
|
(res = ID0uu_lock(p->name.base)) != UU_LOCK_OK) {
|
|
|
|
if (res == UU_LOCK_INUSE)
|
|
|
|
log_Printf(LogPHASE, "%s: %s is in use\n", p->link.name, p->name.full);
|
|
|
|
else
|
|
|
|
log_Printf(LogPHASE, "%s: %s is in use: uu_lock: %s\n",
|
|
|
|
p->link.name, p->name.full, uu_lockerr(res));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
|
|
|
|
lockfile = ID0fopen(fn, "w");
|
|
|
|
if (lockfile != NULL) {
|
|
|
|
fprintf(lockfile, "%s%d\n", TUN_NAME, tunno);
|
|
|
|
fclose(lockfile);
|
|
|
|
}
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
else
|
|
|
|
log_Printf(LogALERT, "%s: Can't create %s: %s\n",
|
|
|
|
p->link.name, fn, strerror(errno));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tty_Unlock(struct physical *p)
|
|
|
|
{
|
|
|
|
char fn[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (*p->name.full != '/')
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(fn, sizeof fn, "%s%s.if", _PATH_VARRUN, p->name.base);
|
|
|
|
#ifndef RELEASE_CRUNCH
|
|
|
|
if (ID0unlink(fn) == -1)
|
|
|
|
log_Printf(LogALERT, "%s: Can't remove %s: %s\n",
|
|
|
|
p->link.name, fn, strerror(errno));
|
|
|
|
#else
|
|
|
|
ID0unlink(fn);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (p->type != PHYS_DIRECT && ID0uu_unlock(p->name.base) == -1)
|
|
|
|
log_Printf(LogALERT, "%s: Can't uu_unlock %s\n", p->link.name, fn);
|
|
|
|
}
|
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
static struct device *
|
1999-05-08 11:07:56 +00:00
|
|
|
tty_SetupDevice(struct physical *p)
|
|
|
|
{
|
1999-05-13 16:35:13 +00:00
|
|
|
struct ttydevice *dev;
|
|
|
|
struct termios ios;
|
1999-05-08 11:07:56 +00:00
|
|
|
int oldflag;
|
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
if ((dev = malloc(sizeof *dev)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tcgetattr(p->fd, &ios);
|
|
|
|
dev->ios = ios;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
log_Printf(LogDEBUG, "%s: tty_SetupDevice: physical (get): fd = %d,"
|
|
|
|
" iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd,
|
1999-05-13 16:35:13 +00:00
|
|
|
(u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
cfmakeraw(&ios);
|
1999-05-08 11:07:56 +00:00
|
|
|
if (p->cfg.rts_cts)
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
1999-05-08 11:07:56 +00:00
|
|
|
else {
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= CLOCAL;
|
|
|
|
ios.c_iflag |= IXOFF;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_iflag |= IXON;
|
1999-05-08 11:07:56 +00:00
|
|
|
if (p->type != PHYS_DEDICATED)
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= HUPCL;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
if (p->type != PHYS_DIRECT) {
|
|
|
|
/* Change tty speed when we're not in -direct mode */
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag &= ~(CSIZE | PARODD | PARENB);
|
|
|
|
ios.c_cflag |= p->cfg.parity;
|
|
|
|
if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1)
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n",
|
|
|
|
p->link.name, p->name.full, p->cfg.speed);
|
|
|
|
}
|
1999-05-13 16:35:13 +00:00
|
|
|
tcsetattr(p->fd, TCSADRAIN, &ios);
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, "
|
1999-05-13 16:35:13 +00:00
|
|
|
"cflag = %lx\n", p->link.name, (u_long)ios.c_iflag,
|
|
|
|
(u_long)ios.c_oflag, (u_long)ios.c_cflag);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
if (ioctl(p->fd, TIOCMGET, &dev->mbits) == -1) {
|
1999-05-08 11:07:56 +00:00
|
|
|
if (p->type != PHYS_DIRECT) {
|
|
|
|
log_Printf(LogWARN, "%s: Open: Cannot get physical status: %s\n",
|
|
|
|
p->link.name, strerror(errno));
|
|
|
|
physical_Close(p);
|
1999-05-13 16:35:13 +00:00
|
|
|
return NULL;
|
1999-05-08 11:07:56 +00:00
|
|
|
} else
|
1999-05-12 09:49:12 +00:00
|
|
|
dev->mbits = TIOCM_CD;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
log_Printf(LogDEBUG, "%s: Open: physical control = %o\n",
|
1999-05-12 09:49:12 +00:00
|
|
|
p->link.name, dev->mbits);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
oldflag = fcntl(p->fd, F_GETFL, 0);
|
|
|
|
if (oldflag < 0) {
|
|
|
|
log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n",
|
|
|
|
p->link.name, strerror(errno));
|
|
|
|
physical_Close(p);
|
1999-05-13 16:35:13 +00:00
|
|
|
return NULL;
|
1999-05-08 11:07:56 +00:00
|
|
|
} else
|
|
|
|
fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
physical_SetupStack(p, PHYSICAL_NOFORCE);
|
1999-05-13 16:35:13 +00:00
|
|
|
|
|
|
|
return &dev->dev;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tty_Timeout() watches the DCD signal and mentions it if it's status
|
|
|
|
* changes.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tty_Timeout(void *data)
|
|
|
|
{
|
|
|
|
struct physical *p = data;
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
1999-05-08 11:07:56 +00:00
|
|
|
int ombits, change;
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
timer_Stop(&dev->Timer);
|
|
|
|
dev->Timer.load = SECTICKS; /* Once a second please */
|
|
|
|
timer_Start(&dev->Timer);
|
|
|
|
ombits = dev->mbits;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
if (p->fd >= 0) {
|
1999-05-12 09:49:12 +00:00
|
|
|
if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) {
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogPHASE, "%s: ioctl error (%s)!\n", p->link.name,
|
|
|
|
strerror(errno));
|
|
|
|
datalink_Down(p->dl, CLOSE_NORMAL);
|
1999-05-12 09:49:12 +00:00
|
|
|
timer_Stop(&dev->Timer);
|
1999-05-08 11:07:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
1999-05-12 09:49:12 +00:00
|
|
|
dev->mbits = 0;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
if (ombits == -1) {
|
|
|
|
/* First time looking for carrier */
|
1999-05-12 09:49:12 +00:00
|
|
|
if (Online(dev))
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogDEBUG, "%s: %s: CD detected\n", p->link.name, p->name.full);
|
|
|
|
else if (p->cfg.cd.required) {
|
|
|
|
log_Printf(LogPHASE, "%s: %s: Required CD not detected\n",
|
|
|
|
p->link.name, p->name.full);
|
|
|
|
datalink_Down(p->dl, CLOSE_NORMAL);
|
|
|
|
} else {
|
|
|
|
log_Printf(LogPHASE, "%s: %s doesn't support CD\n",
|
|
|
|
p->link.name, p->name.full);
|
1999-05-12 09:49:12 +00:00
|
|
|
timer_Stop(&dev->Timer);
|
|
|
|
dev->mbits = TIOCM_CD;
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
1999-05-12 09:49:12 +00:00
|
|
|
change = ombits ^ dev->mbits;
|
1999-05-08 11:07:56 +00:00
|
|
|
if (change & TIOCM_CD) {
|
1999-05-12 09:49:12 +00:00
|
|
|
if (dev->mbits & TIOCM_CD)
|
1999-05-08 11:07:56 +00:00
|
|
|
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);
|
1999-05-12 09:49:12 +00:00
|
|
|
timer_Stop(&dev->Timer);
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name,
|
1999-05-12 09:49:12 +00:00
|
|
|
Online(dev) ? "on" : "off");
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tty_StartTimer(struct physical *p)
|
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
|
|
|
|
|
|
|
timer_Stop(&dev->Timer);
|
|
|
|
dev->Timer.load = SECTICKS * p->cfg.cd.delay;
|
|
|
|
dev->Timer.func = tty_Timeout;
|
|
|
|
dev->Timer.name = "tty CD";
|
|
|
|
dev->Timer.arg = p;
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n",
|
|
|
|
p->link.name, tty_Timeout);
|
1999-05-12 09:49:12 +00:00
|
|
|
dev->mbits = -1; /* So we know it's the first time */
|
|
|
|
timer_Start(&dev->Timer);
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tty_Raw(struct physical *p)
|
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
1999-05-13 16:35:13 +00:00
|
|
|
struct termios ios;
|
1999-05-08 11:07:56 +00:00
|
|
|
int oldflag;
|
|
|
|
|
|
|
|
if (physical_IsSync(p))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
log_Printf(LogDEBUG, "%s: Entering physical_Raw\n", p->link.name);
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev))
|
1999-05-08 11:07:56 +00:00
|
|
|
log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n",
|
1999-05-12 09:49:12 +00:00
|
|
|
p->link.name, p->fd, dev->mbits);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
tcgetattr(p->fd, &ios);
|
|
|
|
cfmakeraw(&ios);
|
1999-05-08 11:07:56 +00:00
|
|
|
if (p->cfg.rts_cts)
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW;
|
1999-05-08 11:07:56 +00:00
|
|
|
else
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= CLOCAL;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
if (p->type != PHYS_DEDICATED)
|
1999-05-13 16:35:13 +00:00
|
|
|
ios.c_cflag |= HUPCL;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
tcsetattr(p->fd, TCSANOW, &ios);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
oldflag = fcntl(p->fd, F_GETFL, 0);
|
|
|
|
if (oldflag < 0)
|
|
|
|
return 0;
|
|
|
|
fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK);
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
if (ioctl(p->fd, TIOCMGET, &dev->mbits) == 0)
|
1999-05-08 11:07:56 +00:00
|
|
|
tty_StartTimer(p);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tty_Offline(struct physical *p)
|
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
if (p->fd >= 0) {
|
1999-05-12 09:49:12 +00:00
|
|
|
timer_Stop(&dev->Timer);
|
|
|
|
dev->mbits &= ~TIOCM_DTR;
|
|
|
|
if (Online(dev)) {
|
1999-05-08 11:07:56 +00:00
|
|
|
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)
|
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
1999-05-08 11:07:56 +00:00
|
|
|
int oldflag;
|
|
|
|
|
|
|
|
tcflush(p->fd, TCIOFLUSH);
|
|
|
|
if (!physical_IsSync(p)) {
|
1999-05-12 09:49:12 +00:00
|
|
|
tcsetattr(p->fd, TCSAFLUSH, &dev->ios);
|
1999-05-08 11:07:56 +00:00
|
|
|
oldflag = fcntl(p->fd, F_GETFL, 0);
|
|
|
|
if (oldflag == 0)
|
|
|
|
fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-05-12 09:49:12 +00:00
|
|
|
tty_StopTimer(struct physical *p)
|
1999-05-08 11:07:56 +00:00
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
|
|
|
|
|
|
|
timer_Stop(&dev->Timer);
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-05-12 09:49:12 +00:00
|
|
|
tty_Free(struct physical *p)
|
1999-05-08 11:07:56 +00:00
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
|
|
|
|
|
|
|
tty_Unlock(p);
|
|
|
|
free(dev);
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tty_Speed(struct physical *p)
|
|
|
|
{
|
1999-05-13 16:35:13 +00:00
|
|
|
struct termios ios;
|
1999-05-08 11:07:56 +00:00
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
if (tcgetattr(p->fd, &ios) == -1)
|
1999-05-08 11:07:56 +00:00
|
|
|
return 0;
|
|
|
|
|
1999-05-13 16:35:13 +00:00
|
|
|
return SpeedToInt(cfgetispeed(&ios));
|
1999-05-08 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
tty_OpenInfo(struct physical *p)
|
|
|
|
{
|
1999-05-12 09:49:12 +00:00
|
|
|
struct ttydevice *dev = device2tty(p->handler);
|
1999-05-08 11:07:56 +00:00
|
|
|
static char buf[13];
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
if (Online(dev))
|
1999-05-08 11:07:56 +00:00
|
|
|
strcpy(buf, "with");
|
|
|
|
else
|
|
|
|
strcpy(buf, "no");
|
|
|
|
strcat(buf, " carrier");
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
static void
|
|
|
|
tty_device2iov(struct physical *p, struct iovec *iov, int *niov,
|
|
|
|
int maxiov, pid_t newpid)
|
|
|
|
{
|
|
|
|
struct ttydevice *dev = p ? device2tty(p->handler) : NULL;
|
|
|
|
|
|
|
|
iov[*niov].iov_base = p ? p->handler : malloc(sizeof(struct ttydevice));
|
|
|
|
iov[*niov].iov_len = sizeof(struct ttydevice);
|
|
|
|
(*niov)++;
|
|
|
|
|
|
|
|
if (dev->Timer.state != TIMER_STOPPED) {
|
|
|
|
timer_Stop(&dev->Timer);
|
|
|
|
dev->Timer.state = TIMER_RUNNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct device basettydevice = {
|
1999-05-08 11:07:56 +00:00
|
|
|
TTY_DEVICE,
|
|
|
|
"tty",
|
|
|
|
tty_Raw,
|
|
|
|
tty_Offline,
|
|
|
|
tty_Cooked,
|
1999-05-12 09:49:12 +00:00
|
|
|
tty_StopTimer,
|
|
|
|
tty_Free,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
tty_device2iov,
|
1999-05-08 11:07:56 +00:00
|
|
|
tty_Speed,
|
|
|
|
tty_OpenInfo
|
|
|
|
};
|
1999-05-12 09:49:12 +00:00
|
|
|
|
|
|
|
struct device *
|
|
|
|
tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov,
|
|
|
|
int maxiov)
|
|
|
|
{
|
|
|
|
if (type == TTY_DEVICE) {
|
|
|
|
struct ttydevice *dev;
|
|
|
|
|
|
|
|
/* It's one of ours ! Let's create the device */
|
|
|
|
|
|
|
|
dev = (struct ttydevice *)iov[(*niov)++].iov_base;
|
|
|
|
/* Refresh function pointers etc */
|
|
|
|
memcpy(&dev->dev, &basettydevice, sizeof dev->dev);
|
|
|
|
|
|
|
|
physical_SetupStack(p, PHYSICAL_NOFORCE);
|
|
|
|
if (dev->Timer.state != TIMER_STOPPED) {
|
|
|
|
dev->Timer.state = TIMER_STOPPED;
|
|
|
|
tty_StartTimer(p);
|
|
|
|
}
|
|
|
|
return &dev->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct device *
|
|
|
|
tty_Create(struct physical *p)
|
|
|
|
{
|
|
|
|
if (p->fd >= 0 && isatty(p->fd)) {
|
|
|
|
if (*p->name.full == '\0') {
|
|
|
|
log_Printf(LogDEBUG, "%s: Input is a tty\n", p->link.name);
|
|
|
|
physical_SetDevice(p, ttyname(p->fd));
|
|
|
|
if (tty_Lock(p, p->dl->bundle->unit) == -1) {
|
|
|
|
close(p->fd);
|
|
|
|
p->fd = -1;
|
1999-05-13 16:35:13 +00:00
|
|
|
} else
|
|
|
|
return tty_SetupDevice(p);
|
1999-05-12 09:49:12 +00:00
|
|
|
} else if (tty_Lock(p, p->dl->bundle->unit) != -1) {
|
|
|
|
log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full);
|
1999-05-13 16:35:13 +00:00
|
|
|
return tty_SetupDevice(p);
|
1999-05-12 09:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|