/* Hangup the line. */ static int tty_hangup(void) { struct termios tty; tty = tty_current; (void) tty_set_speed(&tty, "0"); if (tty_set_state(&tty) < 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(DROP): %s\n"), strerror(errno)); return(-errno); } (void) sleep(1); if (tty_set_state(&tty_current) < 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_hangup(RAISE): %s\n"), strerror(errno)); return(-errno); } return(0); }
/* Restore the TTY to its previous state. */ static int tty_restore(void) { int ret; struct termios tty; tty = tty_before; (void) tty_set_speed(&tty, "0"); if ((ret = tty_set_state(&tty)) < 0) { fprintf(stderr, "slattach: tty_restore: %s\n", strerror(-ret)); return ret; } return 0; }
/* Open and initialize a terminal line. */ static int tty_open(char *name) { int fd; int ret; int saved_errno; char pathbuf[PATH_MAX]; register char *path_open, *path_lock; /* Try opening the TTY device. */ if (name != NULL) { if (name[0] != '/') { if (strlen(name + 6) > sizeof(pathbuf)) { fprintf(stderr, "tty name too long\n"); return -1; } sprintf(pathbuf, "/dev/%s", name); path_open = pathbuf; path_lock = name; } else if (!strncmp(name, "/dev/", 5)) { path_open = name; path_lock = name + 5; } else { path_open = name; path_lock = name; } fprintf(stderr, "tty_open: looking for lock\n"); if (tty_lock(path_lock, 1)) return -1 ; /* can we lock the device? */ fprintf(stderr, "tty_open: trying to open %s\n", path_open); if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) { saved_errno = errno; fprintf(stderr, "tty_open(%s, RW): %s\n", path_open, strerror(errno)); return -saved_errno; } tty_fd = fd; fprintf(stderr, "tty_open: %s (fd=%d) ", path_open, fd); } else { tty_fd = 0; } /* Fetch the current state of the terminal. */ if (tty_get_state(&tty_before) < 0) { saved_errno = errno; fprintf(stderr, "tty_open: cannot get current state\n"); return -saved_errno; } tty_current = tty_before; /* Fetch the current line discipline of this terminal. */ if (tty_get_ldisc(&ldisc_before) < 0) { saved_errno = errno; fprintf(stderr, "tty_open: cannot get current line disc\n"); return -saved_errno; } /* Put this terminal line in a 8-bit transparent mode. */ if (tty_set_raw(&tty_current) < 0) { saved_errno = errno; fprintf(stderr, "tty_open: cannot set RAW mode\n"); return -saved_errno; } /************************************************** * The sensor needs to be setup at * 57600bps, 8 data bits, No parity, 1 stop bit: ************************************************** */ if (tty_set_speed(&tty_current, "57600") != 0) { saved_errno = errno; fprintf(stderr, "tty_open: cannot set data rate to 57600bps\n"); return -saved_errno; } if (tty_set_databits(&tty_current, "8") || tty_set_stopbits(&tty_current, "1") || tty_set_parity(&tty_current, "N")) { saved_errno = errno; fprintf(stderr, "tty_open: cannot set 8N1 mode\n"); return -saved_errno; }; /* Set the new line mode. */ if ((ret = tty_set_state(&tty_current)) < 0) return ret; /* And activate the new line discipline */ if ((ret = tty_set_ldisc(N_LUNIX_LDISC)) < 0) return ret; return 0; }
/* Open and initialize a terminal line. */ static int tty_open(char *name, const char *speed) { char pathbuf[PATH_MAX]; register char *path_open, *path_lock; int fd; /* Try opening the TTY device. */ if (name != NULL) { if (name[0] != '/') { if (strlen(name + 6) > sizeof(pathbuf)) { if (opt_q == 0) fprintf(stderr, _("slattach: tty name too long\n")); return (-1); } sprintf(pathbuf, "/dev/%s", name); path_open = pathbuf; path_lock = name; } else if (!strncmp(name, "/dev/", 5)) { path_open = name; path_lock = name + 5; } else { path_open = name; path_lock = name; } if (opt_d) printf("slattach: tty_open: looking for lock\n"); if (tty_lock(path_lock, 1)) return(-1); /* can we lock the device? */ if (opt_d) printf("slattach: tty_open: trying to open %s\n", path_open); if ((fd = open(path_open, O_RDWR|O_NDELAY)) < 0) { if (opt_q == 0) fprintf(stderr, "slattach: tty_open(%s, RW): %s\n", path_open, strerror(errno)); return(-errno); } tty_fd = fd; if (opt_d) printf("slattach: tty_open: %s (fd=%d)\n", path_open, fd); if (!strcmp(path_open, _PATH_DEVPTMX)) { if (opt_d) printf("slattach: tty_open: trying to grantpt and unlockpt\n"); if (grantpt(fd) < 0) { if (opt_q == 0) fprintf(stderr, "slattach: tty_open: grantpt: %s\n", strerror(errno)); return(-errno); } if (unlockpt(fd) < 0) { if (opt_q == 0) fprintf(stderr, "slattach: tty_open: unlockpt: %s\n", strerror(errno)); return(-errno); } path_pts = ptsname(fd); if (path_pts == NULL) { if (opt_q == 0) fprintf(stderr, "slattach: tty_open: ptsname: %s\n", strerror(errno)); return(-errno); } if (opt_d) printf("slattach: tty_open: %s: slave pseudo-terminal is %s\n", path_open, path_pts); } } else { tty_fd = 0; } /* Fetch the current state of the terminal. */ if (tty_get_state(&tty_saved) < 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current state!\n")); return(-errno); } tty_current = tty_saved; /* Fetch the current line discipline of this terminal. */ if (tty_get_disc(&tty_sdisc) < 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot get current line disc!\n")); return(-errno); } tty_ldisc = tty_sdisc; /* Put this terminal line in a 8-bit transparent mode. */ if (opt_m == 0) { if (tty_set_raw(&tty_current) < 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set RAW mode!\n")); return(-errno); } /* Set the default speed if we need to. */ if (speed != NULL) { if (tty_set_speed(&tty_current, speed) != 0) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set %s bps!\n"), speed); return(-errno); } } /* Set up a completely 8-bit clean line. */ if (tty_set_databits(&tty_current, "8") || tty_set_stopbits(&tty_current, "1") || tty_set_parity(&tty_current, "N")) { if (opt_q == 0) fprintf(stderr, _("slattach: tty_open: cannot set 8N1 mode!\n")); return(-errno); } /* Set the new line mode. */ if ((fd = tty_set_state(&tty_current)) < 0) return(fd); } /* OK, line is open. Do we need to "silence" it? */ (void) tty_nomesg(tty_fd); return(0); }