1 /*- 2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: src/usr.sbin/ppp/tty.c,v 1.21.2.3 2002/09/01 02:12:32 brian Exp $ 27 * $DragonFly: src/usr.sbin/ppp/tty.c,v 1.4 2007/06/04 00:40:32 swildner Exp $ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/un.h> 32 #if defined(__OpenBSD__) || defined(__NetBSD__) 33 #include <sys/ioctl.h> 34 #endif 35 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sysexits.h> 42 #include <sys/uio.h> 43 #include <termios.h> 44 #include <ttyent.h> 45 #include <unistd.h> 46 #ifndef NONETGRAPH 47 #include <netgraph.h> 48 #include <netgraph/async/ng_async.h> 49 #include <netgraph/ng_message.h> 50 #include <netgraph/ppp/ng_ppp.h> 51 #include <netgraph/tty/ng_tty.h> 52 #endif 53 54 #include "layer.h" 55 #include "defs.h" 56 #include "mbuf.h" 57 #include "log.h" 58 #include "timer.h" 59 #include "lqr.h" 60 #include "hdlc.h" 61 #include "throughput.h" 62 #include "fsm.h" 63 #include "lcp.h" 64 #include "ccp.h" 65 #include "link.h" 66 #include "async.h" 67 #include "descriptor.h" 68 #include "physical.h" 69 #include "mp.h" 70 #include "chat.h" 71 #include "auth.h" 72 #include "chap.h" 73 #include "cbcp.h" 74 #include "datalink.h" 75 #include "main.h" 76 #include "id.h" 77 #include "tty.h" 78 79 #if defined(__mac68k__) || defined(__macppc__) 80 #undef CRTS_IFLOW 81 #undef CCTS_OFLOW 82 #define CRTS_IFLOW CDTRCTS 83 #define CCTS_OFLOW CDTRCTS 84 #endif 85 86 #define Online(dev) ((dev)->mbits & TIOCM_CD) 87 88 struct ttydevice { 89 struct device dev; /* What struct physical knows about */ 90 struct pppTimer Timer; /* CD checks */ 91 int mbits; /* Current DCD status */ 92 int carrier_seconds; /* seconds before CD is *required* */ 93 #ifndef NONETGRAPH 94 struct { 95 int speed; /* Pre-line-discipline speed */ 96 int fd; /* Pre-line-discipline fd */ 97 int disc; /* Old line-discipline */ 98 } real; 99 char hook[sizeof NG_ASYNC_HOOK_SYNC]; /* our ng_socket hook */ 100 int cs; /* A netgraph control socket (maybe) */ 101 #endif 102 struct termios ios; /* To be able to reset from raw mode */ 103 }; 104 105 #define device2tty(d) ((d)->type == TTY_DEVICE ? (struct ttydevice *)d : NULL) 106 107 int 108 tty_DeviceSize(void) 109 { 110 return sizeof(struct ttydevice); 111 } 112 113 /* 114 * tty_Timeout() watches the DCD signal and mentions it if it's status 115 * changes. 116 */ 117 static void 118 tty_Timeout(void *data) 119 { 120 struct physical *p = data; 121 struct ttydevice *dev = device2tty(p->handler); 122 int ombits, change; 123 124 timer_Stop(&dev->Timer); 125 dev->Timer.load = SECTICKS; /* Once a second please */ 126 timer_Start(&dev->Timer); 127 ombits = dev->mbits; 128 129 if (p->fd >= 0) { 130 if (ioctl(p->fd, TIOCMGET, &dev->mbits) < 0) { 131 /* we must be a pty ? */ 132 if (p->cfg.cd.necessity != CD_DEFAULT) 133 log_Printf(LogWARN, "%s: Carrier ioctl not supported, " 134 "using ``set cd off''\n", p->link.name); 135 timer_Stop(&dev->Timer); 136 dev->mbits = TIOCM_CD; 137 return; 138 } 139 } else 140 dev->mbits = 0; 141 142 if (ombits == -1) { 143 /* First time looking for carrier */ 144 if (Online(dev)) 145 log_Printf(LogPHASE, "%s: %s: CD detected\n", p->link.name, p->name.full); 146 else if (++dev->carrier_seconds >= dev->dev.cd.delay) { 147 if (dev->dev.cd.necessity == CD_REQUIRED) 148 log_Printf(LogPHASE, "%s: %s: Required CD not detected\n", 149 p->link.name, p->name.full); 150 else { 151 log_Printf(LogPHASE, "%s: %s doesn't support CD\n", 152 p->link.name, p->name.full); 153 dev->mbits = TIOCM_CD; /* Dodgy null-modem cable ? */ 154 } 155 timer_Stop(&dev->Timer); 156 /* tty_AwaitCarrier() will notice */ 157 } else { 158 /* Keep waiting */ 159 log_Printf(LogDEBUG, "%s: %s: Still no carrier (%d/%d)\n", 160 p->link.name, p->name.full, dev->carrier_seconds, 161 dev->dev.cd.delay); 162 dev->mbits = -1; 163 } 164 } else { 165 change = ombits ^ dev->mbits; 166 if (change & TIOCM_CD) { 167 if (dev->mbits & TIOCM_CD) 168 log_Printf(LogDEBUG, "%s: offline -> online\n", p->link.name); 169 else { 170 log_Printf(LogDEBUG, "%s: online -> offline\n", p->link.name); 171 log_Printf(LogPHASE, "%s: Carrier lost\n", p->link.name); 172 datalink_Down(p->dl, CLOSE_NORMAL); 173 timer_Stop(&dev->Timer); 174 } 175 } else 176 log_Printf(LogDEBUG, "%s: Still %sline\n", p->link.name, 177 Online(dev) ? "on" : "off"); 178 } 179 } 180 181 static void 182 tty_StartTimer(struct physical *p) 183 { 184 struct ttydevice *dev = device2tty(p->handler); 185 186 timer_Stop(&dev->Timer); 187 dev->Timer.load = SECTICKS; 188 dev->Timer.func = tty_Timeout; 189 dev->Timer.name = "tty CD"; 190 dev->Timer.arg = p; 191 log_Printf(LogDEBUG, "%s: Using tty_Timeout [%p]\n", 192 p->link.name, tty_Timeout); 193 timer_Start(&dev->Timer); 194 } 195 196 static int 197 tty_AwaitCarrier(struct physical *p) 198 { 199 struct ttydevice *dev = device2tty(p->handler); 200 201 if (dev->dev.cd.necessity == CD_NOTREQUIRED || physical_IsSync(p)) 202 return CARRIER_OK; 203 204 if (dev->mbits == -1) { 205 if (dev->Timer.state == TIMER_STOPPED) { 206 dev->carrier_seconds = 0; 207 tty_StartTimer(p); 208 } 209 return CARRIER_PENDING; /* Not yet ! */ 210 } 211 212 return Online(dev) ? CARRIER_OK : CARRIER_LOST; 213 } 214 215 #ifdef NONETGRAPH 216 #define tty_SetAsyncParams NULL 217 #define tty_Write NULL 218 #define tty_Read NULL 219 #else 220 221 static int 222 isngtty(struct ttydevice *dev) 223 { 224 return dev->real.fd != -1; 225 } 226 227 static void 228 tty_SetAsyncParams(struct physical *p, u_int32_t mymap, u_int32_t hismap) 229 { 230 struct ttydevice *dev = device2tty(p->handler); 231 char asyncpath[NG_PATHSIZ]; 232 struct ng_async_cfg cfg; 233 234 if (isngtty(dev)) { 235 /* Configure the async converter node */ 236 237 snprintf(asyncpath, sizeof asyncpath, ".:%s", dev->hook); 238 memset(&cfg, 0, sizeof cfg); 239 cfg.enabled = 1; 240 cfg.accm = mymap | hismap; 241 cfg.amru = MAX_MTU; 242 cfg.smru = MAX_MRU; 243 log_Printf(LogDEBUG, "Configure async node at %s\n", asyncpath); 244 if (NgSendMsg(dev->cs, asyncpath, NGM_ASYNC_COOKIE, 245 NGM_ASYNC_CMD_SET_CONFIG, &cfg, sizeof cfg) < 0) 246 log_Printf(LogWARN, "%s: Can't configure async node at %s\n", 247 p->link.name, asyncpath); 248 } else 249 /* No netgraph node, just config the async layer */ 250 async_SetLinkParams(&p->async, mymap, hismap); 251 } 252 253 static int 254 LoadLineDiscipline(struct physical *p) 255 { 256 struct ttydevice *dev = device2tty(p->handler); 257 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)]; 258 struct ng_mesg *reply; 259 struct nodeinfo *info; 260 char ttypath[NG_NODESIZ]; 261 struct ngm_mkpeer ngm; 262 struct ngm_connect ngc; 263 int ldisc, cs, ds, hot, speed; 264 265 /* 266 * Don't use the netgraph line discipline for now. Using it works, but 267 * carrier cannot be detected via TIOCMGET and the device doesn't become 268 * selectable with 0 bytes to read when carrier is lost :( 269 */ 270 return 0; 271 272 reply = (struct ng_mesg *)rbuf; 273 info = (struct nodeinfo *)reply->data; 274 275 loadmodules(LOAD_VERBOSLY, "netgraph", "ng_tty", "ng_async", "ng_socket", 276 NULL); 277 278 /* Get the speed before loading the line discipline */ 279 speed = physical_GetSpeed(p); 280 281 if (ioctl(p->fd, TIOCGETD, &dev->real.disc) < 0) { 282 log_Printf(LogDEBUG, "%s: Couldn't get tty line discipline\n", 283 p->link.name); 284 return 0; 285 } 286 ldisc = NETGRAPHDISC; 287 if (ID0ioctl(p->fd, TIOCSETD, &ldisc) < 0) { 288 log_Printf(LogDEBUG, "%s: Couldn't set NETGRAPHDISC line discipline\n", 289 p->link.name); 290 return 0; 291 } 292 293 /* Get the name of the tty node */ 294 if (ioctl(p->fd, NGIOCGINFO, info) < 0) { 295 log_Printf(LogWARN, "%s: ioctl(NGIOCGINFO): %s\n", p->link.name, 296 strerror(errno)); 297 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc); 298 return 0; 299 } 300 snprintf(ttypath, sizeof ttypath, "%s:", info->name); 301 302 /* Create a socket node for our endpoint (and to send messages via) */ 303 if (ID0NgMkSockNode(NULL, &cs, &ds) == -1) { 304 log_Printf(LogWARN, "%s: NgMkSockNode: %s\n", p->link.name, 305 strerror(errno)); 306 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc); 307 return 0; 308 } 309 310 /* Set the ``hot char'' on the TTY node */ 311 hot = HDLC_SYN; 312 log_Printf(LogDEBUG, "%s: Set tty hotchar to 0x%02x\n", p->link.name, hot); 313 if (NgSendMsg(cs, ttypath, NGM_TTY_COOKIE, 314 NGM_TTY_SET_HOTCHAR, &hot, sizeof hot) < 0) { 315 log_Printf(LogWARN, "%s: Can't set hot char\n", p->link.name); 316 goto failed; 317 } 318 319 /* Attach an async converter node */ 320 snprintf(ngm.type, sizeof ngm.type, "%s", NG_ASYNC_NODE_TYPE); 321 snprintf(ngm.ourhook, sizeof ngm.ourhook, "%s", NG_TTY_HOOK); 322 snprintf(ngm.peerhook, sizeof ngm.peerhook, "%s", NG_ASYNC_HOOK_ASYNC); 323 log_Printf(LogDEBUG, "%s: Send mkpeer async:%s to %s:%s\n", p->link.name, 324 ngm.peerhook, ttypath, ngm.ourhook); 325 if (NgSendMsg(cs, ttypath, NGM_GENERIC_COOKIE, 326 NGM_MKPEER, &ngm, sizeof ngm) < 0) { 327 log_Printf(LogWARN, "%s: Can't create %s node\n", p->link.name, 328 NG_ASYNC_NODE_TYPE); 329 goto failed; 330 } 331 332 /* Connect the async node to our socket */ 333 snprintf(ngc.path, sizeof ngc.path, "%s%s", ttypath, NG_TTY_HOOK); 334 snprintf(ngc.peerhook, sizeof ngc.peerhook, "%s", NG_ASYNC_HOOK_SYNC); 335 memcpy(ngc.ourhook, ngc.peerhook, sizeof ngc.ourhook); 336 log_Printf(LogDEBUG, "%s: Send connect %s:%s to .:%s\n", p->link.name, 337 ngc.path, ngc.peerhook, ngc.ourhook); 338 if (NgSendMsg(cs, ".:", NGM_GENERIC_COOKIE, NGM_CONNECT, 339 &ngc, sizeof ngc) < 0) { 340 log_Printf(LogWARN, "%s: Can't connect .:%s -> %s.%s: %s\n", 341 p->link.name, ngc.ourhook, ngc.path, ngc.peerhook, 342 strerror(errno)); 343 goto failed; 344 } 345 346 /* Get the async node id */ 347 if (NgSendMsg(cs, ngc.path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) { 348 log_Printf(LogWARN, "%s: Can't request async node info at %s: %s\n", 349 p->link.name, ngc.path, strerror(errno)); 350 goto failed; 351 } 352 if (NgRecvMsg(cs, reply, sizeof rbuf, NULL) < 0) { 353 log_Printf(LogWARN, "%s: Can't obtain async node info at %s: %s\n", 354 p->link.name, ngc.path, strerror(errno)); 355 goto failed; 356 } 357 358 /* All done, set up our device state */ 359 snprintf(dev->hook, sizeof dev->hook, "%s", ngc.ourhook); 360 dev->cs = cs; 361 dev->real.fd = p->fd; 362 p->fd = ds; 363 dev->real.speed = speed; 364 physical_SetSync(p); 365 366 tty_SetAsyncParams(p, 0xffffffff, 0xffffffff); 367 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 368 log_Printf(LogPHASE, "%s: Loaded netgraph tty line discipline\n", 369 p->link.name); 370 371 return 1; 372 373 failed: 374 ID0ioctl(p->fd, TIOCSETD, &dev->real.disc); 375 close(ds); 376 close(cs); 377 378 return 0; 379 } 380 381 static void 382 UnloadLineDiscipline(struct physical *p) 383 { 384 struct ttydevice *dev = device2tty(p->handler); 385 386 if (isngtty(dev)) { 387 log_Printf(LogPHASE, "back to speed %d\n", dev->real.speed); 388 if (!physical_SetSpeed(p, dev->real.speed)) 389 log_Printf(LogWARN, "Couldn't reset tty speed to %d\n", dev->real.speed); 390 dev->real.speed = 0; 391 close(p->fd); 392 p->fd = dev->real.fd; 393 dev->real.fd = -1; 394 close(dev->cs); 395 dev->cs = -1; 396 *dev->hook = '\0'; 397 if (ID0ioctl(p->fd, TIOCSETD, &dev->real.disc) == 0) { 398 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 399 log_Printf(LogPHASE, "%s: Unloaded netgraph tty line discipline\n", 400 p->link.name); 401 } else 402 log_Printf(LogWARN, "%s: Failed to unload netgraph tty line discipline\n", 403 p->link.name); 404 } 405 } 406 407 static ssize_t 408 tty_Write(struct physical *p, const void *v, size_t n) 409 { 410 struct ttydevice *dev = device2tty(p->handler); 411 412 if (isngtty(dev)) 413 return NgSendData(p->fd, dev->hook, v, n) == -1 ? -1 : n; 414 else 415 return write(p->fd, v, n); 416 } 417 418 static ssize_t 419 tty_Read(struct physical *p, void *v, size_t n) 420 { 421 struct ttydevice *dev = device2tty(p->handler); 422 char hook[sizeof NG_ASYNC_HOOK_SYNC]; 423 424 if (isngtty(dev)) 425 return NgRecvData(p->fd, v, n, hook); 426 else 427 return read(p->fd, v, n); 428 } 429 430 #endif /* NETGRAPH */ 431 432 static int 433 tty_Raw(struct physical *p) 434 { 435 struct ttydevice *dev = device2tty(p->handler); 436 struct termios ios; 437 int oldflag; 438 439 log_Printf(LogDEBUG, "%s: Entering tty_Raw\n", p->link.name); 440 441 if (p->type != PHYS_DIRECT && p->fd >= 0 && !Online(dev)) 442 log_Printf(LogDEBUG, "%s: Raw: descriptor = %d, mbits = %x\n", 443 p->link.name, p->fd, dev->mbits); 444 445 if (!physical_IsSync(p)) { 446 #ifndef NONETGRAPH 447 if (!LoadLineDiscipline(p)) 448 #endif 449 { 450 tcgetattr(p->fd, &ios); 451 cfmakeraw(&ios); 452 if (p->cfg.rts_cts) 453 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; 454 else 455 ios.c_cflag |= CLOCAL; 456 457 if (p->type != PHYS_DEDICATED) 458 ios.c_cflag |= HUPCL; 459 460 if (tcsetattr(p->fd, TCSANOW, &ios) == -1) 461 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n", 462 p->link.name); 463 } 464 } 465 466 oldflag = fcntl(p->fd, F_GETFL, 0); 467 if (oldflag < 0) 468 return 0; 469 fcntl(p->fd, F_SETFL, oldflag | O_NONBLOCK); 470 471 return 1; 472 } 473 474 static void 475 tty_Offline(struct physical *p) 476 { 477 struct ttydevice *dev = device2tty(p->handler); 478 479 if (p->fd >= 0) { 480 timer_Stop(&dev->Timer); 481 dev->mbits &= ~TIOCM_DTR; /* XXX: Hmm, what's this supposed to do ? */ 482 if (Online(dev)) { 483 struct termios tio; 484 485 tcgetattr(p->fd, &tio); 486 if (cfsetspeed(&tio, B0) == -1 || tcsetattr(p->fd, TCSANOW, &tio) == -1) 487 log_Printf(LogWARN, "%s: Unable to set physical to speed 0\n", 488 p->link.name); 489 } 490 } 491 } 492 493 static void 494 tty_Cooked(struct physical *p) 495 { 496 struct ttydevice *dev = device2tty(p->handler); 497 int oldflag; 498 499 tty_Offline(p); /* In case of emergency close()s */ 500 501 tcflush(p->fd, TCIOFLUSH); 502 503 if (!physical_IsSync(p) && tcsetattr(p->fd, TCSAFLUSH, &dev->ios) == -1) 504 log_Printf(LogWARN, "%s: tcsetattr: Unable to restore device settings\n", 505 p->link.name); 506 507 #ifndef NONETGRAPH 508 UnloadLineDiscipline(p); 509 #endif 510 511 if ((oldflag = fcntl(p->fd, F_GETFL, 0)) != -1) 512 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); 513 } 514 515 static void 516 tty_StopTimer(struct physical *p) 517 { 518 struct ttydevice *dev = device2tty(p->handler); 519 520 timer_Stop(&dev->Timer); 521 } 522 523 static void 524 tty_Free(struct physical *p) 525 { 526 struct ttydevice *dev = device2tty(p->handler); 527 528 tty_Offline(p); /* In case of emergency close()s */ 529 free(dev); 530 } 531 532 static int 533 tty_Speed(struct physical *p) 534 { 535 struct termios ios; 536 537 if (tcgetattr(p->fd, &ios) == -1) 538 return 0; 539 540 return SpeedToInt(cfgetispeed(&ios)); 541 } 542 543 static const char * 544 tty_OpenInfo(struct physical *p) 545 { 546 struct ttydevice *dev = device2tty(p->handler); 547 static char buf[13]; 548 549 if (Online(dev)) 550 strcpy(buf, "with"); 551 else 552 strcpy(buf, "no"); 553 strcat(buf, " carrier"); 554 555 return buf; 556 } 557 558 static int 559 tty_Slot(struct physical *p) 560 { 561 struct ttyent *ttyp; 562 int slot; 563 564 setttyent(); 565 for (slot = 1; (ttyp = getttyent()); ++slot) 566 if (!strcmp(ttyp->ty_name, p->name.base)) { 567 endttyent(); 568 return slot; 569 } 570 571 endttyent(); 572 return -1; 573 } 574 575 static void 576 tty_device2iov(struct device *d, struct iovec *iov, int *niov, 577 int maxiov, int *auxfd, int *nauxfd) 578 { 579 struct ttydevice *dev = device2tty(d); 580 int sz = physical_MaxDeviceSize(); 581 582 iov[*niov].iov_base = realloc(d, sz); 583 if (iov[*niov].iov_base == NULL) { 584 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz); 585 AbortProgram(EX_OSERR); 586 } 587 iov[*niov].iov_len = sz; 588 (*niov)++; 589 590 #ifndef NONETGRAPH 591 if (dev->cs >= 0) { 592 *auxfd = dev->cs; 593 (*nauxfd)++; 594 } 595 #endif 596 597 if (dev->Timer.state != TIMER_STOPPED) { 598 timer_Stop(&dev->Timer); 599 dev->Timer.state = TIMER_RUNNING; 600 } 601 } 602 603 static struct device basettydevice = { 604 TTY_DEVICE, 605 "tty", 606 0, 607 { CD_VARIABLE, DEF_TTYCDDELAY }, 608 tty_AwaitCarrier, 609 NULL, 610 tty_Raw, 611 tty_Offline, 612 tty_Cooked, 613 tty_SetAsyncParams, 614 tty_StopTimer, 615 tty_Free, 616 tty_Read, 617 tty_Write, 618 tty_device2iov, 619 tty_Speed, 620 tty_OpenInfo, 621 tty_Slot 622 }; 623 624 struct device * 625 tty_iov2device(int type, struct physical *p, struct iovec *iov, int *niov, 626 int maxiov, int *auxfd, int *nauxfd) 627 { 628 if (type == TTY_DEVICE) { 629 struct ttydevice *dev = (struct ttydevice *)iov[(*niov)++].iov_base; 630 631 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */ 632 if (dev == NULL) { 633 log_Printf(LogALERT, "Failed to allocate memory: %d\n", 634 (int)(sizeof *dev)); 635 AbortProgram(EX_OSERR); 636 } 637 638 #ifndef NONETGRAPH 639 if (*nauxfd) { 640 dev->cs = *auxfd; 641 (*nauxfd)--; 642 } else 643 dev->cs = -1; 644 #endif 645 646 /* Refresh function pointers etc */ 647 memcpy(&dev->dev, &basettydevice, sizeof dev->dev); 648 649 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 650 if (dev->Timer.state != TIMER_STOPPED) { 651 dev->Timer.state = TIMER_STOPPED; 652 p->handler = &dev->dev; /* For the benefit of StartTimer */ 653 tty_StartTimer(p); 654 } 655 return &dev->dev; 656 } 657 658 return NULL; 659 } 660 661 struct device * 662 tty_Create(struct physical *p) 663 { 664 struct ttydevice *dev; 665 struct termios ios; 666 int oldflag; 667 668 if (p->fd < 0 || !isatty(p->fd)) 669 /* Don't want this */ 670 return NULL; 671 672 if (*p->name.full == '\0') { 673 physical_SetDevice(p, ttyname(p->fd)); 674 log_Printf(LogDEBUG, "%s: Input is a tty (%s)\n", 675 p->link.name, p->name.full); 676 } else 677 log_Printf(LogDEBUG, "%s: Opened %s\n", p->link.name, p->name.full); 678 679 /* We're gonna return a ttydevice (unless something goes horribly wrong) */ 680 681 if ((dev = malloc(sizeof *dev)) == NULL) { 682 /* Complete failure - parent doesn't continue trying to ``create'' */ 683 close(p->fd); 684 p->fd = -1; 685 return NULL; 686 } 687 688 memcpy(&dev->dev, &basettydevice, sizeof dev->dev); 689 memset(&dev->Timer, '\0', sizeof dev->Timer); 690 dev->mbits = -1; 691 #ifndef NONETGRAPH 692 dev->real.speed = 0; 693 dev->real.fd = -1; 694 dev->real.disc = -1; 695 *dev->hook = '\0'; 696 #endif 697 tcgetattr(p->fd, &ios); 698 dev->ios = ios; 699 700 if (p->cfg.cd.necessity != CD_DEFAULT) 701 /* Any override is ok for the tty device */ 702 dev->dev.cd = p->cfg.cd; 703 704 log_Printf(LogDEBUG, "%s: tty_Create: physical (get): fd = %d," 705 " iflag = %lx, oflag = %lx, cflag = %lx\n", p->link.name, p->fd, 706 (u_long)ios.c_iflag, (u_long)ios.c_oflag, (u_long)ios.c_cflag); 707 708 cfmakeraw(&ios); 709 if (p->cfg.rts_cts) 710 ios.c_cflag |= CLOCAL | CCTS_OFLOW | CRTS_IFLOW; 711 else { 712 ios.c_cflag |= CLOCAL; 713 ios.c_iflag |= IXOFF; 714 } 715 ios.c_iflag |= IXON; 716 if (p->type != PHYS_DEDICATED) 717 ios.c_cflag |= HUPCL; 718 719 if (p->type != PHYS_DIRECT) { 720 /* Change tty speed when we're not in -direct mode */ 721 ios.c_cflag &= ~(CSIZE | PARODD | PARENB); 722 ios.c_cflag |= p->cfg.parity; 723 if (cfsetspeed(&ios, IntToSpeed(p->cfg.speed)) == -1) 724 log_Printf(LogWARN, "%s: %s: Unable to set speed to %d\n", 725 p->link.name, p->name.full, p->cfg.speed); 726 } 727 728 if (tcsetattr(p->fd, TCSADRAIN, &ios) == -1) { 729 log_Printf(LogWARN, "%s: tcsetattr: Failed configuring device\n", 730 p->link.name); 731 if (p->type != PHYS_DIRECT && p->cfg.speed > 115200) 732 log_Printf(LogWARN, "%.*s Perhaps the speed is unsupported\n", 733 (int)strlen(p->link.name), ""); 734 } 735 736 log_Printf(LogDEBUG, "%s: physical (put): iflag = %lx, oflag = %lx, " 737 "cflag = %lx\n", p->link.name, (u_long)ios.c_iflag, 738 (u_long)ios.c_oflag, (u_long)ios.c_cflag); 739 740 oldflag = fcntl(p->fd, F_GETFL, 0); 741 if (oldflag < 0) { 742 /* Complete failure - parent doesn't continue trying to ``create'' */ 743 744 log_Printf(LogWARN, "%s: Open: Cannot get physical flags: %s\n", 745 p->link.name, strerror(errno)); 746 tty_Cooked(p); 747 close(p->fd); 748 p->fd = -1; 749 free(dev); 750 return NULL; 751 } else 752 fcntl(p->fd, F_SETFL, oldflag & ~O_NONBLOCK); 753 754 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE); 755 756 return &dev->dev; 757 } 758