1 /* $NetBSD: slattach.c,v 1.32 2011/12/30 03:19:36 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. 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.32 2011/12/30 03:19:36 christos 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 int slipdisc = SLIPDISC; 71 72 static char devicename[32]; 73 74 static int ttydisc(char *); 75 __dead static void usage(void); 76 77 int 78 main(int argc, char *argv[]) 79 { 80 int fd; 81 char *dev = argv[1]; 82 struct termios tty; 83 tcflag_t cflag = HUPCL; 84 int ch; 85 sigset_t nsigset; 86 int opt_detach = 1; 87 88 while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) { 89 switch (ch) { 90 case 'h': 91 cflag |= CRTSCTS; 92 break; 93 case 'H': 94 cflag |= CDTRCTS; 95 break; 96 case 'l': 97 cflag |= CLOCAL; 98 break; 99 case 'm': 100 cflag &= ~HUPCL; 101 break; 102 case 'n': 103 opt_detach = 0; 104 break; 105 case 's': 106 speed = atoi(optarg); 107 break; 108 case 'r': case 't': 109 slipdisc = ttydisc(optarg); 110 break; 111 case '?': 112 default: 113 usage(); 114 } 115 } 116 argc -= optind; 117 argv += optind; 118 119 if (argc != 1) 120 usage(); 121 122 dev = *argv; 123 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 124 (void)snprintf(devicename, sizeof(devicename), 125 "%s%s", _PATH_DEV, dev); 126 dev = devicename; 127 } 128 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) 129 err(1, "%s", dev); 130 tty.c_cflag = CREAD | CS8 | cflag; 131 tty.c_iflag = 0; 132 tty.c_lflag = 0; 133 tty.c_oflag = 0; 134 tty.c_cc[VMIN] = 1; 135 tty.c_cc[VTIME] = 0; 136 cfsetspeed(&tty, speed); 137 if (tcsetattr(fd, TCSADRAIN, &tty) < 0) 138 err(1, "tcsetattr"); 139 if (ioctl(fd, TIOCSDTR, 0) < 0 && errno != ENOTTY) 140 err(1, "TIOCSDTR"); 141 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) 142 err(1, "TIOCSETD"); 143 if (opt_detach && daemon(0, 0) != 0) 144 err(1, "couldn't detach"); 145 sigemptyset(&nsigset); 146 for (;;) 147 sigsuspend(&nsigset); 148 } 149 150 static int 151 ttydisc(char *name) 152 { 153 if (strcmp(name, "slip") == 0) 154 return(SLIPDISC); 155 #ifdef STRIPDISC 156 else if (strcmp(name, "strip") == 0) 157 return(STRIPDISC); 158 #endif 159 else 160 usage(); 161 /* NOTREACHED */ 162 return -1; 163 } 164 165 static void 166 usage(void) 167 { 168 169 (void)fprintf(stderr, 170 "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n", 171 getprogname()); 172 exit(1); 173 } 174