slattach.c revision 1.23
1/*	$NetBSD: slattach.c,v 1.23 2001/02/04 21:11:47 christos Exp $	*/
2
3/*
4 * Copyright (c) 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Adams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40#ifndef lint
41__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
42	The Regents of the University of California.  All rights reserved.\n");
43#endif /* not lint */
44
45#ifndef lint
46#if 0
47static char sccsid[] = "@(#)slattach.c	8.2 (Berkeley) 1/7/94";
48#else
49__RCSID("$NetBSD: slattach.c,v 1.23 2001/02/04 21:11:47 christos Exp $");
50#endif
51#endif /* not lint */
52
53#include <sys/param.h>
54#include <sys/socket.h>
55#include <sys/types.h>
56#include <sys/ioctl.h>
57
58#include <net/if.h>
59#include <netinet/in.h>
60
61#include <err.h>
62#include <fcntl.h>
63#include <netdb.h>
64#include <paths.h>
65#include <signal.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <termios.h>
70#include <unistd.h>
71
72int	speed = 9600;
73int	slipdisc = SLIPDISC;
74
75char	devicename[32];
76
77int	main __P((int, char *[]));
78int	ttydisc __P((char *));
79void	usage __P((void));
80
81extern char *__progname;
82
83int
84main(argc, argv)
85	int argc;
86	char *argv[];
87{
88	int fd;
89	char *dev = argv[1];
90	struct termios tty;
91	tcflag_t cflag = HUPCL;
92	int ch;
93	sigset_t sigset;
94	int opt_detach = 1;
95
96	while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
97		switch (ch) {
98		case 'h':
99			cflag |= CRTSCTS;
100			break;
101		case 'H':
102			cflag |= CDTRCTS;
103			break;
104		case 'l':
105			cflag |= CLOCAL;
106			break;
107		case 'm':
108			cflag &= ~HUPCL;
109			break;
110		case 'n':
111			opt_detach = 0;
112			break;
113		case 's':
114			speed = atoi(optarg);
115			break;
116		case 'r': case 't':
117			slipdisc = ttydisc(optarg);
118			break;
119		case '?':
120		default:
121			usage();
122		}
123	}
124	argc -= optind;
125	argv += optind;
126
127	if (argc != 1)
128		usage();
129
130	dev = *argv;
131	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
132		(void)snprintf(devicename, sizeof(devicename),
133		    "%s%s", _PATH_DEV, dev);
134		dev = devicename;
135	}
136	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
137		err(1, "%s", dev);
138	tty.c_cflag = CREAD | CS8 | cflag;
139	tty.c_iflag = 0;
140	tty.c_lflag = 0;
141	tty.c_oflag = 0;
142	tty.c_cc[VMIN] = 1;
143	tty.c_cc[VTIME] = 0;
144	cfsetspeed(&tty, speed);
145	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
146		err(1, "tcsetattr");
147	if (ioctl(fd, TIOCSDTR, 0) < 0)
148		err(1, "TIOCSDTR");
149	if (ioctl(fd, TIOCSETD, &slipdisc) < 0)
150		err(1, "TIOCSETD");
151	if (opt_detach && daemon(0, 0) != 0)
152		err(1, "couldn't detach");
153	sigemptyset(&sigset);
154	for (;;)
155		sigsuspend(&sigset);
156}
157
158int
159ttydisc(name)
160     char *name;
161{
162	if (strcmp(name, "slip") == 0)
163		return(SLIPDISC);
164#ifdef STRIPDISC
165	else if (strcmp(name, "strip") == 0)
166  		return(STRIPDISC);
167#endif
168	else
169		usage();
170	/* NOTREACHED */
171	return -1;
172}
173
174void
175usage()
176{
177	(void)fprintf(stderr,
178	    "Usage: %s [-t ldisc] [-hHlm] [-s baudrate] ttyname\n",
179		__progname);
180	exit(1);
181}
182