28d00fe4 |
1 | /* |
2 | * Pseudo-tty backend for pterm. |
3 | * |
4 | * Unlike the other backends, data for this one is not neatly |
5 | * encapsulated into a data structure, because it wouldn't make |
6 | * sense to do so - the utmp stuff has to be done before a backend |
7 | * is initialised, and starting a second pterm from the same |
8 | * process would therefore be infeasible because privileges would |
9 | * already have been dropped. Hence, I haven't bothered to keep the |
10 | * data dynamically allocated: instead, the backend handle is just |
11 | * a null pointer and ignored everywhere. |
12 | */ |
13 | |
054d8535 |
14 | #define _XOPEN_SOURCE |
88e6b9ca |
15 | #define _XOPEN_SOURCE_EXTENDED |
74aca06d |
16 | #define _GNU_SOURCE |
054d8535 |
17 | #include <features.h> |
18 | |
1709795f |
19 | #include <stdio.h> |
20 | #include <stdlib.h> |
054d8535 |
21 | #include <string.h> |
22 | #include <unistd.h> |
a0e16eb1 |
23 | #include <signal.h> |
054d8535 |
24 | #include <fcntl.h> |
88e6b9ca |
25 | #include <termios.h> |
be4f86de |
26 | #include <grp.h> |
755a6d84 |
27 | #include <utmp.h> |
28 | #include <pwd.h> |
29 | #include <time.h> |
a0e16eb1 |
30 | #include <sys/types.h> |
31 | #include <sys/wait.h> |
88e6b9ca |
32 | #include <sys/ioctl.h> |
0f33f9d1 |
33 | #include <errno.h> |
1709795f |
34 | |
35 | #include "putty.h" |
36 | |
37 | #ifndef FALSE |
38 | #define FALSE 0 |
39 | #endif |
40 | #ifndef TRUE |
41 | #define TRUE 1 |
42 | #endif |
43 | |
755a6d84 |
44 | #ifndef UTMP_FILE |
45 | #define UTMP_FILE "/var/run/utmp" |
46 | #endif |
47 | #ifndef WTMP_FILE |
48 | #define WTMP_FILE "/var/log/wtmp" |
49 | #endif |
50 | #ifndef LASTLOG_FILE |
51 | #ifdef _PATH_LASTLOG |
52 | #define LASTLOG_FILE _PATH_LASTLOG |
53 | #else |
54 | #define LASTLOG_FILE "/var/log/lastlog" |
55 | #endif |
56 | #endif |
57 | |
f51dc031 |
58 | /* |
59 | * Set up a default for vaguely sane systems. The idea is that if |
60 | * OMIT_UTMP is not defined, then at least one of the symbols which |
61 | * enable particular forms of utmp processing should be, if only so |
62 | * that a link error can warn you that you should have defined |
63 | * OMIT_UTMP if you didn't want any. Currently HAVE_PUTUTLINE is |
64 | * the only such symbol. |
65 | */ |
66 | #ifndef OMIT_UTMP |
67 | #if !defined HAVE_PUTUTLINE |
68 | #define HAVE_PUTUTLINE |
69 | #endif |
70 | #endif |
71 | |
74aca06d |
72 | static Config pty_cfg; |
73 | static int pty_master_fd; |
a8327734 |
74 | static void *pty_frontend; |
95c47834 |
75 | static char pty_name[FILENAME_MAX]; |
74aca06d |
76 | static int pty_signal_pipe[2]; |
f51dc031 |
77 | static int pty_stamped_utmp = 0; |
a0e16eb1 |
78 | static int pty_child_pid; |
95c47834 |
79 | static int pty_utmp_helper_pid, pty_utmp_helper_pipe; |
f278d6f8 |
80 | static int pty_term_width, pty_term_height; |
74aca06d |
81 | static int pty_child_dead, pty_finished; |
82 | static int pty_exit_code; |
755a6d84 |
83 | #ifndef OMIT_UTMP |
84 | static struct utmp utmp_entry; |
85 | #endif |
6169c758 |
86 | char **pty_argv; |
054d8535 |
87 | |
74aca06d |
88 | static void pty_close(void); |
89 | |
95c47834 |
90 | static void setup_utmp(char *ttyname, char *location) |
755a6d84 |
91 | { |
92 | #ifndef OMIT_UTMP |
93 | #ifdef HAVE_LASTLOG |
94 | struct lastlog lastlog_entry; |
95 | FILE *lastlog; |
96 | #endif |
97 | struct passwd *pw; |
755a6d84 |
98 | FILE *wtmp; |
99 | |
100 | pw = getpwuid(getuid()); |
101 | memset(&utmp_entry, 0, sizeof(utmp_entry)); |
102 | utmp_entry.ut_type = USER_PROCESS; |
103 | utmp_entry.ut_pid = getpid(); |
104 | strncpy(utmp_entry.ut_line, ttyname+5, lenof(utmp_entry.ut_line)); |
105 | strncpy(utmp_entry.ut_id, ttyname+8, lenof(utmp_entry.ut_id)); |
106 | strncpy(utmp_entry.ut_user, pw->pw_name, lenof(utmp_entry.ut_user)); |
107 | strncpy(utmp_entry.ut_host, location, lenof(utmp_entry.ut_host)); |
108 | time(&utmp_entry.ut_time); |
109 | |
110 | #if defined HAVE_PUTUTLINE |
111 | utmpname(UTMP_FILE); |
112 | setutent(); |
113 | pututline(&utmp_entry); |
114 | endutent(); |
115 | #endif |
116 | |
117 | if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) { |
118 | fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp); |
119 | fclose(wtmp); |
120 | } |
121 | |
122 | #ifdef HAVE_LASTLOG |
123 | memset(&lastlog_entry, 0, sizeof(lastlog_entry)); |
124 | strncpy(lastlog_entry.ll_line, ttyname+5, lenof(lastlog_entry.ll_line)); |
125 | strncpy(lastlog_entry.ll_host, location, lenof(lastlog_entry.ll_host)); |
126 | time(&lastlog_entry.ll_time); |
127 | if ((lastlog = fopen(LASTLOG_FILE, "r+")) != NULL) { |
128 | fseek(lastlog, sizeof(lastlog_entry) * getuid(), SEEK_SET); |
129 | fwrite(&lastlog_entry, 1, sizeof(lastlog_entry), lastlog); |
130 | fclose(lastlog); |
131 | } |
132 | #endif |
133 | |
f51dc031 |
134 | pty_stamped_utmp = 1; |
135 | |
755a6d84 |
136 | #endif |
137 | } |
138 | |
139 | static void cleanup_utmp(void) |
140 | { |
141 | #ifndef OMIT_UTMP |
142 | FILE *wtmp; |
143 | |
95c47834 |
144 | if (!pty_stamped_utmp) |
f51dc031 |
145 | return; |
146 | |
755a6d84 |
147 | utmp_entry.ut_type = DEAD_PROCESS; |
148 | memset(utmp_entry.ut_user, 0, lenof(utmp_entry.ut_user)); |
149 | time(&utmp_entry.ut_time); |
150 | |
151 | if ((wtmp = fopen(WTMP_FILE, "a")) != NULL) { |
152 | fwrite(&utmp_entry, 1, sizeof(utmp_entry), wtmp); |
153 | fclose(wtmp); |
154 | } |
155 | |
156 | memset(utmp_entry.ut_line, 0, lenof(utmp_entry.ut_line)); |
157 | utmp_entry.ut_time = 0; |
158 | |
159 | #if defined HAVE_PUTUTLINE |
160 | utmpname(UTMP_FILE); |
161 | setutent(); |
162 | pututline(&utmp_entry); |
163 | endutent(); |
164 | #endif |
165 | |
f51dc031 |
166 | pty_stamped_utmp = 0; /* ensure we never double-cleanup */ |
755a6d84 |
167 | #endif |
168 | } |
169 | |
a0e16eb1 |
170 | static void sigchld_handler(int signum) |
171 | { |
74aca06d |
172 | write(pty_signal_pipe[1], "x", 1); |
a0e16eb1 |
173 | } |
174 | |
755a6d84 |
175 | static void fatal_sig_handler(int signum) |
176 | { |
0f33f9d1 |
177 | putty_signal(signum, SIG_DFL); |
755a6d84 |
178 | cleanup_utmp(); |
179 | setuid(getuid()); |
180 | raise(signum); |
181 | } |
182 | |
95c47834 |
183 | static void pty_open_master(void) |
1709795f |
184 | { |
be4f86de |
185 | #ifdef BSD_PTYS |
95c47834 |
186 | const char chars1[] = "pqrstuvwxyz"; |
187 | const char chars2[] = "0123456789abcdef"; |
188 | const char *p1, *p2; |
189 | char master_name[20]; |
190 | struct group *gp; |
191 | |
192 | for (p1 = chars1; *p1; p1++) |
193 | for (p2 = chars2; *p2; p2++) { |
194 | sprintf(master_name, "/dev/pty%c%c", *p1, *p2); |
195 | pty_master_fd = open(master_name, O_RDWR); |
196 | if (pty_master_fd >= 0) { |
197 | if (geteuid() == 0 || |
198 | access(master_name, R_OK | W_OK) == 0) |
199 | goto got_one; |
200 | close(pty_master_fd); |
be4f86de |
201 | } |
95c47834 |
202 | } |
be4f86de |
203 | |
95c47834 |
204 | /* If we get here, we couldn't get a tty at all. */ |
205 | fprintf(stderr, "pterm: unable to open a pseudo-terminal device\n"); |
206 | exit(1); |
be4f86de |
207 | |
95c47834 |
208 | got_one: |
209 | strcpy(pty_name, master_name); |
210 | pty_name[5] = 't'; /* /dev/ptyXX -> /dev/ttyXX */ |
211 | |
212 | /* We need to chown/chmod the /dev/ttyXX device. */ |
213 | gp = getgrnam("tty"); |
214 | chown(pty_name, getuid(), gp ? gp->gr_gid : -1); |
215 | chmod(pty_name, 0600); |
be4f86de |
216 | #else |
054d8535 |
217 | pty_master_fd = open("/dev/ptmx", O_RDWR); |
218 | |
219 | if (pty_master_fd < 0) { |
220 | perror("/dev/ptmx: open"); |
221 | exit(1); |
222 | } |
223 | |
224 | if (grantpt(pty_master_fd) < 0) { |
225 | perror("grantpt"); |
226 | exit(1); |
227 | } |
228 | |
229 | if (unlockpt(pty_master_fd) < 0) { |
230 | perror("unlockpt"); |
231 | exit(1); |
232 | } |
233 | |
95c47834 |
234 | pty_name[FILENAME_MAX-1] = '\0'; |
235 | strncpy(pty_name, ptsname(pty_master_fd), FILENAME_MAX-1); |
be4f86de |
236 | #endif |
95c47834 |
237 | } |
054d8535 |
238 | |
95c47834 |
239 | /* |
240 | * Pre-initialisation. This is here to get around the fact that GTK |
241 | * doesn't like being run in setuid/setgid programs (probably |
242 | * sensibly). So before we initialise GTK - and therefore before we |
243 | * even process the command line - we check to see if we're running |
244 | * set[ug]id. If so, we open our pty master _now_, chown it as |
245 | * necessary, and drop privileges. We can always close it again |
246 | * later. If we're potentially going to be doing utmp as well, we |
247 | * also fork off a utmp helper process and communicate with it by |
248 | * means of a pipe; the utmp helper will keep privileges in order |
249 | * to clean up utmp when we exit (i.e. when its end of our pipe |
250 | * closes). |
251 | */ |
252 | void pty_pre_init(void) |
253 | { |
254 | pid_t pid; |
255 | int pipefd[2]; |
256 | |
0f33f9d1 |
257 | /* set the child signal handler straight away; it needs to be set |
258 | * before we ever fork. */ |
259 | putty_signal(SIGCHLD, sigchld_handler); |
95c47834 |
260 | pty_master_fd = -1; |
261 | |
262 | if (geteuid() != getuid() || getegid() != getgid()) { |
263 | pty_open_master(); |
39baeaa4 |
264 | } |
265 | |
95c47834 |
266 | #ifndef OMIT_UTMP |
39baeaa4 |
267 | /* |
95c47834 |
268 | * Fork off the utmp helper. |
755a6d84 |
269 | */ |
95c47834 |
270 | if (pipe(pipefd) < 0) { |
271 | perror("pterm: pipe"); |
272 | exit(1); |
273 | } |
274 | pid = fork(); |
275 | if (pid < 0) { |
276 | perror("pterm: fork"); |
277 | exit(1); |
278 | } else if (pid == 0) { |
279 | char display[128], buffer[128]; |
280 | int dlen, ret; |
281 | |
282 | close(pipefd[1]); |
283 | /* |
284 | * Now sit here until we receive a display name from the |
285 | * other end of the pipe, and then stamp utmp. Unstamp utmp |
286 | * again, and exit, when the pipe closes. |
287 | */ |
288 | |
289 | dlen = 0; |
290 | while (1) { |
291 | |
292 | ret = read(pipefd[0], buffer, lenof(buffer)); |
293 | if (ret <= 0) { |
294 | cleanup_utmp(); |
0f33f9d1 |
295 | _exit(0); |
95c47834 |
296 | } else if (!pty_stamped_utmp) { |
297 | if (dlen < lenof(display)) |
298 | memcpy(display+dlen, buffer, |
299 | min(ret, lenof(display)-dlen)); |
300 | if (buffer[ret-1] == '\0') { |
301 | /* |
302 | * Now we have a display name. NUL-terminate |
303 | * it, and stamp utmp. |
304 | */ |
305 | display[lenof(display)-1] = '\0'; |
306 | /* |
307 | * Trap as many fatal signals as we can in the |
308 | * hope of having the best possible chance to |
309 | * clean up utmp before termination. We are |
310 | * unfortunately unprotected against SIGKILL, |
311 | * but that's life. |
312 | */ |
0f33f9d1 |
313 | putty_signal(SIGHUP, fatal_sig_handler); |
314 | putty_signal(SIGINT, fatal_sig_handler); |
315 | putty_signal(SIGQUIT, fatal_sig_handler); |
316 | putty_signal(SIGILL, fatal_sig_handler); |
317 | putty_signal(SIGABRT, fatal_sig_handler); |
318 | putty_signal(SIGFPE, fatal_sig_handler); |
319 | putty_signal(SIGPIPE, fatal_sig_handler); |
320 | putty_signal(SIGALRM, fatal_sig_handler); |
321 | putty_signal(SIGTERM, fatal_sig_handler); |
322 | putty_signal(SIGSEGV, fatal_sig_handler); |
323 | putty_signal(SIGUSR1, fatal_sig_handler); |
324 | putty_signal(SIGUSR2, fatal_sig_handler); |
755a6d84 |
325 | #ifdef SIGBUS |
0f33f9d1 |
326 | putty_signal(SIGBUS, fatal_sig_handler); |
755a6d84 |
327 | #endif |
328 | #ifdef SIGPOLL |
0f33f9d1 |
329 | putty_signal(SIGPOLL, fatal_sig_handler); |
755a6d84 |
330 | #endif |
331 | #ifdef SIGPROF |
0f33f9d1 |
332 | putty_signal(SIGPROF, fatal_sig_handler); |
755a6d84 |
333 | #endif |
334 | #ifdef SIGSYS |
0f33f9d1 |
335 | putty_signal(SIGSYS, fatal_sig_handler); |
755a6d84 |
336 | #endif |
337 | #ifdef SIGTRAP |
0f33f9d1 |
338 | putty_signal(SIGTRAP, fatal_sig_handler); |
755a6d84 |
339 | #endif |
340 | #ifdef SIGVTALRM |
0f33f9d1 |
341 | putty_signal(SIGVTALRM, fatal_sig_handler); |
755a6d84 |
342 | #endif |
343 | #ifdef SIGXCPU |
0f33f9d1 |
344 | putty_signal(SIGXCPU, fatal_sig_handler); |
755a6d84 |
345 | #endif |
346 | #ifdef SIGXFSZ |
0f33f9d1 |
347 | putty_signal(SIGXFSZ, fatal_sig_handler); |
755a6d84 |
348 | #endif |
349 | #ifdef SIGIO |
0f33f9d1 |
350 | putty_signal(SIGIO, fatal_sig_handler); |
95c47834 |
351 | #endif |
95c47834 |
352 | setup_utmp(pty_name, display); |
353 | } |
354 | } |
355 | } |
356 | } else { |
357 | close(pipefd[0]); |
358 | pty_utmp_helper_pid = pid; |
359 | pty_utmp_helper_pipe = pipefd[1]; |
360 | } |
755a6d84 |
361 | #endif |
95c47834 |
362 | |
363 | /* Drop privs. */ |
364 | { |
365 | int gid = getgid(), uid = getuid(); |
366 | #ifndef HAVE_NO_SETRESUID |
367 | int setresgid(gid_t, gid_t, gid_t); |
368 | int setresuid(uid_t, uid_t, uid_t); |
369 | setresgid(gid, gid, gid); |
370 | setresuid(uid, uid, uid); |
371 | #else |
372 | setgid(getgid()); |
373 | setuid(getuid()); |
374 | #endif |
375 | } |
376 | } |
377 | |
74aca06d |
378 | int pty_select_result(int fd, int event) |
379 | { |
380 | char buf[4096]; |
381 | int ret; |
382 | int finished = FALSE; |
383 | |
384 | if (fd == pty_master_fd && event == 1) { |
385 | |
386 | ret = read(pty_master_fd, buf, sizeof(buf)); |
387 | |
388 | /* |
389 | * Clean termination condition is that either ret == 0, or ret |
390 | * < 0 and errno == EIO. Not sure why the latter, but it seems |
391 | * to happen. Boo. |
392 | */ |
393 | if (ret == 0 || (ret < 0 && errno == EIO)) { |
394 | /* |
395 | * We assume a clean exit if the pty has closed but the |
396 | * actual child process hasn't. The only way I can |
397 | * imagine this happening is if it detaches itself from |
398 | * the pty and goes daemonic - in which case the |
399 | * expected usage model would precisely _not_ be for |
400 | * the pterm window to hang around! |
401 | */ |
402 | finished = TRUE; |
403 | if (!pty_child_dead) |
404 | pty_exit_code = 0; |
405 | } else if (ret < 0) { |
406 | perror("read pty master"); |
407 | exit(1); |
408 | } else if (ret > 0) { |
409 | from_backend(pty_frontend, 0, buf, ret); |
410 | } |
411 | } else if (fd == pty_signal_pipe[0]) { |
412 | pid_t pid; |
413 | int status; |
414 | char c[1]; |
415 | |
416 | read(pty_signal_pipe[0], c, 1); /* ignore its value; it'll be `x' */ |
417 | |
418 | do { |
419 | pid = waitpid(-1, &status, WNOHANG); |
420 | if (pid == pty_child_pid && |
421 | (WIFEXITED(status) || WIFSIGNALED(status))) { |
422 | /* |
423 | * The primary child process died. We could keep |
424 | * the terminal open for remaining subprocesses to |
425 | * output to, but conventional wisdom seems to feel |
426 | * that that's the Wrong Thing for an xterm-alike, |
427 | * so we bail out now (though we don't necessarily |
428 | * _close_ the window, depending on the state of |
429 | * Close On Exit). This would be easy enough to |
430 | * change or make configurable if necessary. |
431 | */ |
432 | pty_exit_code = status; |
433 | pty_child_dead = TRUE; |
434 | finished = TRUE; |
435 | } |
436 | } while(pid > 0); |
437 | } |
438 | |
439 | if (finished && !pty_finished) { |
440 | uxsel_del(pty_master_fd); |
441 | pty_close(); |
442 | pty_master_fd = -1; |
443 | |
444 | pty_finished = TRUE; |
445 | |
446 | /* |
447 | * This is a slight layering-violation sort of hack: only |
448 | * if we're not closing on exit (COE is set to Never, or to |
449 | * Only On Clean and it wasn't a clean exit) do we output a |
450 | * `terminated' message. |
451 | */ |
452 | if (pty_cfg.close_on_exit == FORCE_OFF || |
453 | (pty_cfg.close_on_exit == AUTO && pty_exit_code != 0)) { |
454 | char message[512]; |
455 | if (WIFEXITED(pty_exit_code)) |
456 | sprintf(message, "\r\n[pterm: process terminated with exit" |
457 | " code %d]\r\n", WEXITSTATUS(pty_exit_code)); |
458 | else if (WIFSIGNALED(pty_exit_code)) |
459 | #ifdef HAVE_NO_STRSIGNAL |
460 | sprintf(message, "\r\n[pterm: process terminated on signal" |
461 | " %d]\r\n", WTERMSIG(pty_exit_code)); |
462 | #else |
463 | sprintf(message, "\r\n[pterm: process terminated on signal" |
464 | " %d (%.400s)]\r\n", WTERMSIG(pty_exit_code), |
465 | strsignal(WTERMSIG(pty_exit_code))); |
466 | #endif |
467 | from_backend(pty_frontend, 0, message, strlen(message)); |
468 | } |
469 | } |
470 | return !finished; |
471 | } |
472 | |
473 | static void pty_uxsel_setup(void) |
474 | { |
475 | uxsel_set(pty_master_fd, 1, pty_select_result); |
476 | uxsel_set(pty_signal_pipe[0], 1, pty_select_result); |
477 | } |
478 | |
95c47834 |
479 | /* |
480 | * Called to set up the pty. |
481 | * |
482 | * Returns an error message, or NULL on success. |
483 | * |
484 | * Also places the canonical host name into `realhost'. It must be |
485 | * freed by the caller. |
486 | */ |
86916870 |
487 | static char *pty_init(void *frontend, void **backend_handle, Config *cfg, |
887035a5 |
488 | char *host, int port, char **realhost, int nodelay) |
95c47834 |
489 | { |
490 | int slavefd; |
491 | pid_t pid, pgrp; |
7af753e6 |
492 | long windowid; |
95c47834 |
493 | |
a8327734 |
494 | pty_frontend = frontend; |
28d00fe4 |
495 | *backend_handle = NULL; /* we can't sensibly use this, sadly */ |
496 | |
74aca06d |
497 | pty_cfg = *cfg; /* structure copy */ |
86916870 |
498 | pty_term_width = cfg->width; |
499 | pty_term_height = cfg->height; |
f278d6f8 |
500 | |
95c47834 |
501 | if (pty_master_fd < 0) |
502 | pty_open_master(); |
503 | |
504 | /* |
505 | * Set the backspace character to be whichever of ^H and ^? is |
506 | * specified by bksp_is_delete. |
507 | */ |
508 | { |
509 | struct termios attrs; |
510 | tcgetattr(pty_master_fd, &attrs); |
86916870 |
511 | attrs.c_cc[VERASE] = cfg->bksp_is_delete ? '\177' : '\010'; |
95c47834 |
512 | tcsetattr(pty_master_fd, TCSANOW, &attrs); |
513 | } |
514 | |
515 | /* |
516 | * Stamp utmp (that is, tell the utmp helper process to do so), |
517 | * or not. |
518 | */ |
86916870 |
519 | if (!cfg->stamp_utmp) |
95c47834 |
520 | close(pty_utmp_helper_pipe); /* just let the child process die */ |
521 | else { |
a8327734 |
522 | char *location = get_x_display(pty_frontend); |
95c47834 |
523 | int len = strlen(location)+1, pos = 0; /* +1 to include NUL */ |
524 | while (pos < len) { |
525 | int ret = write(pty_utmp_helper_pipe, location+pos, len - pos); |
526 | if (ret < 0) { |
527 | perror("pterm: writing to utmp helper process"); |
528 | close(pty_utmp_helper_pipe); /* arrgh, just give up */ |
529 | break; |
530 | } |
531 | pos += ret; |
532 | } |
533 | } |
755a6d84 |
534 | |
7af753e6 |
535 | windowid = get_windowid(pty_frontend); |
536 | |
755a6d84 |
537 | /* |
054d8535 |
538 | * Fork and execute the command. |
539 | */ |
540 | pid = fork(); |
541 | if (pid < 0) { |
542 | perror("fork"); |
88e6b9ca |
543 | exit(1); |
054d8535 |
544 | } |
545 | |
546 | if (pid == 0) { |
547 | int i; |
548 | /* |
549 | * We are the child. |
550 | */ |
d37c2d81 |
551 | |
95c47834 |
552 | slavefd = open(pty_name, O_RDWR); |
d37c2d81 |
553 | if (slavefd < 0) { |
554 | perror("slave pty: open"); |
0f33f9d1 |
555 | _exit(1); |
d37c2d81 |
556 | } |
557 | |
054d8535 |
558 | close(pty_master_fd); |
054d8535 |
559 | fcntl(slavefd, F_SETFD, 0); /* don't close on exec */ |
560 | dup2(slavefd, 0); |
561 | dup2(slavefd, 1); |
562 | dup2(slavefd, 2); |
563 | setsid(); |
d37c2d81 |
564 | ioctl(slavefd, TIOCSCTTY, 1); |
565 | pgrp = getpid(); |
566 | tcsetpgrp(slavefd, pgrp); |
567 | setpgrp(); |
95c47834 |
568 | close(open(pty_name, O_WRONLY, 0)); |
054d8535 |
569 | setpgrp(); |
054d8535 |
570 | /* Close everything _else_, for tidiness. */ |
571 | for (i = 3; i < 1024; i++) |
572 | close(i); |
fe9548aa |
573 | { |
86916870 |
574 | char term_env_var[10 + sizeof(cfg->termtype)]; |
575 | sprintf(term_env_var, "TERM=%s", cfg->termtype); |
fe9548aa |
576 | putenv(term_env_var); |
577 | } |
7af753e6 |
578 | { |
579 | char windowid_env_var[40]; |
580 | sprintf(windowid_env_var, "WINDOWID=%ld", windowid); |
581 | putenv(windowid_env_var); |
582 | } |
8bf5c419 |
583 | /* |
584 | * SIGINT and SIGQUIT may have been set to ignored by our |
585 | * parent, particularly by things like sh -c 'pterm &' and |
586 | * some window managers. Reverse this for our child process. |
587 | */ |
0f33f9d1 |
588 | putty_signal(SIGINT, SIG_DFL); |
589 | putty_signal(SIGQUIT, SIG_DFL); |
6169c758 |
590 | if (pty_argv) |
591 | execvp(pty_argv[0], pty_argv); |
c8ee61b9 |
592 | else { |
593 | char *shell = getenv("SHELL"); |
594 | char *shellname; |
86916870 |
595 | if (cfg->login_shell) { |
c8ee61b9 |
596 | char *p = strrchr(shell, '/'); |
3d88e64d |
597 | shellname = snewn(2+strlen(shell), char); |
c8ee61b9 |
598 | p = p ? p+1 : shell; |
599 | sprintf(shellname, "-%s", p); |
600 | } else |
601 | shellname = shell; |
602 | execl(getenv("SHELL"), shellname, NULL); |
603 | } |
604 | |
054d8535 |
605 | /* |
606 | * If we're here, exec has gone badly foom. |
607 | */ |
608 | perror("exec"); |
0f33f9d1 |
609 | _exit(127); |
054d8535 |
610 | } else { |
a0e16eb1 |
611 | pty_child_pid = pid; |
612 | pty_child_dead = FALSE; |
74aca06d |
613 | pty_finished = FALSE; |
0f33f9d1 |
614 | } |
054d8535 |
615 | |
74aca06d |
616 | if (pipe(pty_signal_pipe) < 0) { |
617 | perror("pipe"); |
618 | exit(1); |
619 | } |
620 | pty_uxsel_setup(); |
621 | |
1709795f |
622 | return NULL; |
623 | } |
624 | |
625 | /* |
86916870 |
626 | * Stub routine (we don't have any need to reconfigure this backend). |
627 | */ |
628 | static void pty_reconfig(void *handle, Config *cfg) |
629 | { |
630 | } |
631 | |
632 | /* |
cb60dcb3 |
633 | * Stub routine (never called in pterm |
634 | */ |
635 | static void pty_free(void *handle) |
636 | { |
637 | } |
638 | |
639 | |
640 | /* |
1709795f |
641 | * Called to send data down the pty. |
642 | */ |
28d00fe4 |
643 | static int pty_send(void *handle, char *buf, int len) |
1709795f |
644 | { |
90cfd8f4 |
645 | if (pty_master_fd < 0) |
646 | return 0; /* ignore all writes if fd closed */ |
647 | |
054d8535 |
648 | while (len > 0) { |
649 | int ret = write(pty_master_fd, buf, len); |
650 | if (ret < 0) { |
651 | perror("write pty master"); |
652 | exit(1); |
653 | } |
654 | buf += ret; |
655 | len -= ret; |
656 | } |
1709795f |
657 | return 0; |
658 | } |
659 | |
74aca06d |
660 | static void pty_close(void) |
90cfd8f4 |
661 | { |
662 | if (pty_master_fd >= 0) { |
663 | close(pty_master_fd); |
664 | pty_master_fd = -1; |
665 | } |
666 | close(pty_utmp_helper_pipe); /* this causes utmp to be cleaned up */ |
667 | } |
668 | |
1709795f |
669 | /* |
670 | * Called to query the current socket sendability status. |
671 | */ |
28d00fe4 |
672 | static int pty_sendbuffer(void *handle) |
1709795f |
673 | { |
674 | return 0; |
675 | } |
676 | |
677 | /* |
678 | * Called to set the size of the window |
679 | */ |
28d00fe4 |
680 | static void pty_size(void *handle, int width, int height) |
1709795f |
681 | { |
88e6b9ca |
682 | struct winsize size; |
683 | |
f278d6f8 |
684 | pty_term_width = width; |
685 | pty_term_height = height; |
686 | |
687 | size.ws_row = (unsigned short)pty_term_height; |
688 | size.ws_col = (unsigned short)pty_term_width; |
a8327734 |
689 | size.ws_xpixel = (unsigned short) pty_term_width * |
690 | font_dimension(pty_frontend, 0); |
691 | size.ws_ypixel = (unsigned short) pty_term_height * |
692 | font_dimension(pty_frontend, 1); |
88e6b9ca |
693 | ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size); |
1709795f |
694 | return; |
695 | } |
696 | |
697 | /* |
698 | * Send special codes. |
699 | */ |
28d00fe4 |
700 | static void pty_special(void *handle, Telnet_Special code) |
1709795f |
701 | { |
702 | /* Do nothing! */ |
703 | return; |
704 | } |
705 | |
28d00fe4 |
706 | static Socket pty_socket(void *handle) |
1709795f |
707 | { |
708 | return NULL; /* shouldn't ever be needed */ |
709 | } |
710 | |
28d00fe4 |
711 | static int pty_sendok(void *handle) |
1709795f |
712 | { |
713 | return 1; |
714 | } |
715 | |
28d00fe4 |
716 | static void pty_unthrottle(void *handle, int backlog) |
1709795f |
717 | { |
718 | /* do nothing */ |
719 | } |
720 | |
28d00fe4 |
721 | static int pty_ldisc(void *handle, int option) |
1709795f |
722 | { |
723 | return 0; /* neither editing nor echoing */ |
724 | } |
725 | |
b9d7bcad |
726 | static void pty_provide_ldisc(void *handle, void *ldisc) |
727 | { |
728 | /* This is a stub. */ |
729 | } |
730 | |
a8327734 |
731 | static void pty_provide_logctx(void *handle, void *logctx) |
732 | { |
733 | /* This is a stub. */ |
734 | } |
735 | |
28d00fe4 |
736 | static int pty_exitcode(void *handle) |
1709795f |
737 | { |
74aca06d |
738 | if (!pty_finished) |
90cfd8f4 |
739 | return -1; /* not dead yet */ |
740 | else |
741 | return pty_exit_code; |
1709795f |
742 | } |
743 | |
744 | Backend pty_backend = { |
745 | pty_init, |
cb60dcb3 |
746 | pty_free, |
86916870 |
747 | pty_reconfig, |
1709795f |
748 | pty_send, |
749 | pty_sendbuffer, |
750 | pty_size, |
751 | pty_special, |
752 | pty_socket, |
753 | pty_exitcode, |
754 | pty_sendok, |
755 | pty_ldisc, |
b9d7bcad |
756 | pty_provide_ldisc, |
a8327734 |
757 | pty_provide_logctx, |
1709795f |
758 | pty_unthrottle, |
759 | 1 |
760 | }; |