Debian Codegraph @

attachtty.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * detachtty - run long-lived processes non interactively in the background,
3 * and reattach them to a terminal at later time
4 *
5 * Copyright (C) 2016-2017 Massimiliano Ghilardi
6 * Copyright (C) 2001-2005 Daniel Barlow
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #include <errno.h>
24 #include <time.h>
25
26 #include "config.h"
27
28 extern FILE *log_fp;
29 void connect_direct(char * path, char *text, int timeout) ;
30 void connect_ssh(char *host, char *path, char *text, char *timeout) ;
31
32 int init_tty(void);
33 int cleanup_tty(void);
34 static void init_signal_handlers(void);
35 static void cleanup_signal_handler(int signal);
36
37 #ifndef UNIX_PATH_MAX
38 # define UNIX_PATH_MAX 108
39 #endif
40
41 static char * const MY_NAME = "attachtty";
42
43 /*
44 attachtty /path/to/socket [text-to-send [timeout]]
45
46 or
47
48 attachtty user@hostname:/path/to/socket [text-to-send [timeout]]
49
50 In the latter case, we open an ssh connection to user@hostname then
51 run attachtty on the remote machine with the remainder of the arguments
52 */
53
54 volatile int was_interrupted=0, was_suspended=0, was_resized=0, time_to_die=0;
55 void tears_in_the_rain(int signal) {
56 time_to_die=signal;
57 cleanup_signal_handler(signal);
58 }
59 void control_c_pressed(int signal) {
60 was_interrupted=1;
61 }
62 void control_z_pressed(int signal) {
63 was_suspended=1;
64 }
65 void window_resized(int signal) {
66 was_resized=1;
67 }
68
69 void init_ctrl_z_handler(void) {
70 struct sigaction act;
71 act.sa_handler = control_z_pressed;
72 sigemptyset(&(act.sa_mask));
73 act.sa_flags = SA_RESETHAND;
74 sigaction(SIGTSTP,&act,0);
75 }
76
77 void cleanup_ctrl_z_handler(void) {
78 cleanup_signal_handler(SIGTSTP);
79 }
80
81 void suspend_myself(void) {
82 /* restore tty and SIGTSTP settings before suspending myself */
83 cleanup_tty();
84 cleanup_ctrl_z_handler();
85
86 kill(getpid(), SIGTSTP);
87
88 /* received SIGCONT: perform again initial setup */
89 init_ctrl_z_handler();
90 init_tty();
91 }
92
93
94 static void init_signal_handlers(void) {
95 struct sigaction act;
96 int i, fatal_sig[] = {
97 SIGHUP, SIGQUIT, SIGILL, SIGABRT, SIGBUS, SIGFPE, SIGSEGV, SIGPIPE,
98 SIGTERM, SIGSTKFLT, SIGCHLD, SIGXCPU, SIGXFSZ,
99 };
100
101 /* catch SIGINT and send character \003 over the link */
102 act.sa_handler=control_c_pressed;
103 sigemptyset(&(act.sa_mask));
104 act.sa_flags=0;
105 sigaction(SIGINT,&act,0);
106
107 /* catch SIGWINCH and send window size over the link */
108 act.sa_handler=window_resized;
109 sigemptyset(&(act.sa_mask));
110 act.sa_flags=0;
111 sigaction(SIGWINCH,&act,0);
112
113 /* catch SIGSTOP and cleanup tty before suspending */
114 init_ctrl_z_handler();
115
116 /* catch SIGCHLD, SIGQUIT, SIGTERM, SIGILL, SIGFPE... and exit */
117 act.sa_handler = tears_in_the_rain;
118 sigemptyset(&(act.sa_mask));
119 act.sa_flags=SA_RESETHAND;
120 for (i = 0; i < sizeof(fatal_sig)/sizeof(fatal_sig[0]); i++) {
121 sigaction(fatal_sig[i],&act,0);
122 }
123 }
124
125 static void cleanup_signal_handler(int sig) {
126 struct sigaction act;
127 act.sa_handler = SIG_DFL;
128 sigemptyset(&(act.sa_mask));
129 act.sa_flags = 0;
130 sigaction(sig, &act, 0);
131 }
132
133
134
135
136
137 struct termios saved_tty;
138
139 #ifndef _POSIX_VDISABLE
140 # define _POSIX_VDISABLE 0
141 #endif
142 #ifndef ONOCR
143 # define ONOCR 0
144 #endif
145
146 int init_tty(void) {
147 struct termios tty;
148 int err = tcgetattr(0, &saved_tty);
149 if (err == 0)
150 {
151 tty = saved_tty;
152 tty.c_iflag &= ~(INLCR|ICRNL|IGNCR|IXON|IXOFF);
153 tty.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
154 #ifdef ONLCR
155 tty.c_oflag |= ONLCR; /* avoid staircase effect */
156 #endif
157 tty.c_oflag &= ~(ONLCR|OCRNL|ONOCR|ONLRET);
158 tty.c_lflag &= ~(ECHO|ICANON|IEXTEN);
159 tty.c_cc[VSTART] = tty.c_cc[VSTOP] = _POSIX_VDISABLE;
160 err = tcsetattr(0, TCSADRAIN, &tty);
161 }
162 return err;
163 }
164
165 int cleanup_tty(void) {
166 return tcsetattr(0, TCSADRAIN, &saved_tty);
167 }
168
169 int main(int argc,char *argv[], char *envp[]) {
170 char *host=NULL; /* "hostname" or "user@hostname" */
171 char *path; /* path to socket */
172 char *p;
173 char *text=NULL;
174 char *timeout_str=NULL;
175 int timeout = 1;
176
177 if (argc<2 || argc>4) {
178 fprintf(stderr,
179 "%s: unrecognized arguments\n"
180 "usage: %s /path/to/socket [text] [timeout]\n"
181 " %s remote_user@remote_host:/path/to/socket [text] [timeout]\n",
182 argv[0], argv[0], argv[0]);
183 exit(1);
184 }
185 p=strdup(argv[1]);
186 log_fp=stderr;
187 if((path=strchr(p,':')) != NULL) {
188 host=p;
189 *path='\0';
190 path++;
191 } else {
192 path=p;
193 }
194 if (argc >= 3)
195 text = argv[2];
196 if (argc == 4) {
197 timeout_str = argv[3];
198 int read_timeout = atoi(timeout_str);
199 if (read_timeout > 0)
200 timeout = read_timeout;
201 }
202
203 init_signal_handlers();
204
205 setlinebuf(stdout);
206 setlinebuf(stderr);
207
208 if (host) {
209 logprintf(MY_NAME,"connecting through ssh to %s on %s",path,host);
210 connect_ssh(host,path,text,timeout_str);
211 } else {
212 logprintf(MY_NAME,"connecting directly to %s",path);
213 init_tty();
214 connect_direct(path,text,timeout);
215 cleanup_tty();
216 }
217 if (time_to_die != 0) {
218 logprintf(MY_NAME,"got signal %d, exiting",time_to_die);
219 }
220 return 0;
221 }
222
223
224 int send_window_size(int fd)
225 {
226 struct winsize my_winsize;
227
228 if (fd >= 0 && ioctl(0,TIOCGWINSZ,&my_winsize) == 0 && ioctl(fd,TIOCSWINSZ,&my_winsize) == 0)
229 return 0;
230
231 return -1;
232 }
233
234 /* copy between stdin,stdout and unix-domain socket */
235
236 void connect_direct(char * path, char *text, int timeout) {
237 int sock=-1, pty_master=-1, ufds_n = 0;
238 int *recv_fd = &pty_master;
239 int text_len = text ? strlen(text) : 0;
240 struct pollfd ufds[3];
241 struct sockaddr_un s_a;
242
243 s_a.sun_family=AF_UNIX;
244 strncpy(s_a.sun_path,path,UNIX_PATH_MAX);
245 s_a.sun_path[UNIX_PATH_MAX-1]='\0';
246
247 sock=socket(PF_UNIX,SOCK_STREAM,0);
248 if(sock==-1) bail(MY_NAME,"socket");
249
250 if(connect(sock,(const struct sockaddr *) &s_a,sizeof s_a)!=0)
251 bail(MY_NAME,"connect");
252
253 int time_end = time(NULL) + timeout;
254 int msec_left = -1;
255
256 if (text) {
257 ufds[0].fd=sock; ufds[0].events=POLLIN|POLLOUT;
258 ufds_n=1;
259 } else {
260 ufds[0].fd=sock; ufds[0].events=POLLIN;
261 ufds[1].fd=0; ufds[1].events=POLLIN|POLLHUP;
262 ufds_n=2;
263 }
264
265 while (!time_to_die) {
266
267 if (was_interrupted) {
268 was_interrupted=0;
269 write(sock,"\003",1);
270 }
271 if (was_suspended) {
272 was_suspended=0;
273 suspend_myself();
274 }
275 if (was_resized && pty_master >= 0) {
276 was_resized=0;
277 send_window_size(pty_master);
278 }
279 if (text)
280 msec_left = (time_end - time(NULL)) * 1000;
281 else
282 msec_left = -1;
283
284 if (poll(ufds, ufds_n, msec_left) == -1) {
285 if (errno == EINTR)
286 continue;
287 else
288 bail(MY_NAME, "poll");
289 }
290
291 if (ufds[0].revents & POLLIN) {
292 if (copy_a_bit_recvfd_with_log(sock,1,-1,recv_fd, MY_NAME,
293 "copying from socket, exiting") == 0)
294 break;
295 /*
296 send window size as soon as we retrieve pty_master through the link.
297 later, we will send window size again each time we receive a SIGWINCH
298 */
299 if (recv_fd != NULL && pty_master >= 0) {
300 send_window_size(pty_master);
301 recv_fd = NULL;
302 }
303 }
304
305 if (text) {
306 if (ufds[0].revents & POLLOUT) {
307 int written = write(sock,text,text_len);
308 if (written > 0) {
309 text += written;
310 text_len -= written;
311 if (text_len <= 0) {
312 ufds[0].events = POLLIN; /* no longer need to output */
313 write(sock,"\r",1);
314 }
315 }
316 }
317
318 if (time(NULL) >= time_end) {
319 close(sock);
320 sock=-1;
321 time_to_die = 1;
322 }
323 } else {
324 if (ufds[1].revents & POLLIN) {
325 if (copy_a_bit_with_log(0,sock,-1,MY_NAME, "copying to socket, exiting") == 0)
326 break;
327 }
328 if (ufds[1].revents & POLLHUP) {
329 logprintf(MY_NAME, "closed connection due to hangup, exiting");
330 break;
331 }
332 }
333 }
334 }
335
336
337
338 void connect_ssh(char *host, char *path, char *text, char *timeout_str) {
339 /*
340 * ssh option -t forces tty allocation on remote side.
341 * Needed to properly setup local and remote tty,
342 * including -icanon -iecho flags and forwarding of CTRL+C
343 */
344 execlp("ssh", "ssh", "-t", host, "attachtty", path, text, timeout_str, (char *)NULL);
345 bail(MY_NAME, "exec ssh failed");
346 }
347
348 /*
349 * Local Variables:
350 * c-basic-offset: 4
351 * End:
352 */