Skip to content

Commit

Permalink
[TTY Resizer] open/close TTY each time when need to issue ioctl()
Browse files Browse the repository at this point in the history
  • Loading branch information
YaSuenag committed Jul 15, 2024
1 parent 68e11ca commit 239e5ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
12 changes: 1 addition & 11 deletions tty-resizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,4 @@ When `0x12` (DC2) is received in tty-resizer, subsequent chars are captured in t

# Known issue

Some strings for resizing might be shown on your serial console in earlier phase (especially just after the boot) like following:

```
[root@raspberry-pi ~]# 30;122t31;122t31;123t31;124t
```

You should wait few seconds if you start tty-resizer from `tty-resizer.service` when you encounter this problem.

This might be caused that `ioctl` to TTY is failed. So `tty-resizer.service` in this source would restart when the issue happens to avoid it. Then it will work fine.

And also TTY Resizer wouldn't work file with full-screen application like Vim. Noisy chars (for Vim) might be input, and TTY Resizer cannot set resized console size.
TTY Resizer might not work fine with full-screen application like Vim. Noisy chars (for Vim) might be input, and TTY Resizer might not be able to set resized console size while that app is running.
26 changes: 14 additions & 12 deletions tty-resizer/tty-resizer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023, Yasumasa Suenaga
* Copyright (C) 2023, 2024, Yasumasa Suenaga
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -27,7 +27,7 @@
#include "common.h"


static int tty_fd = -1;
static const char *ttypath;
static ino_t tty_ino;
static struct tty_resizer_bpf *skel = NULL;
static struct ring_buffer *rb = NULL;
Expand All @@ -47,10 +47,16 @@ int on_char_received(void *ctx, void *data, size_t size){
if(ch == 't'){
ringbuf_received[ringbuf_received_idx] = '\0';
if(sscanf(ringbuf_received, "%hu" RESIZER_SEPARATOR "%hu", &ws.ws_row, &ws.ws_col) == 2){
int tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
if(tty_fd == -1){
perror("TTY open");
_exit(-100);
}
if(ioctl(tty_fd, TIOCSWINSZ, &ws) == -1){
perror("ioctl");
_exit(-200);
}
close(tty_fd);
}
ringbuf_received_idx = 0;
}
Expand All @@ -64,10 +70,10 @@ int on_char_received(void *ctx, void *data, size_t size){
return 0;
}

static int setup_tty(const char *ttypath){
static int setup_tty(){
struct stat statst;

tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
int tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
if(tty_fd == -1){
perror("open");
return -1;
Expand All @@ -78,6 +84,7 @@ static int setup_tty(const char *ttypath){
return -2;
}
tty_ino = statst.st_ino;
close(tty_fd);

return 0;
}
Expand Down Expand Up @@ -110,21 +117,19 @@ static int setup_bpf(){
}

int main(int argc, char *argv[]){
char *devfile;

if((argc == 3) && (strcmp(argv[1], "-v") == 0)){
libbpf_set_print(libbpf_print);
devfile = argv[2];
ttypath = argv[2];
}
else if(argc == 2){
devfile = argv[1];
ttypath = argv[1];
}
else{
printf("Usage: %s <-v> [TTY device file]\n", argv[0]);
return -100;
}

if((setup_tty(devfile) == 0) && (setup_bpf() == 0)){
if((setup_tty() == 0) && (setup_bpf() == 0)){
while(true){
int ret = ring_buffer__poll(rb, -1);
if (ret < 0 && errno != EINTR) {
Expand All @@ -143,9 +148,6 @@ int main(int argc, char *argv[]){
if(skel != NULL){
tty_resizer_bpf__destroy(skel);
}
if(tty_fd != -1){
close(tty_fd);
}

return -1;
}

0 comments on commit 239e5ee

Please sign in to comment.