slattach.c revision 1.32
11.32Schristos/*	$NetBSD: slattach.c,v 1.32 2011/12/30 03:19:36 christos 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.27Sagc * 3. Neither the name of the University nor the names of its contributors
191.1Scgd *    may be used to endorse or promote products derived from this software
201.1Scgd *    without specific prior written permission.
211.1Scgd *
221.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Scgd * SUCH DAMAGE.
331.1Scgd */
341.1Scgd
351.18Slukem#include <sys/cdefs.h>
361.1Scgd#ifndef lint
371.30Slukem__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
381.30Slukem The Regents of the University of California.  All rights reserved.");
391.1Scgd#endif /* not lint */
401.1Scgd
411.1Scgd#ifndef lint
421.15Scgd#if 0
431.15Scgdstatic char sccsid[] = "@(#)slattach.c	8.2 (Berkeley) 1/7/94";
441.15Scgd#else
451.32Schristos__RCSID("$NetBSD: slattach.c,v 1.32 2011/12/30 03:19:36 christos Exp $");
461.15Scgd#endif
471.1Scgd#endif /* not lint */
481.1Scgd
491.1Scgd#include <sys/param.h>
501.12Smycroft#include <sys/socket.h>
511.14Scgd#include <sys/types.h>
521.14Scgd#include <sys/ioctl.h>
531.12Smycroft
541.12Smycroft#include <net/if.h>
551.12Smycroft#include <netinet/in.h>
561.12Smycroft
571.12Smycroft#include <err.h>
581.32Schristos#include <errno.h>
591.12Smycroft#include <fcntl.h>
601.1Scgd#include <netdb.h>
611.12Smycroft#include <paths.h>
621.14Scgd#include <signal.h>
631.1Scgd#include <stdio.h>
641.12Smycroft#include <stdlib.h>
651.13Scgd#include <string.h>
661.12Smycroft#include <termios.h>
671.14Scgd#include <unistd.h>
681.12Smycroft
691.31Sjoergstatic int	speed = 9600;
701.31Sjoergstatic int	slipdisc = SLIPDISC;
711.1Scgd
721.31Sjoergstatic char	devicename[32];
731.1Scgd
741.31Sjoergstatic int	ttydisc(char *);
751.31Sjoerg__dead static void	usage(void);
761.1Scgd
771.12Smycroftint
781.29Sxtraememain(int argc, char *argv[])
791.1Scgd{
801.18Slukem	int fd;
811.18Slukem	char *dev = argv[1];
821.4Scgd	struct termios tty;
831.12Smycroft	tcflag_t cflag = HUPCL;
841.12Smycroft	int ch;
851.26Skleink	sigset_t nsigset;
861.22Sproff	int opt_detach = 1;
871.4Scgd
881.22Sproff	while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
891.12Smycroft		switch (ch) {
901.4Scgd		case 'h':
911.12Smycroft			cflag |= CRTSCTS;
921.19Sscottr			break;
931.19Sscottr		case 'H':
941.19Sscottr			cflag |= CDTRCTS;
951.8Smycroft			break;
961.21Sfair		case 'l':
971.21Sfair			cflag |= CLOCAL;
981.21Sfair			break;
991.8Smycroft		case 'm':
1001.12Smycroft			cflag &= ~HUPCL;
1011.4Scgd			break;
1021.22Sproff		case 'n':
1031.22Sproff			opt_detach = 0;
1041.22Sproff			break;
1051.4Scgd		case 's':
1061.4Scgd			speed = atoi(optarg);
1071.4Scgd			break;
1081.17Sjonathan		case 'r': case 't':
1091.17Sjonathan			slipdisc = ttydisc(optarg);
1101.17Sjonathan			break;
1111.4Scgd		case '?':
1121.4Scgd		default:
1131.12Smycroft			usage();
1141.4Scgd		}
1151.4Scgd	}
1161.12Smycroft	argc -= optind;
1171.12Smycroft	argv += optind;
1181.4Scgd
1191.12Smycroft	if (argc != 1)
1201.12Smycroft		usage();
1211.4Scgd
1221.12Smycroft	dev = *argv;
1231.1Scgd	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
1241.12Smycroft		(void)snprintf(devicename, sizeof(devicename),
1251.12Smycroft		    "%s%s", _PATH_DEV, dev);
1261.12Smycroft		dev = devicename;
1271.1Scgd	}
1281.22Sproff	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
1291.22Sproff		err(1, "%s", dev);
1301.12Smycroft	tty.c_cflag = CREAD | CS8 | cflag;
1311.4Scgd	tty.c_iflag = 0;
1321.12Smycroft	tty.c_lflag = 0;
1331.4Scgd	tty.c_oflag = 0;
1341.12Smycroft	tty.c_cc[VMIN] = 1;
1351.12Smycroft	tty.c_cc[VTIME] = 0;
1361.12Smycroft	cfsetspeed(&tty, speed);
1371.12Smycroft	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
1381.12Smycroft		err(1, "tcsetattr");
1391.32Schristos	if (ioctl(fd, TIOCSDTR, 0) < 0 && errno != ENOTTY)
1401.12Smycroft		err(1, "TIOCSDTR");
1411.12Smycroft	if (ioctl(fd, TIOCSETD, &slipdisc) < 0)
1421.12Smycroft		err(1, "TIOCSETD");
1431.22Sproff	if (opt_detach && daemon(0, 0) != 0)
1441.22Sproff		err(1, "couldn't detach");
1451.26Skleink	sigemptyset(&nsigset);
1461.1Scgd	for (;;)
1471.26Skleink		sigsuspend(&nsigset);
1481.12Smycroft}
1491.12Smycroft
1501.31Sjoergstatic int
1511.29Sxtraemettydisc(char *name)
1521.17Sjonathan{
1531.17Sjonathan	if (strcmp(name, "slip") == 0)
1541.17Sjonathan		return(SLIPDISC);
1551.17Sjonathan#ifdef STRIPDISC
1561.17Sjonathan	else if (strcmp(name, "strip") == 0)
1571.17Sjonathan  		return(STRIPDISC);
1581.17Sjonathan#endif
1591.17Sjonathan	else
1601.17Sjonathan		usage();
1611.18Slukem	/* NOTREACHED */
1621.18Slukem	return -1;
1631.17Sjonathan}
1641.17Sjonathan
1651.31Sjoergstatic void
1661.29Sxtraemeusage(void)
1671.12Smycroft{
1681.24Scgd
1691.17Sjonathan	(void)fprintf(stderr,
1701.28Sjmmv	    "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n",
1711.24Scgd		getprogname());
1721.12Smycroft	exit(1);
1731.1Scgd}
174