slattach.c revision 1.22
11.22Sproff/*	$NetBSD: slattach.c,v 1.22 1999/09/03 13:31:29 proff Exp $	*/
21.15Scgd
31.1Scgd/*
41.12Smycroft * Copyright (c) 1988, 1993
51.12Smycroft *	The Regents of the University of California.  All rights reserved.
61.1Scgd *
71.1Scgd * This code is derived from software contributed to Berkeley by
81.1Scgd * Rick Adams.
91.1Scgd *
101.1Scgd * Redistribution and use in source and binary forms, with or without
111.1Scgd * modification, are permitted provided that the following conditions
121.1Scgd * are met:
131.1Scgd * 1. Redistributions of source code must retain the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer.
151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
161.1Scgd *    notice, this list of conditions and the following disclaimer in the
171.1Scgd *    documentation and/or other materials provided with the distribution.
181.1Scgd * 3. All advertising materials mentioning features or use of this software
191.1Scgd *    must display the following acknowledgement:
201.1Scgd *	This product includes software developed by the University of
211.1Scgd *	California, Berkeley and its contributors.
221.1Scgd * 4. Neither the name of the University nor the names of its contributors
231.1Scgd *    may be used to endorse or promote products derived from this software
241.1Scgd *    without specific prior written permission.
251.1Scgd *
261.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361.1Scgd * SUCH DAMAGE.
371.1Scgd */
381.1Scgd
391.18Slukem#include <sys/cdefs.h>
401.1Scgd#ifndef lint
411.18Slukem__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
421.18Slukem	The Regents of the University of California.  All rights reserved.\n");
431.1Scgd#endif /* not lint */
441.1Scgd
451.1Scgd#ifndef lint
461.15Scgd#if 0
471.15Scgdstatic char sccsid[] = "@(#)slattach.c	8.2 (Berkeley) 1/7/94";
481.15Scgd#else
491.22Sproff__RCSID("$NetBSD: slattach.c,v 1.22 1999/09/03 13:31:29 proff Exp $");
501.15Scgd#endif
511.1Scgd#endif /* not lint */
521.1Scgd
531.1Scgd#include <sys/param.h>
541.12Smycroft#include <sys/socket.h>
551.14Scgd#include <sys/types.h>
561.14Scgd#include <sys/ioctl.h>
571.12Smycroft
581.12Smycroft#include <net/if.h>
591.12Smycroft#include <netinet/in.h>
601.12Smycroft
611.12Smycroft#include <err.h>
621.12Smycroft#include <fcntl.h>
631.1Scgd#include <netdb.h>
641.12Smycroft#include <paths.h>
651.14Scgd#include <signal.h>
661.1Scgd#include <stdio.h>
671.12Smycroft#include <stdlib.h>
681.13Scgd#include <string.h>
691.12Smycroft#include <termios.h>
701.14Scgd#include <unistd.h>
711.12Smycroft
721.12Smycroftint	speed = 9600;
731.12Smycroftint	slipdisc = SLIPDISC;
741.1Scgd
751.12Smycroftchar	devicename[32];
761.1Scgd
771.18Slukemint	main __P((int, char *[]));
781.18Slukemint	ttydisc __P((char *));
791.12Smycroftvoid	usage __P((void));
801.1Scgd
811.17Sjonathan
821.12Smycroftint
831.12Smycroftmain(argc, argv)
841.12Smycroft	int argc;
851.12Smycroft	char *argv[];
861.1Scgd{
871.18Slukem	int fd;
881.18Slukem	char *dev = argv[1];
891.4Scgd	struct termios tty;
901.12Smycroft	tcflag_t cflag = HUPCL;
911.12Smycroft	int ch;
921.16Smycroft	sigset_t sigset;
931.22Sproff	int opt_detach = 1;
941.4Scgd
951.22Sproff	while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
961.12Smycroft		switch (ch) {
971.4Scgd		case 'h':
981.12Smycroft			cflag |= CRTSCTS;
991.19Sscottr			break;
1001.19Sscottr		case 'H':
1011.19Sscottr			cflag |= CDTRCTS;
1021.8Smycroft			break;
1031.21Sfair		case 'l':
1041.21Sfair			cflag |= CLOCAL;
1051.21Sfair			break;
1061.8Smycroft		case 'm':
1071.12Smycroft			cflag &= ~HUPCL;
1081.4Scgd			break;
1091.22Sproff		case 'n':
1101.22Sproff			opt_detach = 0;
1111.22Sproff			break;
1121.4Scgd		case 's':
1131.4Scgd			speed = atoi(optarg);
1141.4Scgd			break;
1151.17Sjonathan		case 'r': case 't':
1161.17Sjonathan			slipdisc = ttydisc(optarg);
1171.17Sjonathan			break;
1181.4Scgd		case '?':
1191.4Scgd		default:
1201.12Smycroft			usage();
1211.4Scgd		}
1221.4Scgd	}
1231.12Smycroft	argc -= optind;
1241.12Smycroft	argv += optind;
1251.4Scgd
1261.12Smycroft	if (argc != 1)
1271.12Smycroft		usage();
1281.4Scgd
1291.12Smycroft	dev = *argv;
1301.1Scgd	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
1311.12Smycroft		(void)snprintf(devicename, sizeof(devicename),
1321.12Smycroft		    "%s%s", _PATH_DEV, dev);
1331.12Smycroft		dev = devicename;
1341.1Scgd	}
1351.22Sproff	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
1361.22Sproff		err(1, "%s", dev);
1371.12Smycroft	tty.c_cflag = CREAD | CS8 | cflag;
1381.4Scgd	tty.c_iflag = 0;
1391.12Smycroft	tty.c_lflag = 0;
1401.4Scgd	tty.c_oflag = 0;
1411.12Smycroft	tty.c_cc[VMIN] = 1;
1421.12Smycroft	tty.c_cc[VTIME] = 0;
1431.12Smycroft	cfsetspeed(&tty, speed);
1441.12Smycroft	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
1451.12Smycroft		err(1, "tcsetattr");
1461.12Smycroft	if (ioctl(fd, TIOCSDTR, 0) < 0)
1471.12Smycroft		err(1, "TIOCSDTR");
1481.12Smycroft	if (ioctl(fd, TIOCSETD, &slipdisc) < 0)
1491.12Smycroft		err(1, "TIOCSETD");
1501.22Sproff	if (opt_detach && daemon(0, 0) != 0)
1511.22Sproff		err(1, "couldn't detach");
1521.16Smycroft	sigemptyset(&sigset);
1531.1Scgd	for (;;)
1541.16Smycroft		sigsuspend(&sigset);
1551.12Smycroft}
1561.12Smycroft
1571.17Sjonathanint
1581.17Sjonathanttydisc(name)
1591.17Sjonathan     char *name;
1601.17Sjonathan{
1611.17Sjonathan	if (strcmp(name, "slip") == 0)
1621.17Sjonathan		return(SLIPDISC);
1631.17Sjonathan#ifdef STRIPDISC
1641.17Sjonathan	else if (strcmp(name, "strip") == 0)
1651.17Sjonathan  		return(STRIPDISC);
1661.17Sjonathan#endif
1671.17Sjonathan	else
1681.17Sjonathan		usage();
1691.18Slukem	/* NOTREACHED */
1701.18Slukem	return -1;
1711.17Sjonathan}
1721.17Sjonathan
1731.12Smycroftvoid
1741.12Smycroftusage()
1751.12Smycroft{
1761.20Sthorpej	extern char *__progname;
1771.12Smycroft
1781.17Sjonathan	(void)fprintf(stderr,
1791.21Sfair	    "usage: %s [-t ldisc] [-hHlm] [-s baudrate] ttyname\n",
1801.20Sthorpej		__progname);
1811.12Smycroft	exit(1);
1821.1Scgd}
183