1*45802ab3Smbalmer /* $NetBSD: slattach.c,v 1.33 2013/10/20 20:17:52 mbalmer Exp $ */
20114e805Scgd
361f28255Scgd /*
43c78ef33Smycroft * Copyright (c) 1988, 1993
53c78ef33Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Rick Adams.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18276d62f6Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
35048874c5Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
376543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
386543a91fSlukem The Regents of the University of California. All rights reserved.");
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4161f28255Scgd #ifndef lint
420114e805Scgd #if 0
430114e805Scgd static char sccsid[] = "@(#)slattach.c 8.2 (Berkeley) 1/7/94";
440114e805Scgd #else
45*45802ab3Smbalmer __RCSID("$NetBSD: slattach.c,v 1.33 2013/10/20 20:17:52 mbalmer Exp $");
460114e805Scgd #endif
4761f28255Scgd #endif /* not lint */
4861f28255Scgd
4961f28255Scgd #include <sys/param.h>
503c78ef33Smycroft #include <sys/socket.h>
511cb390e8Scgd #include <sys/types.h>
521cb390e8Scgd #include <sys/ioctl.h>
533c78ef33Smycroft
543c78ef33Smycroft #include <net/if.h>
553c78ef33Smycroft #include <netinet/in.h>
563c78ef33Smycroft
573c78ef33Smycroft #include <err.h>
58b8425b06Schristos #include <errno.h>
5961f28255Scgd #include <fcntl.h>
603c78ef33Smycroft #include <netdb.h>
6161f28255Scgd #include <paths.h>
621cb390e8Scgd #include <signal.h>
633c78ef33Smycroft #include <stdio.h>
643c78ef33Smycroft #include <stdlib.h>
6596209ecfScgd #include <string.h>
663c78ef33Smycroft #include <termios.h>
671cb390e8Scgd #include <unistd.h>
6861f28255Scgd
69bab034baSjoerg static int speed = 9600;
70*45802ab3Smbalmer static const char *ldisc = "slip";
7161f28255Scgd
72bab034baSjoerg static char devicename[32];
73e43b6d9dScgd
74bab034baSjoerg __dead static void usage(void);
75c2a1985aSjonathan
763c78ef33Smycroft int
main(int argc,char * argv[])77921249f1Sxtraeme main(int argc, char *argv[])
783c78ef33Smycroft {
79048874c5Slukem int fd;
80048874c5Slukem char *dev = argv[1];
813c78ef33Smycroft struct termios tty;
823c78ef33Smycroft tcflag_t cflag = HUPCL;
833c78ef33Smycroft int ch;
84b3df6303Skleink sigset_t nsigset;
8550aa8b2bSproff int opt_detach = 1;
863c78ef33Smycroft
8750aa8b2bSproff while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) {
883c78ef33Smycroft switch (ch) {
89e43b6d9dScgd case 'h':
903c78ef33Smycroft cflag |= CRTSCTS;
91d1a8a089Smycroft break;
9294cf4332Sscottr case 'H':
9394cf4332Sscottr cflag |= CDTRCTS;
9494cf4332Sscottr break;
95782eb428Sfair case 'l':
96782eb428Sfair cflag |= CLOCAL;
97782eb428Sfair break;
98d1a8a089Smycroft case 'm':
993c78ef33Smycroft cflag &= ~HUPCL;
100e43b6d9dScgd break;
10150aa8b2bSproff case 'n':
10250aa8b2bSproff opt_detach = 0;
10350aa8b2bSproff break;
104e43b6d9dScgd case 's':
105e43b6d9dScgd speed = atoi(optarg);
106e43b6d9dScgd break;
107*45802ab3Smbalmer case 't':
108*45802ab3Smbalmer ldisc = optarg;
109c2a1985aSjonathan break;
110e43b6d9dScgd case '?':
111e43b6d9dScgd default:
1123c78ef33Smycroft usage();
11361f28255Scgd }
114e43b6d9dScgd }
1153c78ef33Smycroft argc -= optind;
1163c78ef33Smycroft argv += optind;
117e43b6d9dScgd
1183c78ef33Smycroft if (argc != 1)
1193c78ef33Smycroft usage();
120e43b6d9dScgd
1213c78ef33Smycroft dev = *argv;
12261f28255Scgd if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) {
1233c78ef33Smycroft (void)snprintf(devicename, sizeof(devicename),
1243c78ef33Smycroft "%s%s", _PATH_DEV, dev);
1253c78ef33Smycroft dev = devicename;
12661f28255Scgd }
12750aa8b2bSproff if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0)
12850aa8b2bSproff err(1, "%s", dev);
1293c78ef33Smycroft tty.c_cflag = CREAD | CS8 | cflag;
130e43b6d9dScgd tty.c_iflag = 0;
131e43b6d9dScgd tty.c_lflag = 0;
1323c78ef33Smycroft tty.c_oflag = 0;
1333c78ef33Smycroft tty.c_cc[VMIN] = 1;
1343c78ef33Smycroft tty.c_cc[VTIME] = 0;
1353c78ef33Smycroft cfsetspeed(&tty, speed);
1363c78ef33Smycroft if (tcsetattr(fd, TCSADRAIN, &tty) < 0)
1373c78ef33Smycroft err(1, "tcsetattr");
138b8425b06Schristos if (ioctl(fd, TIOCSDTR, 0) < 0 && errno != ENOTTY)
1393c78ef33Smycroft err(1, "TIOCSDTR");
140*45802ab3Smbalmer if (ioctl(fd, TIOCSLINED, ldisc) < 0)
141*45802ab3Smbalmer err(1, "TIOCSLINED");
14250aa8b2bSproff if (opt_detach && daemon(0, 0) != 0)
14350aa8b2bSproff err(1, "couldn't detach");
144b3df6303Skleink sigemptyset(&nsigset);
14561f28255Scgd for (;;)
146b3df6303Skleink sigsuspend(&nsigset);
14761f28255Scgd }
1483c78ef33Smycroft
149bab034baSjoerg static void
usage(void)150921249f1Sxtraeme usage(void)
1513c78ef33Smycroft {
1528a986b2eScgd
153c2a1985aSjonathan (void)fprintf(stderr,
154b635f565Sjmmv "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n",
1558a986b2eScgd getprogname());
1563c78ef33Smycroft exit(1);
1573c78ef33Smycroft }
158