X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Fmicrocom.c;h=5e29a1acdd483ba51e4382807c24685f8aa1b577;hb=45dc96c8a6dce3aeea983f6c32572d4cee646b5d;hp=2857a1b1b1c2598eeafc4e8d58d7609f4ff4e75b;hpb=ffae845cfd0a0b9872827d806984841d4cfee104;p=oweals%2Fbusybox.git diff --git a/miscutils/microcom.c b/miscutils/microcom.c index 2857a1b1b..5e29a1acd 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c @@ -5,21 +5,24 @@ * * Copyright (C) 2008 by Vladimir Dronnikov * - * Licensed under GPLv2, see file LICENSE in this tarball for details. + * Licensed under GPLv2, see file LICENSE in this source tree. */ -#include "libbb.h" -/* All known arches use small ints for signals */ -static volatile smallint signalled; +//usage:#define microcom_trivial_usage +//usage: "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY" +//usage:#define microcom_full_usage "\n\n" +//usage: "Copy bytes for stdin to TTY and from TTY to stdout\n" +//usage: "\n -d Wait up to DELAY ms for TTY output before sending every" +//usage: "\n next byte to it" +//usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms" +//usage: "\n -s Set serial line to SPEED" +//usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin" -static void signal_handler(int signo) -{ - signalled = signo; -} +#include "libbb.h" // set raw tty mode static void xget1(int fd, struct termios *t, struct termios *oldt) -{ +{ tcgetattr(fd, oldt); *t = *oldt; cfmakeraw(t); @@ -41,7 +44,7 @@ static int xset1(int fd, struct termios *tio, const char *device) } int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; -int microcom_main(int argc, char **argv) +int microcom_main(int argc UNUSED_PARAM, char **argv) { int sfd; int nfd; @@ -51,18 +54,17 @@ int microcom_main(int argc, char **argv) enum { OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ OPT_s = 1 << 1, // baudrate - OPT_d = 1 << 2, // wait for device response, msecs + OPT_d = 1 << 2, // wait for device response, ms OPT_t = 1 << 3, // timeout, ms }; speed_t speed = 9600; int delay = -1; int timeout = -1; + unsigned opts; // fetch options - unsigned opts; - opt_complementary = "=1:s+:d+:t+"; // exactly one arg should be there + opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout); - // argc -= optind; argv += optind; @@ -71,19 +73,18 @@ int microcom_main(int argc, char **argv) device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file); sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); if (sfd < 0) { + // device already locked -> bail out if (errno == EEXIST) - bb_perror_msg_and_die("can't create %s", device_lock_file); + bb_perror_msg_and_die("can't create '%s'", device_lock_file); + // can't create lock -> don't care if (ENABLE_FEATURE_CLEAN_UP) free(device_lock_file); device_lock_file = NULL; - } - if (sfd > 0) { - // %4d to make mgetty happy. It treats 4-bytes lock files as binary, + } else { + // %4d to make concurrent mgetty (if any) happy. + // Mgetty treats 4-bytes lock files as binary, // not text, PID. Making 5+ char file. Brrr... - char *s = xasprintf("%4d\n", getpid()); - write(sfd, s, strlen(s)); - if (ENABLE_FEATURE_CLEAN_UP) - free(s); + fdprintf(sfd, "%4d\n", getpid()); close(sfd); } @@ -93,32 +94,31 @@ int microcom_main(int argc, char **argv) + (1 << SIGINT) + (1 << SIGTERM) + (1 << SIGPIPE) - , signal_handler); + , record_signo); // error exit code if we fail to open the device - signalled = 1; - - // put stdin to "raw mode" (if stdin is a TTY), - // handle one character at a time - if (isatty(STDIN_FILENO)) { - xget1(STDIN_FILENO, &tio, &tio0); - if (xset1(STDIN_FILENO, &tio, "stdin")) - goto done; - } + bb_got_signal = 1; // open device sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); if (sfd < 0) goto done; - fcntl(sfd, F_SETFL, 0); + fcntl(sfd, F_SETFL, O_RDWR); // put device to "raw mode" xget1(sfd, &tio, &tiosfd); -// tio.c_cflag |= (CREAD|HUPCL); // we just bail out on any device error // set device speed cfsetspeed(&tio, tty_value_to_baud(speed)); if (xset1(sfd, &tio, argv[0])) - goto restore0_and_done; + goto done; + + // put stdin to "raw mode" (if stdin is a TTY), + // handle one character at a time + if (isatty(STDIN_FILENO)) { + xget1(STDIN_FILENO, &tio, &tio0); + if (xset1(STDIN_FILENO, &tio, "stdin")) + goto done; + } // main loop: check with poll(), then read/write bytes across pfd[0].fd = sfd; @@ -126,9 +126,10 @@ int microcom_main(int argc, char **argv) pfd[1].fd = STDIN_FILENO; pfd[1].events = POLLIN; - signalled = 0; + bb_got_signal = 0; nfd = 2; - while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { + // Not safe_poll: we want to exit on signal + while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) { if (nfd > 1 && pfd[1].revents) { char c; // read from stdin -> write to device @@ -162,7 +163,7 @@ skip_write: ; full_write(STDOUT_FILENO, iobuf, len); else { // EOF/error -> bail out - signalled = SIGHUP; + bb_got_signal = SIGHUP; break; } } @@ -171,7 +172,6 @@ skip_write: ; // restore device mode tcsetattr(sfd, TCSAFLUSH, &tiosfd); -restore0_and_done: if (isatty(STDIN_FILENO)) tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); @@ -179,5 +179,5 @@ done: if (device_lock_file) unlink(device_lock_file); - return signalled; + return bb_got_signal; }