diff --git a/ttynvt.c b/ttynvt.c index 0eaf66c..a36480a 100644 --- a/ttynvt.c +++ b/ttynvt.c @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -18,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -32,15 +30,13 @@ #define RX_BUF_SIZE 1024 -static int lfd; - static struct ttynvt_param { unsigned int major; unsigned int minor; char *dev_name; - char *logfile; char *host; unsigned int port; + unsigned int debug; } ttynvt_param; typedef struct ttynvt { @@ -69,22 +65,15 @@ typedef struct ttynvt { tn_ctx_t *tn; } ttynvt_t; -void ttynvt_log(const char *fmt, ...) +static void ttynvt_log(int prio, const char *fmt, ...) { - struct tm *ts; - time_t t; - char s[32]; va_list arg; - if (lfd < 0) + if (prio >= LOG_DEBUG && ttynvt_param.debug <= 0) return; - t = time(NULL); - ts = gmtime(&t); - strftime(s, 32, "%b %a %T", ts); - dprintf(lfd, "%s ", s); va_start(arg, fmt); - vdprintf(lfd, fmt, arg); + vsyslog(prio, fmt, arg); va_end(arg); } @@ -92,7 +81,7 @@ static void ttynvt_release(fuse_req_t req, struct fuse_file_info *info) { ttynvt_t *tty = (ttynvt_t *) (uintptr_t) info->fh; - ttynvt_log("connection closed\n"); + ttynvt_log(LOG_INFO, "connection closed\n"); close(tty->fds[0].fd); @@ -193,7 +182,7 @@ static void *ttynvt_read_net(void *arg) /*buffer overflow */ tty->blen = 0; read_avail = RX_BUF_SIZE; - ttynvt_log("WARNING: rx buffer overflow\n"); + ttynvt_log(LOG_WARNING, "Rx buffer overflow\n"); } res = read(tty->fds[0].fd, tty->buf + tty->blen, read_avail); if (res <= 0) @@ -217,9 +206,9 @@ static void *ttynvt_read_net(void *arg) } if (res == 0) - ttynvt_log("connection closed by remote host\n"); + ttynvt_log(LOG_INFO, "Connection closed by remote host\n"); else - ttynvt_log("RX read error:%s\n", strerror(errno)); + ttynvt_log(LOG_ERR, "Rx read error:%s\n", strerror(errno)); tty->error = 1; ttynvt_notify(tty); @@ -237,13 +226,13 @@ static int ttynvt_connect(const char *host, unsigned int port) if (sockfd < 0) { - ttynvt_log("ERROR: opening socket: %s\n", strerror(errno)); + ttynvt_log(LOG_ERR, "Socket open failed: %s\n", strerror(errno)); return sockfd; } server = gethostbyname(host); if (server == NULL) { - ttynvt_log("ERROR: can't resolve host name: %s\n", strerror(errno)); + ttynvt_log(LOG_ERR, "Cannot resolve host name: %s\n", strerror(errno)); return -1; } bzero((char *)&serveraddr, sizeof(serveraddr)); @@ -255,7 +244,7 @@ static int ttynvt_connect(const char *host, unsigned int port) if (connect (sockfd, (const struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) { - ttynvt_log("ERROR: failed to connect to: %s:%u:%s\n", + ttynvt_log(LOG_ERR, "Failed to connect to: %s:%u:%s\n", host, port, strerror(errno)); return -1; } @@ -304,7 +293,7 @@ void modem_status_cb(void *cctx, int status) tty->mcr &= ~(TIOCM_CD | TIOCM_RI | TIOCM_CTS | TIOCM_DSR); - ttynvt_log("modem status: 0x%02x\n", status); + ttynvt_log(LOG_INFO, "Modem status: 0x%02x\n", status); if (status & TNS_STATE_CD) tty->mcr |= TIOCM_CD; @@ -385,7 +374,7 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info) info->nonseekable = 1; info->direct_io = 1; - ttynvt_log("open connection to server: %s:%d\n", + ttynvt_log(LOG_INFO, "Open connection to server: %s:%d\n", ttynvt_param.host, ttynvt_param.port); fuse_reply_open(req, info); @@ -401,7 +390,7 @@ ttynvt_read(fuse_req_t req, size_t size, off_t off, { if (tty->epipe == 0) { - ttynvt_log("RX EPIPE\n"); + ttynvt_log(LOG_WARNING, "Rx EPIPE\n"); tty->epipe = 1; fuse_reply_buf(req, tty->buf, 0); } @@ -566,7 +555,8 @@ ttynvt_ioctl(fuse_req_t req, int cmd, void *arg, } ttynvt_log - ("CFG ioctl: %u%c%u%u %cCTSRTS %cCREAD %cHUPCL %cCLOCAL\n", + (LOG_DEBUG, + "CFG ioctl: %u%c%u%u %cCTSRTS %cCREAD %cHUPCL %cCLOCAL\n", baudrate(speed), pstr[par], csize, sb, tio.c_cflag & CRTSCTS ? '+' : '-', tio.c_cflag & CREAD ? '+' : '-', @@ -717,7 +707,7 @@ ttynvt_ioctl(fuse_req_t req, int cmd, void *arg, tiocm = *(unsigned int *)in_buf; fuse_reply_ioctl(req, 0, 0, 0); - ttynvt_log("CFG ioctl: DTR=%c RTS=%c\n", + ttynvt_log(LOG_DEBUG, "CFG ioctl: DTR=%c RTS=%c\n", tiocm & TIOCM_DTR ? '1' : '0', tiocm & TIOCM_RTS ? '1' : '0'); @@ -786,6 +776,7 @@ static const struct cuse_lowlevel_ops ttynvt_op = { #define TTYNET_OPT(t, p, v) { t, offsetof(struct ttynvt_param, p), v } static const struct fuse_opt ttynvt_opts[] = { + TTYNET_OPT("-D", debug, 0), TTYNET_OPT("-M %u", major, 1), TTYNET_OPT("--maj=%u", major, 1), TTYNET_OPT("-m %u", minor, 1), @@ -796,8 +787,6 @@ static const struct fuse_opt ttynvt_opts[] = { TTYNET_OPT("--server=%s", host, 0), TTYNET_OPT("-p %u", port, 0), TTYNET_OPT("--port=%u", port, 0), - TTYNET_OPT("-l %s", logfile, 0), - TTYNET_OPT("--log=%s", logfile, 0), FUSE_OPT_KEY("-h", 0), FUSE_OPT_KEY("--help", 0), FUSE_OPT_END @@ -806,11 +795,11 @@ static const struct fuse_opt ttynvt_opts[] = { static void print_help(void) { fprintf(stderr, "ttynvt Usage:\n" + "\t-D\tEnable debug\n" "\t-M major, --maj=major\n" "\t-m minor, --min=minor\n" "\t-n name, --name=name (default: ttyNVT0)\n" "\t\tDevice will be created in /dev/$name.\n" - "\t-l logfile, --log=file name\n" "\t-s server, --server=host name or IP address\n" "\t-p port, --port=host port number\n" "\n"); } @@ -835,6 +824,8 @@ int main(int argc, char *argv[]) char dev_name[128] = { 0 }; const char *dev_info_argv[] = { dev_name }; + openlog("ttynvt", LOG_PID, LOG_DAEMON); + if (fuse_opt_parse(&args, &ttynvt_param, ttynvt_opts, process_arg)) { fprintf(stderr, "Failed to parse arguments\n"); @@ -844,22 +835,6 @@ int main(int argc, char *argv[]) snprintf(dev_name, sizeof(dev_name), "DEVNAME=%s", ttynvt_param.dev_name ? ttynvt_param.dev_name : "ttyNVT0"); - if (ttynvt_param.logfile) - { - lfd = - open(ttynvt_param.logfile, O_CREAT | O_RDWR | O_APPEND, - S_IWUSR | S_IRUSR); - if (lfd < 0) - { - perror("Failed to open log file"); - return 1; - } - ttynvt_log("ttynvt ver:%s started, dev:%s\n", - PACKAGE_VERSION, ttynvt_param.dev_name); - } - else - lfd = -1; - struct cuse_info ci = { .dev_major = ttynvt_param.major, .dev_minor = ttynvt_param.minor,