slattach.c revision 1.28
1/* $NetBSD: slattach.c,v 1.28 2004/01/05 23:23:33 jmmv 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\n\ 38 The Regents of the University of California. All rights reserved.\n"); 39#endif /* not lint */ 40 41#ifndef lint 42#if 0 43static char sccsid[] = "@(#)slattach.c 8.2 (Berkeley) 1/7/94"; 44#else 45__RCSID("$NetBSD: slattach.c,v 1.28 2004/01/05 23:23:33 jmmv 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 <fcntl.h> 59#include <netdb.h> 60#include <paths.h> 61#include <signal.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <termios.h> 66#include <unistd.h> 67 68int speed = 9600; 69int slipdisc = SLIPDISC; 70 71char devicename[32]; 72 73int main __P((int, char *[])); 74int ttydisc __P((char *)); 75void usage __P((void)); 76 77int 78main(argc, argv) 79 int argc; 80 char *argv[]; 81{ 82 int fd; 83 char *dev = argv[1]; 84 struct termios tty; 85 tcflag_t cflag = HUPCL; 86 int ch; 87 sigset_t nsigset; 88 int opt_detach = 1; 89 90 while ((ch = getopt(argc, argv, "hHlmns:t:")) != -1) { 91 switch (ch) { 92 case 'h': 93 cflag |= CRTSCTS; 94 break; 95 case 'H': 96 cflag |= CDTRCTS; 97 break; 98 case 'l': 99 cflag |= CLOCAL; 100 break; 101 case 'm': 102 cflag &= ~HUPCL; 103 break; 104 case 'n': 105 opt_detach = 0; 106 break; 107 case 's': 108 speed = atoi(optarg); 109 break; 110 case 'r': case 't': 111 slipdisc = ttydisc(optarg); 112 break; 113 case '?': 114 default: 115 usage(); 116 } 117 } 118 argc -= optind; 119 argv += optind; 120 121 if (argc != 1) 122 usage(); 123 124 dev = *argv; 125 if (strncmp(_PATH_DEV, dev, sizeof(_PATH_DEV) - 1)) { 126 (void)snprintf(devicename, sizeof(devicename), 127 "%s%s", _PATH_DEV, dev); 128 dev = devicename; 129 } 130 if ((fd = open(dev, O_RDWR | O_NDELAY)) < 0) 131 err(1, "%s", dev); 132 tty.c_cflag = CREAD | CS8 | cflag; 133 tty.c_iflag = 0; 134 tty.c_lflag = 0; 135 tty.c_oflag = 0; 136 tty.c_cc[VMIN] = 1; 137 tty.c_cc[VTIME] = 0; 138 cfsetspeed(&tty, speed); 139 if (tcsetattr(fd, TCSADRAIN, &tty) < 0) 140 err(1, "tcsetattr"); 141 if (ioctl(fd, TIOCSDTR, 0) < 0) 142 err(1, "TIOCSDTR"); 143 if (ioctl(fd, TIOCSETD, &slipdisc) < 0) 144 err(1, "TIOCSETD"); 145 if (opt_detach && daemon(0, 0) != 0) 146 err(1, "couldn't detach"); 147 sigemptyset(&nsigset); 148 for (;;) 149 sigsuspend(&nsigset); 150} 151 152int 153ttydisc(name) 154 char *name; 155{ 156 if (strcmp(name, "slip") == 0) 157 return(SLIPDISC); 158#ifdef STRIPDISC 159 else if (strcmp(name, "strip") == 0) 160 return(STRIPDISC); 161#endif 162 else 163 usage(); 164 /* NOTREACHED */ 165 return -1; 166} 167 168void 169usage() 170{ 171 172 (void)fprintf(stderr, 173 "usage: %s [-t ldisc] [-hHlmn] [-s baudrate] ttyname\n", 174 getprogname()); 175 exit(1); 176} 177