1178ea7a2da01eb29e6b2373a50445197cdaa1ec
[pve-common.git] / src / PVE / PTY.pm
1 package PVE::PTY;
2
3 use strict;
4 use warnings;
5
6 use Fcntl;
7 use POSIX qw(O_RDWR O_NOCTTY);
8
9 # Constants
10
11 use constant {
12 TCGETS => 0x5401, # fixed, from asm-generic/ioctls.h
13 TCSETS => 0x5402, # fixed, from asm-generic/ioctls.h
14 TIOCGWINSZ => 0x5413, # fixed, from asm-generic/ioctls.h
15 TIOCSWINSZ => 0x5414, # fixed, from asm-generic/ioctls.h
16 TIOCSCTTY => 0x540E, # fixed, from asm-generic/ioctls.h
17 TIOCNOTTY => 0x5422, # fixed, from asm-generic/ioctls.h
18 TIOCGPGRP => 0x540F, # fixed, from asm-generic/ioctls.h
19 TIOCSPGRP => 0x5410, # fixed, from asm-generic/ioctls.h
20
21 # IOC: dir:2 size:14 type:8 nr:8
22 # Get pty number: dir=2 size=4 type='T' nr=0x30
23 TIOCGPTN => 0x80045430,
24
25 # Set pty lock: dir=1 size=4 type='T' nr=0x31
26 TIOCSPTLCK => 0x40045431,
27
28 # Send signal: dir=1 size=4 type='T' nr=0x36
29 TIOCSIG => 0x40045436,
30
31 # c_cc indices:
32 VINTR => 0,
33 VQUIT => 1,
34 VERASE => 2,
35 VKILL => 3,
36 VEOF => 4,
37 VTIME => 5,
38 VMIN => 6,
39 VSWTC => 7,
40 VSTART => 8,
41 VSTOP => 9,
42 VSUSP => 10,
43 VEOL => 11,
44 VREPRINT => 12,
45 VDISCARD => 13,
46 VWERASE => 14,
47 VLNEXT => 15,
48 VEOL2 => 16,
49 };
50
51 # Utility functions
52
53 sub createpty() {
54 # Open the master file descriptor:
55 sysopen(my $master, '/dev/ptmx', O_RDWR | O_NOCTTY)
56 or die "failed to create pty: $!\n";
57
58 # Find the tty number
59 my $ttynum = pack('L', 0);
60 ioctl($master, TIOCGPTN, $ttynum)
61 or die "failed to query pty number: $!\n";
62 $ttynum = unpack('L', $ttynum);
63
64 # Get the slave name/path
65 my $ttyname = "/dev/pts/$ttynum";
66
67 # Unlock
68 my $false = pack('L', 0);
69 ioctl($master, TIOCSPTLCK, $false)
70 or die "failed to unlock pty: $!\n";
71
72 return ($master, $ttyname);
73 }
74
75 my $openslave = sub {
76 my ($ttyname) = @_;
77
78 # Create a slave file descriptor:
79 sysopen(my $slave, $ttyname, O_RDWR | O_NOCTTY)
80 or die "failed to open slave pty handle: $!\n";
81 return $slave;
82 };
83
84 sub lose_controlling_terminal() {
85 # Can we open our current terminal?
86 if (sysopen(my $ttyfd, '/dev/tty', O_RDWR)) {
87 # Disconnect:
88 ioctl($ttyfd, TIOCNOTTY, 0)
89 or die "failed to disconnect controlling tty: $!\n";
90 close($ttyfd);
91 }
92 }
93
94 sub termios(%) {
95 my (%termios) = @_;
96 my $cc = $termios{cc} // [];
97 if (@$cc < 19) {
98 push @$cc, (0) x (19-@$cc);
99 } elsif (@$cc > 19) {
100 @$cc = $$cc[0..18];
101 }
102
103 return pack('LLLLCC[19]',
104 $termios{iflag} || 0,
105 $termios{oflag} || 0,
106 $termios{cflag} || 0,
107 $termios{lflag} || 0,
108 $termios{line} || 0,
109 @$cc);
110 }
111
112 my $parse_termios = sub {
113 my ($blob) = @_;
114 my ($iflag, $oflag, $cflag, $lflag, $line, @cc) =
115 unpack('LLLLCC[19]', $blob);
116 return {
117 iflag => $iflag,
118 oflag => $oflag,
119 cflag => $cflag,
120 lflag => $lflag,
121 line => $line,
122 cc => \@cc
123 };
124 };
125
126 sub cfmakeraw($) {
127 my ($termios) = @_;
128 $termios->{iflag} &=
129 ~(POSIX::IGNBRK | POSIX::BRKINT | POSIX::PARMRK | POSIX::ISTRIP |
130 POSIX::INLCR | POSIX::IGNCR | POSIX::ICRNL | POSIX::IXON);
131 $termios->{oflag} &= ~POSIX::OPOST;
132 $termios->{lflag} &=
133 ~(POSIX::ECHO | POSIX::ECHONL | POSIX::ICANON | POSIX::ISIG |
134 POSIX::IEXTEN);
135 $termios->{cflag} &= ~(POSIX::CSIZE | POSIX::PARENB);
136 $termios->{cflag} |= POSIX::CS8;
137 }
138
139 sub tcgetattr($) {
140 my ($fd) = @_;
141 my $blob = termios();
142 ioctl($fd, TCGETS, $blob) or die "failed to get terminal attributes\n";
143 return $parse_termios->($blob);
144 }
145
146 sub tcsetattr($$) {
147 my ($fd, $termios) = @_;
148 my $blob = termios(%$termios);
149 ioctl($fd, TCSETS, $blob) or die "failed to set terminal attributes\n";
150 }
151
152 # tcgetsize -> (columns, rows)
153 sub tcgetsize($) {
154 my ($fd) = @_;
155 my $struct_winsz = pack('SSSS', 0, 0, 0, 0);
156 ioctl($fd, TIOCGWINSZ, $struct_winsz)
157 or die "failed to get window size: $!\n";
158 return reverse unpack('SS', $struct_winsz);
159 }
160
161 sub tcsetsize($$$) {
162 my ($fd, $columns, $rows) = @_;
163 my $struct_winsz = pack('SSSS', $rows, $columns, 0, 0);
164 ioctl($fd, TIOCSWINSZ, $struct_winsz)
165 or die "failed to set window size: $!\n";
166 }
167
168 sub read_password(;$$) {
169 my ($query, $infd, $outfd) = @_;
170
171 my $password = '';
172
173 $infd //= \*STDIN;
174
175 if (!-t $infd) { # Not a terminal? Then just get a line...
176 local $/ = "\n";
177 $password = <$infd>;
178 die "EOF while reading password\n" if !defined $password;
179 chomp $password; # Chop off the newline
180 return $password;
181 }
182
183 $outfd //= \*STDOUT;
184
185 # Raw read loop:
186 my $old_termios;
187 $old_termios = tcgetattr($infd);
188 my $raw_termios = {%$old_termios};
189 cfmakeraw($raw_termios);
190 tcsetattr($infd, $raw_termios);
191 eval {
192 my $echo = undef;
193 my ($ch, $got);
194 syswrite($outfd, $query, length($query));
195 while (($got = sysread($infd, $ch, 1))) {
196 my ($ord) = unpack('C', $ch);
197 if ($ord == 0xD) {
198 # newline, we're done
199 syswrite($outfd, "\r\n", 2);
200 last;
201 } elsif ($ord == 0x7f) {
202 # backspace - if it's the first key disable
203 # asterisks
204 $echo //= 0;
205 if (length($password)) {
206 chop $password;
207 syswrite($outfd, "\b \b", 3);
208 }
209 } elsif ($ord == 0x09) {
210 # TAB disables the asterisk-echo
211 $echo = 0;
212 } else {
213 # other character, append to password, if it's
214 # the first character enable asterisks echo
215 $echo //= 1;
216 $password .= $ch;
217 syswrite($outfd, '*', 1) if $echo;
218 }
219 }
220 die "read error: $!\n" if !defined($got);
221 };
222 my $err = $@;
223 tcsetattr($infd, $old_termios);
224 die $err if $err;
225 return $password;
226 }
227
228 # Class functions
229
230 sub new {
231 my ($class) = @_;
232
233 my ($master, $ttyname) = createpty();
234
235 my $self = {
236 master => $master,
237 ttyname => $ttyname,
238 };
239
240 return bless $self, $class;
241 }
242
243 # Properties
244
245 sub master { return $_[0]->{master} }
246 sub ttyname { return $_[0]->{ttyname} }
247
248 # Methods
249
250 sub close {
251 my ($self) = @_;
252 close($self->{master});
253 }
254
255 sub open_slave {
256 my ($self) = @_;
257 return $openslave->($self->{ttyname});
258 }
259
260 sub set_size {
261 my ($self, $columns, $rows) = @_;
262 tcsetsize($self->{master}, $columns, $rows);
263 }
264
265 # get_size -> (columns, rows)
266 sub get_size {
267 my ($self) = @_;
268 return tcgetsize($self->{master});
269 }
270
271 sub kill {
272 my ($self, $signal) = @_;
273 if (!ioctl($self->{master}, TIOCSIG, $signal)) {
274 # kill fallback if the ioctl does not work
275 kill $signal, $self->get_foreground_pid()
276 or die "failed to send signal: $!\n";
277 }
278 }
279
280 sub get_foreground_pid {
281 my ($self) = @_;
282 my $pid = pack('L', 0);
283 ioctl($self->{master}, TIOCGPGRP, $pid)
284 or die "failed to get foreground pid: $!\n";
285 return unpack('L', $pid);
286 }
287
288 sub has_process {
289 my ($self) = @_;
290 return 0 != $self->get_foreground_pid();
291 }
292
293 sub make_controlling_terminal {
294 my ($self) = @_;
295
296 #lose_controlling_terminal();
297 POSIX::setsid();
298 my $slave = $self->open_slave();
299 ioctl($slave, TIOCSCTTY, 0)
300 or die "failed to change controlling tty: $!\n";
301 POSIX::dup2(fileno($slave), 0) or die "failed to dup stdin\n";
302 POSIX::dup2(fileno($slave), 1) or die "failed to dup stdout\n";
303 POSIX::dup2(fileno($slave), 2) or die "failed to dup stderr\n";
304 CORE::close($slave) if fileno($slave) > 2;
305 CORE::close($self->{master});
306 }
307
308 sub getattr {
309 my ($self) = @_;
310 return tcgetattr($self->{master});
311 }
312
313 sub setattr {
314 my ($self, $termios) = @_;
315 return tcsetattr($self->{master}, $termios);
316 }
317
318 sub send_cc {
319 my ($self, $ccidx) = @_;
320 my $attrs = $self->getattr();
321 my $data = pack('C', $attrs->{cc}->[$ccidx]);
322 syswrite($self->{master}, $data)
323 == 1 || die "write failed: $!\n";
324 }
325
326 sub send_eof {
327 my ($self) = @_;
328 $self->send_cc(VEOF);
329 }
330
331 sub send_interrupt {
332 my ($self) = @_;
333 $self->send_cc(VINTR);
334 }
335
336 1;