1 |
|
|
/* $OpenBSD: tty.c,v 1.16 2015/12/14 13:59:42 tb Exp $ */ |
2 |
|
|
|
3 |
|
|
#include <errno.h> |
4 |
|
|
#include <fcntl.h> |
5 |
|
|
#include <string.h> |
6 |
|
|
#include <unistd.h> |
7 |
|
|
|
8 |
|
|
#include "sh.h" |
9 |
|
|
#include "tty.h" |
10 |
|
|
|
11 |
|
|
int tty_fd = -1; /* dup'd tty file descriptor */ |
12 |
|
|
int tty_devtty; /* true if tty_fd is from /dev/tty */ |
13 |
|
|
struct termios tty_state; /* saved tty state */ |
14 |
|
|
|
15 |
|
|
void |
16 |
|
|
tty_close(void) |
17 |
|
|
{ |
18 |
✓✓ |
171182 |
if (tty_fd >= 0) { |
19 |
|
108 |
close(tty_fd); |
20 |
|
108 |
tty_fd = -1; |
21 |
|
108 |
} |
22 |
|
85591 |
} |
23 |
|
|
|
24 |
|
|
/* Initialize tty_fd. Used for saving/resetting tty modes upon |
25 |
|
|
* foreground job completion and for setting up tty process group. |
26 |
|
|
*/ |
27 |
|
|
void |
28 |
|
|
tty_init(int init_ttystate) |
29 |
|
|
{ |
30 |
|
|
int do_close = 1; |
31 |
|
|
int tfd; |
32 |
|
|
|
33 |
|
7248 |
tty_close(); |
34 |
|
3624 |
tty_devtty = 1; |
35 |
|
|
|
36 |
|
3624 |
tfd = open("/dev/tty", O_RDWR, 0); |
37 |
✗✓ |
3624 |
if (tfd < 0) { |
38 |
|
|
tty_devtty = 0; |
39 |
|
|
warningf(false, "No controlling tty (open /dev/tty: %s)", |
40 |
|
|
strerror(errno)); |
41 |
|
|
|
42 |
|
|
do_close = 0; |
43 |
|
|
if (isatty(0)) |
44 |
|
|
tfd = 0; |
45 |
|
|
else if (isatty(2)) |
46 |
|
|
tfd = 2; |
47 |
|
|
else { |
48 |
|
|
warningf(false, "Can't find tty file descriptor"); |
49 |
|
|
return; |
50 |
|
|
} |
51 |
|
|
} |
52 |
✗✓ |
3624 |
if ((tty_fd = fcntl(tfd, F_DUPFD_CLOEXEC, FDBASE)) < 0) { |
53 |
|
|
warningf(false, "j_ttyinit: dup of tty fd failed: %s", |
54 |
|
|
strerror(errno)); |
55 |
✗✓ |
3624 |
} else if (init_ttystate) |
56 |
|
|
tcgetattr(tty_fd, &tty_state); |
57 |
✓✗ |
3624 |
if (do_close) |
58 |
|
3624 |
close(tfd); |
59 |
|
7248 |
} |