/
Add-additional-functions-to-dtach.patch
197 lines (183 loc) · 5.13 KB
/
Add-additional-functions-to-dtach.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
From: Jason Wessel <jason.wessel@windriver.com>
Date: Sun, 17 Jul 2016 12:07:33 -0400
Subject: [PATCH] Add additional functions to dtach
-C Do not force the client to clear the screen on connect
-q Quiet exit from client when running commands
-w Allow the server to wait for a client to connect
-W <#> Terminate the server if a client doesn't connect in # seconds
Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
---
attach.c | 8 +++++---
dtach.h | 1 +
main.c | 35 +++++++++++++++++++++++++++++++++--
master.c | 24 ++++++++++++++++++++++++
4 files changed, 63 insertions(+), 5 deletions(-)
diff --git a/attach.c b/attach.c
index 034e277..a43268c 100644
--- a/attach.c
+++ b/attach.c
@@ -215,7 +215,8 @@ attach_main(int noerror)
tcsetattr(0, TCSADRAIN, &cur_term);
/* Clear the screen. This assumes VT100. */
- write(1, "\33[H\33[J", 6);
+ if (do_clear)
+ write(1, "\33[H\33[J", 6);
/* Tell the master that we want to attach. */
memset(&pkt, 0, sizeof(struct packet));
@@ -250,8 +251,9 @@ attach_main(int noerror)
if (len == 0)
{
- printf(EOS "\r\n[EOF - dtach terminating]"
- "\r\n");
+ if (!quiet_flag)
+ printf(EOS "\r\n[EOF - dtach terminating]"
+ "\r\n");
exit(0);
}
else if (len < 0)
diff --git a/dtach.h b/dtach.h
index d0ba87e..e369321 100644
--- a/dtach.h
+++ b/dtach.h
@@ -83,6 +83,7 @@
extern char *progname, *sockname;
extern int detach_char, no_suspend, redraw_method;
+extern int quiet_flag, do_clear, alarm_time;
extern struct termios orig_term;
extern int dont_have_tty;
diff --git a/main.c b/main.c
index 5fd7bcc..eb3a6a8 100644
--- a/main.c
+++ b/main.c
@@ -37,6 +37,12 @@ int detach_char = '\\' - 64;
int no_suspend;
/* The default redraw method. Initially set to unspecified. */
int redraw_method = REDRAW_UNSPEC;
+/* Clear screen on attach */
+int do_clear = 1;
+/* Quiet on exit */
+int quiet_flag = 0;
+/* Timeout for client connect */
+int alarm_time = 0;
/*
** The original terminal settings. Shared between the master and attach
@@ -70,14 +76,18 @@ usage()
" -p\t\tCopy the contents of standard input to the specified\n"
"\t\t socket.\n"
"Options:\n"
+ " -C\t\tDo not clear screen on attach.\n"
" -e <char>\tSet the detach character to <char>, defaults "
"to ^\\.\n"
" -E\t\tDisable the detach character.\n"
+ " -q\t\tQuiet on exit from client.\n"
" -r <method>\tSet the redraw method to <method>. The "
"valid methods are:\n"
"\t\t none: Don't redraw at all.\n"
"\t\t ctrl_l: Send a Ctrl L character to the program.\n"
"\t\t winch: Send a WINCH signal to the program.\n"
+ " -w\t\tWait for client to connect before process exec.\n"
+ " -W <#>\tServer exit after # seconds.\n"
" -z\t\tDisable processing of the suspend key.\n"
"\nReport any bugs to <" PACKAGE_BUGREPORT ">.\n",
PACKAGE_VERSION, __DATE__, __TIME__);
@@ -88,6 +98,7 @@ int
main(int argc, char **argv)
{
int mode = 0;
+ int connect_wait = 0;
/* Save the program name */
progname = argv[0];
@@ -157,6 +168,12 @@ main(int argc, char **argv)
{
if (*p == 'E')
detach_char = -1;
+ else if (*p == 'C')
+ do_clear = 0;
+ else if (*p == 'q')
+ quiet_flag = 1;
+ else if (*p == 'w')
+ connect_wait = 1;
else if (*p == 'z')
no_suspend = 1;
else if (*p == 'e')
@@ -208,6 +225,20 @@ main(int argc, char **argv)
}
break;
}
+ else if (*p == 'W')
+ {
+ ++argv; --argc;
+ if (argc < 1)
+ {
+ printf("%s: No time specified.\n",
+ progname);
+ printf("Try '%s --help' for more "
+ "information.\n", progname);
+ return 1;
+ }
+ alarm_time = atoi(argv[0]);
+ break;
+ }
else
{
printf("%s: Invalid option '-%c'\n",
@@ -255,9 +286,9 @@ main(int argc, char **argv)
return attach_main(0);
}
else if (mode == 'n')
- return master_main(argv, 0, 0);
+ return master_main(argv, connect_wait, 0);
else if (mode == 'N')
- return master_main(argv, 0, 1);
+ return master_main(argv, connect_wait, 1);
else if (mode == 'c')
{
if (master_main(argv, 1, 0) != 0)
diff --git a/master.c b/master.c
index b136f03..774cfe6 100644
--- a/master.c
+++ b/master.c
@@ -339,6 +339,10 @@ control_activity(int s)
close(fd);
return;
}
+ if (alarm_time) {
+ alarm(0);
+ alarm_time = 0;
+ }
/* Link it in. */
p = malloc(sizeof(struct client));
@@ -430,6 +434,17 @@ client_activity(struct client *p)
}
}
+static int dup_stdout = -1;
+
+/* Alarm handler for timeout waiting on client connect */
+static void
+ alarm_handler(int sig)
+{
+ if (!quiet_flag && dup_stdout >= 0)
+ dprintf(dup_stdout, "EXIT: Client connect timeout\n");
+ exit(sig);
+}
+
/* The master process - It watches over the pty process and the attached */
/* clients. */
static void
@@ -475,6 +490,15 @@ master_process(int s, char **argv, int waitattach, int statusfd)
if (statusfd != -1)
close(statusfd);
+
+ if (alarm_time)
+ {
+ /* Duplicate stdout for later output use */
+ dup_stdout = dup(1);
+ signal(SIGALRM, alarm_handler);
+ alarm(alarm_time);
+ }
+
/* Make sure stdin/stdout/stderr point to /dev/null. We are now a
** daemon. */
nullfd = open("/dev/null", O_RDWR);
--
2.5.0