xref: /netbsd-src/sbin/slattach/slattach.c (revision 45802ab30c6a2fe6336225c2bc67e264b206c123)
1 /*	$NetBSD: slattach.c,v 1.33 2013/10/20 20:17:52 mbalmer 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)slattach.c	8.2 (Berkeley) 1/7/94";
44 #else
45 __RCSID("$NetBSD: slattach.c,v 1.33 2013/10/20 20:17:52 mbalmer Exp $");
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
53 
54 #include <net/if.h>
55 #include <netinet/in.h>
56 
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <netdb.h>
61 #include <paths.h>
62 #include <signal.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <termios.h>
67 #include <unistd.h>
68 
69 static int	speed = 9600;
70 static const char	*ldisc = "slip";
71 
72 static char	devicename[32];
73 
74 __dead static void	usage(void);
75 
76 int
main(int argc,char * argv[])77 main(int argc, char *argv[])
78 {
79 	int fd;
80 	char *dev = argv[1];
81 	struct termios tty;
82 	tcflag_t cflag = HUPCL;
83 	int ch;
84 	sigset_t nsigset;
85 	int opt_detach = 1;
86 
87 	while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
88 		switch (ch) {
89 		case 'h':
90 			cflag |= CRTSCTS;
91 			break;
92 		case 'H':
93 			cflag |= CDTRCTS;
94 			break;
95 		case 'l':
96 			cflag |= CLOCAL;
97 			break;
98 		case 'm':
99 			cflag &= ~HUPCL;
100 			break;
101 		case 'n':
102 			opt_detach = 0;
103 			break;
104 		case 's':
105 			speed = atoi(optarg);
106 			break;
107 		case 't':
108 			ldisc = optarg;
109 			break;
110 		case '?':
111 		default:
112 			usage();
113 		}
114 	}
115 	argc -= optind;
116 	argv += optind;
117 
118 	if (argc != 1)
119 		usage();
120 
121 	dev = *argv;
122 	if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
123 		(void)snprintf(devicename, sizeof(devicename),
124 		    "%s%s", _PATH_DEV, dev);
125 		dev = devicename;
126 	}
127 	if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
128 		err(1, "%s", dev);
129 	tty.c_cflag = CREAD | CS8 | cflag;
130 	tty.c_iflag = 0;
131 	tty.c_lflag = 0;
132 	tty.c_oflag = 0;
133 	tty.c_cc[VMIN] = 1;
134 	tty.c_cc[VTIME] = 0;
135 	cfsetspeed(&tty, speed);
136 	if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
137 		err(1, "tcsetattr");
138 	if (ioctl(fd, TIOCSDTR, 0) < 0 && errno != ENOTTY)
139 		err(1, "TIOCSDTR");
140 	if (ioctl(fd, TIOCSLINED, ldisc) < 0)
141 		err(1, "TIOCSLINED");
142 	if (opt_detach && daemon(0, 0) != 0)
143 		err(1, "couldn't detach");
144 	sigemptyset(&nsigset);
145 	for (;;)
146 		sigsuspend(&nsigset);
147 }
148 
149 static void
usage(void)150 usage(void)
151 {
152 
153 	(void)fprintf(stderr,
154 	    "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n",
155 		getprogname());
156 	exit(1);
157 }
158