1995-02-17 14:36:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Ugen J.S.Antsilevich
|
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
|
|
|
*
|
|
|
|
* Snoop stuff.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 21:06:42 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-10-27 12:21:10 +00:00
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
#include <sys/param.h>
|
1995-02-17 14:36:03 +00:00
|
|
|
#include <sys/fcntl.h>
|
1997-03-13 08:35:45 +00:00
|
|
|
#include <sys/filio.h>
|
1995-02-17 14:36:03 +00:00
|
|
|
#include <sys/snoop.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
#include <sys/stat.h>
|
2001-09-09 14:57:48 +00:00
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/module.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
|
1997-10-27 12:21:10 +00:00
|
|
|
#include <err.h>
|
2004-08-10 01:49:46 +00:00
|
|
|
#include <errno.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
#include <locale.h>
|
2000-12-09 09:35:55 +00:00
|
|
|
#include <paths.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-13 08:35:45 +00:00
|
|
|
#include <sysexits.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
#include <termcap.h>
|
|
|
|
#include <termios.h>
|
2002-02-25 05:31:49 +00:00
|
|
|
#include <time.h>
|
1997-03-19 04:44:07 +00:00
|
|
|
#include <unistd.h>
|
1995-02-17 14:36:03 +00:00
|
|
|
|
|
|
|
#define MSG_INIT "Snoop started."
|
1995-02-25 20:14:31 +00:00
|
|
|
#define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
|
|
|
|
#define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
|
1995-02-17 14:36:03 +00:00
|
|
|
#define MSG_CHANGE "Snoop device change by user request."
|
1995-02-27 19:48:19 +00:00
|
|
|
#define MSG_NOWRITE "Snoop device change due to write failure."
|
1995-02-17 14:36:03 +00:00
|
|
|
|
1995-02-25 20:14:31 +00:00
|
|
|
#define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
|
1995-02-17 14:36:03 +00:00
|
|
|
#define MIN_SIZE 256
|
|
|
|
|
|
|
|
#define CHR_SWITCH 24 /* Ctrl+X */
|
|
|
|
#define CHR_CLEAR 23 /* Ctrl+V */
|
|
|
|
|
2002-06-12 04:28:15 +00:00
|
|
|
static void clear(void);
|
|
|
|
static void timestamp(const char *);
|
|
|
|
static void set_tty(void);
|
|
|
|
static void unset_tty(void);
|
|
|
|
static void fatal(int, const char *);
|
|
|
|
static int open_snp(void);
|
|
|
|
static void cleanup(int);
|
|
|
|
static void usage(void) __dead2;
|
|
|
|
static void setup_scr(void);
|
|
|
|
static void attach_snp(void);
|
|
|
|
static void detach_snp(void);
|
|
|
|
static void set_dev(const char *);
|
|
|
|
static void ask_dev(char *, const char *);
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2012-03-10 14:35:09 +00:00
|
|
|
int opt_reconn_close = 0;
|
|
|
|
int opt_reconn_oflow = 0;
|
|
|
|
int opt_interactive = 1;
|
|
|
|
int opt_timestamp = 0;
|
1995-02-27 19:48:19 +00:00
|
|
|
int opt_write = 0;
|
1996-12-02 12:32:46 +00:00
|
|
|
int opt_no_switch = 0;
|
2001-11-24 15:41:38 +00:00
|
|
|
const char *opt_snpdev;
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2012-03-10 14:35:09 +00:00
|
|
|
char dev_name[DEV_NAME_LEN];
|
|
|
|
int snp_io;
|
|
|
|
int std_in = 0, std_out = 1;
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2012-03-10 14:35:09 +00:00
|
|
|
int clear_ok = 0;
|
|
|
|
struct termios otty;
|
|
|
|
char tbuf[1024], gbuf[1024];
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
clear(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
if (clear_ok)
|
2001-07-15 11:03:28 +00:00
|
|
|
tputs(gbuf, 1, putchar);
|
1995-02-17 14:36:03 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
timestamp(const char *buf)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
time_t t;
|
|
|
|
char btmp[1024];
|
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
clear();
|
|
|
|
printf("\n---------------------------------------------\n");
|
|
|
|
t = time(NULL);
|
|
|
|
strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
|
|
|
|
printf("%s\n", btmp);
|
|
|
|
printf("%s\n", buf);
|
|
|
|
printf("---------------------------------------------\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
set_tty(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
struct termios ntty;
|
1997-03-13 08:35:45 +00:00
|
|
|
|
2012-03-10 14:38:34 +00:00
|
|
|
tcgetattr(std_in, &otty);
|
1997-03-13 08:35:45 +00:00
|
|
|
ntty = otty;
|
2012-03-10 14:35:09 +00:00
|
|
|
ntty.c_lflag &= ~ICANON; /* disable canonical operation */
|
1997-03-13 08:35:45 +00:00
|
|
|
ntty.c_lflag &= ~ECHO;
|
|
|
|
#ifdef FLUSHO
|
|
|
|
ntty.c_lflag &= ~FLUSHO;
|
|
|
|
#endif
|
|
|
|
#ifdef PENDIN
|
|
|
|
ntty.c_lflag &= ~PENDIN;
|
|
|
|
#endif
|
|
|
|
#ifdef IEXTEN
|
|
|
|
ntty.c_lflag &= ~IEXTEN;
|
|
|
|
#endif
|
2012-03-10 14:35:09 +00:00
|
|
|
ntty.c_cc[VMIN] = 1; /* minimum of one character */
|
|
|
|
ntty.c_cc[VTIME] = 0; /* timeout value */
|
1997-03-13 08:35:45 +00:00
|
|
|
|
2012-03-10 14:35:09 +00:00
|
|
|
ntty.c_cc[VINTR] = 07; /* ^G */
|
|
|
|
ntty.c_cc[VQUIT] = 07; /* ^G */
|
2012-03-10 14:38:34 +00:00
|
|
|
tcsetattr(std_in, TCSANOW, &ntty);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
unset_tty(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:38:34 +00:00
|
|
|
|
|
|
|
tcsetattr(std_in, TCSANOW, &otty);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
fatal(int error, const char *buf)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:38:34 +00:00
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
unset_tty();
|
|
|
|
if (buf)
|
2001-07-15 11:03:28 +00:00
|
|
|
errx(error, "fatal: %s", buf);
|
1997-10-27 12:21:10 +00:00
|
|
|
else
|
2001-07-15 11:03:28 +00:00
|
|
|
exit(error);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static int
|
2002-06-12 04:28:15 +00:00
|
|
|
open_snp(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2008-08-15 13:07:07 +00:00
|
|
|
int f, mode;
|
1995-02-27 19:48:19 +00:00
|
|
|
|
|
|
|
if (opt_write)
|
|
|
|
mode = O_RDWR;
|
|
|
|
else
|
|
|
|
mode = O_RDONLY;
|
|
|
|
|
2001-11-24 15:41:38 +00:00
|
|
|
if (opt_snpdev == NULL)
|
2008-08-15 13:07:07 +00:00
|
|
|
f = open(_PATH_DEV "snp", mode);
|
2001-11-24 15:41:38 +00:00
|
|
|
else
|
2008-08-15 13:07:07 +00:00
|
|
|
f = open(opt_snpdev, mode);
|
|
|
|
if (f == -1)
|
|
|
|
fatal(EX_OSFILE, "cannot open snoop device");
|
|
|
|
|
|
|
|
return (f);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
cleanup(int signo __unused)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:38:34 +00:00
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
if (opt_timestamp)
|
|
|
|
timestamp("Logging Exited.");
|
|
|
|
close(snp_io);
|
|
|
|
unset_tty();
|
1997-03-13 08:35:45 +00:00
|
|
|
exit(EX_OK);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
1997-10-27 12:21:10 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
usage(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:38:34 +00:00
|
|
|
|
1997-10-27 12:21:10 +00:00
|
|
|
fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
|
1997-03-13 08:35:45 +00:00
|
|
|
exit(EX_USAGE);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
setup_scr(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
char *cbuf = gbuf, *term;
|
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
if (!opt_interactive)
|
|
|
|
return;
|
|
|
|
if ((term = getenv("TERM")))
|
|
|
|
if (tgetent(tbuf, term) == 1)
|
|
|
|
if (tgetstr("cl", &cbuf))
|
|
|
|
clear_ok = 1;
|
|
|
|
set_tty();
|
1995-08-04 07:17:24 +00:00
|
|
|
clear();
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
detach_snp(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2005-09-19 13:48:45 +00:00
|
|
|
int fd;
|
1995-02-25 20:14:31 +00:00
|
|
|
|
2005-09-19 13:48:45 +00:00
|
|
|
fd = -1;
|
|
|
|
ioctl(snp_io, SNPSTTY, &fd);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
attach_snp(void)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2005-09-21 14:30:14 +00:00
|
|
|
int snp_tty;
|
|
|
|
|
|
|
|
snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (snp_tty < 0)
|
|
|
|
fatal(EX_DATAERR, "can't open device");
|
1995-02-17 14:36:03 +00:00
|
|
|
if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_UNAVAILABLE, "cannot attach to tty");
|
2005-09-21 14:30:14 +00:00
|
|
|
close(snp_tty);
|
1995-02-17 14:36:03 +00:00
|
|
|
if (opt_timestamp)
|
|
|
|
timestamp("Logging Started.");
|
|
|
|
}
|
|
|
|
|
2001-07-15 11:03:28 +00:00
|
|
|
static void
|
2002-06-12 04:28:15 +00:00
|
|
|
set_dev(const char *name)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
char buf[DEV_NAME_LEN];
|
1995-02-25 20:14:31 +00:00
|
|
|
struct stat sb;
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2000-12-09 09:35:55 +00:00
|
|
|
if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
|
1997-03-13 08:35:45 +00:00
|
|
|
snprintf(buf, sizeof buf, "%s", name);
|
2002-06-12 04:28:15 +00:00
|
|
|
} else {
|
1995-02-27 19:48:19 +00:00
|
|
|
if (strlen(name) == 2)
|
2000-12-09 09:35:55 +00:00
|
|
|
sprintf(buf, "%s%s", _PATH_TTY, name);
|
1995-02-27 19:48:19 +00:00
|
|
|
else
|
2000-12-09 09:35:55 +00:00
|
|
|
sprintf(buf, "%s%s", _PATH_DEV, name);
|
1995-02-27 19:48:19 +00:00
|
|
|
}
|
1995-02-25 20:14:31 +00:00
|
|
|
|
1997-03-13 08:35:45 +00:00
|
|
|
if (*name == '\0' || stat(buf, &sb) < 0)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_DATAERR, "bad device name");
|
1997-03-13 08:35:45 +00:00
|
|
|
|
|
|
|
if ((sb.st_mode & S_IFMT) != S_IFCHR)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_DATAERR, "must be a character device");
|
1995-02-25 20:14:31 +00:00
|
|
|
|
2005-09-21 14:30:14 +00:00
|
|
|
strncpy(dev_name, buf, DEV_NAME_LEN);
|
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
attach_snp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-06-12 04:28:15 +00:00
|
|
|
ask_dev(char *dbuf, const char *msg)
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
char buf[DEV_NAME_LEN];
|
|
|
|
int len;
|
1995-02-17 14:36:03 +00:00
|
|
|
|
|
|
|
clear();
|
|
|
|
unset_tty();
|
|
|
|
|
|
|
|
if (msg)
|
|
|
|
printf("%s\n", msg);
|
2001-07-15 11:03:28 +00:00
|
|
|
if (dbuf)
|
|
|
|
printf("Enter device name [%s]:", dbuf);
|
1995-02-17 14:36:03 +00:00
|
|
|
else
|
|
|
|
printf("Enter device name:");
|
|
|
|
|
|
|
|
if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
|
|
|
|
len = strlen(buf);
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
if (buf[0] != '\0' && buf[0] != ' ')
|
2001-07-15 11:03:28 +00:00
|
|
|
strcpy(dbuf, buf);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
set_tty();
|
|
|
|
}
|
|
|
|
|
1995-02-27 19:48:19 +00:00
|
|
|
#define READB_LEN 5
|
1995-02-17 14:36:03 +00:00
|
|
|
|
1999-09-15 01:58:44 +00:00
|
|
|
int
|
2002-06-12 04:28:15 +00:00
|
|
|
main(int ac, char *av[])
|
1995-02-17 14:36:03 +00:00
|
|
|
{
|
2012-03-10 14:35:09 +00:00
|
|
|
int ch, res, rv, nread;
|
2002-11-11 10:56:57 +00:00
|
|
|
size_t b_size = MIN_SIZE;
|
2012-03-10 14:35:09 +00:00
|
|
|
char *buf, chb[READB_LEN];
|
|
|
|
fd_set fd_s;
|
1995-02-17 14:36:03 +00:00
|
|
|
|
1995-10-26 23:15:14 +00:00
|
|
|
(void) setlocale(LC_TIME, "");
|
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
if (isatty(std_out))
|
|
|
|
opt_interactive = 1;
|
|
|
|
else
|
|
|
|
opt_interactive = 0;
|
|
|
|
|
2001-11-24 15:41:38 +00:00
|
|
|
while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
|
1995-02-17 14:36:03 +00:00
|
|
|
switch (ch) {
|
1995-02-27 19:48:19 +00:00
|
|
|
case 'W':
|
|
|
|
opt_write = 1;
|
|
|
|
break;
|
1995-02-17 14:36:03 +00:00
|
|
|
case 'c':
|
|
|
|
opt_reconn_close = 1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
opt_interactive = 1;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
opt_reconn_oflow = 1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
opt_timestamp = 1;
|
|
|
|
break;
|
1996-12-02 12:32:46 +00:00
|
|
|
case 'n':
|
|
|
|
opt_no_switch = 1;
|
|
|
|
break;
|
2001-11-24 15:41:38 +00:00
|
|
|
case 'f':
|
|
|
|
opt_snpdev = optarg;
|
|
|
|
break;
|
1995-02-17 14:36:03 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
1997-10-27 12:21:10 +00:00
|
|
|
usage();
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
|
2001-09-09 14:57:48 +00:00
|
|
|
if (modfind("snp") == -1)
|
|
|
|
if (kldload("snp") == -1 || modfind("snp") == -1)
|
|
|
|
warn("snp module not available");
|
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
signal(SIGINT, cleanup);
|
|
|
|
|
|
|
|
snp_io = open_snp();
|
2004-08-10 01:49:46 +00:00
|
|
|
setup_scr();
|
1995-02-17 14:36:03 +00:00
|
|
|
|
|
|
|
if (*(av += optind) == NULL) {
|
1996-12-02 12:32:46 +00:00
|
|
|
if (opt_interactive && !opt_no_switch)
|
1995-02-17 14:36:03 +00:00
|
|
|
ask_dev(dev_name, MSG_INIT);
|
|
|
|
else
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_DATAERR, "no device name given");
|
1995-02-17 14:36:03 +00:00
|
|
|
} else
|
|
|
|
strncpy(dev_name, *av, DEV_NAME_LEN);
|
|
|
|
|
|
|
|
set_dev(dev_name);
|
|
|
|
|
|
|
|
if (!(buf = (char *) malloc(b_size)))
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_UNAVAILABLE, "malloc failed");
|
1995-02-17 14:36:03 +00:00
|
|
|
|
|
|
|
FD_ZERO(&fd_s);
|
|
|
|
|
2012-03-10 14:38:34 +00:00
|
|
|
for (;;) {
|
1995-02-17 14:36:03 +00:00
|
|
|
if (opt_interactive)
|
|
|
|
FD_SET(std_in, &fd_s);
|
|
|
|
FD_SET(snp_io, &fd_s);
|
|
|
|
res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
|
|
|
|
if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
|
1995-02-27 19:48:19 +00:00
|
|
|
|
|
|
|
if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_OSERR, "ioctl(FIONREAD)");
|
1995-02-27 19:48:19 +00:00
|
|
|
if (nread > READB_LEN)
|
|
|
|
nread = READB_LEN;
|
2001-07-15 11:03:28 +00:00
|
|
|
rv = read(std_in, chb, nread);
|
2002-11-11 10:56:57 +00:00
|
|
|
if (rv == -1 || rv != nread)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_IOERR, "read (stdin) failed");
|
1995-02-27 19:48:19 +00:00
|
|
|
|
|
|
|
switch (chb[0]) {
|
1995-02-17 14:36:03 +00:00
|
|
|
case CHR_CLEAR:
|
|
|
|
clear();
|
|
|
|
break;
|
|
|
|
case CHR_SWITCH:
|
2001-11-07 19:15:19 +00:00
|
|
|
if (!opt_no_switch) {
|
|
|
|
detach_snp();
|
|
|
|
ask_dev(dev_name, MSG_CHANGE);
|
|
|
|
set_dev(dev_name);
|
1996-12-02 12:32:46 +00:00
|
|
|
break;
|
2001-11-07 19:15:19 +00:00
|
|
|
}
|
1995-02-17 14:36:03 +00:00
|
|
|
default:
|
1995-02-27 19:48:19 +00:00
|
|
|
if (opt_write) {
|
2001-07-15 11:03:28 +00:00
|
|
|
rv = write(snp_io, chb, nread);
|
2002-11-11 10:56:57 +00:00
|
|
|
if (rv == -1 || rv != nread) {
|
1995-02-27 19:48:19 +00:00
|
|
|
detach_snp();
|
1996-12-02 12:32:46 +00:00
|
|
|
if (opt_no_switch)
|
2002-06-12 04:28:15 +00:00
|
|
|
fatal(EX_IOERR,
|
2012-03-10 14:35:09 +00:00
|
|
|
"write failed");
|
1995-02-27 19:48:19 +00:00
|
|
|
ask_dev(dev_name, MSG_NOWRITE);
|
|
|
|
set_dev(dev_name);
|
|
|
|
}
|
|
|
|
}
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!FD_ISSET(snp_io, &fd_s))
|
|
|
|
continue;
|
|
|
|
|
2002-11-11 10:56:57 +00:00
|
|
|
if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_OSERR, "ioctl(FIONREAD)");
|
1995-02-17 14:36:03 +00:00
|
|
|
|
2002-11-11 10:56:57 +00:00
|
|
|
switch (nread) {
|
1995-02-17 14:36:03 +00:00
|
|
|
case SNP_OFLOW:
|
|
|
|
if (opt_reconn_oflow)
|
|
|
|
attach_snp();
|
1996-12-02 12:32:46 +00:00
|
|
|
else if (opt_interactive && !opt_no_switch) {
|
1995-02-17 14:36:03 +00:00
|
|
|
ask_dev(dev_name, MSG_OFLOW);
|
|
|
|
set_dev(dev_name);
|
|
|
|
} else
|
2001-07-15 11:03:28 +00:00
|
|
|
cleanup(-1);
|
2002-08-10 08:42:10 +00:00
|
|
|
break;
|
1995-02-17 14:36:03 +00:00
|
|
|
case SNP_DETACH:
|
|
|
|
case SNP_TTYCLOSE:
|
|
|
|
if (opt_reconn_close)
|
|
|
|
attach_snp();
|
1996-12-02 12:32:46 +00:00
|
|
|
else if (opt_interactive && !opt_no_switch) {
|
1995-02-17 14:36:03 +00:00
|
|
|
ask_dev(dev_name, MSG_CLOSED);
|
|
|
|
set_dev(dev_name);
|
|
|
|
} else
|
2001-07-15 11:03:28 +00:00
|
|
|
cleanup(-1);
|
2002-08-10 08:42:10 +00:00
|
|
|
break;
|
1995-02-17 14:36:03 +00:00
|
|
|
default:
|
|
|
|
if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
|
|
|
|
free(buf);
|
|
|
|
if (!(buf = (char *) malloc(b_size / 2)))
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_UNAVAILABLE, "malloc failed");
|
1995-02-17 14:36:03 +00:00
|
|
|
b_size = b_size / 2;
|
|
|
|
}
|
|
|
|
if (nread > b_size) {
|
|
|
|
b_size = (nread % 2) ? (nread + 1) : (nread);
|
|
|
|
free(buf);
|
|
|
|
if (!(buf = (char *) malloc(b_size)))
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_UNAVAILABLE, "malloc failed");
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
2001-07-15 11:03:28 +00:00
|
|
|
rv = read(snp_io, buf, nread);
|
2002-11-11 10:56:57 +00:00
|
|
|
if (rv == -1 || rv != nread)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_IOERR, "read failed");
|
2001-07-15 11:03:28 +00:00
|
|
|
rv = write(std_out, buf, nread);
|
2002-11-11 10:56:57 +00:00
|
|
|
if (rv == -1 || rv != nread)
|
1997-10-27 12:21:10 +00:00
|
|
|
fatal(EX_IOERR, "write failed");
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|
|
|
|
} /* While */
|
2000-01-23 20:27:32 +00:00
|
|
|
return(0);
|
1995-02-17 14:36:03 +00:00
|
|
|
}
|