Skip to content

Commit 239e5ee

Browse files
committed
[TTY Resizer] open/close TTY each time when need to issue ioctl()
1 parent 68e11ca commit 239e5ee

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

tty-resizer/README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,4 @@ When `0x12` (DC2) is received in tty-resizer, subsequent chars are captured in t
9696

9797
# Known issue
9898

99-
Some strings for resizing might be shown on your serial console in earlier phase (especially just after the boot) like following:
100-
101-
```
102-
[root@raspberry-pi ~]# 30;122t31;122t31;123t31;124t
103-
```
104-
105-
You should wait few seconds if you start tty-resizer from `tty-resizer.service` when you encounter this problem.
106-
107-
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.
108-
109-
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.
99+
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.

tty-resizer/tty-resizer.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023, Yasumasa Suenaga
2+
* Copyright (C) 2023, 2024, Yasumasa Suenaga
33
*
44
* This program is free software; you can redistribute it and/or
55
* modify it under the terms of the GNU General Public License
@@ -27,7 +27,7 @@
2727
#include "common.h"
2828

2929

30-
static int tty_fd = -1;
30+
static const char *ttypath;
3131
static ino_t tty_ino;
3232
static struct tty_resizer_bpf *skel = NULL;
3333
static struct ring_buffer *rb = NULL;
@@ -47,10 +47,16 @@ int on_char_received(void *ctx, void *data, size_t size){
4747
if(ch == 't'){
4848
ringbuf_received[ringbuf_received_idx] = '\0';
4949
if(sscanf(ringbuf_received, "%hu" RESIZER_SEPARATOR "%hu", &ws.ws_row, &ws.ws_col) == 2){
50+
int tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
51+
if(tty_fd == -1){
52+
perror("TTY open");
53+
_exit(-100);
54+
}
5055
if(ioctl(tty_fd, TIOCSWINSZ, &ws) == -1){
5156
perror("ioctl");
5257
_exit(-200);
5358
}
59+
close(tty_fd);
5460
}
5561
ringbuf_received_idx = 0;
5662
}
@@ -64,10 +70,10 @@ int on_char_received(void *ctx, void *data, size_t size){
6470
return 0;
6571
}
6672

67-
static int setup_tty(const char *ttypath){
73+
static int setup_tty(){
6874
struct stat statst;
6975

70-
tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
76+
int tty_fd = open(ttypath, O_RDONLY | O_NOCTTY);
7177
if(tty_fd == -1){
7278
perror("open");
7379
return -1;
@@ -78,6 +84,7 @@ static int setup_tty(const char *ttypath){
7884
return -2;
7985
}
8086
tty_ino = statst.st_ino;
87+
close(tty_fd);
8188

8289
return 0;
8390
}
@@ -110,21 +117,19 @@ static int setup_bpf(){
110117
}
111118

112119
int main(int argc, char *argv[]){
113-
char *devfile;
114-
115120
if((argc == 3) && (strcmp(argv[1], "-v") == 0)){
116121
libbpf_set_print(libbpf_print);
117-
devfile = argv[2];
122+
ttypath = argv[2];
118123
}
119124
else if(argc == 2){
120-
devfile = argv[1];
125+
ttypath = argv[1];
121126
}
122127
else{
123128
printf("Usage: %s <-v> [TTY device file]\n", argv[0]);
124129
return -100;
125130
}
126131

127-
if((setup_tty(devfile) == 0) && (setup_bpf() == 0)){
132+
if((setup_tty() == 0) && (setup_bpf() == 0)){
128133
while(true){
129134
int ret = ring_buffer__poll(rb, -1);
130135
if (ret < 0 && errno != EINTR) {
@@ -143,9 +148,6 @@ int main(int argc, char *argv[]){
143148
if(skel != NULL){
144149
tty_resizer_bpf__destroy(skel);
145150
}
146-
if(tty_fd != -1){
147-
close(tty_fd);
148-
}
149151

150152
return -1;
151153
}

0 commit comments

Comments
 (0)