-
Notifications
You must be signed in to change notification settings - Fork 4
/
dtattach.c
442 lines (391 loc) · 9.77 KB
/
dtattach.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
dtach - A simple program that emulates the detach feature of screen.
Copyright (C) 2004-2008 Ned T. Crigler, 2011 Devin J. Pohly
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "dtach.h"
/* Make sure the binary has a copyright. */
const char copyright[] =
"dtattach - version " PACKAGE_VERSION ", compiled on " BUILD_DATE ".\n"
" (C) Copyright 2004-2008 Ned T. Crigler, 2011 Devin J. Pohly\n";
#ifndef VDISABLE
#ifdef _POSIX_VDISABLE
#define VDISABLE _POSIX_VDISABLE
#else
#define VDISABLE 0377
#endif
#endif
/* argv[0] from the program */
char *progname;
/* The name of the passed in socket. */
char *sockname;
/* Flag to signal detachment. */
int attached;
/* The character used for detaching. Defaults to '^\' */
int detach_char = '\\' - 64;
/* 1 if we should not interpret the suspend character. */
int no_suspend;
/* The default redraw method. Initially set to unspecified. */
int redraw_method = REDRAW_UNSPEC;
/* The original terminal settings, for restoring later. */
struct termios orig_term;
/*
** The current terminal settings. After coming back from a suspend, we
** restore this.
*/
static struct termios cur_term;
/* 1 if the window size changed */
static int win_changed;
static void
usage()
{
printf(
"Usage: dtattach <socket> <options>\n"
"Options:\n"
" -e <char>\tSet the detach character to <char>. Defaults "
"to ^\\.\n"
"\t\t Use \"\" to disable.\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"
" -z\t\tDisable processing of the suspend key.\n"
"\nReport any bugs to <%s>.\n", PACKAGE_BUGREPORT);
}
/* Restores the original terminal settings. */
static void
restore_term(void)
{
tcsetattr(0, TCSADRAIN, &orig_term);
}
/* Connects to a unix domain socket */
static int
connect_socket(char *name)
{
int s;
struct sockaddr_un sockun;
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
init_sockaddr_un(&sockun, name);
if (connect(s, (struct sockaddr*)&sockun, sizeof(sockun)) < 0)
{
close(s);
/* ECONNREFUSED is also returned for regular files, so make
** sure we are trying to connect to a socket. */
if (errno == ECONNREFUSED)
{
struct stat st;
if (stat(name, &st) < 0)
return -1;
else if (!S_ISSOCK(st.st_mode) || S_ISREG(st.st_mode))
errno = ENOTSOCK;
}
return -1;
}
return s;
}
/* Signal */
static RETSIGTYPE
die(int sig)
{
/* Print a nice pretty message for some things. */
if (sig != SIGHUP && sig != SIGINT)
{
fprintf(stderr, "got signal %d\r\n", sig);
exit(128 + sig);
}
attached = 0;
}
/* Window size change. */
static RETSIGTYPE
win_change()
{
win_changed = 1;
}
/* Handles input from the keyboard. */
static void
process_kbd(int s, struct packet *pkt)
{
/* Suspend? */
if (!no_suspend && (pkt->u.buf[0] == cur_term.c_cc[VSUSP]))
{
/* Tell the master that we are suspending. */
pkt->type = MSG_DETACH;
write(s, pkt, sizeof(struct packet));
/* And suspend... */
tcsetattr(0, TCSADRAIN, &orig_term);
printf("\r\n");
raise(SIGTSTP);
tcsetattr(0, TCSADRAIN, &cur_term);
/* Tell the master that we are returning. */
pkt->type = MSG_ATTACH;
write(s, pkt, sizeof(struct packet));
/* We would like a redraw, too. */
pkt->type = MSG_REDRAW;
pkt->len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt->u.ws);
write(s, pkt, sizeof(struct packet));
return;
}
/* Detach char? */
else if (pkt->u.buf[0] == detach_char)
{
attached = 0;
return;
}
/* Just in case something pukes out. */
else if (pkt->u.buf[0] == '\f')
win_changed = 1;
/* Push it out */
write(s, pkt, sizeof(struct packet));
}
static int
attach_main()
{
struct packet pkt;
unsigned char buf[BUFSIZE];
fd_set readfds;
int s;
/* Attempt to open the socket. */
s = connect_socket(sockname);
if (s < 0)
{
fprintf(stderr, "%s: %s: %s\r\n", progname, sockname,
strerror(errno));
return (errno == ECONNREFUSED) ? 2 : 1;
}
/* The current terminal settings are equal to the original terminal
** settings at this point. */
cur_term = orig_term;
/* Set a trap to restore the terminal when we die. */
atexit(restore_term);
/* Mask out our signals. */
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, SIGHUP);
sigaddset(&sigs, SIGTERM);
sigaddset(&sigs, SIGINT);
sigaddset(&sigs, SIGQUIT);
sigaddset(&sigs, SIGWINCH);
sigprocmask(SIG_SETMASK, &sigs, NULL);
sigemptyset(&sigs);
/* Register signal handlers. */
struct sigaction sa;
sa.sa_flags = 0;
sa.sa_mask = sigs;
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGXFSZ, &sa, NULL);
sa.sa_handler = die;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sa.sa_handler = win_change;
sigaction(SIGWINCH, &sa, NULL);
/* Set raw mode. */
cur_term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
cur_term.c_iflag &= ~(IXON|IXOFF);
cur_term.c_oflag &= ~(OPOST);
cur_term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
cur_term.c_cflag &= ~(CSIZE|PARENB);
cur_term.c_cflag |= CS8;
cur_term.c_cc[VLNEXT] = VDISABLE;
cur_term.c_cc[VMIN] = 1;
cur_term.c_cc[VTIME] = 0;
tcsetattr(0, TCSADRAIN, &cur_term);
/* Tell the master that we want to attach. */
memset(&pkt, 0, sizeof(struct packet));
pkt.type = MSG_ATTACH;
write(s, &pkt, sizeof(struct packet));
/* We would like a redraw, too. */
pkt.type = MSG_REDRAW;
pkt.len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write(s, &pkt, sizeof(struct packet));
/* Wait for things to happen */
attached = 1;
while (attached)
{
int n;
/* Window size changed? */
if (win_changed)
{
win_changed = 0;
pkt.type = MSG_WINCH;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write(s, &pkt, sizeof(pkt));
}
FD_ZERO(&readfds);
FD_SET(0, &readfds);
FD_SET(s, &readfds);
n = pselect(s + 1, &readfds, NULL, NULL, NULL, &sigs);
if (n < 0 && errno != EINTR && errno != EAGAIN)
{
fprintf(stderr, "select failed\r\n");
return 1;
}
/* Pty activity */
if (n > 0 && FD_ISSET(s, &readfds))
{
ssize_t len = read(s, buf, sizeof(buf));
if (len == 0)
return 3;
else if (len < 0)
{
fprintf(stderr, "read returned an error\r\n");
return 1;
}
/* Send the data to the terminal. */
write(1, buf, len);
n--;
}
/* stdin activity */
if (n > 0 && FD_ISSET(0, &readfds))
{
ssize_t len;
pkt.type = MSG_PUSH;
memset(pkt.u.buf, 0, sizeof(pkt.u.buf));
len = read(0, pkt.u.buf, sizeof(pkt.u.buf));
/* Detach on EOF from stdin */
if (len == 0)
break;
else if (len < 0)
return 1;
pkt.len = len;
process_kbd(s, &pkt);
n--;
}
}
return 0;
}
int
main(int argc, char **argv)
{
/* Save the program name */
progname = argv[0];
++argv; --argc;
/* Parse the arguments */
if (argc < 1)
{
fprintf(stderr, "%s: No socket was specified.\n", progname);
fprintf(stderr, "Try '%s --help' for more information.\n",
progname);
return 1;
}
if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-?") == 0)
{
usage();
return 0;
}
else if (strcmp(*argv, "--version") == 0)
{
printf("%s", copyright);
return 0;
}
sockname = *argv;
++argv; --argc;
while (argc >= 1 && **argv == '-')
{
char *p;
for (p = *argv + 1; *p; ++p)
{
if (*p == 'z')
no_suspend = 1;
else if (*p == 'e')
{
++argv; --argc;
if (argc < 1)
{
fprintf(stderr, "%s: No escape "
"character specified.\n",
progname);
fprintf(stderr, "Try '%s --help' for "
"more information.\n",
progname);
return 1;
}
if (argv[0][0] == '^' && argv[0][1])
{
if (argv[0][1] == '?')
detach_char = '\177';
else
detach_char = argv[0][1] & 037;
}
else if (!argv[0][0])
detach_char = -1;
else
detach_char = argv[0][0];
break;
}
else if (*p == 'r')
{
++argv; --argc;
if (argc < 1)
{
fprintf(stderr, "%s: No redraw method "
"specified.\n", progname);
fprintf(stderr, "Try '%s --help' for "
"more information.\n",
progname);
return 1;
}
if (strcmp(argv[0], "none") == 0)
redraw_method = REDRAW_NONE;
else if (strcmp(argv[0], "ctrl_l") == 0)
redraw_method = REDRAW_CTRL_L;
else if (strcmp(argv[0], "winch") == 0)
redraw_method = REDRAW_WINCH;
else
{
fprintf(stderr, "%s: Invalid redraw "
"method specified.\n",
progname);
fprintf(stderr, "Try '%s --help' for "
"more information.\n",
progname);
return 1;
}
break;
}
else if (*p == '?')
{
usage();
return 0;
}
else
{
fprintf(stderr, "%s: Invalid option '-%c'\n",
progname, *p);
fprintf(stderr, "Try '%s --help' for more "
"information.\n", progname);
return 1;
}
}
++argv; --argc;
}
if (argc > 0)
{
fprintf(stderr, "%s: Invalid number of arguments.\n",
progname);
fprintf(stderr, "Try '%s --help' for more information.\n",
progname);
return 1;
}
/* Save the original terminal settings. */
if (tcgetattr(0, &orig_term) < 0)
memset(&orig_term, 0, sizeof(struct termios));
return attach_main();
}